VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/DHCPServerRunner.cpp@ 41105

Last change on this file since 41105 was 35368, checked in by vboxsync, 14 years ago

Main: source re-org.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/* $Id: DHCPServerRunner.cpp 35368 2010-12-30 13:38:23Z vboxsync $ */
2/** @file
3 * VirtualBox Main - interface for VBox DHCP server
4 */
5
6/*
7 * Copyright (C) 2009 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
22struct 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
34static 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
53static 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
62DHCPServerRunner::DHCPServerRunner()
63{
64 mProcess = NIL_RTPROCESS;
65 for (unsigned i = 0; i < DHCPCFG_NOTOPT_MAXVAL; i++)
66 {
67 mOptionEnabled[i] = false;
68 }
69}
70
71void DHCPServerRunner::detachFromServer()
72{
73 mProcess = NIL_RTPROCESS;
74}
75
76int 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 args[index++] = pArgDef->Name; // e.g. "--network"
108
109 /* value can be null for e.g. --begin-config has no value
110 * and thus check the mOptions string length here
111 */
112 if (mOptions[i].length())
113 args[index++] = mOptions[i].c_str(); // value
114 }
115 }
116
117 args[index++] = NULL;
118
119 int rc = RTProcCreate(exePath, args, RTENV_DEFAULT, 0, &mProcess);
120 if (RT_FAILURE(rc))
121 mProcess = NIL_RTPROCESS;
122
123 return rc;
124}
125
126int DHCPServerRunner::stop()
127{
128 if (!isRunning())
129 return VINF_OBJECT_DESTROYED;
130
131 int rc = RTProcTerminate(mProcess);
132 mProcess = NIL_RTPROCESS;
133 return rc;
134}
135
136bool DHCPServerRunner::isRunning()
137{
138 if (mProcess == NIL_RTPROCESS)
139 return false;
140
141 RTPROCSTATUS status;
142 int rc = RTProcWait(mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status);
143
144 if (rc == VERR_PROCESS_RUNNING)
145 return true;
146
147 mProcess = NIL_RTPROCESS;
148 return false;
149}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette