blob: a2020153bc7ae969b5cb87a163f26fcfe2680b48 (
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
|
/*
* Copyright (c) 2011 Tobias Markmann
* Licensed under the simplified BSD license.
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
#include <Swiften/Network/WindowsNetworkEnvironment.h>
#include <string>
#include <vector>
#include <map>
#include <boost/optional.hpp>
#include <Swiften/Network/HostAddress.h>
#include <Swiften/Network/NetworkInterface.h>
#include <Swiften/Base/foreach.h>
#include <Swiften/Base/ByteArray.h>
#include <winsock2.h>
#include <iphlpapi.h>
namespace Swift {
std::vector<NetworkInterface> WindowsNetworkEnvironment::getNetworkInterfaces() const {
std::vector<NetworkInterface> result;
std::vector<IP_ADAPTER_ADDRESSES> 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, vecptr(adapters), &bufferSize)) == ERROR_BUFFER_OVERFLOW) {
adapters.resize(bufferSize / sizeof(IP_ADAPTER_ADDRESSES));
};
if (ret != ERROR_SUCCESS) {
return result;
}
std::map<std::string,NetworkInterface> interfaces;
for (IP_ADAPTER_ADDRESSES* adapter = 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) {
std::map<std::string, NetworkInterface>::iterator i = interfaces.insert(std::make_pair(name, NetworkInterface(name, false))).first;
i->second.addAddress(*hostAddress);
}
}
}
for (std::map<std::string,NetworkInterface>::const_iterator i = interfaces.begin(); i != interfaces.end(); ++i) {
result.push_back(i->second);
}
return result;
}
}
|