VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/NetworkAdapterImpl.cpp@ 67954

Last change on this file since 67954 was 67804, checked in by vboxsync, 7 years ago

Main/Net (bugref:8905) canonicalize bridged interface name upon modification

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.1 KB
Line 
1/* $Id: NetworkAdapterImpl.cpp 67804 2017-07-05 14:53:27Z vboxsync $ */
2/** @file
3 * Implementation of INetworkAdapter in VBoxSVC.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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 "NetworkAdapterImpl.h"
19#include "NATEngineImpl.h"
20#include "AutoCaller.h"
21#include "Logging.h"
22#include "MachineImpl.h"
23#include "GuestOSTypeImpl.h"
24#include "HostImpl.h"
25#include "SystemPropertiesImpl.h"
26#include "VirtualBoxImpl.h"
27
28#include <iprt/ctype.h>
29#include <iprt/string.h>
30#include <iprt/cpp/utils.h>
31
32#include <VBox/err.h>
33#include <VBox/settings.h>
34
35#include "AutoStateDep.h"
36
37// constructor / destructor
38////////////////////////////////////////////////////////////////////////////////
39
40NetworkAdapter::NetworkAdapter()
41 : mParent(NULL)
42{
43}
44
45NetworkAdapter::~NetworkAdapter()
46{
47}
48
49HRESULT NetworkAdapter::FinalConstruct()
50{
51 return BaseFinalConstruct();
52}
53
54void NetworkAdapter::FinalRelease()
55{
56 uninit();
57 BaseFinalRelease();
58}
59
60// public initializer/uninitializer for internal purposes only
61////////////////////////////////////////////////////////////////////////////////
62
63/**
64 * Initializes the network adapter object.
65 *
66 * @param aParent Handle of the parent object.
67 * @param uSlot Slot number this network adapter is plugged into.
68 */
69HRESULT NetworkAdapter::init(Machine *aParent, ULONG uSlot)
70{
71 LogFlowThisFunc(("aParent=%p, uSlot=%d\n", aParent, uSlot));
72
73 ComAssertRet(aParent, E_INVALIDARG);
74 uint32_t maxNetworkAdapters = Global::getMaxNetworkAdapters(aParent->i_getChipsetType());
75 ComAssertRet(uSlot < maxNetworkAdapters, E_INVALIDARG);
76
77 /* Enclose the state transition NotReady->InInit->Ready */
78 AutoInitSpan autoInitSpan(this);
79 AssertReturn(autoInitSpan.isOk(), E_FAIL);
80
81 unconst(mParent) = aParent;
82 unconst(mNATEngine).createObject();
83 mNATEngine->init(aParent, this);
84 /* mPeer is left null */
85
86 mData.allocate();
87
88 /* initialize data */
89 mData->ulSlot = uSlot;
90
91 /* default to Am79C973 */
92 mData->type = NetworkAdapterType_Am79C973;
93
94 /* Confirm a successful initialization */
95 autoInitSpan.setSucceeded();
96
97 return S_OK;
98}
99
100/**
101 * Initializes the network adapter object given another network adapter object
102 * (a kind of copy constructor). This object shares data with
103 * the object passed as an argument.
104 *
105 * @param aParent Parent object.
106 * @param aThat
107 * @param aReshare
108 * When false, the original object will remain a data owner.
109 * Otherwise, data ownership will be transferred from the original
110 * object to this one.
111 *
112 * @note This object must be destroyed before the original object
113 * it shares data with is destroyed.
114 *
115 * @note Locks @a aThat object for reading.
116 */
117HRESULT NetworkAdapter::init(Machine *aParent, NetworkAdapter *aThat, bool aReshare /* = false */)
118{
119 LogFlowThisFunc(("aParent=%p, aThat=%p, aReshare=%RTbool\n", aParent, aThat, aReshare));
120
121 ComAssertRet(aParent && aThat, E_INVALIDARG);
122
123 /* Enclose the state transition NotReady->InInit->Ready */
124 AutoInitSpan autoInitSpan(this);
125 AssertReturn(autoInitSpan.isOk(), E_FAIL);
126
127 unconst(mParent) = aParent;
128 /* mPeer is left null */
129
130 unconst(mNATEngine).createObject();
131 mNATEngine->init(aParent, this, aThat->mNATEngine);
132
133 /* sanity */
134 AutoCaller thatCaller(aThat);
135 AssertComRCReturnRC(thatCaller.rc());
136
137 if (aReshare)
138 {
139 AutoWriteLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
140
141 unconst(aThat->mPeer) = this;
142 mData.attach(aThat->mData);
143 }
144 else
145 {
146 unconst(mPeer) = aThat;
147
148 AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
149 mData.share(aThat->mData);
150 }
151
152 /* Confirm a successful initialization */
153 autoInitSpan.setSucceeded();
154
155 return S_OK;
156}
157
158/**
159 * Initializes the guest object given another guest object
160 * (a kind of copy constructor). This object makes a private copy of data
161 * of the original object passed as an argument.
162 *
163 * @note Locks @a aThat object for reading.
164 */
165HRESULT NetworkAdapter::initCopy(Machine *aParent, NetworkAdapter *aThat)
166{
167 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
168
169 ComAssertRet(aParent && aThat, E_INVALIDARG);
170
171 /* Enclose the state transition NotReady->InInit->Ready */
172 AutoInitSpan autoInitSpan(this);
173 AssertReturn(autoInitSpan.isOk(), E_FAIL);
174
175 unconst(mParent) = aParent;
176 /* mPeer is left null */
177
178 unconst(mNATEngine).createObject();
179 mNATEngine->initCopy(aParent, this, aThat->mNATEngine);
180
181 /* sanity */
182 AutoCaller thatCaller(aThat);
183 AssertComRCReturnRC(thatCaller.rc());
184
185 AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
186 mData.attachCopy(aThat->mData);
187
188 /* Confirm a successful initialization */
189 autoInitSpan.setSucceeded();
190
191 return S_OK;
192}
193
194/**
195 * Uninitializes the instance and sets the ready flag to FALSE.
196 * Called either from FinalRelease() or by the parent when it gets destroyed.
197 */
198void NetworkAdapter::uninit()
199{
200 LogFlowThisFunc(("\n"));
201
202 /* Enclose the state transition Ready->InUninit->NotReady */
203 AutoUninitSpan autoUninitSpan(this);
204 if (autoUninitSpan.uninitDone())
205 return;
206
207 mData.free();
208
209 unconst(mNATEngine).setNull();
210 unconst(mPeer) = NULL;
211 unconst(mParent) = NULL;
212}
213
214// wrapped INetworkAdapter properties
215////////////////////////////////////////////////////////////////////////////////
216HRESULT NetworkAdapter::getAdapterType(NetworkAdapterType_T *aAdapterType)
217{
218 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
219
220 *aAdapterType = mData->type;
221
222 return S_OK;
223}
224
225HRESULT NetworkAdapter::setAdapterType(NetworkAdapterType_T aAdapterType)
226{
227 /* the machine needs to be mutable */
228 AutoMutableStateDependency adep(mParent);
229 if (FAILED(adep.rc())) return adep.rc();
230
231 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
232
233 /* make sure the value is allowed */
234 switch (aAdapterType)
235 {
236 case NetworkAdapterType_Am79C970A:
237 case NetworkAdapterType_Am79C973:
238#ifdef VBOX_WITH_E1000
239 case NetworkAdapterType_I82540EM:
240 case NetworkAdapterType_I82543GC:
241 case NetworkAdapterType_I82545EM:
242#endif
243#ifdef VBOX_WITH_VIRTIO
244 case NetworkAdapterType_Virtio:
245#endif /* VBOX_WITH_VIRTIO */
246 break;
247 default:
248 return setError(E_FAIL,
249 tr("Invalid network adapter type '%d'"),
250 aAdapterType);
251 }
252
253 if (mData->type != aAdapterType)
254 {
255 mData.backup();
256 mData->type = aAdapterType;
257
258 // leave the lock before informing callbacks
259 alock.release();
260
261 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
262 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
263 mlock.release();
264
265 /* Changing the network adapter type during runtime is not allowed,
266 * therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
267 mParent->i_onNetworkAdapterChange(this, FALSE);
268 }
269
270 return S_OK;
271}
272
273
274HRESULT NetworkAdapter::getSlot(ULONG *uSlot)
275{
276 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
277
278 *uSlot = mData->ulSlot;
279
280 return S_OK;
281}
282
283HRESULT NetworkAdapter::getEnabled(BOOL *aEnabled)
284{
285 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
286
287 *aEnabled = mData->fEnabled;
288
289 return S_OK;
290}
291
292HRESULT NetworkAdapter::setEnabled(BOOL aEnabled)
293{
294 /* the machine needs to be mutable */
295 AutoMutableStateDependency adep(mParent);
296 if (FAILED(adep.rc())) return adep.rc();
297
298 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
299
300 if (mData->fEnabled != RT_BOOL(aEnabled))
301 {
302 mData.backup();
303 mData->fEnabled = RT_BOOL(aEnabled);
304 if (RT_BOOL(aEnabled) && mData->strMACAddress.isEmpty())
305 i_generateMACAddress();
306
307 // leave the lock before informing callbacks
308 alock.release();
309
310 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
311 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
312 mlock.release();
313
314 /* Disabling the network adapter during runtime is not allowed
315 * therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
316 mParent->i_onNetworkAdapterChange(this, FALSE);
317 }
318
319 return S_OK;
320}
321
322HRESULT NetworkAdapter::getMACAddress(com::Utf8Str &aMACAddress)
323{
324 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
325
326 ComAssertRet(!mData->fEnabled || !mData->strMACAddress.isEmpty(), E_FAIL);
327
328 aMACAddress = mData->strMACAddress;
329
330 return S_OK;
331}
332
333HRESULT NetworkAdapter::i_updateMacAddress(Utf8Str aMACAddress)
334{
335 HRESULT rc = S_OK;
336
337 /*
338 * Are we supposed to generate a MAC?
339 */
340 if (mData->fEnabled && aMACAddress.isEmpty())
341 i_generateMACAddress();
342 else
343 {
344 if (mData->strMACAddress != aMACAddress)
345 {
346 if (mData->fEnabled || !aMACAddress.isEmpty())
347 {
348 /*
349 * Verify given MAC address
350 */
351 char *macAddressStr = aMACAddress.mutableRaw();
352 int i = 0;
353 while ((i < 13) && macAddressStr && *macAddressStr && (rc == S_OK))
354 {
355 char c = *macAddressStr;
356 /* canonicalize hex digits to capital letters */
357 if (c >= 'a' && c <= 'f')
358 {
359 c = (char)RTLocCToUpper(c);
360 *macAddressStr = c;
361 }
362 /* we only accept capital letters */
363 if ( (c < '0' || c > '9')
364 && (c < 'A' || c > 'F'))
365 rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
366 /* the second digit must have even value for unicast addresses */
367 if ( (i == 1)
368 && (!!(c & 1) == (c >= '0' && c <= '9')))
369 rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
370
371 macAddressStr++;
372 i++;
373 }
374 /* we must have parsed exactly 12 characters */
375 if (i != 12)
376 rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
377 }
378
379 if (SUCCEEDED(rc))
380 mData->strMACAddress = aMACAddress;
381 }
382 }
383
384 return rc;
385}
386
387HRESULT NetworkAdapter::setMACAddress(const com::Utf8Str &aMACAddress)
388{
389 /* the machine needs to be mutable */
390 AutoMutableStateDependency adep(mParent);
391 if (FAILED(adep.rc())) return adep.rc();
392
393 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
394 mData.backup();
395
396 HRESULT rc = i_updateMacAddress(aMACAddress);
397 if (SUCCEEDED(rc))
398 {
399 // leave the lock before informing callbacks
400 alock.release();
401
402 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
403 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
404 mlock.release();
405
406 /* Changing the MAC via the Main API during runtime is not allowed,
407 * therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
408 mParent->i_onNetworkAdapterChange(this, FALSE);
409 }
410
411 return rc;
412}
413
414HRESULT NetworkAdapter::getAttachmentType(NetworkAttachmentType_T *aAttachmentType)
415{
416 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
417
418 *aAttachmentType = mData->mode;
419
420 return S_OK;
421}
422
423HRESULT NetworkAdapter::setAttachmentType(NetworkAttachmentType_T aAttachmentType)
424{
425 /* the machine needs to be mutable */
426 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
427 if (FAILED(adep.rc())) return adep.rc();
428
429 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
430
431 if (mData->mode != aAttachmentType)
432 {
433 mData.backup();
434
435 /* there must an internal network name */
436 if (mData->strInternalNetworkName.isEmpty())
437 {
438 Log(("Internal network name not defined, setting to default \"intnet\"\n"));
439 mData->strInternalNetworkName = "intnet";
440 }
441
442 /* there must a NAT network name */
443 if (mData->strNATNetworkName.isEmpty())
444 {
445 Log(("NAT network name not defined, setting to default \"NatNetwork\"\n"));
446 mData->strNATNetworkName = "NatNetwork";
447 }
448
449 NetworkAttachmentType_T oldAttachmentType = mData->mode;
450 mData->mode = aAttachmentType;
451
452 // leave the lock before informing callbacks
453 alock.release();
454
455 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
456 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
457 mlock.release();
458
459 if (oldAttachmentType == NetworkAttachmentType_NATNetwork)
460 i_switchFromNatNetworking(mData->strNATNetworkName);
461
462 if (aAttachmentType == NetworkAttachmentType_NATNetwork)
463 i_switchToNatNetworking(mData->strNATNetworkName);
464
465 /* Adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
466 mParent->i_onNetworkAdapterChange(this, TRUE);
467 }
468
469 return S_OK;
470}
471
472HRESULT NetworkAdapter::getBridgedInterface(com::Utf8Str &aBridgedInterface)
473{
474 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
475
476 aBridgedInterface = mData->strBridgedName;
477
478 return S_OK;
479}
480
481HRESULT NetworkAdapter::setBridgedInterface(const com::Utf8Str &aBridgedInterface)
482{
483 /* the machine needs to be mutable */
484 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
485 if (FAILED(adep.rc())) return adep.rc();
486
487 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
488
489 Bstr canonicalName = aBridgedInterface;
490#ifdef RT_OS_DARWIN
491 com::SafeIfaceArray<IHostNetworkInterface> hostNetworkInterfaces;
492 ComPtr<IHost> host;
493 HRESULT rc = mParent->i_getVirtualBox()->COMGETTER(Host)(host.asOutParam());
494 if (SUCCEEDED(rc))
495 {
496 host->FindHostNetworkInterfacesOfType(HostNetworkInterfaceType_Bridged,
497 ComSafeArrayAsOutParam(hostNetworkInterfaces));
498 for (size_t i = 0; i < hostNetworkInterfaces.size(); ++i)
499 {
500 Bstr shortName;
501 ComPtr<IHostNetworkInterface> ni = hostNetworkInterfaces[i];
502 ni->COMGETTER(ShortName)(shortName.asOutParam());
503 if (shortName == aBridgedInterface)
504 {
505 ni->COMGETTER(Name)(canonicalName.asOutParam());
506 break;
507 }
508 }
509 }
510#endif /* RT_OS_DARWIN */
511 if (Bstr(mData->strBridgedName) != canonicalName)
512 {
513 /* if an empty/null string is to be set, bridged interface must be
514 * turned off */
515 if (canonicalName.isEmpty()
516 && mData->mode == NetworkAttachmentType_Bridged)
517 {
518 return setError(E_FAIL,
519 tr("Empty or null bridged interface name is not valid"));
520 }
521
522 mData.backup();
523 mData->strBridgedName = canonicalName;
524
525 // leave the lock before informing callbacks
526 alock.release();
527
528 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
529 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
530 mlock.release();
531
532 /* When changing the host adapter, adapt the CFGM logic to make this
533 * change immediately effect and to notify the guest that the network
534 * might have changed, therefore changeAdapter=TRUE. */
535 mParent->i_onNetworkAdapterChange(this, TRUE);
536 }
537
538 return S_OK;
539}
540
541HRESULT NetworkAdapter::getHostOnlyInterface(com::Utf8Str &aHostOnlyInterface)
542{
543 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
544
545 aHostOnlyInterface = mData->strHostOnlyName;
546
547 return S_OK;
548}
549
550HRESULT NetworkAdapter::setHostOnlyInterface(const com::Utf8Str &aHostOnlyInterface)
551{
552 /* the machine needs to be mutable */
553 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
554 if (FAILED(adep.rc())) return adep.rc();
555
556 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
557
558 if (mData->strHostOnlyName != aHostOnlyInterface)
559 {
560 /* if an empty/null string is to be set, host only interface must be
561 * turned off */
562 if ( aHostOnlyInterface.isEmpty()
563 && mData->mode == NetworkAttachmentType_HostOnly)
564 {
565 return setError(E_FAIL,
566 tr("Empty or null host only interface name is not valid"));
567 }
568
569 mData.backup();
570 mData->strHostOnlyName = aHostOnlyInterface;
571
572 // leave the lock before informing callbacks
573 alock.release();
574
575 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
576 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
577 mlock.release();
578
579 /* When changing the host adapter, adapt the CFGM logic to make this
580 * change immediately effect and to notify the guest that the network
581 * might have changed, therefore changeAdapter=TRUE. */
582 mParent->i_onNetworkAdapterChange(this, TRUE);
583 }
584
585 return S_OK;
586}
587
588
589HRESULT NetworkAdapter::getInternalNetwork(com::Utf8Str &aInternalNetwork)
590{
591 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
592
593 aInternalNetwork = mData->strInternalNetworkName;
594
595 return S_OK;
596}
597
598HRESULT NetworkAdapter::setInternalNetwork(const com::Utf8Str &aInternalNetwork)
599{
600 /* the machine needs to be mutable */
601 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
602 if (FAILED(adep.rc())) return adep.rc();
603
604 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
605
606 if (mData->strInternalNetworkName != aInternalNetwork)
607 {
608 /* if an empty/null string is to be set, internal networking must be
609 * turned off */
610 if (aInternalNetwork.isEmpty() && mData->mode == NetworkAttachmentType_Internal)
611 {
612 return setError(E_FAIL,
613 tr("Empty or null internal network name is not valid"));
614 }
615 mData.backup();
616 mData->strInternalNetworkName = aInternalNetwork;
617
618 // leave the lock before informing callbacks
619 alock.release();
620
621 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
622 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
623 mlock.release();
624
625 /* When changing the internal network, adapt the CFGM logic to make this
626 * change immediately effect and to notify the guest that the network
627 * might have changed, therefore changeAdapter=TRUE. */
628 mParent->i_onNetworkAdapterChange(this, TRUE);
629 }
630
631 return S_OK;
632}
633
634HRESULT NetworkAdapter::getNATNetwork(com::Utf8Str &aNATNetwork)
635{
636 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
637
638 aNATNetwork = mData->strNATNetworkName;
639
640 return S_OK;
641}
642
643
644HRESULT NetworkAdapter::setNATNetwork(const com::Utf8Str &aNATNetwork)
645{
646 /* the machine needs to be mutable */
647 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
648 if (FAILED(adep.rc())) return adep.rc();
649
650 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
651
652 if (mData->strNATNetworkName != aNATNetwork)
653 {
654 /* if an empty/null string is to be set, host only interface must be
655 * turned off */
656 if (aNATNetwork.isEmpty()
657 && mData->mode == NetworkAttachmentType_NATNetwork)
658 return setError(E_FAIL,
659 tr("Empty or null NAT network name is not valid"));
660
661 mData.backup();
662
663 Bstr oldNatNetworkName = mData->strNATNetworkName;
664 mData->strNATNetworkName = aNATNetwork;
665
666 // leave the lock before informing callbacks
667 alock.release();
668
669 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
670 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
671 mlock.release();
672
673 if (mData->mode == NetworkAttachmentType_NATNetwork)
674 {
675 i_switchFromNatNetworking(oldNatNetworkName.raw());
676 i_switchToNatNetworking(aNATNetwork);
677 }
678
679 /* When changing the host adapter, adapt the CFGM logic to make this
680 * change immediately effect and to notify the guest that the network
681 * might have changed, therefore changeAdapter=TRUE. */
682 mParent->i_onNetworkAdapterChange(this, TRUE);
683 }
684
685 return S_OK;
686}
687
688HRESULT NetworkAdapter::getGenericDriver(com::Utf8Str &aGenericDriver)
689{
690 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
691
692 aGenericDriver = mData->strGenericDriver;
693
694 return S_OK;
695}
696
697HRESULT NetworkAdapter::setGenericDriver(const com::Utf8Str &aGenericDriver)
698{
699 /* the machine needs to be mutable */
700 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
701 if (FAILED(adep.rc())) return adep.rc();
702
703 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
704
705 if (mData->strGenericDriver != aGenericDriver)
706 {
707 mData.backup();
708 mData->strGenericDriver = aGenericDriver;
709
710 /* leave the lock before informing callbacks */
711 alock.release();
712
713 mParent->i_onNetworkAdapterChange(this, FALSE);
714 }
715
716 return S_OK;
717}
718
719
720HRESULT NetworkAdapter::getCableConnected(BOOL *aConnected)
721{
722 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
723
724 *aConnected = mData->fCableConnected;
725
726 return S_OK;
727}
728
729
730HRESULT NetworkAdapter::setCableConnected(BOOL aConnected)
731{
732 /* the machine needs to be mutable */
733 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
734 if (FAILED(adep.rc())) return adep.rc();
735
736 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
737
738 if (RT_BOOL(aConnected) != mData->fCableConnected)
739 {
740 mData.backup();
741 mData->fCableConnected = RT_BOOL(aConnected);
742
743 // leave the lock before informing callbacks
744 alock.release();
745
746 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
747 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
748 mlock.release();
749
750 /* No change in CFGM logic => changeAdapter=FALSE. */
751 mParent->i_onNetworkAdapterChange(this, FALSE);
752 }
753
754 return S_OK;
755}
756
757
758HRESULT NetworkAdapter::getLineSpeed(ULONG *aSpeed)
759{
760 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
761
762 *aSpeed = mData->ulLineSpeed;
763
764 return S_OK;
765}
766
767HRESULT NetworkAdapter::setLineSpeed(ULONG aSpeed)
768{
769 /* the machine needs to be mutable */
770 AutoMutableStateDependency adep(mParent);
771 if (FAILED(adep.rc())) return adep.rc();
772
773 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
774
775 if (aSpeed != mData->ulLineSpeed)
776 {
777 mData.backup();
778 mData->ulLineSpeed = aSpeed;
779
780 // leave the lock before informing callbacks
781 alock.release();
782
783 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
784 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
785 mlock.release();
786
787 /* No change in CFGM logic => changeAdapter=FALSE. */
788 mParent->i_onNetworkAdapterChange(this, FALSE);
789 }
790
791 return S_OK;
792}
793
794HRESULT NetworkAdapter::getPromiscModePolicy(NetworkAdapterPromiscModePolicy_T *aPromiscModePolicy)
795{
796 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
797
798 *aPromiscModePolicy = mData->enmPromiscModePolicy;
799
800 return S_OK;
801}
802
803HRESULT NetworkAdapter::setPromiscModePolicy(NetworkAdapterPromiscModePolicy_T aPromiscModePolicy)
804{
805 /* the machine needs to be mutable */
806 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
807 if (FAILED(adep.rc())) return adep.rc();
808
809 switch (aPromiscModePolicy)
810 {
811 case NetworkAdapterPromiscModePolicy_Deny:
812 case NetworkAdapterPromiscModePolicy_AllowNetwork:
813 case NetworkAdapterPromiscModePolicy_AllowAll:
814 break;
815 default:
816 return setError(E_INVALIDARG, tr("Invalid promiscuous mode policy (%d)"), aPromiscModePolicy);
817 }
818
819 AutoCaller autoCaller(this);
820 HRESULT hrc = autoCaller.rc();
821
822 if (SUCCEEDED(hrc))
823 {
824 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
825 if (aPromiscModePolicy != mData->enmPromiscModePolicy)
826 {
827 mData.backup();
828 mData->enmPromiscModePolicy = aPromiscModePolicy;
829
830 alock.release();
831 mParent->i_setModifiedLock(Machine::IsModified_NetworkAdapters);
832 mParent->i_onNetworkAdapterChange(this, TRUE);
833 }
834 }
835
836 return hrc;
837}
838
839
840HRESULT NetworkAdapter::getTraceEnabled(BOOL *aEnabled)
841{
842
843 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
844
845 *aEnabled = mData->fTraceEnabled;
846
847 return S_OK;
848}
849
850HRESULT NetworkAdapter::setTraceEnabled(BOOL aEnabled)
851{
852 /* the machine needs to be mutable */
853 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
854 if (FAILED(adep.rc())) return adep.rc();
855
856 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
857
858 if (RT_BOOL(aEnabled) != mData->fTraceEnabled)
859 {
860 mData.backup();
861 mData->fTraceEnabled = RT_BOOL(aEnabled);
862
863 // leave the lock before informing callbacks
864 alock.release();
865
866 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
867 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
868 mlock.release();
869
870 /* Adapt the CFGM logic changeAdapter=TRUE */
871 mParent->i_onNetworkAdapterChange(this, TRUE);
872 }
873
874 return S_OK;
875}
876
877HRESULT NetworkAdapter::getTraceFile(com::Utf8Str &aTraceFile)
878{
879 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
880
881 aTraceFile = mData->strTraceFile;
882
883 return S_OK;
884}
885
886
887HRESULT NetworkAdapter::setTraceFile(const com::Utf8Str &aTraceFile)
888{
889 /* the machine needs to be mutable */
890 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
891 if (FAILED(adep.rc())) return adep.rc();
892
893 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
894
895 if (mData->strTraceFile != aTraceFile)
896 {
897 mData.backup();
898 mData->strTraceFile = aTraceFile;
899
900 // leave the lock before informing callbacks
901 alock.release();
902
903 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
904 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
905 mlock.release();
906
907 /* We change the 'File' => changeAdapter=TRUE. */
908 mParent->i_onNetworkAdapterChange(this, TRUE);
909 }
910
911 return S_OK;
912}
913
914HRESULT NetworkAdapter::getNATEngine(ComPtr<INATEngine> &aNATEngine)
915{
916 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
917
918 aNATEngine = mNATEngine;
919
920 return S_OK;
921}
922
923HRESULT NetworkAdapter::getBootPriority(ULONG *aBootPriority)
924{
925 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
926
927 *aBootPriority = mData->ulBootPriority;
928
929 return S_OK;
930}
931
932HRESULT NetworkAdapter::setBootPriority(ULONG aBootPriority)
933{
934 /* the machine needs to be mutable */
935 AutoMutableStateDependency adep(mParent);
936 if (FAILED(adep.rc())) return adep.rc();
937
938 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
939
940 if (aBootPriority != mData->ulBootPriority)
941 {
942 mData.backup();
943 mData->ulBootPriority = aBootPriority;
944
945 // leave the lock before informing callbacks
946 alock.release();
947
948 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
949 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
950 mlock.release();
951
952 /* No change in CFGM logic => changeAdapter=FALSE. */
953 mParent->i_onNetworkAdapterChange(this, FALSE);
954 }
955
956 return S_OK;
957}
958
959// wrapped INetworkAdapter methods
960////////////////////////////////////////////////////////////////////////////////
961
962HRESULT NetworkAdapter::getProperty(const com::Utf8Str &aKey, com::Utf8Str &aValue)
963{
964 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
965 aValue = "";
966 settings::StringsMap::const_iterator it = mData->genericProperties.find(aKey);
967 if (it != mData->genericProperties.end())
968 aValue = it->second; // source is a Utf8Str
969
970 return S_OK;
971}
972
973HRESULT NetworkAdapter::setProperty(const com::Utf8Str &aKey, const com::Utf8Str &aValue)
974{
975 LogFlowThisFunc(("\n"));
976 /* The machine needs to be mutable. */
977 AutoMutableOrSavedOrRunningStateDependency adep(mParent);
978 if (FAILED(adep.rc())) return adep.rc();
979 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
980 bool fGenericChange = (mData->mode == NetworkAttachmentType_Generic);
981 /* Generic properties processing.
982 * Look up the old value first; if nothing's changed then do nothing.
983 */
984 Utf8Str strOldValue;
985 settings::StringsMap::const_iterator it = mData->genericProperties.find(aKey);
986 if (it != mData->genericProperties.end())
987 strOldValue = it->second;
988
989 if (strOldValue != aValue)
990 {
991 if (aValue.isEmpty())
992 mData->genericProperties.erase(aKey);
993 else
994 mData->genericProperties[aKey] = aValue;
995
996 /* leave the lock before informing callbacks */
997 alock.release();
998
999 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS);
1000 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
1001 mlock.release();
1002
1003 /* Avoid deadlock when the event triggers a call to a method of this
1004 * interface. */
1005 adep.release();
1006
1007 mParent->i_onNetworkAdapterChange(this, fGenericChange);
1008 }
1009
1010 return S_OK;
1011}
1012
1013HRESULT NetworkAdapter::getProperties(const com::Utf8Str &aNames,
1014 std::vector<com::Utf8Str> &aReturnNames,
1015 std::vector<com::Utf8Str> &aReturnValues)
1016{
1017 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1018
1019 /// @todo make use of aNames according to the documentation
1020 NOREF(aNames);
1021 aReturnNames.resize(mData->genericProperties.size());
1022 aReturnValues.resize(mData->genericProperties.size());
1023
1024 size_t i = 0;
1025
1026 for (settings::StringsMap::const_iterator it = mData->genericProperties.begin();
1027 it != mData->genericProperties.end();
1028 ++it, ++i)
1029 {
1030 aReturnNames[i] = it->first;
1031 aReturnValues[i] = it->second;
1032 }
1033
1034 return S_OK;
1035}
1036
1037
1038
1039// public methods only for internal purposes
1040////////////////////////////////////////////////////////////////////////////////
1041
1042/**
1043 * Loads settings from the given adapter node.
1044 * May be called once right after this object creation.
1045 *
1046 * @param bwctl bandwidth control object.
1047 * @param data Configuration settings.
1048 *
1049 * @note Locks this object for writing.
1050 */
1051HRESULT NetworkAdapter::i_loadSettings(BandwidthControl *bwctl,
1052 const settings::NetworkAdapter &data)
1053{
1054 AutoCaller autoCaller(this);
1055 AssertComRCReturnRC(autoCaller.rc());
1056
1057 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1058
1059 /* Note: we assume that the default values for attributes of optional
1060 * nodes are assigned in the Data::Data() constructor and don't do it
1061 * here. It implies that this method may only be called after constructing
1062 * a new BIOSSettings object while all its data fields are in the default
1063 * values. Exceptions are fields whose creation time defaults don't match
1064 * values that should be applied when these fields are not explicitly set
1065 * in the settings file (for backwards compatibility reasons). This takes
1066 * place when a setting of a newly created object must default to A while
1067 * the same setting of an object loaded from the old settings file must
1068 * default to B. */
1069
1070 HRESULT rc = S_OK;
1071
1072 /* MAC address (can be null) */
1073 rc = i_updateMacAddress(data.strMACAddress);
1074 if (FAILED(rc)) return rc;
1075
1076 mData.assignCopy(&data);
1077
1078 if (mData->strBandwidthGroup.isNotEmpty())
1079 {
1080 ComObjPtr<BandwidthGroup> group;
1081 rc = bwctl->i_getBandwidthGroupByName(data.strBandwidthGroup, group, true);
1082 if (FAILED(rc)) return rc;
1083 group->i_reference();
1084 }
1085
1086 // Load NAT engine settings.
1087 mNATEngine->i_loadSettings(data.nat);
1088
1089 // leave the lock before setting attachment type
1090 alock.release();
1091
1092 rc = COMSETTER(AttachmentType)(data.mode);
1093 if (FAILED(rc)) return rc;
1094
1095 return S_OK;
1096}
1097
1098/**
1099 * Saves settings to the given adapter node.
1100 *
1101 * Note that the given Adapter node is completely empty on input.
1102 *
1103 * @param data Configuration settings.
1104 *
1105 * @note Locks this object for reading.
1106 */
1107HRESULT NetworkAdapter::i_saveSettings(settings::NetworkAdapter &data)
1108{
1109 AutoCaller autoCaller(this);
1110 AssertComRCReturnRC(autoCaller.rc());
1111
1112 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1113
1114 data = *mData.data();
1115
1116 mNATEngine->i_saveSettings(data.nat);
1117
1118 return S_OK;
1119}
1120
1121/**
1122 * Returns true if any setter method has modified settings of this instance.
1123 * @return
1124 */
1125bool NetworkAdapter::i_isModified() {
1126
1127 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1128
1129 bool fChanged = mData.isBackedUp();
1130 fChanged |= (mData->type == NetworkAttachmentType_NAT? mNATEngine->i_isModified() : false);
1131 return fChanged;
1132}
1133
1134/**
1135 * @note Locks this object for writing.
1136 */
1137void NetworkAdapter::i_rollback()
1138{
1139 /* sanity */
1140 AutoCaller autoCaller(this);
1141 AssertComRCReturnVoid(autoCaller.rc());
1142
1143 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1144
1145 mNATEngine->i_rollback();
1146
1147 mData.rollback();
1148}
1149
1150/**
1151 * @note Locks this object for writing, together with the peer object (also
1152 * for writing) if there is one.
1153 */
1154void NetworkAdapter::i_commit()
1155{
1156 /* sanity */
1157 AutoCaller autoCaller(this);
1158 AssertComRCReturnVoid(autoCaller.rc());
1159
1160 /* sanity too */
1161 AutoCaller peerCaller(mPeer);
1162 AssertComRCReturnVoid(peerCaller.rc());
1163
1164 /* lock both for writing since we modify both (mPeer is "master" so locked
1165 * first) */
1166 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
1167
1168 mNATEngine->i_commit();
1169
1170 if (mData.isBackedUp())
1171 {
1172 mData.commit();
1173 if (mPeer)
1174 {
1175 /* attach new data to the peer and reshare it */
1176 mPeer->mData.attach(mData);
1177 }
1178 }
1179}
1180
1181/**
1182 * @note Locks this object for writing, together with the peer object
1183 * represented by @a aThat (locked for reading).
1184 */
1185void NetworkAdapter::i_copyFrom(NetworkAdapter *aThat)
1186{
1187 AssertReturnVoid(aThat != NULL);
1188
1189 /* sanity */
1190 AutoCaller autoCaller(this);
1191 AssertComRCReturnVoid(autoCaller.rc());
1192
1193 /* sanity too */
1194 AutoCaller thatCaller(aThat);
1195 AssertComRCReturnVoid(thatCaller.rc());
1196
1197 mNATEngine->i_copyFrom(aThat->mNATEngine);
1198
1199 /* peer is not modified, lock it for reading (aThat is "master" so locked
1200 * first) */
1201 AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
1202 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
1203
1204 /* this will back up current data */
1205 mData.assignCopy(aThat->mData);
1206
1207}
1208
1209/**
1210 * Applies the defaults for this network adapter.
1211 *
1212 * @note This method currently assumes that the object is in the state after
1213 * calling init(), it does not set defaults from an arbitrary state.
1214 */
1215void NetworkAdapter::i_applyDefaults(GuestOSType *aOsType)
1216{
1217 /* sanity */
1218 AutoCaller autoCaller(this);
1219 AssertComRCReturnVoid(autoCaller.rc());
1220
1221 mNATEngine->i_applyDefaults();
1222
1223 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1224
1225 bool e1000enabled = false;
1226#ifdef VBOX_WITH_E1000
1227 e1000enabled = true;
1228#endif // VBOX_WITH_E1000
1229
1230 NetworkAdapterType_T defaultType = NetworkAdapterType_Am79C973;
1231 if (aOsType)
1232 defaultType = aOsType->i_networkAdapterType();
1233
1234 /* Set default network adapter for this OS type */
1235 if (defaultType == NetworkAdapterType_I82540EM ||
1236 defaultType == NetworkAdapterType_I82543GC ||
1237 defaultType == NetworkAdapterType_I82545EM)
1238 {
1239 if (e1000enabled)
1240 mData->type = defaultType;
1241 }
1242 else
1243 mData->type = defaultType;
1244
1245 /* Enable the first one adapter and set it to NAT */
1246 /** @todo r=klaus remove this long term, since a newly created VM should
1247 * have no additional hardware components unless configured either
1248 * explicitly or through Machine::applyDefaults. */
1249 if (aOsType && mData->ulSlot == 0)
1250 {
1251 mData->fEnabled = true;
1252 if (mData->strMACAddress.isEmpty())
1253 i_generateMACAddress();
1254 mData->mode = NetworkAttachmentType_NAT;
1255 }
1256 mData->fCableConnected = true;
1257}
1258
1259bool NetworkAdapter::i_hasDefaults()
1260{
1261 /* sanity */
1262 AutoCaller autoCaller(this);
1263 AssertComRCReturn(autoCaller.rc(), true);
1264
1265 ComObjPtr<GuestOSType> pGuestOSType;
1266 HRESULT rc = mParent->i_getVirtualBox()->i_findGuestOSType(mParent->i_getOSTypeId(),
1267 pGuestOSType);
1268 if (FAILED(rc))
1269 return false;
1270
1271 NetworkAdapterType_T defaultType = pGuestOSType->i_networkAdapterType();
1272
1273 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1274
1275 if ( !mData->fEnabled
1276 && mData->strMACAddress.isEmpty()
1277 && mData->type == defaultType
1278 && mData->fCableConnected
1279 && mData->ulLineSpeed == 0
1280 && mData->enmPromiscModePolicy == NetworkAdapterPromiscModePolicy_Deny
1281 && mData->mode == NetworkAttachmentType_Null
1282 && mData->strBridgedName.isEmpty()
1283 && mData->strInternalNetworkName.isEmpty()
1284 && mData->strHostOnlyName.isEmpty()
1285 && mData->strNATNetworkName.isEmpty()
1286 && mData->strGenericDriver.isEmpty()
1287 && mData->genericProperties.size() == 0)
1288 {
1289 /* Could be default, check NAT defaults. */
1290 return mNATEngine->i_hasDefaults();
1291 }
1292
1293 return false;
1294}
1295
1296ComObjPtr<NetworkAdapter> NetworkAdapter::i_getPeer()
1297{
1298 return mPeer;
1299}
1300
1301
1302// private methods
1303////////////////////////////////////////////////////////////////////////////////
1304
1305/**
1306 * Generates a new unique MAC address based on our vendor ID and
1307 * parts of a GUID.
1308 *
1309 * @note Must be called from under the object's write lock or within the init
1310 * span.
1311 */
1312void NetworkAdapter::i_generateMACAddress()
1313{
1314 Utf8Str mac;
1315 Host::i_generateMACAddress(mac);
1316 LogFlowThisFunc(("generated MAC: '%s'\n", mac.c_str()));
1317 mData->strMACAddress = mac;
1318}
1319
1320HRESULT NetworkAdapter::getBandwidthGroup(ComPtr<IBandwidthGroup> &aBandwidthGroup)
1321{
1322 LogFlowThisFuncEnter();
1323
1324 HRESULT hrc = S_OK;
1325
1326 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1327
1328 if (mData->strBandwidthGroup.isNotEmpty())
1329 {
1330 ComObjPtr<BandwidthGroup> pBwGroup;
1331 hrc = mParent->i_getBandwidthGroup(mData->strBandwidthGroup, pBwGroup, true /* fSetError */);
1332
1333 Assert(SUCCEEDED(hrc)); /* This is not allowed to fail because the existence
1334 * of the group was checked when it was attached. */
1335 if (SUCCEEDED(hrc))
1336 pBwGroup.queryInterfaceTo(aBandwidthGroup.asOutParam());
1337 }
1338
1339 LogFlowThisFuncLeave();
1340 return hrc;
1341}
1342
1343HRESULT NetworkAdapter::setBandwidthGroup(const ComPtr<IBandwidthGroup> &aBandwidthGroup)
1344{
1345 LogFlowThisFuncEnter();
1346
1347 /* the machine needs to be mutable */
1348 AutoMutableOrSavedStateDependency adep(mParent);
1349 if (FAILED(adep.rc())) return adep.rc();
1350
1351 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1352
1353 IBandwidthGroup *iBw = aBandwidthGroup;
1354 Utf8Str strBwGroup;
1355 if (aBandwidthGroup)
1356 strBwGroup = static_cast<BandwidthGroup *>(iBw)->i_getName();
1357
1358 if (mData->strBandwidthGroup != strBwGroup)
1359 {
1360 ComObjPtr<BandwidthGroup> pBwGroup;
1361 if (!strBwGroup.isEmpty())
1362 {
1363 HRESULT hrc = mParent->i_getBandwidthGroup(strBwGroup, pBwGroup, false /* fSetError */);
1364 NOREF(hrc);
1365 Assert(SUCCEEDED(hrc)); /* This is not allowed to fail because the existence
1366 of the group was checked when it was attached. */
1367 }
1368
1369 i_updateBandwidthGroup(pBwGroup);
1370
1371 // leave the lock before informing callbacks
1372 alock.release();
1373
1374 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS);
1375 mParent->i_setModified(Machine::IsModified_NetworkAdapters);
1376 mlock.release();
1377
1378 /** @todo changeAdapter=???. */
1379 mParent->i_onNetworkAdapterChange(this, FALSE);
1380 }
1381
1382 LogFlowThisFuncLeave();
1383 return S_OK;
1384}
1385
1386void NetworkAdapter::i_updateBandwidthGroup(BandwidthGroup *aBwGroup)
1387{
1388 LogFlowThisFuncEnter();
1389 Assert(isWriteLockOnCurrentThread());
1390
1391 ComObjPtr<BandwidthGroup> pOldBwGroup;
1392 if (!mData->strBandwidthGroup.isEmpty())
1393 {
1394 HRESULT hrc = mParent->i_getBandwidthGroup(mData->strBandwidthGroup, pOldBwGroup, false /* fSetError */);
1395 NOREF(hrc);
1396 Assert(SUCCEEDED(hrc)); /* This is not allowed to fail because the existence of
1397 the group was checked when it was attached. */
1398 }
1399
1400 mData.backup();
1401 if (!pOldBwGroup.isNull())
1402 {
1403 pOldBwGroup->i_release();
1404 mData->strBandwidthGroup = Utf8Str::Empty;
1405 }
1406
1407 if (aBwGroup)
1408 {
1409 mData->strBandwidthGroup = aBwGroup->i_getName();
1410 aBwGroup->i_reference();
1411 }
1412
1413 LogFlowThisFuncLeave();
1414}
1415
1416
1417HRESULT NetworkAdapter::i_switchFromNatNetworking(const com::Utf8Str &networkName)
1418{
1419 HRESULT hrc;
1420 MachineState_T state;
1421
1422 hrc = mParent->COMGETTER(State)(&state);
1423 if (FAILED(hrc))
1424 return hrc;
1425
1426 if ( state == MachineState_Running
1427 || state == MachineState_Paused)
1428 {
1429 Bstr bstrName;
1430 hrc = mParent->COMGETTER(Name)(bstrName.asOutParam());
1431 LogRel(("VM '%ls' stops using NAT network '%s'\n", bstrName.raw(), networkName.c_str()));
1432 int natCount = mParent->i_getVirtualBox()->i_natNetworkRefDec(Bstr(networkName).raw());
1433 if (natCount == -1)
1434 return E_INVALIDARG; /* no such network */
1435 }
1436
1437 return S_OK;
1438}
1439
1440
1441HRESULT NetworkAdapter::i_switchToNatNetworking(const com::Utf8Str &aNatNetworkName)
1442{
1443 HRESULT hrc;
1444 MachineState_T state;
1445
1446 hrc = mParent->COMGETTER(State)(&state);
1447 if (FAILED(hrc))
1448 return hrc;
1449
1450 if ( state == MachineState_Running
1451 || state == MachineState_Paused)
1452 {
1453 Bstr bstrName;
1454 hrc = mParent->COMGETTER(Name)(bstrName.asOutParam());
1455 LogRel(("VM '%ls' starts using NAT network '%s'\n", bstrName.raw(), aNatNetworkName.c_str()));
1456 int natCount = mParent->i_getVirtualBox()->i_natNetworkRefInc(Bstr(aNatNetworkName).raw());
1457 if (natCount == -1)
1458 return E_INVALIDARG; /* not found */
1459 }
1460
1461 return S_OK;
1462}
1463/* 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