]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared: add ip_protocol_{from|to}_tcp_udp helpers
authorJulia Kartseva <hex@fb.com>
Sat, 26 Jun 2021 00:14:40 +0000 (17:14 -0700)
committerJulia Kartseva <hex@fb.com>
Tue, 29 Jun 2021 21:37:07 +0000 (14:37 -0700)
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.

src/shared/ip-protocol-list.c
src/shared/ip-protocol-list.h

index 0623d5e9be847c7b0e28488879504b6e5933c8e9..21d88f2660776f761b2f296c233c4277b071cc10 100644 (file)
@@ -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;
+}
index abe3f5fc284621f49fb88a8874125843ec09c296..b40ec083016fd3d63af07855f51a65113d17f955 100644 (file)
@@ -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);