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