- Timestamp:
- Jan 28, 2022 12:01:55 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/adpctl/VBoxNetAdpCtl.cpp
r93115 r93472 277 277 /* 278 278 * Helper class to incapsulate IPv4 address conversion. 279 * 280 * Note that this class relies on NetworkAddress to have been used for 281 * checking validity of IP addresses prior calling any methods of this 282 * class. 279 283 */ 280 284 class AddressIPv4 … … 283 287 AddressIPv4(const char *pcszAddress, const char *pcszNetmask = 0) 284 288 { 289 m_Prefix = 0; 290 memset(&m_Address, 0, sizeof(m_Address)); 291 285 292 if (pcszNetmask) 286 293 m_Prefix = maskToPrefix(pcszNetmask); … … 294 301 m_Prefix = 24; 295 302 } 296 inet_pton(AF_INET, pcszAddress, &(m_Address.sin_addr)); 303 int rc = RTNetStrToIPv4Addr(pcszAddress, &m_Address); 304 AssertRCReturnVoid(rc); 297 305 snprintf(m_szAddressAndMask, sizeof(m_szAddressAndMask), "%s/%d", pcszAddress, m_Prefix); 298 m_Broadcast.sin_addr.s_addr = computeBroadcast(m_Address.sin_addr.s_addr, m_Prefix); 299 inet_ntop(AF_INET, &(m_Broadcast.sin_addr), m_szBroadcast, sizeof(m_szBroadcast)); 306 deriveBroadcast(&m_Address, m_Prefix); 300 307 } 301 308 const char *getBroadcast() const { return m_szBroadcast; }; 302 309 const char *getAddressAndMask() const { return m_szAddressAndMask; }; 303 310 private: 304 unsigned int maskToPrefix(const char *pcszNetmask); 305 unsigned long computeBroadcast(unsigned long ulAddress, unsigned int uPrefix); 306 307 unsigned int m_Prefix; 308 struct sockaddr_in m_Address; 309 struct sockaddr_in m_Broadcast; 311 int maskToPrefix(const char *pcszNetmask); 312 void deriveBroadcast(PCRTNETADDRIPV4 pcAddress, int uPrefix); 313 314 int m_Prefix; 315 RTNETADDRIPV4 m_Address; 310 316 char m_szAddressAndMask[INET_ADDRSTRLEN + 3]; /* e.g. 192.168.56.101/24 */ 311 317 char m_szBroadcast[INET_ADDRSTRLEN]; 312 318 }; 313 319 314 unsigned int AddressIPv4::maskToPrefix(const char *pcszNetmask) 315 { 316 unsigned cBits = 0; 317 unsigned m[4]; 318 319 if (sscanf(pcszNetmask, "%u.%u.%u.%u", &m[0], &m[1], &m[2], &m[3]) == 4) 320 { 321 for (int i = 0; i < 4 && m[i]; ++i) 322 { 323 int mask = m[i]; 324 while (mask & 0x80) 325 { 326 cBits++; 327 mask <<= 1; 328 } 329 } 330 } 331 return cBits; 332 } 333 334 unsigned long AddressIPv4::computeBroadcast(unsigned long ulAddress, unsigned int uPrefix) 320 int AddressIPv4::maskToPrefix(const char *pcszNetmask) 321 { 322 RTNETADDRIPV4 mask; 323 int prefix = 0; 324 325 int rc = RTNetStrToIPv4Addr(pcszNetmask, &mask); 326 AssertRCReturn(rc, 0); 327 rc = RTNetMaskToPrefixIPv4(&mask, &prefix); 328 AssertRCReturn(rc, 0); 329 330 return prefix; 331 } 332 333 void AddressIPv4::deriveBroadcast(PCRTNETADDRIPV4 pcAddress, int iPrefix) 335 334 { 336 335 /* Note: the address is big-endian. */ 337 unsigned long ulNetworkMask = (1l << uPrefix) - 1; 338 return (ulAddress & ulNetworkMask) | ~ulNetworkMask; 336 RTNETADDRIPV4 mask, broadcast; 337 int rc = RTNetPrefixToMaskIPv4(iPrefix, &mask); 338 AssertRCReturnVoid(rc); 339 broadcast.au32[0] = (pcAddress->au32[0] & mask.au32[0]) | ~mask.au32[0]; 340 inet_ntop(AF_INET, broadcast.au32, m_szBroadcast, sizeof(m_szBroadcast)); 339 341 } 340 342
Note:
See TracChangeset
for help on using the changeset viewer.