VirtualBox

Changeset 93472 in vbox for trunk


Ignore:
Timestamp:
Jan 28, 2022 12:01:55 PM (3 years ago)
Author:
vboxsync
Message:

VBoxNetAdpCtl: (bugref:8093) Replaced adhoc IP address conversions with calls to IPRT functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/adpctl/VBoxNetAdpCtl.cpp

    r93115 r93472  
    277277/*
    278278 * 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.
    279283 */
    280284class AddressIPv4
     
    283287    AddressIPv4(const char *pcszAddress, const char *pcszNetmask = 0)
    284288        {
     289            m_Prefix = 0;
     290            memset(&m_Address, 0, sizeof(m_Address));
     291
    285292            if (pcszNetmask)
    286293                m_Prefix = maskToPrefix(pcszNetmask);
     
    294301                m_Prefix = 24;
    295302            }
    296             inet_pton(AF_INET, pcszAddress, &(m_Address.sin_addr));
     303            int rc = RTNetStrToIPv4Addr(pcszAddress, &m_Address);
     304            AssertRCReturnVoid(rc);
    297305            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);
    300307        }
    301308    const char *getBroadcast() const { return m_szBroadcast; };
    302309    const char *getAddressAndMask() const { return m_szAddressAndMask; };
    303310private:
    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;
    310316    char m_szAddressAndMask[INET_ADDRSTRLEN + 3]; /* e.g. 192.168.56.101/24 */
    311317    char m_szBroadcast[INET_ADDRSTRLEN];
    312318};
    313319
    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)
     320int 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
     333void AddressIPv4::deriveBroadcast(PCRTNETADDRIPV4 pcAddress, int iPrefix)
    335334{
    336335    /* 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));
    339341}
    340342
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette