diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-04-07 14:03:26 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2022-04-07 18:25:55 +0200 |
commit | 3fb72d6388c70def79b70b9f20c8e3c5c4609840 (patch) | |
tree | 33f971330410d6b8a894acb73ae6f3cbeee8a80d | |
parent | Move path_simplify_and_warn() to new shared/parse-helpers.c (diff) | |
download | systemd-3fb72d6388c70def79b70b9f20c8e3c5c4609840.tar.xz systemd-3fb72d6388c70def79b70b9f20c8e3c5c4609840.zip |
Merge parse-socket-bind-item.? into parse-helpers.?
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.
-rw-r--r-- | src/core/load-fragment.c | 1 | ||||
-rw-r--r-- | src/shared/bus-unit-util.c | 2 | ||||
-rw-r--r-- | src/shared/meson.build | 2 | ||||
-rw-r--r-- | src/shared/parse-helpers.c | 147 | ||||
-rw-r--r-- | src/shared/parse-helpers.h | 9 | ||||
-rw-r--r-- | src/shared/parse-socket-bind-item.c | 150 | ||||
-rw-r--r-- | src/shared/parse-socket-bind-item.h | 12 | ||||
-rw-r--r-- | src/test/meson.build | 2 | ||||
-rw-r--r-- | src/test/test-parse-helpers.c (renamed from src/test/test-parse-socket-bind-item.c) | 2 |
9 files changed, 159 insertions, 168 deletions
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index b81db4617b..74c41335ca 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -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" diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c index 95ef3230b4..4f02c2c373 100644 --- a/src/shared/bus-unit-util.c +++ b/src/shared/bus-unit-util.c @@ -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" diff --git a/src/shared/meson.build b/src/shared/meson.build index 8f1f3b40a9..1d4e4a07c0 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -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', diff --git a/src/shared/parse-helpers.c b/src/shared/parse-helpers.c index 5ebe366265..a9bb58715e 100644 --- a/src/shared/parse-helpers.c +++ b/src/shared/parse-helpers.c @@ -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; +} diff --git a/src/shared/parse-helpers.h b/src/shared/parse-helpers.h index 8b1d33498c..49da2815fb 100644 --- a/src/shared/parse-helpers.h +++ b/src/shared/parse-helpers.h @@ -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 index 3c92467632..0000000000 --- a/src/shared/parse-socket-bind-item.c +++ /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 index c8cff8d730..0000000000 --- a/src/shared/parse-socket-bind-item.h +++ /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); diff --git a/src/test/meson.build b/src/test/meson.build index 297a65d9af..c189f7073b 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -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-socket-bind-item.c b/src/test/test-parse-helpers.c index 6bca27265c..052e2514f4 100644 --- a/src/test/test-parse-socket-bind-item.c +++ b/src/test/test-parse-helpers.c @@ -5,7 +5,7 @@ #include <stdio.h> #include "macro.h" -#include "parse-socket-bind-item.h" +#include "parse-helpers.h" #include "tests.h" static void test_valid_item( |