VirtualBox

Changeset 65569 in vbox for trunk/src/VBox/Main


Ignore:
Timestamp:
Feb 1, 2017 2:44:34 PM (8 years ago)
Author:
vboxsync
Message:

Main/DHCPServerImpl: verify values passed to setConfiguration.

File:
1 edited

Legend:

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

    r61009 r65569  
    2424#include "Logging.h"
    2525
     26#include <iprt/net.h>
    2627#include <iprt/cpp/utils.h>
    2728
     
    255256                                     const com::Utf8Str &aUpperIP)
    256257{
    257     AssertReturn(!aIPAddress.isEmpty(), E_INVALIDARG);
    258     AssertReturn(!aNetworkMask.isEmpty(), E_INVALIDARG);
    259     AssertReturn(!aLowerIP.isEmpty(), E_INVALIDARG);
    260     AssertReturn(!aUpperIP.isEmpty(), E_INVALIDARG);
     258    RTNETADDRIPV4 IPAddress, NetworkMask, LowerIP, UpperIP;
     259    int rc;
     260
     261    rc = RTNetStrToIPv4Addr(aIPAddress.c_str(), &IPAddress);
     262    if (RT_FAILURE(rc))
     263        return E_INVALIDARG;
     264
     265    rc = RTNetStrToIPv4Addr(aNetworkMask.c_str(), &NetworkMask);
     266    if (RT_FAILURE(rc))
     267        return E_INVALIDARG;
     268
     269    rc = RTNetStrToIPv4Addr(aLowerIP.c_str(), &LowerIP);
     270    if (RT_FAILURE(rc))
     271        return E_INVALIDARG;
     272
     273    rc = RTNetStrToIPv4Addr(aUpperIP.c_str(), &UpperIP);
     274    if (RT_FAILURE(rc))
     275        return E_INVALIDARG;
     276
     277    /* It's more convenient to convert to host order once */
     278    IPAddress.u = RT_N2H_U32(IPAddress.u);
     279    NetworkMask.u = RT_N2H_U32(NetworkMask.u);
     280    LowerIP.u = RT_N2H_U32(LowerIP.u);
     281    UpperIP.u = RT_N2H_U32(UpperIP.u);
     282
     283    /* Addresses must be unicast */
     284    if (   (IPAddress.u & 0xe0000000) == 0xe0000000
     285        || (LowerIP.u   & 0xe0000000) == 0xe0000000
     286        || (UpperIP.u   & 0xe0000000) == 0xe0000000)
     287    {
     288        return E_INVALIDARG;
     289    }
     290
     291    /*
     292     * Insist on continuous mask.  May be also accept prefix length
     293     * here or address/prefix for aIPAddress?
     294     */
     295    if (NetworkMask.u != 0) {
     296        /* TODO: factor out mask<->length to <iptr/cidr.h>? */
     297        uint32_t prefixMask = 0xffffffff;
     298        int prefixLen = 32;
     299
     300        while (prefixLen > 0) {
     301            if (NetworkMask.u == prefixMask)
     302                break;
     303            --prefixLen;
     304            prefixMask <<= 1;
     305        }
     306
     307        if (prefixLen == 0)
     308            return E_INVALIDARG;
     309    }
     310
     311    /* Addresses should be from the same network */
     312    if (   (IPAddress.u & NetworkMask.u) != (LowerIP.u &NetworkMask.u)
     313        || (LowerIP.u & NetworkMask.u) != (UpperIP.u &NetworkMask.u))
     314    {
     315        return E_INVALIDARG;
     316    }
     317
     318    /* The range should be valid ... */
     319    if (LowerIP.u > UpperIP.u)
     320        return E_INVALIDARG;
     321
     322    /* ... and shouldn't contain the server's address */
     323    if (LowerIP.u <= IPAddress.u && IPAddress.u <= UpperIP.u)
     324        return E_INVALIDARG;
     325
    261326
    262327    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
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