VirtualBox

source: vbox/trunk/src/VBox/Main/DHCPServerRunner.cpp@ 21827

Last change on this file since 21827 was 21404, checked in by vboxsync, 15 years ago

IPRT, Main: make ministring throw std::bad_alloc on allocation failure; remove isEmpty() and isNull(), change Main code to using length() instead; second try

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1/* $Id: DHCPServerRunner.cpp 21404 2009-07-08 15:19:42Z 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
26struct 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
38static 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
57static 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
66void DHCPServerRunner::detachFromServer()
67{
68 mProcess = NIL_RTPROCESS;
69}
70
71int DHCPServerRunner::start()
72{
73 if (isRunning())
74 return VINF_ALREADY_INITIALIZED;
75
76 const char * args[DHCPCFG_NOTOPT_MAXVAL * 2];
77
78 /* get the path to the executable */
79 char exePathBuf [RTPATH_MAX];
80 const char *exePath = RTProcGetExecutableName (exePathBuf, RTPATH_MAX);
81 char *substrSl = strrchr(exePathBuf, '/');
82 char *substrBs = strrchr(exePathBuf, '\\');
83 char *suffix = substrSl ? substrSl : substrBs;
84
85 if (suffix)
86 {
87 suffix++;
88 strcpy(suffix, DHCP_EXECUTABLE_NAME);
89 }
90 else
91 exePath = DHCP_EXECUTABLE_NAME;
92
93 int index = 0;
94
95 args[index++] = exePath;
96
97 for (unsigned i = 0; i < DHCPCFG_NOTOPT_MAXVAL; i++)
98 {
99 if (mOptions[i].length())
100 {
101 const ARGDEF * pArgDef = getArgDef((DHCPCFG)i);
102 args[index++] = pArgDef->Name; // e.g. "--network"
103 args[index++] = mOptions[i].raw(); // value
104 }
105 }
106
107 args[index++] = NULL;
108
109 int rc = RTProcCreate (exePath, args, RTENV_DEFAULT, 0, &mProcess);
110 if (RT_FAILURE (rc))
111 mProcess = NIL_RTPROCESS;
112
113 return rc;
114}
115
116int DHCPServerRunner::stop()
117{
118 if (!isRunning())
119 return VINF_OBJECT_DESTROYED;
120
121 int rc = RTProcTerminate(mProcess);
122 mProcess = NIL_RTPROCESS;
123 return rc;
124}
125
126bool DHCPServerRunner::isRunning()
127{
128 if(mProcess == NIL_RTPROCESS)
129 return false;
130
131 RTPROCSTATUS status;
132 int rc = RTProcWait(mProcess, RTPROCWAIT_FLAGS_NOBLOCK, &status);
133
134 if(rc == VERR_PROCESS_RUNNING)
135 return true;
136
137 mProcess = NIL_RTPROCESS;
138 return false;
139}
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