VirtualBox

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

Last change on this file since 47117 was 47117, checked in by vboxsync, 12 years ago

Main: RT_ZERO() / RTStrCopy()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1/* $Id: NetIf-generic.cpp 47117 2013-07-12 12:48:17Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Generic NetIf implementation.
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
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#include <sys/ioctl.h>
25#include <netinet/in.h>
26#include <net/if.h>
27#include <errno.h>
28#include <unistd.h>
29
30#if defined(RT_OS_SOLARIS)
31# include <sys/sockio.h>
32#endif
33
34#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
35# include <cstdio>
36#endif
37
38#include "HostNetworkInterfaceImpl.h"
39#include "ProgressImpl.h"
40#include "VirtualBoxImpl.h"
41#include "netif.h"
42
43#define VBOXNETADPCTL_NAME "VBoxNetAdpCtl"
44
45static int NetIfAdpCtl(const char * pcszIfName, const char *pszAddr, const char *pszOption, const char *pszMask)
46{
47 const char *args[] = { NULL, pcszIfName, pszAddr, pszOption, pszMask, NULL };
48
49 char szAdpCtl[RTPATH_MAX];
50 int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME));
51 if (RT_FAILURE(rc))
52 {
53 LogRel(("NetIfAdpCtl: failed to get program path, rc=%Rrc.\n", rc));
54 return rc;
55 }
56 strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME);
57 args[0] = szAdpCtl;
58 if (!RTPathExists(szAdpCtl))
59 {
60 LogRel(("NetIfAdpCtl: path %s does not exist. Failed to run " VBOXNETADPCTL_NAME " helper.\n",
61 szAdpCtl));
62 return VERR_FILE_NOT_FOUND;
63 }
64
65 RTPROCESS pid;
66 rc = RTProcCreate(szAdpCtl, args, RTENV_DEFAULT, 0, &pid);
67 if (RT_SUCCESS(rc))
68 {
69 RTPROCSTATUS Status;
70 rc = RTProcWait(pid, 0, &Status);
71 if ( RT_SUCCESS(rc)
72 && Status.iStatus == 0
73 && Status.enmReason == RTPROCEXITREASON_NORMAL)
74 return VINF_SUCCESS;
75 }
76 else
77 LogRel(("NetIfAdpCtl: failed to create process for %.\n",
78 szAdpCtl));
79 return rc;
80}
81
82static int NetIfAdpCtl(HostNetworkInterface * pIf, const char *pszAddr, const char *pszOption, const char *pszMask)
83{
84 Bstr interfaceName;
85 pIf->COMGETTER(Name)(interfaceName.asOutParam());
86 Utf8Str strName(interfaceName);
87 return NetIfAdpCtl(strName.c_str(), pszAddr, pszOption, pszMask);
88}
89
90int NetIfAdpCtlOut(const char * pcszName, const char * pcszCmd, char *pszBuffer, size_t cBufSize)
91{
92 char szAdpCtl[RTPATH_MAX];
93 int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " ") - strlen(pcszCmd));
94 if (RT_FAILURE(rc))
95 {
96 LogRel(("NetIfAdpCtlOut: Failed to get program path, rc=%Rrc\n", rc));
97 return VERR_INVALID_PARAMETER;
98 }
99 strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " ");
100 if (pcszName && strlen(pcszName) <= RTPATH_MAX - strlen(szAdpCtl) - 1 - strlen(pcszCmd))
101 {
102 strcat(szAdpCtl, pcszName);
103 strcat(szAdpCtl, " ");
104 strcat(szAdpCtl, pcszCmd);
105 }
106 else
107 {
108 LogRel(("NetIfAdpCtlOut: Command line is too long: %s%s %s\n", szAdpCtl, pcszName, pcszCmd));
109 return VERR_INVALID_PARAMETER;
110 }
111 if (strlen(szAdpCtl) < RTPATH_MAX - sizeof(" 2>&1"))
112 strcat(szAdpCtl, " 2>&1");
113 FILE *fp = popen(szAdpCtl, "r");
114 if (fp)
115 {
116 if (fgets(pszBuffer, cBufSize, fp))
117 {
118 if (!strncmp(VBOXNETADPCTL_NAME ":", pszBuffer, sizeof(VBOXNETADPCTL_NAME)))
119 {
120 LogRel(("NetIfAdpCtlOut: %s", pszBuffer));
121 rc = VERR_INTERNAL_ERROR;
122 }
123 }
124 else
125 {
126 LogRel(("NetIfAdpCtlOut: No output from " VBOXNETADPCTL_NAME));
127 rc = VERR_INTERNAL_ERROR;
128 }
129 pclose(fp);
130 }
131 return rc;
132}
133
134int NetIfEnableStaticIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * pIf, ULONG aOldIp, ULONG aNewIp, ULONG aMask)
135{
136 const char *pszOption, *pszMask;
137 char szAddress[16]; /* 4*3 + 3*1 + 1 */
138 char szNetMask[16]; /* 4*3 + 3*1 + 1 */
139 uint8_t *pu8Addr = (uint8_t *)&aNewIp;
140 uint8_t *pu8Mask = (uint8_t *)&aMask;
141 if (aNewIp == 0)
142 {
143 pu8Addr = (uint8_t *)&aOldIp;
144 pszOption = "remove";
145 pszMask = NULL;
146 }
147 else
148 {
149 pszOption = "netmask";
150 pszMask = szNetMask;
151 RTStrPrintf(szNetMask, sizeof(szNetMask), "%d.%d.%d.%d",
152 pu8Mask[0], pu8Mask[1], pu8Mask[2], pu8Mask[3]);
153 }
154 RTStrPrintf(szAddress, sizeof(szAddress), "%d.%d.%d.%d",
155 pu8Addr[0], pu8Addr[1], pu8Addr[2], pu8Addr[3]);
156 return NetIfAdpCtl(pIf, szAddress, pszOption, pszMask);
157}
158
159int NetIfEnableStaticIpConfigV6(VirtualBox * /* vBox */, HostNetworkInterface * pIf, IN_BSTR aOldIPV6Address, IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
160{
161 char szAddress[5*8 + 1 + 5 + 1];
162 if (Bstr(aIPV6Address).length())
163 {
164 RTStrPrintf(szAddress, sizeof(szAddress), "%ls/%d",
165 aIPV6Address, aIPV6MaskPrefixLength);
166 return NetIfAdpCtl(pIf, szAddress, NULL, NULL);
167 }
168 else
169 {
170 RTStrPrintf(szAddress, sizeof(szAddress), "%ls",
171 aOldIPV6Address);
172 return NetIfAdpCtl(pIf, szAddress, "remove", NULL);
173 }
174}
175
176int NetIfEnableDynamicIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * /* pIf */)
177{
178 return VERR_NOT_IMPLEMENTED;
179}
180
181
182int NetIfCreateHostOnlyNetworkInterface(VirtualBox *pVBox,
183 IHostNetworkInterface **aHostNetworkInterface,
184 IProgress **aProgress,
185 const char *pcszName)
186{
187#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
188 /* create a progress object */
189 ComObjPtr<Progress> progress;
190 progress.createObject();
191
192 ComPtr<IHost> host;
193 HRESULT hrc = pVBox->COMGETTER(Host)(host.asOutParam());
194 if (SUCCEEDED(hrc))
195 {
196 hrc = progress->init(pVBox, host,
197 Bstr("Creating host only network interface").raw(),
198 FALSE /* aCancelable */);
199 if (SUCCEEDED(hrc))
200 {
201 progress.queryInterfaceTo(aProgress);
202
203 char szAdpCtl[RTPATH_MAX];
204 int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " add"));
205 if (RT_FAILURE(rc))
206 {
207 progress->notifyComplete(E_FAIL,
208 COM_IIDOF(IHostNetworkInterface),
209 HostNetworkInterface::getStaticComponentName(),
210 "Failed to get program path, rc=%Rrc\n", rc);
211 return rc;
212 }
213 strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " ");
214 if (pcszName && strlen(pcszName) <= RTPATH_MAX - strlen(szAdpCtl) - sizeof(" add"))
215 {
216 strcat(szAdpCtl, pcszName);
217 strcat(szAdpCtl, " add");
218 }
219 else
220 strcat(szAdpCtl, "add");
221 if (strlen(szAdpCtl) < RTPATH_MAX - sizeof(" 2>&1"))
222 strcat(szAdpCtl, " 2>&1");
223 FILE *fp = popen(szAdpCtl, "r");
224
225 if (fp)
226 {
227 char szBuf[128]; /* We are not interested in long error messages. */
228 if (fgets(szBuf, sizeof(szBuf), fp))
229 {
230 if (!strncmp(VBOXNETADPCTL_NAME ":", szBuf, sizeof(VBOXNETADPCTL_NAME)))
231 {
232 progress->notifyComplete(E_FAIL,
233 COM_IIDOF(IHostNetworkInterface),
234 HostNetworkInterface::getStaticComponentName(),
235 "%s", szBuf);
236 pclose(fp);
237 return E_FAIL;
238 }
239 char *pLast = szBuf + strlen(szBuf) - 1;
240 if (pLast >= szBuf && *pLast == '\n')
241 *pLast = 0;
242
243 size_t cbNameLen = strlen(szBuf) + 1;
244 PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
245 if (!pInfo)
246 rc = VERR_NO_MEMORY;
247 else
248 {
249 strcpy(pInfo->szShortName, szBuf);
250 strcpy(pInfo->szName, szBuf);
251 rc = NetIfGetConfigByName(pInfo);
252 if (RT_FAILURE(rc))
253 {
254 progress->notifyComplete(E_FAIL,
255 COM_IIDOF(IHostNetworkInterface),
256 HostNetworkInterface::getStaticComponentName(),
257 "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add')\n", szBuf);
258 }
259 else
260 {
261 Bstr IfName(szBuf);
262 /* create a new uninitialized host interface object */
263 ComObjPtr<HostNetworkInterface> iface;
264 iface.createObject();
265 iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
266 iface->setVirtualBox(pVBox);
267 iface.queryInterfaceTo(aHostNetworkInterface);
268 }
269 RTMemFree(pInfo);
270 }
271 if ((rc = pclose(fp)) != 0)
272 {
273 progress->notifyComplete(E_FAIL,
274 COM_IIDOF(IHostNetworkInterface),
275 HostNetworkInterface::getStaticComponentName(),
276 "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
277 rc = VERR_INTERNAL_ERROR;
278 }
279 }
280 else
281 {
282 /* Failed to add an interface */
283 rc = VERR_PERMISSION_DENIED;
284 progress->notifyComplete(E_FAIL,
285 COM_IIDOF(IHostNetworkInterface),
286 HostNetworkInterface::getStaticComponentName(),
287 "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d). Check permissions!", rc);
288 pclose(fp);
289 }
290 }
291 if (RT_SUCCESS(rc))
292 progress->notifyComplete(rc);
293 else
294 hrc = E_FAIL;
295 }
296 }
297
298 return hrc;
299
300#else
301 NOREF(pVBox);
302 NOREF(aHostNetworkInterface);
303 NOREF(aProgress);
304 NOREF(pcszName);
305 return VERR_NOT_IMPLEMENTED;
306#endif
307}
308
309int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVBox, IN_GUID aId,
310 IProgress **aProgress)
311{
312#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
313 /* create a progress object */
314 ComObjPtr<Progress> progress;
315 progress.createObject();
316 ComPtr<IHost> host;
317 int rc = VINF_SUCCESS;
318 HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
319 if (SUCCEEDED(hr))
320 {
321 Bstr ifname;
322 ComPtr<IHostNetworkInterface> iface;
323 if (FAILED(host->FindHostNetworkInterfaceById(Guid(aId).toUtf16().raw(), iface.asOutParam())))
324 return VERR_INVALID_PARAMETER;
325 iface->COMGETTER(Name)(ifname.asOutParam());
326 if (ifname.isEmpty())
327 return VERR_INTERNAL_ERROR;
328
329 rc = progress->init(pVBox, host,
330 Bstr("Removing host network interface").raw(),
331 FALSE /* aCancelable */);
332 if (SUCCEEDED(rc))
333 {
334 progress.queryInterfaceTo(aProgress);
335 rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
336 if (RT_FAILURE(rc))
337 progress->notifyComplete(E_FAIL,
338 COM_IIDOF(IHostNetworkInterface),
339 HostNetworkInterface::getStaticComponentName(),
340 "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
341 else
342 progress->notifyComplete(S_OK);
343 }
344 }
345 else
346 {
347 progress->notifyComplete(hr);
348 rc = VERR_INTERNAL_ERROR;
349 }
350 return rc;
351#else
352 NOREF(pVBox);
353 NOREF(aId);
354 NOREF(aProgress);
355 return VERR_NOT_IMPLEMENTED;
356#endif
357}
358
359int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
360{
361 return VERR_NOT_IMPLEMENTED;
362}
363
364int NetIfDhcpRediscover(VirtualBox * /* pVbox */, HostNetworkInterface * /* pIf */)
365{
366 return VERR_NOT_IMPLEMENTED;
367}
368
369/**
370 * Obtain the current state of the interface.
371 *
372 * @returns VBox status code.
373 *
374 * @param pcszIfName Interface name.
375 * @param penmState Where to store the retrieved state.
376 */
377int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState)
378{
379 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
380 if (sock < 0)
381 return VERR_OUT_OF_RESOURCES;
382 struct ifreq Req;
383 RT_ZERO(Req);
384 RTStrCopy(Req.ifr_name, sizeof(Req.ifr_name), pcszIfName);
385 if (ioctl(sock, SIOCGIFFLAGS, &Req) < 0)
386 {
387 Log(("NetIfGetState: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
388 *penmState = NETIF_S_UNKNOWN;
389 }
390 else
391 *penmState = (Req.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
392 close(sock);
393 return VINF_SUCCESS;
394}
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