Exchanged gethostbyname for getaddrinfo
This commit is contained in:
parent
a1344ede16
commit
5e0e1e974e
@ -117,6 +117,8 @@ iperf_tcp_listen(struct iperf_test *test)
|
|||||||
int s, opt;
|
int s, opt;
|
||||||
struct sockaddr_in sa;
|
struct sockaddr_in sa;
|
||||||
struct hostent *hent;
|
struct hostent *hent;
|
||||||
|
struct addrinfo hints, *res;
|
||||||
|
char portstr[6];
|
||||||
s = test->listener;
|
s = test->listener;
|
||||||
|
|
||||||
if (test->no_delay || test->settings->mss) {
|
if (test->no_delay || test->settings->mss) {
|
||||||
@ -148,25 +150,25 @@ iperf_tcp_listen(struct iperf_test *test)
|
|||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&sa, 0, sizeof(sa));
|
snprintf(portstr, 6, "%d", test->server_port);
|
||||||
sa.sin_family = AF_INET;
|
memset(&hints, 0, sizeof(hints));
|
||||||
if (test->bind_address) {
|
hints.ai_family = AF_INET;
|
||||||
if ((hent = gethostbyname(test->bind_address)) == NULL) {
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
i_errno = IESTREAMLISTEN;
|
hints.ai_flags = AI_PASSIVE;
|
||||||
return (-1);
|
// XXX: Check getaddrinfo for errors!
|
||||||
}
|
if (getaddrinfo(test->bind_address, portstr, &hints, &res) != 0) {
|
||||||
memcpy(&sa.sin_addr, hent->h_addr_list[0], 4);
|
i_errno = IESTREAMLISTEN;
|
||||||
} else {
|
return (-1);
|
||||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
}
|
}
|
||||||
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);
|
close(s);
|
||||||
i_errno = IESTREAMLISTEN;
|
i_errno = IESTREAMLISTEN;
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(res);
|
||||||
|
|
||||||
if (listen(s, 5) < 0) {
|
if (listen(s, 5) < 0) {
|
||||||
i_errno = IESTREAMLISTEN;
|
i_errno = IESTREAMLISTEN;
|
||||||
return (-1);
|
return (-1);
|
||||||
@ -189,6 +191,8 @@ iperf_tcp_connect(struct iperf_test *test)
|
|||||||
int s, opt;
|
int s, opt;
|
||||||
struct sockaddr_in sa, local;
|
struct sockaddr_in sa, local;
|
||||||
struct hostent *hent;
|
struct hostent *hent;
|
||||||
|
struct addrinfo hints, *res;
|
||||||
|
char portstr[6];
|
||||||
|
|
||||||
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||||
i_errno = IESTREAMCONNECT;
|
i_errno = IESTREAMCONNECT;
|
||||||
@ -196,29 +200,22 @@ iperf_tcp_connect(struct iperf_test *test)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (test->bind_address) {
|
if (test->bind_address) {
|
||||||
if ((hent = gethostbyname(test->bind_address)) == NULL) {
|
memset(&hints, 0, sizeof(hints));
|
||||||
/* XXX: Make IESTREAMBIND? */
|
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;
|
i_errno = IESTREAMCONNECT;
|
||||||
return (-1);
|
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;
|
i_errno = IESTREAMCONNECT;
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ((hent = gethostbyname(test->server_hostname)) == 0) {
|
freeaddrinfo(res);
|
||||||
i_errno = IESTREAMCONNECT;
|
|
||||||
return (-1);
|
|
||||||
}
|
}
|
||||||
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 */
|
/* Set TCP options */
|
||||||
if (test->no_delay) {
|
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;
|
i_errno = IESTREAMCONNECT;
|
||||||
return (-1);
|
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 */
|
/* Send cookie for verification */
|
||||||
if (Nwrite(s, test->cookie, COOKIE_SIZE, Ptcp) < 0) {
|
if (Nwrite(s, test->cookie, COOKIE_SIZE, Ptcp) < 0) {
|
||||||
i_errno = IESENDCOOKIE;
|
i_errno = IESENDCOOKIE;
|
||||||
|
75
src/net.c
75
src/net.c
@ -22,8 +22,7 @@ int
|
|||||||
netdial(int proto, char *local, char *server, int port)
|
netdial(int proto, char *local, char *server, int port)
|
||||||
{
|
{
|
||||||
int s;
|
int s;
|
||||||
struct hostent *hent;
|
struct addrinfo hints, *res;
|
||||||
struct sockaddr_in sa, lo;
|
|
||||||
|
|
||||||
s = socket(AF_INET, proto, 0);
|
s = socket(AF_INET, proto, 0);
|
||||||
if (s < 0) {
|
if (s < 0) {
|
||||||
@ -31,30 +30,35 @@ netdial(int proto, char *local, char *server, int port)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (local) {
|
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);
|
return (-1);
|
||||||
|
|
||||||
memset(&lo, 0, sizeof(lo));
|
if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0)
|
||||||
memmove(&lo.sin_addr, hent->h_addr_list[0], 4);
|
|
||||||
lo.sin_family = AF_INET;
|
|
||||||
|
|
||||||
if (bind(s, (struct sockaddr *) &lo, sizeof(lo)) < 0)
|
|
||||||
return (-1);
|
return (-1);
|
||||||
|
|
||||||
|
freeaddrinfo(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* XXX: This is not working for non-fully qualified host names use getaddrinfo() instead? */
|
memset(&hints, 0, sizeof(hints));
|
||||||
if ((hent = gethostbyname(server)) == 0) {
|
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);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&sa, 0, sizeof sa);
|
freeaddrinfo(res);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (s);
|
return (s);
|
||||||
}
|
}
|
||||||
@ -64,35 +68,32 @@ netdial(int proto, char *local, char *server, int port)
|
|||||||
int
|
int
|
||||||
netannounce(int proto, char *local, int port)
|
netannounce(int proto, char *local, int port)
|
||||||
{
|
{
|
||||||
int s;
|
int s, opt;
|
||||||
struct sockaddr_in sa;
|
struct addrinfo hints, *res;
|
||||||
struct hostent *hent;
|
char portstr[6];
|
||||||
/* XXX: implement binding to a local address rather than * */
|
|
||||||
|
|
||||||
memset((void *) &sa, 0, sizeof sa);
|
|
||||||
|
|
||||||
s = socket(AF_INET, proto, 0);
|
s = socket(AF_INET, proto, 0);
|
||||||
if (s < 0) {
|
if (s < 0) {
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
int opt = 1;
|
opt = 1;
|
||||||
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt));
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt));
|
||||||
|
|
||||||
if (local) {
|
snprintf(portstr, 6, "%d", port);
|
||||||
if ((hent = gethostbyname(local)) == NULL)
|
memset(&hints, 0, sizeof(hints));
|
||||||
return (-1);
|
hints.ai_family = AF_INET;
|
||||||
memcpy(&sa.sin_addr, hent->h_addr_list[0], 4);
|
hints.ai_socktype = proto;
|
||||||
} else {
|
hints.ai_flags = AI_PASSIVE;
|
||||||
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
// XXX: Check getaddrinfo for errors!
|
||||||
}
|
if (getaddrinfo(local, portstr, &hints, &res) != 0)
|
||||||
|
return (-1);
|
||||||
|
|
||||||
sa.sin_port = htons(port);
|
if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0) {
|
||||||
sa.sin_family = AF_INET;
|
|
||||||
|
|
||||||
if (bind(s, (struct sockaddr *) & sa, sizeof(struct sockaddr_in)) < 0) {
|
|
||||||
close(s);
|
close(s);
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(res);
|
||||||
|
|
||||||
if (proto == SOCK_STREAM) {
|
if (proto == SOCK_STREAM) {
|
||||||
if (listen(s, 5) < 0) {
|
if (listen(s, 5) < 0) {
|
||||||
@ -100,7 +101,7 @@ netannounce(int proto, char *local, int port)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return (s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user