Index: trunk/src/VBox/Main/ConsoleImpl2.cpp
===================================================================
--- trunk/src/VBox/Main/ConsoleImpl2.cpp	(revision 18206)
+++ trunk/src/VBox/Main/ConsoleImpl2.cpp	(revision 18208)
@@ -84,4 +84,6 @@
 # include <netif.h>
 #endif
+
+#include "DHCPServerRunner.h"
 
 #include <VBox/param.h>
@@ -1307,5 +1309,5 @@
         NetworkAttachmentType_T networkAttachment;
         hrc = networkAdapter->COMGETTER(AttachmentType)(&networkAttachment);        H();
-        Bstr networkName;
+        Bstr networkName, trunkName, trunkType;
         switch (networkAttachment)
         {
@@ -1353,5 +1355,6 @@
                     {
                         rc = CFGMR3InsertString(pCfg, "Network", psz);              RC_CHECK();
-                        networkName = Bstr(psz);
+                        /* NAT uses its own DHCP implementation */
+                        //networkName = Bstr(psz);
                     }
 
@@ -1498,4 +1501,6 @@
                     rc = CFGMR3InsertString(pCfg, "Network", szNetwork);            RC_CHECK();
                     networkName = Bstr(szNetwork);
+                    trunkName = Bstr(pszTrunk);
+                    trunkType = Bstr(TRUNKTYPE_NETFLT);
 
 # if defined(RT_OS_DARWIN)
@@ -1715,5 +1720,7 @@
                         rc = CFGMR3InsertNode(pLunL0, "Config", &pCfg);             RC_CHECK();
                         rc = CFGMR3InsertString(pCfg, "Network", psz);              RC_CHECK();
+                        rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_WhateverNone); RC_CHECK();
                         networkName = Bstr(psz);
+                        trunkType = Bstr(TRUNKTYPE_WHATEVER);
                     }
                     STR_FREE();
@@ -1791,4 +1798,6 @@
                 rc = CFGMR3InsertString(pCfg, "Network", szNetwork);            RC_CHECK();
                 networkName = Bstr(szNetwork);
+                trunkName   = Bstr(pszTrunk);
+                trunkType   = TRUNKTYPE_NETADP;
 #elif defined(RT_OS_DARWIN)
                 rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0");             RC_CHECK();
@@ -1796,4 +1805,6 @@
                 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetAdp); RC_CHECK();
                 networkName = Bstr("HostInterfaceNetworking-vboxnet0");
+                trunkName   = Bstr("vboxnet0");
+                trunkType   = TRUNKTYPE_NETADP;
 #else
                 rc = CFGMR3InsertString(pCfg, "Trunk", "vboxnet0");             RC_CHECK();
@@ -1801,4 +1812,6 @@
                 rc = CFGMR3InsertInteger(pCfg, "TrunkType", kIntNetTrunkType_NetFlt); RC_CHECK();
                 networkName = Bstr("HostInterfaceNetworking-vboxnet0");
+                trunkName   = Bstr("vboxnet0");
+                trunkType   = TRUNKTYPE_NETFLT;
 #endif
 #if !defined(RT_OS_WINDOWS) && defined(VBOX_WITH_NETFLT)
@@ -1851,4 +1864,8 @@
         if(!networkName.isNull())
         {
+            /*
+             * Until we implement service reference counters DHCP Server will be stopped
+             * by DHCPServerRunner destructor.
+             */
             ComPtr<IDHCPServer> dhcpServer;
             hrc = virtualBox->FindDHCPServerByNetworkName(networkName.mutableRaw(), dhcpServer.asOutParam());
@@ -1863,49 +1880,7 @@
                     H();
                 }
-
+            
                 if(bEnabled)
