LEFT | RIGHT |
(no file at all) | |
| 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 |
| 37 DhcpHeader::DhcpHeader () |
| 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 |
| 71 DhcpHeader::~DhcpHeader () |
| 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 |
| 107 void DhcpHeader::SetTime () |
| 108 { |
| 109 m_secs = (uint16_t) Simulator::Now ().GetSeconds (); |
| 110 } |
| 111 |
| 112 void DhcpHeader::SetChaddr (Address addr) |
| 113 { |
| 114 addr.CopyAllTo (m_chaddr, 16); |
| 115 } |
| 116 |
| 117 Address DhcpHeader::GetChaddr () |
| 118 { |
| 119 Address addr; |
| 120 addr.CopyAllFrom (m_chaddr, 16); |
| 121 return addr; |
| 122 } |
| 123 |
| 124 void DhcpHeader::SetYiaddr (Ipv4Address addr) |
| 125 { |
| 126 m_yiAddr = addr; |
| 127 } |
| 128 |
| 129 Ipv4Address DhcpHeader::GetYiaddr (void) const |
| 130 { |
| 131 return m_yiAddr; |
| 132 } |
| 133 |
| 134 void DhcpHeader::SetDhcps (Ipv4Address addr) |
| 135 { |
| 136 if (m_opt[OP_SERVID] == false) |
| 137 { |
| 138 m_len += 6; |
| 139 m_opt[OP_SERVID] = true; |
| 140 } |
| 141 m_dhcps = addr; |
| 142 } |
| 143 |
| 144 Ipv4Address DhcpHeader::GetDhcps (void) const |
| 145 { |
| 146 return m_dhcps; |
| 147 } |
| 148 |
| 149 void DhcpHeader::SetReq (Ipv4Address addr) |
| 150 { |
| 151 if (m_opt[OP_ADDREQ] == false) |
| 152 { |
| 153 m_len += 6; |
| 154 m_opt[OP_ADDREQ] = true; |
| 155 } |
| 156 m_req = addr; |
| 157 } |
| 158 |
| 159 Ipv4Address DhcpHeader::GetReq (void) const |
| 160 { |
| 161 return m_req; |
| 162 } |
| 163 |
| 164 void DhcpHeader::SetMask (uint32_t addr) |
| 165 { |
| 166 if (m_opt[OP_MASK] == false) |
| 167 { |
| 168 m_len += 6; |
| 169 m_opt[OP_MASK] = true; |
| 170 } |
| 171 m_mask = addr; |
| 172 } |
| 173 |
| 174 uint32_t DhcpHeader::GetMask (void) const |
| 175 { |
| 176 return m_mask; |
| 177 } |
| 178 |
| 179 void DhcpHeader::SetRouter (Ipv4Address addr) |
| 180 { |
| 181 if (m_opt[OP_ROUTE] == false) |
| 182 { |
| 183 m_len += 6; |
| 184 m_opt[OP_ROUTE] = true; |
| 185 } |
| 186 m_route = addr; |
| 187 } |
| 188 |
| 189 Ipv4Address DhcpHeader::GetRouter (void) const |
| 190 { |
| 191 return m_route; |
| 192 } |
| 193 |
| 194 void DhcpHeader::SetLease (uint32_t time) |
| 195 { |
| 196 if (m_opt[OP_LEASE] == false) |
| 197 { |
| 198 m_len += 6; |
| 199 m_opt[OP_LEASE] = true; |
| 200 } |
| 201 m_lease = time; |
| 202 } |
| 203 |
| 204 uint32_t DhcpHeader::GetLease (void) const |
| 205 { |
| 206 return m_lease; |
| 207 } |
| 208 |
| 209 void DhcpHeader::SetRenew (uint32_t time) |
| 210 { |
| 211 if (m_opt[OP_RENEW] == false) |
| 212 { |
| 213 m_len += 6; |
| 214 m_opt[OP_RENEW] = true; |
| 215 } |
| 216 m_renew = time; |
| 217 } |
| 218 |
| 219 uint32_t DhcpHeader::GetRenew (void) const |
| 220 { |
| 221 return m_renew; |
| 222 } |
| 223 |
| 224 void DhcpHeader::SetRebind (uint32_t time) |
| 225 { |
| 226 if (m_opt[OP_REBIND] == false) |
| 227 { |
| 228 m_len += 6; |
| 229 m_opt[OP_REBIND] = true; |
| 230 } |
| 231 m_rebind = time; |
| 232 } |
| 233 |
| 234 uint32_t DhcpHeader::GetRebind (void) const |
| 235 { |
| 236 return m_rebind; |
| 237 } |
| 238 |
| 239 void DhcpHeader::ResetOpt () |
| 240 { |
| 241 m_len = 241; |
| 242 int i; |
| 243 for (i = 0; i < OP_END; i++) |
| 244 { |
| 245 m_opt[i] = false; |
| 246 } |
| 247 } |
| 248 |
| 249 uint32_t DhcpHeader::GetSerializedSize (void) const |
| 250 { |
| 251 return m_len; |
| 252 } |
| 253 |
| 254 TypeId DhcpHeader::GetTypeId (void) |
| 255 { |
| 256 static TypeId tid = TypeId ("ns3::DhcpHeader") |
| 257 .SetParent<Header> () |
| 258 .SetGroupName ("Internet-Apps") |
| 259 .AddConstructor<DhcpHeader> () |
| 260 ; |
| 261 return tid; |
| 262 } |
| 263 |
| 264 TypeId DhcpHeader::GetInstanceTypeId (void) const |
| 265 { |
| 266 return GetTypeId (); |
| 267 } |
| 268 |
| 269 void DhcpHeader::Print (std::ostream &os) const |
| 270 { |
| 271 os << "(type=" << m_op << ")"; |
| 272 } |
| 273 |
| 274 void |
| 275 DhcpHeader::Serialize (Buffer::Iterator start) const |
| 276 { |
| 277 Buffer::Iterator i = start; |
| 278 i.WriteU8 (m_bootp); |
| 279 i.WriteU8 ( m_hType); |
| 280 i.WriteU8 ( m_hLen); |
| 281 i.WriteU8 ( m_hops); |
| 282 i.WriteU32 ( m_xid); |
| 283 i.WriteHtonU16 ( m_secs); |
| 284 i.WriteU16 ( m_flags); |
| 285 WriteTo (i, m_ciAddr); |
| 286 WriteTo (i, m_yiAddr); |
| 287 WriteTo (i, m_siAddr); |
| 288 WriteTo (i, m_giAddr); |
| 289 i.Write (m_chaddr,16); |
| 290 i.Write (m_sname,64); |
| 291 i.Write (m_file,128); |
| 292 i.Write (m_magic_cookie,4); |
| 293 if (m_opt[OP_MASK]) |
| 294 { |
| 295 i.WriteU8 (OP_MASK); |
| 296 i.WriteU8 (4); |
| 297 i.WriteHtonU32 (m_mask); |
| 298 } |
| 299 if (m_opt[OP_MSGTYPE]) |
| 300 { |
| 301 i.WriteU8 (OP_MSGTYPE); |
| 302 i.WriteU8 (1); |
| 303 i.WriteU8 ((m_op + 1)); |
| 304 } |
| 305 if (m_opt[OP_ADDREQ]) |
| 306 { |
| 307 i.WriteU8 (OP_ADDREQ); |
| 308 i.WriteU8 (4); |
| 309 WriteTo (i, m_req); |
| 310 } |
| 311 if (m_opt[OP_SERVID]) |
| 312 { |
| 313 i.WriteU8 (OP_SERVID); |
| 314 i.WriteU8 (4); |
| 315 WriteTo (i, m_dhcps); |
| 316 } |
| 317 if (m_opt[OP_ROUTE]) |
| 318 { |
| 319 i.WriteU8 (OP_ROUTE); |
| 320 i.WriteU8 (4); |
| 321 WriteTo (i, m_route); |
| 322 } |
| 323 if (m_opt[OP_LEASE]) |
| 324 { |
| 325 i.WriteU8 (OP_LEASE); |
| 326 i.WriteU8 (4); |
| 327 i.WriteHtonU32 (m_lease); |
| 328 } |
| 329 if (m_opt[OP_RENEW]) |
| 330 { |
| 331 i.WriteU8 (OP_RENEW); |
| 332 i.WriteU8 (4); |
| 333 i.WriteHtonU32 (m_renew); |
| 334 } |
| 335 if (m_opt[OP_REBIND]) |
| 336 { |
| 337 i.WriteU8 (OP_REBIND); |
| 338 i.WriteU8 (4); |
| 339 i.WriteHtonU32 (m_rebind); |
| 340 } |
| 341 i.WriteU8 (OP_END); |
| 342 } |
| 343 |
| 344 uint32_t DhcpHeader::Deserialize (Buffer::Iterator start) |
| 345 { |
| 346 uint32_t len, clen = start.GetSize (); |
| 347 if (clen < 240) |
| 348 { |
| 349 NS_LOG_WARN ("Malformed Packet"); |
| 350 return 0; |
| 351 } |
| 352 Buffer::Iterator i = start; |
| 353 m_bootp = i.ReadU8 (); |
| 354 m_hType = i.ReadU8 (); |
| 355 m_hLen = i.ReadU8 (); |
| 356 m_hops = i.ReadU8 (); |
| 357 m_xid = i.ReadU32 (); |
| 358 m_secs = i.ReadNtohU16 (); |
| 359 m_flags = i.ReadU16 (); |
| 360 ReadFrom (i, m_ciAddr); |
| 361 ReadFrom (i,m_yiAddr); |
| 362 ReadFrom (i,m_siAddr); |
| 363 ReadFrom (i,m_giAddr); |
| 364 i.Read (m_chaddr, 16); |
| 365 i.Read (m_sname, 64); |
| 366 i.Read (m_file,128); |
| 367 i.Read (m_magic_cookie,4); |
| 368 if ( m_magic_cookie[0] != 99 || m_magic_cookie[1] != 130 || m_magic_cookie[2]
!= 83 || m_magic_cookie[3] != 99) |
| 369 { |
| 370 NS_LOG_WARN ("Malformed Packet"); |
| 371 return 0; |
| 372 } |
| 373 len = 240; |
| 374 uint8_t option; |
| 375 bool loop = true; |
| 376 do |
| 377 { |
| 378 if (len + 1 <= clen) |
| 379 { |
| 380 option = i.ReadU8 (); |
| 381 len += 1; |
| 382 } |
| 383 else |
| 384 { |
| 385 NS_LOG_WARN ("Malformed Packet"); |
| 386 return 0; |
| 387 } |
| 388 switch (option) |
| 389 { |
| 390 case OP_MASK: |
| 391 if (len + 5 < clen) |
| 392 { |
| 393 i.ReadU8 (); |
| 394 m_mask = i.ReadNtohU32 (); |
| 395 len += 5; |
| 396 } |
| 397 else |
| 398 { |
| 399 NS_LOG_WARN ("Malformed Packet"); |
| 400 return 0; |
| 401 } |
| 402 break; |
| 403 case OP_ROUTE: |
| 404 if (len + 5 < clen) |
| 405 { |
| 406 i.ReadU8 (); |
| 407 ReadFrom (i, m_route); |
| 408 len += 5; |
| 409 } |
| 410 else |
| 411 { |
| 412 NS_LOG_WARN ("Malformed Packet"); |
| 413 return 0; |
| 414 } |
| 415 break; |
| 416 case OP_MSGTYPE: |
| 417 if (len + 2 < clen) |
| 418 { |
| 419 i.ReadU8 (); |
| 420 m_op = (i.ReadU8 () - 1); |
| 421 len += 2; |
| 422 } |
| 423 else |
| 424 { |
| 425 NS_LOG_WARN ("Malformed Packet"); |
| 426 return 0; |
| 427 } |
| 428 break; |
| 429 case OP_SERVID: |
| 430 if (len + 5 < clen) |
| 431 { |
| 432 i.ReadU8 (); |
| 433 ReadFrom (i, m_dhcps); |
| 434 len += 5; |
| 435 } |
| 436 else |
| 437 { |
| 438 NS_LOG_WARN ("Malformed Packet"); |
| 439 return 0; |
| 440 } |
| 441 break; |
| 442 case OP_ADDREQ: |
| 443 if (len + 5 < clen) |
| 444 { |
| 445 i.ReadU8 (); |
| 446 ReadFrom (i, m_req); |
| 447 len += 5; |
| 448 } |
| 449 else |
| 450 { |
| 451 NS_LOG_WARN ("Malformed Packet"); |
| 452 return 0; |
| 453 } |
| 454 break; |
| 455 case OP_LEASE: |
| 456 if (len + 5 < clen) |
| 457 { |
| 458 i.ReadU8 (); |
| 459 m_lease = i.ReadNtohU32 (); |
| 460 len += 5; |
| 461 } |
| 462 else |
| 463 { |
| 464 NS_LOG_WARN ("Malformed Packet"); |
| 465 return 0; |
| 466 } |
| 467 break; |
| 468 case OP_RENEW: |
| 469 if (len + 5 < clen) |
| 470 { |
| 471 i.ReadU8 (); |
| 472 m_renew = i.ReadNtohU32 (); |
| 473 len += 5; |
| 474 } |
| 475 else |
| 476 { |
| 477 NS_LOG_WARN ("Malformed Packet"); |
| 478 return 0; |
| 479 } |
| 480 break; |
| 481 case OP_REBIND: |
| 482 if (len + 5 < clen) |
| 483 { |
| 484 i.ReadU8 (); |
| 485 m_rebind = i.ReadNtohU32 (); |
| 486 len += 5; |
| 487 } |
| 488 else |
| 489 { |
| 490 NS_LOG_WARN ("Malformed Packet"); |
| 491 return 0; |
| 492 } |
| 493 break; |
| 494 case OP_END: |
| 495 loop = false; |
| 496 break; |
| 497 default: |
| 498 NS_LOG_WARN ("Malformed Packet"); |
| 499 return 0; |
| 500 } |
| 501 } |
| 502 while (loop); |
| 503 |
| 504 m_len = len; |
| 505 return m_len; |
| 506 } |
| 507 |
| 508 } // namespace ns3 |
LEFT | RIGHT |