udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / basic / af-list.c
bloba9ab891e20dc9a075f896857c343d2c567ff1668
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <errno.h>
4 #include <string.h>
5 #include <sys/socket.h>
7 #include "af-list.h"
8 #include "macro.h"
10 static const struct af_name* lookup_af(register const char *str, register GPERF_LEN_TYPE len);
12 #include "af-from-name.h"
13 #include "af-to-name.h"
15 const char *af_to_name(int id) {
17 if (id <= 0)
18 return NULL;
20 if ((size_t) id >= ELEMENTSOF(af_names))
21 return NULL;
23 return af_names[id];
26 int af_from_name(const char *name) {
27 const struct af_name *sc;
29 assert(name);
31 sc = lookup_af(name, strlen(name));
32 if (!sc)
33 return -EINVAL;
35 return sc->id;
38 int af_max(void) {
39 return ELEMENTSOF(af_names);
42 const char *af_to_ipv4_ipv6(int id) {
43 /* Pretty often we want to map the address family to the typically used protocol name for IPv4 +
44 * IPv6. Let's add special helpers for that. */
45 return id == AF_INET ? "ipv4" :
46 id == AF_INET6 ? "ipv6" : NULL;
49 int af_from_ipv4_ipv6(const char *af) {
50 return streq_ptr(af, "ipv4") ? AF_INET :
51 streq_ptr(af, "ipv6") ? AF_INET6 : AF_UNSPEC;