A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp-header.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 UPB
3 * Copyright (c) 2017 NITK Surathkal
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Radu Lupu <rlupu@elcom.pub.ro>
8 * Ankit Deepak <adadeepak8@gmail.com>
9 * Deepti Rajagopal <deeptir96@gmail.com>
10 *
11 */
12
13#ifndef DHCP_HEADER_H
14#define DHCP_HEADER_H
15
16#include "ns3/address.h"
17#include "ns3/buffer.h"
18#include "ns3/header.h"
19#include "ns3/ipv4-address.h"
20
21#include <array>
22
23/**
24 * @file
25 * @ingroup internet-apps
26 * DhcpHeader classes declaration
27 */
28
29namespace ns3
30{
31
32/**
33 * @ingroup internet-apps
34 * @defgroup dhcp DHCPv4 Client and Server
35 */
36
37/**
38 * @ingroup dhcp
39 *
40 * This is the Chaddr field, which is 16 bytes long. Originally it was
41 * meant to hold the Client Hardware Address (hence the name), but later RFCs
42 * refers to it just as the Client Identifier.
43 */
44using DhcpChaddr = std::array<uint8_t, 16>;
45
46/**
47 * @ingroup dhcp
48 * Function to pretty-print a Chaddr.
49 *
50 * @param chaddr the Chaddr
51 * @return A formatted string
52 */
53std::string DhcpChaddrToString(const DhcpChaddr& chaddr);
54
55/**
56 * @ingroup dhcp
57 *
58 * @class DhcpHeader
59 * @brief BOOTP header with DHCP messages. This supports the following options:
60 * Subnet Mask (1), Address Request (50), Refresh Lease Time (51),
61 * DHCP Message Type (53), DHCP Server ID (54), Renew Time (58),
62 * Rebind Time (59) and End (255) of BOOTP
63 *
64 * @verbatim
65 0 1 2 3
66 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
67 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 | op (1) | htype (1) | hlen (1) | hops (1) |
69 +---------------+---------------+---------------+---------------+
70 | xid (4) |
71 +-------------------------------+-------------------------------+
72 | secs (2) | flags (2) |
73 +-------------------------------+-------------------------------+
74 | ciaddr (4) |
75 +---------------------------------------------------------------+
76 | yiaddr (4) |
77 +---------------------------------------------------------------+
78 | siaddr (4) |
79 +---------------------------------------------------------------+
80 | giaddr (4) |
81 +---------------------------------------------------------------+
82 | |
83 | chaddr (16) |
84 | |
85 | |
86 +---------------------------------------------------------------+
87 | |
88 | sname (64) |
89 +---------------------------------------------------------------+
90 | |
91 | file (128) |
92 +---------------------------------------------------------------+
93 | |
94 | options (variable) |
95 +---------------------------------------------------------------+
96 \endverbatim
97 */
98class DhcpHeader : public Header
99{
100 public:
101 /**
102 * @brief Get the type ID.
103 * @return the object TypeId
104 */
105 static TypeId GetTypeId();
106
107 /**
108 * @brief Constructor
109 */
110 DhcpHeader();
111
112 /**
113 * @brief Destructor
114 */
115 ~DhcpHeader() override;
116
117 /// BOOTP options
119 {
120 OP_MASK = 1, //!< BOOTP Option 1: Address Mask
121 OP_ROUTE = 3, //!< BOOTP Option 3: Router Option
122 OP_ADDREQ = 50, //!< BOOTP Option 50: Requested Address
123 OP_LEASE = 51, //!< BOOTP Option 51: Address Lease Time
124 OP_MSGTYPE = 53, //!< BOOTP Option 53: DHCP Message Type
125 OP_SERVID = 54, //!< BOOTP Option 54: Server Identifier
126 OP_RENEW = 58, //!< BOOTP Option 58: Address Renewal Time
127 OP_REBIND = 59, //!< BOOTP Option 59: Address Rebind Time
128 OP_END = 255 //!< BOOTP Option 255: END
129 };
130
131 /// DHCP messages
133 {
134 DHCPDISCOVER = 0, //!< Code for DHCP Discover
135 DHCPOFFER = 1, //!< Code for DHCP Offer
136 DHCPREQ = 2, //!< Code for DHCP Request
137 DHCPACK = 4, //!< Code for DHCP ACK
138 DHCPNACK = 5 //!< Code for DHCP NACK
139 };
140
141 /**
142 * @brief Set the type of BOOTP and DHCP messages
143 * @param type The type of message
144 */
145 void SetType(uint8_t type);
146
147 /**
148 * @brief Return the type of DHCP message
149 * @return The type of message
150 */
151 uint8_t GetType() const;
152
153 /**
154 * @brief Set the hardware information
155 * @param htype Hardware type
156 * @param hlen Hardware length
157 */
158 void SetHWType(uint8_t htype, uint8_t hlen);
159
160 /**
161 * @brief Set the transaction ID
162 * @param tran The transaction number
163 */
164 void SetTran(uint32_t tran);
165
166 /**
167 * @brief Get the transaction id
168 * @return The transaction id
169 */
170 uint32_t GetTran() const;
171
172 /**
173 * @brief Set the time when message is sent
174 */
175 void SetTime();
176
177 /**
178 * @brief Set the Chaddress of the device.
179 *
180 * @param addr Address of the device
181 */
182 void SetChaddr(DhcpChaddr addr);
183
184 /**
185 * @brief Get the Address of the client.
186 *
187 * Note: the address is always 16-bytes long.
188 *
189 * @return Address of the client
190 */
192
193 /**
194 * @brief Set the IPv4Address of the client
195 * @param addr The client Ipv4Address
196 */
197 void SetYiaddr(Ipv4Address addr);
198
199 /**
200 * @brief Get the IPv4Address of the client
201 * @return IPv4Address of the client
202 */
203 Ipv4Address GetYiaddr() const;
204
205 /**
206 * @brief Set the DHCP server information
207 * @param addr IPv4Address of the server
208 */
209 void SetDhcps(Ipv4Address addr);
210
211 /**
212 * @brief Get the information about the DHCP server
213 * @return IPv4Address of DHCP server
214 */
215 Ipv4Address GetDhcps() const;
216
217 /**
218 * @brief Set the Ipv4Address requested by the client
219 * @param addr Ipv4Address requested by the client
220 */
221 void SetReq(Ipv4Address addr);
222
223 /**
224 * @brief Get the IPv4Address requested by the client
225 * @return IPv4Address requested by the client
226 */
227 Ipv4Address GetReq() const;
228
229 /**
230 * @brief Set the mask of the IPv4Address
231 * @param addr 32 bit mask
232 */
233 void SetMask(uint32_t addr);
234
235 /**
236 * @brief Return the mask of the network
237 * @return 32 bit mask
238 */
239 uint32_t GetMask() const;
240
241 /**
242 * @brief Set the Ipv4Address of gateway to be used
243 * @param addr The Ipv4Address of the gateway
244 */
245 void SetRouter(Ipv4Address addr);
246
247 /**
248 * @brief Return the Ipv4Address of gateway to be used
249 * @return The Ipv4Address of the gateway
250 */
251 Ipv4Address GetRouter() const;
252
253 /**
254 * @brief Set the lease time of the IPv4Address
255 * @param time 32 bit time
256 */
257 void SetLease(uint32_t time);
258
259 /**
260 * @brief Return the lease time of the IPv4Address
261 * @return 32 bit time
262 */
263 uint32_t GetLease() const;
264
265 /**
266 * @brief Set the Renewal time of the IPv4Address
267 * @param time 32 bit time
268 */
269 void SetRenew(uint32_t time);
270
271 /**
272 * @brief Return the Renewal time of the address
273 * @return 32 bit time
274 */
275 uint32_t GetRenew() const;
276
277 /**
278 * @brief Set the Rebind time of the IPv4Address
279 * @param time 32 bit time
280 */
281 void SetRebind(uint32_t time);
282
283 /**
284 * @brief Return the Rebind time of the address
285 * @return 32 bit time
286 */
287 uint32_t GetRebind() const;
288
289 /**
290 * @brief Reset the BOOTP options
291 */
292 void ResetOpt();
293
294 private:
295 TypeId GetInstanceTypeId() const override;
296 void Print(std::ostream& os) const override;
297 uint32_t GetSerializedSize() const override;
298 void Serialize(Buffer::Iterator start) const override;
299 uint32_t Deserialize(Buffer::Iterator start) override;
300
301 uint8_t m_op; //!< The DHCP Message type
302 uint8_t m_bootp; //!< The BOOTP Message type
303 uint8_t m_hType; //!< The hardware type
304 uint8_t m_hLen; //!< The hardware length
305 uint8_t m_hops; //!< The number of hops covered by the message
306 uint32_t m_xid; //!< The transaction number
307 uint32_t m_mask; //!< The mask of the network
308 uint32_t m_len; //!< The length of the header
309 uint16_t m_secs; //!< Seconds elapsed
310 uint16_t m_flags; //!< BOOTP flags
311 DhcpChaddr m_chaddr; //!< The client identifier
312 Ipv4Address m_yiAddr; //!< Your (client) IP address
313 Ipv4Address m_ciAddr; //!< The IP address of the client
314 Ipv4Address m_siAddr; //!< Next Server IP address
315 Ipv4Address m_giAddr; //!< Relay Agent IP address
316 Ipv4Address m_dhcps; //!< DHCP server IP address
317 Ipv4Address m_req; //!< Requested Address
318 Ipv4Address m_route; //!< Router Option Address
319 uint8_t m_sname[64]; //!< Server name (Padded for now)
320 uint8_t m_file[128]; //!< File name (Padded for now)
321 uint8_t m_magic_cookie[4]; //!< DHCP Magic Cookie
322 uint32_t m_lease; //!< The lease time of the address
323 uint32_t m_renew; //!< The renewal time for the client
324 uint32_t m_rebind; //!< The rebinding time for the client
325 bool m_opt[255]; //!< BOOTP option list
326};
327
328} // namespace ns3
329
330#endif /* DHCP_HEADER_H */
iterator in a Buffer instance
Definition buffer.h:89
uint32_t GetLease() const
Return the lease time of the IPv4Address.
void SetTime()
Set the time when message is sent.
Ipv4Address m_ciAddr
The IP address of the client.
Ipv4Address GetReq() const
Get the IPv4Address requested by the client.
uint32_t m_rebind
The rebinding time for the client.
void Serialize(Buffer::Iterator start) const override
Ipv4Address m_giAddr
Relay Agent IP address.
void ResetOpt()
Reset the BOOTP options.
uint8_t m_hops
The number of hops covered by the message.
Ipv4Address GetRouter() const
Return the Ipv4Address of gateway to be used.
uint8_t m_bootp
The BOOTP Message type.
uint32_t m_lease
The lease time of the address.
void SetType(uint8_t type)
Set the type of BOOTP and DHCP messages.
Messages
DHCP messages.
@ DHCPACK
Code for DHCP ACK.
@ DHCPOFFER
Code for DHCP Offer.
@ DHCPDISCOVER
Code for DHCP Discover.
@ DHCPREQ
Code for DHCP Request.
@ DHCPNACK
Code for DHCP NACK.
void SetTran(uint32_t tran)
Set the transaction ID.
uint8_t m_sname[64]
Server name (Padded for now).
Ipv4Address GetDhcps() const
Get the information about the DHCP server.
Ipv4Address m_yiAddr
Your (client) IP address.
~DhcpHeader() override
Destructor.
void SetYiaddr(Ipv4Address addr)
Set the IPv4Address of the client.
DhcpChaddr m_chaddr
The client identifier.
uint32_t m_len
The length of the header.
void SetDhcps(Ipv4Address addr)
Set the DHCP server information.
uint16_t m_secs
Seconds elapsed.
uint32_t GetMask() const
Return the mask of the network.
uint32_t m_mask
The mask of the network.
Ipv4Address m_dhcps
DHCP server IP address.
Ipv4Address m_siAddr
Next Server IP address.
uint8_t GetType() const
Return the type of DHCP message.
void SetRenew(uint32_t time)
Set the Renewal time of the IPv4Address.
uint32_t GetTran() const
Get the transaction id.
bool m_opt[255]
BOOTP option list.
uint8_t m_magic_cookie[4]
DHCP Magic Cookie.
void SetLease(uint32_t time)
Set the lease time of the IPv4Address.
static TypeId GetTypeId()
Get the type ID.
Options
BOOTP options.
@ OP_SERVID
BOOTP Option 54: Server Identifier.
@ OP_MASK
BOOTP Option 1: Address Mask.
@ OP_REBIND
BOOTP Option 59: Address Rebind Time.
@ OP_MSGTYPE
BOOTP Option 53: DHCP Message Type.
@ OP_RENEW
BOOTP Option 58: Address Renewal Time.
@ OP_ADDREQ
BOOTP Option 50: Requested Address.
@ OP_ROUTE
BOOTP Option 3: Router Option.
@ OP_END
BOOTP Option 255: END.
@ OP_LEASE
BOOTP Option 51: Address Lease Time.
uint8_t m_hLen
The hardware length.
DhcpHeader()
Constructor.
void SetRouter(Ipv4Address addr)
Set the Ipv4Address of gateway to be used.
void SetMask(uint32_t addr)
Set the mask of the IPv4Address.
uint32_t m_renew
The renewal time for the client.
DhcpChaddr GetChaddr()
Get the Address of the client.
uint8_t m_hType
The hardware type.
void SetReq(Ipv4Address addr)
Set the Ipv4Address requested by the client.
uint16_t m_flags
BOOTP flags.
uint32_t Deserialize(Buffer::Iterator start) override
Ipv4Address GetYiaddr() const
Get the IPv4Address of the client.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void SetRebind(uint32_t time)
Set the Rebind time of the IPv4Address.
void Print(std::ostream &os) const override
Ipv4Address m_route
Router Option Address.
uint8_t m_op
The DHCP Message type.
uint32_t m_xid
The transaction number.
uint32_t GetRebind() const
Return the Rebind time of the address.
Ipv4Address m_req
Requested Address.
uint32_t GetRenew() const
Return the Renewal time of the address.
uint8_t m_file[128]
File name (Padded for now).
uint32_t GetSerializedSize() const override
void SetChaddr(DhcpChaddr addr)
Set the Chaddress of the device.
void SetHWType(uint8_t htype, uint8_t hlen)
Set the hardware information.
Protocol header serialization and deserialization.
Definition header.h:33
Ipv4 addresses are stored in host order in this class.
a unique identifier for an interface.
Definition type-id.h:49
std::array< uint8_t, 16 > DhcpChaddr
This is the Chaddr field, which is 16 bytes long.
Definition dhcp-header.h:44
std::string DhcpChaddrToString(const DhcpChaddr &chaddr)
Function to pretty-print a Chaddr.
Every class exported by the ns3 library is enclosed in the ns3 namespace.