From 1319d78c9a00faf49f6104ab8c2813558367e442 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 22 Mar 2018 02:09:43 +0100 Subject: [PATCH] net: Improve error diagnostics of makesock_addrinfo(). Since the function iterates over all addresses in the passed addressinfo structure, errors can be non-fatal and may even be expected. Therefore the function does not log any errors from socket(2), setsockopt(2), connect(2) or bind(2), but only returns a generic -E_MAKESOCK error code if none of the addressinfo members worked. Unfortunately, this means it's impossible to tell from the log message which of these system calls has failed. This patch changes the function to also log the errors from the above system calls, but only with loglevel notice. This way the non-fatal errors are not shown by default (since the default loglevel is "warning"), but one can easily activate them by specifying a lower loglevel. --- net.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/net.c b/net.c index 3f76d21c..b9176a66 100644 --- a/net.c +++ b/net.c @@ -425,15 +425,20 @@ int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai, for (; ai; ai = ai->ai_next) { int fd; ret = socket(ai->ai_family, sock_type(l4type), l4type); - if (ret < 0) + if (ret < 0) { + PARA_NOTICE_LOG("socket(): %s\n", strerror(errno)); continue; + } fd = ret; flowopt_setopts(fd, fo); if (!passive) { - if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) - return fd; - close(fd); - continue; + if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) { + PARA_NOTICE_LOG("connect(): %s\n", + strerror(errno)); + close(fd); + continue; + } + return fd; } /* * Reuse the address on passive sockets to avoid failure on @@ -442,10 +447,12 @@ int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai, */ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { + PARA_NOTICE_LOG("setsockopt(): %s\n", strerror(errno)); close(fd); continue; } if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0) { + PARA_NOTICE_LOG("bind(): %s\n", strerror(errno)); close(fd); continue; } -- 2.30.2