00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2005 INRIA 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 00021 #ifndef IPV4_HEADER_H 00022 #define IPV4_HEADER_H 00023 00024 #include "ns3/header.h" 00025 #include "ns3/ipv4-address.h" 00026 00027 namespace ns3 { 00028 /** 00029 * \brief Packet header for IPv4 00030 */ 00031 class Ipv4Header : public Header 00032 { 00033 public: 00034 /** 00035 * \brief Construct a null IPv4 header 00036 */ 00037 Ipv4Header (); 00038 /** 00039 * \brief Enable checksum calculation for this header. 00040 */ 00041 void EnableChecksum (void); 00042 /** 00043 * \param size the size of the payload in bytes 00044 */ 00045 void SetPayloadSize (uint16_t size); 00046 /** 00047 * \param identification the Identification field of IPv4 packets. 00048 * 00049 * By default, set to zero. 00050 */ 00051 void SetIdentification (uint16_t identification); 00052 /** 00053 * \param tos the 8 bits of Ipv4 TOS. 00054 */ 00055 void SetTos (uint8_t tos); 00056 /** 00057 * This packet is not the last packet of a fragmented ipv4 packet. 00058 */ 00059 void SetMoreFragments (void); 00060 /** 00061 * This packet is the last packet of a fragmented ipv4 packet. 00062 */ 00063 void SetLastFragment (void); 00064 /** 00065 * Don't fragment this packet: if you need to anyway, drop it. 00066 */ 00067 void SetDontFragment (void); 00068 /** 00069 * If you need to fragment this packet, you can do it. 00070 */ 00071 void SetMayFragment (void); 00072 /** 00073 * \param offset the ipv4 fragment offset 00074 */ 00075 void SetFragmentOffset (uint16_t offset); 00076 /** 00077 * \param ttl the ipv4 TTL 00078 */ 00079 void SetTtl (uint8_t ttl); 00080 /** 00081 * \param num the ipv4 protocol field 00082 */ 00083 void SetProtocol (uint8_t num); 00084 /** 00085 * \param source the source of this packet 00086 */ 00087 void SetSource (Ipv4Address source); 00088 /** 00089 * \param destination the destination of this packet. 00090 */ 00091 void SetDestination (Ipv4Address destination); 00092 /** 00093 * \returns the size of the payload in bytes 00094 */ 00095 uint16_t GetPayloadSize (void) const; 00096 /** 00097 * \returns the identification field of this packet. 00098 */ 00099 uint16_t GetIdentification (void) const; 00100 /** 00101 * \returns the TOS field of this packet. 00102 */ 00103 uint8_t GetTos (void) const; 00104 /** 00105 * \returns true if this is the last fragment of a packet, false otherwise. 00106 */ 00107 bool IsLastFragment (void) const; 00108 /** 00109 * \returns true if this is this packet can be fragmented. 00110 */ 00111 bool IsDontFragment (void) const; 00112 /** 00113 * \returns the offset of this fragment. 00114 */ 00115 uint16_t GetFragmentOffset (void) const; 00116 /** 00117 * \returns the TTL field of this packet 00118 */ 00119 uint8_t GetTtl (void) const; 00120 /** 00121 * \returns the protocol field of this packet 00122 */ 00123 uint8_t GetProtocol (void) const; 00124 /** 00125 * \returns the source address of this packet 00126 */ 00127 Ipv4Address GetSource (void) const; 00128 /** 00129 * \returns the destination address of this packet 00130 */ 00131 Ipv4Address GetDestination (void) const; 00132 00133 /** 00134 * \returns true if the ipv4 checksum is correct, false otherwise. 00135 * 00136 * If Ipv4Header::EnableChecksums has not been called prior to 00137 * deserializing this header, this method will always return true. 00138 */ 00139 bool IsChecksumOk (void) const; 00140 00141 static TypeId GetTypeId (void); 00142 virtual TypeId GetInstanceTypeId (void) const; 00143 virtual void Print (std::ostream &os) const; 00144 virtual uint32_t GetSerializedSize (void) const; 00145 virtual void Serialize (Buffer::Iterator start) const; 00146 virtual uint32_t Deserialize (Buffer::Iterator start); 00147 private: 00148 00149 enum FlagsE { 00150 DONT_FRAGMENT = (1<<0), 00151 MORE_FRAGMENTS = (1<<1) 00152 }; 00153 00154 bool m_calcChecksum; 00155 00156 uint16_t m_payloadSize; 00157 uint16_t m_identification; 00158 uint32_t m_tos : 8; 00159 uint32_t m_ttl : 8; 00160 uint32_t m_protocol : 8; 00161 uint32_t m_flags : 3; 00162 uint16_t m_fragmentOffset : 13; 00163 Ipv4Address m_source; 00164 Ipv4Address m_destination; 00165 uint16_t m_checksum; 00166 bool m_goodChecksum; 00167 }; 00168 00169 } // namespace ns3 00170 00171 00172 #endif /* IPV4_HEADER_H */