VirtualBox

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

Last change on this file since 26603 was 26603, checked in by vboxsync, 15 years ago

Main: back out r57775

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