VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/* $Id: NetIf-generic.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * VirtualBox Main - Generic NetIf implementation.
4 */
5
6/*
7 * Copyright (C) 2009 Oracle Corporation
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
18#include <VBox/err.h>
19#include <VBox/log.h>
20#include <iprt/process.h>
21#include <iprt/env.h>
22#include <iprt/path.h>
23#include <iprt/param.h>
24
25#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
26# include <cstdio>
27#endif
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 = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME));
42 if (RT_FAILURE(rc))
43 {
44 LogRel(("NetIfAdpCtl: failed to get program path, rc=%Rrc.\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.c_str(), 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) || defined(RT_OS_FREEBSD)
132 /* create a progress object */
133 ComObjPtr<Progress> progress;
134 progress.createObject();
135
136 ComPtr<IHost> host;
137 HRESULT hrc = pVBox->COMGETTER(Host)(host.asOutParam());
138 if (SUCCEEDED(hrc))
139 {
140 hrc = progress->init(pVBox, host,
141 Bstr ("Creating host only network interface"),
142 FALSE /* aCancelable */);
143 if (SUCCEEDED(hrc))
144 {
145 progress.queryInterfaceTo(aProgress);
146
147 char szAdpCtl[RTPATH_MAX];
148 int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " add"));
149 if (RT_FAILURE(rc))
150 {
151 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(),
152 "Failed to get program path, rc=%Rrc\n", rc);
153 return rc;
154 }
155 strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " add");
156 FILE *fp = popen(szAdpCtl, "r");
157
158 if (fp)
159 {
160 char szBuf[VBOXNET_MAX_SHORT_NAME];
161 if (fgets(szBuf, sizeof(szBuf), fp))
162 {
163 char *pLast = szBuf + strlen(szBuf) - 1;
164 if (pLast >= szBuf && *pLast == '\n')
165 *pLast = 0;
166
167 size_t cbNameLen = strlen(szBuf) + 1;
168 PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
169 if (!pInfo)
170 rc = VERR_NO_MEMORY;
171 else
172 {
173 strcpy(pInfo->szShortName, szBuf);
174 strcpy(pInfo->szName, szBuf);
175 rc = NetIfGetConfigByName(pInfo);
176 if (RT_FAILURE(rc))
177 {
178 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(),
179 "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add')\n", szBuf);
180 }
181 else
182 {
183 Bstr IfName(szBuf);
184 /* create a new uninitialized host interface object */
185 ComObjPtr<HostNetworkInterface> iface;
186 iface.createObject();
187 iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
188 iface->setVirtualBox(pVBox);
189 iface.queryInterfaceTo(aHostNetworkInterface);
190 }
191 RTMemFree(pInfo);
192 }
193 }
194 if ((rc = pclose(fp)) != 0)
195 {
196 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(), "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
197 rc = VERR_INTERNAL_ERROR;
198 }
199 }
200 if (RT_SUCCESS(rc))
201 progress->notifyComplete(rc);
202 }
203 }
204
205 return hrc;
206
207#else
208 NOREF(pVBox);
209 NOREF(aHostNetworkInterface);
210 NOREF(aProgress);
211 return VERR_NOT_IMPLEMENTED;
212#endif
213}
214
215int NetIfRemoveHostOnlyNetworkInterface (VirtualBox *pVBox, IN_GUID aId,
216 IProgress **aProgress)
217{
218#if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
219 /* create a progress object */
220 ComObjPtr<Progress> progress;
221 progress.createObject();
222 ComPtr<IHost> host;
223 int rc = VINF_SUCCESS;
224 HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
225 if(SUCCEEDED(hr))
226 {
227 Bstr ifname;
228 ComPtr<IHostNetworkInterface> iface;
229 if (FAILED(host->FindHostNetworkInterfaceById (Guid(aId).toUtf16(), iface.asOutParam())))
230 return VERR_INVALID_PARAMETER;
231 iface->COMGETTER(Name) (ifname.asOutParam());
232 if (ifname.isEmpty())
233 return VERR_INTERNAL_ERROR;
234
235 rc = progress->init (pVBox, host,
236 Bstr ("Removing host network interface"),
237 FALSE /* aCancelable */);
238 if(SUCCEEDED(rc))
239 {
240 progress.queryInterfaceTo(aProgress);
241 rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
242 if (RT_FAILURE(rc))
243 progress->notifyComplete(E_FAIL, COM_IIDOF(IHostNetworkInterface), HostNetworkInterface::getComponentName(), "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
244 else
245 progress->notifyComplete(S_OK);
246 }
247 }
248 else
249 {
250 progress->notifyComplete(hr);
251 rc = VERR_INTERNAL_ERROR;
252 }
253 return rc;
254#else
255 NOREF(pVBox);
256 NOREF(aId);
257 NOREF(aProgress);
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