1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
|
/* $Id$ */
/** @file
* Implementation of INetworkAdaptor in VBoxSVC.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include "NetworkAdapterImpl.h"
#include "NATEngineImpl.h"
#include "AutoCaller.h"
#include "Logging.h"
#include "MachineImpl.h"
#include "GuestOSTypeImpl.h"
#include <iprt/string.h>
#include <iprt/cpp/utils.h>
#include <VBox/err.h>
#include <VBox/settings.h>
#include "AutoStateDep.h"
// constructor / destructor
////////////////////////////////////////////////////////////////////////////////
NetworkAdapter::NetworkAdapter()
: mParent(NULL)
{
}
NetworkAdapter::~NetworkAdapter()
{
}
HRESULT NetworkAdapter::FinalConstruct()
{
return S_OK;
}
void NetworkAdapter::FinalRelease()
{
uninit();
}
// public initializer/uninitializer for internal purposes only
////////////////////////////////////////////////////////////////////////////////
/**
* Initializes the network adapter object.
*
* @param aParent Handle of the parent object.
*/
HRESULT NetworkAdapter::init(Machine *aParent, ULONG aSlot)
{
LogFlowThisFunc(("aParent=%p, aSlot=%d\n", aParent, aSlot));
ComAssertRet(aParent, E_INVALIDARG);
ComAssertRet(aSlot < SchemaDefs::NetworkAdapterCount, E_INVALIDARG);
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
AssertReturn(autoInitSpan.isOk(), E_FAIL);
unconst(mParent) = aParent;
unconst(mNATEngine).createObject();
mNATEngine->init(aParent, this);
/* mPeer is left null */
m_fModified = false;
mData.allocate();
/* initialize data */
mData->mSlot = aSlot;
/* Default limit is not capped/unlimited. */
mData->mBandwidthLimit = 0;
/* default to Am79C973 */
mData->mAdapterType = NetworkAdapterType_Am79C973;
/* generate the MAC address early to guarantee it is the same both after
* changing some other property (i.e. after mData.backup()) and after the
* subsequent mData.rollback(). */
generateMACAddress();
/* Confirm a successful initialization */
autoInitSpan.setSucceeded();
return S_OK;
}
/**
* Initializes the network adapter object given another network adapter object
* (a kind of copy constructor). This object shares data with
* the object passed as an argument.
*
* @note This object must be destroyed before the original object
* it shares data with is destroyed.
*
* @note Locks @a aThat object for reading.
*/
HRESULT NetworkAdapter::init(Machine *aParent, NetworkAdapter *aThat)
{
LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
ComAssertRet(aParent && aThat, E_INVALIDARG);
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
AssertReturn(autoInitSpan.isOk(), E_FAIL);
unconst(mParent) = aParent;
unconst(mPeer) = aThat;
unconst(mNATEngine).createObject();
mNATEngine->init(aParent, this, aThat->mNATEngine);
AutoCaller thatCaller (aThat);
AssertComRCReturnRC(thatCaller.rc());
AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
mData.share (aThat->mData);
/* Confirm a successful initialization */
autoInitSpan.setSucceeded();
return S_OK;
}
/**
* Initializes the guest object given another guest object
* (a kind of copy constructor). This object makes a private copy of data
* of the original object passed as an argument.
*
* @note Locks @a aThat object for reading.
*/
HRESULT NetworkAdapter::initCopy(Machine *aParent, NetworkAdapter *aThat)
{
LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
ComAssertRet(aParent && aThat, E_INVALIDARG);
/* Enclose the state transition NotReady->InInit->Ready */
AutoInitSpan autoInitSpan(this);
AssertReturn(autoInitSpan.isOk(), E_FAIL);
unconst(mParent) = aParent;
/* mPeer is left null */
unconst(mNATEngine).createObject();
mNATEngine->initCopy(aParent, this, aThat->mNATEngine);
AutoCaller thatCaller (aThat);
AssertComRCReturnRC(thatCaller.rc());
AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
mData.attachCopy (aThat->mData);
/* Confirm a successful initialization */
autoInitSpan.setSucceeded();
return S_OK;
}
/**
* Uninitializes the instance and sets the ready flag to FALSE.
* Called either from FinalRelease() or by the parent when it gets destroyed.
*/
void NetworkAdapter::uninit()
{
LogFlowThisFunc(("\n"));
/* Enclose the state transition Ready->InUninit->NotReady */
AutoUninitSpan autoUninitSpan(this);
if (autoUninitSpan.uninitDone())
return;
mData.free();
unconst(mNATEngine).setNull();
unconst(mPeer) = NULL;
unconst(mParent) = NULL;
}
// INetworkAdapter properties
////////////////////////////////////////////////////////////////////////////////
STDMETHODIMP NetworkAdapter::COMGETTER(AdapterType) (NetworkAdapterType_T *aAdapterType)
{
CheckComArgOutPointerValid(aAdapterType);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aAdapterType = mData->mAdapterType;
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(AdapterType) (NetworkAdapterType_T aAdapterType)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/* make sure the value is allowed */
switch (aAdapterType)
{
case NetworkAdapterType_Am79C970A:
case NetworkAdapterType_Am79C973:
#ifdef VBOX_WITH_E1000
case NetworkAdapterType_I82540EM:
case NetworkAdapterType_I82543GC:
case NetworkAdapterType_I82545EM:
#endif
#ifdef VBOX_WITH_VIRTIO
case NetworkAdapterType_Virtio:
#endif /* VBOX_WITH_VIRTIO */
break;
default:
return setError(E_FAIL,
tr("Invalid network adapter type '%d'"),
aAdapterType);
}
if (mData->mAdapterType != aAdapterType)
{
mData.backup();
mData->mAdapterType = aAdapterType;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* Changing the network adapter type during runtime is not allowed,
* therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
mParent->onNetworkAdapterChange(this, FALSE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(Slot) (ULONG *aSlot)
{
CheckComArgOutPointerValid(aSlot);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aSlot = mData->mSlot;
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(Enabled) (BOOL *aEnabled)
{
CheckComArgOutPointerValid(aEnabled);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aEnabled = mData->mEnabled;
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(Enabled) (BOOL aEnabled)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (mData->mEnabled != aEnabled)
{
mData.backup();
mData->mEnabled = aEnabled;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* Disabling the network adapter during runtime is not allowed
* therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
mParent->onNetworkAdapterChange(this, FALSE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(MACAddress)(BSTR *aMACAddress)
{
CheckComArgOutPointerValid(aMACAddress);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
ComAssertRet(!mData->mMACAddress.isEmpty(), E_FAIL);
mData->mMACAddress.cloneTo(aMACAddress);
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(MACAddress)(IN_BSTR aMACAddress)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
HRESULT rc = S_OK;
bool emitChangeEvent = false;
/*
* Are we supposed to generate a MAC?
*/
if (!aMACAddress || !*aMACAddress)
{
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
mData.backup();
generateMACAddress();
emitChangeEvent = true;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
}
else
{
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (mData->mMACAddress != aMACAddress)
{
/*
* Verify given MAC address
*/
Utf8Str macAddressUtf = aMACAddress;
char *macAddressStr = macAddressUtf.mutableRaw();
int i = 0;
while ((i < 13) && macAddressStr && *macAddressStr && (rc == S_OK))
{
char c = *macAddressStr;
/* canonicalize hex digits to capital letters */
if (c >= 'a' && c <= 'f')
{
/** @todo the runtime lacks an ascii lower/upper conv */
c &= 0xdf;
*macAddressStr = c;
}
/* we only accept capital letters */
if (((c < '0') || (c > '9')) &&
((c < 'A') || (c > 'F')))
rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
/* the second digit must have even value for unicast addresses */
if ((i == 1) && (!!(c & 1) == (c >= '0' && c <= '9')))
rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
macAddressStr++;
i++;
}
/* we must have parsed exactly 12 characters */
if (i != 12)
rc = setError(E_INVALIDARG, tr("Invalid MAC address format"));
if (SUCCEEDED(rc))
{
mData.backup();
mData->mMACAddress = macAddressUtf;
emitChangeEvent = true;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
}
}
}
// we have left the lock in any case at this point
if (emitChangeEvent)
{
/* Changing the MAC via the Main API during runtime is not allowed,
* therefore no immediate change in CFGM logic => changeAdapter=FALSE. */
mParent->onNetworkAdapterChange(this, FALSE);
}
return rc;
}
STDMETHODIMP NetworkAdapter::COMGETTER(AttachmentType)(
NetworkAttachmentType_T *aAttachmentType)
{
CheckComArgOutPointerValid(aAttachmentType);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aAttachmentType = mData->mAttachmentType;
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(HostInterface)(BSTR *aHostInterface)
{
CheckComArgOutPointerValid(aHostInterface);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
mData->mHostInterface.cloneTo(aHostInterface);
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(HostInterface)(IN_BSTR aHostInterface)
{
Bstr bstrEmpty("");
if (!aHostInterface)
aHostInterface = bstrEmpty.raw();
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (mData->mHostInterface != aHostInterface)
{
mData.backup();
mData->mHostInterface = aHostInterface;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* When changing the host adapter, adapt the CFGM logic to make this
* change immediately effect and to notify the guest that the network
* might have changed, therefore changeAdapter=TRUE. */
mParent->onNetworkAdapterChange(this, TRUE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(InternalNetwork) (BSTR *aInternalNetwork)
{
CheckComArgOutPointerValid(aInternalNetwork);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
mData->mInternalNetwork.cloneTo(aInternalNetwork);
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(InternalNetwork) (IN_BSTR aInternalNetwork)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (mData->mInternalNetwork != aInternalNetwork)
{
/* if an empty/null string is to be set, internal networking must be
* turned off */
if ( (aInternalNetwork == NULL || *aInternalNetwork == '\0')
&& mData->mAttachmentType == NetworkAttachmentType_Internal)
{
return setError(E_FAIL,
tr("Empty or null internal network name is not valid"));
}
mData.backup();
mData->mInternalNetwork = aInternalNetwork;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* When changing the internal network, adapt the CFGM logic to make this
* change immediately effect and to notify the guest that the network
* might have changed, therefore changeAdapter=TRUE. */
mParent->onNetworkAdapterChange(this, TRUE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(NATNetwork) (BSTR *aNATNetwork)
{
CheckComArgOutPointerValid(aNATNetwork);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
mData->mNATNetwork.cloneTo(aNATNetwork);
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(NATNetwork) (IN_BSTR aNATNetwork)
{
Bstr bstrEmpty("");
if (!aNATNetwork)
aNATNetwork = bstrEmpty.raw();
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (mData->mNATNetwork != aNATNetwork)
{
mData.backup();
mData->mNATNetwork = aNATNetwork;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* Changing the NAT network isn't allowed during runtime, therefore
* no immediate replug in CFGM logic => changeAdapter=FALSE */
mParent->onNetworkAdapterChange(this, FALSE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(VDENetwork) (BSTR *aVDENetwork)
{
#if defined(VBOX_WITH_VDE)
CheckComArgOutPointerValid(aVDENetwork);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
mData->mVDENetwork.cloneTo(aVDENetwork);
return S_OK;
#else
NOREF(aVDENetwork);
return E_NOTIMPL;
#endif
}
STDMETHODIMP NetworkAdapter::COMSETTER(VDENetwork) (IN_BSTR aVDENetwork)
{
#if defined(VBOX_WITH_VDE)
Bstr bstrEmpty("");
if (!aVDENetwork)
aVDENetwork = bstrEmpty.raw();
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep (mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (mData->mVDENetwork != aVDENetwork)
{
mData.backup();
mData->mVDENetwork = aVDENetwork;
/* leave the lock before informing callbacks */
alock.release();
mParent->onNetworkAdapterChange (this, FALSE);
}
return S_OK;
#else
NOREF(aVDENetwork);
return E_NOTIMPL;
#endif
}
STDMETHODIMP NetworkAdapter::COMGETTER(CableConnected) (BOOL *aConnected)
{
CheckComArgOutPointerValid(aConnected);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aConnected = mData->mCableConnected;
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(CableConnected) (BOOL aConnected)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (aConnected != mData->mCableConnected)
{
mData.backup();
mData->mCableConnected = aConnected;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* No change in CFGM logic => changeAdapter=FALSE. */
mParent->onNetworkAdapterChange(this, FALSE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(LineSpeed) (ULONG *aSpeed)
{
CheckComArgOutPointerValid(aSpeed);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aSpeed = mData->mLineSpeed;
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(LineSpeed) (ULONG aSpeed)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (aSpeed != mData->mLineSpeed)
{
mData.backup();
mData->mLineSpeed = aSpeed;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* No change in CFGM logic => changeAdapter=FALSE. */
mParent->onNetworkAdapterChange(this, FALSE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(BandwidthLimit) (ULONG *aLimit)
{
CheckComArgOutPointerValid(aLimit);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aLimit = mData->mBandwidthLimit;
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(BandwidthLimit) (ULONG aLimit)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine doesn't need to be mutable */
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (aLimit != mData->mBandwidthLimit)
{
mData.backup();
mData->mBandwidthLimit = aLimit;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* No change in CFGM logic => changeAdapter=FALSE. */
mParent->onNetworkAdapterChange(this, FALSE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(TraceEnabled) (BOOL *aEnabled)
{
CheckComArgOutPointerValid(aEnabled);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aEnabled = mData->mTraceEnabled;
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(TraceEnabled) (BOOL aEnabled)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (aEnabled != mData->mTraceEnabled)
{
mData.backup();
mData->mTraceEnabled = aEnabled;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* Adapt the CFGM logic changeAdapter=TRUE */
mParent->onNetworkAdapterChange(this, TRUE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(TraceFile) (BSTR *aTraceFile)
{
CheckComArgOutPointerValid(aTraceFile);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
mData->mTraceFile.cloneTo(aTraceFile);
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(TraceFile) (IN_BSTR aTraceFile)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (mData->mTraceFile != aTraceFile)
{
mData.backup();
mData->mTraceFile = aTraceFile;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* No change in CFGM logic => changeAdapter=FALSE. */
mParent->onNetworkAdapterChange(this, FALSE);
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(NatDriver) (INATEngine **aNatDriver)
{
CheckComArgOutPointerValid(aNatDriver);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
mNATEngine.queryInterfaceTo(aNatDriver);
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMGETTER(BootPriority) (ULONG *aBootPriority)
{
CheckComArgOutPointerValid(aBootPriority);
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
*aBootPriority = mData->mBootPriority;
return S_OK;
}
STDMETHODIMP NetworkAdapter::COMSETTER(BootPriority) (ULONG aBootPriority)
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (aBootPriority != mData->mBootPriority)
{
mData.backup();
mData->mBootPriority = aBootPriority;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* No change in CFGM logic => changeAdapter=FALSE. */
mParent->onNetworkAdapterChange(this, FALSE);
}
return S_OK;
}
// INetworkAdapter methods
////////////////////////////////////////////////////////////////////////////////
STDMETHODIMP NetworkAdapter::AttachToNAT()
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (mData->mAttachmentType != NetworkAttachmentType_NAT)
{
mData.backup();
// Commented this for now as it resets the parameter mData->mNATNetwork
// which is essential while changing the Attachment dynamically.
//detach();
mData->mAttachmentType = NetworkAttachmentType_NAT;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* Adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
HRESULT rc = mParent->onNetworkAdapterChange(this, TRUE);
if (FAILED(rc))
{
/* If changing the attachment failed then we can't assume
* that the previous attachment will attach correctly
* and thus return error along with detaching all
* attachments.
*/
Detach();
return rc;
}
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::AttachToBridgedInterface()
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/* don't do anything if we're already host interface attached */
if (mData->mAttachmentType != NetworkAttachmentType_Bridged)
{
mData.backup();
/* first detach the current attachment */
// Commented this for now as it reset the parameter mData->mHostInterface
// which is essential while changing the Attachment dynamically.
//detach();
mData->mAttachmentType = NetworkAttachmentType_Bridged;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* Adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
HRESULT rc = mParent->onNetworkAdapterChange(this, TRUE);
if (FAILED(rc))
{
/* If changing the attachment failed then we can't assume that the
* previous attachment will attach correctly and thus return error
* along with detaching all attachments.
*/
Detach();
return rc;
}
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::AttachToInternalNetwork()
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/* don't do anything if we're already internal network attached */
if (mData->mAttachmentType != NetworkAttachmentType_Internal)
{
mData.backup();
/* first detach the current attachment */
// Commented this for now as it reset the parameter mData->mInternalNetwork
// which is essential while changing the Attachment dynamically.
//detach();
/* there must an internal network name */
if (mData->mInternalNetwork.isEmpty())
{
LogRel (("Internal network name not defined, "
"setting to default \"intnet\"\n"));
mData->mInternalNetwork = "intnet";
}
mData->mAttachmentType = NetworkAttachmentType_Internal;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* Adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
HRESULT rc = mParent->onNetworkAdapterChange(this, TRUE);
if (FAILED(rc))
{
/* If changing the attachment failed then we can't assume
* that the previous attachment will attach correctly
* and thus return error along with detaching all
* attachments.
*/
Detach();
return rc;
}
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::AttachToHostOnlyInterface()
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/* don't do anything if we're already host interface attached */
if (mData->mAttachmentType != NetworkAttachmentType_HostOnly)
{
mData.backup();
/* first detach the current attachment */
// Commented this for now as it reset the parameter mData->mHostInterface
// which is essential while changing the Attachment dynamically.
//detach();
mData->mAttachmentType = NetworkAttachmentType_HostOnly;
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* Adapt the CFGM logic and notify the guest => changeAdpater=TRUE. */
HRESULT rc = mParent->onNetworkAdapterChange(this, TRUE);
if (FAILED(rc))
{
/* If changing the attachment failed then we can't assume
* that the previous attachment will attach correctly
* and thus return error along with detaching all
* attachments.
*/
Detach();
return rc;
}
}
return S_OK;
}
STDMETHODIMP NetworkAdapter::AttachToVDE()
{
#if defined(VBOX_WITH_VDE)
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep (mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
/* don't do anything if we're already host interface attached */
if (mData->mAttachmentType != NetworkAttachmentType_VDE)
{
mData.backup();
/* first detach the current attachment */
// Commented this for now as it reset the parameter mData->mHostInterface
// which is essential while changing the Attachment dynamically.
//detach();
mData->mAttachmentType = NetworkAttachmentType_VDE;
/* leave the lock before informing callbacks */
alock.release();
HRESULT rc = mParent->onNetworkAdapterChange (this, TRUE);
if (FAILED (rc))
{
/* If changing the attachment failed then we can't assume
* that the previous attachment will attach correctly
* and thus return error along with detaching all
* attachments.
*/
Detach();
return rc;
}
}
return S_OK;
#else /* !VBOX_WITH_VDE */
return E_NOTIMPL;
#endif
}
STDMETHODIMP NetworkAdapter::Detach()
{
AutoCaller autoCaller(this);
if (FAILED(autoCaller.rc())) return autoCaller.rc();
/* the machine needs to be mutable */
AutoMutableStateDependency adep(mParent);
if (FAILED(adep.rc())) return adep.rc();
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
if (mData->mAttachmentType != NetworkAttachmentType_Null)
{
mData.backup();
detach();
m_fModified = true;
// leave the lock before informing callbacks
alock.release();
AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, no need to lock
mParent->setModified(Machine::IsModified_NetworkAdapters);
mlock.release();
/* adapt the CFGM logic and notify the guest => changeAdapter=TRUE. */
mParent->onNetworkAdapterChange(this, TRUE);
}
return S_OK;
}
// public methods only for internal purposes
////////////////////////////////////////////////////////////////////////////////
/**
* Loads settings from the given adapter node.
* May be called once right after this object creation.
*
* @param aAdapterNode <Adapter> node.
*
* @note Locks this object for writing.
*/
HRESULT NetworkAdapter::loadSettings(const settings::NetworkAdapter &data)
{
AutoCaller autoCaller(this);
AssertComRCReturnRC(autoCaller.rc());
/* Note: we assume that the default values for attributes of optional
* nodes are assigned in the Data::Data() constructor and don't do it
* here. It implies that this method may only be called after constructing
* a new BIOSSettings object while all its data fields are in the default
* values. Exceptions are fields whose creation time defaults don't match
* values that should be applied when these fields are not explicitly set
* in the settings file (for backwards compatibility reasons). This takes
* place when a setting of a newly created object must default to A while
* the same setting of an object loaded from the old settings file must
* default to B. */
HRESULT rc = S_OK;
mData->mAdapterType = data.type;
mData->mEnabled = data.fEnabled;
/* MAC address (can be null) */
rc = COMSETTER(MACAddress)(Bstr(data.strMACAddress).raw());
if (FAILED(rc)) return rc;
/* cable (required) */
mData->mCableConnected = data.fCableConnected;
/* line speed (defaults to 100 Mbps) */
mData->mLineSpeed = data.ulLineSpeed;
/* tracing (defaults to false) */
mData->mTraceEnabled = data.fTraceEnabled;
mData->mTraceFile = data.strTraceFile;
/* boot priority (defaults to 0, i.e. lowest) */
mData->mBootPriority = data.ulBootPriority;
/* Bandwidth limit in Mbps. */
mData->mBandwidthLimit = data.ulBandwidthLimit;
switch (data.mode)
{
case NetworkAttachmentType_NAT:
mNATEngine->loadSettings(data.nat);
rc = AttachToNAT();
if (FAILED(rc)) return rc;
break;
case NetworkAttachmentType_Bridged:
rc = COMSETTER(HostInterface)(Bstr(data.strName).raw());
if (FAILED(rc)) return rc;
rc = AttachToBridgedInterface();
if (FAILED(rc)) return rc;
break;
case NetworkAttachmentType_Internal:
mData->mInternalNetwork = data.strName;
Assert(!mData->mInternalNetwork.isEmpty());
rc = AttachToInternalNetwork();
if (FAILED(rc)) return rc;
break;
case NetworkAttachmentType_HostOnly:
#if defined(VBOX_WITH_NETFLT)
rc = COMSETTER(HostInterface)(Bstr(data.strName).raw());
if (FAILED(rc)) return rc;
#endif
rc = AttachToHostOnlyInterface();
if (FAILED(rc)) return rc;
break;
#if defined(VBOX_WITH_VDE)
case NetworkAttachmentType_VDE:
mData->mVDENetwork = data.strName;
rc = AttachToVDE();
if (FAILED(rc)) return rc;
break;
#endif
case NetworkAttachmentType_Null:
rc = Detach();
if (FAILED(rc)) return rc;
break;
}
if (data.fHasDisabledNAT)
mNATEngine->loadSettings(data.nat);
// after loading settings, we are no longer different from the XML on disk
m_fModified = false;
return S_OK;
}
/**
* Saves settings to the given adapter node.
*
* Note that the given Adapter node is completely empty on input.
*
* @param aAdapterNode <Adapter> node.
*
* @note Locks this object for reading.
*/
HRESULT NetworkAdapter::saveSettings(settings::NetworkAdapter &data)
{
AutoCaller autoCaller(this);
AssertComRCReturnRC(autoCaller.rc());
AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
data.fEnabled = !!mData->mEnabled;
data.strMACAddress = mData->mMACAddress;
data.fCableConnected = !!mData->mCableConnected;
data.ulLineSpeed = mData->mLineSpeed;
data.fTraceEnabled = !!mData->mTraceEnabled;
data.strTraceFile = mData->mTraceFile;
data.ulBootPriority = mData->mBootPriority;
data.ulBandwidthLimit = mData->mBandwidthLimit;
data.type = mData->mAdapterType;
switch (data.mode = mData->mAttachmentType)
{
case NetworkAttachmentType_Null:
data.strName.setNull();
break;
case NetworkAttachmentType_NAT:
data.fHasDisabledNAT = 0;
mNATEngine->commit();
mNATEngine->saveSettings(data.nat);
break;
case NetworkAttachmentType_Bridged:
data.strName = mData->mHostInterface;
break;
case NetworkAttachmentType_Internal:
data.strName = mData->mInternalNetwork;
break;
case NetworkAttachmentType_HostOnly:
data.strName = mData->mHostInterface;
break;
#if defined(VBOX_WITH_VDE)
case NetworkAttachmentType_VDE:
data.strName = mData->mVDENetwork;
break;
#endif
}
if (data.mode != NetworkAttachmentType_NAT)
{
data.fHasDisabledNAT = 1; /* ??? */
mNATEngine->commit();
mNATEngine->saveSettings(data.nat);
}
// after saving settings, we are no longer different from the XML on disk
m_fModified = false;
return S_OK;
}
/**
* Returns true if any setter method has modified settings of this instance.
* @return
*/
bool NetworkAdapter::isModified() {
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
bool fChanged = m_fModified;
fChanged |= (mData->mAdapterType == NetworkAttachmentType_NAT? mNATEngine->isModified() : false);
return fChanged;
}
/**
* @note Locks this object for writing.
*/
void NetworkAdapter::rollback()
{
/* sanity */
AutoCaller autoCaller(this);
AssertComRCReturnVoid(autoCaller.rc());
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
mData.rollback();
}
/**
* @note Locks this object for writing, together with the peer object (also
* for writing) if there is one.
*/
void NetworkAdapter::commit()
{
/* sanity */
AutoCaller autoCaller(this);
AssertComRCReturnVoid (autoCaller.rc());
/* sanity too */
AutoCaller peerCaller (mPeer);
AssertComRCReturnVoid (peerCaller.rc());
/* lock both for writing since we modify both (mPeer is "master" so locked
* first) */
AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
if (mData.isBackedUp())
{
mData.commit();
if (mPeer)
{
/* attach new data to the peer and reshare it */
mPeer->mData.attach (mData);
}
}
}
/**
* @note Locks this object for writing, together with the peer object
* represented by @a aThat (locked for reading).
*/
void NetworkAdapter::copyFrom (NetworkAdapter *aThat)
{
AssertReturnVoid (aThat != NULL);
/* sanity */
AutoCaller autoCaller(this);
AssertComRCReturnVoid (autoCaller.rc());
/* sanity too */
AutoCaller thatCaller (aThat);
AssertComRCReturnVoid (thatCaller.rc());
/* peer is not modified, lock it for reading (aThat is "master" so locked
* first) */
AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
/* this will back up current data */
mData.assignCopy (aThat->mData);
}
void NetworkAdapter::applyDefaults (GuestOSType *aOsType)
{
AssertReturnVoid (aOsType != NULL);
/* sanity */
AutoCaller autoCaller(this);
AssertComRCReturnVoid (autoCaller.rc());
AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
bool e1000enabled = false;
#ifdef VBOX_WITH_E1000
e1000enabled = true;
#endif // VBOX_WITH_E1000
NetworkAdapterType_T defaultType = aOsType->networkAdapterType();
/* Set default network adapter for this OS type */
if (defaultType == NetworkAdapterType_I82540EM ||
defaultType == NetworkAdapterType_I82543GC ||
defaultType == NetworkAdapterType_I82545EM)
{
if (e1000enabled) mData->mAdapterType = defaultType;
}
else mData->mAdapterType = defaultType;
/* Enable and connect the first one adapter to the NAT */
if (mData->mSlot == 0)
{
mData->mEnabled = true;
mData->mAttachmentType = NetworkAttachmentType_NAT;
mData->mCableConnected = true;
}
}
// private methods
////////////////////////////////////////////////////////////////////////////////
/**
* Worker routine for detach handling. No locking, no notifications.
* @note Must be called from under the object's write lock.
*/
void NetworkAdapter::detach()
{
AssertReturnVoid (isWriteLockOnCurrentThread());
switch (mData->mAttachmentType)
{
case NetworkAttachmentType_Null:
{
/* nothing to do here */
break;
}
case NetworkAttachmentType_NAT:
{
break;
}
case NetworkAttachmentType_Bridged:
{
/* reset handle and device name */
mData->mHostInterface = "";
break;
}
case NetworkAttachmentType_Internal:
{
mData->mInternalNetwork.setNull();
break;
}
case NetworkAttachmentType_HostOnly:
{
#if defined(VBOX_WITH_NETFLT)
/* reset handle and device name */
mData->mHostInterface = "";
#endif
break;
}
}
mData->mAttachmentType = NetworkAttachmentType_Null;
}
/**
* Generates a new unique MAC address based on our vendor ID and
* parts of a GUID.
*
* @note Must be called from under the object's write lock or within the init
* span.
*/
void NetworkAdapter::generateMACAddress()
{
/*
* Our strategy is as follows: the first three bytes are our fixed
* vendor ID (080027). The remaining 3 bytes will be taken from the
* start of a GUID. This is a fairly safe algorithm.
*/
char strMAC[13];
Guid guid;
guid.create();
RTStrPrintf (strMAC, sizeof(strMAC), "080027%02X%02X%02X",
guid.raw()->au8[0], guid.raw()->au8[1], guid.raw()->au8[2]);
LogFlowThisFunc(("generated MAC: '%s'\n", strMAC));
mData->mMACAddress = strMAC;
}
/* vi: set tabstop=4 shiftwidth=4 expandtab: */
|