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