VirtualBox

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

Last change on this file since 19242 was 19242, checked in by vboxsync, 16 years ago

Main: hopefully fixed Linux burns

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1/* $Id: NetIf-generic.cpp 19242 2009-04-28 14:10:45Z 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 = RTPathProgram(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, 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 = RTPathProgram(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 NETIFINFO Info;
173 Bstr IfName(szBuf);
174 rc = NetIfGetConfigByName(IfName, &Info);
175 if (RT_FAILURE(rc))
176 {
177 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(),
178 "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add').\n", szBuf);
179 }
180 else
181 {
182 /* create a new uninitialized host interface object */
183 ComObjPtr <HostNetworkInterface> iface;
184 iface.createObject();
185 iface->init(IfName, HostNetworkInterfaceType_HostOnly, &Info);
186 iface.queryInterfaceTo (aHostNetworkInterface);
187 }
188 }
189 if ((rc = pclose(fp)) != 0)
190 {
191 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(), "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d).", rc);
192 rc = VERR_INTERNAL_ERROR;
193 }
194 }
195 if (RT_SUCCESS(rc))
196 progress->notifyComplete(rc);
197 }
198 }
199
200 return rc;
201
202#else
203 return VERR_NOT_IMPLEMENTED;
204#endif
205}
206
207int NetIfRemoveHostOnlyNetworkInterface (VirtualBox *pVBox, IN_GUID aId, IHostNetworkInterface **aHostNetworkInterface, IProgress **aProgress)
208{
209#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
210 /* create a progress object */
211 ComObjPtr <Progress> progress;
212 progress.createObject();
213 ComPtr<IHost> host;
214 int rc = VINF_SUCCESS;
215 HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
216 if(SUCCEEDED(hr))
217 {
218 Bstr ifname;
219 ComPtr <IHostNetworkInterface> iface;
220 if (FAILED (host->FindHostNetworkInterfaceById (Guid(aId).toUtf16(), iface.asOutParam())))
221 return VERR_INVALID_PARAMETER;
222 iface->COMGETTER (Name) (ifname.asOutParam());
223 if (ifname.isNull())
224 return VERR_INTERNAL_ERROR;
225
226 rc = progress->init (pVBox, host,
227 Bstr ("Removing host network interface"),
228 FALSE /* aCancelable */);
229 if(SUCCEEDED(rc))
230 {
231 CheckComRCReturnRC (rc);
232 progress.queryInterfaceTo (aProgress);
233 iface.queryInterfaceTo (aHostNetworkInterface);
234 rc = NetIfAdpCtl(Utf8Str(ifname), "remove", NULL, NULL);
235 if (RT_FAILURE(rc))
236 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(), "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d).", rc);
237 else
238 progress->notifyComplete(S_OK);
239 }
240 }
241 else
242 {
243 progress->notifyComplete(hr);
244 rc = VERR_INTERNAL_ERROR;
245 }
246 return rc;
247#else
248 return VERR_NOT_IMPLEMENTED;
249#endif
250}
251
252int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
253{
254 return VERR_NOT_IMPLEMENTED;
255}
256
257int NetIfDhcpRediscover(VirtualBox * /* pVbox */, HostNetworkInterface * /* pIf */)
258{
259 return VERR_NOT_IMPLEMENTED;
260}
261
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