A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-header.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
20#ifndef IPV4_HEADER_H
21#define IPV4_HEADER_H
22
23#include "ns3/header.h"
24#include "ns3/ipv4-address.h"
25
26namespace ns3
27{
28/**
29 * \ingroup ipv4
30 *
31 * \brief Packet header for IPv4
32 */
33class Ipv4Header : public Header
34{
35 public:
36 /**
37 * \brief Construct a null IPv4 header
38 */
39 Ipv4Header();
40 /**
41 * \brief Enable checksum calculation for this header.
42 */
43 void EnableChecksum();
44 /**
45 * \param size the size of the payload in bytes
46 */
47 void SetPayloadSize(uint16_t size);
48 /**
49 * \param identification the Identification field of IPv4 packets.
50 *
51 * By default, set to zero.
52 */
53 void SetIdentification(uint16_t identification);
54 /**
55 * \param tos the 8 bits of Ipv4 TOS.
56 */
57 void SetTos(uint8_t tos);
58
59 public:
60 /**
61 * \enum DscpType
62 * \brief DiffServ codepoints
63 *
64 * The values correspond to the 6-bit DSCP codepoint within the 8-bit
65 * DS field defined in \RFC{2474}. ECN bits are separately set with the
66 * SetEcn() method. Codepoints are defined in
67 * Assured Forwarding (AF) \RFC{2597},
68 * Expedited Forwarding (EF) \RFC{2598}, and
69 * Default and Class Selector (CS) \RFC{2474}.
70 */
72 {
74
75 // Prefixed with "DSCP" to avoid name clash (bug 1723)
76 DSCP_CS1 = 0x08, // octal 010
77 DSCP_AF11 = 0x0A, // octal 012
78 DSCP_AF12 = 0x0C, // octal 014
79 DSCP_AF13 = 0x0E, // octal 016
80
81 DSCP_CS2 = 0x10, // octal 020
82 DSCP_AF21 = 0x12, // octal 022
83 DSCP_AF22 = 0x14, // octal 024
84 DSCP_AF23 = 0x16, // octal 026
85
86 DSCP_CS3 = 0x18, // octal 030
87 DSCP_AF31 = 0x1A, // octal 032
88 DSCP_AF32 = 0x1C, // octal 034
89 DSCP_AF33 = 0x1E, // octal 036
90
91 DSCP_CS4 = 0x20, // octal 040
92 DSCP_AF41 = 0x22, // octal 042
93 DSCP_AF42 = 0x24, // octal 044
94 DSCP_AF43 = 0x26, // octal 046
95
96 DSCP_CS5 = 0x28, // octal 050
97 DSCP_EF = 0x2E, // octal 056
98
99 DSCP_CS6 = 0x30, // octal 060
100 DSCP_CS7 = 0x38 // octal 070
101 };
102
103 /**
104 * \brief Set DSCP Field
105 * \param dscp DSCP value
106 */
107 void SetDscp(DscpType dscp);
108
109 /**
110 * \enum EcnType
111 * \brief ECN Type defined in \RFC{3168}
112 */
114 {
115 // Prefixed with "ECN" to avoid name clash (bug 1723)
117 ECN_ECT1 = 0x01,
118 ECN_ECT0 = 0x02,
119 ECN_CE = 0x03
120 };
121
122 /**
123 * \brief Set ECN Field
124 * \param ecn ECN Type
125 */
126 void SetEcn(EcnType ecn);
127 /**
128 * This packet is not the last packet of a fragmented ipv4 packet.
129 */
130 void SetMoreFragments();
131 /**
132 * This packet is the last packet of a fragmented ipv4 packet.
133 */
134 void SetLastFragment();
135 /**
136 * Don't fragment this packet: if you need to anyway, drop it.
137 */
138 void SetDontFragment();
139 /**
140 * If you need to fragment this packet, you can do it.
141 */
142 void SetMayFragment();
143 /**
144 * The offset is measured in bytes for the packet start.
145 * Mind that IPv4 "fragment offset" field is 13 bits long and is measured in 8-bytes words.
146 * Hence, the function does enforce that the offset is a multiple of 8.
147 * \param offsetBytes the ipv4 fragment offset measured in bytes from the start.
148 */
149 void SetFragmentOffset(uint16_t offsetBytes);
150 /**
151 * \param ttl the ipv4 TTL
152 */
153 void SetTtl(uint8_t ttl);
154 /**
155 * \param num the ipv4 protocol field
156 */
157 void SetProtocol(uint8_t num);
158 /**
159 * \param source the source of this packet
160 */
161 void SetSource(Ipv4Address source);
162 /**
163 * \param destination the destination of this packet.
164 */
165 void SetDestination(Ipv4Address destination);
166 /**
167 * \returns the size of the payload in bytes
168 */
169 uint16_t GetPayloadSize() const;
170 /**
171 * \returns the identification field of this packet.
172 */
173 uint16_t GetIdentification() const;
174 /**
175 * \returns the TOS field of this packet.
176 */
177 uint8_t GetTos() const;
178 /**
179 * \returns the DSCP field of this packet.
180 */
181 DscpType GetDscp() const;
182 /**
183 * \param dscp the dscp
184 * \returns std::string of DSCPType
185 */
186 std::string DscpTypeToString(DscpType dscp) const;
187 /**
188 * \returns the ECN field of this packet.
189 */
190 EcnType GetEcn() const;
191 /**
192 * \param ecn the ECNType
193 * \returns std::string of ECNType
194 */
195 std::string EcnTypeToString(EcnType ecn) const;
196 /**
197 * \returns true if this is the last fragment of a packet, false otherwise.
198 */
199 bool IsLastFragment() const;
200 /**
201 * \returns true if this is this packet can be fragmented.
202 */
203 bool IsDontFragment() const;
204 /**
205 * \returns the offset of this fragment measured in bytes from the start.
206 */
207 uint16_t GetFragmentOffset() const;
208 /**
209 * \returns the TTL field of this packet
210 */
211 uint8_t GetTtl() const;
212 /**
213 * \returns the protocol field of this packet
214 */
215 uint8_t GetProtocol() const;
216 /**
217 * \returns the source address of this packet
218 */
219 Ipv4Address GetSource() const;
220 /**
221 * \returns the destination address of this packet
222 */
224
225 /**
226 * \returns true if the ipv4 checksum is correct, false otherwise.
227 *
228 * If Ipv4Header::EnableChecksums has not been called prior to
229 * deserializing this header, this method will always return true.
230 */
231 bool IsChecksumOk() const;
232
233 /**
234 * \brief Get the type ID.
235 * \return the object TypeId
236 */
237 static TypeId GetTypeId();
238 TypeId GetInstanceTypeId() const override;
239 void Print(std::ostream& os) const override;
240 uint32_t GetSerializedSize() const override;
241 void Serialize(Buffer::Iterator start) const override;
242 uint32_t Deserialize(Buffer::Iterator start) override;
243
244 private:
245 /// flags related to IP fragmentation
247 {
248 DONT_FRAGMENT = (1 << 0),
249 MORE_FRAGMENTS = (1 << 1)
250 };
251
252 bool m_calcChecksum; //!< true if the checksum must be calculated
253
254 uint16_t m_payloadSize; //!< payload size
255 uint16_t m_identification; //!< identification
256 uint32_t m_tos : 8; //!< TOS, also used as DSCP + ECN value
257 uint32_t m_ttl : 8; //!< TTL
258 uint32_t m_protocol : 8; //!< Protocol
259 uint32_t m_flags : 3; //!< flags
260 uint16_t m_fragmentOffset; //!< Fragment offset
261 Ipv4Address m_source; //!< source address
262 Ipv4Address m_destination; //!< destination address
263 uint16_t m_checksum; //!< checksum
264 bool m_goodChecksum; //!< true if checksum is correct
265 uint16_t m_headerSize; //!< IP header size
266};
267
268} // namespace ns3
269
270#endif /* IPV4_HEADER_H */
iterator in a Buffer instance
Definition: buffer.h:100
Protocol header serialization and deserialization.
Definition: header.h:44
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Packet header for IPv4.
Definition: ipv4-header.h:34
Ipv4Address m_source
source address
Definition: ipv4-header.h:261
bool IsChecksumOk() const
Definition: ipv4-header.cc:323
void Print(std::ostream &os) const override
Definition: ipv4-header.cc:347
void SetDestination(Ipv4Address destination)
Definition: ipv4-header.cc:309
Ipv4Address GetSource() const
Definition: ipv4-header.cc:302
uint16_t m_fragmentOffset
Fragment offset.
Definition: ipv4-header.h:260
void SetDontFragment()
Don't fragment this packet: if you need to anyway, drop it.
Definition: ipv4-header.cc:224
std::string EcnTypeToString(EcnType ecn) const
Definition: ipv4-header.cc:177
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: ipv4-header.cc:340
FlagsE
flags related to IP fragmentation
Definition: ipv4-header.h:247
uint8_t GetTos() const
Definition: ipv4-header.cc:196
void SetLastFragment()
This packet is the last packet of a fragmented ipv4 packet.
Definition: ipv4-header.cc:210
uint32_t m_ttl
TTL.
Definition: ipv4-header.h:257
uint32_t m_protocol
Protocol.
Definition: ipv4-header.h:258
void SetPayloadSize(uint16_t size)
Definition: ipv4-header.cc:57
EcnType GetEcn() const
Definition: ipv4-header.cc:169
uint16_t m_identification
identification
Definition: ipv4-header.h:255
uint16_t GetIdentification() const
Definition: ipv4-header.cc:71
bool IsDontFragment() const
Definition: ipv4-header.cc:238
uint8_t GetProtocol() const
Definition: ipv4-header.cc:281
uint16_t m_payloadSize
payload size
Definition: ipv4-header.h:254
void SetTtl(uint8_t ttl)
Definition: ipv4-header.cc:267
bool IsLastFragment() const
Definition: ipv4-header.cc:217
uint32_t m_tos
TOS, also used as DSCP + ECN value.
Definition: ipv4-header.h:256
bool m_goodChecksum
true if checksum is correct
Definition: ipv4-header.h:264
void SetMoreFragments()
This packet is not the last packet of a fragmented ipv4 packet.
Definition: ipv4-header.cc:203
Ipv4Address GetDestination() const
Definition: ipv4-header.cc:316
void Serialize(Buffer::Iterator start) const override
Definition: ipv4-header.cc:392
void SetDscp(DscpType dscp)
Set DSCP Field.
Definition: ipv4-header.cc:92
Ipv4Address m_destination
destination address
Definition: ipv4-header.h:262
void SetMayFragment()
If you need to fragment this packet, you can do it.
Definition: ipv4-header.cc:231
EcnType
ECN Type defined in RFC 3168
Definition: ipv4-header.h:114
uint16_t m_checksum
checksum
Definition: ipv4-header.h:263
uint16_t m_headerSize
IP header size.
Definition: ipv4-header.h:265
DscpType GetDscp() const
Definition: ipv4-header.cc:108
uint16_t GetPayloadSize() const
Definition: ipv4-header.cc:64
void SetEcn(EcnType ecn)
Set ECN Field.
Definition: ipv4-header.cc:100
DscpType
DiffServ codepoints.
Definition: ipv4-header.h:72
std::string DscpTypeToString(DscpType dscp) const
Definition: ipv4-header.cc:116
uint32_t m_flags
flags
Definition: ipv4-header.h:259
uint32_t GetSerializedSize() const override
Definition: ipv4-header.cc:384
uint16_t GetFragmentOffset() const
Definition: ipv4-header.cc:254
void SetProtocol(uint8_t num)
Definition: ipv4-header.cc:288
void SetFragmentOffset(uint16_t offsetBytes)
The offset is measured in bytes for the packet start.
Definition: ipv4-header.cc:245
void SetIdentification(uint16_t identification)
Definition: ipv4-header.cc:78
static TypeId GetTypeId()
Get the type ID.
Definition: ipv4-header.cc:330
uint8_t GetTtl() const
Definition: ipv4-header.cc:274
void EnableChecksum()
Enable checksum calculation for this header.
Definition: ipv4-header.cc:50
void SetTos(uint8_t tos)
Definition: ipv4-header.cc:85
uint32_t Deserialize(Buffer::Iterator start) override
Definition: ipv4-header.cc:433
void SetSource(Ipv4Address source)
Definition: ipv4-header.cc:295
Ipv4Header()
Construct a null IPv4 header.
Definition: ipv4-header.cc:34
bool m_calcChecksum
true if the checksum must be calculated
Definition: ipv4-header.h:252
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.