Lines 255-291
Link Here
|
255 |
m_iBufferLength = 0; |
255 |
m_iBufferLength = 0; |
256 |
m_iBufferPos = 0; |
256 |
m_iBufferPos = 0; |
257 |
|
257 |
|
258 |
struct hostent *hinfo; |
258 |
int res; |
259 |
|
259 |
|
260 |
hinfo = ::gethostbyname( m_pNetAddress->GetHost() ); |
260 |
struct addrinfo addr_hints, *addr_list, *addr; |
261 |
|
261 |
char iPortStr[sizeof(int) * 4 + 1]; //is enough to hold any converted int |
262 |
if( !hinfo ) |
262 |
|
|
|
263 |
memset(&addr_hints, 0, sizeof(addr_hints)); |
264 |
addr_hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ |
265 |
addr_hints.ai_socktype = SOCK_STREAM, |
266 |
|
267 |
sprintf(iPortStr, "%d", m_pNetAddress->GetPort()); |
268 |
|
269 |
res = ::getaddrinfo( m_pNetAddress->GetHost(), iPortStr, &addr_hints, &addr_list ); |
270 |
|
271 |
if( res != 0 ) |
263 |
{ |
272 |
{ |
264 |
error( "Could not resolve hostname %s", m_pNetAddress->GetHost() ); |
273 |
error( "Could not resolve hostname %s", m_pNetAddress->GetHost() ); |
265 |
return -1; |
274 |
return -1; |
266 |
} |
275 |
} |
267 |
|
276 |
|
268 |
memset(&m_SocketAddress, '\0', sizeof(m_SocketAddress)); |
277 |
for (addr = addr_list; addr != NULL; addr = addr->ai_next) |
269 |
m_SocketAddress.sin_family = AF_INET; |
278 |
{ |
270 |
memcpy(&m_SocketAddress.sin_addr.s_addr, hinfo->h_addr_list[0], sizeof(m_SocketAddress.sin_addr.s_addr)); |
279 |
m_iSocket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); |
271 |
m_SocketAddress.sin_port = htons( m_pNetAddress->GetPort() ); |
280 |
if (m_iSocket == -1) continue; |
272 |
|
281 |
|
273 |
m_iSocket = ::socket(PF_INET,SOCK_STREAM,0); |
282 |
res = ::connect( m_iSocket , addr->ai_addr, addr->ai_addrlen ); |
274 |
|
283 |
if (res != -1) break; /* Connection established */ |
275 |
if( m_iSocket < 0 ) |
284 |
} |
276 |
{ |
285 |
|
277 |
error("Socket creation failed for %s!", m_pNetAddress->GetHost() ); |
286 |
if( addr == NULL ) |
278 |
return -1; |
|
|
279 |
} |
280 |
|
281 |
int res = ::connect( m_iSocket ,(struct sockaddr *) &m_SocketAddress, sizeof(m_SocketAddress) ); |
282 |
|
283 |
if (res < 0) |
284 |
{ |
287 |
{ |
285 |
error("Connection to %s failed!", m_pNetAddress->GetHost() ); |
288 |
error("Socket creation or connection failed for %s!", m_pNetAddress->GetHost() ); |
286 |
return -1; |
289 |
return -1; |
287 |
} |
290 |
} |
288 |
|
291 |
|
|
|
292 |
freeaddrinfo( addr_list ); /* No longer needed */ |
293 |
|
289 |
m_pFileBuffer = fdopen( m_iSocket, "r+" ); |
294 |
m_pFileBuffer = fdopen( m_iSocket, "r+" ); |
290 |
|
295 |
|
291 |
if( !m_pFileBuffer ) |
296 |
if( !m_pFileBuffer ) |
Lines 339-372
Link Here
|
339 |
{ |
344 |
{ |
340 |
debug("Connection::DoBind()"); |
345 |
debug("Connection::DoBind()"); |
341 |
|
346 |
|
342 |
struct hostent *hinfo; |
347 |
int res; |
343 |
|
348 |
|
344 |
hinfo = ::gethostbyname( m_pNetAddress->GetHost() ); |
349 |
struct addrinfo addr_hints, *addr_list, *addr; |
345 |
|
350 |
char iPortStr[sizeof(int) * 4 + 1]; //is enough to hold any converted int |
346 |
if( !hinfo ) |
351 |
|
|
|
352 |
memset(&addr_hints, 0, sizeof(addr_hints)); |
353 |
addr_hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ |
354 |
addr_hints.ai_socktype = SOCK_STREAM, |
355 |
addr_hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ |
356 |
|
357 |
sprintf(iPortStr, "%d", m_pNetAddress->GetPort()); |
358 |
|
359 |
res = ::getaddrinfo( m_pNetAddress->GetHost(), iPortStr, &addr_hints, &addr_list ); |
360 |
|
361 |
if( res != 0 ) |
347 |
{ |
362 |
{ |
348 |
error( "Could not resolve hostname %s", m_pNetAddress->GetHost() ); |
363 |
error( "Could not resolve hostname %s", m_pNetAddress->GetHost() ); |
349 |
return -1; |
364 |
return -1; |
350 |
} |
365 |
} |
351 |
|
366 |
|
352 |
m_iSocket = ::socket( PF_INET, SOCK_STREAM, 0 ); |
367 |
for (addr = addr_list; addr != NULL; addr = addr->ai_next) |
353 |
|
368 |
{ |
354 |
if( m_iSocket < 0 ) |
369 |
m_iSocket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); |
|
|
370 |
if (m_iSocket == -1) continue; |
371 |
|
372 |
res = ::bind( m_iSocket , addr->ai_addr, addr->ai_addrlen ); |
373 |
if (res != -1) break; /* Connection established */ |
374 |
} |
375 |
|
376 |
if( addr == NULL ) |
355 |
{ |
377 |
{ |
356 |
error("Socket creation failed for %s!", m_pNetAddress->GetHost() ); |
378 |
error("Socket creation or binding failed for %s!", m_pNetAddress->GetHost() ); |
357 |
return -1; |
379 |
return -1; |
358 |
} |
380 |
} |
359 |
|
381 |
|
360 |
memset(&m_SocketAddress, '\0', sizeof(m_SocketAddress)); |
382 |
freeaddrinfo( addr_list ); /* No longer needed */ |
361 |
m_SocketAddress.sin_family = AF_INET; |
|
|
362 |
m_SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY); |
363 |
m_SocketAddress.sin_port = htons( m_pNetAddress->GetPort() ); |
364 |
|
365 |
if( bind( m_iSocket, (struct sockaddr *) &m_SocketAddress, sizeof(m_SocketAddress)) < 0) |
366 |
{ |
367 |
error( "binding socket failed for %s!", m_pNetAddress->GetHost() ); |
368 |
return -1; |
369 |
} |
370 |
|
383 |
|
371 |
if( listen( m_iSocket, 10 ) < 0 ) |
384 |
if( listen( m_iSocket, 10 ) < 0 ) |
372 |
{ |
385 |
{ |