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

Port gethostbyname to getaddrinfo #42

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 21 additions & 20 deletions src/modules/internet/Rsock.c
Expand Up @@ -195,7 +195,7 @@ void in_Rsockwrite(int *sockp, char **buf, int *start, int *end, int *len)
#include <sys/select.h>
#endif

struct hostent *R_gethostbyname(const char *name);
struct addrinfo *R_getaddrinfo(const char *name, int port);

#ifdef Unix
#include <R_ext/eventloop.h>
Expand Down Expand Up @@ -401,8 +401,7 @@ int R_SockConnect(int port, char *host, int timeout)
struct timeval tv;
int status = 0;
double used = 0.0;
struct sockaddr_in server;
struct hostent *hp;
struct addrinfo *hp;

check_init();
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
Expand All @@ -413,14 +412,9 @@ int R_SockConnect(int port, char *host, int timeout)
if (R_set_nonblocking(s))
return -1;

if (! (hp = R_gethostbyname(host))) CLOSE_N_RETURN(-1);
if (! (hp = R_getaddrinfo(host, port))) CLOSE_N_RETURN(-1);

memcpy((char *)&server.sin_addr, hp->h_addr_list[0], hp->h_length);
server.sin_port = htons((short)port);
server.sin_family = AF_INET;

if (R_socket_error(connect(s, (struct sockaddr *) &server,
sizeof(server)))) {
if (R_socket_error(connect(s, hp->ai_addr, hp->ai_addrlen))) {

switch (R_socket_errno()) {
case EINPROGRESS:
Expand Down Expand Up @@ -668,15 +662,22 @@ ssize_t R_SockWrite(int sockp, const void *buf, size_t len, int timeout)
return out;
}

struct hostent *R_gethostbyname(const char *name)
struct addrinfo *R_getaddrinfo(const char *name, int port)
{
struct hostent *ans = gethostbyname(name);

/* hard-code IPv4 address for localhost to be robust against
misconfigured systems */

if (ans == NULL && !strcmp(name, "localhost"))
ans = gethostbyname("127.0.0.1");
return ans;
struct addrinfo hints;
struct addrinfo *addr = NULL;
memset(&hints,0,sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
char service[10];
sprintf(service, "%d", port);
if(getaddrinfo(name, service, &hints, &addr)){
printf("Failure in getaddrinfo() for %s:%s\n", name, service);
/* hard-code IPv4 address for localhost to be robust against
misconfigured systems */
if(!strcmp(name, "localhost"))
getaddrinfo("127.0.0.1", service, &hints, &addr);
}
return addr;
}

14 changes: 5 additions & 9 deletions src/modules/internet/sock.c
Expand Up @@ -173,7 +173,7 @@ int R_set_nodelay(SOCKET s)
extern int h_errno; /* HP-UX 9.05 forgets to declare this in netdb.h */
#endif

extern struct hostent *R_gethostbyname(const char *name);
extern struct addrinfo *R_getaddrinfo(const char *name, int port);

#define MAXBACKLOG SOMAXCONN

Expand Down Expand Up @@ -376,27 +376,23 @@ int Sock_listen(int fd, char *cname, int buflen, Sock_error_t perr)
int Sock_connect(Sock_port_t port, char *sname, Sock_error_t perr)
{
struct sockaddr_in server;
struct hostent *hp;
struct addrinfo *hp;
SOCKET sock;
int retval;

if (! (hp = R_gethostbyname(sname)))
if (! (hp = R_getaddrinfo(sname, port)))
#ifdef Win32
return Sock_error(perr, R_socket_errno(), WSAGetLastError());
#else
return Sock_error(perr, R_socket_errno(), h_errno);
#endif

sock = socket(AF_INET, SOCK_STREAM, 0);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (R_invalid_socket(sock))
return Sock_error(perr, R_socket_errno(), 0);

memcpy((char *)&server.sin_addr, hp->h_addr_list[0], hp->h_length);
server.sin_port = htons((short)port);
server.sin_family = AF_INET;

do
retval = connect(sock, (struct sockaddr *) &server, sizeof(server));
retval = connect(sock, hp->ai_addr, hp->ai_addrlen);
while (R_socket_error_eintr(retval));
if (R_socket_error(retval)) {
R_close_socket(sock);
Expand Down
3 changes: 2 additions & 1 deletion src/modules/internet/sock.h
Expand Up @@ -42,7 +42,8 @@ ssize_t Sock_write(int fd, const void *buf, size_t nbytes, Sock_error_t perr);
#ifndef Win32
# define SOCKET int
#else
# define FD_SETSIZE 1024
# undef S /* both ws2tcpip.h and graphapp.h define macro S */
# include<ws2tcpip.h>
# include<winsock2.h>
#endif

Expand Down