MCPcopy Create free account
hub / github.com/catboost/catboost / GetNetworkInterfaces

Function GetNetworkInterfaces

util/network/interface.cpp:19–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17 }
18
19 TNetworkInterfaceList GetNetworkInterfaces() {
20 TNetworkInterfaceList result;
21
22#ifdef _win_
23 TVector<char> buf;
24 buf.resize(1000000);
25 PIP_ADAPTER_ADDRESSES adapterBuf = (PIP_ADAPTER_ADDRESSES)&buf[0];
26 ULONG bufSize = buf.size();
27
28 if (GetAdaptersAddresses(AF_UNSPEC, 0, nullptr, adapterBuf, &bufSize) == ERROR_SUCCESS) {
29 for (PIP_ADAPTER_ADDRESSES ptr = adapterBuf; ptr != 0; ptr = ptr->Next) {
30 // The check below makes code working on Vista+
31 if ((ptr->Flags & (IP_ADAPTER_IPV4_ENABLED | IP_ADAPTER_IPV6_ENABLED)) == 0) {
32 continue;
33 }
34 if (ptr->IfType == IF_TYPE_TUNNEL) {
35 // ignore tunnels
36 continue;
37 }
38 if (ptr->OperStatus != IfOperStatusUp) {
39 // ignore disable adapters
40 continue;
41 }
42
43 for (IP_ADAPTER_UNICAST_ADDRESS* addr = ptr->FirstUnicastAddress; addr != 0; addr = addr->Next) {
44 sockaddr* a = (sockaddr*)addr->Address.lpSockaddr;
45 if (IsInetAddress(a)) {
46 TNetworkInterface networkInterface;
47
48 // Not very efficient but straightforward
49 wchar_t* it = ptr->FriendlyName;
50 while (*it != '\0') {
51 networkInterface.Name += IsAscii(*it) ? static_cast<char>(*it) : '?';
52 ++it;
53 }
54
55 networkInterface.Address = new TOpaqueAddr(a);
56 result.push_back(networkInterface);
57 }
58 }
59 }
60 }
61#else
62 ifaddrs* ifap;
63 if (getifaddrs(&ifap) != -1) {
64 for (ifaddrs* ifa = ifap; ifa != nullptr; ifa = ifa->ifa_next) {
65 if (IsInetAddress(ifa->ifa_addr)) {
66 TNetworkInterface interface;
67 interface.Name = ifa->ifa_name;
68 interface.Address = new TOpaqueAddr(ifa->ifa_addr);
69 if (IsInetAddress(ifa->ifa_netmask)) {
70 interface.Mask = new TOpaqueAddr(ifa->ifa_netmask);
71 }
72 result.push_back(interface);
73 }
74 }
75 freeifaddrs(ifap);
76 }

Callers

nothing calls this directly

Calls 7

IsInetAddressFunction · 0.85
IsAsciiFunction · 0.85
getifaddrsFunction · 0.85
freeifaddrsFunction · 0.85
resizeMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected