VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/NATNetworkServiceRunner.cpp@ 49498

Last change on this file since 49498 was 48955, checked in by vboxsync, 11 years ago

Main: Whitespace (including tabs!) and svn:keywords cleanups by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1/* $Id: NATNetworkServiceRunner.cpp 48955 2013-10-07 21:59:25Z vboxsync $ */
2/** @file
3 * VirtualBox Main - interface for VBox NAT Network service
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 "NATNetworkServiceRunner.h"
18#include <iprt/process.h>
19#include <iprt/param.h>
20#include <iprt/env.h>
21
22struct ARGDEF
23{
24 NATSCCFG Type;
25 const char * Name;
26};
27
28#ifdef RT_OS_WINDOWS
29# define NATSR_EXECUTABLE_NAME "VBoxNetNAT.exe"
30#else
31# define NATSR_EXECUTABLE_NAME "VBoxNetNAT"
32#endif
33
34static const ARGDEF g_aArgDefs[] =
35{
36 {NATSCCFG_NAME, "-n"},
37 {NATSCCFG_TRUNKTYPE, "--trunk-type"},
38 {NATSCCFG_MACADDRESS, "--mac-address"},
39 {NATSCCFG_IPADDRESS, "--ip-address"},
40 {NATSCCFG_NETMASK, "--netmask"},
41 {NATSCCFG_PORTFORWARD4, "--pf4"},
42 {NATSCCFG_PORTFORWARD6, "--pf6"}
43
44};
45
46static const ARGDEF * getArgDef(NATSCCFG type)
47{
48 for (unsigned i = 0; i < RT_ELEMENTS(g_aArgDefs); i++)
49 if (g_aArgDefs[i].Type == type)
50 return &g_aArgDefs[i];
51
52 return NULL;
53}
54
55NATNetworkServiceRunner::NATNetworkServiceRunner()
56{
57 mProcess = NIL_RTPROCESS;
58 for (unsigned i = 0; i < NATSCCFG_NOTOPT_MAXVAL; i++)
59 {
60 mOptionEnabled[i] = false;
61 }
62}
63
64void NATNetworkServiceRunner::detachFromServer()
65{
66 mProcess = NIL_RTPROCESS;
67}
68
69int NATNetworkServiceRunner::start()
70{
71 if (isRunning())
72 return VINF_ALREADY_INITIALIZED;
73
74 const char * args[NATSCCFG_NOTOPT_MAXVAL * 2];
75
76 /* get the path to the executable */
77 char exePathBuf[RTPATH_MAX];
78 const char *exePath = RTProcGetExecutablePath(exePathBuf, RTPATH_MAX);
79 char *substrSl = strrchr(exePathBuf, '/');
80 char *substrBs = strrchr(exePathBuf, '\\');
81 char *suffix = substrSl ? substrSl : substrBs;
82
83 if (suffix)
84 {
85 suffix++;
86 strcpy(suffix, NATSR_EXECUTABLE_NAME);
87 }
88 else
89 exePath = NATSR_EXECUTABLE_NAME;
90
91 int index = 0;
92
93 args[index++] = exePath;
94
95 for (unsigned i = 0; i < NATSCCFG_NOTOPT_MAXVAL; i++)
96 {
97 if (mOptionEnabled[i])
98 {
99 const ARGDEF *pArgDef = getArgDef((NATSCCFG)i);
100 if (!pArgDef)
101 continue;
102 args[index++] = pArgDef->Name; // e.g. "--network"
103
104 /* value can be null for e.g. --begin-config has no value
105 * and thus check the mOptions string length here
106 */
107 if (mOptions[i].length())
108 args[index++] = mOptions[i].c_str(); // value
109 }
110 }
111
112 args[index++] = NULL;
113 RTENV env;
114 int rc = RTEnvCreate(&env);
115 AssertRCReturn(rc,rc);
116
117 RTEnvPutEx(env, "VBOX_LOG=e.l.f");
118
119 rc = RTProcCreate(exePath, args, RTENV_DEFAULT, 0, &mProcess);
120 if (RT_FAILURE(rc))
121 mProcess = NIL_RTPROCESS;
122 RTEnvDestroy(env);
123 return rc;
124}
125
126int NATNetworkServiceRunner::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 NATNetworkServiceRunner::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