blob: 8b94c2f2493b27366f6d213843e96c9e78752315 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenkofb132e42010-10-29 11:46:52 +02003 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 */
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000010#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000011#include "rt_names.h"
12#include "utils.h"
13
Denys Vlasenko4323ac82020-11-21 17:23:13 +010014static const char keywords[] ALIGN1 =
15 "local\0""nat\0""broadcast\0""brd\0""anycast\0"
16 "multicast\0""prohibit\0""unreachable\0""blackhole\0"
17 "xresolve\0""unicast\0""throw\0";
18enum {
19 ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast,
20 ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole,
21 ARG_xresolve, ARG_unicast, ARG_throw
22};
23#define str_local keywords
24#define str_nat (str_local + sizeof("local"))
25#define str_broadcast (str_nat + sizeof("nat"))
26#define str_brd (str_broadcast + sizeof("broadcast"))
27#define str_anycast (str_brd + sizeof("brd"))
28#define str_multicast (str_anycast + sizeof("anycast"))
29#define str_prohibit (str_multicast + sizeof("multicast"))
30#define str_unreachable (str_prohibit + sizeof("prohibit"))
31#define str_blackhole (str_unreachable + sizeof("unreachable"))
32#define str_xresolve (str_blackhole + sizeof("blackhole"))
33#define str_unicast (str_xresolve + sizeof("xresolve"))
34#define str_throw (str_unicast + sizeof("unicast"))
35
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +020036const char* FAST_FUNC rtnl_rtntype_n2a(int id)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000037{
38 switch (id) {
39 case RTN_UNSPEC:
40 return "none";
41 case RTN_UNICAST:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010042 return str_unicast;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000043 case RTN_LOCAL:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010044 return str_local;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000045 case RTN_BROADCAST:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010046 return str_broadcast;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000047 case RTN_ANYCAST:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010048 return str_anycast;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049 case RTN_MULTICAST:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010050 return str_multicast;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051 case RTN_BLACKHOLE:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010052 return str_blackhole;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000053 case RTN_UNREACHABLE:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010054 return str_unreachable;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000055 case RTN_PROHIBIT:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010056 return str_prohibit;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000057 case RTN_THROW:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010058 return str_throw;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000059 case RTN_NAT:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010060 return str_nat;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061 case RTN_XRESOLVE:
Denys Vlasenko4323ac82020-11-21 17:23:13 +010062 return str_xresolve;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063 default:
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +020064 return itoa(id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065 }
66}
67
Denys Vlasenkod31575a2009-10-13 17:58:24 +020068int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000069{
Denis Vlasenko990d0f62007-07-24 15:54:42 +000070 const smalluint key = index_in_substrings(keywords, arg) + 1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071 char *end;
72 unsigned long res;
73
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000074 if (key == ARG_local)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075 res = RTN_LOCAL;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000076 else if (key == ARG_nat)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077 res = RTN_NAT;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000078 else if (key == ARG_broadcast || key == ARG_brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079 res = RTN_BROADCAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000080 else if (key == ARG_anycast)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081 res = RTN_ANYCAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000082 else if (key == ARG_multicast)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000083 res = RTN_MULTICAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000084 else if (key == ARG_prohibit)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085 res = RTN_PROHIBIT;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000086 else if (key == ARG_unreachable)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000087 res = RTN_UNREACHABLE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000088 else if (key == ARG_blackhole)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000089 res = RTN_BLACKHOLE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000090 else if (key == ARG_xresolve)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091 res = RTN_XRESOLVE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000092 else if (key == ARG_unicast)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093 res = RTN_UNICAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000094 else if (key == ARG_throw)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095 res = RTN_THROW;
96 else {
97 res = strtoul(arg, &end, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020098 if (end == arg || *end || res > 255)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099 return -1;
100 }
101 *id = res;
102 return 0;
103}
104
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200105int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000106{
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000107 uint32_t realm = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000108 char *p = strchr(arg, '/');
109
110 *realms = 0;
111 if (p) {
112 *p = 0;
113 if (rtnl_rtrealm_a2n(realms, arg)) {
114 *p = '/';
115 return -1;
116 }
117 *realms <<= 16;
118 *p = '/';
119 arg = p+1;
120 }
121 if (*arg && rtnl_rtrealm_a2n(&realm, arg))
122 return -1;
123 *realms |= realm;
124 return 0;
125}