1 | /* $Id: DHCPServerRunner.h 18039 2009-03-17 16:43:44Z 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 <iprt/err.h>
|
---|
22 | #include <iprt/types.h>
|
---|
23 | #include <iprt/string.h>
|
---|
24 | #include <iprt/mem.h>
|
---|
25 | //#include <VBox/com/string.h>
|
---|
26 |
|
---|
27 | //using namespace com;
|
---|
28 | typedef enum
|
---|
29 | {
|
---|
30 | DHCPCFG_NAME = 1,
|
---|
31 | DHCPCFG_NETNAME,
|
---|
32 | DHCPCFG_TRUNKTYPE,
|
---|
33 | DHCPCFG_TRUNKNAME,
|
---|
34 | DHCPCFG_MACADDRESS,
|
---|
35 | DHCPCFG_IPADDRESS,
|
---|
36 | DHCPCFG_LEASEDB,
|
---|
37 | DHCPCFG_VERBOSE,
|
---|
38 | DHCPCFG_GATEWAY,
|
---|
39 | DHCPCFG_LOWERIP,
|
---|
40 | DHCPCFG_UPPERIP,
|
---|
41 | DHCPCFG_NETMASK,
|
---|
42 | DHCPCFG_HELP,
|
---|
43 | DHCPCFG_VERSION,
|
---|
44 | DHCPCFG_BEGINCONFIG,
|
---|
45 | DHCPCFG_NOTOPT_MAXVAL
|
---|
46 | }DHCPCFG;
|
---|
47 |
|
---|
48 | #define TRUNKTYPE_WHATEVER "whatever"
|
---|
49 | #define TRUNKTYPE_NETFLT "netflt"
|
---|
50 | #define TRUNKTYPE_NETADP "netadp"
|
---|
51 | #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 | };
|
---|
295 |
|
---|
296 | class DHCPServerRunner
|
---|
297 | {
|
---|
298 | public:
|
---|
299 | DHCPServerRunner() : mProcess (NIL_RTPROCESS) {}
|
---|
300 | ~DHCPServerRunner() { stop(); /* don't leave abandoned servers */}
|
---|
301 |
|
---|
302 | int setOption(DHCPCFG opt, const char *val)
|
---|
303 | {
|
---|
304 | if(opt == 0 || opt >= DHCPCFG_NOTOPT_MAXVAL)
|
---|
305 | return VERR_INVALID_PARAMETER;
|
---|
306 | if(isRunning())
|
---|
307 | return VERR_INVALID_STATE;
|
---|
308 |
|
---|
309 | #ifdef RT_OS_WINDOWS
|
---|
310 | if(val && strlen(val))
|
---|
311 | {
|
---|
312 | mOptions[opt] = "\"";
|
---|
313 | mOptions[opt].append(val);
|
---|
314 | mOptions[opt].append("\"");
|
---|
315 | }
|
---|
316 | #endif
|
---|
317 | else
|
---|
318 | {
|
---|
319 | mOptions[opt] = val;
|
---|
320 | }
|
---|
321 | return VINF_SUCCESS;
|
---|
322 | }
|
---|
323 |
|
---|
324 | int start();
|
---|
325 | int stop();
|
---|
326 | bool isRunning();
|
---|
327 |
|
---|
328 | void detachFromServer();
|
---|
329 | private:
|
---|
330 | Utf8Str mOptions[DHCPCFG_NOTOPT_MAXVAL];
|
---|
331 | RTPROCESS mProcess;
|
---|
332 | };
|
---|