VirtualBox

source: vbox/trunk/src/VBox/Main/NetworkAdapterImpl.cpp@ 2705

Last change on this file since 2705 was 1168, checked in by vboxsync, 18 years ago

Main: Fixed: The Snapshot ID was not reset on a normal hard disk object after discarding the last snapshot.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.9 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include "NetworkAdapterImpl.h"
23#include "Logging.h"
24#include "MachineImpl.h"
25
26#include <VBox/err.h>
27#include <iprt/string.h>
28
29// defines
30////////////////////////////////////////////////////////////////////////////////
31
32// constructor / destructor
33////////////////////////////////////////////////////////////////////////////////
34
35HRESULT NetworkAdapter::FinalConstruct()
36{
37 return S_OK;
38}
39
40void NetworkAdapter::FinalRelease()
41{
42 if (isReady())
43 uninit ();
44}
45
46// public initializer/uninitializer for internal purposes only
47////////////////////////////////////////////////////////////////////////////////
48
49/**
50 * Initializes the network adapter object.
51 *
52 * @param parent handle of our parent object
53 * @return COM result indicator
54 */
55HRESULT NetworkAdapter::init (Machine *parent, ULONG slot)
56{
57 LogFlowMember (("NetworkAdapter::init (%p): slot=%d\n", parent, slot));
58
59 ComAssertRet (parent, E_INVALIDARG);
60 ComAssertRet (slot < SchemaDefs::NetworkAdapterCount, E_INVALIDARG);
61
62 AutoLock alock (this);
63 ComAssertRet (!isReady(), E_UNEXPECTED);
64
65 mParent = parent;
66 // mPeer is left null
67
68 mData.allocate();
69
70 // initialize data
71 mData->mSlot = slot;
72
73 // default to Am79C973
74 mData->mAdapterType = NetworkAdapterType_NetworkAdapterAm79C973;
75
76 // generate the MAC address early to guarantee it is the same both after
77 // changing some other property (i.e. after mData.backup()) and after the
78 // subsequent mData.rollback().
79 generateMACAddress();
80
81 setReady (true);
82 return S_OK;
83}
84
85/**
86 * Initializes the network adapter object given another network adapter object
87 * (a kind of copy constructor). This object shares data with
88 * the object passed as an argument.
89 *
90 * @note This object must be destroyed before the original object
91 * it shares data with is destroyed.
92 */
93HRESULT NetworkAdapter::init (Machine *parent, NetworkAdapter *that)
94{
95 LogFlowMember (("NetworkAdapter::init (%p, %p)\n", parent, that));
96
97 ComAssertRet (parent && that, E_INVALIDARG);
98
99 AutoLock alock (this);
100 ComAssertRet (!isReady(), E_UNEXPECTED);
101
102 mParent = parent;
103 mPeer = that;
104
105 AutoLock thatlock (that);
106 mData.share (that->mData);
107
108 setReady (true);
109 return S_OK;
110}
111
112/**
113 * Initializes the guest object given another guest object
114 * (a kind of copy constructor). This object makes a private copy of data
115 * of the original object passed as an argument.
116 */
117HRESULT NetworkAdapter::initCopy (Machine *parent, NetworkAdapter *that)
118{
119 LogFlowMember (("NetworkAdapter::initCopy (%p, %p)\n", parent, that));
120
121 ComAssertRet (parent && that, E_INVALIDARG);
122
123 AutoLock alock (this);
124 ComAssertRet (!isReady(), E_UNEXPECTED);
125
126 mParent = parent;
127 // mPeer is left null
128
129 AutoLock thatlock (that);
130 mData.attachCopy (that->mData);
131
132 setReady (true);
133 return S_OK;
134}
135
136/**
137 * Uninitializes the instance and sets the ready flag to FALSE.
138 * Called either from FinalRelease() or by the parent when it gets destroyed.
139 */
140void NetworkAdapter::uninit()
141{
142 LogFlowMember (("INetworkAdapter::uninit()\n"));
143
144 AutoLock alock (this);
145 AssertReturn (isReady(), (void) 0);
146
147 mData.free();
148
149 mPeer.setNull();
150 mParent.setNull();
151
152 setReady (false);
153}
154
155// INetworkAdapter properties
156////////////////////////////////////////////////////////////////////////////////
157
158STDMETHODIMP NetworkAdapter::COMGETTER(AdapterType) (NetworkAdapterType_T *adapterType)
159{
160 if (!adapterType)
161 return E_POINTER;
162
163 AutoLock alock (this);
164 CHECK_READY();
165
166 *adapterType = mData->mAdapterType;
167 return S_OK;
168}
169
170STDMETHODIMP NetworkAdapter::COMSETTER(AdapterType) (NetworkAdapterType_T adapterType)
171{
172 AutoLock alock(this);
173 CHECK_READY();
174
175 CHECK_MACHINE_MUTABILITY (mParent);
176
177 /* make sure the value is allowed */
178 switch (adapterType)
179 {
180 case NetworkAdapterType_NetworkAdapterAm79C970A:
181 case NetworkAdapterType_NetworkAdapterAm79C973:
182 break;
183 default:
184 return setError(E_FAIL, tr("Invalid network adapter type '%d'"), adapterType);
185 }
186
187 if (mData->mAdapterType != adapterType)
188 {
189 mData.backup();
190 mData->mAdapterType = adapterType;
191
192 /* notify parent */
193 alock.unlock();
194 mParent->onNetworkAdapterChange (this);
195 }
196
197 return S_OK;
198}
199
200STDMETHODIMP NetworkAdapter::COMGETTER(Slot) (ULONG *slot)
201{
202 if (!slot)
203 return E_POINTER;
204
205 AutoLock alock (this);
206 CHECK_READY();
207
208 *slot = mData->mSlot;
209 return S_OK;
210}
211
212STDMETHODIMP NetworkAdapter::COMGETTER(Enabled) (BOOL *enabled)
213{
214 if (!enabled)
215 return E_POINTER;
216
217 AutoLock alock (this);
218 CHECK_READY();
219
220 *enabled = mData->mEnabled;
221 return S_OK;
222}
223
224STDMETHODIMP NetworkAdapter::COMSETTER(Enabled) (BOOL enabled)
225{
226 AutoLock alock(this);
227 CHECK_READY();
228
229 CHECK_MACHINE_MUTABILITY (mParent);
230
231 if (mData->mEnabled != enabled)
232 {
233 mData.backup();
234 mData->mEnabled = enabled;
235
236 /* notify parent */
237 alock.unlock();
238 mParent->onNetworkAdapterChange (this);
239 }
240
241 return S_OK;
242}
243
244/**
245 * Returns the MAC address string
246 *
247 * @returns COM status code
248 * @param macAddress address of result variable
249 */
250STDMETHODIMP NetworkAdapter::COMGETTER(MACAddress)(BSTR *macAddress)
251{
252 if (!macAddress)
253 return E_POINTER;
254
255 AutoLock alock(this);
256 CHECK_READY();
257
258 ComAssertRet (!!mData->mMACAddress, E_FAIL);
259
260 mData->mMACAddress.cloneTo (macAddress);
261
262 return S_OK;
263}
264
265/**
266 * Sets the MAC address
267 *
268 * @returns COM status code
269 * @param macAddress 12-digit hexadecimal MAC address string with
270 * capital letters. Can be NULL to generate a MAC
271 */
272STDMETHODIMP NetworkAdapter::COMSETTER(MACAddress)(INPTR BSTR macAddress)
273{
274 AutoLock alock(this);
275 CHECK_READY();
276
277 CHECK_MACHINE_MUTABILITY (mParent);
278
279 HRESULT rc = S_OK;
280 bool emitChangeEvent = false;
281
282 /*
283 * Are we supposed to generate a MAC?
284 */
285 if (!macAddress)
286 {
287 mData.backup();
288 generateMACAddress();
289 emitChangeEvent = true;
290 }
291 else
292 {
293 if (mData->mMACAddress != macAddress)
294 {
295 /*
296 * Verify given MAC address
297 */
298 Utf8Str macAddressUtf = macAddress;
299 char *macAddressStr = (char*)macAddressUtf.raw();
300 int i = 0;
301 while ((i < 12) && macAddressStr && (rc == S_OK))
302 {
303 char c = *macAddressStr;
304 /* we only accept capital letters */
305 if (((c < '0') || (c > '9')) &&
306 ((c < 'A') || (c > 'F')))
307 rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
308 macAddressStr++;
309 i++;
310 }
311 /* we must have parsed exactly 12 characters */
312 if (i != 12)
313 rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
314
315 if (SUCCEEDED (rc))
316 {
317 mData.backup();
318 mData->mMACAddress = macAddress;
319 emitChangeEvent = true;
320 }
321 }
322 }
323
324 if (emitChangeEvent)
325 {
326 /* notify parent */
327 alock.unlock();
328 mParent->onNetworkAdapterChange (this);
329 }
330
331 return rc;
332}
333
334/**
335 * Returns the attachment type
336 *
337 * @returns COM status code
338 * @param attachmentType address of result variable
339 */
340STDMETHODIMP NetworkAdapter::COMGETTER(AttachmentType)(NetworkAttachmentType_T *attachmentType)
341{
342 if (!attachmentType)
343 return E_POINTER;
344 AutoLock alock(this);
345 CHECK_READY();
346
347 *attachmentType = mData->mAttachmentType;
348
349 return S_OK;
350}
351
352/**
353 * Returns the host interface the adapter is attached to
354 *
355 * @returns COM status code
356 * @param hostInterface address of result string
357 */
358STDMETHODIMP NetworkAdapter::COMGETTER(HostInterface)(BSTR *hostInterface)
359{
360 if (!hostInterface)
361 return E_POINTER;
362 AutoLock alock(this);
363 CHECK_READY();
364
365 mData->mHostInterface.cloneTo(hostInterface);
366
367 return S_OK;
368}
369
370/**
371 * Sets the host interface device name.
372 *
373 * @returns COM status code
374 * @param hostInterface name of the host interface device in use
375 */
376STDMETHODIMP NetworkAdapter::COMSETTER(HostInterface)(INPTR BSTR hostInterface)
377{
378 /** @todo Validate input string length. r=dmik: do it in XML schema?*/
379
380#ifdef __WIN__
381 // we don't allow null strings for the host interface on Win32
382 // (because the @name attribute of <HostInerface> must be always present,
383 // but can be empty).
384 if (!hostInterface)
385 return E_INVALIDARG;
386#endif
387#ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
388 // empty strings are not allowed as path names
389 if (hostInterface && !(*hostInterface))
390 return E_INVALIDARG;
391#endif
392
393 AutoLock alock(this);
394 CHECK_READY();
395
396 CHECK_MACHINE_MUTABILITY (mParent);
397
398 if (mData->mHostInterface != hostInterface)
399 {
400 mData.backup();
401 mData->mHostInterface = hostInterface;
402
403 /* notify parent */
404 alock.unlock();
405 mParent->onNetworkAdapterChange(this);
406 }
407
408 return S_OK;
409}
410
411#ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
412/**
413 * Returns the TAP file descriptor the adapter is attached to
414 *
415 * @returns COM status code
416 * @param tapFileDescriptor address of result string
417 */
418STDMETHODIMP NetworkAdapter::COMGETTER(TAPFileDescriptor)(LONG *tapFileDescriptor)
419{
420 if (!tapFileDescriptor)
421 return E_POINTER;
422
423 AutoLock alock(this);
424 CHECK_READY();
425
426 *tapFileDescriptor = mData->mTAPFD;
427
428 return S_OK;
429}
430
431/**
432 * Sets the TAP file descriptor the adapter is attached to
433 *
434 * @returns COM status code
435 * @param tapFileDescriptor file descriptor of existing TAP interface
436 */
437STDMETHODIMP NetworkAdapter::COMSETTER(TAPFileDescriptor)(LONG tapFileDescriptor)
438{
439 /*
440 * Validate input.
441 */
442 RTFILE tapFD = tapFileDescriptor;
443 if (tapFD != NIL_RTFILE && (LONG)tapFD != tapFileDescriptor)
444 {
445 AssertMsgFailed(("Invalid file descriptor: %ld.\n", tapFileDescriptor));
446 return setError (E_INVALIDARG,
447 tr ("Invalid file descriptor: %ld"), tapFileDescriptor);
448 }
449
450 AutoLock alock(this);
451 CHECK_READY();
452
453 CHECK_MACHINE_MUTABILITY (mParent);
454
455 if (mData->mTAPFD != (RTFILE) tapFileDescriptor)
456 {
457 mData.backup();
458 mData->mTAPFD = tapFileDescriptor;
459
460 /* notify parent */
461 alock.unlock();
462 mParent->onNetworkAdapterChange(this);
463 }
464
465 return S_OK;
466}
467
468
469/**
470 * Returns the current TAP setup application path
471 *
472 * @returns COM status code
473 * @param tapSetupApplication address of result buffer
474 */
475STDMETHODIMP NetworkAdapter::COMGETTER(TAPSetupApplication)(BSTR *tapSetupApplication)
476{
477 if (!tapSetupApplication)
478 return E_POINTER;
479 AutoLock alock(this);
480 CHECK_READY();
481
482 /* we don't have to be in TAP mode to support this call */
483 mData->mTAPSetupApplication.cloneTo(tapSetupApplication);
484
485 return S_OK;
486}
487
488/**
489 * Stores a new TAP setup application
490 *
491 * @returns COM status code
492 * @param tapSetupApplication new TAP setup application path
493 */
494STDMETHODIMP NetworkAdapter::COMSETTER(TAPSetupApplication)(INPTR BSTR tapSetupApplication)
495{
496 // empty strings are not allowed as path names
497 if (tapSetupApplication && !(*tapSetupApplication))
498 return E_INVALIDARG;
499
500 AutoLock alock(this);
501 CHECK_READY();
502
503 CHECK_MACHINE_MUTABILITY (mParent);
504
505 if (mData->mTAPSetupApplication != tapSetupApplication)
506 {
507 mData.backup();
508 mData->mTAPSetupApplication = tapSetupApplication;
509
510 /* notify parent */
511 alock.unlock();
512 mParent->onNetworkAdapterChange(this);
513 }
514
515 return S_OK;
516}
517
518/**
519 * Returns the current TAP terminate application path
520 *
521 * @returns COM status code
522 * @param tapTerminateApplication address of result buffer
523 */
524STDMETHODIMP NetworkAdapter::COMGETTER(TAPTerminateApplication)(BSTR *tapTerminateApplication)
525{
526 if (!tapTerminateApplication)
527 return E_POINTER;
528 AutoLock alock(this);
529 CHECK_READY();
530
531 /* we don't have to be in TAP mode to support this call */
532 mData->mTAPTerminateApplication.cloneTo(tapTerminateApplication);
533
534 return S_OK;
535}
536
537/**
538 * Stores a new TAP terminate application
539 *
540 * @returns COM status code
541 * @param tapTerminateApplication new TAP terminate application path
542 */
543STDMETHODIMP NetworkAdapter::COMSETTER(TAPTerminateApplication)(INPTR BSTR tapTerminateApplication)
544{
545 // empty strings are not allowed as path names
546 if (tapTerminateApplication && !(*tapTerminateApplication))
547 return E_INVALIDARG;
548
549 AutoLock alock(this);
550 CHECK_READY();
551
552 CHECK_MACHINE_MUTABILITY (mParent);
553
554 if (mData->mTAPTerminateApplication != tapTerminateApplication)
555 {
556 mData.backup();
557 mData->mTAPTerminateApplication = tapTerminateApplication;
558
559 /* notify parent */
560 alock.unlock();
561 mParent->onNetworkAdapterChange(this);
562 }
563
564 return S_OK;
565}
566
567#endif /* VBOX_WITH_UNIXY_TAP_NETWORKING */
568
569/**
570 * Returns the internal network the adapter is attached to
571 *
572 * @returns COM status code
573 * @param internalNetwork address of result variable
574 */
575STDMETHODIMP NetworkAdapter::COMGETTER(InternalNetwork)(BSTR *internalNetwork)
576{
577 // we don't allow null strings
578 if (!internalNetwork)
579 return E_POINTER;
580 AutoLock alock(this);
581 CHECK_READY();
582
583 mData->mInternalNetwork.cloneTo(internalNetwork);
584
585 return S_OK;
586}
587
588/**
589 * Sets the internal network for attachment.
590 *
591 * @returns COM status code
592 * @param internalNetwork internal network name
593 */
594STDMETHODIMP NetworkAdapter::COMSETTER(InternalNetwork)(INPTR BSTR internalNetwork)
595{
596 if (!internalNetwork)
597 return E_INVALIDARG;
598
599 AutoLock alock(this);
600 CHECK_READY();
601
602 CHECK_MACHINE_MUTABILITY (mParent);
603
604 if (mData->mInternalNetwork != internalNetwork)
605 {
606 /* if an empty string is to be set, internal networking must be turned off */
607 if ( (internalNetwork == Bstr(""))
608 && (mData->mAttachmentType = NetworkAttachmentType_InternalNetworkAttachment))
609 {
610 return setError(E_FAIL, tr("A valid internal network name must be set"));
611 }
612
613 mData.backup();
614 mData->mInternalNetwork = internalNetwork;
615
616 /* notify parent */
617 alock.unlock();
618 mParent->onNetworkAdapterChange(this);
619 }
620
621 return S_OK;
622}
623
624
625/**
626 * Return the current cable status
627 *
628 * @returns COM status code
629 * @param connected address of result variable
630 */
631STDMETHODIMP NetworkAdapter::COMGETTER(CableConnected)(BOOL *connected)
632{
633 if (!connected)
634 return E_POINTER;
635
636 AutoLock alock(this);
637 CHECK_READY();
638
639 *connected = mData->mCableConnected;
640 return S_OK;
641}
642
643/**
644 * Set the cable status flag.
645 *
646 * @returns COM status code
647 * @param connected new trace flag
648 */
649STDMETHODIMP NetworkAdapter::COMSETTER(CableConnected)(BOOL connected)
650{
651 AutoLock alock(this);
652 CHECK_READY();
653
654 CHECK_MACHINE_MUTABILITY (mParent);
655
656 if (connected != mData->mCableConnected)
657 {
658 mData.backup();
659 mData->mCableConnected = connected;
660
661 /* notify parent */
662 alock.unlock();
663 mParent->onNetworkAdapterChange(this);
664 }
665
666 return S_OK;
667}
668
669/**
670 * Return the current trace status
671 *
672 * @returns COM status code
673 * @param enabled address of result variable
674 */
675STDMETHODIMP NetworkAdapter::COMGETTER(TraceEnabled)(BOOL *enabled)
676{
677 if (!enabled)
678 return E_POINTER;
679
680 AutoLock alock(this);
681 CHECK_READY();
682
683 *enabled = mData->mTraceEnabled;
684 return S_OK;
685}
686
687/**
688 * Set the trace flag.
689 *
690 * @returns COM status code
691 * @param enabled new trace flag
692 */
693STDMETHODIMP NetworkAdapter::COMSETTER(TraceEnabled)(BOOL enabled)
694{
695 AutoLock alock(this);
696 CHECK_READY();
697
698 CHECK_MACHINE_MUTABILITY (mParent);
699
700 if (enabled != mData->mTraceEnabled)
701 {
702 mData.backup();
703 mData->mTraceEnabled = enabled;
704
705 /* notify parent */
706 alock.unlock();
707 mParent->onNetworkAdapterChange(this);
708 }
709
710 return S_OK;
711}
712
713/**
714 * Return the current trace file name
715 *
716 * @returns COM status code
717 * @param address where to store result
718 */
719STDMETHODIMP NetworkAdapter::COMGETTER(TraceFile)(BSTR *traceFile)
720{
721 if (!traceFile)
722 return E_POINTER;
723
724 AutoLock alock(this);
725 CHECK_READY();
726
727 mData->mTraceFile.cloneTo(traceFile);
728 return S_OK;
729}
730
731/**
732 * Set the trace file name
733 *
734 * @returns COM status code
735 * @param New trace file name
736 */
737STDMETHODIMP NetworkAdapter::COMSETTER(TraceFile)(INPTR BSTR traceFile)
738{
739 if (!traceFile)
740 return E_INVALIDARG;
741
742 AutoLock alock(this);
743 CHECK_READY();
744
745 CHECK_MACHINE_MUTABILITY(mParent);
746
747 mData.backup();
748 mData->mTraceFile = traceFile;
749
750 /* notify parent */
751 alock.unlock();
752 mParent->onNetworkAdapterChange(this);
753
754 return S_OK;
755}
756
757// INetworkAdapter methods
758////////////////////////////////////////////////////////////////////////////////
759
760/**
761 * Attach the network card to the NAT interface.
762 *
763 * @returns COM status code
764 */
765STDMETHODIMP NetworkAdapter::AttachToNAT()
766{
767 AutoLock alock(this);
768 CHECK_READY();
769
770 CHECK_MACHINE_MUTABILITY (mParent);
771
772 if (mData->mAttachmentType != NetworkAttachmentType_NATNetworkAttachment)
773 {
774 mData.backup();
775 detach();
776 mData->mAttachmentType = NetworkAttachmentType_NATNetworkAttachment;
777
778 /* notify parent */
779 alock.unlock();
780 mParent->onNetworkAdapterChange(this);
781 }
782
783 return S_OK;
784}
785
786/**
787 * Attach the network card to the defined host interface.
788 *
789 * @returns COM status code
790 */
791STDMETHODIMP NetworkAdapter::AttachToHostInterface()
792{
793 AutoLock alock(this);
794 CHECK_READY();
795
796 CHECK_MACHINE_MUTABILITY (mParent);
797
798 /* don't do anything if we're already host interface attached */
799 if (mData->mAttachmentType != NetworkAttachmentType_HostInterfaceNetworkAttachment)
800 {
801 mData.backup();
802 /* first detach the current attachment */
803 if (mData->mAttachmentType != NetworkAttachmentType_NoNetworkAttachment)
804 detach();
805 mData->mAttachmentType = NetworkAttachmentType_HostInterfaceNetworkAttachment;
806
807 /* notify parent */
808 alock.unlock();
809 mParent->onNetworkAdapterChange(this);
810 }
811
812 return S_OK;
813}
814
815/**
816 * Attach the network card to the defined internal network.
817 *
818 * @returns COM status code
819 */
820STDMETHODIMP NetworkAdapter::AttachToInternalNetwork()
821{
822 AutoLock alock(this);
823 CHECK_READY();
824
825 CHECK_MACHINE_MUTABILITY (mParent);
826
827 /* don't do anything if we're already internal network attached */
828 if (mData->mAttachmentType != NetworkAttachmentType_InternalNetworkAttachment)
829 {
830 /* there must an internal network name */
831 if ( !mData->mInternalNetwork
832 || (mData->mInternalNetwork == Bstr("")))
833 {
834 LogRel(("Internal network name not defined, setting to default \"intnet\"\n"));
835 HRESULT rc = COMSETTER(InternalNetwork)(Bstr("intnet"));
836 if (FAILED(rc))
837 return rc;
838 }
839
840 mData.backup();
841 /* first detach the current attachment */
842 detach();
843 mData->mAttachmentType = NetworkAttachmentType_InternalNetworkAttachment;
844
845 /* notify parent */
846 alock.unlock();
847 mParent->onNetworkAdapterChange(this);
848 }
849
850 return S_OK;
851}
852
853/**
854 * Detach the network card from its interface
855 *
856 * @returns COM status code
857 */
858STDMETHODIMP NetworkAdapter::Detach()
859{
860 AutoLock alock(this);
861 CHECK_READY();
862
863 CHECK_MACHINE_MUTABILITY (mParent);
864
865 if (mData->mAttachmentType != NetworkAttachmentType_NoNetworkAttachment)
866 {
867 mData.backup();
868 detach();
869
870 /* notify parent */
871 alock.unlock();
872 mParent->onNetworkAdapterChange(this);
873 }
874
875 return S_OK;
876}
877
878// public methods only for internal purposes
879////////////////////////////////////////////////////////////////////////////////
880
881bool NetworkAdapter::rollback()
882{
883 AutoLock alock (this);
884
885 bool changed = false;
886
887 if (mData.isBackedUp())
888 {
889 // we need to check all data to see whether anything will be changed
890 // after rollback
891 changed = mData.hasActualChanges();
892 mData.rollback();
893 }
894
895 return changed;
896}
897
898void NetworkAdapter::commit()
899{
900 AutoLock alock (this);
901 if (mData.isBackedUp())
902 {
903 mData.commit();
904 if (mPeer)
905 {
906 // attach new data to the peer and reshare it
907 AutoLock peerlock (mPeer);
908 mPeer->mData.attach (mData);
909 }
910 }
911}
912
913void NetworkAdapter::copyFrom (NetworkAdapter *aThat)
914{
915 AutoLock alock (this);
916
917 // this will back up current data
918 mData.assignCopy (aThat->mData);
919}
920
921// private methods
922////////////////////////////////////////////////////////////////////////////////
923
924/**
925 * Worker routine for detach handling. No locking, no notifications.
926 */
927void NetworkAdapter::detach()
928{
929 switch (mData->mAttachmentType)
930 {
931 case NetworkAttachmentType_NoNetworkAttachment:
932 {
933 /* nothing to do here */
934 break;
935 }
936 case NetworkAttachmentType_NATNetworkAttachment:
937 {
938 break;
939 }
940 case NetworkAttachmentType_HostInterfaceNetworkAttachment:
941 {
942 /* reset handle and device name */
943#ifdef __WIN__
944 mData->mHostInterface = "";
945#endif
946#ifdef VBOX_WITH_UNIXY_TAP_NETWORKING
947 mData->mHostInterface.setNull();
948 mData->mTAPFD = NIL_RTFILE;
949#endif
950 break;
951 }
952 case NetworkAttachmentType_InternalNetworkAttachment:
953 {
954 mData->mInternalNetwork.setNull();
955 break;
956 }
957 }
958
959 mData->mAttachmentType = NetworkAttachmentType_NoNetworkAttachment;
960}
961
962/**
963 * Generates a new unique MAC address based on our vendor ID and
964 * parts of a GUID
965 */
966void NetworkAdapter::generateMACAddress()
967{
968 /*
969 * Our strategy is as follows: the first three bytes are our fixed
970 * vendor ID (080027). The remaining 3 bytes will be taken from the
971 * start of a GUID. This is a fairly safe algorithm.
972 */
973 char strMAC[13];
974 Guid guid;
975 guid.create();
976 RTStrPrintf(strMAC, sizeof(strMAC), "080027%02X%02X%02X", guid.ptr()->au8[0], guid.ptr()->au8[1], guid.ptr()->au8[2]);
977 LogFlowThisFunc (("Generated '%s'\n", strMAC));
978 mData->mMACAddress = strMAC;
979}
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