]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
parse-util: add explicit parsers for MTU values
authorLennart Poettering <lennart@poettering.net>
Fri, 20 Apr 2018 14:28:12 +0000 (16:28 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 26 Apr 2018 11:51:44 +0000 (13:51 +0200)
We use MTUs all over the place, let's add a unified, strict parser for
it, that takes MTU ranges into account.

We already have parse_ifindex() close-by, hence this appears to be a
natural addition, in particular as the range checking is not entirely
trivial to do, as it depends on the protocol used.

src/basic/parse-util.c
src/basic/parse-util.h
src/test/test-parse-util.c

index 8398bdf117d0335dabbeb4d40fa57c6497100383..fbb32f3dc58566490d8143b139d6162ed3093a85 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/socket.h>
 
 #include "alloc-util.h"
 #include "errno-list.h"
 #include "extract-word.h"
 #include "locale-util.h"
 #include "macro.h"
+#include "missing.h"
 #include "parse-util.h"
 #include "process-util.h"
 #include "string-util.h"
@@ -93,6 +95,30 @@ int parse_ifindex(const char *s, int *ret) {
         return 0;
 }
 
+int parse_mtu(int family, const char *s, uint32_t *ret) {
+        uint64_t u;
+        size_t m;
+        int r;
+
+        r = parse_size(s, 1024, &u);
+        if (r < 0)
+                return r;
+
+        if (u > UINT32_MAX)
+                return -ERANGE;
+
+        if (family == AF_INET6)
+                m = IPV6_MIN_MTU; /* This is 1280 */
+        else
+                m = IPV4_MIN_MTU; /* For all other protocols, including 'unspecified' we assume the IPv4 minimal MTU */
+
+        if (u < m)
+                return -ERANGE;
+
+        *ret = (uint32_t) u;
+        return 0;
+}
+
 int parse_size(const char *t, uint64_t base, uint64_t *size) {
 
         /* Soo, sometimes we want to parse IEC binary suffixes, and
index 74c30b55706e608af9d1972db3897709187f95d2..742063c9a4057e067860cbd01dcc12b06a023d74 100644 (file)
@@ -22,6 +22,7 @@ int parse_dev(const char *s, dev_t *ret);
 int parse_pid(const char *s, pid_t* ret_pid);
 int parse_mode(const char *s, mode_t *ret);
 int parse_ifindex(const char *s, int *ret);
+int parse_mtu(int family, const char *s, uint32_t *ret);
 
 int parse_size(const char *t, uint64_t base, uint64_t *size);
 int parse_range(const char *t, unsigned *lower, unsigned *upper);
index 37dbb0ce7a9080bdbf66308d1ef0bd11140efd96..a04ab9c9aa494232896d6b65d206fa7c00787774 100644 (file)
@@ -9,6 +9,7 @@
 #include <errno.h>
 #include <locale.h>
 #include <math.h>
+#include <sys/socket.h>
 
 #include "alloc-util.h"
 #include "errno-list.h"
@@ -757,6 +758,27 @@ static void test_parse_syscall_and_errno(void) {
         assert_se(parse_syscall_and_errno("hoge:", &n, &e) == -EINVAL);
 }
 
+static void test_parse_mtu(void) {
+        uint32_t mtu = 0;
+
+        assert_se(parse_mtu(AF_UNSPEC, "1500", &mtu) >= 0 && mtu == 1500);
+        assert_se(parse_mtu(AF_UNSPEC, "1400", &mtu) >= 0 && mtu == 1400);
+        assert_se(parse_mtu(AF_UNSPEC, "65535", &mtu) >= 0 && mtu == 65535);
+        assert_se(parse_mtu(AF_UNSPEC, "65536", &mtu) >= 0 && mtu == 65536);
+        assert_se(parse_mtu(AF_UNSPEC, "4294967295", &mtu) >= 0 && mtu == 4294967295);
+        assert_se(parse_mtu(AF_UNSPEC, "500", &mtu) >= 0 && mtu == 500);
+        assert_se(parse_mtu(AF_UNSPEC, "1280", &mtu) >= 0 && mtu == 1280);
+        assert_se(parse_mtu(AF_INET6, "1280", &mtu) >= 0 && mtu == 1280);
+        assert_se(parse_mtu(AF_INET6, "1279", &mtu) == -ERANGE);
+        assert_se(parse_mtu(AF_UNSPEC, "4294967296", &mtu) == -ERANGE);
+        assert_se(parse_mtu(AF_INET6, "4294967296", &mtu) == -ERANGE);
+        assert_se(parse_mtu(AF_INET6, "68", &mtu) == -ERANGE);
+        assert_se(parse_mtu(AF_UNSPEC, "68", &mtu) >= 0 && mtu == 68);
+        assert_se(parse_mtu(AF_UNSPEC, "67", &mtu) == -ERANGE);
+        assert_se(parse_mtu(AF_UNSPEC, "0", &mtu) == -ERANGE);
+        assert_se(parse_mtu(AF_UNSPEC, "", &mtu) == -EINVAL);
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -779,6 +801,7 @@ int main(int argc, char *argv[]) {
         test_parse_dev();
         test_parse_errno();
         test_parse_syscall_and_errno();
+        test_parse_mtu();
 
         return 0;
 }