]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
socket-util: add common API for querying socket MTU 17855/head
authorLennart Poettering <lennart@poettering.net>
Mon, 16 Nov 2020 14:01:03 +0000 (15:01 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 7 Dec 2020 12:46:35 +0000 (13:46 +0100)
src/basic/socket-util.c
src/basic/socket-util.h

index 1e53ac4e8692bf8ae6006f255e0f97af4b604e5d..59039bea4fff0bf8cada04a29586b22034a28d04 100644 (file)
@@ -1314,3 +1314,35 @@ int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val) {
                 return -EAFNOSUPPORT;
         }
 }
+
+int socket_get_mtu(int fd, int af, size_t *ret) {
+        int mtu, r;
+
+        if (af == AF_UNSPEC) {
+                r = socket_get_family(fd, &af);
+                if (r < 0)
+                        return r;
+        }
+
+        switch (af) {
+
+        case AF_INET:
+                r = getsockopt_int(fd, IPPROTO_IP, IP_MTU, &mtu);
+                break;
+
+        case AF_INET6:
+                r = getsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, &mtu);
+                break;
+
+        default:
+                return -EAFNOSUPPORT;
+        }
+
+        if (r < 0)
+                return r;
+        if (mtu <= 0)
+                return -EINVAL;
+
+        *ret = (size_t) mtu;
+        return 0;
+}
index 0dd6798d32e07df61056d104a10abba82506262e..240d209c143897778c1a7658e3be92aaf66ae19f 100644 (file)
@@ -299,3 +299,5 @@ static inline int socket_set_transparent(int fd, int af, bool b) {
 static inline int socket_set_recvfragsize(int fd, int af, bool b) {
         return socket_set_option(fd, af, IP_RECVFRAGSIZE, IPV6_RECVFRAGSIZE, b);
 }
+
+int socket_get_mtu(int fd, int af, size_t *ret);