VirtualBox

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

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

Main/Network: fixed descriptor leak introduced in r77794 (#6182, #6183)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1/* $Id: NetIf-generic.cpp 41290 2012-05-14 18:07:13Z 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.215389.xyz. 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 if (strlen(szAdpCtl) < RTPATH_MAX - sizeof(" 2>&1"))
169 strcat(szAdpCtl, " 2>&1");
170 FILE *fp = popen(szAdpCtl, "r");
171
172 if (fp)
173 {
174 char szBuf[128]; /* We are not interested in long error messages. */
175 if (fgets(szBuf, sizeof(szBuf), fp))
176 {
177 if (!strncmp(VBOXNETADPCTL_NAME ":", szBuf, sizeof(VBOXNETADPCTL_NAME)))
178 {
179 progress->notifyComplete(E_FAIL,
180 COM_IIDOF(IHostNetworkInterface),
181 HostNetworkInterface::getStaticComponentName(),
182 "%s", szBuf);
183 pclose(fp);
184 return E_FAIL;
185 }
186 char *pLast = szBuf + strlen(szBuf) - 1;
187 if (pLast >= szBuf && *pLast == '\n')
188 *pLast = 0;
189
190 size_t cbNameLen = strlen(szBuf) + 1;
191 PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
192 if (!pInfo)
193 rc = VERR_NO_MEMORY;
194 else
195 {
196 strcpy(pInfo->szShortName, szBuf);
197 strcpy(pInfo->szName, szBuf);
198 rc = NetIfGetConfigByName(pInfo);
199 if (RT_FAILURE(rc))
200 {
201 progress->notifyComplete(E_FAIL,
202 COM_IIDOF(IHostNetworkInterface),
203 HostNetworkInterface::getStaticComponentName(),
204 "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add')\n", szBuf);
205 }
206 else
207 {
208 Bstr IfName(szBuf);
209 /* create a new uninitialized host interface object */
210 ComObjPtr<HostNetworkInterface> iface;
211 iface.createObject();
212 iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
213 iface->setVirtualBox(pVBox);
214 iface.queryInterfaceTo(aHostNetworkInterface);
215 }
216 RTMemFree(pInfo);
217 }
218 if ((rc = pclose(fp)) != 0)
219 {
220 progress->notifyComplete(E_FAIL,
221 COM_IIDOF(IHostNetworkInterface),
222 HostNetworkInterface::getStaticComponentName(),
223 "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
224 rc = VERR_INTERNAL_ERROR;
225 }
226 }
227 else
228 {
229 /* Failed to add an interface */
230 rc = VERR_PERMISSION_DENIED;
231 progress->notifyComplete(E_FAIL,
232 COM_IIDOF(IHostNetworkInterface),
233 HostNetworkInterface::getStaticComponentName(),
234 "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d). Check permissions!", rc);
235 pclose(fp);
236 }
237 }
238 if (RT_SUCCESS(rc))
239 progress->notifyComplete(rc);
240 else
241 hrc = E_FAIL;
242 }
243 }
244
245 return hrc;
246
247#else
248 NOREF(pVBox);
249 NOREF(aHostNetworkInterface);
250 NOREF(aProgress);
251 return VERR_NOT_IMPLEMENTED;
252#endif
253}
254
255int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVBox, IN_GUID aId,
256 IProgress **aProgress)
257{
258#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
259 /* create a progress object */
260 ComObjPtr<Progress> progress;
261 progress.createObject();
262 ComPtr<IHost> host;
263 int rc = VINF_SUCCESS;
264 HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
265 if(SUCCEEDED(hr))
266 {
267 Bstr ifname;
268 ComPtr<IHostNetworkInterface> iface;
269 if (FAILED(host->FindHostNetworkInterfaceById(Guid(aId).toUtf16().raw(), iface.asOutParam())))
270 return VERR_INVALID_PARAMETER;
271 iface->COMGETTER(Name)(ifname.asOutParam());
272 if (ifname.isEmpty())
273 return VERR_INTERNAL_ERROR;
274
275 rc = progress->init(pVBox, host,
276 Bstr("Removing host network interface").raw(),
277 FALSE /* aCancelable */);
278 if(SUCCEEDED(rc))
279 {
280 progress.queryInterfaceTo(aProgress);
281 rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
282 if (RT_FAILURE(rc))
283 progress->notifyComplete(E_FAIL,
284 COM_IIDOF(IHostNetworkInterface),
285 HostNetworkInterface::getStaticComponentName(),
286 "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
287 else
288 progress->notifyComplete(S_OK);
289 }
290 }
291 else
292 {
293 progress->notifyComplete(hr);
294 rc = VERR_INTERNAL_ERROR;
295 }
296 return rc;
297#else
298 NOREF(pVBox);
299 NOREF(aId);
300 NOREF(aProgress);
301 return VERR_NOT_IMPLEMENTED;
302#endif
303}
304
305int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
306{
307 return VERR_NOT_IMPLEMENTED;
308}
309
310int NetIfDhcpRediscover(VirtualBox * /* pVbox */, HostNetworkInterface * /* pIf */)
311{
312 return VERR_NOT_IMPLEMENTED;
313}
314
Note: See TracBrowser for help on using the repository browser.

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