]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
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> | |
aba5acdf SH |
16 | #include <fcntl.h> |
17 | #include <sys/socket.h> | |
18 | #include <netinet/in.h> | |
19 | #include <netdb.h> | |
20 | #include <string.h> | |
21 | ||
46ac8a55 | 22 | #include "rt_names.h" |
aba5acdf SH |
23 | #include "utils.h" |
24 | ||
46ac8a55 | 25 | const char *inet_proto_n2a(int proto, char *buf, int len) |
aba5acdf | 26 | { |
cfda500a | 27 | static char *ncache; |
aba5acdf SH |
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) { | |
cfda500a PS |
36 | if (icache != -1) |
37 | free(ncache); | |
aba5acdf | 38 | icache = proto; |
cfda500a | 39 | ncache = strdup(pe->p_name); |
18f156bf | 40 | strlcpy(buf, pe->p_name, len); |
aba5acdf SH |
41 | return buf; |
42 | } | |
43 | snprintf(buf, len, "ipproto-%d", proto); | |
44 | return buf; | |
45 | } | |
46 | ||
46ac8a55 | 47 | int inet_proto_a2n(const char *buf) |
aba5acdf | 48 | { |
cfda500a | 49 | static char *ncache; |
aba5acdf SH |
50 | static int icache = -1; |
51 | struct protoent *pe; | |
cfda500a | 52 | __u8 ret; |
aba5acdf | 53 | |
cfda500a | 54 | if (icache != -1 && strcmp(ncache, buf) == 0) |
aba5acdf SH |
55 | return icache; |
56 | ||
cfda500a | 57 | if (!get_u8(&ret, buf, 10)) |
aba5acdf | 58 | return ret; |
aba5acdf SH |
59 | |
60 | pe = getprotobyname(buf); | |
61 | if (pe) { | |
cfda500a PS |
62 | if (icache != -1) |
63 | free(ncache); | |
aba5acdf | 64 | icache = pe->p_proto; |
cfda500a | 65 | ncache = strdup(pe->p_name); |
aba5acdf SH |
66 | return pe->p_proto; |
67 | } | |
68 | return -1; | |
69 | } |