Merge parse-socket-bind-item.? into parse-helpers.?
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 Apr 2022 12:03:26 +0000 (14:03 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 7 Apr 2022 16:25:55 +0000 (18:25 +0200)
That file only exported one function, and it fits nicely within the scope of
"parse helpers". Let's move it there to reduce the file count a bit.

No functional change.

src/core/load-fragment.c
src/shared/bus-unit-util.c
src/shared/meson.build
src/shared/parse-helpers.c
src/shared/parse-helpers.h
src/shared/parse-socket-bind-item.c [deleted file]
src/shared/parse-socket-bind-item.h [deleted file]
src/test/meson.build
src/test/test-parse-helpers.c [new file with mode: 0644]
src/test/test-parse-socket-bind-item.c [deleted file]

index b81db4617ba2b9cd0af5e80f7e9354a5aaf36465..74c41335caefe5f00c48071c9022347a3e887e08 100644 (file)
@@ -49,7 +49,6 @@
 #include "missing_ioprio.h"
 #include "mountpoint-util.h"
 #include "nulstr-util.h"
-#include "parse-socket-bind-item.h"
 #include "parse-helpers.h"
 #include "parse-util.h"
 #include "path-util.h"
index 95ef3230b4a9675f4b7f6116d54054b703aaa45b..4f02c2c373062a8dd19a957a93b22a3595f9f718 100644 (file)
@@ -29,7 +29,7 @@
 #include "mountpoint-util.h"
 #include "nsflags.h"
 #include "numa-util.h"
-#include "parse-socket-bind-item.h"
+#include "parse-helpers.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "percent-util.h"
index 8f1f3b40a9b6dd4cfdfe1339a2775b87d1d3bbd0..1d4e4a07c04a53039f45b265281e469165838461 100644 (file)
@@ -247,8 +247,6 @@ shared_sources = files(
         'parse-argument.h',
         'parse-helpers.c',
         'parse-helpers.h',
-        'parse-socket-bind-item.c',
-        'parse-socket-bind-item.h',
         'pcre2-dlopen.c',
         'pcre2-dlopen.h',
         'pe-header.h',
index 5ebe366265adf005e30f082581872be1fa314770..a9bb58715edcc33442812c02cd961347ff7a5cc5 100644 (file)
@@ -1,7 +1,11 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
+#include "af-list.h"
+#include "extract-word.h"
+#include "ip-protocol-list.h"
 #include "log.h"
 #include "parse-helpers.h"
+#include "parse-util.h"
 #include "path-util.h"
 #include "utf8.h"
 
@@ -50,3 +54,146 @@ int path_simplify_and_warn(
 
         return 0;
 }
+
+static int parse_af_token(
+                const char *token,
+                int *family,
+                int *ip_protocol,
+                uint16_t *nr_ports,
+                uint16_t *port_min) {
+
+        int af;
+
+        assert(token);
+        assert(family);
+
+        af = af_from_ipv4_ipv6(token);
+        if (af == AF_UNSPEC)
+                return -EINVAL;
+
+        *family = af;
+        return 0;
+}
+
+static int parse_ip_protocol_token(
+                const char *token,
+                int *family,
+                int *ip_protocol,
+                uint16_t *nr_ports,
+                uint16_t *port_min) {
+
+        int proto;
+
+        assert(token);
+        assert(ip_protocol);
+
+        proto = ip_protocol_from_tcp_udp(token);
+        if (proto < 0)
+                return -EINVAL;
+
+        *ip_protocol = proto;
+        return 0;
+}
+
+static int parse_ip_ports_token(
+                const char *token,
+                int *family,
+                int *ip_protocol,
+                uint16_t *nr_ports,
+                uint16_t *port_min) {
+
+        assert(token);
+        assert(nr_ports);
+        assert(port_min);
+
+        if (streq(token, "any"))
+                *nr_ports = *port_min = 0;
+        else {
+                uint16_t mn = 0, mx = 0;
+                int r = parse_ip_port_range(token, &mn, &mx);
+                if (r < 0)
+                        return r;
+
+                *nr_ports = mx - mn + 1;
+                *port_min = mn;
+        }
+
+        return 0;
+}
+
+typedef int (*parse_token_f)(
+                const char *,
+                int *,
+                int *,
+                uint16_t *,
+                uint16_t *);
+
+int parse_socket_bind_item(
+                const char *str,
+                int *address_family,
+                int *ip_protocol,
+                uint16_t *nr_ports,
+                uint16_t *port_min) {
+
+        /* Order of token parsers is important. */
+        const parse_token_f parsers[] = {
+                &parse_af_token,
+                &parse_ip_protocol_token,
+                &parse_ip_ports_token,
+        };
+        parse_token_f const *parser_ptr = parsers;
+        int af = AF_UNSPEC, proto = 0, r;
+        uint16_t nr = 0, mn = 0;
+        const char *p = str;
+
+        assert(str);
+        assert(address_family);
+        assert(ip_protocol);
+        assert(nr_ports);
+        assert(port_min);
+
+        if (isempty(p))
+                return -EINVAL;
+
+        for (;;) {
+                _cleanup_free_ char *token = NULL;
+
+                r = extract_first_word(&p, &token, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
+                if (r == 0)
+                        break;
+                if (r < 0)
+                        return r;
+
+                if (isempty(token))
+                        return -EINVAL;
+
+                while (parser_ptr != parsers + ELEMENTSOF(parsers)) {
+                        r = (*parser_ptr)(token, &af, &proto, &nr, &mn);
+                        if (r == -ENOMEM)
+                                return r;
+
+                        ++parser_ptr;
+                        /* Continue to next token if parsing succeeded,
+                         * otherwise apply next parser to the same token.
+                         */
+                        if (r >= 0)
+                                break;
+                }
+                if (parser_ptr == parsers + ELEMENTSOF(parsers))
+                                break;
+        }
+
+        /* Failed to parse a token. */
+        if (r < 0)
+                return r;
+
+        /* Parsers applied successfully, but end of the string not reached. */
+        if (p)
+                return -EINVAL;
+
+        *address_family = af;
+        *ip_protocol = proto;
+        *nr_ports = nr;
+        *port_min = mn;
+        return 0;
+}
index 8b1d33498c3e8db7d2f2fd6e0b86f5fca09a9944..49da2815fb221125addbe5e5cf470fe00dd7429e 100644 (file)
@@ -1,6 +1,8 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 #pragma once
 
+#include <stdint.h>
+
 enum {
         PATH_CHECK_FATAL    = 1 << 0,  /* If not set, then error message is appended with 'ignoring'. */
         PATH_CHECK_ABSOLUTE = 1 << 1,
@@ -14,3 +16,10 @@ int path_simplify_and_warn(
                 const char *filename,
                 unsigned line,
                 const char *lvalue);
+
+int parse_socket_bind_item(
+        const char *str,
+        int *address_family,
+        int *ip_protocol,
+        uint16_t *nr_ports,
+        uint16_t *port_min);
diff --git a/src/shared/parse-socket-bind-item.c b/src/shared/parse-socket-bind-item.c
deleted file mode 100644 (file)
index 3c92467..0000000
+++ /dev/null
@@ -1,150 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include "af-list.h"
-#include "extract-word.h"
-#include "ip-protocol-list.h"
-#include "parse-socket-bind-item.h"
-#include "parse-util.h"
-
-static int parse_af_token(
-                const char *token,
-                int *family,
-                int *ip_protocol,
-                uint16_t *nr_ports,
-                uint16_t *port_min) {
-
-        int af;
-
-        assert(token);
-        assert(family);
-
-        af = af_from_ipv4_ipv6(token);
-        if (af == AF_UNSPEC)
-                return -EINVAL;
-
-        *family = af;
-        return 0;
-}
-
-static int parse_ip_protocol_token(
-                const char *token,
-                int *family,
-                int *ip_protocol,
-                uint16_t *nr_ports,
-                uint16_t *port_min) {
-
-        int proto;
-
-        assert(token);
-        assert(ip_protocol);
-
-        proto = ip_protocol_from_tcp_udp(token);
-        if (proto < 0)
-                return -EINVAL;
-
-        *ip_protocol = proto;
-        return 0;
-}
-
-static int parse_ip_ports_token(
-                const char *token,
-                int *family,
-                int *ip_protocol,
-                uint16_t *nr_ports,
-                uint16_t *port_min) {
-
-        assert(token);
-        assert(nr_ports);
-        assert(port_min);
-
-        if (streq(token, "any"))
-                *nr_ports = *port_min = 0;
-        else {
-                uint16_t mn = 0, mx = 0;
-                int r = parse_ip_port_range(token, &mn, &mx);
-                if (r < 0)
-                        return r;
-
-                *nr_ports = mx - mn + 1;
-                *port_min = mn;
-        }
-
-        return 0;
-}
-
-typedef int (*parse_token_f)(
-                const char *,
-                int *,
-                int *,
-                uint16_t *,
-                uint16_t *);
-
-int parse_socket_bind_item(
-                const char *str,
-                int *address_family,
-                int *ip_protocol,
-                uint16_t *nr_ports,
-                uint16_t *port_min) {
-
-        /* Order of token parsers is important. */
-        const parse_token_f parsers[] = {
-                &parse_af_token,
-                &parse_ip_protocol_token,
-                &parse_ip_ports_token,
-        };
-        parse_token_f const *parser_ptr = parsers;
-        int af = AF_UNSPEC, proto = 0, r;
-        uint16_t nr = 0, mn = 0;
-        const char *p = str;
-
-        assert(str);
-        assert(address_family);
-        assert(ip_protocol);
-        assert(nr_ports);
-        assert(port_min);
-
-        if (isempty(p))
-                return -EINVAL;
-
-        for (;;) {
-                _cleanup_free_ char *token = NULL;
-
-                r = extract_first_word(&p, &token, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
-                if (r == 0)
-                        break;
-                if (r < 0)
-                        return r;
-
-                if (isempty(token))
-                        return -EINVAL;
-
-                while (parser_ptr != parsers + ELEMENTSOF(parsers)) {
-                        r = (*parser_ptr)(token, &af, &proto, &nr, &mn);
-                        if (r == -ENOMEM)
-                                return r;
-
-                        ++parser_ptr;
-                        /* Continue to next token if parsing succeeded,
-                         * otherwise apply next parser to the same token.
-                         */
-                        if (r >= 0)
-                                break;
-                }
-                if (parser_ptr == parsers + ELEMENTSOF(parsers))
-                                break;
-        }
-
-        /* Failed to parse a token. */
-        if (r < 0)
-                return r;
-
-        /* Parsers applied successfully, but end of the string not reached. */
-        if (p)
-                return -EINVAL;
-
-        *address_family = af;
-        *ip_protocol = proto;
-        *nr_ports = nr;
-        *port_min = mn;
-        return 0;
-}
diff --git a/src/shared/parse-socket-bind-item.h b/src/shared/parse-socket-bind-item.h
deleted file mode 100644 (file)
index c8cff8d..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#pragma once
-
-#include <stdint.h>
-
-int parse_socket_bind_item(
-        const char *str,
-        int *address_family,
-        int *ip_protocol,
-        uint16_t *nr_ports,
-        uint16_t *port_min);
index 297a65d9afc54f2f743b4bed4044b64cc138debb..c189f7073b764802bd14830bfb2c3dbf643b9b24 100644 (file)
@@ -255,7 +255,7 @@ tests += [
 
         [files('test-parse-argument.c')],
 
-        [files('test-parse-socket-bind-item.c')],
+        [files('test-parse-helpers.c')],
 
         [files('test-parse-util.c')],
 
diff --git a/src/test/test-parse-helpers.c b/src/test/test-parse-helpers.c
new file mode 100644 (file)
index 0000000..052e251
--- /dev/null
@@ -0,0 +1,95 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <linux/in.h>
+#include <sys/socket.h>
+#include <stdio.h>
+
+#include "macro.h"
+#include "parse-helpers.h"
+#include "tests.h"
+
+static void test_valid_item(
+                const char *str,
+                int expected_af,
+                int expected_ip_protocol,
+                uint16_t expected_nr_ports,
+                uint16_t expected_port_min) {
+        uint16_t nr_ports, port_min;
+        int af, ip_protocol;
+
+        assert_se(parse_socket_bind_item(str, &af, &ip_protocol, &nr_ports, &port_min) >= 0);
+        assert_se(af == expected_af);
+        assert_se(ip_protocol == expected_ip_protocol);
+        assert_se(nr_ports == expected_nr_ports);
+        assert_se(port_min == expected_port_min);
+
+        log_info("%s: \"%s\" ok", __func__, str);
+}
+
+static void test_invalid_item(const char *str) {
+        uint16_t nr_ports, port_min;
+        int af, ip_protocol;
+
+        assert_se(parse_socket_bind_item(str, &af, &ip_protocol, &nr_ports, &port_min) == -EINVAL);
+
+        log_info("%s: \"%s\" ok", __func__, str);
+}
+
+TEST(valid_items) {
+        test_valid_item("any", AF_UNSPEC, 0, 0, 0);
+        test_valid_item("ipv4", AF_INET, 0, 0, 0);
+        test_valid_item("ipv6", AF_INET6, 0, 0, 0);
+        test_valid_item("ipv4:any", AF_INET, 0, 0, 0);
+        test_valid_item("ipv6:any", AF_INET6, 0, 0, 0);
+        test_valid_item("tcp", AF_UNSPEC, IPPROTO_TCP, 0, 0);
+        test_valid_item("udp", AF_UNSPEC, IPPROTO_UDP, 0, 0);
+        test_valid_item("tcp:any", AF_UNSPEC, IPPROTO_TCP, 0, 0);
+        test_valid_item("udp:any", AF_UNSPEC, IPPROTO_UDP, 0, 0);
+        test_valid_item("6666", AF_UNSPEC, 0, 1, 6666);
+        test_valid_item("6666-6667", AF_UNSPEC, 0, 2, 6666);
+        test_valid_item("65535", AF_UNSPEC, 0, 1, 65535);
+        test_valid_item("1-65535", AF_UNSPEC, 0, 65535, 1);
+        test_valid_item("ipv4:tcp", AF_INET, IPPROTO_TCP, 0, 0);
+        test_valid_item("ipv4:udp", AF_INET, IPPROTO_UDP, 0, 0);
+        test_valid_item("ipv6:tcp", AF_INET6, IPPROTO_TCP, 0, 0);
+        test_valid_item("ipv6:udp", AF_INET6, IPPROTO_UDP, 0, 0);
+        test_valid_item("ipv4:6666", AF_INET, 0, 1, 6666);
+        test_valid_item("ipv6:6666", AF_INET6, 0, 1, 6666);
+        test_valid_item("tcp:6666", AF_UNSPEC, IPPROTO_TCP, 1, 6666);
+        test_valid_item("udp:6666", AF_UNSPEC, IPPROTO_UDP, 1, 6666);
+        test_valid_item("ipv4:tcp:6666", AF_INET, IPPROTO_TCP, 1, 6666);
+        test_valid_item("ipv6:tcp:6666", AF_INET6, IPPROTO_TCP, 1, 6666);
+        test_valid_item("ipv6:udp:6666-6667", AF_INET6, IPPROTO_UDP, 2, 6666);
+        test_valid_item("ipv6:tcp:any", AF_INET6, IPPROTO_TCP, 0, 0);
+}
+
+TEST(invalid_items) {
+        test_invalid_item("");
+        test_invalid_item(":");
+        test_invalid_item("::");
+        test_invalid_item("any:");
+        test_invalid_item("meh");
+        test_invalid_item("zupa:meh");
+        test_invalid_item("zupa:meh:eh");
+        test_invalid_item("ip");
+        test_invalid_item("dccp");
+        test_invalid_item("ipv6meh");
+        test_invalid_item("ipv6::");
+        test_invalid_item("ipv6:ipv6");
+        test_invalid_item("ipv6:icmp");
+        test_invalid_item("ipv6:tcp:0");
+        test_invalid_item("65536");
+        test_invalid_item("0-65535");
+        test_invalid_item("ipv6:tcp:6666-6665");
+        test_invalid_item("ipv6:tcp:6666-100000");
+        test_invalid_item("ipv6::6666");
+        test_invalid_item("ipv6:tcp:any:");
+        test_invalid_item("ipv6:tcp:any:ipv6");
+        test_invalid_item("ipv6:tcp:6666:zupa");
+        test_invalid_item("ipv6:tcp:6666:any");
+        test_invalid_item("ipv6:tcp:6666 zupa");
+        test_invalid_item("ipv6:tcp:6666: zupa");
+        test_invalid_item("ipv6:tcp:6666\n zupa");
+}
+
+DEFINE_TEST_MAIN(LOG_INFO);
diff --git a/src/test/test-parse-socket-bind-item.c b/src/test/test-parse-socket-bind-item.c
deleted file mode 100644 (file)
index 6bca272..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include <linux/in.h>
-#include <sys/socket.h>
-#include <stdio.h>
-
-#include "macro.h"
-#include "parse-socket-bind-item.h"
-#include "tests.h"
-
-static void test_valid_item(
-                const char *str,
-                int expected_af,
-                int expected_ip_protocol,
-                uint16_t expected_nr_ports,
-                uint16_t expected_port_min) {
-        uint16_t nr_ports, port_min;
-        int af, ip_protocol;
-
-        assert_se(parse_socket_bind_item(str, &af, &ip_protocol, &nr_ports, &port_min) >= 0);
-        assert_se(af == expected_af);
-        assert_se(ip_protocol == expected_ip_protocol);
-        assert_se(nr_ports == expected_nr_ports);
-        assert_se(port_min == expected_port_min);
-
-        log_info("%s: \"%s\" ok", __func__, str);
-}
-
-static void test_invalid_item(const char *str) {
-        uint16_t nr_ports, port_min;
-        int af, ip_protocol;
-
-        assert_se(parse_socket_bind_item(str, &af, &ip_protocol, &nr_ports, &port_min) == -EINVAL);
-
-        log_info("%s: \"%s\" ok", __func__, str);
-}
-
-TEST(valid_items) {
-        test_valid_item("any", AF_UNSPEC, 0, 0, 0);
-        test_valid_item("ipv4", AF_INET, 0, 0, 0);
-        test_valid_item("ipv6", AF_INET6, 0, 0, 0);
-        test_valid_item("ipv4:any", AF_INET, 0, 0, 0);
-        test_valid_item("ipv6:any", AF_INET6, 0, 0, 0);
-        test_valid_item("tcp", AF_UNSPEC, IPPROTO_TCP, 0, 0);
-        test_valid_item("udp", AF_UNSPEC, IPPROTO_UDP, 0, 0);
-        test_valid_item("tcp:any", AF_UNSPEC, IPPROTO_TCP, 0, 0);
-        test_valid_item("udp:any", AF_UNSPEC, IPPROTO_UDP, 0, 0);
-        test_valid_item("6666", AF_UNSPEC, 0, 1, 6666);
-        test_valid_item("6666-6667", AF_UNSPEC, 0, 2, 6666);
-        test_valid_item("65535", AF_UNSPEC, 0, 1, 65535);
-        test_valid_item("1-65535", AF_UNSPEC, 0, 65535, 1);
-        test_valid_item("ipv4:tcp", AF_INET, IPPROTO_TCP, 0, 0);
-        test_valid_item("ipv4:udp", AF_INET, IPPROTO_UDP, 0, 0);
-        test_valid_item("ipv6:tcp", AF_INET6, IPPROTO_TCP, 0, 0);
-        test_valid_item("ipv6:udp", AF_INET6, IPPROTO_UDP, 0, 0);
-        test_valid_item("ipv4:6666", AF_INET, 0, 1, 6666);
-        test_valid_item("ipv6:6666", AF_INET6, 0, 1, 6666);
-        test_valid_item("tcp:6666", AF_UNSPEC, IPPROTO_TCP, 1, 6666);
-        test_valid_item("udp:6666", AF_UNSPEC, IPPROTO_UDP, 1, 6666);
-        test_valid_item("ipv4:tcp:6666", AF_INET, IPPROTO_TCP, 1, 6666);
-        test_valid_item("ipv6:tcp:6666", AF_INET6, IPPROTO_TCP, 1, 6666);
-        test_valid_item("ipv6:udp:6666-6667", AF_INET6, IPPROTO_UDP, 2, 6666);
-        test_valid_item("ipv6:tcp:any", AF_INET6, IPPROTO_TCP, 0, 0);
-}
-
-TEST(invalid_items) {
-        test_invalid_item("");
-        test_invalid_item(":");
-        test_invalid_item("::");
-        test_invalid_item("any:");
-        test_invalid_item("meh");
-        test_invalid_item("zupa:meh");
-        test_invalid_item("zupa:meh:eh");
-        test_invalid_item("ip");
-        test_invalid_item("dccp");
-        test_invalid_item("ipv6meh");
-        test_invalid_item("ipv6::");
-        test_invalid_item("ipv6:ipv6");
-        test_invalid_item("ipv6:icmp");
-        test_invalid_item("ipv6:tcp:0");
-        test_invalid_item("65536");
-        test_invalid_item("0-65535");
-        test_invalid_item("ipv6:tcp:6666-6665");
-        test_invalid_item("ipv6:tcp:6666-100000");
-        test_invalid_item("ipv6::6666");
-        test_invalid_item("ipv6:tcp:any:");
-        test_invalid_item("ipv6:tcp:any:ipv6");
-        test_invalid_item("ipv6:tcp:6666:zupa");
-        test_invalid_item("ipv6:tcp:6666:any");
-        test_invalid_item("ipv6:tcp:6666 zupa");
-        test_invalid_item("ipv6:tcp:6666: zupa");
-        test_invalid_item("ipv6:tcp:6666\n zupa");
-}
-
-DEFINE_TEST_MAIN(LOG_INFO);