14 #pragma comment(lib, "IPHLPAPI.lib")
42 void Win32LinkManager::freeAdapterInfo(IP_ADAPTER_ADDRESSES *pAddresses)
const
44 HeapFree(GetProcessHeap(), 0, (pAddresses));
47 IP_ADAPTER_ADDRESSES* Win32LinkManager::getAdapterInfo()
const
49 PIP_ADAPTER_ADDRESSES pAddresses = NULL;
52 ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
55 ULONG family = AF_UNSPEC;
61 ULONG outBufLen = 15000;
63 for (
int i = 0; i < 3; i++)
65 pAddresses = (IP_ADAPTER_ADDRESSES *) HeapAlloc(GetProcessHeap(), 0, (outBufLen));
66 if (pAddresses == NULL) {
70 dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
72 if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
73 freeAdapterInfo(pAddresses);
80 if (dwRetVal == NO_ERROR)
return pAddresses;
82 if (pAddresses) freeAdapterInfo(pAddresses);
84 if (dwRetVal == ERROR_NO_DATA) {
88 ss <<
"Call to GetAdaptersAddresses failed with error: " << dwRetVal;
97 PIP_ADAPTER_ADDRESSES pAddresses = getAdapterInfo();
100 for (PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses; pCurrAddresses->Next != NULL; pCurrAddresses = pCurrAddresses->Next)
102 if (pCurrAddresses->IfIndex == index) {
103 ret =
vinterface(pCurrAddresses->AdapterName);
108 freeAdapterInfo(pAddresses);
112 vaddress Win32LinkManager::getAddress(SOCKET_ADDRESS &sockaddr)
const {
113 char str_address[256];
114 DWORD str_length =
static_cast<DWORD
>(
sizeof str_address);
115 if (WSAAddressToString(sockaddr.lpSockaddr, sockaddr.iSockaddrLength, NULL, str_address, &str_length) == 0) {
116 return vaddress(std::string(str_address),
"");
124 std::list<vaddress> ret;
126 PIP_ADAPTER_ADDRESSES pAddresses = getAdapterInfo();
128 for (PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses; pCurrAddresses->Next != NULL; pCurrAddresses = pCurrAddresses->Next)
130 const std::string name(pCurrAddresses->AdapterName);
133 PIP_ADAPTER_UNICAST_ADDRESS pUnicast;
135 for (pUnicast = pCurrAddresses->FirstUnicastAddress; pUnicast != NULL; pUnicast = pUnicast->Next) {
137 if (pUnicast->Flags & IP_ADAPTER_ADDRESS_TRANSIENT)
continue;
139 if (scope.length() > 0) {
141 if (pUnicast->Address.lpSockaddr->sa_family == AF_INET6) {
143 sockaddr_in6 *addr6 = (sockaddr_in6*)pUnicast->Address.lpSockaddr;
146 if (addr6->sin6_scope_id == 0) {
155 ret.push_back( getAddress(pUnicast->Address) );
162 freeAdapterInfo(pAddresses);
170 std::set<ibrcommon::vinterface> ret;
172 PIP_ADAPTER_ADDRESSES pAddresses = getAdapterInfo();
174 for (PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses; pCurrAddresses->Next != NULL; pCurrAddresses = pCurrAddresses->Next)
176 const std::string name(pCurrAddresses->AdapterName);
177 const std::wstring friendlyName(pCurrAddresses->FriendlyName);
183 freeAdapterInfo(pAddresses);