VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/generic/NetIf-generic.cpp@ 39429

Last change on this file since 39429 was 39429, checked in by vboxsync, 13 years ago

Main/NetIf: Fixed segmentation fault in VBoxSVC on host-creation failure

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: NetIf-generic.cpp 39429 2011-11-28 06:22:46Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Generic NetIf implementation.
4 */
5
6/*
7 * Copyright (C) 2009-2010 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
18#include <VBox/err.h>
19#include <VBox/log.h>
20#include <iprt/process.h>
21#include <iprt/env.h>
22#include <iprt/path.h>
23#include <iprt/param.h>
24
25#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
26# include <cstdio>
27#endif
28
29#include "HostNetworkInterfaceImpl.h"
30#include "ProgressImpl.h"
31#include "VirtualBoxImpl.h"
32#include "netif.h"
33
34#define VBOXNETADPCTL_NAME "VBoxNetAdpCtl"
35
36static int NetIfAdpCtl(const char * pcszIfName, const char *pszAddr, const char *pszOption, const char *pszMask)
37{
38 const char *args[] = { NULL, pcszIfName, pszAddr, pszOption, pszMask, NULL };
39
40 char szAdpCtl[RTPATH_MAX];
41 int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME));
42 if (RT_FAILURE(rc))
43 {
44 LogRel(("NetIfAdpCtl: failed to get program path, rc=%Rrc.\n", rc));
45 return rc;
46 }
47 strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME);
48 args[0] = szAdpCtl;
49 if (!RTPathExists(szAdpCtl))
50 {
51 LogRel(("NetIfAdpCtl: path %s does not exist. Failed to run " VBOXNETADPCTL_NAME " helper.\n",
52 szAdpCtl));
53 return VERR_FILE_NOT_FOUND;
54 }
55
56 RTPROCESS pid;
57 rc = RTProcCreate(szAdpCtl, args, RTENV_DEFAULT, 0, &pid);
58 if (RT_SUCCESS(rc))
59 {
60 RTPROCSTATUS Status;
61 rc = RTProcWait(pid, 0, &Status);
62 if ( RT_SUCCESS(rc)
63 && Status.iStatus == 0
64 && Status.enmReason == RTPROCEXITREASON_NORMAL)
65 return VINF_SUCCESS;
66 }
67 else
68 LogRel(("NetIfAdpCtl: failed to create process for %.\n",
69 szAdpCtl));
70 return rc;
71}
72
73static int NetIfAdpCtl(HostNetworkInterface * pIf, const char *pszAddr, const char *pszOption, const char *pszMask)
74{
75 Bstr interfaceName;
76 pIf->COMGETTER(Name)(interfaceName.asOutParam());
77 Utf8Str strName(interfaceName);
78 return NetIfAdpCtl(strName.c_str(), pszAddr, pszOption, pszMask);
79}
80
81int NetIfEnableStaticIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * pIf, ULONG aOldIp, ULONG aNewIp, ULONG aMask)
82{
83 const char *pszOption, *pszMask;
84 char szAddress[16]; /* 4*3 + 3*1 + 1 */
85 char szNetMask[16]; /* 4*3 + 3*1 + 1 */
86 uint8_t *pu8Addr = (uint8_t *)&aNewIp;
87 uint8_t *pu8Mask = (uint8_t *)&aMask;
88 if (aNewIp == 0)
89 {
90 pu8Addr = (uint8_t *)&aOldIp;
91 pszOption = "remove";
92 pszMask = NULL;
93 }
94 else
95 {
96 pszOption = "netmask";
97 pszMask = szNetMask;
98 RTStrPrintf(szNetMask, sizeof(szNetMask), "%d.%d.%d.%d",
99 pu8Mask[0], pu8Mask[1], pu8Mask[2], pu8Mask[3]);
100 }
101 RTStrPrintf(szAddress, sizeof(szAddress), "%d.%d.%d.%d",
102 pu8Addr[0], pu8Addr[1], pu8Addr[2], pu8Addr[3]);
103 return NetIfAdpCtl(pIf, szAddress, pszOption, pszMask);
104}
105
106int NetIfEnableStaticIpConfigV6(VirtualBox * /* vBox */, HostNetworkInterface * pIf, IN_BSTR aOldIPV6Address, IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
107{
108 char szAddress[5*8 + 1 + 5 + 1];
109 if (Bstr(aIPV6Address).length())
110 {
111 RTStrPrintf(szAddress, sizeof(szAddress), "%ls/%d",
112 aIPV6Address, aIPV6MaskPrefixLength);
113 return NetIfAdpCtl(pIf, szAddress, NULL, NULL);
114 }
115 else
116 {
117 RTStrPrintf(szAddress, sizeof(szAddress), "%ls",
118 aOldIPV6Address);
119 return NetIfAdpCtl(pIf, szAddress, "remove", NULL);
120 }
121}
122
123int NetIfEnableDynamicIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * /* pIf */)
124{
125 return VERR_NOT_IMPLEMENTED;
126}
127
128
129int NetIfCreateHostOnlyNetworkInterface(VirtualBox *pVBox,
130 IHostNetworkInterface **aHostNetworkInterface,
131 IProgress **aProgress,
132 const char *pcszName)
133{
134#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
135 /* create a progress object */
136 ComObjPtr<Progress> progress;
137 progress.createObject();
138
139 ComPtr<IHost> host;
140 HRESULT hrc = pVBox->COMGETTER(Host)(host.asOutParam());
141 if (SUCCEEDED(hrc))
142 {
143 hrc = progress->init(pVBox, host,
144 Bstr("Creating host only network interface").raw(),
145 FALSE /* aCancelable */);
146 if (SUCCEEDED(hrc))
147 {
148 progress.queryInterfaceTo(aProgress);
149
150 char szAdpCtl[RTPATH_MAX];
151 int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " add"));
152 if (RT_FAILURE(rc))
153 {
154 progress->notifyComplete(E_FAIL,
155 COM_IIDOF(IHostNetworkInterface),
156 HostNetworkInterface::getStaticComponentName(),
157 "Failed to get program path, rc=%Rrc\n", rc);
158 return rc;
159 }
160 strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " ");
161 if (pcszName && strlen(pcszName) <= RTPATH_MAX - strlen(szAdpCtl) - sizeof(" add"))
162 {
163 strcat(szAdpCtl, pcszName);
164 strcat(szAdpCtl, " add");
165 }
166 else
167 strcat(szAdpCtl, "add");
168 FILE *fp = popen(szAdpCtl, "r");
169
170 if (fp)
171 {
172 char szBuf[VBOXNET_MAX_SHORT_NAME];
173 if (fgets(szBuf, sizeof(szBuf), fp))
174 {
175 char *pLast = szBuf + strlen(szBuf) - 1;
176 if (pLast >= szBuf && *pLast == '\n')
177 *pLast = 0;
178
179 size_t cbNameLen = strlen(szBuf) + 1;
180 PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
181 if (!pInfo)
182 rc = VERR_NO_MEMORY;
183 else
184 {
185 strcpy(pInfo->szShortName, szBuf);
186 strcpy(pInfo->szName, szBuf);
187 rc = NetIfGetConfigByName(pInfo);
188 if (RT_FAILURE(rc))
189 {
190 progress->notifyComplete(E_FAIL,
191 COM_IIDOF(IHostNetworkInterface),
192 HostNetworkInterface::getStaticComponentName(),
193 "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add')\n", szBuf);
194 }
195 else
196 {
197 Bstr IfName(szBuf);
198 /* create a new uninitialized host interface object */
199 ComObjPtr<HostNetworkInterface> iface;
200 iface.createObject();
201 iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
202 iface->setVirtualBox(pVBox);
203 iface.queryInterfaceTo(aHostNetworkInterface);
204 }
205 RTMemFree(pInfo);
206 }
207 if ((rc = pclose(fp)) != 0)
208 {
209 progress->notifyComplete(E_FAIL,
210 COM_IIDOF(IHostNetworkInterface),
211 HostNetworkInterface::getStaticComponentName(),
212 "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
213 rc = VERR_INTERNAL_ERROR;
214 }
215 }
216 else
217 {
218 /* Failed to add an interface */
219 rc = VERR_PERMISSION_DENIED;
220 progress->notifyComplete(E_FAIL,
221 COM_IIDOF(IHostNetworkInterface),
222 HostNetworkInterface::getStaticComponentName(),
223 "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d). Check permissions!", rc);
224 pclose(fp);
225 }
226 }
227 if (RT_SUCCESS(rc))
228 progress->notifyComplete(rc);
229 else
230 hrc = E_FAIL;
231 }
232 }
233
234 return hrc;
235
236#else
237 NOREF(pVBox);
238 NOREF(aHostNetworkInterface);
239 NOREF(aProgress);
240 return VERR_NOT_IMPLEMENTED;
241#endif
242}
243
244int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVBox, IN_GUID aId,
245 IProgress **aProgress)
246{
247#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
248 /* create a progress object */
249 ComObjPtr<Progress> progress;
250 progress.createObject();
251 ComPtr<IHost> host;
252 int rc = VINF_SUCCESS;
253 HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
254 if(SUCCEEDED(hr))
255 {
256 Bstr ifname;
257 ComPtr<IHostNetworkInterface> iface;
258 if (FAILED(host->FindHostNetworkInterfaceById(Guid(aId).toUtf16().raw(), iface.asOutParam())))
259 return VERR_INVALID_PARAMETER;
260 iface->COMGETTER(Name)(ifname.asOutParam());
261 if (ifname.isEmpty())
262 return VERR_INTERNAL_ERROR;
263
264 rc = progress->init(pVBox, host,
265 Bstr("Removing host network interface").raw(),
266 FALSE /* aCancelable */);
267 if(SUCCEEDED(rc))
268 {
269 progress.queryInterfaceTo(aProgress);
270 rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
271 if (RT_FAILURE(rc))
272 progress->notifyComplete(E_FAIL,
273 COM_IIDOF(IHostNetworkInterface),
274 HostNetworkInterface::getStaticComponentName(),
275 "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
276 else
277 progress->notifyComplete(S_OK);
278 }
279 }
280 else
281 {
282 progress->notifyComplete(hr);
283 rc = VERR_INTERNAL_ERROR;
284 }
285 return rc;
286#else
287 NOREF(pVBox);
288 NOREF(aId);
289 NOREF(aProgress);
290 return VERR_NOT_IMPLEMENTED;
291#endif
292}
293
294int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
295{
296 return VERR_NOT_IMPLEMENTED;
297}
298
299int NetIfDhcpRediscover(VirtualBox * /* pVbox */, HostNetworkInterface * /* pIf */)
300{
301 return VERR_NOT_IMPLEMENTED;
302}
303
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