Skip to content

Commit

Permalink
Unbreak netutils tcp resolving
Browse files Browse the repository at this point in the history
The previous commit dcf0a59 caused freeaddrinfo to be called with a NULL pointer if lookup was exhausted. On FreeBSD this caused a segfault.
Used res0 name as per FreeBSD manpage example.
  • Loading branch information
stromnet committed Sep 5, 2015
1 parent aea1db0 commit a18f60c
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions plugins/netutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int
np_net_connect (const char *host_name, int port, int *sd, int proto)
{
struct addrinfo hints;
struct addrinfo *res;
struct addrinfo *res, *res0;
struct sockaddr_un su;
char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
size_t len;
Expand All @@ -185,20 +185,21 @@ np_net_connect (const char *host_name, int port, int *sd, int proto)
memcpy (host, host_name, len);
host[len] = '\0';
snprintf (port_str, sizeof (port_str), "%d", port);
result = getaddrinfo (host, port_str, &hints, &res);
result = getaddrinfo (host, port_str, &hints, &res0);

if (result != 0) {
printf ("%s\n", gai_strerror (result));
return STATE_UNKNOWN;
}
res = res0;

while (res) {
/* attempt to create a socket */
*sd = socket (res->ai_family, socktype, res->ai_protocol);

if (*sd < 0) {
printf ("%s\n", _("Socket creation failed"));
freeaddrinfo (res);
freeaddrinfo (res0);
return STATE_UNKNOWN;
}

Expand All @@ -221,7 +222,7 @@ np_net_connect (const char *host_name, int port, int *sd, int proto)
close (*sd);
res = res->ai_next;
}
freeaddrinfo (res);
freeaddrinfo (res0);
}
/* else the hostname is interpreted as a path to a unix socket */
else {
Expand Down

0 comments on commit a18f60c

Please sign in to comment.