diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 14:18:53 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 14:18:53 +0000 |
commit | a0e0018c9a7ef5ce7f6d2c3ae16aecbbd16a8f67 (patch) | |
tree | 8feaf1a1932871b139b3b30be4c09c66489918be /lib/inet_proto.c | |
parent | Initial commit. (diff) | |
download | iproute2-upstream/6.1.0.tar.xz iproute2-upstream/6.1.0.zip |
Adding upstream version 6.1.0.upstream/6.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | lib/inet_proto.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/inet_proto.c b/lib/inet_proto.c new file mode 100644 index 0000000..41e2e8b --- /dev/null +++ b/lib/inet_proto.c @@ -0,0 +1,69 @@ +/* + * inet_proto.c + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netdb.h> +#include <string.h> + +#include "rt_names.h" +#include "utils.h" + +const char *inet_proto_n2a(int proto, char *buf, int len) +{ + static char *ncache; + static int icache = -1; + struct protoent *pe; + + if (proto == icache) + return ncache; + + pe = getprotobynumber(proto); + if (pe && !numeric) { + if (icache != -1) + free(ncache); + icache = proto; + ncache = strdup(pe->p_name); + strlcpy(buf, pe->p_name, len); + return buf; + } + snprintf(buf, len, "ipproto-%d", proto); + return buf; +} + +int inet_proto_a2n(const char *buf) +{ + static char *ncache; + static int icache = -1; + struct protoent *pe; + __u8 ret; + + if (icache != -1 && strcmp(ncache, buf) == 0) + return icache; + + if (!get_u8(&ret, buf, 10)) + return ret; + + pe = getprotobyname(buf); + if (pe) { + if (icache != -1) + free(ncache); + icache = pe->p_proto; + ncache = strdup(pe->p_name); + return pe->p_proto; + } + return -1; +} |