usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / iproute2 / lib / inet_proto.c
bloba55e0e7ba3239eee32e87878f47ce458710f3087
1 /*
2 * inet_proto.c
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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>
23 #include "utils.h"
25 char *inet_proto_n2a(int proto, char *buf, int len)
27 static char ncache[16];
28 static int icache = -1;
29 struct protoent *pe;
31 if (proto == icache)
32 return ncache;
34 pe = getprotobynumber(proto);
35 if (pe) {
36 icache = proto;
37 strncpy(ncache, pe->p_name, 16);
38 strncpy(buf, pe->p_name, len);
39 return buf;
41 snprintf(buf, len, "ipproto-%d", proto);
42 return buf;
45 int inet_proto_a2n(char *buf)
47 static char ncache[16];
48 static int icache = -1;
49 struct protoent *pe;
51 if (icache>=0 && strcmp(ncache, buf) == 0)
52 return icache;
54 if (buf[0] >= '0' && buf[0] <= '9') {
55 __u8 ret;
56 if (get_u8(&ret, buf, 10))
57 return -1;
58 return ret;
61 pe = getprotobyname(buf);
62 if (pe) {
63 icache = pe->p_proto;
64 strncpy(ncache, pe->p_name, 16);
65 return pe->p_proto;
67 return -1;