VirtualBox

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

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

#2946 & #2954: Main API now supports dynamic adding/removing network adapters on all platforms. Linux backend.

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