VirtualBox

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

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

HostImpl: Removed the return parameter from RemoveHostOnlyNetworkInterface()
and RemoveUSBDeviceFilter() methods so as to be consistent with Main API.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1/* $Id: NetIf-generic.cpp 22211 2009-08-12 16:05:19Z 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=%Vrc.\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)
136 /* create a progress object */
137 ComObjPtr<Progress> progress;
138 progress.createObject();
139
140 ComPtr<IHost> host;
141 HRESULT rc = pVBox->COMGETTER(Host)(host.asOutParam());
142 if(SUCCEEDED(rc))
143 {
144 rc = progress->init (pVBox, host,
145 Bstr ("Creating host only network interface"),
146 FALSE /* aCancelable */);
147 if(SUCCEEDED(rc))
148 {
149 CheckComRCReturnRC(rc);
150 progress.queryInterfaceTo(aProgress);
151
152 char szAdpCtl[RTPATH_MAX];
153 int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " add"));
154 if (RT_FAILURE(rc))
155 {
156 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(),
157 "Failed to get program path, rc=%Vrc\n", rc);
158 return rc;
159 }
160 strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " add");
161 FILE *fp = popen(szAdpCtl, "r");
162
163 if (fp)
164 {
165 char szBuf[VBOXNET_MAX_SHORT_NAME];
166 if (fgets(szBuf, sizeof(szBuf), fp))
167 {
168 char *pLast = szBuf + strlen(szBuf) - 1;
169 if (pLast >= szBuf && *pLast == '\n')
170 *pLast = 0;
171
172 size_t cbNameLen = strlen(szBuf) + 1;
173 PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
174 if (!pInfo)
175 rc = VERR_NO_MEMORY;
176 else
177 {
178 strcpy(pInfo->szShortName, szBuf);
179 strcpy(pInfo->szName, szBuf);
180 rc = NetIfGetConfigByName(pInfo);
181 if (RT_FAILURE(rc))
182 {
183 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(),
184 "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add')\n", szBuf);
185 }
186 else
187 {
188 Bstr IfName(szBuf);
189 /* create a new uninitialized host interface object */
190 ComObjPtr<HostNetworkInterface> iface;
191 iface.createObject();
192 iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
193 iface->setVirtualBox(pVBox);
194 iface.queryInterfaceTo(aHostNetworkInterface);
195 }
196 RTMemFree(pInfo);
197 }
198 }
199 if ((rc = pclose(fp)) != 0)
200 {
201 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(), "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
202 rc = VERR_INTERNAL_ERROR;
203 }
204 }
205 if (RT_SUCCESS(rc))
206 progress->notifyComplete(rc);
207 }
208 }
209
210 return rc;
211
212#else
213 return VERR_NOT_IMPLEMENTED;
214#endif
215}
216
217int NetIfRemoveHostOnlyNetworkInterface (VirtualBox *pVBox, IN_GUID aId,
218 IProgress **aProgress)
219{
220#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
221 /* create a progress object */
222 ComObjPtr<Progress> progress;
223 progress.createObject();
224 ComPtr<IHost> host;
225 int rc = VINF_SUCCESS;
226 HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
227 if(SUCCEEDED(hr))
228 {
229 Bstr ifname;
230 ComPtr<IHostNetworkInterface> iface;
231 if (FAILED (host->FindHostNetworkInterfaceById (Guid(aId).toUtf16(), iface.asOutParam())))
232 return VERR_INVALID_PARAMETER;
233 iface->COMGETTER (Name) (ifname.asOutParam());
234 if (ifname.isNull())
235 return VERR_INTERNAL_ERROR;
236
237 rc = progress->init (pVBox, host,
238 Bstr ("Removing host network interface"),
239 FALSE /* aCancelable */);
240 if(SUCCEEDED(rc))
241 {
242 CheckComRCReturnRC(rc);
243 progress.queryInterfaceTo(aProgress);
244 rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
245 if (RT_FAILURE(rc))
246 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(), "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
247 else
248 progress->notifyComplete(S_OK);
249 }
250 }
251 else
252 {
253 progress->notifyComplete(hr);
254 rc = VERR_INTERNAL_ERROR;
255 }
256 return rc;
257#else
258 return VERR_NOT_IMPLEMENTED;
259#endif
260}
261
262int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
263{
264 return VERR_NOT_IMPLEMENTED;
265}
266
267int NetIfDhcpRediscover(VirtualBox * /* pVbox */, HostNetworkInterface * /* pIf */)
268{
269 return VERR_NOT_IMPLEMENTED;
270}
271
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