21 #define NTDDI_VERSION 0x05000000 22 #define WINVER _WIN32_WINNT 25 #endif // _WIN32_WINNT 26 #define _WIN32_WINNT 0x0500 27 #define _WIN32_WINDOWS _WIN32_WINNT 28 #define _WIN32_IE 0x0501 29 #endif // NTDDI_VERSION 35 #pragma comment(lib, "iphlpapi.lib") 36 #pragma comment(lib, "ws2_32.lib") 38 #include <coil/Routing.h> 39 #include <coil/stringutil.h> 42 #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) 43 #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) 54 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
74 hp = ::gethostbyname(dest_addr.c_str());
75 if (hp == 0) {
return false; }
78 while (hp->h_addr_list[i] != 0)
80 if(hp->h_addrtype == AF_INET)
82 struct sockaddr_in addr;
83 memset((
char*)&addr, 0,
sizeof(addr));
84 memcpy((
char*)&addr.sin_addr, hp->h_addr_list[i], hp->h_length);
85 dest_addr = inet_ntoa(addr.sin_addr);
92 UINT ipaddress(inet_addr(dest_addr.c_str()));
93 if (ipaddress == INADDR_NONE) {
return false; }
96 if (NO_ERROR != GetBestInterface(ipaddress, &bestifindex)) {
return false; }
98 PMIB_IPADDRTABLE ipaddr_table;
99 ipaddr_table = (MIB_IPADDRTABLE *)
MALLOC(
sizeof (MIB_IPADDRTABLE));
100 if (ipaddr_table == 0) {
return false; }
105 if (GetIpAddrTable(ipaddr_table, &size, 0) == ERROR_INSUFFICIENT_BUFFER)
108 ipaddr_table = (MIB_IPADDRTABLE *)
MALLOC(size);
110 if (ipaddr_table == 0) {
return false; }
111 if (GetIpAddrTable(ipaddr_table, &size, 0) != NO_ERROR) {
return false; }
113 for (
int i(0); i < (int) ipaddr_table->dwNumEntries; ++i)
115 if (bestifindex == ipaddr_table->table[i].dwIndex)
118 inipaddr.S_un.S_addr = (u_long) ipaddr_table->table[i].dwAddr;
119 endpoint = inet_ntoa(inipaddr);
bool dest_to_endpoint(std::string dest_addr, std::string &endpoint)
Getting network interface name from destination address.
Common Object Interface Layer.