blob: e90a5c6b66b5bc4bf34a723e2487090e09d11241 (
plain)
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
|
/*
* Copyright (c) 2011 Tobias Markmann
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
/*
* Copyright (c) 2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swiften/Network/WindowsNetworkEnvironment.h>
#include <map>
#include <string>
#include <vector>
#include <boost/optional.hpp>
#include <iphlpapi.h>
#include <winsock2.h>
#include <Swiften/Base/ByteArray.h>
#include <Swiften/Network/HostAddress.h>
#include <Swiften/Network/NetworkInterface.h>
namespace Swift {
std::vector<NetworkInterface> WindowsNetworkEnvironment::getNetworkInterfaces() const {
std::vector<NetworkInterface> result;
ByteArray adapters;
ULONG bufferSize = 0;
ULONG ret;
ULONG flags = GAA_FLAG_INCLUDE_ALL_INTERFACES | GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
while ((ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, reinterpret_cast<IP_ADAPTER_ADDRESSES*>(vecptr(adapters)), &bufferSize)) == ERROR_BUFFER_OVERFLOW) {
adapters.resize(bufferSize);
};
if (ret != ERROR_SUCCESS) {
return result;
}
std::map<std::string,NetworkInterface> interfaces;
for (IP_ADAPTER_ADDRESSES* adapter = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(vecptr(adapters)); adapter; adapter = adapter->Next) {
std::string name(adapter->AdapterName);
if (adapter->OperStatus != IfOperStatusUp) {
continue;
}
for (IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; address; address = address->Next) {
boost::optional<HostAddress> hostAddress;
if (address->Address.lpSockaddr->sa_family == PF_INET) {
sockaddr_in* sa = reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr);
hostAddress = HostAddress(reinterpret_cast<const unsigned char*>(&(sa->sin_addr)), 4);
}
else if (address->Address.lpSockaddr->sa_family == PF_INET6) {
sockaddr_in6* sa = reinterpret_cast<sockaddr_in6*>(address->Address.lpSockaddr);
hostAddress = HostAddress(reinterpret_cast<const unsigned char*>(&(sa->sin6_addr)), 16);
}
if (hostAddress && !hostAddress->isLocalhost()) {
std::map<std::string, NetworkInterface>::iterator i = interfaces.insert(std::make_pair(name, NetworkInterface(name, false))).first;
i->second.addAddress(*hostAddress);
}
}
}
for (const auto& interface : interfaces) {
result.push_back(interface.second);
}
return result;
}
}
|