-                {
-                    Bstr ip, mask, lowerIp, upperIp;
-
-                    hrc = dhcpServer->COMGETTER(IPAddress)(ip.asOutParam());
-                    if(FAILED(hrc))
-                    {
-                        LogRel(("DHCP svr: COMGETTER(IPAddress) failed, hrc (0x%x)", hrc));
-                        H();
-                    }
-
-                    hrc = dhcpServer->COMGETTER(NetworkMask)(mask.asOutParam());
-                    if(FAILED(hrc))
-                    {
-                        LogRel(("DHCP svr: COMGETTER(NetworkMask) failed, hrc (0x%x)", hrc));
-                        H();
-                    }
-
-                    hrc = dhcpServer->COMGETTER(LowerIP)(lowerIp.asOutParam());
-                    if(FAILED(hrc))
-                    {
-                        LogRel(("DHCP svr: COMGETTER(LowerIP) failed, hrc (0x%x)", hrc));
-                        H();
-                    }
-
-                    hrc = dhcpServer->COMGETTER(UpperIP)(upperIp.asOutParam());
-                    if(FAILED(hrc))
-                    {
-                        LogRel(("DHCP svr: COMGETTER(UpperIP) failed, hrc (0x%x)", hrc));
-                        H();
-                    }
-
-                    char strMAC[13];
-                    Guid guid;
-                    guid.create();
-                    RTStrPrintf (strMAC, sizeof(strMAC), "080027%02X%02X%02X",
-                                 guid.ptr()->au8[0], guid.ptr()->au8[1], guid.ptr()->au8[2]);
-
-                    rc = CFGMR3InsertString(pCfg, "DhcpIPAddress", Utf8Str(ip).raw());             RC_CHECK();
-                    rc = CFGMR3InsertString(pCfg, "DhcpNetworkMask", Utf8Str(mask).raw());             RC_CHECK();
-                    rc = CFGMR3InsertString(pCfg, "DhcpLowerIP", Utf8Str(lowerIp).raw());             RC_CHECK();
-                    rc = CFGMR3InsertString(pCfg, "DhcpUpperIP", Utf8Str(upperIp).raw());             RC_CHECK();
-                    rc = CFGMR3InsertString(pCfg, "DhcpMacAddress", strMAC);             RC_CHECK();
-                }
+                    hrc = dhcpServer->Start(networkName, trunkName, trunkType);
             }
             else
Index: trunk/src/VBox/Main/DHCPServerImpl.cpp
===================================================================
--- trunk/src/VBox/Main/DHCPServerImpl.cpp	(revision 18206)
+++ trunk/src/VBox/Main/DHCPServerImpl.cpp	(revision 18208)
@@ -22,4 +22,5 @@
  */
 
+#include "DHCPServerRunner.h"
 #include "DHCPServerImpl.h"
 #include "Logging.h"
@@ -240,2 +241,43 @@
     return mVirtualBox->saveSettings();
 }
