25 #pragma comment(lib, "iphlpapi.lib") 26 #pragma comment(lib, "ws2_32.lib") 28 #include <coil/Routing.h> 29 #include <coil/stringutil.h> 32 #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) 33 #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) 44 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
64 hp = ::gethostbyname(dest_addr.c_str());
65 if (hp == 0) {
return false; }
68 while (hp->h_addr_list[i] != 0)
70 if(hp->h_addrtype == AF_INET)
72 struct sockaddr_in addr;
73 memset((
char*)&addr, 0,
sizeof(addr));
74 memcpy((
char*)&addr.sin_addr, hp->h_addr_list[i], hp->h_length);
75 dest_addr = inet_ntoa(addr.sin_addr);
82 UINT ipaddress(inet_addr(dest_addr.c_str()));
83 if (ipaddress == INADDR_NONE) {
return false; }
86 if (NO_ERROR != GetBestInterface(ipaddress, &bestifindex)) {
return false; }
88 PMIB_IPADDRTABLE ipaddr_table;
89 ipaddr_table = (MIB_IPADDRTABLE *)
MALLOC(
sizeof (MIB_IPADDRTABLE));
90 if (ipaddr_table == 0) {
return false; }
95 if (GetIpAddrTable(ipaddr_table, &size, 0) == ERROR_INSUFFICIENT_BUFFER)
98 ipaddr_table = (MIB_IPADDRTABLE *)
MALLOC(size);
100 if (ipaddr_table == 0) {
return false; }
101 if (GetIpAddrTable(ipaddr_table, &size, 0) != NO_ERROR) {
return false; }
103 for (
int i(0); i < (int) ipaddr_table->dwNumEntries; ++i)
105 if (bestifindex == ipaddr_table->table[i].dwIndex)
108 inipaddr.S_un.S_addr = (u_long) ipaddr_table->table[i].dwAddr;
109 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.