Skip to content

Commit 159d68c

Browse files
author
Julia Kartseva
committed
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.
1 parent 560d762 commit 159d68c

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/shared/ip-protocol-list.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,13 @@ int parse_ip_protocol(const char *s) {
6565

6666
return i;
6767
}
68+
69+
const char *ip_protocol_to_tcp_udp(int id) {
70+
return IN_SET(id, IPPROTO_TCP, IPPROTO_UDP) ?
71+
ip_protocol_to_name(id) : NULL;
72+
}
73+
74+
int ip_protocol_from_tcp_udp(const char *ip_protocol) {
75+
int id = ip_protocol_from_name(ip_protocol);
76+
return IN_SET(id, IPPROTO_TCP, IPPROTO_UDP) ? id : -EINVAL;
77+
}

src/shared/ip-protocol-list.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
const char *ip_protocol_to_name(int id);
55
int ip_protocol_from_name(const char *name);
66
int parse_ip_protocol(const char *s);
7+
8+
const char *ip_protocol_to_tcp_udp(int id);
9+
int ip_protocol_from_tcp_udp(const char *ip_protocol);

0 commit comments

Comments
 (0)