+
+STDMETHODIMP DHCPServer::Start (IN_BSTR aNetworkName, IN_BSTR aTrunkName, IN_BSTR aTrunkType)
+{
+    /* Silently ignore attepmts to run disabled servers. */
+    if (!m.enabled)
+        return S_OK;
+
+    m.dhcp.setOption(DHCPCFG_NETNAME, Utf8Str(aNetworkName));
+    Bstr tmp(aTrunkName);
+    if (!tmp.isEmpty())
+        m.dhcp.setOption(DHCPCFG_TRUNKNAME, Utf8Str(tmp));
+    m.dhcp.setOption(DHCPCFG_TRUNKTYPE, Utf8Str(aTrunkType));
+    //temporary hack for testing
+    //    DHCPCFG_NAME
+    char strMAC[13];
+    Guid guid;
+    guid.create();
+    RTStrPrintf (strMAC, sizeof(strMAC), "080027%02X%02X%02X",
+                 guid.ptr()->au8[0], guid.ptr()->au8[1], guid.ptr()->au8[2]);
+    m.dhcp.setOption(DHCPCFG_MACADDRESS, strMAC);
+    m.dhcp.setOption(DHCPCFG_IPADDRESS,  Utf8Str(m.IPAddress));
+    //        DHCPCFG_LEASEDB,
+    //        DHCPCFG_VERBOSE,
+    //        DHCPCFG_GATEWAY,
+    m.dhcp.setOption(DHCPCFG_LOWERIP,  Utf8Str(m.lowerIP));
+    m.dhcp.setOption(DHCPCFG_UPPERIP,  Utf8Str(m.upperIP));
+    m.dhcp.setOption(DHCPCFG_NETMASK,  Utf8Str(m.networkMask));
+
+    //        DHCPCFG_HELP,
+    //        DHCPCFG_VERSION,
+    //        DHCPCFG_NOTOPT_MAXVAL
+    m.dhcp.setOption(DHCPCFG_BEGINCONFIG,  "");
+
+    return RT_FAILURE(m.dhcp.start()) ? E_FAIL : S_OK;
+    //m.dhcp.detachFromServer(); /* need to do this to avoid server shutdown on runner destruction */
+}
+
+STDMETHODIMP DHCPServer::Stop (void)
+{
+    return RT_FAILURE(m.dhcp.stop()) ? E_FAIL : S_OK;
+}
Index: trunk/src/VBox/Main/DHCPServerRunner.cpp
===================================================================
--- trunk/src/VBox/Main/DHCPServerRunner.cpp	(revision 18208)
+++ trunk/src/VBox/Main/DHCPServerRunner.cpp	(revision 18208)
@@ -0,0 +1,148 @@
+/* $Id$ */
+/** @file
+ * VirtualBox Main - interface for VBox DHCP server
+ */
+
+/*
+ * Copyright (C) 2009 Sun Microsystems, Inc.
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
+ * Clara, CA 95054 USA or visit http://www.sun.com if you need
+ * additional information or have any questions.
+ */
+#include "DHCPServerRunner.h"
+#include <iprt/process.h>
+#include <iprt/param.h>
+#include <iprt/env.h>
+
+struct ARGDEF
+{
+    DHCPCFG Type;
+    const char * Name;
+};
+
+#ifdef RT_OS_WINDOWS
+# define DHCP_EXECUTABLE_NAME "VBoxNetDHCP.exe"
+#else
+# define DHCP_EXECUTABLE_NAME "VBoxNetDHCP"
+#endif
+
+static const ARGDEF g_aArgDefs[] = {
+        {DHCPCFG_NAME, "--name"},
+        {DHCPCFG_NETNAME, "--network"},
+        {DHCPCFG_TRUNKTYPE, "--trunk-type"},
+        {DHCPCFG_TRUNKNAME, "--trunk-name"},
+        {DHCPCFG_MACADDRESS, "--mac-address"},
+        {DHCPCFG_IPADDRESS, "--ip-address"},
+        {DHCPCFG_LEASEDB, "--lease-db"},
+        {DHCPCFG_VERBOSE, "--verbose"},
+        {DHCPCFG_BEGINCONFIG, "--begin-config"},
+        {DHCPCFG_GATEWAY, "--gateway"},
+        {DHCPCFG_LOWERIP, "--lower-ip"},
+        {DHCPCFG_UPPERIP, "--upper-ip"},
+        {DHCPCFG_NETMASK, "--netmask"},
+        {DHCPCFG_HELP, "--help"},
+        {DHCPCFG_VERSION, "--version"}
+};
+
+static const ARGDEF * getArgDef(DHCPCFG type)
+{
+    for (unsigned i = 0; i < RT_ELEMENTS(g_aArgDefs); i++)
+    {
+        if(g_aArgDefs[i].Type == type)
+        {
+            return &g_aArgDefs[i];
+        }
+    }
+    return NULL;
+}
+
+void DHCPServerRunner::detachFromServer()
+{
+    mProcess = NIL_RTPROCESS;
+}
+
+int DHCPServerRunner::start()
+{
+    if(isRunning())
+        return VINF_ALREADY_INITIALIZED;
+
+    const char * args[DHCPCFG_NOTOPT_MAXVAL * 2];
+
+    /* get the path to the executable */
+//    const char *exePath = DHCP_EXECUTABLE_NAME;
+    char exePathBuf [RTPATH_MAX];
+    char *exePath = RTProcGetExecutableName (exePathBuf, RTPATH_MAX);
+    char *substrSl = strrchr( exePath, '/');
+    char *substrBs = strrchr( exePath, '\\');
+    char *suffix = substrSl ? substrSl : substrBs;
+
+    if(suffix)
+    {
+        suffix++;
+        strcpy(suffix, DHCP_EXECUTABLE_NAME);
+    }
+    else
+    {
+        exePath = DHCP_EXECUTABLE_NAME;
+    }
+
+    int index = 0;
+
+    args[index++] = exePath;
+
+    for(int i = 0; i < DHCPCFG_NOTOPT_MAXVAL; i++)
+    {
+        if(!mOptions[i].isNull())
+        {
+            const ARGDEF * pArgDef = getArgDef((DHCPCFG)i);
+            args[index++] = pArgDef->Name;
+            if(!mOptions[i].isEmpty())
+            {
+                args[index++] = mOptions[i].raw();
+            }
+        }
+    }
+
+    args[index++] = NULL;
+
+    int rc = RTProcCreate (exePath, args, RTENV_DEFAULT, 0, &mProcess);
+    if (RT_FAILURE (rc))
+    {
+        mProcess = NIL_RTPROCESS;
+    }
+    return rc;
+}
+
+int DHCPServerRunner::stop()
+{
+    if(!isRunning())
+        return VINF_OBJECT_DESTROYED;
+
+    int rc = RTProcTerminate(mProcess);
+    mProcess = NIL_RTPROCESS;
+    return rc;
+}
+
+bool DHCPServerRunner::isRunning()
+{
+    if(mProcess == NIL_RTPROCESS)
+        return false;
+
+    RTPROCSTATUS status;
+    int rc = RTProcWait(mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status);
+
+    if(rc == VERR_PROCESS_RUNNING)
+        return true;
+
+    mProcess = NIL_RTPROCESS;
+    return false;
+}
Index: trunk/src/VBox/Main/Makefile.kmk
===================================================================
--- trunk/src/VBox/Main/Makefile.kmk	(revision 18206)
+++ trunk/src/VBox/Main/Makefile.kmk	(revision 18208)
@@ -280,4 +280,5 @@
 	HostNetworkInterfaceImpl.cpp \
 	DHCPServerImpl.cpp \
