1 | /* $Id: NetIf-generic.cpp 32718 2010-09-23 12:57:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Main - Generic NetIf implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2010 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 |
|
---|
36 | static 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 |
|
---|
73 | static 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 |
|
---|
81 | int 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 |
|
---|
106 | int 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 |
|
---|
123 | int NetIfEnableDynamicIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * /* pIf */)
|
---|
124 | {
|
---|
125 | return VERR_NOT_IMPLEMENTED;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | int 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").raw(),
|
---|
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,
|
---|
152 | COM_IIDOF(IHostNetworkInterface),
|
---|
153 | HostNetworkInterface::getStaticComponentName(),
|
---|
154 | "Failed to get program path, rc=%Rrc\n", rc);
|
---|
155 | return rc;
|
---|
156 | }
|
---|
157 | strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " add");
|
---|
158 | FILE *fp = popen(szAdpCtl, "r");
|
---|
159 |
|
---|
160 | if (fp)
|
---|
161 | {
|
---|
162 | char szBuf[VBOXNET_MAX_SHORT_NAME];
|
---|
163 | if (fgets(szBuf, sizeof(szBuf), fp))
|
---|
164 | {
|
---|
165 | char *pLast = szBuf + strlen(szBuf) - 1;
|
---|
166 | if (pLast >= szBuf && *pLast == '\n')
|
---|
167 | *pLast = 0;
|
---|
168 |
|
---|
169 | size_t cbNameLen = strlen(szBuf) + 1;
|
---|
170 | PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
|
---|
171 | if (!pInfo)
|
---|
172 | rc = VERR_NO_MEMORY;
|
---|
173 | else
|
---|
174 | {
|
---|
175 | strcpy(pInfo->szShortName, szBuf);
|
---|
176 | strcpy(pInfo->szName, szBuf);
|
---|
177 | rc = NetIfGetConfigByName(pInfo);
|
---|
178 | if (RT_FAILURE(rc))
|
---|
179 | {
|
---|
180 | progress->notifyComplete(E_FAIL,
|
---|
181 | COM_IIDOF(IHostNetworkInterface),
|
---|
182 | HostNetworkInterface::getStaticComponentName(),
|
---|
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,
|
---|
201 | COM_IIDOF(IHostNetworkInterface),
|
---|
202 | HostNetworkInterface::getStaticComponentName(),
|
---|
203 | "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
|
---|
204 | rc = VERR_INTERNAL_ERROR;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | if (RT_SUCCESS(rc))
|
---|
208 | progress->notifyComplete(rc);
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | return hrc;
|
---|
213 |
|
---|
214 | #else
|
---|
215 | NOREF(pVBox);
|
---|
216 | NOREF(aHostNetworkInterface);
|
---|
217 | NOREF(aProgress);
|
---|
218 | return VERR_NOT_IMPLEMENTED;
|
---|
219 | #endif
|
---|
220 | }
|
---|
221 |
|
---|
222 | int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVBox, IN_GUID aId,
|
---|
223 | IProgress **aProgress)
|
---|
224 | {
|
---|
225 | #if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
226 | /* create a progress object */
|
---|
227 | ComObjPtr<Progress> progress;
|
---|
228 | progress.createObject();
|
---|
229 | ComPtr<IHost> host;
|
---|
230 | int rc = VINF_SUCCESS;
|
---|
231 | HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
|
---|
232 | if(SUCCEEDED(hr))
|
---|
233 | {
|
---|
234 | Bstr ifname;
|
---|
235 | ComPtr<IHostNetworkInterface> iface;
|
---|
236 | if (FAILED(host->FindHostNetworkInterfaceById(Guid(aId).toUtf16().raw(), iface.asOutParam())))
|
---|
237 | return VERR_INVALID_PARAMETER;
|
---|
238 | iface->COMGETTER(Name)(ifname.asOutParam());
|
---|
239 | if (ifname.isEmpty())
|
---|
240 | return VERR_INTERNAL_ERROR;
|
---|
241 |
|
---|
242 | rc = progress->init(pVBox, host,
|
---|
243 | Bstr("Removing host network interface").raw(),
|
---|
244 | FALSE /* aCancelable */);
|
---|
245 | if(SUCCEEDED(rc))
|
---|
246 | {
|
---|
247 | progress.queryInterfaceTo(aProgress);
|
---|
248 | rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
|
---|
249 | if (RT_FAILURE(rc))
|
---|
250 | progress->notifyComplete(E_FAIL,
|
---|
251 | COM_IIDOF(IHostNetworkInterface),
|
---|
252 | HostNetworkInterface::getStaticComponentName(),
|
---|
253 | "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
|
---|
254 | else
|
---|
255 | progress->notifyComplete(S_OK);
|
---|
256 | }
|
---|
257 | }
|
---|
258 | else
|
---|
259 | {
|
---|
260 | progress->notifyComplete(hr);
|
---|
261 | rc = VERR_INTERNAL_ERROR;
|
---|
262 | }
|
---|
263 | return rc;
|
---|
264 | #else
|
---|
265 | NOREF(pVBox);
|
---|
266 | NOREF(aId);
|
---|
267 | NOREF(aProgress);
|
---|
268 | return VERR_NOT_IMPLEMENTED;
|
---|
269 | #endif
|
---|
270 | }
|
---|
271 |
|
---|
272 | int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
|
---|
273 | {
|
---|
274 | return VERR_NOT_IMPLEMENTED;
|
---|
275 | }
|
---|
276 |
|
---|
277 | int NetIfDhcpRediscover(VirtualBox * /* pVbox */, HostNetworkInterface * /* pIf */)
|
---|
278 | {
|
---|
279 | return VERR_NOT_IMPLEMENTED;
|
---|
280 | }
|
---|
281 |
|
---|