1 | /* $Id: DHCPServerRunner.cpp 44529 2013-02-04 15:54:15Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Main - interface for VBox DHCP server
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 | #include "DHCPServerRunner.h"
|
---|
18 | #include <iprt/process.h>
|
---|
19 | #include <iprt/param.h>
|
---|
20 | #include <iprt/env.h>
|
---|
21 |
|
---|
22 | struct ARGDEF
|
---|
23 | {
|
---|
24 | DHCPCFG Type;
|
---|
25 | const char * Name;
|
---|
26 | };
|
---|
27 |
|
---|
28 | #ifdef RT_OS_WINDOWS
|
---|
29 | # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP.exe"
|
---|
30 | #else
|
---|
31 | # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP"
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | static const ARGDEF g_aArgDefs[] =
|
---|
35 | {
|
---|
36 | {DHCPCFG_NAME, "--name"},
|
---|
37 | {DHCPCFG_NETNAME, "--network"},
|
---|
38 | {DHCPCFG_TRUNKTYPE, "--trunk-type"},
|
---|
39 | {DHCPCFG_TRUNKNAME, "--trunk-name"},
|
---|
40 | {DHCPCFG_MACADDRESS, "--mac-address"},
|
---|
41 | {DHCPCFG_IPADDRESS, "--ip-address"},
|
---|
42 | {DHCPCFG_LEASEDB, "--lease-db"},
|
---|
43 | {DHCPCFG_VERBOSE, "--verbose"},
|
---|
44 | {DHCPCFG_BEGINCONFIG, "--begin-config"},
|
---|
45 | {DHCPCFG_GATEWAY, "--gateway"},
|
---|
46 | {DHCPCFG_LOWERIP, "--lower-ip"},
|
---|
47 | {DHCPCFG_UPPERIP, "--upper-ip"},
|
---|
48 | {DHCPCFG_NETMASK, "--netmask"},
|
---|
49 | {DHCPCFG_HELP, "--help"},
|
---|
50 | {DHCPCFG_VERSION, "--version"}
|
---|
51 | };
|
---|
52 |
|
---|
53 | static const ARGDEF * getArgDef(DHCPCFG type)
|
---|
54 | {
|
---|
55 | for (unsigned i = 0; i < RT_ELEMENTS(g_aArgDefs); i++)
|
---|
56 | if (g_aArgDefs[i].Type == type)
|
---|
57 | return &g_aArgDefs[i];
|
---|
58 |
|
---|
59 | return NULL;
|
---|
60 | }
|
---|
61 |
|
---|
62 | DHCPServerRunner::DHCPServerRunner()
|
---|
63 | {
|
---|
64 | mProcess = NIL_RTPROCESS;
|
---|
65 | for (unsigned i = 0; i < DHCPCFG_NOTOPT_MAXVAL; i++)
|
---|
66 | {
|
---|
67 | mOptionEnabled[i] = false;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | void DHCPServerRunner::detachFromServer()
|
---|
72 | {
|
---|
73 | mProcess = NIL_RTPROCESS;
|
---|
74 | }
|
---|
75 |
|
---|
76 | int DHCPServerRunner::start()
|
---|
77 | {
|
---|
78 | if (isRunning())
|
---|
79 | return VINF_ALREADY_INITIALIZED;
|
---|
80 |
|
---|
81 | const char * args[DHCPCFG_NOTOPT_MAXVAL * 2];
|
---|
82 |
|
---|
83 | /* get the path to the executable */
|
---|
84 | char exePathBuf[RTPATH_MAX];
|
---|
85 | const char *exePath = RTProcGetExecutablePath(exePathBuf, RTPATH_MAX);
|
---|
86 | char *substrSl = strrchr(exePathBuf, '/');
|
---|
87 | char *substrBs = strrchr(exePathBuf, '\\');
|
---|
88 | char *suffix = substrSl ? substrSl : substrBs;
|
---|
89 |
|
---|
90 | if (suffix)
|
---|
91 | {
|
---|
92 | suffix++;
|
---|
93 | strcpy(suffix, DHCP_EXECUTABLE_NAME);
|
---|
94 | }
|
---|
95 | else
|
---|
96 | exePath = DHCP_EXECUTABLE_NAME;
|
---|
97 |
|
---|
98 | int index = 0;
|
---|
99 |
|
---|
100 | args[index++] = exePath;
|
---|
101 |
|
---|
102 | for (unsigned i = 0; i < DHCPCFG_NOTOPT_MAXVAL; i++)
|
---|
103 | {
|
---|
104 | if (mOptionEnabled[i])
|
---|
105 | {
|
---|
106 | const ARGDEF *pArgDef = getArgDef((DHCPCFG)i);
|
---|
107 | if (!pArgDef)
|
---|
108 | continue;
|
---|
109 | args[index++] = pArgDef->Name; // e.g. "--network"
|
---|
110 |
|
---|
111 | /* value can be null for e.g. --begin-config has no value
|
---|
112 | * and thus check the mOptions string length here
|
---|
113 | */
|
---|
114 | if (mOptions[i].length())
|
---|
115 | args[index++] = mOptions[i].c_str(); // value
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | args[index++] = NULL;
|
---|
120 |
|
---|
121 | int rc = RTProcCreate(exePath, args, RTENV_DEFAULT, 0, &mProcess);
|
---|
122 | if (RT_FAILURE(rc))
|
---|
123 | mProcess = NIL_RTPROCESS;
|
---|
124 |
|
---|
125 | return rc;
|
---|
126 | }
|
---|
127 |
|
---|
128 | int DHCPServerRunner::stop()
|
---|
129 | {
|
---|
130 | if (!isRunning())
|
---|
131 | return VINF_OBJECT_DESTROYED;
|
---|
132 |
|
---|
133 | int rc = RTProcTerminate(mProcess);
|
---|
134 | mProcess = NIL_RTPROCESS;
|
---|
135 | return rc;
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool DHCPServerRunner::isRunning()
|
---|
139 | {
|
---|
140 | if (mProcess == NIL_RTPROCESS)
|
---|
141 | return false;
|
---|
142 |
|
---|
143 | RTPROCSTATUS status;
|
---|
144 | int rc = RTProcWait(mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status);
|
---|
145 |
|
---|
146 | if (rc == VERR_PROCESS_RUNNING)
|
---|
147 | return true;
|
---|
148 |
|
---|
149 | mProcess = NIL_RTPROCESS;
|
---|
150 | return false;
|
---|
151 | }
|
---|