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