A Discrete-Event Network Simulator
API
dhcp-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 UPB
4  * Copyright (c) 2017 NITK Surathkal
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Radu Lupu <rlupu@elcom.pub.ro>
20  * Ankit Deepak <adadeepak8@gmail.com>
21  * Deepti Rajagopal <deeptir96@gmail.com>
22  *
23  */
24 
25 #include "ns3/assert.h"
26 #include "ns3/log.h"
27 #include "ns3/header.h"
28 #include "ns3/simulator.h"
29 #include "dhcp-header.h"
30 #include "ns3/address-utils.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("DhcpHeader");
35 NS_OBJECT_ENSURE_REGISTERED (DhcpHeader);
36 
38 {
39  m_hType = 1;
40  m_hLen = 6;
41  m_xid = 0;
42  m_secs = 0;
43  m_hops = 0;
44  m_flags = 0;
45  Ipv4Address addr = Ipv4Address ("0.0.0.0");
46  m_yiAddr = addr;
47  m_ciAddr = addr;
48  m_siAddr = addr;
49  m_giAddr = addr;
50  m_dhcps = addr;
51  m_req = addr;
52  m_route = addr;
53  m_len = 240;
54 
55  uint32_t i;
56 
57  for (i = 0; i < 64; i++)
58  {
59  m_sname[i] = 0;
60  }
61  for (i = 0; i < 128; i++)
62  {
63  m_file[i] = 0;
64  }
65  m_magic_cookie[0] = 99;
66  m_magic_cookie[1] = 130;
67  m_magic_cookie[2] = 83;
68  m_magic_cookie[3] = 99;
69 }
70 
72 {
73 }
74 
75 void DhcpHeader::SetType (uint8_t type)
76 {
77  if (m_opt[OP_MSGTYPE] == false)
78  {
79  m_len += 3;
80  m_opt[OP_MSGTYPE] = true;
81  }
82  m_op = type;
83  m_bootp = (m_op == 0||m_op == 2) ? 1 : 2;
84 }
85 
86 uint8_t DhcpHeader::GetType (void) const
87 {
88  return m_op;
89 }
90 
91 void DhcpHeader::SetHWType (uint8_t htype, uint8_t hlen)
92 {
93  m_hType = htype;
94  m_hLen = hlen;
95 }
96 
97 void DhcpHeader::SetTran (uint32_t tran)
98 {
99  m_xid = tran;
100 }
101 
102 uint32_t DhcpHeader::GetTran (void) const
103 {
104  return m_xid;
105 }
106 
108 {
109  m_secs = (uint16_t) Simulator::Now ().GetSeconds ();
110 }
111 
113 {
114  std::memset (m_chaddr, 0, 16);
115  NS_ASSERT_MSG (addr.GetLength () <= 16, "Address length too big");
116  addr.CopyTo (m_chaddr);
117 }
118 
119 void DhcpHeader::SetChaddr (uint8_t* addr, uint8_t len)
120 {
121  std::memset (m_chaddr, 0, 16);
122  NS_ASSERT_MSG (len <= 16, "Address length too big");
123  std::memcpy (m_chaddr, addr, len);
124 }
125 
127 {
128  Address addr;
129  addr.CopyFrom (m_chaddr, 16);
130  return addr;
131 }
132 
134 {
135  m_yiAddr = addr;
136 }
137 
139 {
140  return m_yiAddr;
141 }
142 
144 {
145  if (m_opt[OP_SERVID] == false)
146  {
147  m_len += 6;
148  m_opt[OP_SERVID] = true;
149  }
150  m_dhcps = addr;
151 }
152 
154 {
155  return m_dhcps;
156 }
157 
159 {
160  if (m_opt[OP_ADDREQ] == false)
161  {
162  m_len += 6;
163  m_opt[OP_ADDREQ] = true;
164  }
165  m_req = addr;
166 }
167 
169 {
170  return m_req;
171 }
172 
173 void DhcpHeader::SetMask (uint32_t addr)
174 {
175  if (m_opt[OP_MASK] == false)
176  {
177  m_len += 6;
178  m_opt[OP_MASK] = true;
179  }
180  m_mask = addr;
181 }
182 
183 uint32_t DhcpHeader::GetMask (void) const
184 {
185  return m_mask;
186 }
187 
189 {
190  if (m_opt[OP_ROUTE] == false)
191  {
192  m_len += 6;
193  m_opt[OP_ROUTE] = true;
194  }
195  m_route = addr;
196 }
197 
199 {
200  return m_route;
201 }
202 
203 void DhcpHeader::SetLease (uint32_t time)
204 {
205  if (m_opt[OP_LEASE] == false)
206  {
207  m_len += 6;
208  m_opt[OP_LEASE] = true;
209  }
210  m_lease = time;
211 }
212 
213 uint32_t DhcpHeader::GetLease (void) const
214 {
215  return m_lease;
216 }
217 
218 void DhcpHeader::SetRenew (uint32_t time)
219 {
220  if (m_opt[OP_RENEW] == false)
221  {
222  m_len += 6;
223  m_opt[OP_RENEW] = true;
224  }
225  m_renew = time;
226 }
227 
228 uint32_t DhcpHeader::GetRenew (void) const
229 {
230  return m_renew;
231 }
232 
233 void DhcpHeader::SetRebind (uint32_t time)
234 {
235  if (m_opt[OP_REBIND] == false)
236  {
237  m_len += 6;
238  m_opt[OP_REBIND] = true;
239  }
240  m_rebind = time;
241 }
242 
243 uint32_t DhcpHeader::GetRebind (void) const
244 {
245  return m_rebind;
246 }
247 
249 {
250  m_len = 241;
251  int i;
252  for (i = 0; i < OP_END; i++)
253  {
254  m_opt[i] = false;
255  }
256 }
257 
258 uint32_t DhcpHeader::GetSerializedSize (void) const
259 {
260  return m_len;
261 }
262 
264 {
265  static TypeId tid = TypeId ("ns3::DhcpHeader")
266  .SetParent<Header> ()
267  .SetGroupName ("Internet-Apps")
268  .AddConstructor<DhcpHeader> ()
269  ;
270  return tid;
271 }
272 
274 {
275  return GetTypeId ();
276 }
277 
278 void DhcpHeader::Print (std::ostream &os) const
279 {
280  os << "(type=" << m_op << ")";
281 }
282 
283 void
285 {
287  i.WriteU8 (m_bootp);
288  i.WriteU8 (m_hType);
289  i.WriteU8 (m_hLen);
290  i.WriteU8 (m_hops);
291  i.WriteU32 (m_xid);
292  i.WriteHtonU16 (m_secs);
293  i.WriteU16 ( m_flags);
294  WriteTo (i, m_ciAddr);
295  WriteTo (i, m_yiAddr);
296  WriteTo (i, m_siAddr);
297  WriteTo (i, m_giAddr);
298  i.Write (m_chaddr, 16);
299  i.Write (m_sname,64);
300  i.Write (m_file,128);
301  i.Write (m_magic_cookie,4);
302  if (m_opt[OP_MASK])
303  {
304  i.WriteU8 (OP_MASK);
305  i.WriteU8 (4);
306  i.WriteHtonU32 (m_mask);
307  }
308  if (m_opt[OP_MSGTYPE])
309  {
310  i.WriteU8 (OP_MSGTYPE);
311  i.WriteU8 (1);
312  i.WriteU8 ((m_op + 1));
313  }
314  if (m_opt[OP_ADDREQ])
315  {
316  i.WriteU8 (OP_ADDREQ);
317  i.WriteU8 (4);
318  WriteTo (i, m_req);
319  }
320  if (m_opt[OP_SERVID])
321  {
322  i.WriteU8 (OP_SERVID);
323  i.WriteU8 (4);
324  WriteTo (i, m_dhcps);
325  }
326  if (m_opt[OP_ROUTE])
327  {
328  i.WriteU8 (OP_ROUTE);
329  i.WriteU8 (4);
330  WriteTo (i, m_route);
331  }
332  if (m_opt[OP_LEASE])
333  {
334  i.WriteU8 (OP_LEASE);
335  i.WriteU8 (4);
336  i.WriteHtonU32 (m_lease);
337  }
338  if (m_opt[OP_RENEW])
339  {
340  i.WriteU8 (OP_RENEW);
341  i.WriteU8 (4);
342  i.WriteHtonU32 (m_renew);
343  }
344  if (m_opt[OP_REBIND])
345  {
346  i.WriteU8 (OP_REBIND);
347  i.WriteU8 (4);
349  }
350  i.WriteU8 (OP_END);
351 }
352 
354 {
355  uint32_t len, clen = start.GetSize ();
356  if (clen < 240)
357  {
358  NS_LOG_WARN ("Malformed Packet");
359  return 0;
360  }
362  m_bootp = i.ReadU8 ();
363  m_hType = i.ReadU8 ();
364  m_hLen = i.ReadU8 ();
365  m_hops = i.ReadU8 ();
366  m_xid = i.ReadU32 ();
367  m_secs = i.ReadNtohU16 ();
368  m_flags = i.ReadU16 ();
369  ReadFrom (i, m_ciAddr);
370  ReadFrom (i, m_yiAddr);
371  ReadFrom (i, m_siAddr);
372  ReadFrom (i, m_giAddr);
373  i.Read (m_chaddr, 16);
374  i.Read (m_sname, 64);
375  i.Read (m_file, 128);
376  i.Read (m_magic_cookie, 4);
377  if ( m_magic_cookie[0] != 99 || m_magic_cookie[1] != 130 || m_magic_cookie[2] != 83 || m_magic_cookie[3] != 99)
378  {
379  NS_LOG_WARN ("Malformed Packet");
380  return 0;
381  }
382  len = 240;
383  uint8_t option;
384  bool loop = true;
385  do
386  {
387  if (len + 1 <= clen)
388  {
389  option = i.ReadU8 ();
390  len += 1;
391  }
392  else
393  {
394  NS_LOG_WARN ("Malformed Packet");
395  return 0;
396  }
397  switch (option)
398  {
399  case OP_MASK:
400  if (len + 5 < clen)
401  {
402  i.ReadU8 ();
403  m_mask = i.ReadNtohU32 ();
404  len += 5;
405  }
406  else
407  {
408  NS_LOG_WARN ("Malformed Packet");
409  return 0;
410  }
411  break;
412  case OP_ROUTE:
413  if (len + 5 < clen)
414  {
415  i.ReadU8 ();
416  ReadFrom (i, m_route);
417  len += 5;
418  }
419  else
420  {
421  NS_LOG_WARN ("Malformed Packet");
422  return 0;
423  }
424  break;
425  case OP_MSGTYPE:
426  if (len + 2 < clen)
427  {
428  i.ReadU8 ();
429  m_op = (i.ReadU8 () - 1);
430  len += 2;
431  }
432  else
433  {
434  NS_LOG_WARN ("Malformed Packet");
435  return 0;
436  }
437  break;
438  case OP_SERVID:
439  if (len + 5 < clen)
440  {
441  i.ReadU8 ();
442  ReadFrom (i, m_dhcps);
443  len += 5;
444  }
445  else
446  {
447  NS_LOG_WARN ("Malformed Packet");
448  return 0;
449  }
450  break;
451  case OP_ADDREQ:
452  if (len + 5 < clen)
453  {
454  i.ReadU8 ();
455  ReadFrom (i, m_req);
456  len += 5;
457  }
458  else
459  {
460  NS_LOG_WARN ("Malformed Packet");
461  return 0;
462  }
463  break;
464  case OP_LEASE:
465  if (len + 5 < clen)
466  {
467  i.ReadU8 ();
468  m_lease = i.ReadNtohU32 ();
469  len += 5;
470  }
471  else
472  {
473  NS_LOG_WARN ("Malformed Packet");
474  return 0;
475  }
476  break;
477  case OP_RENEW:
478  if (len + 5 < clen)
479  {
480  i.ReadU8 ();
481  m_renew = i.ReadNtohU32 ();
482  len += 5;
483  }
484  else
485  {
486  NS_LOG_WARN ("Malformed Packet");
487  return 0;
488  }
489  break;
490  case OP_REBIND:
491  if (len + 5 < clen)
492  {
493  i.ReadU8 ();
494  m_rebind = i.ReadNtohU32 ();
495  len += 5;
496  }
497  else
498  {
499  NS_LOG_WARN ("Malformed Packet");
500  return 0;
501  }
502  break;
503  case OP_END:
504  loop = false;
505  break;
506  default:
507  NS_LOG_WARN ("Malformed Packet");
508  return 0;
509  }
510  }
511  while (loop);
512 
513  m_len = len;
514  return m_len;
515 }
516 
517 } // namespace ns3
uint16_t ReadU16(void)
Definition: buffer.h:1029
Protocol header serialization and deserialization.
Definition: header.h:42
BOOTP Option 255: END.
Definition: dhcp-header.h:113
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: dhcp-header.cc:273
uint32_t ReadU32(void)
Definition: buffer.cc:975
Ipv4Address m_ciAddr
The IP address of the client.
Definition: dhcp-header.h:307
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
void SetTime()
Set the time when message is sent.
Definition: dhcp-header.cc:107
BOOTP Option 58: Address Renewal Time.
Definition: dhcp-header.h:111
Ipv4Address m_giAddr
Relay Agent IP address.
Definition: dhcp-header.h:309
virtual uint32_t GetSerializedSize(void) const
Definition: dhcp-header.cc:258
Ipv4Address GetRouter(void) const
Return the Ipv4Address of gateway to be used.
Definition: dhcp-header.cc:198
static TypeId GetTypeId(void)
Get the type ID.
Definition: dhcp-header.cc:263
uint32_t m_renew
The renewal time for the client.
Definition: dhcp-header.h:317
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
def start()
Definition: core.py:1790
uint32_t m_rebind
The rebinding time for the client.
Definition: dhcp-header.h:318
void SetYiaddr(Ipv4Address addr)
Set the IPv4Address of the client.
Definition: dhcp-header.cc:133
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Ipv4Address m_yiAddr
Your (client) IP address.
Definition: dhcp-header.h:306
virtual void Serialize(Buffer::Iterator start) const
Definition: dhcp-header.cc:284
BOOTP Option 51: Address Lease Time.
Definition: dhcp-header.h:108
uint32_t GetTran(void) const
Get the transaction id.
Definition: dhcp-header.cc:102
Ipv4Address m_route
Router Option Address.
Definition: dhcp-header.h:312
uint32_t m_lease
The lease time of the address.
Definition: dhcp-header.h:316
uint8_t GetType(void) const
Return the type of DHCP message.
Definition: dhcp-header.cc:86
uint32_t GetLease(void) const
Return the lease time of the IPv4Address.
Definition: dhcp-header.cc:213
Address GetChaddr(void)
Get the Address of the client.
Definition: dhcp-header.cc:126
uint32_t ReadNtohU32(void)
Definition: buffer.h:970
void SetType(uint8_t type)
Set the type of BOOTP and DHCP messages.
Definition: dhcp-header.cc:75
Ipv4Address m_dhcps
DHCP server IP address.
Definition: dhcp-header.h:310
iterator in a Buffer instance
Definition: buffer.h:98
a polymophic address class
Definition: address.h:90
uint8_t m_hType
The hardware type.
Definition: dhcp-header.h:297
uint32_t GetMask(void) const
Return the mask of the network.
Definition: dhcp-header.cc:183
void SetHWType(uint8_t htype, uint8_t hlen)
Set the hardware information.
Definition: dhcp-header.cc:91
virtual void Print(std::ostream &os) const
Definition: dhcp-header.cc:278
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
Ipv4Address m_req
Requested Address.
Definition: dhcp-header.h:311
void SetTran(uint32_t tran)
Set the transaction ID.
Definition: dhcp-header.cc:97
uint8_t m_hLen
The hardware length.
Definition: dhcp-header.h:298
uint32_t GetRenew(void) const
Return the Renewal time of the address.
Definition: dhcp-header.cc:228
BOOTP Option 53: DHCP Message Type.
Definition: dhcp-header.h:109
Ipv4Address m_siAddr
Next Server IP address.
Definition: dhcp-header.h:308
void WriteU16(uint16_t data)
Definition: buffer.cc:873
BOOTP Option 50: Requested Address.
Definition: dhcp-header.h:107
void WriteHtonU16(uint16_t data)
Definition: buffer.h:905
Introspection did not find any typical Config paths.
Definition: dhcp-header.h:83
uint8_t GetLength(void) const
Get the length of the underlying address.
Definition: address.cc:75
uint32_t m_len
The length of the header.
Definition: dhcp-header.h:302
void SetReq(Ipv4Address addr)
Set the Ipv4Address requested by the client.
Definition: dhcp-header.cc:158
uint8_t m_op
The DHCP Message type.
Definition: dhcp-header.h:295
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t CopyFrom(const uint8_t *buffer, uint8_t len)
Definition: address.cc:101
void SetChaddr(Address addr)
Set the Address of the device.
Definition: dhcp-header.cc:112
BOOTP Option 59: Address Rebind Time.
Definition: dhcp-header.h:112
Ipv4Address GetDhcps(void) const
Get the information about the DHCP server.
Definition: dhcp-header.cc:153
uint8_t m_file[128]
File name (Padded for now)
Definition: dhcp-header.h:314
bool m_opt[255]
BOOTP option list.
Definition: dhcp-header.h:319
DhcpHeader()
Constructor.
Definition: dhcp-header.cc:37
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1126
BOOTP Option 1: Address Mask.
Definition: dhcp-header.h:105
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
void WriteHtonU32(uint32_t data)
Definition: buffer.h:924
void ResetOpt()
Reset the BOOTP options.
Definition: dhcp-header.cc:248
uint32_t GetRebind(void) const
Return the Rebind time of the address.
Definition: dhcp-header.cc:243
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
void SetDhcps(Ipv4Address addr)
Set the DHCP server information.
Definition: dhcp-header.cc:143
uint8_t m_magic_cookie[4]
DHCP Magic Cookie.
Definition: dhcp-header.h:315
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition: address.cc:82
uint32_t m_xid
The transaction number.
Definition: dhcp-header.h:300
void WriteU8(uint8_t data)
Definition: buffer.h:869
uint8_t m_hops
The number of hops covered by the message.
Definition: dhcp-header.h:299
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
uint16_t m_secs
Seconds elapsed.
Definition: dhcp-header.h:303
~DhcpHeader()
Destructor.
Definition: dhcp-header.cc:71
uint8_t ReadU8(void)
Definition: buffer.h:1021
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:956
void SetLease(uint32_t time)
Set the lease time of the IPv4Address.
Definition: dhcp-header.cc:203
uint8_t m_sname[64]
Server name (Padded for now)
Definition: dhcp-header.h:313
uint32_t m_mask
The mask of the network.
Definition: dhcp-header.h:301
void SetRenew(uint32_t time)
Set the Renewal time of the IPv4Address.
Definition: dhcp-header.cc:218
Ipv4Address GetYiaddr(void) const
Get the IPv4Address of the client.
Definition: dhcp-header.cc:138
void SetMask(uint32_t addr)
Set the mask of the IPv4Address.
Definition: dhcp-header.cc:173
Ipv4Address GetReq(void) const
Get the IPv4Address requested by the client.
Definition: dhcp-header.cc:168
uint16_t ReadNtohU16(void)
Definition: buffer.h:946
void WriteU32(uint32_t data)
Definition: buffer.cc:881
BOOTP Option 54: Server Identifier.
Definition: dhcp-header.h:110
uint32_t GetSize(void) const
Definition: buffer.cc:1161
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
BOOTP Option 3: Router Option.
Definition: dhcp-header.h:106
uint16_t m_flags
BOOTP flags.
Definition: dhcp-header.h:304
uint8_t m_chaddr[16]
The address identifier.
Definition: dhcp-header.h:305
void SetRouter(Ipv4Address addr)
Set the Ipv4Address of gateway to be used.
Definition: dhcp-header.cc:188
uint8_t m_bootp
The BOOTP Message type.
Definition: dhcp-header.h:296
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: dhcp-header.cc:353
void SetRebind(uint32_t time)
Set the Rebind time of the IPv4Address.
Definition: dhcp-header.cc:233