VirtualBox

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

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

dhcp settings fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 KB
Line 
1/* $Id: HostNetworkInterfaceImpl.cpp 17890 2009-03-15 17:27:19Z 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#ifndef RT_OS_WINDOWS
29#include <arpa/inet.h>
30#endif /* RT_OS_WINDOWS */
31
32// constructor / destructor
33/////////////////////////////////////////////////////////////////////////////
34
35DEFINE_EMPTY_CTOR_DTOR (HostNetworkInterface)
36
37HRESULT HostNetworkInterface::FinalConstruct()
38{
39 return S_OK;
40}
41
42void HostNetworkInterface::FinalRelease()
43{
44 uninit ();
45}
46
47// public initializer/uninitializer for internal purposes only
48/////////////////////////////////////////////////////////////////////////////
49
50/**
51 * Initializes the host object.
52 *
53 * @returns COM result indicator
54 * @param aInterfaceName name of the network interface
55 * @param aGuid GUID of the host network interface
56 */
57HRESULT HostNetworkInterface::init (Bstr aInterfaceName, Guid aGuid, HostNetworkInterfaceType_T ifType)
58{
59 LogFlowThisFunc (("aInterfaceName={%ls}, aGuid={%s}\n",
60 aInterfaceName.raw(), aGuid.toString().raw()));
61
62 ComAssertRet (aInterfaceName, E_INVALIDARG);
63 ComAssertRet (!aGuid.isEmpty(), E_INVALIDARG);
64
65 /* Enclose the state transition NotReady->InInit->Ready */
66 AutoInitSpan autoInitSpan (this);
67 AssertReturn (autoInitSpan.isOk(), E_FAIL);
68
69 unconst (mInterfaceName) = aInterfaceName;
70 unconst (mGuid) = aGuid;
71 mIfType = ifType;
72
73
74 /* Confirm a successful initialization */
75 autoInitSpan.setSucceeded();
76
77 return S_OK;
78}
79
80#ifdef VBOX_WITH_HOSTNETIF_API
81
82HRESULT HostNetworkInterface::updateConfig ()
83{
84 NETIFINFO info;
85 int rc = NetIfGetConfig(this, &info);
86 if(RT_SUCCESS(rc))
87 {
88 m.IPAddress = info.IPAddress.u;
89 m.networkMask = info.IPNetMask.u;
90 m.dhcpEnabled = info.bDhcpEnabled;
91 m.IPV6Address = composeIPv6Address(&info.IPv6Address);
92 m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&info.IPv6NetMask);
93 m.hardwareAddress = composeHardwareAddress(&info.MACAddress);
94#ifdef RT_OS_WINDOWS
95 m.mediumType = (HostNetworkInterfaceMediumType)info.enmMediumType;
96 m.status = (HostNetworkInterfaceStatus)info.enmStatus;
97#else /* !RT_OS_WINDOWS */
98 m.mediumType = info.enmMediumType;
99 m.status = info.enmStatus;
100
101#endif /* !RT_OS_WINDOWS */
102 return S_OK;
103 }
104 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
105}
106
107/**
108 * Initializes the host object.
109 *
110 * @returns COM result indicator
111 * @param aInterfaceName name of the network interface
112 * @param aGuid GUID of the host network interface
113 */
114HRESULT HostNetworkInterface::init (Bstr aInterfaceName, HostNetworkInterfaceType_T ifType, PNETIFINFO pIf)
115{
116// LogFlowThisFunc (("aInterfaceName={%ls}, aGuid={%s}\n",
117// aInterfaceName.raw(), aGuid.toString().raw()));
118
119// ComAssertRet (aInterfaceName, E_INVALIDARG);
120// ComAssertRet (!aGuid.isEmpty(), E_INVALIDARG);
121 ComAssertRet (pIf, E_INVALIDARG);
122
123 /* Enclose the state transition NotReady->InInit->Ready */
124 AutoInitSpan autoInitSpan (this);
125 AssertReturn (autoInitSpan.isOk(), E_FAIL);
126
127 unconst (mInterfaceName) = aInterfaceName;
128 unconst (mGuid) = pIf->Uuid;
129 mIfType = ifType;
130
131 m.IPAddress = pIf->IPAddress.u;
132 m.networkMask = pIf->IPNetMask.u;
133 m.IPV6Address = composeIPv6Address(&pIf->IPv6Address);
134 m.IPV6NetworkMaskPrefixLength = composeIPv6PrefixLenghFromAddress(&pIf->IPv6NetMask);
135 m.dhcpEnabled = pIf->bDhcpEnabled;
136 m.hardwareAddress = composeHardwareAddress(&pIf->MACAddress);
137#ifdef RT_OS_WINDOWS
138 m.mediumType = (HostNetworkInterfaceMediumType)pIf->enmMediumType;
139 m.status = (HostNetworkInterfaceStatus)pIf->enmStatus;
140#else /* !RT_OS_WINDOWS */
141 m.mediumType = pIf->enmMediumType;
142 m.status = pIf->enmStatus;
143#endif /* !RT_OS_WINDOWS */
144
145 /* Confirm a successful initialization */
146 autoInitSpan.setSucceeded();
147
148 return S_OK;
149}
150#endif
151
152// IHostNetworkInterface properties
153/////////////////////////////////////////////////////////////////////////////
154
155/**
156 * Returns the name of the host network interface.
157 *
158 * @returns COM status code
159 * @param aInterfaceName address of result pointer
160 */
161STDMETHODIMP HostNetworkInterface::COMGETTER(Name) (BSTR *aInterfaceName)
162{
163 CheckComArgOutPointerValid(aInterfaceName);
164
165 AutoCaller autoCaller (this);
166 CheckComRCReturnRC (autoCaller.rc());
167
168 mInterfaceName.cloneTo (aInterfaceName);
169
170 return S_OK;
171}
172
173/**
174 * Returns the GUID of the host network interface.
175 *
176 * @returns COM status code
177 * @param aGuid address of result pointer
178 */
179STDMETHODIMP HostNetworkInterface::COMGETTER(Id) (OUT_GUID aGuid)
180{
181 CheckComArgOutPointerValid(aGuid);
182
183 AutoCaller autoCaller (this);
184 CheckComRCReturnRC (autoCaller.rc());
185
186 mGuid.cloneTo (aGuid);
187
188 return S_OK;
189}
190
191STDMETHODIMP HostNetworkInterface::COMGETTER(DhcpEnabled) (BOOL *aDhcpEnabled)
192{
193 CheckComArgOutPointerValid(aDhcpEnabled);
194
195 AutoCaller autoCaller (this);
196 CheckComRCReturnRC (autoCaller.rc());
197
198 *aDhcpEnabled = m.dhcpEnabled;
199
200 return S_OK;
201}
202
203
204/**
205 * Returns the IP address of the host network interface.
206 *
207 * @returns COM status code
208 * @param aIPAddress address of result pointer
209 */
210STDMETHODIMP HostNetworkInterface::COMGETTER(IPAddress) (BSTR *aIPAddress)
211{
212 CheckComArgOutPointerValid(aIPAddress);
213
214 AutoCaller autoCaller (this);
215 CheckComRCReturnRC (autoCaller.rc());
216
217 in_addr tmp;
218#if defined(RT_OS_WINDOWS)
219 tmp.S_un.S_addr = m.IPAddress;
220#else
221 tmp.s_addr = m.IPAddress;
222#endif
223 char *addr = inet_ntoa(tmp);
224 if(addr)
225 {
226 Bstr(addr).detachTo(aIPAddress);
227 return S_OK;
228 }
229
230 return E_FAIL;
231}
232
233/**
234 * Returns the netwok mask of the host network interface.
235 *
236 * @returns COM status code
237 * @param aNetworkMask address of result pointer
238 */
239STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkMask) (BSTR *aNetworkMask)
240{
241 CheckComArgOutPointerValid(aNetworkMask);
242
243 AutoCaller autoCaller (this);
244 CheckComRCReturnRC (autoCaller.rc());
245
246 in_addr tmp;
247#if defined(RT_OS_WINDOWS)
248 tmp.S_un.S_addr = m.networkMask;
249#else
250 tmp.s_addr = m.networkMask;
251#endif
252 char *addr = inet_ntoa(tmp);
253 if(addr)
254 {
255 Bstr(addr).detachTo(aNetworkMask);
256 return S_OK;
257 }
258
259 return E_FAIL;
260}
261
262STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Supported) (BOOL *aIPV6Supported)
263{
264 CheckComArgOutPointerValid(aIPV6Supported);
265#if defined(RT_OS_WINDOWS)
266 *aIPV6Supported = FALSE;
267#else
268 *aIPV6Supported = TRUE;
269#endif
270
271 return S_OK;
272}
273
274/**
275 * Returns the IP V6 address of the host network interface.
276 *
277 * @returns COM status code
278 * @param aIPV6Address address of result pointer
279 */
280STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6Address) (BSTR *aIPV6Address)
281{
282 CheckComArgOutPointerValid(aIPV6Address);
283
284 AutoCaller autoCaller (this);
285 CheckComRCReturnRC (autoCaller.rc());
286
287 m.IPV6Address.cloneTo (aIPV6Address);
288
289 return S_OK;
290}
291
292/**
293 * Returns the IP V6 network mask of the host network interface.
294 *
295 * @returns COM status code
296 * @param aIPV6Mask address of result pointer
297 */
298STDMETHODIMP HostNetworkInterface::COMGETTER(IPV6NetworkMaskPrefixLength) (ULONG *aIPV6NetworkMaskPrefixLength)
299{
300 CheckComArgOutPointerValid(aIPV6NetworkMaskPrefixLength);
301
302 AutoCaller autoCaller (this);
303 CheckComRCReturnRC (autoCaller.rc());
304
305 *aIPV6NetworkMaskPrefixLength = m.IPV6NetworkMaskPrefixLength;
306
307 return S_OK;
308}
309
310/**
311 * Returns the hardware address of the host network interface.
312 *
313 * @returns COM status code
314 * @param aHardwareAddress address of result pointer
315 */
316STDMETHODIMP HostNetworkInterface::COMGETTER(HardwareAddress) (BSTR *aHardwareAddress)
317{
318 CheckComArgOutPointerValid(aHardwareAddress);
319
320 AutoCaller autoCaller (this);
321 CheckComRCReturnRC (autoCaller.rc());
322
323 m.hardwareAddress.cloneTo (aHardwareAddress);
324
325 return S_OK;
326}
327
328/**
329 * Returns the encapsulation protocol type of the host network interface.
330 *
331 * @returns COM status code
332 * @param aType address of result pointer
333 */
334STDMETHODIMP HostNetworkInterface::COMGETTER(MediumType) (HostNetworkInterfaceMediumType_T *aType)
335{
336 CheckComArgOutPointerValid(aType);
337
338 AutoCaller autoCaller (this);
339 CheckComRCReturnRC (autoCaller.rc());
340
341 *aType = m.mediumType;
342
343 return S_OK;
344}
345
346/**
347 * Returns the current state of the host network interface.
348 *
349 * @returns COM status code
350 * @param aStatus address of result pointer
351 */
352STDMETHODIMP HostNetworkInterface::COMGETTER(Status) (HostNetworkInterfaceStatus_T *aStatus)
353{
354 CheckComArgOutPointerValid(aStatus);
355
356 AutoCaller autoCaller (this);
357 CheckComRCReturnRC (autoCaller.rc());
358
359 *aStatus = m.status;
360
361 return S_OK;
362}
363
364/**
365 * Returns network interface type
366 *
367 * @returns COM status code
368 * @param aType address of result pointer
369 */
370STDMETHODIMP HostNetworkInterface::COMGETTER(InterfaceType) (HostNetworkInterfaceType_T *aType)
371{
372 CheckComArgOutPointerValid(aType);
373
374 AutoCaller autoCaller (this);
375 CheckComRCReturnRC (autoCaller.rc());
376
377 *aType = mIfType;
378
379 return S_OK;
380
381}
382
383STDMETHODIMP HostNetworkInterface::COMGETTER(NetworkName) (BSTR *aNetworkName)
384{
385 AutoCaller autoCaller (this);
386 CheckComRCReturnRC (autoCaller.rc());
387
388 Utf8Str utf8Name("HostInterfaceNetworking-");
389 utf8Name.append(Utf8Str(mInterfaceName)) ;
390 Bstr netName(utf8Name);
391 netName.detachTo (aNetworkName);
392
393 return S_OK;
394}
395
396STDMETHODIMP HostNetworkInterface::EnableStaticIpConfig (IN_BSTR aIPAddress, IN_BSTR aNetMask)
397{
398#ifndef VBOX_WITH_HOSTNETIF_API
399 return E_NOTIMPL;
400#else
401 AutoCaller autoCaller (this);
402 CheckComRCReturnRC (autoCaller.rc());
403
404 ULONG ip, mask;
405 ip = inet_addr(Utf8Str(aIPAddress).raw());
406 if(ip != INADDR_NONE)
407 {
408 mask = inet_addr(Utf8Str(aNetMask).raw());
409 if(mask != INADDR_NONE)
410 {
411 int rc = NetIfEnableStaticIpConfig(mVBox, this, ip, mask);
412 if (RT_SUCCESS(rc))
413 {
414 return S_OK;
415 }
416 else
417 {
418 LogRel(("Failed to EnableStaticIpConfig with rc=%Vrc\n", rc));
419 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
420 }
421
422 }
423 }
424 return E_FAIL;
425#endif
426}
427
428STDMETHODIMP HostNetworkInterface::EnableStaticIpConfigV6 (IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
429{
430#ifndef VBOX_WITH_HOSTNETIF_API
431 return E_NOTIMPL;
432#else
433 if (!aIPV6Address)
434 return E_INVALIDARG;
435 if (aIPV6MaskPrefixLength > 128)
436 return E_INVALIDARG;
437
438 AutoCaller autoCaller (this);
439 CheckComRCReturnRC (autoCaller.rc());
440
441 int rc = NetIfEnableStaticIpConfigV6(mVBox, this, aIPV6Address, aIPV6MaskPrefixLength);
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
451STDMETHODIMP HostNetworkInterface::EnableDynamicIpConfig ()
452{
453#ifndef VBOX_WITH_HOSTNETIF_API
454 return E_NOTIMPL;
455#else
456 AutoCaller autoCaller (this);
457 CheckComRCReturnRC (autoCaller.rc());
458
459 int rc = NetIfEnableDynamicIpConfig(mVBox, this);
460 if (RT_FAILURE(rc))
461 {
462 LogRel(("Failed to EnableDynamicIpConfig with rc=%Vrc\n", rc));
463 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
464 }
465 return S_OK;
466#endif
467}
468
469STDMETHODIMP HostNetworkInterface::DhcpRediscover ()
470{
471#ifndef VBOX_WITH_HOSTNETIF_API
472 return E_NOTIMPL;
473#else
474 AutoCaller autoCaller (this);
475 CheckComRCReturnRC (autoCaller.rc());
476
477 int rc = NetIfDhcpRediscover(mVBox, this);
478 if (RT_FAILURE(rc))
479 {
480 LogRel(("Failed to DhcpRediscover with rc=%Vrc\n", rc));
481 return rc == VERR_NOT_IMPLEMENTED ? E_NOTIMPL : E_FAIL;
482 }
483 return S_OK;
484#endif
485}
486
487HRESULT HostNetworkInterface::setVirtualBox(VirtualBox *pVBox)
488{
489 AutoCaller autoCaller (this);
490 CheckComRCReturnRC (autoCaller.rc());
491 mVBox = pVBox;
492
493 return S_OK;
494}
495
496/* 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