forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkInterface.cpp
1780 lines (1456 loc) · 44 KB
/
NetworkInterface.cpp
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
//
// NetworkInterface.cpp
//
// $Id: //poco/1.4/Net/src/NetworkInterface.cpp#9 $
//
// Library: Net
// Package: NetCore
// Module: NetworkInterface
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/NetworkInterface.h"
#ifdef POCO_NET_HAS_INTERFACE
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/NumberFormatter.h"
#include "Poco/RefCountedObject.h"
#include "Poco/Format.h"
#if defined(POCO_OS_FAMILY_WINDOWS)
#if defined(POCO_WIN32_UTF8)
#include "Poco/UnicodeConverter.h"
#endif
#include "Poco/Error.h"
#include <wincrypt.h>
#include <iphlpapi.h>
#include <ipifcons.h>
#endif
#include <cstring>
#include <iostream>
#include <iomanip>
using Poco::NumberFormatter;
using Poco::FastMutex;
using Poco::format;
std::ostream& operator<<(std::ostream& os, const Poco::Net::NetworkInterface::MACAddress& mac)
{
std::ios state(0);
state.copyfmt(os);
for (unsigned i = 0; i < mac.size(); ++i)
{
if (i > 0) os << Poco::Net::NetworkInterface::MAC_SEPARATOR;
os << std::hex << std::setw(2) << std::setfill('0') << (unsigned) mac[i];
}
os.copyfmt(state);
return os;
}
namespace Poco {
namespace Net {
//
// NetworkInterfaceImpl
//
class NetworkInterfaceImpl: public Poco::RefCountedObject
{
public:
typedef NetworkInterface::AddressTuple AddressTuple;
typedef NetworkInterface::AddressList AddressList;
typedef NetworkInterface::Type Type;
NetworkInterfaceImpl(unsigned index);
NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const std::string& adapterName, const IPAddress& address, unsigned index, NetworkInterface::MACAddress* pMACAddress = 0);
NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const std::string& adapterName, unsigned index = 0, NetworkInterface::MACAddress* pMACAddress = 0);
NetworkInterfaceImpl(const std::string& name,
const std::string& displayName,
const std::string& adapterName,
const IPAddress& address,
const IPAddress& subnetMask,
const IPAddress& broadcastAddress,
unsigned index,
NetworkInterface::MACAddress* pMACAddress = 0);
unsigned index() const;
const std::string& name() const;
const std::string& displayName() const;
const std::string& adapterName() const;
const IPAddress& firstAddress(IPAddress::Family family) const;
void addAddress(const AddressTuple& address);
const IPAddress& address(unsigned index) const;
const NetworkInterface::AddressList& addressList() const;
bool hasAddress(const IPAddress& address) const;
const IPAddress& subnetMask(unsigned index) const;
const IPAddress& broadcastAddress(unsigned index) const;
const IPAddress& destAddress(unsigned index) const;
const NetworkInterface::MACAddress& macAddress() const;
bool supportsIPv4() const;
bool supportsIPv6() const;
void setName(const std::string& name);
void setDisplayName(const std::string& name);
void setAdapterName(const std::string& name);
void addAddress(const IPAddress& addr);
void setMACAddress(const NetworkInterface::MACAddress& addr);
void setMACAddress(const void *addr, std::size_t len);
unsigned mtu() const;
unsigned ifindex() const;
Type type() const;
bool broadcast() const;
bool loopback() const;
bool multicast() const;
bool pointToPoint() const;
bool running() const;
bool up() const;
#if defined(POCO_OS_FAMILY_WINDOWS)
void setFlags(DWORD flags, DWORD iftype);
void setRunning(bool running);
#else
void setFlags(short flags);
#endif
void setUp(bool up);
void setMTU(unsigned mtu);
void setType(Type type);
void setIndex(unsigned index);
void setPhyParams();
protected:
~NetworkInterfaceImpl();
private:
std::string _name;
std::string _displayName;
std::string _adapterName;
AddressList _addressList;
unsigned _index;
bool _broadcast;
bool _loopback;
bool _multicast;
bool _pointToPoint;
bool _up;
bool _running;
unsigned _mtu;
Type _type;
NetworkInterface::MACAddress _macAddress;
friend class NetworkInterface;
};
NetworkInterfaceImpl::NetworkInterfaceImpl(unsigned index):
_index(index),
_broadcast(false),
_loopback(false),
_multicast(false),
_pointToPoint(false),
_up(false),
_running(false),
_mtu(0),
_type(NetworkInterface::NI_TYPE_OTHER)
{
}
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const std::string& adapterName, const IPAddress& address, unsigned index, NetworkInterface::MACAddress* pMACAddress):
_name(name),
_displayName(displayName),
_adapterName(adapterName),
_index(index),
_broadcast(false),
_loopback(false),
_multicast(false),
_pointToPoint(false),
_up(false),
_running(false),
_mtu(0),
_type(NetworkInterface::NI_TYPE_OTHER)
{
_addressList.push_back(AddressTuple(address, IPAddress(), IPAddress()));
setPhyParams();
if (pMACAddress) setMACAddress(*pMACAddress);
}
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const std::string& adapterName, unsigned index, NetworkInterface::MACAddress* pMACAddress):
_name(name),
_displayName(displayName),
_adapterName(adapterName),
_index(index),
_broadcast(false),
_loopback(false),
_multicast(false),
_pointToPoint(false),
_up(false),
_running(false),
_mtu(0),
_type(NetworkInterface::NI_TYPE_OTHER)
{
setPhyParams();
if (pMACAddress) setMACAddress(*pMACAddress);
}
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name,
const std::string& displayName,
const std::string& adapterName,
const IPAddress& address,
const IPAddress& subnetMask,
const IPAddress& broadcastAddress,
unsigned index,
NetworkInterface::MACAddress* pMACAddress):
_name(name),
_displayName(displayName),
_adapterName(adapterName),
_index(index),
_broadcast(false),
_loopback(false),
_multicast(false),
_pointToPoint(false),
_up(false),
_running(false),
_mtu(0)
{
_addressList.push_back(AddressTuple(address, subnetMask, broadcastAddress));
setPhyParams();
if (pMACAddress) setMACAddress(*pMACAddress);
}
void NetworkInterfaceImpl::setPhyParams()
{
#if !defined(POCO_OS_FAMILY_WINDOWS) && !defined(POCO_VXWORKS)
struct ifreq ifr;
std::strncpy(ifr.ifr_name, _name.c_str(), IFNAMSIZ);
DatagramSocket ds;
ds.impl()->ioctl(SIOCGIFFLAGS, &ifr);
setFlags(ifr.ifr_flags);
ds.impl()->ioctl(SIOCGIFMTU, &ifr);
setMTU(ifr.ifr_mtu);
#endif
}
NetworkInterfaceImpl::~NetworkInterfaceImpl()
{
}
bool NetworkInterfaceImpl::supportsIPv4() const
{
AddressList::const_iterator it = _addressList.begin();
AddressList::const_iterator end = _addressList.end();
for (; it != end; ++it)
{
if (IPAddress::IPv4 == it->get<NetworkInterface::IP_ADDRESS>().family())
return true;
}
return false;
}
bool NetworkInterfaceImpl::supportsIPv6() const
{
#ifdef POCO_HAVE_IPv6
AddressList::const_iterator it = _addressList.begin();
AddressList::const_iterator end = _addressList.end();
for (; it != end; ++it)
{
if (IPAddress::IPv6 == it->get<NetworkInterface::IP_ADDRESS>().family())
return true;
}
#endif
return false;
}
inline unsigned NetworkInterfaceImpl::index() const
{
return _index;
}
inline const std::string& NetworkInterfaceImpl::name() const
{
return _name;
}
inline const std::string& NetworkInterfaceImpl::displayName() const
{
return _displayName;
}
inline const std::string& NetworkInterfaceImpl::adapterName() const
{
return _adapterName;
}
const IPAddress& NetworkInterfaceImpl::firstAddress(IPAddress::Family family) const
{
AddressList::const_iterator it = _addressList.begin();
AddressList::const_iterator end = _addressList.end();
for (;it != end; ++it)
{
const IPAddress& addr = it->get<NetworkInterface::IP_ADDRESS>();
if (addr.family() == family) return addr;
}
throw NotFoundException(format("%s family address not found.", (family == IPAddress::IPv4) ? std::string("IPv4") : std::string("IPv6")));
}
inline void NetworkInterfaceImpl::addAddress(const AddressTuple& address)
{
_addressList.push_back(address);
}
bool NetworkInterfaceImpl::hasAddress(const IPAddress& address) const
{
NetworkInterface::ConstAddressIterator it = _addressList.begin();
NetworkInterface::ConstAddressIterator end = _addressList.end();
for (; it != end; ++it)
{
if (it->get<NetworkInterface::IP_ADDRESS>() == address)
return true;
}
return false;
}
inline const IPAddress& NetworkInterfaceImpl::address(unsigned index) const
{
if (index < _addressList.size()) return _addressList[index].get<NetworkInterface::IP_ADDRESS>();
else throw NotFoundException(Poco::format("No address with index %u.", index));
}
inline const NetworkInterface::AddressList& NetworkInterfaceImpl::addressList() const
{
return _addressList;
}
const IPAddress& NetworkInterfaceImpl::subnetMask(unsigned index) const
{
if (index < _addressList.size())
return _addressList[index].get<NetworkInterface::SUBNET_MASK>();
throw NotFoundException(Poco::format("No subnet mask with index %u.", index));
}
const IPAddress& NetworkInterfaceImpl::broadcastAddress(unsigned index) const
{
if (index < _addressList.size())
return _addressList[index].get<NetworkInterface::BROADCAST_ADDRESS>();
throw NotFoundException(Poco::format("No subnet mask with index %u.", index));
}
const IPAddress& NetworkInterfaceImpl::destAddress(unsigned index) const
{
if (!pointToPoint())
throw InvalidAccessException("Only PPP addresses have destination address.");
else if (index < _addressList.size())
return _addressList[index].get<NetworkInterface::BROADCAST_ADDRESS>();
throw NotFoundException(Poco::format("No address with index %u.", index));
}
const NetworkInterface::MACAddress& NetworkInterfaceImpl::macAddress() const
{
return _macAddress;
}
inline unsigned NetworkInterfaceImpl::mtu() const
{
return _mtu;
}
inline NetworkInterface::Type NetworkInterfaceImpl::type() const
{
return _type;
}
inline bool NetworkInterfaceImpl::broadcast() const
{
return _broadcast;
}
inline bool NetworkInterfaceImpl::loopback() const
{
return _loopback;
}
inline bool NetworkInterfaceImpl::multicast() const
{
return _multicast;
}
inline bool NetworkInterfaceImpl::pointToPoint() const
{
return _pointToPoint;
}
inline bool NetworkInterfaceImpl::running() const
{
return _running;
}
inline bool NetworkInterfaceImpl::up() const
{
return _up;
}
#if defined(POCO_OS_FAMILY_WINDOWS)
void NetworkInterfaceImpl::setFlags(DWORD flags, DWORD iftype)
{
_running = _up = false;
switch (iftype) {
case IF_TYPE_ETHERNET_CSMACD:
case IF_TYPE_ISO88025_TOKENRING:
case IF_TYPE_IEEE80211:
_multicast = _broadcast = true;
break;
case IF_TYPE_SOFTWARE_LOOPBACK:
_loopback = true;
break;
case IF_TYPE_PPP:
case IF_TYPE_ATM:
case IF_TYPE_TUNNEL:
case IF_TYPE_IEEE1394:
_pointToPoint = true;
break;
}
if (!(flags & IP_ADAPTER_NO_MULTICAST))
_multicast = true;
}
void NetworkInterfaceImpl::setRunning(bool running)
{
_running = running;
}
#else
void NetworkInterfaceImpl::setFlags(short flags)
{
#ifdef POCO_OS_FAMILY_UNIX
_broadcast = ((flags & IFF_BROADCAST) != 0);
_loopback = ((flags & IFF_LOOPBACK) != 0);
_multicast = ((flags & IFF_MULTICAST) != 0);
_pointToPoint = ((flags & IFF_POINTOPOINT) != 0);
_running = ((flags & IFF_RUNNING) != 0);
_up = ((flags & IFF_UP) != 0);
#endif
}
#endif
inline void NetworkInterfaceImpl::setUp(bool up)
{
_up = up;
}
inline void NetworkInterfaceImpl::setMTU(unsigned mtu)
{
_mtu = mtu;
}
inline void NetworkInterfaceImpl::setType(Type type)
{
_type = type;
}
inline void NetworkInterfaceImpl::setIndex(unsigned index)
{
_index = index;
}
inline void NetworkInterfaceImpl::setName(const std::string& name)
{
_name = name;
}
inline void NetworkInterfaceImpl::setDisplayName(const std::string& name)
{
_displayName = name;
}
inline void NetworkInterfaceImpl::setAdapterName(const std::string& name)
{
_adapterName = name;
}
inline void NetworkInterfaceImpl::addAddress(const IPAddress& addr)
{
_addressList.push_back(addr);
}
inline void NetworkInterfaceImpl::setMACAddress(const NetworkInterface::MACAddress& addr)
{
_macAddress = addr;
}
inline void NetworkInterfaceImpl::setMACAddress(const void *addr, std::size_t len)
{
_macAddress.clear();
_macAddress.insert(_macAddress.end(), static_cast<const unsigned char*>(addr), static_cast<const unsigned char*>(addr) + len);
}
//
// NetworkInterface
//
FastMutex NetworkInterface::_mutex;
NetworkInterface::NetworkInterface(unsigned index):
_pImpl(new NetworkInterfaceImpl(index))
{
}
NetworkInterface::NetworkInterface(const NetworkInterface& interfc):
_pImpl(interfc._pImpl)
{
_pImpl->duplicate();
}
NetworkInterface::NetworkInterface(const std::string& name, const std::string& displayName, const std::string& adapterName, const IPAddress& address, unsigned index, MACAddress* pMACAddress):
_pImpl(new NetworkInterfaceImpl(name, displayName, adapterName, address, index, pMACAddress))
{
}
NetworkInterface::NetworkInterface(const std::string& name, const std::string& displayName, const std::string& adapterName, unsigned index, MACAddress* pMACAddress):
_pImpl(new NetworkInterfaceImpl(name, displayName, adapterName, index, pMACAddress))
{
}
NetworkInterface::NetworkInterface(const std::string& name, const IPAddress& address, unsigned index, MACAddress* pMACAddress):
_pImpl(new NetworkInterfaceImpl(name, name, name, address, index, pMACAddress))
{
}
NetworkInterface::NetworkInterface(const std::string& name,
const std::string& displayName,
const std::string& adapterName,
const IPAddress& address,
const IPAddress& subnetMask,
const IPAddress& broadcastAddress,
unsigned index,
MACAddress* pMACAddress):
_pImpl(new NetworkInterfaceImpl(name, displayName, adapterName, address, subnetMask, broadcastAddress, index, pMACAddress))
{
}
NetworkInterface::NetworkInterface(const std::string& name,
const IPAddress& address,
const IPAddress& subnetMask,
const IPAddress& broadcastAddress,
unsigned index,
MACAddress* pMACAddress):
_pImpl(new NetworkInterfaceImpl(name, name, name, address, subnetMask, broadcastAddress, index, pMACAddress))
{
}
NetworkInterface::~NetworkInterface()
{
_pImpl->release();
}
NetworkInterface& NetworkInterface::operator = (const NetworkInterface& interfc)
{
NetworkInterface tmp(interfc);
swap(tmp);
return *this;
}
void NetworkInterface::swap(NetworkInterface& other)
{
using std::swap;
swap(_pImpl, other._pImpl);
}
unsigned NetworkInterface::index() const
{
return _pImpl->index();
}
const std::string& NetworkInterface::name() const
{
return _pImpl->name();
}
const std::string& NetworkInterface::displayName() const
{
return _pImpl->displayName();
}
const std::string& NetworkInterface::adapterName() const
{
return _pImpl->adapterName();
}
const IPAddress& NetworkInterface::firstAddress(IPAddress::Family family) const
{
return _pImpl->firstAddress(family);
}
void NetworkInterface::firstAddress(IPAddress& addr, IPAddress::Family family) const
{
try
{
addr = firstAddress(family);
}
catch (NotFoundException&)
{
addr = IPAddress(family);
}
}
void NetworkInterface::addAddress(const IPAddress& address)
{
_pImpl->addAddress(AddressTuple(address, IPAddress(), IPAddress()));
}
void NetworkInterface::addAddress(const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress)
{
_pImpl->addAddress(AddressTuple(address, subnetMask, broadcastAddress));
}
const IPAddress& NetworkInterface::address(unsigned index) const
{
return _pImpl->address(index);
}
const NetworkInterface::AddressList& NetworkInterface::addressList() const
{
return _pImpl->addressList();
}
const IPAddress& NetworkInterface::subnetMask(unsigned index) const
{
return _pImpl->subnetMask(index);
}
const IPAddress& NetworkInterface::broadcastAddress(unsigned index) const
{
return _pImpl->broadcastAddress(index);
}
const NetworkInterface::MACAddress& NetworkInterface::macAddress() const
{
return _pImpl->macAddress();
}
const IPAddress& NetworkInterface::destAddress(unsigned index) const
{
return _pImpl->destAddress(index);
}
unsigned NetworkInterface::mtu() const
{
return _pImpl->mtu();
}
NetworkInterface::Type NetworkInterface::type() const
{
return _pImpl->type();
}
bool NetworkInterface::supportsIP() const
{
return _pImpl->supportsIPv4() || _pImpl->supportsIPv6();
}
bool NetworkInterface::supportsIPv4() const
{
return _pImpl->supportsIPv4();
}
bool NetworkInterface::supportsIPv6() const
{
return _pImpl->supportsIPv6();
}
bool NetworkInterface::supportsBroadcast() const
{
return _pImpl->broadcast();
}
bool NetworkInterface::supportsMulticast() const
{
return _pImpl->multicast();
}
bool NetworkInterface::isLoopback() const
{
return _pImpl->loopback();
}
bool NetworkInterface::isPointToPoint() const
{
return _pImpl->pointToPoint();
}
bool NetworkInterface::isRunning() const
{
return _pImpl->running();
}
bool NetworkInterface::isUp() const
{
return _pImpl->up();
}
NetworkInterface NetworkInterface::forName(const std::string& name, bool requireIPv6)
{
Map map = NetworkInterface::map(false, false);
Map::const_iterator it = map.begin();
Map::const_iterator end = map.end();
for (; it != end; ++it)
{
if (it->second.name() == name && ((requireIPv6 && it->second.supportsIPv6()) || !requireIPv6))
return it->second;
}
throw InterfaceNotFoundException(name);
}
NetworkInterface NetworkInterface::forName(const std::string& name, IPVersion ipVersion)
{
Map map = NetworkInterface::map(false, false);
Map::const_iterator it = map.begin();
Map::const_iterator end = map.end();
for (; it != end; ++it)
{
if (it->second.name() == name)
{
if (ipVersion == IPv4_ONLY && it->second.supportsIPv4())
return it->second;
else if (ipVersion == IPv6_ONLY && it->second.supportsIPv6())
return it->second;
else if (ipVersion == IPv4_OR_IPv6)
return it->second;
}
}
throw InterfaceNotFoundException(name);
}
NetworkInterface NetworkInterface::forAddress(const IPAddress& addr)
{
Map map = NetworkInterface::map(true, false);
Map::const_iterator it = map.begin();
Map::const_iterator end = map.end();
for (; it != end; ++it)
{
const std::size_t count = it->second.addressList().size();
for (int i = 0; i < count; ++i)
{
if (it->second.address(i) == addr)
return it->second;
}
}
throw InterfaceNotFoundException(addr.toString());
}
NetworkInterface NetworkInterface::forIndex(unsigned i)
{
if (i != NetworkInterface::NO_INDEX)
{
Map map = NetworkInterface::map(false, false);
Map::const_iterator it = map.find(i);
if (it != map.end())
return it->second;
else
throw InterfaceNotFoundException("#" + NumberFormatter::format(i));
}
throw InterfaceNotFoundException("#" + NumberFormatter::format(i));
}
NetworkInterface::List NetworkInterface::list(bool ipOnly, bool upOnly)
{
List list;
Map m = map(ipOnly, upOnly);
NetworkInterface::Map::const_iterator it = m.begin();
NetworkInterface::Map::const_iterator end = m.end();
for (; it != end; ++it)
{
int index = it->second.index();
std::string name = it->second.name();
std::string displayName = it->second.displayName();
std::string adapterName = it->second.adapterName();
NetworkInterface::MACAddress mac = it->second.macAddress();
typedef NetworkInterface::AddressList List;
const List& ipList = it->second.addressList();
List::const_iterator ipIt = ipList.begin();
List::const_iterator ipEnd = ipList.end();
for (int counter = 0; ipIt != ipEnd; ++ipIt, ++counter)
{
IPAddress addr = ipIt->get<NetworkInterface::IP_ADDRESS>();
IPAddress mask = ipIt->get<NetworkInterface::SUBNET_MASK>();
NetworkInterface ni;
if (mask.isWildcard())
{
ni = NetworkInterface(name, displayName, adapterName, addr, index, &mac);
}
else
{
IPAddress broadcast = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>();
ni = NetworkInterface(name, displayName, adapterName, addr, mask, broadcast, index, &mac);
}
ni._pImpl->_broadcast = it->second._pImpl->_broadcast;
ni._pImpl->_loopback = it->second._pImpl->_loopback;
ni._pImpl->_multicast = it->second._pImpl->_multicast;
ni._pImpl->_pointToPoint = it->second._pImpl->_pointToPoint;
ni._pImpl->_up = it->second._pImpl->_up;
ni._pImpl->_running = it->second._pImpl->_running;
ni._pImpl->_mtu = it->second._pImpl->_mtu;
ni._pImpl->_type = it->second._pImpl->_type;
list.push_back(ni);
}
}
return list;
}
} } // namespace Poco::Net
//
// platform-specific code below
//
#if defined(POCO_OS_FAMILY_WINDOWS)
//
// Windows
//
#include "Poco/Buffer.h"
#include <iterator>
namespace Poco {
namespace Net {
namespace {
IPAddress getBroadcastAddress(PIP_ADAPTER_PREFIX pPrefix, const IPAddress& addr, ULONG* pprefix = 0)
/// This function relies on (1) subnet prefix being at the position
/// immediately preceding and (2) broadcast address being at the position
/// immediately succeeding the IPv4 unicast address.
///
/// Since there is no explicit guarantee on order, to ensure correctness,
/// the above constraints are checked prior to returning the result.
/// Additionally, on pre-Vista versions on Windows, the main structure does
/// not contain prefix length; for those platforms, this function
/// returns prefix through pprefix argument.
{
PIP_ADAPTER_PREFIX pPrev = 0;
for (int i = 0; pPrefix; pPrefix = pPrefix->Next, ++i)
{
ADDRESS_FAMILY family = pPrefix->Address.lpSockaddr->sa_family;
if ((family == AF_INET) && (addr == IPAddress(pPrefix->Address)))
break;
pPrev = pPrefix;
}
if (pPrefix && pPrefix->Next && pPrev)
{
IPAddress ipPrefix(pPrev->PrefixLength, IPAddress::IPv4);
IPAddress mask(pPrefix->Next->Address);
if ((ipPrefix & mask) == (ipPrefix & addr))
{
if (pprefix) *pprefix = pPrefix->PrefixLength;
return IPAddress(pPrefix->Next->Address);
}
}
return IPAddress(IPAddress::IPv4);
}
NetworkInterface::Type fromNative(DWORD type)
{
switch (type)
{
case IF_TYPE_ETHERNET_CSMACD: return NetworkInterface::NI_TYPE_ETHERNET_CSMACD;
case IF_TYPE_ISO88025_TOKENRING: return NetworkInterface::NI_TYPE_ISO88025_TOKENRING;
case IF_TYPE_FRAMERELAY: return NetworkInterface::NI_TYPE_FRAMERELAY;
case IF_TYPE_PPP: return NetworkInterface::NI_TYPE_PPP;
case IF_TYPE_SOFTWARE_LOOPBACK: return NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK;
case IF_TYPE_ATM: return NetworkInterface::NI_TYPE_ATM;
case IF_TYPE_IEEE80211: return NetworkInterface::NI_TYPE_IEEE80211;
case IF_TYPE_TUNNEL: return NetworkInterface::NI_TYPE_TUNNEL;
case IF_TYPE_IEEE1394: return NetworkInterface::NI_TYPE_IEEE1394;
default: return NetworkInterface::NI_TYPE_OTHER;
}
}
IPAddress subnetMaskForInterface(const std::string& name, bool isLoopback)
{
if (isLoopback)
{
return IPAddress::parse("255.0.0.0");
}
else
{