]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/ip-protocol-list.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <netinet/in.h>
6 #include "alloc-util.h"
7 #include "ip-protocol-list.h"
9 #include "parse-util.h"
10 #include "string-util.h"
12 static const struct ip_protocol_name
* lookup_ip_protocol(register const char *str
, register GPERF_LEN_TYPE len
);
14 #include "ip-protocol-from-name.h"
15 #include "ip-protocol-to-name.h"
17 const char *ip_protocol_to_name(int id
) {
22 if ((size_t) id
>= ELEMENTSOF(ip_protocol_names
))
25 return ip_protocol_names
[id
];
28 int ip_protocol_from_name(const char *name
) {
29 const struct ip_protocol_name
*sc
;
33 sc
= lookup_ip_protocol(name
, strlen(name
));
40 int parse_ip_protocol_full(const char *s
, bool relaxed
) {
48 /* People commonly use lowercase protocol names, which we can look up very quickly, so let's try that
50 r
= ip_protocol_from_name(s
);
54 /* Do not use strdupa() here, as the input string may come from command line or config files. */
55 _cleanup_free_
char *t
= strdup(s
);
59 r
= ip_protocol_from_name(ascii_strlower(t
));
69 /* If @relaxed, we don't check that we have a name for the protocol. */
70 if (!relaxed
&& !ip_protocol_to_name(p
))
71 return -EPROTONOSUPPORT
;
76 const char *ip_protocol_to_tcp_udp(int id
) {
77 return IN_SET(id
, IPPROTO_TCP
, IPPROTO_UDP
) ?
78 ip_protocol_to_name(id
) : NULL
;
81 int ip_protocol_from_tcp_udp(const char *ip_protocol
) {
82 int id
= ip_protocol_from_name(ip_protocol
);
83 return IN_SET(id
, IPPROTO_TCP
, IPPROTO_UDP
) ? id
: -EINVAL
;