chiark / gitweb /
socket-util: socket_address_parse() should not log errors on its own
authorLennart Poettering <lennart@poettering.net>
Fri, 15 May 2015 18:15:59 +0000 (20:15 +0200)
committerSven Eden <yamakuzure@gmx.net>
Tue, 14 Mar 2017 08:56:11 +0000 (09:56 +0100)
Given that socket_address_parse() is mostly a "library" call it
shouldn't log on its own, but leave that to its caller.

This patch removes logging from the call in case IPv6 is not available
but and IPv6 address shall be parsed. Instead a new call
socket_address_parse_and_warn() is introduced which first invokes
socket_address_parse() and then logs if necessary.

This should fix "make check" on ipv6-less kernels:

http://lists.freedesktop.org/archives/systemd-devel/2015-April/031385.html

src/shared/socket-util.c
src/shared/socket-util.h

index 94a0663..e8bb10d 100644 (file)
@@ -51,11 +51,6 @@ int socket_address_parse(SocketAddress *a, const char *s) {
         if (*s == '[') {
                 /* IPv6 in [x:.....:z]:p notation */
 
-                if (!socket_ipv6_is_supported()) {
-                        log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
-                        return -EAFNOSUPPORT;
-                }
-
                 e = strchr(s+1, ']');
                 if (!e)
                         return -EINVAL;
@@ -140,11 +135,6 @@ int socket_address_parse(SocketAddress *a, const char *s) {
                                 if (idx == 0)
                                         return -EINVAL;
 
-                                if (!socket_ipv6_is_supported()) {
-                                        log_warning("Binding to interface is not available since kernel does not support IPv6.");
-                                        return -EAFNOSUPPORT;
-                                }
-
                                 a->sockaddr.in6.sin6_family = AF_INET6;
                                 a->sockaddr.in6.sin6_port = htons((uint16_t) u);
                                 a->sockaddr.in6.sin6_scope_id = idx;
@@ -178,6 +168,25 @@ int socket_address_parse(SocketAddress *a, const char *s) {
         return 0;
 }
 
+int socket_address_parse_and_warn(SocketAddress *a, const char *s) {
+        SocketAddress b;
+        int r;
+
+        /* Similar to socket_address_parse() but warns for IPv6 sockets when we don't support them. */
+
+        r = socket_address_parse(&b, s);
+        if (r < 0)
+                return r;
+
+        if (!socket_ipv6_is_supported() && b.sockaddr.sa.sa_family == AF_INET6) {
+                log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
+                return -EAFNOSUPPORT;
+        }
+
+        *a = b;
+        return 0;
+}
+
 int socket_address_parse_netlink(SocketAddress *a, const char *s) {
         int family;
         unsigned group = 0;
index 1f48230..538cf59 100644 (file)
@@ -66,6 +66,7 @@ typedef enum SocketAddressBindIPv6Only {
 #define socket_address_family(a) ((a)->sockaddr.sa.sa_family)
 
 int socket_address_parse(SocketAddress *a, const char *s);
+int socket_address_parse_and_warn(SocketAddress *a, const char *s);
 int socket_address_parse_netlink(SocketAddress *a, const char *s);
 int socket_address_print(const SocketAddress *a, char **p);
 int socket_address_verify(const SocketAddress *a) _pure_;