| 86 | #ifndef __CYGWIN__ |
| 87 | |
| 88 | int |
| 89 | hostname_to_ip ( |
| 90 | const std::string& hostname, |
| 91 | std::string& ip, |
| 92 | int n |
| 93 | ) |
| 94 | { |
| 95 | try |
| 96 | { |
| 97 | set<std::string>::kernel_1a sos; |
| 98 | |
| 99 | if (hostname.empty()) |
| 100 | return OTHER_ERROR; |
| 101 | |
| 102 | addrinfo* result = 0; |
| 103 | if (getaddrinfo(hostname.c_str(),0,0,&result)) |
| 104 | { |
| 105 | return OTHER_ERROR; |
| 106 | } |
| 107 | addrinfo* result_orig = result; |
| 108 | |
| 109 | // loop over all the addrinfo structures and add them to the set. the reason for doing |
| 110 | // this dumb crap is because different platforms return all kinds of weird garbage. many |
| 111 | // return the same ip multiple times, etc. |
| 112 | while (result != 0) |
| 113 | { |
| 114 | char temp[16]; |
| 115 | inet_ntop ( |
| 116 | AF_INET, |
| 117 | &((reinterpret_cast<sockaddr_in*>(result->ai_addr))->sin_addr), |
| 118 | temp,16 |
| 119 | ); |
| 120 | |
| 121 | result = result->ai_next; |
| 122 | |
| 123 | ip.assign(temp); |
| 124 | if (sos.is_member(ip) == false && ip != "0.0.0.0") |
| 125 | sos.add(ip); |
| 126 | } |
| 127 | |
| 128 | freeaddrinfo(result_orig); |
| 129 | |
| 130 | // now return the nth unique ip address |
| 131 | int i = 0; |
| 132 | while (sos.move_next()) |
| 133 | { |
| 134 | if (i == n) |
| 135 | { |
| 136 | ip = sos.element(); |
| 137 | return 0; |
| 138 | } |
| 139 | ++i; |
| 140 | } |
| 141 | |
| 142 | return OTHER_ERROR; |
| 143 | } |
| 144 | catch (...) |
| 145 | { |