From: Julia Kartseva Date: Sat, 26 Jun 2021 00:14:40 +0000 (-0700) Subject: shared: add ip_protocol_{from|to}_tcp_udp helpers X-Git-Tag: v249-rc3~23^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=159d68c3e09657b4fd252b2c5ea6ef3095a37ad2;p=thirdparty%2Fsystemd.git shared: add ip_protocol_{from|to}_tcp_udp helpers Thin wrappers of ip_protocol_{from|to}_name targeting IPPROTO_TCP and IPPROTO_UDP only. Used to parse IP protocol configuration restricted only to TCP and UDP, e.g. in SocketBind{Allow|Deny}= unit property. These helpers are inspired by af_{from|to}_ipv4_ipv6 and potentially extendable with other IP protocols if there is a use-case to expose them. --- diff --git a/src/shared/ip-protocol-list.c b/src/shared/ip-protocol-list.c index 0623d5e9be8..21d88f26607 100644 --- a/src/shared/ip-protocol-list.c +++ b/src/shared/ip-protocol-list.c @@ -65,3 +65,13 @@ int parse_ip_protocol(const char *s) { return i; } + +const char *ip_protocol_to_tcp_udp(int id) { + return IN_SET(id, IPPROTO_TCP, IPPROTO_UDP) ? + ip_protocol_to_name(id) : NULL; +} + +int ip_protocol_from_tcp_udp(const char *ip_protocol) { + int id = ip_protocol_from_name(ip_protocol); + return IN_SET(id, IPPROTO_TCP, IPPROTO_UDP) ? id : -EINVAL; +} diff --git a/src/shared/ip-protocol-list.h b/src/shared/ip-protocol-list.h index abe3f5fc284..b40ec083016 100644 --- a/src/shared/ip-protocol-list.h +++ b/src/shared/ip-protocol-list.h @@ -4,3 +4,6 @@ const char *ip_protocol_to_name(int id); int ip_protocol_from_name(const char *name); int parse_ip_protocol(const char *s); + +const char *ip_protocol_to_tcp_udp(int id); +int ip_protocol_from_tcp_udp(const char *ip_protocol);