VirtualBox

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


Ignore:
Timestamp:
Mar 24, 2009 5:01:32 PM (16 years ago)
Author:
vboxsync
Message:

#3569: DHCP Server is now started from VBoxSVC and terminates with it.

Location:
trunk/src/VBox/Main
Files:
7 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/ConsoleImpl2.cpp

    r18068 r18208  
    8484# include <netif.h>
    8585#endif
     86
     87#include "DHCPServerRunner.h"
    8688
    8789#include <VBox/param.h>
     
    13071309        NetworkAttachmentType_T networkAttachment;
    13081310        hrc = networkAdapter->COMGETTER(AttachmentType)(&networkAttachment);        H();
    1309         Bstr networkName;
     1311        Bstr networkName, trunkName, trunkType;
    13101312        switch (networkAttachment)
    13111313        {
     
    13531355                    {
    13541356                        rc = CFGMR3InsertString(pCfg, "Network", psz);              RC_CHECK();
    1355                         networkName = Bstr(psz);
     1357                        /* NAT uses its own DHCP implementation */
     1358                        //networkName = Bstr(psz);
    13561359                    }
    13571360
     
    14981501                    rc = CFGMR3InsertString(pCfg, "Network", szNetwork);            RC_CHECK();
    14991502                    networkName = Bstr(szNetwork);
     1503                    trunkName = Bstr(pszTrunk);
     1504                    trunkType = Bstr(TRUNKTYPE_NETFLT);
    15001505
    15011506# if defined(RT_OS_DARWIN)
     
    17151720                        rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg);             RC_CHECK();
    17161721                        rc = CFGMR3InsertString(pCfg, "Network", psz);              RC_CHECK();
     1722                        rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); RC_CHECK();
    17171723                        networkName = Bstr(psz);
     1724                        trunkType = Bstr(TRUNKTYPE_WHATEVER);
    17181725                    }
    17191726                    STR_FREE();
     
    17911798                rc = CFGMR3InsertString(pCfg, "Network", szNetwork);            RC_CHECK();
    17921799                networkName = Bstr(szNetwork);
     1800                trunkName   = Bstr(pszTrunk);
     1801                trunkType   = TRUNKTYPE_NETADP;
    17931802#elif defined(RT_OS_DARWIN)
    17941803                rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0");             RC_CHECK();
     
    17961805                rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK();
    17971806                networkName = Bstr("HostInterfaceNetworking-vboxnet0");
     1807                trunkName   = Bstr("vboxnet0");
     1808                trunkType   = TRUNKTYPE_NETADP;
    17981809#else
    17991810                rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0");             RC_CHECK();
     
    18011812                rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); RC_CHECK();
    18021813                networkName = Bstr("HostInterfaceNetworking-vboxnet0");
     1814                trunkName   = Bstr("vboxnet0");
     1815                trunkType   = TRUNKTYPE_NETFLT;
    18031816#endif
    18041817#if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
     
    18511864        if(!networkName.isNull())
    18521865        {
     1866            /*
     1867             * Until we implement service reference counters DHCP Server will be stopped
     1868             * by DHCPServerRunner destructor.
     1869             */
    18531870            ComPtr<IDHCPServer> dhcpServer;
    18541871            hrc = virtualBox->FindDHCPServerByNetworkName(networkName.mutableRaw(), dhcpServer.asOutParam());
     
    18631880                    H();
    18641881                }
    1865 
     1882           
    18661883                if(bEnabled)
    1867                 {
    1868                     Bstr ip, mask, lowerIp, upperIp;
    1869 
    1870                     hrc = dhcpServer->COMGETTER(IPAddress)(ip.asOutParam());
    1871                     if(FAILED(hrc))
    1872                     {
    1873                         LogRel(("DHCP svr: COMGETTER(IPAddress) failed, hrc (0x%x)", hrc));
    1874                         H();
    1875                     }
    1876 
    1877                     hrc = dhcpServer->COMGETTER(NetworkMask)(mask.asOutParam());
    1878                     if(FAILED(hrc))
    1879                     {
    1880                         LogRel(("DHCP svr: COMGETTER(NetworkMask) failed, hrc (0x%x)", hrc));
    1881                         H();
    1882                     }
    1883 
    1884                     hrc = dhcpServer->COMGETTER(LowerIP)(lowerIp.asOutParam());
    1885                     if(FAILED(hrc))
    1886                     {
    1887                         LogRel(("DHCP svr: COMGETTER(LowerIP) failed, hrc (0x%x)", hrc));
    1888                         H();
    1889                     }
    1890 
    1891                     hrc = dhcpServer->COMGETTER(UpperIP)(upperIp.asOutParam());
    1892                     if(FAILED(hrc))
    1893                     {
    1894                         LogRel(("DHCP svr: COMGETTER(UpperIP) failed, hrc (0x%x)", hrc));
    1895                         H();
    1896                     }
    1897 
    1898                     char strMAC[13];
    1899                     Guid guid;
    1900                     guid.create();
    1901                     RTStrPrintf (strMAC, sizeof(strMAC), "080027%02X%02X%02X",
    1902                                  guid.ptr()->au8[0], guid.ptr()->au8[1], guid.ptr()->au8[2]);
    1903 
    1904                     rc = CFGMR3InsertString(pCfg, "DhcpIPAddress", Utf8Str(ip).raw());             RC_CHECK();
    1905                     rc = CFGMR3InsertString(pCfg, "DhcpNetworkMask", Utf8Str(mask).raw());             RC_CHECK();
    1906                     rc = CFGMR3InsertString(pCfg, "DhcpLowerIP", Utf8Str(lowerIp).raw());             RC_CHECK();
    1907                     rc = CFGMR3InsertString(pCfg, "DhcpUpperIP", Utf8Str(upperIp).raw());             RC_CHECK();
    1908                     rc = CFGMR3InsertString(pCfg, "DhcpMacAddress", strMAC);             RC_CHECK();
    1909                 }
     1884                    hrc = dhcpServer->Start(networkName, trunkName, trunkType);
    19101885            }
    19111886            else
  • trunk/src/VBox/Main/DHCPServerImpl.cpp

    r18023 r18208  
    2222 */
    2323
     24#include "DHCPServerRunner.h"
    2425#include "DHCPServerImpl.h"
    2526#include "Logging.h"
     
    240241    return mVirtualBox->saveSettings();
    241242}
     243
     244STDMETHODIMP DHCPServer::Start (IN_BSTR aNetworkName, IN_BSTR aTrunkName, IN_BSTR aTrunkType)
     245{
     246    /* Silently ignore attepmts to run disabled servers. */
     247    if (!m.enabled)
     248        return S_OK;
     249
     250    m.dhcp.setOption(DHCPCFG_NETNAME, Utf8Str(aNetworkName));
     251    Bstr tmp(aTrunkName);
     252    if (!tmp.isEmpty())
     253        m.dhcp.setOption(DHCPCFG_TRUNKNAME, Utf8Str(tmp));
     254    m.dhcp.setOption(DHCPCFG_TRUNKTYPE, Utf8Str(aTrunkType));
     255    //temporary hack for testing
     256    //    DHCPCFG_NAME
     257    char strMAC[13];
     258    Guid guid;
     259    guid.create();
     260    RTStrPrintf (strMAC, sizeof(strMAC), "080027%02X%02X%02X",
     261                 guid.ptr()->au8[0], guid.ptr()->au8[1], guid.ptr()->au8[2]);
     262    m.dhcp.setOption(DHCPCFG_MACADDRESS, strMAC);
     263    m.dhcp.setOption(DHCPCFG_IPADDRESS,  Utf8Str(m.IPAddress));
     264    //        DHCPCFG_LEASEDB,
     265    //        DHCPCFG_VERBOSE,
     266    //        DHCPCFG_GATEWAY,
     267    m.dhcp.setOption(DHCPCFG_LOWERIP,  Utf8Str(m.lowerIP));
     268    m.dhcp.setOption(DHCPCFG_UPPERIP,  Utf8Str(m.upperIP));
     269    m.dhcp.setOption(DHCPCFG_NETMASK,  Utf8Str(m.networkMask));
     270
     271    //        DHCPCFG_HELP,
     272    //        DHCPCFG_VERSION,
     273    //        DHCPCFG_NOTOPT_MAXVAL
     274    m.dhcp.setOption(DHCPCFG_BEGINCONFIG,  "");
     275
     276    return RT_FAILURE(m.dhcp.start()) ? E_FAIL : S_OK;
     277    //m.dhcp.detachFromServer(); /* need to do this to avoid server shutdown on runner destruction */
     278}
     279
     280STDMETHODIMP DHCPServer::Stop (void)
     281{
     282    return RT_FAILURE(m.dhcp.stop()) ? E_FAIL : S_OK;
     283}
  • trunk/src/VBox/Main/Makefile.kmk

    r18023 r18208  
    280280        HostNetworkInterfaceImpl.cpp \
    281281        DHCPServerImpl.cpp \
     282        DHCPServerRunner.cpp \
    282283        GuestOSTypeImpl.cpp \
    283284        NetworkAdapterImpl.cpp \
  • trunk/src/VBox/Main/VirtualBoxImpl.cpp

    r18177 r18208  
    6262#include "GuestOSTypeImpl.h"
    6363#include "Version.h"
     64#include "DHCPServerRunner.h"
    6465#include "DHCPServerImpl.h"
    6566
  • trunk/src/VBox/Main/idl/VirtualBox.xidl

    r18197 r18208  
    12321232  <interface
    12331233    name="IDHCPServer" extends="$unknown"
    1234     uuid="fab58256-c76e-4ddd-8029-18343e5b0069"
     1234    uuid="6cfe387c-74fb-4ca7-bff6-973bec8af7a3"
    12351235    wsmap="managed"
    12361236  >
     
    13051305        </desc>
    13061306      </param>
     1307    </method>
     1308
     1309    <method name="start">
     1310      <desc>
     1311        Starts DHCP server process.
     1312        <result name="E_FAIL">
     1313          Failed to start the process.
     1314        </result>
     1315      </desc>
     1316      <param name="networkName" type="wstring" dir="in">
     1317        <desc>
     1318          Name of internal network DHCP server should attach to.
     1319        </desc>
     1320      </param>
     1321      <param name="trunkName" type="wstring" dir="in">
     1322        <desc>
     1323          Name of internal network trunk.
     1324        </desc>
     1325      </param>
     1326      <param name="trunkType" type="wstring" dir="in">
     1327        <desc>
     1328          Type of internal network trunk.
     1329        </desc>
     1330      </param>
     1331    </method>
     1332
     1333    <method name="stop">
     1334      <desc>
     1335        Stops DHCP server process.
     1336        <result name="E_FAIL">
     1337          Failed to stop the process.
     1338        </result>
     1339      </desc>
    13071340    </method>
    13081341  </interface>
  • trunk/src/VBox/Main/include/DHCPServerImpl.h

    r18023 r18208  
    7777    STDMETHOD(SetConfiguration) (IN_BSTR aIPAddress, IN_BSTR aNetworkMask, IN_BSTR aFromIPAddress, IN_BSTR aToIPAddress);
    7878
     79    STDMETHOD(Start) (IN_BSTR aNetworkName, IN_BSTR aTrunkName, IN_BSTR aTrunkType);
     80    STDMETHOD(Stop) ();
     81
    7982    // for VirtualBoxSupportErrorInfoImpl
    8083    static const wchar_t *getComponentName() { return L"DHCPServer"; }
     
    9598        Bstr upperIP;
    9699        BOOL enabled;
     100
     101        DHCPServerRunner dhcp;
    97102    } m;
    98103
  • trunk/src/VBox/Main/include/DHCPServerRunner.h

    r18169 r18208  
    1 /* $Id: DHCPServerRunner.h 44622 2009-03-17 13:48:59Z misha $ */
     1/* $Id$ */
    22/** @file
    33 * VirtualBox Main - interface for VBox DHCP server
     
    2323#include <iprt/string.h>
    2424#include <iprt/mem.h>
    25 //#include <VBox/com/string.h>
     25#include <VBox/com/string.h>
    2626
    27 //using namespace com;
    2827typedef enum
    2928{
     
    5049#define TRUNKTYPE_NETADP   "netadp"
    5150#define TRUNKTYPE_SRVNAT   "srvnat"
    52 
    53 class Utf8Str
    54 {
    55 public:
    56 
    57     enum CaseSensitivity
    58     {
    59         CaseSensitive,
    60         CaseInsensitive
    61     };
    62 
    63     typedef char *String;
    64     typedef const char *ConstString;
    65 
    66     Utf8Str () : str (NULL) {}
    67 
    68     Utf8Str (const Utf8Str &that) : str (NULL) { raw_copy (str, that.str); }
    69     Utf8Str (const char *that) : str (NULL) { raw_copy (str, that); }
    70 
    71     /** Shortcut that calls #alloc(aSize) right after object creation. */
    72     Utf8Str (size_t aSize) : str (NULL) { alloc(aSize); }
    73 
    74     virtual ~Utf8Str () { setNull(); }
    75 
    76     Utf8Str &operator = (const Utf8Str &that) { safe_assign (that.str); return *this; }
    77     Utf8Str &operator = (const char *that) { safe_assign (that); return *this; }
    78 
    79     Utf8Str &setNull()
    80     {
    81         if (str)
    82         {
    83             ::RTStrFree (str);
    84             str = NULL;
    85         }
    86         return *this;
    87     }
    88 
    89     Utf8Str &setNullIfEmpty()
    90     {
    91         if (str && *str == 0)
    92         {
    93             ::RTStrFree (str);
    94             str = NULL;
    95         }
    96         return *this;
    97     }
    98 
    99     /**
    100      *  Allocates memory for a string capable to store \a aSize - 1 bytes (not characters!);
    101      *  in other words, aSize includes the terminating zero character. If \a aSize
    102      *  is zero, or if a memory allocation error occurs, this object will become null.
    103      */
    104     Utf8Str &alloc (size_t aSize)
    105     {
    106         setNull();
    107         if (aSize)
    108         {
    109 #if !defined (VBOX_WITH_XPCOM)
    110             str = (char *) ::RTMemTmpAlloc (aSize);
    111 #else
    112             str = (char *) nsMemory::Alloc (aSize);
    113 #endif
    114             if (str)
    115                 str [0] = 0;
    116         }
    117         return *this;
    118     }
    119 
    120     void append(const Utf8Str &that)
    121     {
    122         size_t cbThis = length();
    123         size_t cbThat = that.length();
    124 
    125         if (cbThat)
    126         {
    127             size_t cbBoth = cbThis + cbThat + 1;
    128 
    129             // @todo optimize with realloc() once the memory management is fixed
    130             char *pszTemp;
    131             pszTemp = (char*)::RTMemTmpAlloc(cbBoth);
    132             if (str)
    133             {
    134                 memcpy(pszTemp, str, cbThis);
    135                 setNull();
    136             }
    137             if (that.str)
    138                 memcpy(pszTemp + cbThis, that.str, cbThat);
    139             pszTemp[cbThis + cbThat] = '\0';
    140 
    141             str = pszTemp;
    142         }
    143     }
    144 
    145     int compare (const char *pcsz, CaseSensitivity cs = CaseSensitive) const
    146     {
    147         if (str == pcsz)
    148             return 0;
    149         if (str == NULL)
    150             return -1;
    151         if (pcsz == NULL)
    152             return 1;
    153 
    154         if (cs == CaseSensitive)
    155             return ::RTStrCmp(str, pcsz);
    156         else
    157             return ::RTStrICmp(str, pcsz);
    158     }
    159 
    160     int compare (const Utf8Str &that, CaseSensitivity cs = CaseSensitive) const
    161     {
    162         return compare (that.str, cs);
    163     }
    164 
    165     bool operator == (const Utf8Str &that) const { return !compare (that); }
    166     bool operator != (const Utf8Str &that) const { return !!compare (that); }
    167     bool operator == (const char *that) const { return !compare (that); }
    168     bool operator != (const char *that) const { return !!compare (that); }
    169     bool operator < (const Utf8Str &that) const { return compare (that) < 0; }
    170     bool operator < (const char *that) const { return compare (that) < 0; }
    171 
    172     bool endsWith (const Utf8Str &that, CaseSensitivity cs = CaseSensitive) const
    173     {
    174         if (isNull() || that.isNull())
    175             return false;
    176 
    177         if (length() < that.length())
    178             return false;
    179 
    180         int l = length() - that.length();
    181         if (cs == CaseSensitive)
    182             return ::RTStrCmp(&str[l], that.str) == 0;
    183         else
    184             return ::RTStrICmp(&str[l], that.str) == 0;
    185     }
    186 
    187     bool startsWith (const Utf8Str &that, CaseSensitivity cs = CaseSensitive) const
    188     {
    189         if (isNull() || that.isNull())
    190             return false;
    191 
    192         if (length() < that.length())
    193             return false;
    194 
    195         if (cs == CaseSensitive)
    196             return ::RTStrNCmp(str, that.str, that.length()) == 0;
    197         else
    198             return ::RTStrNICmp(str, that.str, that.length()) == 0;
    199     }
    200 
    201     bool isNull() const { return str == NULL; }
    202     operator bool() const { return !isNull(); }
    203 
    204     bool isEmpty() const { return isNull() || *str == 0; }
    205 
    206     size_t length() const { return isNull() ? 0 : ::strlen (str); }
    207 
    208     /** Intended to to pass instances as input (|char *|) parameters to methods. */
    209     operator const char *() const { return str; }
    210 
    211     /** The same as operator const char *(), but for situations where the compiler
    212         cannot typecast implicitly (for example, in printf() argument list). */
    213     const char *raw() const { return str; }
    214 
    215     /** The same as operator const char *(), but for situations where the compiler
    216         cannot typecast implicitly (for example, in printf() argument list). */
    217     const char *c_str() const { return str; }
    218 
    219     /**
    220      *  Returns a non-const raw pointer that allows to modify the string directly.
    221      *  @warning
    222      *      Be sure not to modify data beyond the allocated memory! The
    223      *      guaranteed size of the allocated memory is at least #length()
    224      *      bytes after creation and after every assignment operation.
    225      */
    226     char *mutableRaw() { return str; }
    227 
    228     /**
    229      *  Intended to assign instances to |char *| out parameters from within the
    230      *  interface method. Transfers the ownership of the duplicated string to the
    231      *  caller.
    232      */
    233     const Utf8Str &cloneTo (char **pstr) const
    234     {
    235         if (pstr)
    236         {
    237             *pstr = NULL;
    238             raw_copy (*pstr, str);
    239         }
    240         return *this;
    241     }
    242 
    243     /**
    244      *  Intended to assign instances to |char *| out parameters from within the
    245      *  interface method. Transfers the ownership of the original string to the
    246      *  caller and resets the instance to null.
    247      *
    248      *  As opposed to cloneTo(), this method doesn't create a copy of the
    249      *  string.
    250      */
    251     Utf8Str &detachTo (char **pstr)
    252     {
    253         *pstr = str;
    254         str = NULL;
    255         return *this;
    256     }
    257 
    258     static const size_t npos;
    259 
    260     /**
    261      *  Intended to pass instances as out (|char **|) parameters to methods.
    262      *  Takes the ownership of the returned data.
    263      */
    264     char **asOutParam() { setNull(); return &str; }
    265 
    266     /**
    267      *  Static immutable null object. May be used for comparison purposes.
    268      */
    269     static const Utf8Str Null;
    270 
    271 protected:
    272 
    273     void safe_assign (const char *s)
    274     {
    275         if (str != s)
    276         {
    277             setNull();
    278             raw_copy (str, s);
    279         }
    280     }
    281 
    282     inline static void raw_copy (char *&ls, const char *rs)
    283     {
    284         if (rs)
    285 //#if !defined (VBOX_WITH_XPCOM)
    286             ::RTStrDupEx (&ls, rs);
    287 //#else
    288 //            ls = (char *) nsMemory::Clone (rs, strlen (rs) + 1);
    289 //#endif
    290     }
    291 
    292     char *str;
    293 
    294 };
    29551
    29652class DHCPServerRunner
     
    32884    void detachFromServer();
    32985private:
    330     Utf8Str mOptions[DHCPCFG_NOTOPT_MAXVAL];
     86    com::Utf8Str mOptions[DHCPCFG_NOTOPT_MAXVAL];
    33187    RTPROCESS mProcess;
    33288};
  • trunk/src/VBox/Main/xpcom/server.cpp

    r18023 r18208  
    111111#include <ParallelPortImpl.h>
    112112#include <USBControllerImpl.h>
     113#include "DHCPServerRunner.h"
    113114#include "DHCPServerImpl.h"
    114115#ifdef VBOX_WITH_USB
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