]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | #include "rt_names.h" | |
23 | #include "utils.h" | |
24 | ||
25 | const 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) { | |
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 | ||
47 | int 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 | } |