Use gai_strerror() to report errors of getaddrinfo() and getnameinfo()
This commit is contained in:
parent
c2d3470a35
commit
69c86b987b
40
src/sock.c
40
src/sock.c
@ -34,6 +34,17 @@
|
|||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return a human readable error for getaddrinfo() and getnameinfo().
|
||||||
|
*/
|
||||||
|
static const char * get_gai_error (int n)
|
||||||
|
{
|
||||||
|
if (n == EAI_SYSTEM)
|
||||||
|
return strerror (errno);
|
||||||
|
else
|
||||||
|
return gai_strerror (n);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Bind the given socket to the supplied address. The socket is
|
* Bind the given socket to the supplied address. The socket is
|
||||||
* returned if the bind succeeded. Otherwise, -1 is returned
|
* returned if the bind succeeded. Otherwise, -1 is returned
|
||||||
@ -43,6 +54,7 @@ static int
|
|||||||
bind_socket (int sockfd, const char *addr, int family)
|
bind_socket (int sockfd, const char *addr, int family)
|
||||||
{
|
{
|
||||||
struct addrinfo hints, *res, *ressave;
|
struct addrinfo hints, *res, *ressave;
|
||||||
|
int n;
|
||||||
|
|
||||||
assert (sockfd >= 0);
|
assert (sockfd >= 0);
|
||||||
assert (addr != NULL && strlen (addr) != 0);
|
assert (addr != NULL && strlen (addr) != 0);
|
||||||
@ -51,9 +63,13 @@ bind_socket (int sockfd, const char *addr, int family)
|
|||||||
hints.ai_family = family;
|
hints.ai_family = family;
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
|
||||||
/* The local port it not important */
|
/* The local port is not important */
|
||||||
if (getaddrinfo (addr, NULL, &hints, &res) != 0)
|
n = getaddrinfo (addr, NULL, &hints, &res);
|
||||||
|
if (n != 0) {
|
||||||
|
log_message (LOG_INFO,
|
||||||
|
"bind_socket: getaddrinfo failed for %s: ", addr, get_gai_error (n));
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
ressave = res;
|
ressave = res;
|
||||||
|
|
||||||
@ -96,7 +112,7 @@ int opensock (const char *host, int port, const char *bind_to)
|
|||||||
n = getaddrinfo (host, portstr, &hints, &res);
|
n = getaddrinfo (host, portstr, &hints, &res);
|
||||||
if (n != 0) {
|
if (n != 0) {
|
||||||
log_message (LOG_ERR,
|
log_message (LOG_ERR,
|
||||||
"opensock: Could not retrieve info for %s", host);
|
"opensock: Could not retrieve address info for %s:%d: %s", host, port, get_gai_error (n));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,8 +150,9 @@ int opensock (const char *host, int port, const char *bind_to)
|
|||||||
freeaddrinfo (ressave);
|
freeaddrinfo (ressave);
|
||||||
if (res == NULL) {
|
if (res == NULL) {
|
||||||
log_message (LOG_ERR,
|
log_message (LOG_ERR,
|
||||||
"opensock: Could not establish a connection to %s",
|
"opensock: Could not establish a connection to %s:%d",
|
||||||
host);
|
host,
|
||||||
|
port);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,8 +203,7 @@ static int listen_on_one_socket(struct addrinfo *ad)
|
|||||||
ret = getnameinfo(ad->ai_addr, ad->ai_addrlen,
|
ret = getnameinfo(ad->ai_addr, ad->ai_addrlen,
|
||||||
numerichost, NI_MAXHOST, NULL, 0, flags);
|
numerichost, NI_MAXHOST, NULL, 0, flags);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
log_message(LOG_ERR, "error calling getnameinfo: %s",
|
log_message(LOG_ERR, "getnameinfo failed: %s", get_gai_error (ret));
|
||||||
gai_strerror(errno));
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -256,6 +272,7 @@ int listen_sock (const char *addr, uint16_t port, vector_t listen_fds)
|
|||||||
struct addrinfo hints, *result, *rp;
|
struct addrinfo hints, *result, *rp;
|
||||||
char portstr[6];
|
char portstr[6];
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
int n;
|
||||||
|
|
||||||
assert (port > 0);
|
assert (port > 0);
|
||||||
assert (listen_fds != NULL);
|
assert (listen_fds != NULL);
|
||||||
@ -270,10 +287,13 @@ int listen_sock (const char *addr, uint16_t port, vector_t listen_fds)
|
|||||||
|
|
||||||
snprintf (portstr, sizeof (portstr), "%d", port);
|
snprintf (portstr, sizeof (portstr), "%d", port);
|
||||||
|
|
||||||
if (getaddrinfo (addr, portstr, &hints, &result) != 0) {
|
n = getaddrinfo (addr, portstr, &hints, &result);
|
||||||
|
if (n != 0) {
|
||||||
log_message (LOG_ERR,
|
log_message (LOG_ERR,
|
||||||
"Unable to getaddrinfo() because of %s",
|
"Unable to getaddrinfo() for %s:%d because of %s",
|
||||||
strerror (errno));
|
addr,
|
||||||
|
port,
|
||||||
|
get_gai_error (n));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user