1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-26 14:04:03 +03:00

shared/ip-procotol-list: generalize and rework parse_ip_protocol()

Optionally, accept protocols that don't have a known name.
Avoid any allocations in the common case.
Return more granular error codes: -ERANGE for negative values,
-EOPNOTSUPP if the protocol is a valid number, but we don't know
the protocol, and -EINVAL only if it's not a numerical string.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2023-09-16 12:43:16 +02:00
parent 04c2a002e6
commit 1b2733b412
3 changed files with 54 additions and 40 deletions

View File

@ -37,33 +37,40 @@ int ip_protocol_from_name(const char *name) {
return sc->id;
}
int parse_ip_protocol(const char *s) {
_cleanup_free_ char *str = NULL;
int i, r;
int parse_ip_protocol_full(const char *s, bool relaxed) {
int r, p;
assert(s);
if (isempty(s))
return IPPROTO_IP;
/* Do not use strdupa() here, as the input string may come from *
* command line or config files. */
str = strdup(s);
if (!str)
return -ENOMEM;
i = ip_protocol_from_name(ascii_strlower(str));
if (i >= 0)
return i;
r = safe_atoi(str, &i);
if (r < 0)
/* People commonly use lowercase protocol names, which we can look up very quickly, so let's try that
* first. */
r = ip_protocol_from_name(s);
if (r >= 0)
return r;
if (!ip_protocol_to_name(i))
return -EINVAL;
/* Do not use strdupa() here, as the input string may come from command line or config files. */
_cleanup_free_ char *t = strdup(s);
if (!t)
return -ENOMEM;
return i;
r = ip_protocol_from_name(ascii_strlower(t));
if (r >= 0)
return r;
r = safe_atoi(t, &p);
if (r < 0)
return r;
if (p < 0)
return -ERANGE;
/* If @relaxed, we don't check that we have a name for the protocol. */
if (!relaxed && !ip_protocol_to_name(p))
return -EPROTONOSUPPORT;
return p;
}
const char *ip_protocol_to_tcp_udp(int id) {

View File

@ -1,9 +1,14 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <stdbool.h>
const char *ip_protocol_to_name(int id);
int ip_protocol_from_name(const char *name);
int parse_ip_protocol(const char *s);
int parse_ip_protocol_full(const char *s, bool relaxed);
static inline int parse_ip_protocol(const char *s) {
return parse_ip_protocol_full(s, false);
}
const char *ip_protocol_to_tcp_udp(int id);
int ip_protocol_from_tcp_udp(const char *ip_protocol);

View File

@ -17,13 +17,13 @@ static void test_int(int i) {
assert_se(ip_protocol_from_name(ip_protocol_to_name(parse_ip_protocol(str))) == i);
}
static void test_int_fail(int i) {
static void test_int_fail(int i, int error) {
char str[DECIMAL_STR_MAX(int)];
assert_se(!ip_protocol_to_name(i));
xsprintf(str, "%i", i);
assert_se(parse_ip_protocol(str) == -EINVAL);
assert_se(parse_ip_protocol(str) == error);
}
static void test_str(const char *s) {
@ -31,39 +31,41 @@ static void test_str(const char *s) {
assert_se(streq(ip_protocol_to_name(parse_ip_protocol(s)), s));
}
static void test_str_fail(const char *s) {
static void test_str_fail(const char *s, int error) {
assert_se(ip_protocol_from_name(s) == -EINVAL);
assert_se(parse_ip_protocol(s) == -EINVAL);
}
static void test_parse_ip_protocol_one(const char *s, int expected) {
assert_se(parse_ip_protocol(s) == expected);
assert_se(parse_ip_protocol(s) == error);
}
TEST(integer) {
test_int(IPPROTO_TCP);
test_int(IPPROTO_DCCP);
test_int_fail(-1);
test_int_fail(1024 * 1024);
test_int_fail(-1, -ERANGE);
test_int_fail(1024 * 1024, -EPROTONOSUPPORT);
}
TEST(string) {
test_str("sctp");
test_str("udp");
test_str_fail("hoge");
test_str_fail("-1");
test_str_fail("1000000000");
test_str_fail("hoge", -EINVAL);
test_str_fail("-1", -ERANGE);
test_str_fail("1000000000", -EPROTONOSUPPORT);
}
TEST(parse_ip_protocol) {
test_parse_ip_protocol_one("sctp", IPPROTO_SCTP);
test_parse_ip_protocol_one("ScTp", IPPROTO_SCTP);
test_parse_ip_protocol_one("ip", IPPROTO_IP);
test_parse_ip_protocol_one("", IPPROTO_IP);
test_parse_ip_protocol_one("1", 1);
test_parse_ip_protocol_one("0", 0);
test_parse_ip_protocol_one("-10", -EINVAL);
test_parse_ip_protocol_one("100000000", -EINVAL);
assert_se(parse_ip_protocol("sctp") == IPPROTO_SCTP);
assert_se(parse_ip_protocol("ScTp") == IPPROTO_SCTP);
assert_se(parse_ip_protocol("ip") == IPPROTO_IP);
assert_se(parse_ip_protocol("") == IPPROTO_IP);
assert_se(parse_ip_protocol("1") == 1);
assert_se(parse_ip_protocol("0") == 0);
assert_se(parse_ip_protocol("-10") == -ERANGE);
assert_se(parse_ip_protocol("100000000") == -EPROTONOSUPPORT);
}
TEST(parse_ip_protocol_full) {
assert_se(parse_ip_protocol_full("-1", true) == -ERANGE);
assert_se(parse_ip_protocol_full("0", true) == 0);
assert_se(parse_ip_protocol_full("11", true) == 11);
}
DEFINE_TEST_MAIN(LOG_INFO);