diff --git a/src/shared/ip-protocol-list.c b/src/shared/ip-protocol-list.c index 775cd3313d..f0c4ddae2d 100644 --- a/src/shared/ip-protocol-list.c +++ b/src/shared/ip-protocol-list.c @@ -2,9 +2,11 @@ #include #include -#include +#include "alloc-util.h" #include "ip-protocol-list.h" +#include "parse-util.h" +#include "string-util.h" #include "macro.h" static const struct ip_protocol_name* lookup_ip_protocol(register const char *str, register GPERF_LEN_TYPE len); @@ -34,3 +36,32 @@ 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; + + 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) + return r; + + if (!ip_protocol_to_name(i)) + return -EINVAL; + + return i; +} diff --git a/src/shared/ip-protocol-list.h b/src/shared/ip-protocol-list.h index dfe346744f..5c94969695 100644 --- a/src/shared/ip-protocol-list.h +++ b/src/shared/ip-protocol-list.h @@ -3,3 +3,4 @@ const char *ip_protocol_to_name(int id); int ip_protocol_from_name(const char *name); +int parse_ip_protocol(const char *s);