Exchanged gethostbyname for getaddrinfo

This commit is contained in:
sethdelliott 2010-07-27 17:32:21 +00:00
parent a1344ede16
commit 5e0e1e974e
2 changed files with 74 additions and 64 deletions

View File

@ -117,6 +117,8 @@ iperf_tcp_listen(struct iperf_test *test)
int s, opt;
struct sockaddr_in sa;
struct hostent *hent;
struct addrinfo hints, *res;
char portstr[6];
s = test->listener;
if (test->no_delay || test->settings->mss) {
@ -148,25 +150,25 @@ iperf_tcp_listen(struct iperf_test *test)
return (-1);
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
if (test->bind_address) {
if ((hent = gethostbyname(test->bind_address)) == NULL) {
i_errno = IESTREAMLISTEN;
return (-1);
}
memcpy(&sa.sin_addr, hent->h_addr_list[0], 4);
} else {
sa.sin_addr.s_addr = htonl(INADDR_ANY);
snprintf(portstr, 6, "%d", test->server_port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
// XXX: Check getaddrinfo for errors!
if (getaddrinfo(test->bind_address, portstr, &hints, &res) != 0) {
i_errno = IESTREAMLISTEN;
return (-1);
}
sa.sin_port = htons(test->server_port);
if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0) {
close(s);
i_errno = IESTREAMLISTEN;
return (-1);
}
freeaddrinfo(res);
if (listen(s, 5) < 0) {
i_errno = IESTREAMLISTEN;
return (-1);
@ -189,6 +191,8 @@ iperf_tcp_connect(struct iperf_test *test)
int s, opt;
struct sockaddr_in sa, local;
struct hostent *hent;
struct addrinfo hints, *res;
char portstr[6];
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
i_errno = IESTREAMCONNECT;
@ -196,29 +200,22 @@ iperf_tcp_connect(struct iperf_test *test)
}
if (test->bind_address) {
if ((hent = gethostbyname(test->bind_address)) == NULL) {
/* XXX: Make IESTREAMBIND? */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// XXX: Check getaddrinfo for errors!
if (getaddrinfo(test->bind_address, NULL, &hints, &res) != 0) {
i_errno = IESTREAMCONNECT;
return (-1);
}
memset(&local, 0, sizeof(local));
memcpy(&local.sin_addr, hent->h_addr_list[0], 4);
local.sin_family = AF_INET;
if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0) {
i_errno = IESTREAMCONNECT;
return (-1);
}
}
if ((hent = gethostbyname(test->server_hostname)) == 0) {
i_errno = IESTREAMCONNECT;
return (-1);
freeaddrinfo(res);
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
memcpy(&sa.sin_addr.s_addr, hent->h_addr, sizeof(sa.sin_addr.s_addr));
sa.sin_port = htons(test->server_port);
/* Set TCP options */
if (test->no_delay) {
@ -235,11 +232,23 @@ iperf_tcp_connect(struct iperf_test *test)
}
}
if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0 && errno != EINPROGRESS) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
snprintf(portstr, 6, "%d", test->server_port);
// XXX: Check getaddrinfo for errors!
if (getaddrinfo(test->server_hostname, portstr, &hints, &res) != 0) {
i_errno = IESTREAMCONNECT;
return (-1);
}
if (connect(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0 && errno != EINPROGRESS) {
i_errno = IESTREAMCONNECT;
return (-1);
}
freeaddrinfo(res);
/* Send cookie for verification */
if (Nwrite(s, test->cookie, COOKIE_SIZE, Ptcp) < 0) {
i_errno = IESENDCOOKIE;

View File

@ -22,8 +22,7 @@ int
netdial(int proto, char *local, char *server, int port)
{
int s;
struct hostent *hent;
struct sockaddr_in sa, lo;
struct addrinfo hints, *res;
s = socket(AF_INET, proto, 0);
if (s < 0) {
@ -31,30 +30,35 @@ netdial(int proto, char *local, char *server, int port)
}
if (local) {
if ((hent = gethostbyname(local)) == 0)
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = proto;
// XXX: Check getaddrinfo for errors!
if (getaddrinfo(local, NULL, &hints, &res) != 0)
return (-1);
memset(&lo, 0, sizeof(lo));
memmove(&lo.sin_addr, hent->h_addr_list[0], 4);
lo.sin_family = AF_INET;
if (bind(s, (struct sockaddr *) &lo, sizeof(lo)) < 0)
if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0)
return (-1);
freeaddrinfo(res);
}
/* XXX: This is not working for non-fully qualified host names use getaddrinfo() instead? */
if ((hent = gethostbyname(server)) == 0) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = proto;
// XXX: Check getaddrinfo for errors!
if (getaddrinfo(server, NULL, &hints, &res) != 0)
return (-1);
((struct sockaddr_in *) res->ai_addr)->sin_port = htons(port);
if (connect(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0 && errno != EINPROGRESS) {
return (-1);
}
memset(&sa, 0, sizeof sa);
memmove(&sa.sin_addr, hent->h_addr, 4);
sa.sin_port = htons(port);
sa.sin_family = AF_INET;
if (connect(s, (struct sockaddr *) & sa, sizeof sa) < 0 && errno != EINPROGRESS) {
return (-1);
}
freeaddrinfo(res);
return (s);
}
@ -64,35 +68,32 @@ netdial(int proto, char *local, char *server, int port)
int
netannounce(int proto, char *local, int port)
{
int s;
struct sockaddr_in sa;
struct hostent *hent;
/* XXX: implement binding to a local address rather than * */
memset((void *) &sa, 0, sizeof sa);
int s, opt;
struct addrinfo hints, *res;
char portstr[6];
s = socket(AF_INET, proto, 0);
if (s < 0) {
return (-1);
}
int opt = 1;
opt = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt));
if (local) {
if ((hent = gethostbyname(local)) == NULL)
return (-1);
memcpy(&sa.sin_addr, hent->h_addr_list[0], 4);
} else {
sa.sin_addr.s_addr = htonl(INADDR_ANY);
}
snprintf(portstr, 6, "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = proto;
hints.ai_flags = AI_PASSIVE;
// XXX: Check getaddrinfo for errors!
if (getaddrinfo(local, portstr, &hints, &res) != 0)
return (-1);
sa.sin_port = htons(port);
sa.sin_family = AF_INET;
if (bind(s, (struct sockaddr *) & sa, sizeof(struct sockaddr_in)) < 0) {
if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0) {
close(s);
return (-1);
}
freeaddrinfo(res);
if (proto == SOCK_STREAM) {
if (listen(s, 5) < 0) {
@ -100,7 +101,7 @@ netannounce(int proto, char *local, int port)
}
}
return s;
return (s);
}