+	DHCPServerRunner.cpp \
 	GuestOSTypeImpl.cpp \
 	NetworkAdapterImpl.cpp \
Index: trunk/src/VBox/Main/VirtualBoxImpl.cpp
===================================================================
--- trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 18206)
+++ trunk/src/VBox/Main/VirtualBoxImpl.cpp	(revision 18208)
@@ -62,4 +62,5 @@
 #include "GuestOSTypeImpl.h"
 #include "Version.h"
+#include "DHCPServerRunner.h"
 #include "DHCPServerImpl.h"
 
Index: trunk/src/VBox/Main/idl/VirtualBox.xidl
===================================================================
--- trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 18206)
+++ trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 18208)
@@ -1232,5 +1232,5 @@
   <interface
     name="IDHCPServer" extends="$unknown"
-    uuid="fab58256-c76e-4ddd-8029-18343e5b0069"
+    uuid="6cfe387c-74fb-4ca7-bff6-973bec8af7a3"
     wsmap="managed"
   >
@@ -1305,4 +1305,37 @@
         </desc>
       </param>
+    </method>
+
+    <method name="start">
+      <desc>
+        Starts DHCP server process.
+        <result name="E_FAIL">
+          Failed to start the process.
+        </result>
+      </desc>
+      <param name="networkName" type="wstring" dir="in">
+        <desc>
+          Name of internal network DHCP server should attach to.
+        </desc>
+      </param>
+      <param name="trunkName" type="wstring" dir="in">
+        <desc>
+          Name of internal network trunk.
+        </desc>
+      </param>
+      <param name="trunkType" type="wstring" dir="in">
+        <desc>
+          Type of internal network trunk.
+        </desc>
+      </param>
+    </method>
+
+    <method name="stop">
+      <desc>
+        Stops DHCP server process.
+        <result name="E_FAIL">
+          Failed to stop the process.
+        </result>
+      </desc>
     </method>
   </interface>
Index: trunk/src/VBox/Main/include/DHCPServerImpl.h
===================================================================
--- trunk/src/VBox/Main/include/DHCPServerImpl.h	(revision 18206)
+++ trunk/src/VBox/Main/include/DHCPServerImpl.h	(revision 18208)
@@ -77,4 +77,7 @@
     STDMETHOD(SetConfiguration) (IN_BSTR aIPAddress, IN_BSTR aNetworkMask, IN_BSTR aFromIPAddress, IN_BSTR aToIPAddress);
 
