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

Function ip_to_hostname

dlib/sockets/sockets_kernel_1.cpp:187–230  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

sockets_startupFunction · 0.70
emptyMethod · 0.45
assignMethod · 0.45

Tested by

no test coverage detected