VirtualBox

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

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

remove changes that break up log and error strings, introduced in 92972

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1/* $Id: NetIf-generic.cpp 50899 2014-03-26 18:08:27Z 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 '" VBOXNETADPCTL_NAME " add')\n", szBuf);
261 }
262 else
263 {
264 Bstr IfName(szBuf);
265 /* create a new uninitialized host interface object */
266 ComObjPtr<HostNetworkInterface> iface;
267 iface.createObject();
268 iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
269 iface->i_setVirtualBox(pVirtualBox);
270 iface.queryInterfaceTo(aHostNetworkInterface);
271 }
272 RTMemFree(pInfo);
273 }
274 if ((rc = pclose(fp)) != 0)
275 {
276 progress->i_notifyComplete(E_FAIL,
277 COM_IIDOF(IHostNetworkInterface),
278 HostNetworkInterface::getStaticComponentName(),
279 "Failed to execute '" VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
280 rc = VERR_INTERNAL_ERROR;
281 }
282 }
283 else
284 {
285 /* Failed to add an interface */
286 rc = VERR_PERMISSION_DENIED;
287 progress->i_notifyComplete(E_FAIL,
288 COM_IIDOF(IHostNetworkInterface),
289 HostNetworkInterface::getStaticComponentName(),
290 "Failed to execute '" VBOXNETADPCTL_NAME " add' (exit status: %d). Check permissions!", rc);
291 pclose(fp);
292 }
293 }
294 if (RT_SUCCESS(rc))
295 progress->i_notifyComplete(rc);
296 else
297 hrc = E_FAIL;
298 }
299 }
300
301 return hrc;
302
303#else
304 NOREF(pVirtualBox);
305 NOREF(aHostNetworkInterface);
306 NOREF(aProgress);
307 NOREF(pcszName);
308 return VERR_NOT_IMPLEMENTED;
309#endif
310}
311
312int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVirtualBox, IN_GUID aId,
313 IProgress **aProgress)
314{
315#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
316 /* create a progress object */
317 ComObjPtr<Progress> progress;
318 progress.createObject();
319 ComPtr<IHost> host;
320 int rc = VINF_SUCCESS;
321 HRESULT hr = pVirtualBox->COMGETTER(Host)(host.asOutParam());
322 if (SUCCEEDED(hr))
323 {
324 Bstr ifname;
325 ComPtr<IHostNetworkInterface> iface;
326 if (FAILED(host->FindHostNetworkInterfaceById(Guid(aId).toUtf16().raw(), iface.asOutParam())))
327 return VERR_INVALID_PARAMETER;
328 iface->COMGETTER(Name)(ifname.asOutParam());
329 if (ifname.isEmpty())
330 return VERR_INTERNAL_ERROR;
331
332 rc = progress->init(pVirtualBox, host,
333 Bstr("Removing host network interface").raw(),
334 FALSE /* aCancelable */);
335 if (SUCCEEDED(rc))
336 {
337 progress.queryInterfaceTo(aProgress);
338 rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
339 if (RT_FAILURE(rc))
340 progress->i_notifyComplete(E_FAIL,
341 COM_IIDOF(IHostNetworkInterface),
342 HostNetworkInterface::getStaticComponentName(),
343 "Failed to execute '" VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
344 else
345 progress->i_notifyComplete(S_OK);
346 }
347 }
348 else
349 {
350 progress->i_notifyComplete(hr);
351 rc = VERR_INTERNAL_ERROR;
352 }
353 return rc;
354#else
355 NOREF(pVirtualBox);
356 NOREF(aId);
357 NOREF(aProgress);
358 return VERR_NOT_IMPLEMENTED;
359#endif
360}
361
362int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
363{
364 return VERR_NOT_IMPLEMENTED;
365}
366
367int NetIfDhcpRediscover(VirtualBox * /* pVBox */, HostNetworkInterface * /* pIf */)
368{
369 return VERR_NOT_IMPLEMENTED;
370}
371
372/**
373 * Obtain the current state of the interface.
374 *
375 * @returns VBox status code.
376 *
377 * @param pcszIfName Interface name.
378 * @param penmState Where to store the retrieved state.
379 */
380int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState)
381{
382 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
383 if (sock < 0)
384 return VERR_OUT_OF_RESOURCES;
385 struct ifreq Req;
386 RT_ZERO(Req);
387 RTStrCopy(Req.ifr_name, sizeof(Req.ifr_name), pcszIfName);
388 if (ioctl(sock, SIOCGIFFLAGS, &Req) < 0)
389 {
390 Log(("NetIfGetState: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
391 *penmState = NETIF_S_UNKNOWN;
392 }
393 else
394 *penmState = (Req.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
395 close(sock);
396 return VINF_SUCCESS;
397}
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