+    STDMETHOD(Start) (IN_BSTR aNetworkName, IN_BSTR aTrunkName, IN_BSTR aTrunkType);
+    STDMETHOD(Stop) ();
+
     // for VirtualBoxSupportErrorInfoImpl
     static const wchar_t *getComponentName() { return L"DHCPServer"; }
@@ -95,4 +98,6 @@
         Bstr upperIP;
         BOOL enabled;
+
+        DHCPServerRunner dhcp;
     } m;
 
Index: trunk/src/VBox/Main/include/DHCPServerRunner.h
===================================================================
--- trunk/src/VBox/Main/include/DHCPServerRunner.h	(revision 18208)
+++ trunk/src/VBox/Main/include/DHCPServerRunner.h	(revision 18208)
@@ -0,0 +1,88 @@
+/* $Id$ */
+/** @file
+ * VirtualBox Main - interface for VBox DHCP server
+ */
+
+/*
+ * Copyright (C) 2009 Sun Microsystems, Inc.
+ *
+ * This file is part of VirtualBox Open Source Edition (OSE), as
+ * available from http://www.virtualbox.org. This file is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU
+ * General Public License (GPL) as published by the Free Software
+ * Foundation, in version 2 as it comes in the "COPYING" file of the
+ * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
+ * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
+ * Clara, CA 95054 USA or visit http://www.sun.com if you need
+ * additional information or have any questions.
+ */
+#include <iprt/err.h>
+#include <iprt/types.h>
+#include <iprt/string.h>
+#include <iprt/mem.h>
+#include <VBox/com/string.h>
+
+typedef enum
+{
+    DHCPCFG_NAME = 1,
+    DHCPCFG_NETNAME,
+    DHCPCFG_TRUNKTYPE,
+    DHCPCFG_TRUNKNAME,
+    DHCPCFG_MACADDRESS,
+    DHCPCFG_IPADDRESS,
+    DHCPCFG_LEASEDB,
+    DHCPCFG_VERBOSE,
+    DHCPCFG_GATEWAY,
+    DHCPCFG_LOWERIP,
+    DHCPCFG_UPPERIP,
+    DHCPCFG_NETMASK,
+    DHCPCFG_HELP,
+    DHCPCFG_VERSION,
+    DHCPCFG_BEGINCONFIG,
+    DHCPCFG_NOTOPT_MAXVAL
+}DHCPCFG;
+
+#define TRUNKTYPE_WHATEVER "whatever"
+#define TRUNKTYPE_NETFLT   "netflt"
+#define TRUNKTYPE_NETADP   "netadp"
+#define TRUNKTYPE_SRVNAT   "srvnat"
+
+class DHCPServerRunner
+{
+public:
+    DHCPServerRunner() : mProcess (NIL_RTPROCESS) {}
+    ~DHCPServerRunner() { stop(); /* don't leave abandoned servers */}
+
+    int setOption(DHCPCFG opt, const char *val)
+    {
+        if(opt == 0 || opt >= DHCPCFG_NOTOPT_MAXVAL)
+            return VERR_INVALID_PARAMETER;
+        if(isRunning())
+            return VERR_INVALID_STATE;
+
+#ifdef RT_OS_WINDOWS
+        if(val && strlen(val))
+        {
+            mOptions[opt] = "\"";
+            mOptions[opt].append(val);
+            mOptions[opt].append("\"");
+        }
+#endif
+        else
+        {
+            mOptions[opt] = val;
+        }
+        return VINF_SUCCESS;
+    }
+
+    int start();
+    int stop();
+    bool isRunning();
+
+    void detachFromServer();
+private:
+    com::Utf8Str mOptions[DHCPCFG_NOTOPT_MAXVAL];
+    RTPROCESS mProcess;
+};
Index: trunk/src/VBox/Main/xpcom/server.cpp
===================================================================
--- trunk/src/VBox/Main/xpcom/server.cpp	(revision 18206)
+++ trunk/src/VBox/Main/xpcom/server.cpp	(revision 18208)
@@ -111,4 +111,5 @@
 #include <ParallelPortImpl.h>
 #include <USBControllerImpl.h>
+#include "DHCPServerRunner.h"
 #include "DHCPServerImpl.h"
 #ifdef VBOX_WITH_USB
