MCPcopy Create free account
hub / github.com/davisking/dlib / ip_to_hostname

Function ip_to_hostname

dlib/sockets/sockets_kernel_1.cpp:188–231  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

186// -----------------
187
188 int
189 ip_to_hostname (
190 const std::string& ip,
191 std::string& hostname
192 )
193 {
194 // ensure that WSAStartup has been called and WSACleanup will eventually
195 // be called when program ends
196 sockets_startup();
197
198 try
199 {
200 // lock this mutex since gethostbyaddr isn't really thread safe
201 auto_mutex M(sockets_kernel_1_mutex::startup_lock);
202
203 // if no ip was given then return error
204 if (ip.empty())
205 return OTHER_ERROR;
206
207 hostent* address;
208 unsigned long ipnum = inet_addr(ip.c_str());
209
210 // if inet_addr couldn't convert ip then return an error
211 if (ipnum == INADDR_NONE)
212 {
213 return OTHER_ERROR;
214 }
215 address = gethostbyaddr(reinterpret_cast<char*>(&ipnum),4,AF_INET);
216
217 // check if gethostbyaddr returned an error
218 if (address == 0)
219 {
220 return OTHER_ERROR;
221 }
222 hostname.assign(address->h_name);
223
224 }
225 catch (...)
226 {
227 return OTHER_ERROR;
228 }
229 return 0;
230
231 }
232
233// ----------------------------------------------------------------------------------------
234// ----------------------------------------------------------------------------------------

Callers

nothing calls this directly

Calls 3

sockets_startupFunction · 0.70
emptyMethod · 0.45
assignMethod · 0.45

Tested by

no test coverage detected