VirtualBox

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

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

Main/NetIf-generic: fix warnings.

  • 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 25088 2009-11-30 09:01:38Z 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) || 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=%Vrc\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 CheckComRCReturnRC(rc);
245 progress.queryInterfaceTo(aProgress);
246 rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
247 if (RT_FAILURE(rc))
248 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(), "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
249 else
250 progress->notifyComplete(S_OK);
251 }
252 }
253 else
254 {
255 progress->notifyComplete(hr);
256 rc = VERR_INTERNAL_ERROR;
257 }
258 return rc;
259#else
260 NOREF(pVBox);
261 NOREF(aId);
262 NOREF(aProgress);
263 return VERR_NOT_IMPLEMENTED;
264#endif
265}
266
267int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
268{
269 return VERR_NOT_IMPLEMENTED;
270}
271
272int NetIfDhcpRediscover(VirtualBox * /* pVbox */, HostNetworkInterface * /* pIf */)
273{
274 return VERR_NOT_IMPLEMENTED;
275}
276
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