Changeset 79568 in vbox for trunk/src/VBox/NetworkServices/Dhcpd
- Timestamp:
- Jul 6, 2019 11:42:51 PM (6 years ago)
- Location:
- trunk/src/VBox/NetworkServices/Dhcpd
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/NetworkServices/Dhcpd/Config.cpp
r79563 r79568 247 247 if (m_strNetwork.isEmpty()) 248 248 { 249 Log DHCP(("network name is not specified\n"));249 LogRel(("network name is not specified\n")); 250 250 return false; 251 251 } … … 269 269 m_MacAddress.au8[5] = Uuid.Gen.au8Node[5]; 270 270 271 Log DHCP(("MAC address is not specified: will use generated MAC %RTmac\n", &m_MacAddress));271 LogRel(("MAC address is not specified: will use generated MAC %RTmac\n", &m_MacAddress)); 272 272 fMACGenerated = true; 273 273 } … … 276 276 if (m_MacAddress.au8[0] & 0x01) 277 277 { 278 Log DHCP(("MAC address is not unicast: %RTmac\n", &m_MacAddress));278 LogRel(("MAC address is not unicast: %RTmac\n", &m_MacAddress)); 279 279 return VERR_GENERAL_FAILURE; 280 280 } … … 283 283 if ((m_IPv4Address.au8[0] & 0xe0) == 0xe0) 284 284 { 285 Log DHCP(("IP address is not unicast: %RTnaipv4\n", m_IPv4Address.u));285 LogRel(("IP address is not unicast: %RTnaipv4\n", m_IPv4Address.u)); 286 286 return VERR_GENERAL_FAILURE; 287 287 } … … 292 292 if (RT_FAILURE(rc) || cPrefixBits == 0) 293 293 { 294 Log DHCP(("IP mask is not valid: %RTnaipv4\n", m_IPv4Netmask.u));294 LogRel(("IP mask is not valid: %RTnaipv4\n", m_IPv4Netmask.u)); 295 295 return VERR_GENERAL_FAILURE; 296 296 } … … 299 299 if ((m_IPv4PoolFirst.u & m_IPv4Netmask.u) != (m_IPv4Address.u & m_IPv4Netmask.u)) 300 300 { 301 Log DHCP(("first pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n",301 LogRel(("first pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n", 302 302 (m_IPv4Address.u & m_IPv4Netmask.u), cPrefixBits, m_IPv4PoolFirst.u)); 303 303 return VERR_GENERAL_FAILURE; … … 307 307 if ((m_IPv4PoolLast.u & m_IPv4Netmask.u) != (m_IPv4Address.u & m_IPv4Netmask.u)) 308 308 { 309 Log DHCP(("last pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n",309 LogRel(("last pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n", 310 310 (m_IPv4Address.u & m_IPv4Netmask.u), cPrefixBits, m_IPv4PoolLast.u)); 311 311 return VERR_GENERAL_FAILURE; … … 315 315 if (RT_N2H_U32(m_IPv4PoolLast.u) < RT_N2H_U32(m_IPv4PoolFirst.u)) 316 316 { 317 Log DHCP(("pool range is invalid: %RTnaipv4 - %RTnaipv4\n",317 LogRel(("pool range is invalid: %RTnaipv4 - %RTnaipv4\n", 318 318 m_IPv4PoolFirst.u, m_IPv4PoolLast.u)); 319 319 return VERR_GENERAL_FAILURE; … … 324 324 && RT_N2H_U32(m_IPv4Address.u) <= RT_N2H_U32(m_IPv4PoolLast.u)) 325 325 { 326 Log DHCP(("server address inside the pool range %RTnaipv4 - %RTnaipv4: %RTnaipv4\n",326 LogRel(("server address inside the pool range %RTnaipv4 - %RTnaipv4: %RTnaipv4\n", 327 327 m_IPv4PoolFirst.u, m_IPv4PoolLast.u, m_IPv4Address.u)); 328 328 return VERR_GENERAL_FAILURE; … … 330 330 331 331 if (!fMACGenerated) 332 Log DHCP(("MAC address %RTmac\n", &m_MacAddress));333 Log DHCP(("IP address %RTnaipv4/%d\n", m_IPv4Address.u, cPrefixBits));334 Log DHCP(("address pool %RTnaipv4 - %RTnaipv4\n", m_IPv4PoolFirst.u, m_IPv4PoolLast.u));332 LogRel(("MAC address %RTmac\n", &m_MacAddress)); 333 LogRel(("IP address %RTnaipv4/%d\n", m_IPv4Address.u, cPrefixBits)); 334 LogRel(("address pool %RTnaipv4 - %RTnaipv4\n", m_IPv4PoolFirst.u, m_IPv4PoolLast.u)); 335 335 336 336 return VINF_SUCCESS; … … 770 770 i_parseVMConfig(pElmChild); 771 771 else 772 Log DHCP(("Ignoring unexpected DHCPServer child: %s\n", pElmChild->getName()));772 LogRel(("Ignoring unexpected DHCPServer child: %s\n", pElmChild->getName())); 773 773 } 774 774 } -
trunk/src/VBox/NetworkServices/Dhcpd/DHCPD.cpp
r79563 r79568 221 221 * @throws std::bad_alloc 222 222 */ 223 DhcpServerMessage *DHCPD::i_createMessage(int type, DhcpClientMessage &req)223 DhcpServerMessage *DHCPD::i_createMessage(int type, const DhcpClientMessage &req) 224 224 { 225 225 return new DhcpServerMessage(req, type, m_pConfig->getIPv4Address()); … … 251 251 * @throws std::bad_alloc 252 252 */ 253 DhcpServerMessage *DHCPD::i_doDiscover( DhcpClientMessage &req)253 DhcpServerMessage *DHCPD::i_doDiscover(const DhcpClientMessage &req) 254 254 { 255 255 /** @todo … … 317 317 * @throws std::bad_alloc 318 318 */ 319 DhcpServerMessage *DHCPD::i_doRequest( DhcpClientMessage &req)319 DhcpServerMessage *DHCPD::i_doRequest(const DhcpClientMessage &req) 320 320 { 321 321 OptRequestedAddress reqAddr(req); … … 367 367 * @throws std::bad_alloc 368 368 */ 369 DhcpServerMessage *DHCPD::i_doInform( DhcpClientMessage &req)369 DhcpServerMessage *DHCPD::i_doInform(const DhcpClientMessage &req) 370 370 { 371 371 if (req.ciaddr().u == 0) -
trunk/src/VBox/NetworkServices/Dhcpd/DHCPD.h
r79563 r79568 62 62 /** @name DHCP message processing methods 63 63 * @{ */ 64 DhcpServerMessage *i_doDiscover( DhcpClientMessage &req);65 DhcpServerMessage *i_doRequest( DhcpClientMessage &req);66 DhcpServerMessage *i_doInform( DhcpClientMessage &req);64 DhcpServerMessage *i_doDiscover(const DhcpClientMessage &req); 65 DhcpServerMessage *i_doRequest(const DhcpClientMessage &req); 66 DhcpServerMessage *i_doInform(const DhcpClientMessage &req); 67 67 DhcpServerMessage *i_doDecline(const DhcpClientMessage &req) RT_NOEXCEPT; 68 68 DhcpServerMessage *i_doRelease(const DhcpClientMessage &req) RT_NOEXCEPT; 69 69 70 DhcpServerMessage *i_createMessage(int type, DhcpClientMessage &req);70 DhcpServerMessage *i_createMessage(int type, const DhcpClientMessage &req); 71 71 /** @} */ 72 72 -
trunk/src/VBox/NetworkServices/Dhcpd/Db.cpp
r79563 r79568 159 159 * Serializes the binding to XML for the lease database. 160 160 * 161 * @throw std::bad_alloc 161 * @throw std::bad_alloc 162 * @note DHCPServerImpl.cpp contains a reader, keep it in sync. 162 163 */ 163 164 void Binding::toXML(xml::ElementNode *pElmParent) const … … 204 205 * Deserializes the binding from the XML lease database. 205 206 * 206 * @param pElmLease The "Lease" element .207 * @param pElmLease The "Lease" element to serialize into. 207 208 * @return Pointer to the resulting binding, NULL on failure. 208 209 * @throw std::bad_alloc 210 * @note DHCPServerImpl.cpp contains a similar reader, keep it in sync. 209 211 */ 210 212 Binding *Binding::fromXML(const xml::ElementNode *pElmLease) 211 213 { 212 /* Lease/@network seems to always have bogus value, ignore it. */ 213 214 /* 215 * Lease/@mac 216 */ 217 RTCString strMac; 218 bool fHasMac = pElmLease->getAttributeValue("mac", &strMac); 219 if (!fHasMac) 220 return NULL; 214 /* Note! Lease/@network seems to always have bogus value, ignore it. */ 215 /* Note! We parse the mandatory attributes and elements first, then 216 the optional ones. This means things appear a little jumbled. */ 217 218 /* 219 * Lease/@mac - mandatory. 220 */ 221 const char *pszMacAddress = pElmLease->findAttributeValue("mac"); 222 if (!pszMacAddress) 223 DHCP_LOG_RET_NULL(("Binding::fromXML: <Lease> element without 'mac' attribute! Skipping lease.\n")); 221 224 222 225 RTMAC mac; 223 int rc = RTNetStrToMacAddr( strMac.c_str(), &mac);226 int rc = RTNetStrToMacAddr(pszMacAddress, &mac); 224 227 if (RT_FAILURE(rc)) 225 return NULL; 226 227 OptClientId id; 228 RTCString strId; 229 bool fHasId = pElmLease->getAttributeValue("id", &strId); 230 if (fHasId) 231 { 232 /* 233 * Decode from "de:ad:be:ef". 234 */ 235 /** @todo RTStrConvertHexBytes() doesn't grok colons */ 236 size_t cbBytes = strId.length() / 2; 237 uint8_t *pbBytes = new uint8_t[cbBytes]; 238 rc = RTStrConvertHexBytes(strId.c_str(), pbBytes, cbBytes, 0); 239 if (RT_SUCCESS(rc)) 240 { 241 try 242 { 243 std::vector<uint8_t> rawopt(pbBytes, pbBytes + cbBytes); 244 id = OptClientId(rawopt); 245 } 246 catch (std::bad_alloc &) 247 { 248 delete[] pbBytes; 249 throw; 250 } 251 } 252 delete[] pbBytes; 253 } 254 255 /* 256 * Lease/@state - not present in old leases file. We will try to 257 * infer from lease time below. 258 */ 259 RTCString strState; 260 bool fHasState = pElmLease->getAttributeValue("state", &strState); 261 262 /* 263 * Lease/Address 264 */ 265 const xml::ElementNode *ndAddress = pElmLease->findChildElement("Address"); 266 if (ndAddress == NULL) 267 return NULL; 268 269 /* 270 * Lease/Address/@value 271 */ 272 RTCString strAddress; 273 bool fHasValue = ndAddress->getAttributeValue("value", &strAddress); 274 if (!fHasValue) 275 return NULL; 228 DHCP_LOG_RET_NULL(("Binding::fromXML: Malformed mac address attribute value '%s': %Rrc - Skipping lease.\n", 229 pszMacAddress, rc)); 230 231 /* 232 * Lease/Address/@value - mandatory. 233 */ 234 const char *pszAddress = pElmLease->findChildElementAttributeValue("Address", "value"); 235 if (!pszAddress) 236 DHCP_LOG_RET_NULL(("Binding::fromXML: Could not find <Address> with a 'value' attribute! Skipping lease.\n")); 276 237 277 238 RTNETADDRIPV4 addr; 278 rc = RTNetStrToIPv4Addr( strAddress.c_str(), &addr);239 rc = RTNetStrToIPv4Addr(pszAddress, &addr); 279 240 if (RT_FAILURE(rc)) 280 return NULL; 281 282 /* 283 * Lease/Time 284 */ 285 const xml::ElementNode *ndTime = pElmLease->findChildElement("Time"); 286 if (ndTime == NULL) 287 return NULL; 288 289 /* 290 * Lease/Time/@issued 291 */ 292 int64_t issued; 293 bool fHasIssued = ndTime->getAttributeValue("issued", &issued); 294 if (!fHasIssued) 295 return NULL; 296 297 /* 298 * Lease/Time/@expiration 299 */ 300 uint32_t duration; 301 bool fHasExpiration = ndTime->getAttributeValue("expiration", &duration); 302 if (!fHasExpiration) 303 return NULL; 241 DHCP_LOG_RET_NULL(("Binding::fromXML: Malformed IPv4 address value '%s': %Rrc - Skipping lease.\n", pszAddress, rc)); 242 243 /* 244 * Lease/Time - mandatory. 245 */ 246 const xml::ElementNode *pElmTime = pElmLease->findChildElement("Time"); 247 if (pElmTime == NULL) 248 DHCP_LOG_RET_NULL(("Binding::fromXML: No <Time> element under <Lease mac=%RTmac>! Skipping lease.\n", &mac)); 249 250 /* 251 * Lease/Time/@issued - mandatory. 252 */ 253 int64_t secIssued; 254 if (!pElmTime->getAttributeValue("issued", &secIssued)) 255 DHCP_LOG_RET_NULL(("Binding::fromXML: <Time> element for %RTmac has no valid 'issued' attribute! Skipping lease.\n", &mac)); 256 257 /* 258 * Lease/Time/@expiration - mandatory. 259 */ 260 uint32_t cSecToLive; 261 if (!pElmTime->getAttributeValue("expiration", &cSecToLive)) 262 DHCP_LOG_RET_NULL(("Binding::fromXML: <Time> element for %RTmac has no valid 'expiration' attribute! Skipping lease.\n", &mac)); 304 263 305 264 std::unique_ptr<Binding> b(new Binding(addr)); 306 b->m_id = ClientId(mac, id); 307 308 if (fHasState) 309 { 310 b->m_issued = Timestamp::absSeconds(issued); 311 b->m_secLease = duration; 312 b->setState(strState.c_str()); 265 266 /* 267 * Lease/@state - mandatory but not present in old leases file, so pretent 268 * we're loading an expired one if absent. 269 */ 270 const char *pszState = pElmLease->findAttributeValue("state"); 271 if (pszState) 272 { 273 b->m_issued = Timestamp::absSeconds(secIssued); 274 b->setState(pszState); 313 275 } 314 276 else 315 277 { /** @todo XXX: old code wrote timestamps instead of absolute time. */ 316 278 /* pretend that lease has just ended */ 317 Timestamp fakeIssued = Timestamp::now(); 318 fakeIssued.subSeconds(duration); 319 b->m_issued = fakeIssued; 320 b->m_secLease = duration; 321 b->m_state = Binding::EXPIRED; 322 } 279 LogRel(("Binding::fromXML: No 'state' attribute for <Lease mac=%RTmac> (ts=%RI64 ttl=%RU32)! Assuming EXPIRED.\n", 280 &mac, secIssued, cSecToLive)); 281 b->m_issued = Timestamp::now().subSeconds(cSecToLive); 282 b->m_state = Binding::EXPIRED; 283 } 284 b->m_secLease = cSecToLive; 285 286 287 /* 288 * Lease/@id - optional, ignore if bad. 289 * Value format: "deadbeef..." or "de:ad:be:ef...". 290 */ 291 const char *pszClientId = pElmLease->findAttributeValue("id"); 292 if (pszClientId) 293 { 294 uint8_t abBytes[255]; 295 size_t cbActual; 296 rc = RTStrConvertHexBytesEx(pszClientId, abBytes, sizeof(abBytes), RTSTRCONVERTHEXBYTES_F_SEP_COLON, NULL, &cbActual); 297 if (RT_SUCCESS(rc)) 298 { 299 b->m_id = ClientId(mac, OptClientId(std::vector<uint8_t>(&abBytes[0], &abBytes[cbActual]))); /* throws bad_alloc */ 300 if (rc != VINF_BUFFER_UNDERFLOW && rc != VINF_SUCCESS) 301 LogRel(("Binding::fromXML: imperfect 'id' attribute: rc=%Rrc, cbActual=%u, '%s'\n", rc, cbActual, pszClientId)); 302 } 303 else 304 { 305 LogRel(("Binding::fromXML: ignoring malformed 'id' attribute: rc=%Rrc, cbActual=%u, '%s'\n", 306 rc, cbActual, pszClientId)); 307 b->m_id = ClientId(mac, OptClientId()); 308 } 309 } 310 else 311 b->m_id = ClientId(mac, OptClientId()); 323 312 324 313 return b.release(); … … 433 422 434 423 if (addr.u != 0) 435 Log DHCP(("> allocateAddress %RTnaipv4 to client %R[id]\n", addr.u, &id));424 LogRel(("> allocateAddress %RTnaipv4 to client %R[id]\n", addr.u, &id)); 436 425 else 437 Log DHCP(("> allocateAddress to client %R[id]\n", &id));426 LogRel(("> allocateAddress to client %R[id]\n", &id)); 438 427 439 428 /* … … 456 445 if (b->m_id == id) 457 446 { 458 Log DHCP(("> ... found existing binding %R[binding]\n", b));447 LogRel(("> ... found existing binding %R[binding]\n", b)); 459 448 return b; 460 449 } … … 464 453 Assert(addrBinding == NULL); 465 454 addrBinding = b; 466 Log DHCP(("> .... noted existing binding %R[binding]\n", addrBinding));455 LogRel(("> .... noted existing binding %R[binding]\n", addrBinding)); 467 456 } 468 457 … … 473 462 { 474 463 freeBinding = b; 475 Log DHCP(("> .... noted free binding %R[binding]\n", freeBinding));464 LogRel(("> .... noted free binding %R[binding]\n", freeBinding)); 476 465 continue; 477 466 } … … 485 474 { 486 475 reuseBinding = b; 487 Log DHCP(("> .... noted released binding %R[binding]\n", reuseBinding));476 LogRel(("> .... noted released binding %R[binding]\n", reuseBinding)); 488 477 } 489 478 } … … 495 484 { 496 485 reuseBinding = b; 497 Log DHCP(("> .... noted expired binding %R[binding]\n", reuseBinding));486 LogRel(("> .... noted expired binding %R[binding]\n", reuseBinding)); 498 487 } 499 488 } … … 510 499 addrBinding = i_createBinding(addr, id); 511 500 Assert(addrBinding != NULL); 512 Log DHCP(("> .... creating new binding for this address %R[binding]\n", addrBinding));501 LogRel(("> .... creating new binding for this address %R[binding]\n", addrBinding)); 513 502 return addrBinding; 514 503 } … … 516 505 if (addrBinding->m_state <= Binding::EXPIRED) /* not in use */ 517 506 { 518 Log DHCP(("> .... reusing %s binding for this address\n", addrBinding->stateName()));507 LogRel(("> .... reusing %s binding for this address\n", addrBinding->stateName())); 519 508 addrBinding->giveTo(id); 520 509 return addrBinding; 521 510 } 522 Log DHCP(("> .... cannot reuse %s binding for this address\n", addrBinding->stateName()));511 LogRel(("> .... cannot reuse %s binding for this address\n", addrBinding->stateName())); 523 512 } 524 513 … … 530 519 { 531 520 idBinding = freeBinding; 532 Log DHCP(("> .... reusing free binding\n"));521 LogRel(("> .... reusing free binding\n")); 533 522 } 534 523 else … … 536 525 idBinding = i_createBinding(); 537 526 if (idBinding != NULL) 538 Log DHCP(("> .... creating new binding\n"));527 LogRel(("> .... creating new binding\n")); 539 528 else 540 529 { 541 530 idBinding = reuseBinding; 542 531 if (idBinding != NULL) 543 Log DHCP(("> .... reusing %s binding %R[binding]\n", reuseBinding->stateName(), reuseBinding));532 LogRel(("> .... reusing %s binding %R[binding]\n", reuseBinding->stateName(), reuseBinding)); 544 533 else 545 { 546 LogDHCP(("> .... failed to allocate binding\n")); 547 return NULL; 548 } 534 DHCP_LOG_RET_NULL(("> .... failed to allocate binding\n")); 549 535 } 550 536 } 551 537 552 538 idBinding->giveTo(id); 553 Log DHCP(("> .... allocated %R[binding]\n", idBinding));539 LogRel(("> .... allocated %R[binding]\n", idBinding)); 554 540 555 541 return idBinding; … … 576 562 if (req.messageType() == RTNET_DHCP_MT_DISCOVER) 577 563 { 578 Log DHCP(("DISCOVER: ignoring invalid requested address\n"));564 LogRel(("DISCOVER: ignoring invalid requested address\n")); 579 565 reqAddr = OptRequestedAddress(); 580 566 } 581 567 else 582 { 583 LogDHCP(("rejecting invalid requested address\n")); 584 return NULL; 585 } 568 DHCP_LOG_RET_NULL(("rejecting invalid requested address\n")); 586 569 } 587 570 … … 621 604 if (!addressBelongs(pNewBinding->m_addr)) 622 605 { 623 Log DHCP(("Binding for out of range address %RTnaipv4 ignored\n", pNewBinding->m_addr.u));606 LogRel(("Binding for out of range address %RTnaipv4 ignored\n", pNewBinding->m_addr.u)); 624 607 return VERR_OUT_OF_RANGE; 625 608 } … … 631 614 if (pNewBinding->m_addr.u == b->m_addr.u) 632 615 { 633 Log DHCP(("> ADD: %R[binding]\n", pNewBinding));634 Log DHCP(("> .... duplicate ip: %R[binding]\n", b));616 LogRel(("> ADD: %R[binding]\n", pNewBinding)); 617 LogRel(("> .... duplicate ip: %R[binding]\n", b)); 635 618 return VERR_DUPLICATE; 636 619 } … … 638 621 if (pNewBinding->m_id == b->m_id) 639 622 { 640 Log DHCP(("> ADD: %R[binding]\n", pNewBinding));641 Log DHCP(("> .... duplicate id: %R[binding]\n", b));623 LogRel(("> ADD: %R[binding]\n", pNewBinding)); 624 LogRel(("> .... duplicate id: %R[binding]\n", b)); 642 625 return VERR_DUPLICATE; 643 626 } … … 734 717 int Db::writeLeases(const RTCString &strFilename) const RT_NOEXCEPT 735 718 { 736 Log DHCP(("writing leases to %s\n", strFilename.c_str()));719 LogRel(("writing leases to %s\n", strFilename.c_str())); 737 720 738 721 /** @todo This could easily be written directly to the file w/o going thru … … 777 760 catch (const xml::EIPRTFailure &e) 778 761 { 779 Log DHCP(("%s\n", e.what()));762 LogRel(("%s\n", e.what())); 780 763 return e.rc(); 781 764 } 782 765 catch (const RTCError &e) 783 766 { 784 Log DHCP(("%s\n", e.what()));767 LogRel(("%s\n", e.what())); 785 768 return VERR_GENERAL_FAILURE; 786 769 } 787 770 catch (...) 788 771 { 789 Log DHCP(("Unknown exception while writing '%s'\n", strFilename.c_str()));772 LogRel(("Unknown exception while writing '%s'\n", strFilename.c_str())); 790 773 return VERR_UNEXPECTED_EXCEPTION; 791 774 } … … 806 789 int Db::loadLeases(const RTCString &strFilename) RT_NOEXCEPT 807 790 { 808 Log DHCP(("loading leases from %s\n", strFilename.c_str()));791 LogRel(("loading leases from %s\n", strFilename.c_str())); 809 792 810 793 /* … … 819 802 catch (const xml::EIPRTFailure &e) 820 803 { 821 Log DHCP(("%s\n", e.what()));804 LogRel(("%s\n", e.what())); 822 805 return e.rc(); 823 806 } 824 807 catch (const RTCError &e) 825 808 { 826 Log DHCP(("%s\n", e.what()));809 LogRel(("%s\n", e.what())); 827 810 return VERR_GENERAL_FAILURE; 828 811 } 829 812 catch (...) 830 813 { 831 Log DHCP(("Unknown exception while reading and parsing '%s'\n", strFilename.c_str()));814 LogRel(("Unknown exception while reading and parsing '%s'\n", strFilename.c_str())); 832 815 return VERR_UNEXPECTED_EXCEPTION; 833 816 } … … 839 822 if (!pElmRoot) 840 823 { 841 Log DHCP(("No root element in '%s'\n", strFilename.c_str()));824 LogRel(("No root element in '%s'\n", strFilename.c_str())); 842 825 return VERR_NOT_FOUND; 843 826 } 844 827 if (!pElmRoot->nameEquals("Leases")) 845 828 { 846 Log DHCP(("No root element is not 'Leases' in '%s', but '%s'\n", strFilename.c_str(), pElmRoot->getName()));829 LogRel(("No root element is not 'Leases' in '%s', but '%s'\n", strFilename.c_str(), pElmRoot->getName())); 847 830 return VERR_NOT_FOUND; 848 831 } … … 864 847 } 865 848 else 866 Log DHCP(("Ignoring unexpected element '%s' under 'Leases'...\n", pElmLease->getName()));849 LogRel(("Ignoring unexpected element '%s' under 'Leases'...\n", pElmLease->getName())); 867 850 } 868 851 … … 892 875 bool fExpired = pBinding->expire(); 893 876 if (!fExpired) 894 Log DHCP(("> LOAD: lease %R[binding]\n", pBinding));877 LogRel(("> LOAD: lease %R[binding]\n", pBinding)); 895 878 else 896 Log DHCP(("> LOAD: EXPIRED lease %R[binding]\n", pBinding));879 LogRel(("> LOAD: EXPIRED lease %R[binding]\n", pBinding)); 897 880 898 881 int rc = i_addBinding(pBinding); … … 901 884 return rc; 902 885 } 903 Log DHCP(("> LOAD: failed to load lease!\n"));886 LogRel(("> LOAD: failed to load lease!\n")); 904 887 return VERR_PARSE_ERROR; 905 888 } -
trunk/src/VBox/NetworkServices/Dhcpd/Db.h
r79563 r79568 51 51 private: 52 52 const RTNETADDRIPV4 m_addr; 53 State m_state;54 ClientId m_id;55 Timestamp m_issued;56 uint32_t m_secLease;53 State m_state; 54 ClientId m_id; 55 Timestamp m_issued; 56 uint32_t m_secLease; 57 57 58 58 public: -
trunk/src/VBox/NetworkServices/Dhcpd/DhcpMessage.cpp
r79563 r79568 16 16 */ 17 17 18 19 /********************************************************************************************************************************* 20 * Header Files * 21 *********************************************************************************************************************************/ 18 22 #include "DhcpdInternal.h" 19 23 #include "DhcpMessage.h" … … 21 25 22 26 #include <iprt/string.h> 23 #include <iprt/stream.h>24 27 25 28 26 29 27 30 DhcpMessage::DhcpMessage() 28 : m_xid(0), m_flags(0), 29 m_ciaddr(), m_yiaddr(), m_siaddr(), m_giaddr(), 30 m_sname(), m_file(), 31 m_optMessageType() 32 { 33 } 34 31 : m_xid(0) 32 , m_flags(0) 33 , m_ciaddr() 34 , m_yiaddr() 35 , m_siaddr() 36 , m_giaddr() 37 #if 0 /* not currently unused */ 38 , m_sname() 39 , m_file() 40 #endif 41 , m_optMessageType() 42 { 43 } 44 45 46 /********************************************************************************************************************************* 47 * DhcpClientMessage Implementation * 48 *********************************************************************************************************************************/ 35 49 36 50 /* static */ 37 51 DhcpClientMessage *DhcpClientMessage::parse(bool broadcasted, const void *buf, size_t buflen) 38 52 { 53 /* 54 * Validate the request. 55 */ 39 56 if (buflen < RT_OFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts)) 40 { 41 RTPrintf("%s: %zu bytes datagram is too short\n", __FUNCTION__, buflen); 42 return NULL; 43 } 57 DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: %zu bytes datagram is too short\n", buflen)); 44 58 45 59 PCRTNETBOOTP bp = (PCRTNETBOOTP)buf; 46 60 47 61 if (bp->bp_op != RTNETBOOTP_OP_REQUEST) 48 { 49 RTPrintf("%s: bad opcode: %d\n", __FUNCTION__, bp->bp_op); 50 return NULL; 51 } 62 DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: bad opcode: %d\n", bp->bp_op)); 52 63 53 64 if (bp->bp_htype != RTNET_ARP_ETHER) 54 { 55 RTPrintf("%s: unsupported htype %d\n", __FUNCTION__, bp->bp_htype); 56 return NULL; 57 } 65 DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: unsupported htype %d\n", bp->bp_htype)); 58 66 59 67 if (bp->bp_hlen != sizeof(RTMAC)) 60 { 61 RTPrintf("%s: unexpected hlen %d\n", __FUNCTION__, bp->bp_hlen); 62 return NULL; 63 } 68 DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: unexpected hlen %d\n", bp->bp_hlen)); 64 69 65 70 if ( (bp->bp_chaddr.Mac.au8[0] & 0x01) != 0 66 71 && (bp->bp_flags & RTNET_DHCP_FLAG_BROADCAST) == 0) 67 { 68 RTPrintf("%s: multicast chaddr %RTmac without broadcast flag\n", 69 __FUNCTION__, &bp->bp_chaddr.Mac); 70 } 72 LogRel2(("DhcpClientMessage::parse: multicast chaddr %RTmac without broadcast flag\n", &bp->bp_chaddr.Mac)); 71 73 72 74 /* we don't want to deal with forwarding */ 73 75 if (bp->bp_giaddr.u != 0) 74 { 75 RTPrintf("%s: giaddr %RTnaipv4\n", __FUNCTION__, bp->bp_giaddr.u); 76 return NULL; 77 } 76 DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: giaddr %RTnaipv4\n", bp->bp_giaddr.u)); 78 77 79 78 if (bp->bp_hops != 0) 80 { 81 RTPrintf("%s: non-zero hops %d\n", __FUNCTION__, bp->bp_hops); 82 return NULL; 83 } 84 79 DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: non-zero hops %d\n", bp->bp_hops)); 80 81 if (bp->bp_vend.Dhcp.dhcp_cookie != RT_H2N_U32_C(RTNET_DHCP_COOKIE)) 82 DHCP_LOG2_RET_NULL(("DhcpClientMessage::parse: bad cookie %#RX32\n", bp->bp_vend.Dhcp.dhcp_cookie)); 83 84 /* 85 * Convert it into a DhcpClientMessage instance. 86 */ 85 87 std::unique_ptr<DhcpClientMessage> msg(new DhcpClientMessage()); 86 88 87 89 msg->m_broadcasted = broadcasted; 88 89 msg->m_xid = bp->bp_xid; 90 msg->m_flags = bp->bp_flags; 91 92 msg->m_mac = bp->bp_chaddr.Mac; 93 94 msg->m_ciaddr = bp->bp_ciaddr; 95 msg->m_yiaddr = bp->bp_yiaddr; 96 msg->m_siaddr = bp->bp_siaddr; 97 msg->m_giaddr = bp->bp_giaddr; 98 99 if (bp->bp_vend.Dhcp.dhcp_cookie != RT_H2N_U32_C(RTNET_DHCP_COOKIE)) 100 { 101 RTPrintf("bad cookie\n"); 102 return NULL; 103 } 104 105 int fOptOverload = msg->parseOptions(&bp->bp_vend.Dhcp.dhcp_opts, buflen - RT_OFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts)); 90 msg->m_xid = bp->bp_xid; 91 msg->m_flags = bp->bp_flags; 92 msg->m_mac = bp->bp_chaddr.Mac; 93 msg->m_ciaddr = bp->bp_ciaddr; 94 msg->m_yiaddr = bp->bp_yiaddr; 95 msg->m_siaddr = bp->bp_siaddr; 96 msg->m_giaddr = bp->bp_giaddr; 97 98 int fOptOverload = msg->i_parseOptions(&bp->bp_vend.Dhcp.dhcp_opts[0], 99 buflen - RT_OFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts)); 106 100 if (fOptOverload < 0) 107 101 return NULL; … … 110 104 if (fOptOverload & RTNET_DHCP_OPTION_OVERLOAD_FILE) 111 105 { 112 int status = msg-> parseOptions(bp->bp_file, sizeof(bp->bp_file));106 int status = msg->i_parseOptions(bp->bp_file, sizeof(bp->bp_file)); 113 107 if (status != 0) 114 108 return NULL; 115 109 } 110 #if 0 /* not currently unused */ 116 111 else if (bp->bp_file[0] != '\0') 117 112 { … … 122 117 msg->m_file.assign(pszFile, len); 123 118 } 119 #endif 124 120 125 121 /* "... followed by the 'sname' field." */ 126 122 if (fOptOverload & RTNET_DHCP_OPTION_OVERLOAD_SNAME) 127 123 { 128 int status = msg-> parseOptions(bp->bp_sname, sizeof(bp->bp_sname));124 int status = msg->i_parseOptions(bp->bp_sname, sizeof(bp->bp_sname)); 129 125 if (status != 0) /* NB: this includes "nested" Option Overload */ 130 126 return NULL; 131 127 } 128 #if 0 /* not currently unused */ 132 129 else if (bp->bp_sname[0] != '\0') 133 130 { … … 138 135 msg->m_sname.assign(pszSName, len); 139 136 } 137 #endif 140 138 141 139 msg->m_optMessageType = OptMessageType(*msg); … … 149 147 150 148 151 int DhcpClientMessage::parseOptions(const void *buf, size_t buflen) 152 { 153 int fOptOverload = 0; 154 const uint8_t *data = static_cast<const uint8_t *>(buf); 155 while (buflen > 0) 156 { 157 uint8_t const opt = *data++; 158 --buflen; 159 160 if (opt == RTNET_DHCP_OPT_PAD) 149 int DhcpClientMessage::i_parseOptions(const uint8_t *pbBuf, size_t cbBuf) RT_NOEXCEPT 150 { 151 int fOptOverload = 0; 152 while (cbBuf > 0) 153 { 154 uint8_t const bOpt = *pbBuf++; 155 --cbBuf; 156 157 if (bOpt == RTNET_DHCP_OPT_PAD) 161 158 continue; 162 159 163 if (opt == RTNET_DHCP_OPT_END) 164 break; 165 166 if (buflen == 0) 167 { 168 RTPrintf("option %d has no length field\n", opt); 169 return -1; 170 } 171 172 uint8_t optlen = *data++; 173 --buflen; 174 175 if (optlen > buflen) 176 { 177 RTPrintf("option %d truncated (length %d, but only %lu bytes left)\n", opt, optlen, (unsigned long)buflen); 178 return -1; 179 } 160 if (bOpt == RTNET_DHCP_OPT_END) 161 break; 162 163 if (cbBuf == 0) 164 DHCP_LOG_RET(-1, ("option %d has no length field\n", bOpt)); 165 166 uint8_t const cbOpt = *pbBuf++; 167 --cbBuf; 168 169 if (cbOpt > cbBuf) 170 DHCP_LOG_RET(-1, ("option %d truncated (length %d, but only %zu bytes left)\n", bOpt, cbOpt, cbBuf)); 180 171 181 172 #if 0 182 rawopts_t::const_iterator it(m_optmap.find( opt));173 rawopts_t::const_iterator it(m_optmap.find(bOpt)); 183 174 if (it != m_optmap.cend()) 184 175 return -1; 185 176 #endif 186 if ( opt == RTNET_DHCP_OPT_OPTION_OVERLOAD)177 if (bOpt == RTNET_DHCP_OPT_OPTION_OVERLOAD) 187 178 { 188 if (optlen != 1) 189 { 190 RTPrintf("Overload Option (option %d) has invalid length %d\n", opt, optlen); 191 return -1; 192 } 193 194 fOptOverload = *data; 179 if (cbOpt != 1) 180 DHCP_LOG_RET(-1, ("Overload Option (option %d) has invalid length %d\n", bOpt, cbOpt)); 181 182 fOptOverload = *pbBuf; 195 183 196 184 if ((fOptOverload & ~RTNET_DHCP_OPTION_OVERLOAD_MASK) != 0) 197 { 198 RTPrintf("Overload Option (option %d) has invalid value 0x%x\n", opt, fOptOverload); 199 return -1; 200 } 185 DHCP_LOG_RET(-1, ("Overload Option (option %d) has invalid value 0x%x\n", bOpt, fOptOverload)); 201 186 } 202 187 else 203 m_rawopts.insert(std::make_pair( opt, octets_t(data, data + optlen)));204 205 data += optlen;206 buflen -= optlen;188 m_rawopts.insert(std::make_pair(bOpt, octets_t(pbBuf, pbBuf + cbOpt))); 189 190 pbBuf += cbOpt; 191 cbBuf -= cbOpt; 207 192 } 208 193 … … 211 196 212 197 213 void DhcpClientMessage::dump() const 198 void DhcpClientMessage::dump() const RT_NOEXCEPT 214 199 { 215 200 switch (m_optMessageType.value()) 216 201 { 217 202 case RTNET_DHCP_MT_DISCOVER: 218 Log DHCP(("DISCOVER"));203 LogRel(("DISCOVER")); 219 204 break; 220 205 221 206 case RTNET_DHCP_MT_REQUEST: 222 Log DHCP(("REQUEST"));207 LogRel(("REQUEST")); 223 208 break; 224 209 225 210 case RTNET_DHCP_MT_INFORM: 226 Log DHCP(("INFORM"));211 LogRel(("INFORM")); 227 212 break; 228 213 229 214 case RTNET_DHCP_MT_DECLINE: 230 Log DHCP(("DECLINE"));215 LogRel(("DECLINE")); 231 216 break; 232 217 233 218 case RTNET_DHCP_MT_RELEASE: 234 Log DHCP(("RELEASE"));219 LogRel(("RELEASE")); 235 220 break; 236 221 237 222 default: 238 Log DHCP(("<Unknown Mesage Type %d>", m_optMessageType.value()));223 LogRel(("<Unknown Mesage Type %d>", m_optMessageType.value())); 239 224 break; 240 225 } 241 226 242 227 if (OptRapidCommit(*this).present()) 243 LogDHCP((" (rapid commit)")); 244 245 246 const OptServerId sid(*this); 247 if (sid.present()) 248 LogDHCP((" for server %RTnaipv4", sid.value().u)); 249 250 LogDHCP((" xid 0x%08x", m_xid)); 251 LogDHCP((" chaddr %RTmac\n", &m_mac)); 252 253 const OptClientId cid(*this); 254 if (cid.present()) { 255 if (cid.value().size() > 0) 256 LogDHCP((" client id: %.*Rhxs\n", cid.value().size(), &cid.value().front())); 257 else 258 LogDHCP((" client id: <empty>\n")); 259 } 260 261 LogDHCP((" ciaddr %RTnaipv4", m_ciaddr.u)); 262 if (m_yiaddr.u != 0) 263 LogDHCP((" yiaddr %RTnaipv4", m_yiaddr.u)); 264 if (m_siaddr.u != 0) 265 LogDHCP((" siaddr %RTnaipv4", m_siaddr.u)); 266 if (m_giaddr.u != 0) 267 LogDHCP((" giaddr %RTnaipv4", m_giaddr.u)); 268 LogDHCP(("%s\n", broadcast() ? "broadcast" : "")); 269 270 271 const OptRequestedAddress reqAddr(*this); 272 if (reqAddr.present()) 273 LogDHCP((" requested address %RTnaipv4", reqAddr.value().u)); 274 const OptLeaseTime reqLeaseTime(*this); 275 if (reqLeaseTime.present()) 276 LogDHCP((" requested lease time %d", reqAddr.value())); 277 if (reqAddr.present() || reqLeaseTime.present()) 278 LogDHCP(("\n")); 279 280 const OptParameterRequest params(*this); 281 if (params.present()) 282 { 283 LogDHCP((" params {")); 284 typedef OptParameterRequest::value_t::const_iterator it_t; 285 for (it_t it = params.value().begin(); it != params.value().end(); ++it) 286 LogDHCP((" %d", *it)); 287 LogDHCP((" }\n")); 228 LogRel((" (rapid commit)")); 229 230 try 231 { 232 const OptServerId sid(*this); 233 if (sid.present()) 234 LogRel((" for server %RTnaipv4", sid.value().u)); 235 236 LogRel((" xid 0x%08x", m_xid)); 237 LogRel((" chaddr %RTmac\n", &m_mac)); 238 239 const OptClientId cid(*this); 240 if (cid.present()) { 241 if (cid.value().size() > 0) 242 LogRel((" client id: %.*Rhxs\n", cid.value().size(), &cid.value().front())); 243 else 244 LogRel((" client id: <empty>\n")); 245 } 246 247 LogRel((" ciaddr %RTnaipv4", m_ciaddr.u)); 248 if (m_yiaddr.u != 0) 249 LogRel((" yiaddr %RTnaipv4", m_yiaddr.u)); 250 if (m_siaddr.u != 0) 251 LogRel((" siaddr %RTnaipv4", m_siaddr.u)); 252 if (m_giaddr.u != 0) 253 LogRel((" giaddr %RTnaipv4", m_giaddr.u)); 254 LogRel(("%s\n", broadcast() ? "broadcast" : "")); 255 256 257 const OptRequestedAddress reqAddr(*this); 258 if (reqAddr.present()) 259 LogRel((" requested address %RTnaipv4", reqAddr.value().u)); 260 const OptLeaseTime reqLeaseTime(*this); 261 if (reqLeaseTime.present()) 262 LogRel((" requested lease time %d", reqAddr.value())); 263 if (reqAddr.present() || reqLeaseTime.present()) 264 LogRel(("\n")); 265 266 const OptParameterRequest params(*this); 267 if (params.present()) 268 { 269 LogRel((" params {")); 270 typedef OptParameterRequest::value_t::const_iterator it_t; 271 for (it_t it = params.value().begin(); it != params.value().end(); ++it) 272 LogRel((" %d", *it)); 273 LogRel((" }\n")); 274 } 275 } 276 catch (std::bad_alloc &) 277 { 278 LogRel(("bad_alloc during dumping\n")); 288 279 } 289 280 … … 292 283 { 293 284 const uint8_t optcode = (*it).first; 294 switch (optcode) { 285 switch (optcode) 286 { 295 287 case OptMessageType::optcode: /* FALLTHROUGH */ 296 288 case OptClientId::optcode: /* FALLTHROUGH */ … … 304 296 if (fHeader) 305 297 { 306 Log DHCP((" other options:"));298 LogRel((" other options:")); 307 299 fHeader = false; 308 300 } 309 Log DHCP((" %d", optcode));301 LogRel((" %d", optcode)); 310 302 break; 311 303 } 312 304 } 313 305 if (!fHeader) 314 LogDHCP(("\n")); 315 } 316 317 318 DhcpServerMessage::DhcpServerMessage(const DhcpClientMessage &req, 319 uint8_t messageTypeParam, RTNETADDRIPV4 serverAddr) 320 : DhcpMessage(), 321 m_optServerId(serverAddr) 306 LogRel(("\n")); 307 } 308 309 310 311 /********************************************************************************************************************************* 312 * DhcpServerMessage Implementation * 313 *********************************************************************************************************************************/ 314 315 DhcpServerMessage::DhcpServerMessage(const DhcpClientMessage &req, uint8_t messageTypeParam, RTNETADDRIPV4 serverAddr) 316 : DhcpMessage() 317 , m_optServerId(serverAddr) 322 318 { 323 319 m_dst.u = 0xffffffff; /* broadcast */ … … 336 332 337 333 338 void DhcpServerMessage::maybeUnicast(const DhcpClientMessage &req) 334 void DhcpServerMessage::maybeUnicast(const DhcpClientMessage &req) RT_NOEXCEPT 339 335 { 340 336 if (!req.broadcast() && req.ciaddr().u != 0) … … 343 339 344 340 341 /** 342 * @throws std::bad_alloc 343 */ 345 344 void DhcpServerMessage::addOption(DhcpOption *opt) 346 345 { … … 349 348 350 349 350 /** 351 * @throws std::bad_alloc 352 */ 351 353 void DhcpServerMessage::addOptions(const optmap_t &optmap) 352 354 { 353 for (optmap_t::const_iterator it ( optmap.begin() ); 354 it != optmap.end(); ++it) 355 { 355 for (optmap_t::const_iterator it( optmap.begin() ); it != optmap.end(); ++it) 356 356 m_optmap << it->second; 357 } 358 } 359 360 357 } 358 359 360 /** 361 * @throws std::bad_alloc 362 */ 361 363 int DhcpServerMessage::encode(octets_t &data) 362 364 { … … 393 395 it != m_optmap.end(); ++it) 394 396 { 395 RTPrintf("encoding option %d\n", it->first);397 LogRel3(("encoding option %d\n", it->first)); 396 398 DhcpOption &opt = *it->second; 397 399 data << opt; … … 400 402 data << OptEnd(); 401 403 402 if (data.size() < 548) /* XXX */ 403 data.resize(548); 404 AssertCompile(RTNET_DHCP_NORMAL_SIZE == 548); 405 if (data.size() < RTNET_DHCP_NORMAL_SIZE) /* XXX */ 406 data.resize(RTNET_DHCP_NORMAL_SIZE); 404 407 405 408 return VINF_SUCCESS; -
trunk/src/VBox/NetworkServices/Dhcpd/DhcpMessage.h
r79563 r79568 29 29 30 30 31 31 /** 32 * Base class for internal DHCP client and server message representations. 33 */ 32 34 class DhcpMessage 33 35 { 34 36 protected: 35 uint32_t m_xid;36 uint16_t m_flags;37 uint32_t m_xid; 38 uint16_t m_flags; 37 39 38 RTMAC m_mac;40 RTMAC m_mac; 39 41 40 42 RTNETADDRIPV4 m_ciaddr; … … 43 45 RTNETADDRIPV4 m_giaddr; 44 46 45 RTCString m_sname; 46 RTCString m_file; 47 #if 0 /* not currently unused, so avoid wasting time on them for now. */ 48 RTCString m_sname; /**< @note Not necessarily UTF-8 clean. */ 49 RTCString m_file; /**< @note Not necessarily UTF-8 clean. */ 50 #endif 47 51 48 OptMessageType m_optMessageType; 52 OptMessageType m_optMessageType; 53 54 protected: 55 DhcpMessage(); 49 56 50 57 public: 51 DhcpMessage(); 58 /** @name Accessors 59 * @{ */ 60 uint32_t xid() const RT_NOEXCEPT { return m_xid; } 52 61 62 uint16_t flags() const RT_NOEXCEPT { return m_flags; } 63 bool broadcast() const RT_NOEXCEPT { return (m_flags & RTNET_DHCP_FLAG_BROADCAST) != 0; } 53 64 54 uint32_t xid() const { return m_xid; }65 const RTMAC &mac() const RT_NOEXCEPT { return m_mac; } 55 66 56 uint16_t flags() const { return m_flags; } 57 bool broadcast() const { return (m_flags & RTNET_DHCP_FLAG_BROADCAST) != 0; } 67 RTNETADDRIPV4 ciaddr() const RT_NOEXCEPT { return m_ciaddr; } 68 RTNETADDRIPV4 yiaddr() const RT_NOEXCEPT { return m_yiaddr; } 69 RTNETADDRIPV4 siaddr() const RT_NOEXCEPT { return m_siaddr; } 70 RTNETADDRIPV4 giaddr() const RT_NOEXCEPT { return m_giaddr; } 58 71 59 const RTMAC &mac() const { return m_mac; } 72 void setCiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT { m_ciaddr = addr; } 73 void setYiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT { m_yiaddr = addr; } 74 void setSiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT { m_siaddr = addr; } 75 void setGiaddr(RTNETADDRIPV4 addr) RT_NOEXCEPT { m_giaddr = addr; } 60 76 61 RTNETADDRIPV4 ciaddr() const { return m_ciaddr; } 62 RTNETADDRIPV4 yiaddr() const { return m_yiaddr; } 63 RTNETADDRIPV4 siaddr() const { return m_siaddr; } 64 RTNETADDRIPV4 giaddr() const { return m_giaddr; } 65 66 void setCiaddr(RTNETADDRIPV4 addr) { m_ciaddr = addr; } 67 void setYiaddr(RTNETADDRIPV4 addr) { m_yiaddr = addr; } 68 void setSiaddr(RTNETADDRIPV4 addr) { m_siaddr = addr; } 69 void setGiaddr(RTNETADDRIPV4 addr) { m_giaddr = addr; } 70 71 uint8_t messageType() const RT_NOEXCEPT 77 uint8_t messageType() const RT_NOEXCEPT 72 78 { 73 79 Assert(m_optMessageType.present()); 74 80 return m_optMessageType.value(); 75 81 } 82 /** @} */ 76 83 }; 77 84 78 85 86 /** 87 * Decoded DHCP client message. 88 * 89 * This is the internal decoded representation of a DHCP message picked up from 90 * the wire. 91 */ 79 92 class DhcpClientMessage 80 : public DhcpMessage93 : public DhcpMessage 81 94 { 82 95 protected: 83 rawopts_t m_rawopts;84 ClientId m_id;85 bool m_broadcasted;96 rawopts_t m_rawopts; 97 ClientId m_id; 98 bool m_broadcasted; 86 99 87 100 public: 88 101 static DhcpClientMessage *parse(bool broadcasted, const void *buf, size_t buflen); 89 102 90 bool broadcasted() const { return m_broadcasted; } 103 /** @name Getters 104 * @{ */ 105 bool broadcasted() const RT_NOEXCEPT { return m_broadcasted; } 106 const rawopts_t &rawopts() const RT_NOEXCEPT { return m_rawopts; } 107 const ClientId &clientId() const RT_NOEXCEPT { return m_id; } 108 /** @} */ 91 109 92 const rawopts_t &rawopts() const { return m_rawopts; } 93 const ClientId &clientId() const { return m_id; } 94 95 void dump() const; 110 void dump() const RT_NOEXCEPT; 96 111 97 112 protected: 98 int parseOptions(const void *buf, size_t buflen);113 int i_parseOptions(const uint8_t *pbBuf, size_t cbBuf) RT_NOEXCEPT; 99 114 }; 100 115 101 116 102 117 118 /** 119 * DHCP server message for encoding. 120 */ 103 121 class DhcpServerMessage 104 : public DhcpMessage122 : public DhcpMessage 105 123 { 106 124 protected: 107 RTNETADDRIPV4 m_dst; 108 109 OptServerId m_optServerId; 110 111 optmap_t m_optmap; 125 RTNETADDRIPV4 m_dst; 126 OptServerId m_optServerId; 127 optmap_t m_optmap; 112 128 113 129 public: 114 DhcpServerMessage(const DhcpClientMessage &req, 115 uint8_t messageType, RTNETADDRIPV4 serverAddr); 130 DhcpServerMessage(const DhcpClientMessage &req, uint8_t messageType, RTNETADDRIPV4 serverAddr); 116 131 117 RTNETADDRIPV4 dst() const { return m_dst; } 118 void setDst(RTNETADDRIPV4 aDst) { m_dst = aDst; } 132 /** @name Accessors 133 * @{ */ 134 RTNETADDRIPV4 dst() const RT_NOEXCEPT { return m_dst; } 135 void setDst(RTNETADDRIPV4 aDst) RT_NOEXCEPT { m_dst = aDst; } 119 136 120 void maybeUnicast(const DhcpClientMessage &req);137 void maybeUnicast(const DhcpClientMessage &req) RT_NOEXCEPT; 121 138 122 void addOption(DhcpOption *opt); 123 void addOption(const DhcpOption &opt) 124 { 125 addOption(opt.clone()); 126 } 139 void addOption(DhcpOption *opt); 140 void addOption(const DhcpOption &opt) { addOption(opt.clone()); } 127 141 128 void addOptions(const optmap_t &optmap); 142 void addOptions(const optmap_t &optmap); 143 /** @} */ 129 144 130 int encode(octets_t &data);145 int encode(octets_t &data); 131 146 }; 132 147 -
trunk/src/VBox/NetworkServices/Dhcpd/DhcpdInternal.h
r79530 r79568 63 63 64 64 65 /** @def LogDHCP 66 * Wrapper around LogRel. */ 67 #if 1 68 # define LogDHCP LogRel 69 #else 70 # include <iprt/stream.h> 71 # define LogDHCP(args) RTPrintf args 72 #endif 65 /** @name LogXRel + return NULL helpers 66 * @{ */ 67 #define DHCP_LOG_RET_NULL(a_MsgArgs) do { LogRel(a_MsgArgs); return NULL; } while (0) 68 #define DHCP_LOG2_RET_NULL(a_MsgArgs) do { LogRel2(a_MsgArgs); return NULL; } while (0) 69 #define DHCP_LOG3_RET_NULL(a_MsgArgs) do { LogRel3(a_MsgArgs); return NULL; } while (0) 70 /** @} */ 71 72 73 /** @name LogXRel + return a_rcRet helpers 74 * @{ */ 75 #define DHCP_LOG_RET(a_rcRet, a_MsgArgs) do { LogRel(a_MsgArgs); return (a_rcRet); } while (0) 76 #define DHCP_LOG2_RET(a_rcRet, a_MsgArgs) do { LogRel2(a_MsgArgs); return (a_rcRet); } while (0) 77 #define DHCP_LOG3_RET(a_rcRet, a_MsgArgs) do { LogRel3(a_MsgArgs); return (a_rcRet); } while (0) 78 /** @} */ 73 79 74 80 #endif /* !VBOX_INCLUDED_SRC_Dhcpd_DhcpdInternal_h */ -
trunk/src/VBox/NetworkServices/Dhcpd/IPv4Pool.cpp
r79563 r79568 76 76 if (a_Range.FirstAddr <= prev->LastAddr) 77 77 { 78 Log DHCP(("%08x-%08x conflicts with %08x-%08x\n",78 LogRel(("%08x-%08x conflicts with %08x-%08x\n", 79 79 a_Range.FirstAddr, a_Range.LastAddr, 80 80 prev->FirstAddr, prev->LastAddr)); … … 127 127 IPv4Range trimmed = *itBeg; 128 128 trimmed.FirstAddr += 1; 129 Assert(trimmed.FirstAddr <= trimmed.LastAddr); 129 130 m_pool.erase(itBeg); 130 131 try -
trunk/src/VBox/NetworkServices/Dhcpd/VBoxNetDhcpd.cpp
r79563 r79568 741 741 || ip_addr_cmp(ip_current_dest_addr(), &ip_addr_any); 742 742 743 DhcpClientMessage *msgIn = DhcpClientMessage::parse(broadcasted, p->payload, p->len); 744 if (msgIn == NULL) 745 return; 746 747 std::unique_ptr<DhcpClientMessage> autoFreeMsgIn(msgIn); 748 749 DhcpServerMessage *msgOut = m_server.process(*msgIn); 750 if (msgOut == NULL) 751 return; 752 753 std::unique_ptr<DhcpServerMessage> autoFreeMsgOut(msgOut); 754 755 ip_addr_t dst = { msgOut->dst().u }; 756 if (ip_addr_cmp(&dst, &ip_addr_any)) 757 ip_addr_copy(dst, ip_addr_broadcast); 758 759 octets_t data; 760 int rc = msgOut->encode(data); 761 if (RT_FAILURE(rc)) 762 return; 763 764 unique_ptr_pbuf q ( pbuf_alloc(PBUF_RAW, (u16_t)data.size(), PBUF_RAM) ); 765 if (!q) 766 return; 767 768 err_t error = pbuf_take(q.get(), &data.front(), (u16_t)data.size()); 769 if (error != ERR_OK) 770 return; 771 772 error = udp_sendto(pcb, q.get(), &dst, RTNETIPV4_PORT_BOOTPC); 773 if (error != ERR_OK) 774 return; 743 try 744 { 745 DhcpClientMessage *msgIn = DhcpClientMessage::parse(broadcasted, p->payload, p->len); 746 if (msgIn == NULL) 747 return; 748 749 std::unique_ptr<DhcpClientMessage> autoFreeMsgIn(msgIn); 750 751 DhcpServerMessage *msgOut = m_server.process(*msgIn); 752 if (msgOut == NULL) 753 return; 754 755 std::unique_ptr<DhcpServerMessage> autoFreeMsgOut(msgOut); 756 757 ip_addr_t dst = { msgOut->dst().u }; 758 if (ip_addr_cmp(&dst, &ip_addr_any)) 759 ip_addr_copy(dst, ip_addr_broadcast); 760 761 octets_t data; 762 int rc = msgOut->encode(data); 763 if (RT_FAILURE(rc)) 764 return; 765 766 unique_ptr_pbuf q ( pbuf_alloc(PBUF_RAW, (u16_t)data.size(), PBUF_RAM) ); 767 if (!q) 768 return; 769 770 err_t error = pbuf_take(q.get(), &data.front(), (u16_t)data.size()); 771 if (error != ERR_OK) 772 return; 773 774 error = udp_sendto(pcb, q.get(), &dst, RTNETIPV4_PORT_BOOTPC); 775 if (error != ERR_OK) 776 return; 777 } 778 catch (std::bad_alloc &) 779 { 780 LogRel(("VBoxNetDhcpd::dhcp4Recv: Caught std::bad_alloc!\n")); 781 } 775 782 } 776 783
Note:
See TracChangeset
for help on using the changeset viewer.