1
2
3
4
5
6
7
8
9
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
22#include "rt_names.h"
23#include "utils.h"
24
25const char *inet_proto_n2a(int proto, char *buf, int len)
26{
27 static char *ncache;
28 static int icache = -1;
29 struct protoent *pe;
30
31 if (proto == icache)
32 return ncache;
33
34 pe = getprotobynumber(proto);
35 if (pe && !numeric) {
36 if (icache != -1)
37 free(ncache);
38 icache = proto;
39 ncache = strdup(pe->p_name);
40 strlcpy(buf, pe->p_name, len);
41 return buf;
42 }
43 snprintf(buf, len, "ipproto-%d", proto);
44 return buf;
45}
46
47int inet_proto_a2n(const char *buf)
48{
49 static char *ncache;
50 static int icache = -1;
51 struct protoent *pe;
52 __u8 ret;
53
54 if (icache != -1 && strcmp(ncache, buf) == 0)
55 return icache;
56
57 if (!get_u8(&ret, buf, 10))
58 return ret;
59
60 pe = getprotobyname(buf);
61 if (pe) {
62 if (icache != -1)
63 free(ncache);
64 icache = pe->p_proto;
65 ncache = strdup(pe->p_name);
66 return pe->p_proto;
67 }
68 return -1;
69}
70