Changeset 79514 in vbox for trunk/src/VBox/NetworkServices/Dhcpd
- Timestamp:
- Jul 4, 2019 8:01:58 AM (6 years ago)
- svn:sync-xref-src-repo-rev:
- 131793
- Location:
- trunk/src/VBox/NetworkServices/Dhcpd
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/NetworkServices/Dhcpd/Config.cpp
r79509 r79514 18 18 #include "Config.h" 19 19 20 #include <iprt/ types.h>20 #include <iprt/ctype.h> 21 21 #include <iprt/net.h> /* NB: must come before getopt.h */ 22 22 #include <iprt/getopt.h> … … 88 88 89 89 90 void Config::setNetwork(const std::string &aStrNetwork)91 { 92 AssertReturnVoid(m_strNetwork. empty());90 void Config::setNetwork(const RTCString &aStrNetwork) 91 { 92 AssertReturnVoid(m_strNetwork.isEmpty()); 93 93 94 94 m_strNetwork = aStrNetwork; … … 104 104 int Config::logInit() 105 105 { 106 int rc; 107 size_t cch; 108 109 if (m_strHome.empty() || m_strBaseName.empty()) 110 return VERR_GENERAL_FAILURE; 106 if (m_strHome.isEmpty() || m_strBaseName.isEmpty()) 107 return VERR_PATH_ZERO_LENGTH; 111 108 112 109 /* default log file name */ 113 110 char szLogFile[RTPATH_MAX]; 114 cch = RTStrPrintf(szLogFile, sizeof(szLogFile),115 "%s%c%s-Dhcpd.log",116 m_strHome.c_str(), RTPATH_DELIMITER, m_strBaseName.c_str());117 if (cch >= sizeof(szLogFile))111 ssize_t cch = RTStrPrintf2(szLogFile, sizeof(szLogFile), 112 "%s%c%s-Dhcpd.log", 113 m_strHome.c_str(), RTPATH_DELIMITER, m_strBaseName.c_str()); 114 if (cch <= 0) 118 115 return VERR_BUFFER_OVERFLOW; 119 116 120 121 /* get a writable copy of the base name */ 122 char szBaseName[RTPATH_MAX]; 123 rc = RTStrCopy(szBaseName, sizeof(szBaseName), m_strBaseName.c_str()); 124 if (RT_FAILURE(rc)) 125 return rc; 126 127 /* sanitize base name some more to be usable in an environment variable name */ 128 for (char *p = szBaseName; *p != '\0'; ++p) 129 { 130 if ( *p != '_' 131 && (*p < '0' || '9' < *p) 132 && (*p < 'a' || 'z' < *p) 133 && (*p < 'A' || 'Z' < *p)) 134 { 117 /* Sanitize base name some more to be usable in an environment variable name: */ 118 char szEnvVarBase[128]; 119 cch = RTStrPrintf(szEnvVarBase, sizeof(szEnvVarBase), "VBOXDHCP_%s_RELEASE_LOG", m_strBaseName.c_str()); 120 if (cch <= 0) 121 return VERR_BUFFER_OVERFLOW; 122 for (char *p = szEnvVarBase; *p != '\0'; ++p) 123 if (*p != '_' && !RT_C_IS_ALNUM(*p)) 135 124 *p = '_'; 136 } 137 } 138 139 140 /* name of the environment variable to control logging */ 141 char szEnvVarBase[128]; 142 cch = RTStrPrintf(szEnvVarBase, sizeof(szEnvVarBase), 143 "VBOXDHCP_%s_RELEASE_LOG", szBaseName); 144 if (cch >= sizeof(szEnvVarBase)) 145 return VERR_BUFFER_OVERFLOW; 146 147 148 rc = com::VBoxLogRelCreate("DHCP Server", 149 szLogFile, 150 RTLOGFLAGS_PREFIX_TIME_PROG, 151 "all all.restrict -default.restrict", 152 szEnvVarBase, 153 RTLOGDEST_FILE 125 126 127 int rc = com::VBoxLogRelCreate("DHCP Server", 128 szLogFile, 129 RTLOGFLAGS_PREFIX_TIME_PROG, 130 "all all.restrict -default.restrict", 131 szEnvVarBase, 132 RTLOGDEST_FILE 154 133 #ifdef DEBUG 155 | RTLOGDEST_STDERR134 | RTLOGDEST_STDERR 156 135 #endif 157 ,158 32768 /* cMaxEntriesPerGroup */,159 0 /* cHistory */,160 0 /* uHistoryFileTime */,161 0 /* uHistoryFileSize */,162 NULL /* pErrInfo */);136 , 137 32768 /* cMaxEntriesPerGroup */, 138 0 /* cHistory */, 139 0 /* uHistoryFileTime */, 140 0 /* uHistoryFileSize */, 141 NULL /* pErrInfo */); 163 142 164 143 return rc; … … 170 149 int rc; 171 150 172 if (m_strNetwork. empty())151 if (m_strNetwork.isEmpty()) 173 152 { 174 153 LogDHCP(("network name is not specified\n")); … … 212 191 213 192 /* valid netmask */ 214 int iPrefixLengh;215 rc = RTNetMaskToPrefixIPv4(&m_IPv4Netmask, & iPrefixLengh);216 if (RT_FAILURE(rc) || iPrefixLengh== 0)193 int cPrefixBits; 194 rc = RTNetMaskToPrefixIPv4(&m_IPv4Netmask, &cPrefixBits); 195 if (RT_FAILURE(rc) || cPrefixBits == 0) 217 196 { 218 197 LogDHCP(("IP mask is not valid: %RTnaipv4\n", m_IPv4Netmask.u)); … … 224 203 { 225 204 LogDHCP(("first pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n", 226 (m_IPv4Address.u & m_IPv4Netmask.u), iPrefixLengh, 227 m_IPv4PoolFirst.u)); 205 (m_IPv4Address.u & m_IPv4Netmask.u), cPrefixBits, m_IPv4PoolFirst.u)); 228 206 return VERR_GENERAL_FAILURE; 229 207 } … … 233 211 { 234 212 LogDHCP(("last pool address is outside the network %RTnaipv4/%d: %RTnaipv4\n", 235 (m_IPv4Address.u & m_IPv4Netmask.u), iPrefixLengh, 236 m_IPv4PoolLast.u)); 213 (m_IPv4Address.u & m_IPv4Netmask.u), cPrefixBits, m_IPv4PoolLast.u)); 237 214 return VERR_GENERAL_FAILURE; 238 215 } … … 257 234 if (!fMACGenerated) 258 235 LogDHCP(("MAC address %RTmac\n", &m_MacAddress)); 259 LogDHCP(("IP address %RTnaipv4/%d\n", m_IPv4Address.u, iPrefixLengh));236 LogDHCP(("IP address %RTnaipv4/%d\n", m_IPv4Address.u, cPrefixBits)); 260 237 LogDHCP(("address pool %RTnaipv4 - %RTnaipv4\n", m_IPv4PoolFirst.u, m_IPv4PoolLast.u)); 261 238 … … 387 364 388 365 case 'n': /* --network */ 389 if (!config->m_strNetwork. empty())366 if (!config->m_strNetwork.isEmpty()) 390 367 { 391 368 RTMsgError("Duplicate --network option"); … … 396 373 397 374 case 't': /* --trunk-name */ 398 if (!config->m_strTrunk. empty())375 if (!config->m_strTrunk.isEmpty()) 399 376 { 400 377 RTMsgError("Duplicate --trunk-name option"); … … 641 618 throw ConfigFileError("DHCPServer/@networkName missing"); 642 619 643 setNetwork(strNetworkName .c_str());620 setNetwork(strNetworkName); 644 621 645 622 RTCString strTrunkType; … … 663 640 if (!server->getAttributeValue("trunkName", &strTrunk)) 664 641 throw ConfigFileError("DHCPServer/@trunkName missing"); 665 m_strTrunk = strTrunk .c_str();642 m_strTrunk = strTrunk; 666 643 } 667 644 else … … 848 825 void Config::sanitizeBaseName() 849 826 { 850 int rc; 851 852 if (m_strNetwork.empty()) 853 return; 854 855 char szBaseName[RTPATH_MAX]; 856 rc = RTStrCopy(szBaseName, sizeof(szBaseName), m_strNetwork.c_str()); 857 if (RT_FAILURE(rc)) 858 return; 859 860 char ch; 827 if (m_strNetwork.isNotEmpty()) 828 { 829 m_strBaseName = m_strNetwork; 830 831 char ch; 861 832 #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) 862 static const char s_szIllegals[] = "/\\\"*:<>?|\t\v\n\r\f\a\b"; /** @todo all control chars... */863 for (char *p = szBaseName; (ch = *p) != '\0'; ++p)864 if (strchr(s_szIllegals, ch))865 *p= '_';833 static const char s_szIllegals[] = "/\\\"*:<>?|\t\v\n\r\f\a\b"; /** @todo all control chars... */ 834 for (char *psz = m_strBaseName.mutableRaw(); (ch = *psz) != '\0'; ++psz) 835 if (strchr(s_szIllegals, ch)) 836 *psz = '_'; 866 837 #else 867 for (char *p = szBaseName; (ch = *p) != '\0'; ++p)868 if (RTPATH_IS_SEP(ch))869 *p= '_';838 for (char *psz = m_strBaseName.mutableRaw(); (ch = *psz) != '\0'; ++psz) 839 if (RTPATH_IS_SEP(ch)) 840 *psz = '_'; 870 841 #endif 871 872 m_strBaseName.assign(szBaseName); 842 m_strBaseName.jolt(); /* Not really necessary, but it's protocol. */ 843 } 844 else 845 m_strBaseName.setNull(); 873 846 } 874 847 -
trunk/src/VBox/NetworkServices/Dhcpd/Config.h
r76576 r79514 25 25 #include <iprt/net.h> 26 26 #include <iprt/cpp/xml.h> 27 #include <iprt/cpp/ministring.h> 27 28 28 29 #include <VBox/intnet.h> 29 30 30 #include <string>31 31 32 32 #include "Defs.h" … … 37 37 class Config 38 38 { 39 /* XXX: TODO: also store fixed address assignments, etc? */39 /** XXX: TODO: also store fixed address assignments, etc? */ 40 40 typedef std::map<RTMAC, optmap_t> vmmap_t; 41 41 42 std::string m_strHome; /* path of ~/.VirtualBox or equivalent */42 RTCString m_strHome; /* path of ~/.VirtualBox or equivalent */ 43 43 44 std::string m_strNetwork;45 std::string m_strBaseName; /* m_strNetwork sanitized to be usable in a path component */44 RTCString m_strNetwork; 45 RTCString m_strBaseName; /* m_strNetwork sanitized to be usable in a path component */ 46 46 47 std::string m_strTrunk;47 RTCString m_strTrunk; 48 48 INTNETTRUNKTYPE m_enmTrunkType; 49 49 … … 73 73 74 74 public: /* accessors */ 75 const std::string &getHome() const { return m_strHome; }75 const RTCString &getHome() const { return m_strHome; } 76 76 77 const std::string &getNetwork() const { return m_strNetwork; }78 void setNetwork(const std::string &aStrNetwork);77 const RTCString &getNetwork() const { return m_strNetwork; } 78 void setNetwork(const RTCString &aStrNetwork); 79 79 80 const std::string &getBaseName() const { return m_strBaseName; }81 const std::string &getTrunk() const { return m_strTrunk; }80 const RTCString &getBaseName() const { return m_strBaseName; } 81 const RTCString &getTrunk() const { return m_strTrunk; } 82 82 INTNETTRUNKTYPE getTrunkType() const { return m_enmTrunkType; } 83 83 -
trunk/src/VBox/NetworkServices/Dhcpd/DHCPD.h
r76576 r79514 22 22 #endif 23 23 24 #include <iprt/cpp/ministring.h> 24 25 #include "Defs.h" 25 26 #include "Config.h" … … 31 32 { 32 33 const Config *m_pConfig; 33 std::string m_strLeasesFileName;34 RTCString m_strLeasesFileName; 34 35 Db m_db; 35 36 -
trunk/src/VBox/NetworkServices/Dhcpd/Db.cpp
r79509 r79514 223 223 Binding *Binding::fromXML(const xml::ElementNode *ndLease) 224 224 { 225 int rc;226 227 225 /* Lease/@network seems to always have bogus value, ignore it. */ 228 226 … … 236 234 237 235 RTMAC mac; 238 rc = RTNetStrToMacAddr(strMac.c_str(), &mac);236 int rc = RTNetStrToMacAddr(strMac.c_str(), &mac); 239 237 if (RT_FAILURE(rc)) 240 238 return NULL; … … 640 638 641 639 642 int Db::writeLeases(const std::string &strFileName) const640 int Db::writeLeases(const RTCString &strFileName) const 643 641 { 644 642 LogDHCP(("writing leases to %s\n", strFileName.c_str())); … … 684 682 685 683 686 int Db::loadLeases(const std::string &strFileName)684 int Db::loadLeases(const RTCString &strFileName) 687 685 { 688 686 LogDHCP(("loading leases from %s\n", strFileName.c_str())); -
trunk/src/VBox/NetworkServices/Dhcpd/Db.h
r76576 r79514 24 24 #include <iprt/net.h> 25 25 26 #include <iprt/cpp/ministring.h> 26 27 #include <iprt/cpp/xml.h> 27 28 … … 145 146 146 147 public: 147 int loadLeases(const std::string &strFileName);148 int loadLeases(const RTCString &strFileName); 148 149 void loadLease(const xml::ElementNode *ndLease); 149 150 150 int writeLeases(const std::string &strFileName) const;151 int writeLeases(const RTCString &strFileName) const; 151 152 152 153 private: -
trunk/src/VBox/NetworkServices/Dhcpd/DhcpMessage.cpp
r76553 r79514 102 102 } 103 103 104 int overload; 105 overload = msg->parseOptions(&bp->bp_vend.Dhcp.dhcp_opts, 106 buflen - RT_OFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts)); 107 if (overload < 0) 104 int fOptOverload = msg->parseOptions(&bp->bp_vend.Dhcp.dhcp_opts, buflen - RT_OFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_opts)); 105 if (fOptOverload < 0) 108 106 return NULL; 109 107 110 108 /* "The 'file' field MUST be interpreted next ..." */ 111 if (overload & DHCP_OPTION_OVERLOAD_FILE) { 109 if (fOptOverload & RTNET_DHCP_OPTION_OVERLOAD_FILE) 110 { 112 111 int status = msg->parseOptions(bp->bp_file, sizeof(bp->bp_file)); 113 112 if (status != 0) … … 124 123 125 124 /* "... followed by the 'sname' field." */ 126 if (overload & DHCP_OPTION_OVERLOAD_SNAME) { 125 if (fOptOverload & RTNET_DHCP_OPTION_OVERLOAD_SNAME) 126 { 127 127 int status = msg->parseOptions(bp->bp_sname, sizeof(bp->bp_sname)); 128 128 if (status != 0) /* NB: this includes "nested" Option Overload */ … … 150 150 int DhcpClientMessage::parseOptions(const void *buf, size_t buflen) 151 151 { 152 uint8_t opt, optlen; 153 const uint8_t *data; 154 int overload; 155 156 overload = 0; 157 158 data = static_cast<const uint8_t *>(buf); 159 while (buflen > 0) { 160 opt = *data++; 152 int fOptOverload = 0; 153 const uint8_t *data = static_cast<const uint8_t *>(buf); 154 while (buflen > 0) 155 { 156 uint8_t const opt = *data++; 161 157 --buflen; 162 158 163 if (opt == RTNET_DHCP_OPT_PAD) {159 if (opt == RTNET_DHCP_OPT_PAD) 164 160 continue; 165 } 166 167 if (opt == RTNET_DHCP_OPT_END) { 168 break; 169 } 170 171 if (buflen == 0) { 161 162 if (opt == RTNET_DHCP_OPT_END) 163 break; 164 165 if (buflen == 0) 166 { 172 167 RTPrintf("option %d has no length field\n", opt); 173 168 return -1; 174 169 } 175 170 176 optlen = *data++;171 uint8_t optlen = *data++; 177 172 --buflen; 178 173 179 if (optlen > buflen) {180 RTPrintf("option %d truncated (length %d, but only %lu bytes left)\n",181 174 if (optlen > buflen) 175 { 176 RTPrintf("option %d truncated (length %d, but only %lu bytes left)\n", opt, optlen, (unsigned long)buflen); 182 177 return -1; 183 178 } … … 188 183 return -1; 189 184 #endif 190 if (opt == RTNET_DHCP_OPT_OPTION_OVERLOAD) { 191 if (optlen != 1) { 192 RTPrintf("Overload Option (option %d) has invalid length %d\n", 193 opt, optlen); 185 if (opt == RTNET_DHCP_OPT_OPTION_OVERLOAD) 186 { 187 if (optlen != 1) 188 { 189 RTPrintf("Overload Option (option %d) has invalid length %d\n", opt, optlen); 194 190 return -1; 195 191 } 196 192 197 overload = *data;198 199 if (( overload & ~DHCP_OPTION_OVERLOAD_MASK) != 0) {200 RTPrintf("Overload Option (option %d) has invalid value 0x%x\n",201 opt, overload);193 fOptOverload = *data; 194 195 if ((fOptOverload & ~RTNET_DHCP_OPTION_OVERLOAD_MASK) != 0) 196 { 197 RTPrintf("Overload Option (option %d) has invalid value 0x%x\n", opt, fOptOverload); 202 198 return -1; 203 199 } 204 200 } 205 201 else 206 {207 202 m_rawopts.insert(std::make_pair(opt, octets_t(data, data + optlen))); 208 } 209 210 data += optlen; 203 204 data += optlen; 211 205 buflen -= optlen; 212 206 } 213 207 214 return overload;208 return fOptOverload; 215 209 } 216 210 -
trunk/src/VBox/NetworkServices/Dhcpd/DhcpMessage.h
r76576 r79514 24 24 #include "Defs.h" 25 25 #include <iprt/net.h> 26 #include < string>26 #include <iprt/cpp/ministring.h> 27 27 #include "ClientId.h" 28 28 #include "DhcpOptions.h" 29 29 30 31 /* move to <iptr/net.h>? */32 #define DHCP_OPTION_OVERLOAD_MASK 0x333 #define DHCP_OPTION_OVERLOAD_FILE 0x134 #define DHCP_OPTION_OVERLOAD_SNAME 0x235 30 36 31 … … 48 43 RTNETADDRIPV4 m_giaddr; 49 44 50 std::string m_sname;51 std::string m_file;45 RTCString m_sname; 46 RTCString m_file; 52 47 53 48 OptMessageType m_optMessageType; -
trunk/src/VBox/NetworkServices/Dhcpd/DhcpOptions.h
r76576 r79514 24 24 #include "Defs.h" 25 25 26 #include <string.h> 27 26 #include <iprt/asm.h> 28 27 #include <iprt/err.h> 29 #include <iprt/types.h>30 #include <iprt/asm.h>31 #include <iprt/stdint.h>32 28 #include <iprt/net.h> 33 34 #include <string> 29 #include <iprt/string.h> 30 #include <iprt/cpp/ministring.h> 31 35 32 36 33 class DhcpClientMessage; … … 105 102 } 106 103 107 static void append(octets_t &aDst, const std::string &str)108 { 109 append(aDst, str.c_str(), str. size());104 static void append(octets_t &aDst, const RTCString &str) 105 { 106 append(aDst, str.c_str(), str.length()); 110 107 } 111 108 … … 148 145 } 149 146 150 static void extract(std::string &aString, octets_t::const_iterator &pos, size_t cb) 147 #if 0 /** @todo fix me */ 148 static void extract(RTCString &aString, octets_t::const_iterator &pos, size_t cb) 151 149 { 152 150 aString.replace(aString.begin(), aString.end(), &pos[0], &pos[cb]); 153 151 pos += cb; 154 152 } 153 #endif 155 154 156 155 … … 362 361 { 363 362 public: 364 typedef std::string value_t;365 366 protected: 367 std::string m_String;363 typedef RTCString value_t; 364 365 protected: 366 RTCString m_String; 368 367 369 368 explicit OptStringBase(uint8_t aOptCode) 370 369 : DhcpOption(aOptCode, false), m_String() {} 371 370 372 OptStringBase(uint8_t aOptCode, const std::string &aOptString)371 OptStringBase(uint8_t aOptCode, const RTCString &aOptString) 373 372 : DhcpOption(aOptCode), m_String(aOptString) {} 374 373 … … 386 385 387 386 public: 388 std::string &value() { return m_String; }389 const std::string &value() const { return m_String; }387 RTCString &value() { return m_String; } 388 const RTCString &value() const { return m_String; } 390 389 391 390 protected: 392 391 virtual ssize_t encodeValue(octets_t &dst) const 393 392 { 394 if (!isLengthValid(m_String. size()))393 if (!isLengthValid(m_String.length())) 395 394 return -1; 396 395 397 396 append(dst, m_String); 398 return m_String. size();397 return m_String.length(); 399 398 } 400 399 … … 410 409 return VERR_INVALID_PARAMETER; 411 410 412 octets_t::const_iterator pos(src.begin()); 413 extract(m_String, pos, cb); 411 int rc = m_String.assignNoThrow((char *)&src.front(), cb); /** @todo encoding. */ 414 412 m_fPresent = true; 415 return VINF_SUCCESS;413 return rc; 416 414 } 417 415 }; … … 427 425 : OptStringBase(optcode) {} 428 426 429 explicit OptString(const std::string &aOptString)427 explicit OptString(const RTCString &aOptString) 430 428 : OptStringBase(optcode, aOptString) {} 431 429 -
trunk/src/VBox/NetworkServices/Dhcpd/VBoxNetDhcpd.cpp
r79509 r79514 119 119 int vmmInit(); 120 120 121 int ifInit(const std::string &strNetwork,122 const std::string &strTrunk = std::string(),121 int ifInit(const RTCString &strNetwork, 122 const RTCString &strTrunk = RTCString(), 123 123 INTNETTRUNKTYPE enmTrunkType = kIntNetTrunkType_WhateverNone); 124 int ifOpen(const std::string &strNetwork,125 const std::string &strTrunk,124 int ifOpen(const RTCString &strNetwork, 125 const RTCString &strTrunk, 126 126 INTNETTRUNKTYPE enmTrunkType); 127 127 int ifGetBuf(); … … 254 254 255 255 256 int VBoxNetDhcpd::ifInit(const std::string &strNetwork,257 const std::string &strTrunk,256 int VBoxNetDhcpd::ifInit(const RTCString &strNetwork, 257 const RTCString &strTrunk, 258 258 INTNETTRUNKTYPE enmTrunkType) 259 259 { … … 276 276 277 277 278 int VBoxNetDhcpd::ifOpen(const std::string &strNetwork,279 const std::string &strTrunk,278 int VBoxNetDhcpd::ifOpen(const RTCString &strNetwork, 279 const RTCString &strTrunk, 280 280 INTNETTRUNKTYPE enmTrunkType) 281 281 { … … 284 284 285 285 INTNETOPENREQ OpenReq; 286 int rc;286 RT_ZERO(OpenReq); 287 287 288 288 OpenReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC; … … 290 290 OpenReq.pSession = m_pSession; 291 291 292 strncpy(OpenReq.szNetwork, strNetwork.c_str(), sizeof(OpenReq.szNetwork));293 OpenReq.szNetwork[sizeof(OpenReq.szNetwork) - 1] = '\0';294 295 strncpy(OpenReq.szTrunk, strTrunk.c_str(), sizeof(OpenReq.szTrunk));296 OpenReq.szTrunk[sizeof(OpenReq.szTrunk) - 1] = '\0';292 int rc = RTStrCopy(OpenReq.szNetwork, sizeof(OpenReq.szNetwork), strNetwork.c_str()); 293 AssertRCReturn(rc, rc); 294 295 rc = RTStrCopy(OpenReq.szTrunk, sizeof(OpenReq.szTrunk), strTrunk.c_str()); 296 AssertRCReturn(rc, rc); 297 297 298 298 if (enmTrunkType != kIntNetTrunkType_Invalid)
Note:
See TracChangeset
for help on using the changeset viewer.