1 | /* $Id: DHCPServerRunner.cpp 21878 2009-07-30 12:42:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Main - interface for VBox DHCP server
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 | #include "DHCPServerRunner.h"
|
---|
22 | #include <iprt/process.h>
|
---|
23 | #include <iprt/param.h>
|
---|
24 | #include <iprt/env.h>
|
---|
25 |
|
---|
26 | struct ARGDEF
|
---|
27 | {
|
---|
28 | DHCPCFG Type;
|
---|
29 | const char * Name;
|
---|
30 | };
|
---|
31 |
|
---|
32 | #ifdef RT_OS_WINDOWS
|
---|
33 | # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP.exe"
|
---|
34 | #else
|
---|
35 | # define DHCP_EXECUTABLE_NAME "VBoxNetDHCP"
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | static const ARGDEF g_aArgDefs[] =
|
---|
39 | {
|
---|
40 | {DHCPCFG_NAME, "--name"},
|
---|
41 | {DHCPCFG_NETNAME, "--network"},
|
---|
42 | {DHCPCFG_TRUNKTYPE, "--trunk-type"},
|
---|
43 | {DHCPCFG_TRUNKNAME, "--trunk-name"},
|
---|
44 | {DHCPCFG_MACADDRESS, "--mac-address"},
|
---|
45 | {DHCPCFG_IPADDRESS, "--ip-address"},
|
---|
46 | {DHCPCFG_LEASEDB, "--lease-db"},
|
---|
47 | {DHCPCFG_VERBOSE, "--verbose"},
|
---|
48 | {DHCPCFG_BEGINCONFIG, "--begin-config"},
|
---|
49 | {DHCPCFG_GATEWAY, "--gateway"},
|
---|
50 | {DHCPCFG_LOWERIP, "--lower-ip"},
|
---|
51 | {DHCPCFG_UPPERIP, "--upper-ip"},
|
---|
52 | {DHCPCFG_NETMASK, "--netmask"},
|
---|
53 | {DHCPCFG_HELP, "--help"},
|
---|
54 | {DHCPCFG_VERSION, "--version"}
|
---|
55 | };
|
---|
56 |
|
---|
57 | static const ARGDEF * getArgDef(DHCPCFG type)
|
---|
58 | {
|
---|
59 | for (unsigned i = 0; i < RT_ELEMENTS(g_aArgDefs); i++)
|
---|
60 | if(g_aArgDefs[i].Type == type)
|
---|
61 | return &g_aArgDefs[i];
|
---|
62 |
|
---|
63 | return NULL;
|
---|
64 | }
|
---|
65 |
|
---|
66 | DHCPServerRunner::DHCPServerRunner()
|
---|
67 | {
|
---|
68 | mProcess = NIL_RTPROCESS;
|
---|
69 | for (unsigned i = 0; i < DHCPCFG_NOTOPT_MAXVAL; i++)
|
---|
70 | {
|
---|
71 | mOptionEnabled[i] = false;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | void DHCPServerRunner::detachFromServer()
|
---|
76 | {
|
---|
77 | mProcess = NIL_RTPROCESS;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int DHCPServerRunner::start()
|
---|
81 | {
|
---|
82 | if (isRunning())
|
---|
83 | return VINF_ALREADY_INITIALIZED;
|
---|
84 |
|
---|
85 | const char * args[DHCPCFG_NOTOPT_MAXVAL * 2];
|
---|
86 |
|
---|
87 | /* get the path to the executable */
|
---|
88 | char exePathBuf [RTPATH_MAX];
|
---|
89 | const char *exePath = RTProcGetExecutableName (exePathBuf, RTPATH_MAX);
|
---|
90 | char *substrSl = strrchr(exePathBuf, '/');
|
---|
91 | char *substrBs = strrchr(exePathBuf, '\\');
|
---|
92 | char *suffix = substrSl ? substrSl : substrBs;
|
---|
93 |
|
---|
94 | if (suffix)
|
---|
95 | {
|
---|
96 | suffix++;
|
---|
97 | strcpy(suffix, DHCP_EXECUTABLE_NAME);
|
---|
98 | }
|
---|
99 | else
|
---|
100 | exePath = DHCP_EXECUTABLE_NAME;
|
---|
101 |
|
---|
102 | int index = 0;
|
---|
103 |
|
---|
104 | args[index++] = exePath;
|
---|
105 |
|
---|
106 | for (unsigned i = 0; i < DHCPCFG_NOTOPT_MAXVAL; i++)
|
---|
107 | {
|
---|
108 | if (mOptionEnabled[i])
|
---|
109 | {
|
---|
110 | const ARGDEF * pArgDef = getArgDef((DHCPCFG)i);
|
---|
111 | args[index++] = pArgDef->Name; // e.g. "--network"
|
---|
112 |
|
---|
113 | /* value can be null for e.g. --begin-config has no value
|
---|
114 | * and thus check the mOptions string length here
|
---|
115 | */
|
---|
116 | if (mOptions[i].length())
|
---|
117 | args[index++] = mOptions[i].raw(); // value
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | args[index++] = NULL;
|
---|
122 |
|
---|
123 | int rc = RTProcCreate (exePath, args, RTENV_DEFAULT, 0, &mProcess);
|
---|
124 | if (RT_FAILURE(rc))
|
---|
125 | mProcess = NIL_RTPROCESS;
|
---|
126 |
|
---|
127 | return rc;
|
---|
128 | }
|
---|
129 |
|
---|
130 | int DHCPServerRunner::stop()
|
---|
131 | {
|
---|
132 | if (!isRunning())
|
---|
133 | return VINF_OBJECT_DESTROYED;
|
---|
134 |
|
---|
135 | int rc = RTProcTerminate(mProcess);
|
---|
136 | mProcess = NIL_RTPROCESS;
|
---|
137 | return rc;
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool DHCPServerRunner::isRunning()
|
---|
141 | {
|
---|
142 | if(mProcess == NIL_RTPROCESS)
|
---|
143 | return false;
|
---|
144 |
|
---|
145 | RTPROCSTATUS status;
|
---|
146 | int rc = RTProcWait(mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status);
|
---|
147 |
|
---|
148 | if(rc == VERR_PROCESS_RUNNING)
|
---|
149 | return true;
|
---|
150 |
|
---|
151 | mProcess = NIL_RTPROCESS;
|
---|
152 | return false;
|
---|
153 | }
|
---|