]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
socket-util: add helper for checking if IPv6 is enabled
authorLennart Poettering <lennart@poettering.net>
Fri, 5 Mar 2021 19:37:24 +0000 (20:37 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 5 Mar 2021 19:51:43 +0000 (20:51 +0100)
src/basic/socket-util.c
src/basic/socket-util.h
src/test/test-socket-util.c

index 315a99c3fa49f6f5fd19f719b03eda44ef64b556..4899c1758987dc3d625dee0404f08b81e15adebd 100644 (file)
@@ -31,6 +31,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
+#include "sysctl-util.h"
 #include "user-util.h"
 #include "utf8.h"
 
@@ -296,6 +297,31 @@ bool socket_ipv6_is_supported(void) {
         return cached;
 }
 
+bool socket_ipv6_is_enabled(void) {
+        _cleanup_free_ char *v;
+        int r;
+
+        /* Much like socket_ipv6_is_supported(), but also checks that the sysctl that disables IPv6 on all
+         * interfaces isn't turned on */
+
+        if (!socket_ipv6_is_supported())
+                return false;
+
+        r = sysctl_read_ip_property(AF_INET6, "all", "disable_ipv6", &v);
+        if (r < 0) {
+                log_debug_errno(r, "Unexpected error reading 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
+                return true;
+        }
+
+        r = parse_boolean(v);
+        if (r < 0) {
+                log_debug_errno(r, "Failed to pare 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
+                return true;
+        }
+
+        return !r;
+}
+
 bool socket_address_matches_fd(const SocketAddress *a, int fd) {
         SocketAddress b;
         socklen_t solen;
index 2c98283cd34b52b2fdab575f3a5d7dff25e6b9fb..507a599d7cfa072833b1364d2e55f4ff45ca5abf 100644 (file)
@@ -101,6 +101,7 @@ bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) _pure_
 const char* socket_address_get_path(const SocketAddress *a);
 
 bool socket_ipv6_is_supported(void);
+bool socket_ipv6_is_enabled(void);
 
 int sockaddr_port(const struct sockaddr *_sa, unsigned *port);
 const union in_addr_union *sockaddr_in_addr(const struct sockaddr *sa);
index 4ff7d714f8f79cf2f261e71a4d3c4fb6d9844243..584253c2fcb60c9a70b91c6774756ab815f81700 100644 (file)
@@ -504,6 +504,11 @@ static void test_flush_accept(void) {
         assert_se(flush_accept(listen_seqpacket) >= 0);
 }
 
+static void test_ipv6_enabled(void) {
+        log_info("IPv6 supported: %s", yes_no(socket_ipv6_is_supported()));
+        log_info("IPv6 enabled: %s", yes_no(socket_ipv6_is_enabled()));
+}
+
 int main(int argc, char *argv[]) {
         test_setup_logging(LOG_DEBUG);
 
@@ -519,6 +524,7 @@ int main(int argc, char *argv[]) {
         test_send_nodata_nofd();
         test_send_emptydata();
         test_flush_accept();
+        test_ipv6_enabled();
 
         return 0;
 }