File: inet_proto.c

package info (click to toggle)
iproute2 6.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,380 kB
  • sloc: ansic: 127,486; sh: 1,363; cpp: 946; makefile: 720; yacc: 421; lex: 145; python: 43
file content (64 lines) | stat: -rw-r--r-- 1,151 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * inet_proto.c
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>

#include "rt_names.h"
#include "utils.h"

const char *inet_proto_n2a(int proto, char *buf, int len)
{
	static char *ncache;
	static int icache = -1;
	struct protoent *pe;

	if (proto == icache)
		return ncache;

	pe = getprotobynumber(proto);
	if (pe && !numeric) {
		if (icache != -1)
			free(ncache);
		icache = proto;
		ncache = strdup(pe->p_name);
		strlcpy(buf, pe->p_name, len);
		return buf;
	}
	snprintf(buf, len, "ipproto-%d", proto);
	return buf;
}

int inet_proto_a2n(const char *buf)
{
	static char *ncache;
	static int icache = -1;
	struct protoent *pe;
	__u8 ret;

	if (icache != -1 && strcmp(ncache, buf) == 0)
		return icache;

	if (!get_u8(&ret, buf, 10))
		return ret;

	pe = getprotobyname(buf);
	if (pe) {
		if (icache != -1)
			free(ncache);
		icache = pe->p_proto;
		ncache = strdup(pe->p_name);
		return pe->p_proto;
	}
	return -1;
}