Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use getaddrinfo over gethostbyname #689

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions libs/mysql/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,30 @@ int psock_recv( PSOCK s, char *buf, int size ) {
return ret;
}

PHOST phost_resolve( const char *host ) {
PHOST ip = inet_addr(host);
if( ip == INADDR_NONE ) {
struct hostent *h;
# if defined(OS_WINDOWS) || defined(OS_MAC) || defined(OS_CYGWIN)
h = gethostbyname(host);
# else
struct hostent hbase;
char buf[1024];
int errcode;
gethostbyname_r(host,&hbase,buf,1024,&h,&errcode);
# endif
if( h == NULL )
return UNRESOLVED_HOST;
ip = *((unsigned int*)h->h_addr_list[0]);
}
return ip;
PHOST phost_resolve(const char *host) {
struct addrinfo hints, *res;
PHOST ip = INADDR_NONE;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

int status = getaddrinfo(host, NULL, &hints, &res);
if (status != 0) {
return UNRESOLVED_HOST;
}

if (res != NULL) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
ip = ipv4->sin_addr.s_addr;
}

freeaddrinfo(res);

return ip;
}


SERR psock_connect( PSOCK s, PHOST host, int port ) {
struct sockaddr_in addr;
memset(&addr,0,sizeof(addr));
Expand Down
39 changes: 21 additions & 18 deletions src/std/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,30 @@ HL_PRIM int hl_socket_recv_char( hl_socket *s ) {
return (unsigned char)cc;
}

HL_PRIM int hl_host_resolve( vbyte *host ) {
unsigned int ip;
HL_PRIM int hl_host_resolve(vbyte *host) {
struct addrinfo hints, *res;
unsigned int ip = INADDR_NONE;

hl_blocking(true);
ip = inet_addr((char*)host);
if( ip == INADDR_NONE ) {
struct hostent *h;
# if defined(HL_WIN) || defined(HL_MAC) || defined(HL_IOS) || defined(HL_TVOS) || defined (HL_CYGWIN) || defined(HL_CONSOLE)
h = gethostbyname((char*)host);
# else
struct hostent hbase;
char buf[1024];
int errcode;
gethostbyname_r((char*)host,&hbase,buf,1024,&h,&errcode);
# endif
if( h == NULL ) {
hl_blocking(false);
return -1;
}
ip = *((unsigned int*)h->h_addr_list[0]);

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

int status = getaddrinfo((char*)host, NULL, &hints, &res);
if (status != 0) {
hl_blocking(false);
return -1;
}

if (res != NULL) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
ip = ipv4->sin_addr.s_addr;
}

freeaddrinfo(res);
hl_blocking(false);

return ip;
}

Expand Down