VirtualBox

Changeset 87360 in vbox


Ignore:
Timestamp:
Jan 21, 2021 8:19:42 PM (4 years ago)
Author:
vboxsync
Message:

fMain/NATNetwork: NATNetwork::setNetwork - validate and normalize the
argument. bugref:9909.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-server/NATNetworkImpl.cpp

    r87323 r87360  
    261261HRESULT NATNetwork::setNetwork(const com::Utf8Str &aIPv4NetworkCidr)
    262262{
    263     {
    264 
     263    RTNETADDRIPV4 Addr, Mask;
     264    int iPrefix;
     265    int rc;
     266
     267    rc = RTNetStrToIPv4Cidr(aIPv4NetworkCidr.c_str(), &Addr, &iPrefix);
     268    if (RT_FAILURE(rc))
     269        return setError(E_FAIL, "%s is not a valid IPv4 CIDR notation",
     270                        aIPv4NetworkCidr.c_str());
     271
     272    /*
     273     * /32 is a single address, not a network, /31 is the degenerate
     274     * point-to-point case, so reject these.  Larger values and
     275     * non-positive values are already treated as errors by the
     276     * conversion.
     277     */
     278    if (iPrefix > 30)
     279        return setError(E_FAIL, "%s network is too small", aIPv4NetworkCidr.c_str());
     280
     281    rc = RTNetPrefixToMaskIPv4(iPrefix, &Mask);
     282    AssertRCReturn(rc, setError(E_FAIL,
     283        "%s: internal error: failed to convert prefix %d to netmask: %Rrc",
     284        aIPv4NetworkCidr.c_str(), iPrefix, rc));
     285
     286    if ((Addr.u & ~Mask.u) != 0)
     287        return setError(E_FAIL,
     288            "%s: the specified address is longer than the specified prefix",
     289            aIPv4NetworkCidr.c_str());
     290
     291    /* normalized CIDR notation */
     292    com::Utf8StrFmt strCidr("%RTnaipv4/%d", Addr.u, iPrefix);
     293
     294    {
    265295        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
    266296
    267         if (aIPv4NetworkCidr == m->s.strIPv4NetworkCidr)
     297        if (m->s.strIPv4NetworkCidr == strCidr)
    268298            return S_OK;
    269299
     
    278308
    279309
    280         m->s.strIPv4NetworkCidr = aIPv4NetworkCidr;
     310        m->s.strIPv4NetworkCidr = strCidr;
    281311        i_recalculateIpv4AddressAssignments();
    282312    }
    283313
    284314    AutoWriteLock vboxLock(m->pVirtualBox COMMA_LOCKVAL_SRC_POS);
    285     HRESULT rc = m->pVirtualBox->i_saveSettings();
    286     ComAssertComRCRetRC(rc);
     315    HRESULT hrc = m->pVirtualBox->i_saveSettings();
     316    ComAssertComRCRetRC(hrc);
    287317    return S_OK;
    288318}
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