VirtualBox

source: vbox/trunk/src/VBox/Main/HostNetworkInterfaceImpl.cpp@ 31613

Last change on this file since 31613 was 31539, checked in by vboxsync, 14 years ago

Main: use settings struct for machine user data; remove iprt::MiniString::raw() and change all occurences to c_str()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: HostNetworkInterfaceImpl.cpp 31539 2010-08-10 15:40:18Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2008 Oracle Corporation
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
20#include "HostNetworkInterfaceImpl.h"
21#include "AutoCaller.h"
22#include "Logging.h"
23#include "netif.h"
24
25#include <iprt/cpp/utils.h>
26
27#ifdef RT_OS_FREEBSD
28# include <netinet/in.h> /* INADDR_NONE */
29#endif /* RT_OS_FREEBSD */
30
31// constructor / destructor
32/////////////////////////////////////////////////////////////////////////////
33
34HostNetworkInterface::HostNetworkInterface()
35 : mVBox(NULL)
36{
37}
38
39HostNetworkInterface::~HostNetworkInterface()
40{
41}
42
43HRESULT HostNetworkInterface::FinalConstruct()
44{
45 return S_OK;
46}
47
48void HostNetworkInterface::FinalRelease()
49{
50 uninit ();
51}
52
53// public initializer/uninitializer for internal purposes only
54/////////////////////////////////////////////////////////////////////////////
55
56/**
57 * Initializes the host object.
58 *
59 * @returns COM result indicator
60 * @param aInterfaceName name of the network interface
61 * @param aGuid GUID of the host network interface
62 */
63HRESULT HostNetworkInterface::init(Bstr aInterfaceName, Guid aGuid, HostNetworkInterfaceType_T ifType)
64{
65 LogFlowThisFunc(("aInterfaceName={%ls}, aGuid={%s}\n",
66 aInterfaceName.raw(), aGuid.toString().c_str()));
67
68 ComAssertRet(aInterfaceName, E_INVALIDARG);
69 ComAssertRet(!aGuid.isEmpty(), E_INVALIDARG);
70
71 /* Enclose the state transition NotReady->InInit->Ready */
72 AutoInitSpan autoInitSpan(this);
73 AssertReturn(autoInitSpan.isOk(), E_FAIL);
74
75 unconst(mInterfaceName) = aInterfaceName;
76 unconst(mGuid) = aGuid;
77 mIfType = ifType;
78
79 /* Confirm a successful initialization */
80 autoInitSpan.setSucceeded();
81
82 return S_OK;
83}
84
85#ifdef VBOX_WITH_HOSTNETIF_API
86
87HRESULT HostNetworkInterface::updateConfig ()
88{
89 NETIFINFO info;
90 int rc = NetIfGetConfig(this, &info);
91 if (RT_SUCCESS(rc))
92 {
93 m.realIPAddress = m.IPAddress = info.IPAddress.u;
94 m.realNetworkMask = m.networkMask = info.IPNetMask.u;
95 m.dhcpEnabled = info.bDhcpEnabled;
96 m.realIPV6Address = m.IPV6Address = composeIPv6Address(&info.IPv6Address);
97 m.realIPV6PrefixLength = m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&info.IPv6NetMask);
98 m.hardwareAddress = composeHardwareAddress(&info.MACAddress);
99#ifdef RT_OS_WINDOWS
100 m.mediumType = (HostNetworkInterfaceMediumType)info.enmMediumType;
101 m.status = (HostNetworkInterfaceStatus)info.enmStatus;
102#else /* !RT_OS_WINDOWS */
103 m.mediumType = info.enmMediumType;
104 m.status = info.enmStatus;
105
106#endif /* !RT_OS_WINDOWS */
107 return S_OK;
108 }
109 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
110}
111
112/**
113 * Initializes the host object.
114 *
115 * @returns COM result indicator
116 * @param aInterfaceName name of the network interface
117 * @param aGuid GUID of the host network interface
118 */
119HRESULT HostNetworkInterface::init (Bstr aInterfaceName, HostNetworkInterfaceType_T ifType, PNETIFINFO pIf)
120{
121// LogFlowThisFunc(("aInterfaceName={%ls}, aGuid={%s}\n",
122// aInterfaceName.raw(), aGuid.toString().raw()));
123
124// ComAssertRet(aInterfaceName, E_INVALIDARG);
125// ComAssertRet(!aGuid.isEmpty(), E_INVALIDARG);
126 ComAssertRet(pIf, E_INVALIDARG);
127
128 /* Enclose the state transition NotReady->InInit->Ready */
129 AutoInitSpan autoInitSpan(this);
130 AssertReturn(autoInitSpan.isOk(), E_FAIL);
131
132 unconst(mInterfaceName) = aInterfaceName;
133 unconst(mGuid) = pIf->Uuid;
134 mIfType = ifType;
135
136 m.realIPAddress = m.IPAddress = pIf->IPAddress.u;
137 m.realNetworkMask = m.networkMask = pIf->IPNetMask.u;
138 m.realIPV6Address = m.IPV6Address = composeIPv6Address(&pIf->IPv6Address);
139 m.realIPV6PrefixLength = m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&pIf->IPv6NetMask);
140 m.dhcpEnabled = pIf->bDhcpEnabled;
141 m.hardwareAddress = composeHardwareAddress(&pIf->MACAddress);
142#ifdef RT_OS_WINDOWS
143 m.mediumType = (HostNetworkInterfaceMediumType)pIf->enmMediumType;
144 m.status = (HostNetworkInterfaceStatus)pIf->enmStatus;
145#else /* !RT_OS_WINDOWS */
146 m.mediumType = pIf->enmMediumType;
147 m.status = pIf->enmStatus;
148#endif /* !RT_OS_WINDOWS */
149
150 /* Confirm a successful initialization */
151 autoInitSpan.setSucceeded();
152
153 return S_OK;
154}
155#endif
156
157// IHostNetworkInterface properties
158/////////////////////////////////////////////////////////////////////////////
159
160/**
161 * Returns the name of the host network interface.
162 *
163 * @returns COM status code
164 * @param aInterfaceName address of result pointer
165 */
166STDMETHODIMP HostNetworkInterface::COMGETTER(Name) (BSTR *aInterfaceName)
167{
168 CheckComArgOutPointerValid(aInterfaceName);
169
170 AutoCaller autoCaller(this);
171 if (FAILED(autoCaller.rc())) return autoCaller.rc();
172
173 mInterfaceName.cloneTo(aInterfaceName);
174
175 return S_OK;
176}
177
178/**
179 * Returns the GUID of the host network interface.
180 *
181 * @returns COM status code
182 * @param aGuid address of result pointer
183 */
184STDMETHODIMP HostNetworkInterface::COMGETTER(Id) (BSTR *aGuid)
185{
186 CheckComArgOutPointerValid(aGuid);
187
188 AutoCaller autoCaller(this);
189 if (FAILED(autoCaller.rc())) return autoCaller.rc();
190
191 mGuid.toUtf16().cloneTo(aGuid);
192
193 return S_OK;
194}
195
196STDMETHODIMP HostNetworkInterface::COMGETTER(DhcpEnabled) (BOOL *aDhcpEnabled)
197{
198 CheckComArgOutPointerValid(aDhcpEnabled);
199
200 AutoCaller autoCaller(this);
201 if (FAILED(autoCaller.rc())) return autoCaller.rc();
202
203 *aDhcpEnabled = m.dhcpEnabled;
204
205 return S_OK;
206}
207
208
209/**
210 * Returns the IP address of the host network interface.
211 *
212 * @returns COM status code
213 * @param aIPAddress address of result pointer
214 */
215STDMETHODIMP HostNetworkInterface::COMGETTER(IPAddress) (BSTR *aIPAddress)
216{
217 CheckComArgOutPointerValid(aIPAddress);
218
219 AutoCaller autoCaller(this);
220 if (FAILED(autoCaller.rc())) return autoCaller.rc();
221
222 if (m.IPAddress == 0)
223 {
224 getDefaultIPv4Address(mInterfaceName).detachTo(aIPAddress);
225 return S_OK;
226 }
227
228 in_addr tmp;
229#if defined(RT_OS_WINDOWS)
230 tmp.S_un.S_addr = m.IPAddress;
231#else
232 tmp.s_addr = m.IPAddress;
233#endif
234 char *addr = inet_ntoa(tmp);
235 if (addr)
236 {
237 Bstr(addr).detachTo(aIPAddress);
238 return S_OK;
239 }
240
241 return E_FAIL;
242}
243
244/**
245 * Returns the netwok mask of the host network interface.
246 *
247 * @returns COM status code
248 * @param aNetworkMask address of result pointer
249 */
250STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkMask) (BSTR *aNetworkMask)
251{
252 CheckComArgOutPointerValid(aNetworkMask);
253
254 AutoCaller autoCaller(this);
255 if (FAILED(autoCaller.rc())) return autoCaller.rc();
256
257 if (m.networkMask == 0)
258 {
259 Bstr(VBOXNET_IPV4MASK_DEFAULT).detachTo(aNetworkMask);
260 return S_OK;
261 }
262
263 in_addr tmp;
264#if defined(RT_OS_WINDOWS)
265 tmp.S_un.S_addr = m.networkMask;
266#else
267 tmp.s_addr = m.networkMask;
268#endif
269 char *addr = inet_ntoa(tmp);
270 if (addr)
271 {
272 Bstr(addr).detachTo(aNetworkMask);
273 return S_OK;
274 }
275
276 return E_FAIL;
277}
278
279STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Supported) (BOOL *aIPV6Supported)
280{
281 CheckComArgOutPointerValid(aIPV6Supported);
282#if defined(RT_OS_WINDOWS)
283 *aIPV6Supported = FALSE;
284#else
285 *aIPV6Supported = TRUE;
286#endif
287
288 return S_OK;
289}
290
291/**
292 * Returns the IP V6 address of the host network interface.
293 *
294 * @returns COM status code
295 * @param aIPV6Address address of result pointer
296 */
297STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Address) (BSTR *aIPV6Address)
298{
299 CheckComArgOutPointerValid(aIPV6Address);
300
301 AutoCaller autoCaller(this);
302 if (FAILED(autoCaller.rc())) return autoCaller.rc();
303
304 m.IPV6Address.cloneTo(aIPV6Address);
305
306 return S_OK;
307}
308
309/**
310 * Returns the IP V6 network mask of the host network interface.
311 *
312 * @returns COM status code
313 * @param aIPV6Mask address of result pointer
314 */
315STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6NetworkMaskPrefixLength) (ULONG *aIPV6NetworkMaskPrefixLength)
316{
317 CheckComArgOutPointerValid(aIPV6NetworkMaskPrefixLength);
318
319 AutoCaller autoCaller(this);
320 if (FAILED(autoCaller.rc())) return autoCaller.rc();
321
322 *aIPV6NetworkMaskPrefixLength = m.IPV6NetworkMaskPrefixLength;
323
324 return S_OK;
325}
326
327/**
328 * Returns the hardware address of the host network interface.
329 *
330 * @returns COM status code
331 * @param aHardwareAddress address of result pointer
332 */
333STDMETHODIMP HostNetworkInterface::COMGETTER(HardwareAddress) (BSTR *aHardwareAddress)
334{
335 CheckComArgOutPointerValid(aHardwareAddress);
336
337 AutoCaller autoCaller(this);
338 if (FAILED(autoCaller.rc())) return autoCaller.rc();
339
340 m.hardwareAddress.cloneTo(aHardwareAddress);
341
342 return S_OK;
343}
344
345/**
346 * Returns the encapsulation protocol type of the host network interface.
347 *
348 * @returns COM status code
349 * @param aType address of result pointer
350 */
351STDMETHODIMP HostNetworkInterface::COMGETTER(MediumType) (HostNetworkInterfaceMediumType_T *aType)
352{
353 CheckComArgOutPointerValid(aType);
354
355 AutoCaller autoCaller(this);
356 if (FAILED(autoCaller.rc())) return autoCaller.rc();
357
358 *aType = m.mediumType;
359
360 return S_OK;
361}
362
363/**
364 * Returns the current state of the host network interface.
365 *
366 * @returns COM status code
367 * @param aStatus address of result pointer
368 */
369STDMETHODIMP HostNetworkInterface::COMGETTER(Status) (HostNetworkInterfaceStatus_T *aStatus)
370{
371 CheckComArgOutPointerValid(aStatus);
372
373 AutoCaller autoCaller(this);
374 if (FAILED(autoCaller.rc())) return autoCaller.rc();
375
376 *aStatus = m.status;
377
378 return S_OK;
379}
380
381/**
382 * Returns network interface type
383 *
384 * @returns COM status code
385 * @param aType address of result pointer
386 */
387STDMETHODIMP HostNetworkInterface::COMGETTER(InterfaceType) (HostNetworkInterfaceType_T *aType)
388{
389 CheckComArgOutPointerValid(aType);
390
391 AutoCaller autoCaller(this);
392 if (FAILED(autoCaller.rc())) return autoCaller.rc();
393
394 *aType = mIfType;
395
396 return S_OK;
397
398}
399
400STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkName) (BSTR *aNetworkName)
401{
402 AutoCaller autoCaller(this);
403 if (FAILED(autoCaller.rc())) return autoCaller.rc();
404
405 Utf8Str utf8Name("HostInterfaceNetworking-");
406 utf8Name.append(Utf8Str(mInterfaceName)) ;
407 Bstr netName(utf8Name);
408 netName.detachTo(aNetworkName);
409
410 return S_OK;
411}
412
413STDMETHODIMP HostNetworkInterface::EnableStaticIpConfig (IN_BSTR aIPAddress, IN_BSTR aNetMask)
414{
415#ifndef VBOX_WITH_HOSTNETIF_API
416 return E_NOTIMPL;
417#else
418 AutoCaller autoCaller(this);
419 if (FAILED(autoCaller.rc())) return autoCaller.rc();
420
421 if (Bstr(aIPAddress).isEmpty())
422 {
423 if (m.IPAddress)
424 {
425 int rc = NetIfEnableStaticIpConfig(mVBox, this, m.IPAddress, 0, 0);
426 if (RT_SUCCESS(rc))
427 {
428 m.realIPAddress = 0;
429 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()), NULL)))
430 return E_FAIL;
431 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()), NULL)))
432 return E_FAIL;
433 return S_OK;
434 }
435 }
436 else
437 return S_OK;
438 }
439
440 ULONG ip, mask;
441 ip = inet_addr(Utf8Str(aIPAddress).c_str());
442 if (ip != INADDR_NONE)
443 {
444 if (Bstr(aNetMask).isEmpty())
445 mask = 0xFFFFFF;
446 else
447 mask = inet_addr(Utf8Str(aNetMask).c_str());
448 if (mask != INADDR_NONE)
449 {
450 if (m.realIPAddress == ip && m.realNetworkMask == mask)
451 return S_OK;
452 int rc = NetIfEnableStaticIpConfig(mVBox, this, m.IPAddress, ip, mask);
453 if (RT_SUCCESS(rc))
454 {
455 m.realIPAddress = ip;
456 m.realNetworkMask = mask;
457 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()), Bstr(aIPAddress))))
458 return E_FAIL;
459 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()), Bstr(aNetMask))))
460 return E_FAIL;
461 return S_OK;
462 }
463 else
464 {
465 LogRel(("Failed to EnableStaticIpConfig with rc=%Rrc\n", rc));
466 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
467 }
468
469 }
470 }
471 return E_FAIL;
472#endif
473}
474
475STDMETHODIMP HostNetworkInterface::EnableStaticIpConfigV6 (IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
476{
477#ifndef VBOX_WITH_HOSTNETIF_API
478 return E_NOTIMPL;
479#else
480 if (!aIPV6Address)
481 return E_INVALIDARG;
482 if (aIPV6MaskPrefixLength > 128)
483 return E_INVALIDARG;
484
485 AutoCaller autoCaller(this);
486 if (FAILED(autoCaller.rc())) return autoCaller.rc();
487
488 int rc = S_OK;
489 if (m.realIPV6Address != aIPV6Address || m.realIPV6PrefixLength != aIPV6MaskPrefixLength)
490 {
491 if (aIPV6MaskPrefixLength == 0)
492 aIPV6MaskPrefixLength = 64;
493 rc = NetIfEnableStaticIpConfigV6(mVBox, this, m.IPV6Address, aIPV6Address, aIPV6MaskPrefixLength);
494 if (RT_FAILURE(rc))
495 {
496 LogRel(("Failed to EnableStaticIpConfigV6 with rc=%Rrc\n", rc));
497 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
498 }
499 else
500 {
501 m.realIPV6Address = aIPV6Address;
502 m.realIPV6PrefixLength = aIPV6MaskPrefixLength;
503 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPV6Address", mInterfaceName.raw()), Bstr(aIPV6Address))))
504 return E_FAIL;
505 if (FAILED(mVBox->SetExtraData(BstrFmt("HostOnly/%ls/IPV6NetMask", mInterfaceName.raw()),
506 BstrFmt("%u", aIPV6MaskPrefixLength))))
507 return E_FAIL;
508 }
509
510 }
511 return S_OK;
512#endif
513}
514
515STDMETHODIMP HostNetworkInterface::EnableDynamicIpConfig ()
516{
517#ifndef VBOX_WITH_HOSTNETIF_API
518 return E_NOTIMPL;
519#else
520 AutoCaller autoCaller(this);
521 if (FAILED(autoCaller.rc())) return autoCaller.rc();
522
523 int rc = NetIfEnableDynamicIpConfig(mVBox, this);
524 if (RT_FAILURE(rc))
525 {
526 LogRel(("Failed to EnableDynamicIpConfig with rc=%Rrc\n", rc));
527 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
528 }
529 return S_OK;
530#endif
531}
532
533STDMETHODIMP HostNetworkInterface::DhcpRediscover ()
534{
535#ifndef VBOX_WITH_HOSTNETIF_API
536 return E_NOTIMPL;
537#else
538 AutoCaller autoCaller(this);
539 if (FAILED(autoCaller.rc())) return autoCaller.rc();
540
541 int rc = NetIfDhcpRediscover(mVBox, this);
542 if (RT_FAILURE(rc))
543 {
544 LogRel(("Failed to DhcpRediscover with rc=%Rrc\n", rc));
545 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
546 }
547 return S_OK;
548#endif
549}
550
551HRESULT HostNetworkInterface::setVirtualBox(VirtualBox *pVBox)
552{
553 HRESULT hrc;
554 AutoCaller autoCaller(this);
555 if (FAILED(autoCaller.rc())) return autoCaller.rc();
556 unconst(mVBox) = pVBox;
557
558 /* If IPv4 address hasn't been initialized */
559 if (m.IPAddress == 0)
560 {
561 Bstr tmpAddr, tmpMask;
562 hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPAddress", mInterfaceName.raw()), tmpAddr.asOutParam());
563 hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPNetMask", mInterfaceName.raw()), tmpMask.asOutParam());
564 if (tmpAddr.isEmpty())
565 tmpAddr = getDefaultIPv4Address(mInterfaceName);
566 if (tmpMask.isEmpty())
567 tmpMask = Bstr(VBOXNET_IPV4MASK_DEFAULT);
568 m.IPAddress = inet_addr(Utf8Str(tmpAddr).c_str());
569 m.networkMask = inet_addr(Utf8Str(tmpMask).c_str());
570 }
571
572 if (m.IPV6Address.isEmpty())
573 {
574 Bstr tmpPrefixLen;
575 hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPV6Address", mInterfaceName.raw()), m.IPV6Address.asOutParam());
576 if (!m.IPV6Address.isEmpty())
577 {
578 hrc = mVBox->GetExtraData(BstrFmt("HostOnly/%ls/IPV6PrefixLen", mInterfaceName.raw()), tmpPrefixLen.asOutParam());
579 if (SUCCEEDED(hrc) && !tmpPrefixLen.isEmpty())
580 m.IPV6NetworkMaskPrefixLength = Utf8Str(tmpPrefixLen).toUInt32();
581 else
582 m.IPV6NetworkMaskPrefixLength = 64;
583 }
584 }
585
586 return S_OK;
587}
588
589/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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