VirtualBox

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

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

6813 src-all/ProgressImp.cpp + some formatting/line length sorting

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1/* $Id: NetIf-generic.cpp 50874 2014-03-25 18:29:02Z 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,
160 IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
161{
162 char szAddress[5*8 + 1 + 5 + 1];
163 if (Bstr(aIPV6Address).length())
164 {
165 RTStrPrintf(szAddress, sizeof(szAddress), "%ls/%d",
166 aIPV6Address, aIPV6MaskPrefixLength);
167 return NetIfAdpCtl(pIf, szAddress, NULL, NULL);
168 }
169 else
170 {
171 RTStrPrintf(szAddress, sizeof(szAddress), "%ls",
172 aOldIPV6Address);
173 return NetIfAdpCtl(pIf, szAddress, "remove", NULL);
174 }
175}
176
177int NetIfEnableDynamicIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * /* pIf */)
178{
179 return VERR_NOT_IMPLEMENTED;
180}
181
182
183int NetIfCreateHostOnlyNetworkInterface(VirtualBox *pVirtualBox,
184 IHostNetworkInterface **aHostNetworkInterface,
185 IProgress **aProgress,
186 const char *pcszName)
187{
188#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
189 /* create a progress object */
190 ComObjPtr<Progress> progress;
191 progress.createObject();
192
193 ComPtr<IHost> host;
194 HRESULT hrc = pVirtualBox->COMGETTER(Host)(host.asOutParam());
195 if (SUCCEEDED(hrc))
196 {
197 hrc = progress->init(pVirtualBox, host,
198 Bstr("Creating host only network interface").raw(),
199 FALSE /* aCancelable */);
200 if (SUCCEEDED(hrc))
201 {
202 progress.queryInterfaceTo(aProgress);
203
204 char szAdpCtl[RTPATH_MAX];
205 int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " add"));
206 if (RT_FAILURE(rc))
207 {
208 progress->i_notifyComplete(E_FAIL,
209 COM_IIDOF(IHostNetworkInterface),
210 HostNetworkInterface::getStaticComponentName(),
211 "Failed to get program path, rc=%Rrc\n", rc);
212 return rc;
213 }
214 strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " ");
215 if (pcszName && strlen(pcszName) <= RTPATH_MAX - strlen(szAdpCtl) - sizeof(" add"))
216 {
217 strcat(szAdpCtl, pcszName);
218 strcat(szAdpCtl, " add");
219 }
220 else
221 strcat(szAdpCtl, "add");
222 if (strlen(szAdpCtl) < RTPATH_MAX - sizeof(" 2>&1"))
223 strcat(szAdpCtl, " 2>&1");
224 FILE *fp = popen(szAdpCtl, "r");
225
226 if (fp)
227 {
228 char szBuf[128]; /* We are not interested in long error messages. */
229 if (fgets(szBuf, sizeof(szBuf), fp))
230 {
231 /* Remove trailing new line characters. */
232 char *pLast = szBuf + strlen(szBuf) - 1;
233 if (pLast >= szBuf && *pLast == '\n')
234 *pLast = 0;
235
236 if (!strncmp(VBOXNETADPCTL_NAME ":", szBuf, sizeof(VBOXNETADPCTL_NAME)))
237 {
238 progress->i_notifyComplete(E_FAIL,
239 COM_IIDOF(IHostNetworkInterface),
240 HostNetworkInterface::getStaticComponentName(),
241 "%s", szBuf);
242 pclose(fp);
243 return E_FAIL;
244 }
245
246 size_t cbNameLen = strlen(szBuf) + 1;
247 PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
248 if (!pInfo)
249 rc = VERR_NO_MEMORY;
250 else
251 {
252 strcpy(pInfo->szShortName, szBuf);
253 strcpy(pInfo->szName, szBuf);
254 rc = NetIfGetConfigByName(pInfo);
255 if (RT_FAILURE(rc))
256 {
257 progress->i_notifyComplete(E_FAIL,
258 COM_IIDOF(IHostNetworkInterface),
259 HostNetworkInterface::getStaticComponentName(),
260 "Failed to get config info for %s (as reported by '"
261 VBOXNETADPCTL_NAME " add')\n", szBuf);
262 }
263 else
264 {
265 Bstr IfName(szBuf);
266 /* create a new uninitialized host interface object */
267 ComObjPtr<HostNetworkInterface> iface;
268 iface.createObject();
269 iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
270 iface->i_setVirtualBox(pVirtualBox);
271 iface.queryInterfaceTo(aHostNetworkInterface);
272 }
273 RTMemFree(pInfo);
274 }
275 if ((rc = pclose(fp)) != 0)
276 {
277 progress->i_notifyComplete(E_FAIL,
278 COM_IIDOF(IHostNetworkInterface),
279 HostNetworkInterface::getStaticComponentName(),
280 "Failed to execute '" VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
281 rc = VERR_INTERNAL_ERROR;
282 }
283 }
284 else
285 {
286 /* Failed to add an interface */
287 rc = VERR_PERMISSION_DENIED;
288 progress->i_notifyComplete(E_FAIL,
289 COM_IIDOF(IHostNetworkInterface),
290 HostNetworkInterface::getStaticComponentName(),
291 "Failed to execute '" VBOXNETADPCTL_NAME
292 " add' (exit status: %d). Check permissions!", rc);
293 pclose(fp);
294 }
295 }
296 if (RT_SUCCESS(rc))
297 progress->i_notifyComplete(rc);
298 else
299 hrc = E_FAIL;
300 }
301 }
302
303 return hrc;
304
305#else
306 NOREF(pVirtualBox);
307 NOREF(aHostNetworkInterface);
308 NOREF(aProgress);
309 NOREF(pcszName);
310 return VERR_NOT_IMPLEMENTED;
311#endif
312}
313
314int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVirtualBox, IN_GUID aId,
315 IProgress **aProgress)
316{
317#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
318 /* create a progress object */
319 ComObjPtr<Progress> progress;
320 progress.createObject();
321 ComPtr<IHost> host;
322 int rc = VINF_SUCCESS;
323 HRESULT hr = pVirtualBox->COMGETTER(Host)(host.asOutParam());
324 if (SUCCEEDED(hr))
325 {
326 Bstr ifname;
327 ComPtr<IHostNetworkInterface> iface;
328 if (FAILED(host->FindHostNetworkInterfaceById(Guid(aId).toUtf16().raw(), iface.asOutParam())))
329 return VERR_INVALID_PARAMETER;
330 iface->COMGETTER(Name)(ifname.asOutParam());
331 if (ifname.isEmpty())
332 return VERR_INTERNAL_ERROR;
333
334 rc = progress->init(pVirtualBox, host,
335 Bstr("Removing host network interface").raw(),
336 FALSE /* aCancelable */);
337 if (SUCCEEDED(rc))
338 {
339 progress.queryInterfaceTo(aProgress);
340 rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
341 if (RT_FAILURE(rc))
342 progress->i_notifyComplete(E_FAIL,
343 COM_IIDOF(IHostNetworkInterface),
344 HostNetworkInterface::getStaticComponentName(),
345 "Failed to execute '" VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
346 else
347 progress->i_notifyComplete(S_OK);
348 }
349 }
350 else
351 {
352 progress->i_notifyComplete(hr);
353 rc = VERR_INTERNAL_ERROR;
354 }
355 return rc;
356#else
357 NOREF(pVirtualBox);
358 NOREF(aId);
359 NOREF(aProgress);
360 return VERR_NOT_IMPLEMENTED;
361#endif
362}
363
364int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
365{
366 return VERR_NOT_IMPLEMENTED;
367}
368
369int NetIfDhcpRediscover(VirtualBox * /* pVBox */, HostNetworkInterface * /* pIf */)
370{
371 return VERR_NOT_IMPLEMENTED;
372}
373
374/**
375 * Obtain the current state of the interface.
376 *
377 * @returns VBox status code.
378 *
379 * @param pcszIfName Interface name.
380 * @param penmState Where to store the retrieved state.
381 */
382int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState)
383{
384 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
385 if (sock < 0)
386 return VERR_OUT_OF_RESOURCES;
387 struct ifreq Req;
388 RT_ZERO(Req);
389 RTStrCopy(Req.ifr_name, sizeof(Req.ifr_name), pcszIfName);
390 if (ioctl(sock, SIOCGIFFLAGS, &Req) < 0)
391 {
392 Log(("NetIfGetState: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
393 *penmState = NETIF_S_UNKNOWN;
394 }
395 else
396 *penmState = (Req.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
397 close(sock);
398 return VINF_SUCCESS;
399}
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