net: Improve error diagnostics of makesock_addrinfo().
authorAndre Noll <maan@tuebingen.mpg.de>
Thu, 22 Mar 2018 01:09:43 +0000 (02:09 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Tue, 17 Apr 2018 07:29:57 +0000 (09:29 +0200)
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

diff --git a/net.c b/net.c
index 3f76d21c6cc9abf60ff5a98d1ac804f4b067ffef..b9176a6659d6d2628aff3f71f9d82718e0e7a802 100644 (file)
--- 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;
                }