1 | /* $Id: HostNetworkInterfaceImpl.cpp 17358 2009-03-04 17:42:18Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VirtualBox COM class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2008 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "HostNetworkInterfaceImpl.h"
|
---|
25 | #include "Logging.h"
|
---|
26 | #include "netif.h"
|
---|
27 |
|
---|
28 | // constructor / destructor
|
---|
29 | /////////////////////////////////////////////////////////////////////////////
|
---|
30 |
|
---|
31 | DEFINE_EMPTY_CTOR_DTOR (HostNetworkInterface)
|
---|
32 |
|
---|
33 | HRESULT HostNetworkInterface::FinalConstruct()
|
---|
34 | {
|
---|
35 | return S_OK;
|
---|
36 | }
|
---|
37 |
|
---|
38 | void HostNetworkInterface::FinalRelease()
|
---|
39 | {
|
---|
40 | uninit ();
|
---|
41 | }
|
---|
42 |
|
---|
43 | // public initializer/uninitializer for internal purposes only
|
---|
44 | /////////////////////////////////////////////////////////////////////////////
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Initializes the host object.
|
---|
48 | *
|
---|
49 | * @returns COM result indicator
|
---|
50 | * @param aInterfaceName name of the network interface
|
---|
51 | * @param aGuid GUID of the host network interface
|
---|
52 | */
|
---|
53 | HRESULT HostNetworkInterface::init (Bstr aInterfaceName, Guid aGuid, HostNetworkInterfaceType_T ifType)
|
---|
54 | {
|
---|
55 | LogFlowThisFunc (("aInterfaceName={%ls}, aGuid={%s}\n",
|
---|
56 | aInterfaceName.raw(), aGuid.toString().raw()));
|
---|
57 |
|
---|
58 | ComAssertRet (aInterfaceName, E_INVALIDARG);
|
---|
59 | ComAssertRet (!aGuid.isEmpty(), E_INVALIDARG);
|
---|
60 |
|
---|
61 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
62 | AutoInitSpan autoInitSpan (this);
|
---|
63 | AssertReturn (autoInitSpan.isOk(), E_FAIL);
|
---|
64 |
|
---|
65 | unconst (mInterfaceName) = aInterfaceName;
|
---|
66 | unconst (mGuid) = aGuid;
|
---|
67 | mIfType = ifType;
|
---|
68 |
|
---|
69 |
|
---|
70 | /* Confirm a successful initialization */
|
---|
71 | autoInitSpan.setSucceeded();
|
---|
72 |
|
---|
73 | return S_OK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | #ifdef VBOX_WITH_HOSTNETIF_API
|
---|
77 | static Bstr composeIPv6Address(PRTNETADDRIPV6 aAddrPtr)
|
---|
78 | {
|
---|
79 | char szTmp[8*5];
|
---|
80 |
|
---|
81 | RTStrPrintf(szTmp, sizeof(szTmp),
|
---|
82 | "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
|
---|
83 | "%02x%02x:%02x%02x:%02x%02x:%02x%02x",
|
---|
84 | aAddrPtr->au8[0], aAddrPtr->au8[1],
|
---|
85 | aAddrPtr->au8[2], aAddrPtr->au8[3],
|
---|
86 | aAddrPtr->au8[4], aAddrPtr->au8[5],
|
---|
87 | aAddrPtr->au8[6], aAddrPtr->au8[7],
|
---|
88 | aAddrPtr->au8[8], aAddrPtr->au8[9],
|
---|
89 | aAddrPtr->au8[10], aAddrPtr->au8[11],
|
---|
90 | aAddrPtr->au8[12], aAddrPtr->au8[13],
|
---|
91 | aAddrPtr->au8[14], aAddrPtr->au8[15]);
|
---|
92 | return Bstr(szTmp);
|
---|
93 | }
|
---|
94 |
|
---|
95 | static Bstr composeHardwareAddress(PRTMAC aMacPtr)
|
---|
96 | {
|
---|
97 | char szTmp[6*3];
|
---|
98 |
|
---|
99 | RTStrPrintf(szTmp, sizeof(szTmp),
|
---|
100 | "%02x:%02x:%02x:%02x:%02x:%02x",
|
---|
101 | aMacPtr->au8[0], aMacPtr->au8[1],
|
---|
102 | aMacPtr->au8[2], aMacPtr->au8[3],
|
---|
103 | aMacPtr->au8[4], aMacPtr->au8[5]);
|
---|
104 | return Bstr(szTmp);
|
---|
105 | }
|
---|
106 |
|
---|
107 | HRESULT HostNetworkInterface::updateConfig (struct NETIFINFO *pIf)
|
---|
108 | {
|
---|
109 | m.IPAddress = pIf->IPAddress.u;
|
---|
110 | m.networkMask = pIf->IPNetMask.u;
|
---|
111 | m.defaultGateway = pIf->IPDefaultGateway.u;
|
---|
112 | m.IPV6Address = composeIPv6Address(&pIf->IPv6Address);
|
---|
113 | m.IPV6NetworkMask = composeIPv6Address(&pIf->IPv6NetMask);
|
---|
114 | m.IPV6DefaultGateway = composeIPv6Address(&pIf->IPV6DefaultGateway);
|
---|
115 | m.hardwareAddress = composeHardwareAddress(&pIf->MACAddress);
|
---|
116 | #ifdef RT_OS_WINDOWS
|
---|
117 | m.mediumType = (HostNetworkInterfaceMediumType)pIf->enmMediumType;
|
---|
118 | m.status = (HostNetworkInterfaceStatus)pIf->enmStatus;
|
---|
119 | #else /* !RT_OS_WINDOWS */
|
---|
120 | m.mediumType = pIf->enmMediumType;
|
---|
121 | m.status = pIf->enmStatus;
|
---|
122 | #endif /* !RT_OS_WINDOWS */
|
---|
123 |
|
---|
124 | return S_OK;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Initializes the host object.
|
---|
129 | *
|
---|
130 | * @returns COM result indicator
|
---|
131 | * @param aInterfaceName name of the network interface
|
---|
132 | * @param aGuid GUID of the host network interface
|
---|
133 | */
|
---|
134 | HRESULT HostNetworkInterface::init (Bstr aInterfaceName, HostNetworkInterfaceType_T ifType, PNETIFINFO pIf)
|
---|
135 | {
|
---|
136 | // LogFlowThisFunc (("aInterfaceName={%ls}, aGuid={%s}\n",
|
---|
137 | // aInterfaceName.raw(), aGuid.toString().raw()));
|
---|
138 |
|
---|
139 | // ComAssertRet (aInterfaceName, E_INVALIDARG);
|
---|
140 | // ComAssertRet (!aGuid.isEmpty(), E_INVALIDARG);
|
---|
141 | ComAssertRet (pIf, E_INVALIDARG);
|
---|
142 |
|
---|
143 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
144 | AutoInitSpan autoInitSpan (this);
|
---|
145 | AssertReturn (autoInitSpan.isOk(), E_FAIL);
|
---|
146 |
|
---|
147 | unconst (mInterfaceName) = aInterfaceName;
|
---|
148 | unconst (mGuid) = pIf->Uuid;
|
---|
149 | mIfType = ifType;
|
---|
150 |
|
---|
151 | updateConfig(pIf);
|
---|
152 |
|
---|
153 | /* Confirm a successful initialization */
|
---|
154 | autoInitSpan.setSucceeded();
|
---|
155 |
|
---|
156 | return S_OK;
|
---|
157 | }
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | // IHostNetworkInterface properties
|
---|
161 | /////////////////////////////////////////////////////////////////////////////
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Returns the name of the host network interface.
|
---|
165 | *
|
---|
166 | * @returns COM status code
|
---|
167 | * @param aInterfaceName address of result pointer
|
---|
168 | */
|
---|
169 | STDMETHODIMP HostNetworkInterface::COMGETTER(Name) (BSTR *aInterfaceName)
|
---|
170 | {
|
---|
171 | CheckComArgOutPointerValid(aInterfaceName);
|
---|
172 |
|
---|
173 | AutoCaller autoCaller (this);
|
---|
174 | CheckComRCReturnRC (autoCaller.rc());
|
---|
175 |
|
---|
176 | mInterfaceName.cloneTo (aInterfaceName);
|
---|
177 |
|
---|
178 | return S_OK;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Returns the GUID of the host network interface.
|
---|
183 | *
|
---|
184 | * @returns COM status code
|
---|
185 | * @param aGuid address of result pointer
|
---|
186 | */
|
---|
187 | STDMETHODIMP HostNetworkInterface::COMGETTER(Id) (OUT_GUID aGuid)
|
---|
188 | {
|
---|
189 | CheckComArgOutPointerValid(aGuid);
|
---|
190 |
|
---|
191 | AutoCaller autoCaller (this);
|
---|
192 | CheckComRCReturnRC (autoCaller.rc());
|
---|
193 |
|
---|
194 | mGuid.cloneTo (aGuid);
|
---|
195 |
|
---|
196 | return S_OK;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Returns the IP address of the host network interface.
|
---|
202 | *
|
---|
203 | * @returns COM status code
|
---|
204 | * @param aIPAddress address of result pointer
|
---|
205 | */
|
---|
206 | STDMETHODIMP HostNetworkInterface::COMGETTER(IPAddress) (ULONG *aIPAddress)
|
---|
207 | {
|
---|
208 | CheckComArgOutPointerValid(aIPAddress);
|
---|
209 |
|
---|
210 | AutoCaller autoCaller (this);
|
---|
211 | CheckComRCReturnRC (autoCaller.rc());
|
---|
212 |
|
---|
213 | *aIPAddress = m.IPAddress;
|
---|
214 |
|
---|
215 | return S_OK;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Returns the netwok mask of the host network interface.
|
---|
220 | *
|
---|
221 | * @returns COM status code
|
---|
222 | * @param aNetworkMask address of result pointer
|
---|
223 | */
|
---|
224 | STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkMask) (ULONG *aNetworkMask)
|
---|
225 | {
|
---|
226 | CheckComArgOutPointerValid(aNetworkMask);
|
---|
227 |
|
---|
228 | AutoCaller autoCaller (this);
|
---|
229 | CheckComRCReturnRC (autoCaller.rc());
|
---|
230 |
|
---|
231 | *aNetworkMask = m.networkMask;
|
---|
232 |
|
---|
233 | return S_OK;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Returns the default gateway of the host network interface.
|
---|
238 | *
|
---|
239 | * @returns COM status code
|
---|
240 | * @param aNetworkMask address of result pointer
|
---|
241 | */
|
---|
242 | STDMETHODIMP HostNetworkInterface::COMGETTER(DefaultGateway) (ULONG *aDefaultGateway)
|
---|
243 | {
|
---|
244 | CheckComArgOutPointerValid(aDefaultGateway);
|
---|
245 |
|
---|
246 | AutoCaller autoCaller (this);
|
---|
247 | CheckComRCReturnRC (autoCaller.rc());
|
---|
248 |
|
---|
249 | *aDefaultGateway = m.defaultGateway;
|
---|
250 |
|
---|
251 | return S_OK;
|
---|
252 | }
|
---|
253 |
|
---|
254 | STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Supported) (BOOL *aIPV6Supported)
|
---|
255 | {
|
---|
256 | CheckComArgOutPointerValid(aIPV6Supported);
|
---|
257 |
|
---|
258 | *aIPV6Supported = TRUE;
|
---|
259 |
|
---|
260 | return S_OK;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Returns the IP V6 address of the host network interface.
|
---|
265 | *
|
---|
266 | * @returns COM status code
|
---|
267 | * @param aIPV6Address address of result pointer
|
---|
268 | */
|
---|
269 | STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Address) (BSTR *aIPV6Address)
|
---|
270 | {
|
---|
271 | CheckComArgOutPointerValid(aIPV6Address);
|
---|
272 |
|
---|
273 | AutoCaller autoCaller (this);
|
---|
274 | CheckComRCReturnRC (autoCaller.rc());
|
---|
275 |
|
---|
276 | m.IPV6Address.cloneTo (aIPV6Address);
|
---|
277 |
|
---|
278 | return S_OK;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Returns the IP V6 network mask of the host network interface.
|
---|
283 | *
|
---|
284 | * @returns COM status code
|
---|
285 | * @param aIPV6Mask address of result pointer
|
---|
286 | */
|
---|
287 | STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6NetworkMask) (BSTR *aIPV6Mask)
|
---|
288 | {
|
---|
289 | CheckComArgOutPointerValid(aIPV6Mask);
|
---|
290 |
|
---|
291 | AutoCaller autoCaller (this);
|
---|
292 | CheckComRCReturnRC (autoCaller.rc());
|
---|
293 |
|
---|
294 | m.IPV6NetworkMask.cloneTo (aIPV6Mask);
|
---|
295 |
|
---|
296 | return S_OK;
|
---|
297 | }
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Returns the IP V6 default gateway of the host network interface.
|
---|
301 | *
|
---|
302 | * @returns COM status code
|
---|
303 | * @param aIPV6DefaultGateway address of result pointer
|
---|
304 | */
|
---|
305 | STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6DefaultGateway) (BSTR *aIPV6DefaultGateway)
|
---|
306 | {
|
---|
307 | CheckComArgOutPointerValid(aIPV6DefaultGateway);
|
---|
308 |
|
---|
309 | AutoCaller autoCaller (this);
|
---|
310 | CheckComRCReturnRC (autoCaller.rc());
|
---|
311 |
|
---|
312 | m.IPV6DefaultGateway.cloneTo (aIPV6DefaultGateway);
|
---|
313 |
|
---|
314 | return S_OK;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Returns the hardware address of the host network interface.
|
---|
319 | *
|
---|
320 | * @returns COM status code
|
---|
321 | * @param aHardwareAddress address of result pointer
|
---|
322 | */
|
---|
323 | STDMETHODIMP HostNetworkInterface::COMGETTER(HardwareAddress) (BSTR *aHardwareAddress)
|
---|
324 | {
|
---|
325 | CheckComArgOutPointerValid(aHardwareAddress);
|
---|
326 |
|
---|
327 | AutoCaller autoCaller (this);
|
---|
328 | CheckComRCReturnRC (autoCaller.rc());
|
---|
329 |
|
---|
330 | m.hardwareAddress.cloneTo (aHardwareAddress);
|
---|
331 |
|
---|
332 | return S_OK;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Returns the encapsulation protocol type of the host network interface.
|
---|
337 | *
|
---|
338 | * @returns COM status code
|
---|
339 | * @param aType address of result pointer
|
---|
340 | */
|
---|
341 | STDMETHODIMP HostNetworkInterface::COMGETTER(MediumType) (HostNetworkInterfaceMediumType_T *aType)
|
---|
342 | {
|
---|
343 | CheckComArgOutPointerValid(aType);
|
---|
344 |
|
---|
345 | AutoCaller autoCaller (this);
|
---|
346 | CheckComRCReturnRC (autoCaller.rc());
|
---|
347 |
|
---|
348 | *aType = m.mediumType;
|
---|
349 |
|
---|
350 | return S_OK;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Returns the current state of the host network interface.
|
---|
355 | *
|
---|
356 | * @returns COM status code
|
---|
357 | * @param aStatus address of result pointer
|
---|
358 | */
|
---|
359 | STDMETHODIMP HostNetworkInterface::COMGETTER(Status) (HostNetworkInterfaceStatus_T *aStatus)
|
---|
360 | {
|
---|
361 | CheckComArgOutPointerValid(aStatus);
|
---|
362 |
|
---|
363 | AutoCaller autoCaller (this);
|
---|
364 | CheckComRCReturnRC (autoCaller.rc());
|
---|
365 |
|
---|
366 | *aStatus = m.status;
|
---|
367 |
|
---|
368 | return S_OK;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Returns network interface type
|
---|
373 | *
|
---|
374 | * @returns COM status code
|
---|
375 | * @param aType address of result pointer
|
---|
376 | */
|
---|
377 | STDMETHODIMP HostNetworkInterface::COMGETTER(InterfaceType) (HostNetworkInterfaceType_T *aType)
|
---|
378 | {
|
---|
379 | CheckComArgOutPointerValid(aType);
|
---|
380 |
|
---|
381 | AutoCaller autoCaller (this);
|
---|
382 | CheckComRCReturnRC (autoCaller.rc());
|
---|
383 |
|
---|
384 | *aType = mIfType;
|
---|
385 |
|
---|
386 | return S_OK;
|
---|
387 |
|
---|
388 | }
|
---|
389 |
|
---|
390 | STDMETHODIMP HostNetworkInterface::EnableStaticIpConfig (ULONG aIPAddress, ULONG aNetworkMask, ULONG aDefaultGateway)
|
---|
391 | {
|
---|
392 | #ifndef VBOX_WITH_HOSTNETIF_API
|
---|
393 | return E_NOTIMPL;
|
---|
394 | #else
|
---|
395 | AutoCaller autoCaller (this);
|
---|
396 | CheckComRCReturnRC (autoCaller.rc());
|
---|
397 |
|
---|
398 | int rc = NetIfEnableStaticIpConfig(this, aIPAddress, aNetworkMask, aDefaultGateway);
|
---|
399 | if (RT_FAILURE(rc))
|
---|
400 | {
|
---|
401 | LogRel(("Failed to EnableStaticIpConfigV6 with rc=%Vrc\n", rc));
|
---|
402 | return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
|
---|
403 | }
|
---|
404 | return S_OK;
|
---|
405 | #endif
|
---|
406 | }
|
---|
407 |
|
---|
408 | STDMETHODIMP HostNetworkInterface::EnableStaticIpConfigV6 (IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength, IN_BSTR aIPV6DefaultGateway)
|
---|
409 | {
|
---|
410 | #ifndef VBOX_WITH_HOSTNETIF_API
|
---|
411 | return E_NOTIMPL;
|
---|
412 | #else
|
---|
413 | if (!aIPV6Address)
|
---|
414 | return E_INVALIDARG;
|
---|
415 | if (!aIPV6DefaultGateway)
|
---|
416 | return E_INVALIDARG;
|
---|
417 | if (aIPV6MaskPrefixLength > 128)
|
---|
418 | return E_INVALIDARG;
|
---|
419 |
|
---|
420 | AutoCaller autoCaller (this);
|
---|
421 | CheckComRCReturnRC (autoCaller.rc());
|
---|
422 |
|
---|
423 | int rc = NetIfEnableStaticIpConfigV6(this, aIPV6Address, aIPV6MaskPrefixLength, aIPV6DefaultGateway);
|
---|
424 | if (RT_FAILURE(rc))
|
---|
425 | {
|
---|
426 | LogRel(("Failed to EnableStaticIpConfigV6 with rc=%Vrc\n", rc));
|
---|
427 | return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
|
---|
428 | }
|
---|
429 | return S_OK;
|
---|
430 | #endif
|
---|
431 | }
|
---|
432 |
|
---|
433 | STDMETHODIMP HostNetworkInterface::EnableDynamicIpConfig ()
|
---|
434 | {
|
---|
435 | #ifndef VBOX_WITH_HOSTNETIF_API
|
---|
436 | return E_NOTIMPL;
|
---|
437 | #else
|
---|
438 | AutoCaller autoCaller (this);
|
---|
439 | CheckComRCReturnRC (autoCaller.rc());
|
---|
440 |
|
---|
441 | int rc = NetIfEnableDynamicIpConfig(this);
|
---|
442 | if (RT_FAILURE(rc))
|
---|
443 | {
|
---|
444 | LogRel(("Failed to EnableStaticIpConfigV6 with rc=%Vrc\n", rc));
|
---|
445 | return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
|
---|
446 | }
|
---|
447 | return S_OK;
|
---|
448 | #endif
|
---|
449 | }
|
---|
450 |
|
---|
451 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|