aboutsummaryrefslogtreecommitdiff
path: root/networking/libiproute/rtm_map.c
blob: 8b94c2f2493b27366f6d213843e96c9e78752315 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* vi: set sw=4 ts=4: */
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 */
#include "libbb.h"
#include "rt_names.h"
#include "utils.h"

static const char keywords[] ALIGN1 =
	"local\0""nat\0""broadcast\0""brd\0""anycast\0"
	"multicast\0""prohibit\0""unreachable\0""blackhole\0"
	"xresolve\0""unicast\0""throw\0";
enum {
	ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast,
	ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole,
	ARG_xresolve, ARG_unicast, ARG_throw
};
#define str_local       keywords
#define str_nat         (str_local       + sizeof("local"))
#define str_broadcast   (str_nat         + sizeof("nat"))
#define str_brd         (str_broadcast   + sizeof("broadcast"))
#define str_anycast     (str_brd         + sizeof("brd"))
#define str_multicast   (str_anycast     + sizeof("anycast"))
#define str_prohibit    (str_multicast   + sizeof("multicast"))
#define str_unreachable (str_prohibit    + sizeof("prohibit"))
#define str_blackhole   (str_unreachable + sizeof("unreachable"))
#define str_xresolve    (str_blackhole   + sizeof("blackhole"))
#define str_unicast     (str_xresolve    + sizeof("xresolve"))
#define str_throw       (str_unicast     + sizeof("unicast"))

const char* FAST_FUNC rtnl_rtntype_n2a(int id)
{
	switch (id) {
	case RTN_UNSPEC:
		return "none";
	case RTN_UNICAST:
		return str_unicast;
	case RTN_LOCAL:
		return str_local;
	case RTN_BROADCAST:
		return str_broadcast;
	case RTN_ANYCAST:
		return str_anycast;
	case RTN_MULTICAST:
		return str_multicast;
	case RTN_BLACKHOLE:
		return str_blackhole;
	case RTN_UNREACHABLE:
		return str_unreachable;
	case RTN_PROHIBIT:
		return str_prohibit;
	case RTN_THROW:
		return str_throw;
	case RTN_NAT:
		return str_nat;
	case RTN_XRESOLVE:
		return str_xresolve;
	default:
		return itoa(id);
	}
}

int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg)
{
	const smalluint key = index_in_substrings(keywords, arg) + 1;
	char *end;
	unsigned long res;

	if (key == ARG_local)
		res = RTN_LOCAL;
	else if (key == ARG_nat)
		res = RTN_NAT;
	else if (key == ARG_broadcast || key == ARG_brd)
		res = RTN_BROADCAST;
	else if (key == ARG_anycast)
		res = RTN_ANYCAST;
	else if (key == ARG_multicast)
		res = RTN_MULTICAST;
	else if (key == ARG_prohibit)
		res = RTN_PROHIBIT;
	else if (key == ARG_unreachable)
		res = RTN_UNREACHABLE;
	else if (key == ARG_blackhole)
		res = RTN_BLACKHOLE;
	else if (key == ARG_xresolve)
		res = RTN_XRESOLVE;
	else if (key == ARG_unicast)
		res = RTN_UNICAST;
	else if (key == ARG_throw)
		res = RTN_THROW;
	else {
		res = strtoul(arg, &end, 0);
		if (end == arg || *end || res > 255)
			return -1;
	}
	*id = res;
	return 0;
}

int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg)
{
	uint32_t realm = 0;
	char *p = strchr(arg, '/');

	*realms = 0;
	if (p) {
		*p = 0;
		if (rtnl_rtrealm_a2n(realms, arg)) {
			*p = '/';
			return -1;
		}
		*realms <<= 16;
		*p = '/';
		arg = p+1;
	}
	if (*arg && rtnl_rtrealm_a2n(&realm, arg))
		return -1;
	*realms |= realm;
	return 0;
}