A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dhcp-header.cc
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#include "dhcp-header.h"
14
15#include "ns3/address-utils.h"
16#include "ns3/assert.h"
17#include "ns3/log.h"
18#include "ns3/simulator.h"
19
20#include <iomanip>
21#include <sstream>
22
23/**
24 * @file
25 * @ingroup internet-apps
26 * DhcpHeader classes implementation.
27 */
28
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE("DhcpHeader");
34
35std::string
37{
38 std::ostringstream outStream;
39 outStream.fill('0');
40 outStream.setf(std::ios::hex, std::ios::basefield);
41
42 for (std::size_t i = 0; i < chaddr.size(); ++i)
43 {
44 outStream << std::setw(2) << (uint32_t)chaddr[i] << (i < chaddr.size() - 1 ? ":" : "");
45 }
46 return outStream.str();
47}
48
50{
51 m_hType = 1;
52 m_hLen = 6;
53 m_xid = 0;
54 m_secs = 0;
55 m_hops = 0;
56 m_flags = 0;
57 Ipv4Address addr("0.0.0.0");
58 m_yiAddr = addr;
59 m_ciAddr = addr;
60 m_siAddr = addr;
61 m_giAddr = addr;
62 m_dhcps = addr;
63 m_req = addr;
64 m_route = addr;
65 m_len = 240;
66
67 uint32_t i;
68
69 for (i = 0; i < 64; i++)
70 {
71 m_sname[i] = 0;
72 }
73 for (i = 0; i < 128; i++)
74 {
75 m_file[i] = 0;
76 }
77 m_magic_cookie[0] = 99;
78 m_magic_cookie[1] = 130;
79 m_magic_cookie[2] = 83;
80 m_magic_cookie[3] = 99;
81}
82
86
87void
89{
90 if (!m_opt[OP_MSGTYPE])
91 {
92 m_len += 3;
93 m_opt[OP_MSGTYPE] = true;
94 }
95 m_op = type;
96 m_bootp = (m_op == 0 || m_op == 2) ? 1 : 2;
97}
98
99uint8_t
101{
102 return m_op;
103}
104
105void
106DhcpHeader::SetHWType(uint8_t htype, uint8_t hlen)
107{
108 m_hType = htype;
109 m_hLen = hlen;
110}
111
112void
114{
115 m_xid = tran;
116}
117
120{
121 return m_xid;
122}
123
124void
126{
127 m_secs = (uint16_t)Simulator::Now().GetSeconds();
128}
129
130void
132{
133 m_chaddr = addr;
134}
135
138{
139 return m_chaddr;
140}
141
142void
144{
145 m_yiAddr = addr;
146}
147
150{
151 return m_yiAddr;
152}
153
154void
156{
157 if (!m_opt[OP_SERVID])
158 {
159 m_len += 6;
160 m_opt[OP_SERVID] = true;
161 }
162 m_dhcps = addr;
163}
164
167{
168 return m_dhcps;
169}
170
171void
173{
174 if (!m_opt[OP_ADDREQ])
175 {
176 m_len += 6;
177 m_opt[OP_ADDREQ] = true;
178 }
179 m_req = addr;
180}
181
184{
185 return m_req;
186}
187
188void
190{
191 if (!m_opt[OP_MASK])
192 {
193 m_len += 6;
194 m_opt[OP_MASK] = true;
195 }
196 m_mask = addr;
197}
198
201{
202 return m_mask;
203}
204
205void
207{
208 if (!m_opt[OP_ROUTE])
209 {
210 m_len += 6;
211 m_opt[OP_ROUTE] = true;
212 }
213 m_route = addr;
214}
215
218{
219 return m_route;
220}
221
222void
224{
225 if (!m_opt[OP_LEASE])
226 {
227 m_len += 6;
228 m_opt[OP_LEASE] = true;
229 }
230 m_lease = time;
231}
232
235{
236 return m_lease;
237}
238
239void
241{
242 if (!m_opt[OP_RENEW])
243 {
244 m_len += 6;
245 m_opt[OP_RENEW] = true;
246 }
247 m_renew = time;
248}
249
252{
253 return m_renew;
254}
255
256void
258{
259 if (!m_opt[OP_REBIND])
260 {
261 m_len += 6;
262 m_opt[OP_REBIND] = true;
263 }
264 m_rebind = time;
265}
266
269{
270 return m_rebind;
271}
272
273void
275{
276 m_len = 241;
277 int i;
278 for (i = 0; i < OP_END; i++)
279 {
280 m_opt[i] = false;
281 }
282}
283
286{
287 return m_len;
288}
289
290TypeId
292{
293 static TypeId tid = TypeId("ns3::DhcpHeader")
294 .SetParent<Header>()
295 .SetGroupName("Internet-Apps")
296 .AddConstructor<DhcpHeader>();
297 return tid;
298}
299
300TypeId
302{
303 return GetTypeId();
304}
305
306void
307DhcpHeader::Print(std::ostream& os) const
308{
309 os << "(type=" << m_op << ")";
310}
311
312void
314{
315 Buffer::Iterator i = start;
316 i.WriteU8(m_bootp);
317 i.WriteU8(m_hType);
318 i.WriteU8(m_hLen);
319 i.WriteU8(m_hops);
320 i.WriteU32(m_xid);
322 i.WriteU16(m_flags);
323 WriteTo(i, m_ciAddr);
324 WriteTo(i, m_yiAddr);
325 WriteTo(i, m_siAddr);
326 WriteTo(i, m_giAddr);
327 i.Write(m_chaddr.data(), 16);
328 i.Write(m_sname, 64);
329 i.Write(m_file, 128);
330 i.Write(m_magic_cookie, 4);
331 if (m_opt[OP_MASK])
332 {
333 i.WriteU8(OP_MASK);
334 i.WriteU8(4);
336 }
337 if (m_opt[OP_MSGTYPE])
338 {
340 i.WriteU8(1);
341 i.WriteU8(m_op + 1);
342 }
343 if (m_opt[OP_ADDREQ])
344 {
346 i.WriteU8(4);
347 WriteTo(i, m_req);
348 }
349 if (m_opt[OP_SERVID])
350 {
352 i.WriteU8(4);
353 WriteTo(i, m_dhcps);
354 }
355 if (m_opt[OP_ROUTE])
356 {
357 i.WriteU8(OP_ROUTE);
358 i.WriteU8(4);
359 WriteTo(i, m_route);
360 }
361 if (m_opt[OP_LEASE])
362 {
363 i.WriteU8(OP_LEASE);
364 i.WriteU8(4);
366 }
367 if (m_opt[OP_RENEW])
368 {
369 i.WriteU8(OP_RENEW);
370 i.WriteU8(4);
372 }
373 if (m_opt[OP_REBIND])
374 {
376 i.WriteU8(4);
378 }
379 i.WriteU8(OP_END);
380}
381
384{
385 uint32_t len;
386 uint32_t cLen = start.GetSize();
387 if (cLen < 240)
388 {
389 NS_LOG_WARN("Malformed Packet");
390 return 0;
391 }
392 Buffer::Iterator i = start;
393 m_bootp = i.ReadU8();
394 m_hType = i.ReadU8();
395 m_hLen = i.ReadU8();
396 m_hops = i.ReadU8();
397 m_xid = i.ReadU32();
398 m_secs = i.ReadNtohU16();
399 m_flags = i.ReadU16();
400 ReadFrom(i, m_ciAddr);
401 ReadFrom(i, m_yiAddr);
402 ReadFrom(i, m_siAddr);
403 ReadFrom(i, m_giAddr);
404 i.Read(m_chaddr.data(), 16);
405 i.Read(m_sname, 64);
406 i.Read(m_file, 128);
407 i.Read(m_magic_cookie, 4);
408 if (m_magic_cookie[0] != 99 || m_magic_cookie[1] != 130 || m_magic_cookie[2] != 83 ||
409 m_magic_cookie[3] != 99)
410 {
411 NS_LOG_WARN("Malformed Packet");
412 return 0;
413 }
414 len = 240;
415 uint8_t option;
416 bool loop = true;
417 do
418 {
419 if (len + 1 <= cLen)
420 {
421 option = i.ReadU8();
422 len += 1;
423 }
424 else
425 {
426 NS_LOG_WARN("Malformed Packet");
427 return 0;
428 }
429 switch (option)
430 {
431 case OP_MASK:
432 if (len + 5 < cLen)
433 {
434 i.ReadU8();
435 m_mask = i.ReadNtohU32();
436 len += 5;
437 }
438 else
439 {
440 NS_LOG_WARN("Malformed Packet");
441 return 0;
442 }
443 break;
444 case OP_ROUTE:
445 if (len + 5 < cLen)
446 {
447 i.ReadU8();
448 ReadFrom(i, m_route);
449 len += 5;
450 }
451 else
452 {
453 NS_LOG_WARN("Malformed Packet");
454 return 0;
455 }
456 break;
457 case OP_MSGTYPE:
458 if (len + 2 < cLen)
459 {
460 i.ReadU8();
461 m_op = (i.ReadU8() - 1);
462 len += 2;
463 }
464 else
465 {
466 NS_LOG_WARN("Malformed Packet");
467 return 0;
468 }
469 break;
470 case OP_SERVID:
471 if (len + 5 < cLen)
472 {
473 i.ReadU8();
474 ReadFrom(i, m_dhcps);
475 len += 5;
476 }
477 else
478 {
479 NS_LOG_WARN("Malformed Packet");
480 return 0;
481 }
482 break;
483 case OP_ADDREQ:
484 if (len + 5 < cLen)
485 {
486 i.ReadU8();
487 ReadFrom(i, m_req);
488 len += 5;
489 }
490 else
491 {
492 NS_LOG_WARN("Malformed Packet");
493 return 0;
494 }
495 break;
496 case OP_LEASE:
497 if (len + 5 < cLen)
498 {
499 i.ReadU8();
500 m_lease = i.ReadNtohU32();
501 len += 5;
502 }
503 else
504 {
505 NS_LOG_WARN("Malformed Packet");
506 return 0;
507 }
508 break;
509 case OP_RENEW:
510 if (len + 5 < cLen)
511 {
512 i.ReadU8();
513 m_renew = i.ReadNtohU32();
514 len += 5;
515 }
516 else
517 {
518 NS_LOG_WARN("Malformed Packet");
519 return 0;
520 }
521 break;
522 case OP_REBIND:
523 if (len + 5 < cLen)
524 {
525 i.ReadU8();
526 m_rebind = i.ReadNtohU32();
527 len += 5;
528 }
529 else
530 {
531 NS_LOG_WARN("Malformed Packet");
532 return 0;
533 }
534 break;
535 case OP_END:
536 loop = false;
537 break;
538 default:
539 NS_LOG_WARN("Malformed Packet");
540 return 0;
541 }
542 } while (loop);
543
544 m_len = len;
545 return m_len;
546}
547
548} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
void WriteU32(uint32_t data)
Definition buffer.cc:857
void WriteU8(uint8_t data)
Definition buffer.h:870
void Write(const uint8_t *buffer, uint32_t size)
Definition buffer.cc:937
void WriteU16(uint16_t data)
Definition buffer.cc:848
void Read(uint8_t *buffer, uint32_t size)
Definition buffer.cc:1114
void WriteHtonU16(uint16_t data)
Definition buffer.h:904
uint32_t ReadNtohU32()
Definition buffer.h:967
uint32_t ReadU32()
Definition buffer.cc:955
void WriteHtonU32(uint32_t data)
Definition buffer.h:922
uint16_t ReadNtohU16()
Definition buffer.h:943
uint16_t ReadU16()
Definition buffer.h:1024
BOOTP header with DHCP messages.
Definition dhcp-header.h:99
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.
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.
@ 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.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
DhcpHeader classes declaration.
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.