]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/inet_proto.c
Merge branch 'iproute2-master' into iproute2-next
[mirror_iproute2.git] / lib / inet_proto.c
1 /*
2 * inet_proto.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #ifdef HAVE_LIBBSD
22 #include <bsd/string.h>
23 #endif
24
25 #include "rt_names.h"
26 #include "utils.h"
27
28 const char *inet_proto_n2a(int proto, char *buf, int len)
29 {
30 static char *ncache;
31 static int icache = -1;
32 struct protoent *pe;
33
34 if (proto == icache)
35 return ncache;
36
37 pe = getprotobynumber(proto);
38 if (pe) {
39 if (icache != -1)
40 free(ncache);
41 icache = proto;
42 ncache = strdup(pe->p_name);
43 strlcpy(buf, pe->p_name, len);
44 return buf;
45 }
46 snprintf(buf, len, "ipproto-%d", proto);
47 return buf;
48 }
49
50 int inet_proto_a2n(const char *buf)
51 {
52 static char *ncache;
53 static int icache = -1;
54 struct protoent *pe;
55 __u8 ret;
56
57 if (icache != -1 && strcmp(ncache, buf) == 0)
58 return icache;
59
60 if (!get_u8(&ret, buf, 10))
61 return ret;
62
63 pe = getprotobyname(buf);
64 if (pe) {
65 if (icache != -1)
66 free(ncache);
67 icache = pe->p_proto;
68 ncache = strdup(pe->p_name);
69 return pe->p_proto;
70 }
71 return -1;
72 }