VirtualBox

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

Last change on this file since 53517 was 51848, checked in by vboxsync, 10 years ago

Main/NetworkAdapterImpl: fixed NAT network refcount regression introduced by r91364

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