| 110 | #ifndef __CYGWIN__ |
| 111 | |
| 112 | int |
| 113 | hostname_to_ip ( |
| 114 | const std::string& hostname, |
| 115 | std::string& ip, |
| 116 | int n |
| 117 | ) |
| 118 | { |
| 119 | try |
| 120 | { |
| 121 | set<std::string>::kernel_1a sos; |
| 122 | |
| 123 | if (hostname.empty()) |
| 124 | return OTHER_ERROR; |
| 125 | |
| 126 | addrinfo* result = 0; |
| 127 | if (getaddrinfo(hostname.c_str(),0,0,&result)) |
| 128 | { |
| 129 | return OTHER_ERROR; |
| 130 | } |
| 131 | addrinfo* result_orig = result; |
| 132 | |
| 133 | // loop over all the addrinfo structures and add them to the set. the reason for doing |
| 134 | // this dumb crap is because different platforms return all kinds of weird garbage. many |
| 135 | // return the same ip multiple times, etc. |
| 136 | while (result != 0) |
| 137 | { |
| 138 | char temp[16]; |
| 139 | inet_ntop ( |
| 140 | AF_INET, |
| 141 | &((reinterpret_cast<sockaddr_in*>(result->ai_addr))->sin_addr), |
| 142 | temp,16 |
| 143 | ); |
| 144 | |
| 145 | result = result->ai_next; |
| 146 | |
| 147 | ip.assign(temp); |
| 148 | if (sos.is_member(ip) == false && ip != "0.0.0.0") |
| 149 | sos.add(ip); |
| 150 | } |
| 151 | |
| 152 | freeaddrinfo(result_orig); |
| 153 | |
| 154 | // now return the nth unique ip address |
| 155 | int i = 0; |
| 156 | while (sos.move_next()) |
| 157 | { |
| 158 | if (i == n) |
| 159 | { |
| 160 | ip = sos.element(); |
| 161 | return 0; |
| 162 | } |
| 163 | ++i; |
| 164 | } |
| 165 | |
| 166 | return OTHER_ERROR; |
| 167 | } |
| 168 | catch (...) |
| 169 | { |