]>
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 SH |
27 | { |
28 | static char ncache[16]; | |
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) { | |
37 | icache = proto; | |
38 | strncpy(ncache, pe->p_name, 16); | |
39 | strncpy(buf, pe->p_name, len); | |
40 | return buf; | |
41 | } | |
42 | snprintf(buf, len, "ipproto-%d", proto); | |
43 | return buf; | |
44 | } | |
45 | ||
46ac8a55 | 46 | int inet_proto_a2n(const char *buf) |
aba5acdf SH |
47 | { |
48 | static char ncache[16]; | |
49 | static int icache = -1; | |
50 | struct protoent *pe; | |
51 | ||
52 | if (icache>=0 && strcmp(ncache, buf) == 0) | |
53 | return icache; | |
54 | ||
55 | if (buf[0] >= '0' && buf[0] <= '9') { | |
56 | __u8 ret; | |
57 | if (get_u8(&ret, buf, 10)) | |
58 | return -1; | |
59 | return ret; | |
60 | } | |
61 | ||
62 | pe = getprotobyname(buf); | |
63 | if (pe) { | |
64 | icache = pe->p_proto; | |
65 | strncpy(ncache, pe->p_name, 16); | |
66 | return pe->p_proto; | |
67 | } | |
68 | return -1; | |
69 | } |