Skip to content

Commit

Permalink
Better use of getaddrinfo().
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Aug 31, 2008
1 parent 6a7d2a7 commit 6651512
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
22 changes: 17 additions & 5 deletions mojocrash_unix.c
Expand Up @@ -351,13 +351,28 @@ static void dns_resolver_thread(void *_dns)
{
char portstr[64];
int app_done = 0;
struct addrinfo hints;
DnsResolve *dns = (DnsResolve *) _dns;
int rc;
int rc = -1;

#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0
#endif
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif
#ifndef AI_V4MAPPED
#define AI_V4MAPPED 0
#endif
memset(&hints, '\0', sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICSERV | AI_V4MAPPED | AI_ADDRCONFIG;

MOJOCRASH_LongToString(dns->port, portstr);

/* this blocks, hence all the thread tapdancing. */
rc = getaddrinfo(dns->host, portstr, NULL, &dns->addr);
rc = getaddrinfo(dns->host, portstr, &hints, &dns->addr);

pthread_mutex_lock(&dns->mutex);
dns->status = (rc == 0) ? 1 : -1;
Expand Down Expand Up @@ -465,9 +480,6 @@ void *MOJOCRASH_platform_open_socket(void *_dns, const int blocking)

for (addr = dns->addr; addr != NULL; addr = addr->ai_next)
{
if (addr->ai_socktype != SOCK_STREAM)
continue;

if (fd != -1)
close(fd);

Expand Down
19 changes: 16 additions & 3 deletions mojocrash_windows.c
Expand Up @@ -314,6 +314,22 @@ static void dns_resolver_thread(void *_dns)
if (pgetaddrinfo != NULL)
{
char portstr[64];
struct addrinfo hints;

#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0
#endif
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif
#ifndef AI_V4MAPPED
#define AI_V4MAPPED 0
#endif
ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICSERV | AI_V4MAPPED | AI_ADDRCONFIG;

MOJOCRASH_LongToString(dns->port, portstr);
rc = pgetaddrinfo(dns->host, portstr, NULL, &dns->addr);
} /* if */
Expand Down Expand Up @@ -423,9 +439,6 @@ void *MOJOCRASH_platform_open_socket(void *_dns, const int blocking)
const struct addrinfo *addr;
for (addr = dns->addr; addr != NULL; addr = addr->ai_next)
{
if (addr->ai_socktype != SOCK_STREAM)
continue;

if (fd != INVALID_SOCKET_HANDLE)
pclosesocket(fd);

Expand Down

0 comments on commit 6651512

Please sign in to comment.