summaryrefslogtreecommitdiffstats
path: root/src/shared/ip-protocol-list.c
blob: aa675ea10b25a5cc9cf1476b1952e269243c12fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* SPDX-License-Identifier: LGPL-2.1+ */

#include <errno.h>
#include <netinet/in.h>

#include "alloc-util.h"
#include "ip-protocol-list.h"
#include "macro.h"
#include "parse-util.h"
#include "string-util.h"

static const struct ip_protocol_name* lookup_ip_protocol(register const char *str, register GPERF_LEN_TYPE len);

#include "ip-protocol-from-name.h"
#include "ip-protocol-to-name.h"

const char *ip_protocol_to_name(int id) {

        if (id < 0)
                return NULL;

        if ((size_t) id >= ELEMENTSOF(ip_protocol_names))
                return NULL;

        return ip_protocol_names[id];
}

int ip_protocol_from_name(const char *name) {
        const struct ip_protocol_name *sc;

        assert(name);

        sc = lookup_ip_protocol(name, strlen(name));
        if (!sc)
                return -EINVAL;

        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;
}