blob: 5e358e1058a089acb791932deff2eac577649896 [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/*
3 * rtm_map.c
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 *
12 */
13
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000014#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include "rt_names.h"
16#include "utils.h"
17
Denys Vlasenkod31575a2009-10-13 17:58:24 +020018const char* FAST_FUNC rtnl_rtntype_n2a(int id, char *buf)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000019{
20 switch (id) {
21 case RTN_UNSPEC:
22 return "none";
23 case RTN_UNICAST:
24 return "unicast";
25 case RTN_LOCAL:
26 return "local";
27 case RTN_BROADCAST:
28 return "broadcast";
29 case RTN_ANYCAST:
30 return "anycast";
31 case RTN_MULTICAST:
32 return "multicast";
33 case RTN_BLACKHOLE:
34 return "blackhole";
35 case RTN_UNREACHABLE:
36 return "unreachable";
37 case RTN_PROHIBIT:
38 return "prohibit";
39 case RTN_THROW:
40 return "throw";
41 case RTN_NAT:
42 return "nat";
43 case RTN_XRESOLVE:
44 return "xresolve";
45 default:
Denys Vlasenkod31575a2009-10-13 17:58:24 +020046 /* buf is SPRINT_BSIZE big */
47 sprintf(buf, "%d", id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000048 return buf;
49 }
50}
51
52
Denys Vlasenkod31575a2009-10-13 17:58:24 +020053int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000054{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000055 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +000056 "local\0""nat\0""broadcast\0""brd\0""anycast\0"
57 "multicast\0""prohibit\0""unreachable\0""blackhole\0"
58 "xresolve\0""unicast\0""throw\0";
59 enum {
60 ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast,
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000061 ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole,
62 ARG_xresolve, ARG_unicast, ARG_throw
63 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +000064 const smalluint key = index_in_substrings(keywords, arg) + 1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000065 char *end;
66 unsigned long res;
67
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000068 if (key == ARG_local)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000069 res = RTN_LOCAL;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000070 else if (key == ARG_nat)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000071 res = RTN_NAT;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000072 else if (key == ARG_broadcast || key == ARG_brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000073 res = RTN_BROADCAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000074 else if (key == ARG_anycast)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075 res = RTN_ANYCAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000076 else if (key == ARG_multicast)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077 res = RTN_MULTICAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000078 else if (key == ARG_prohibit)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000079 res = RTN_PROHIBIT;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000080 else if (key == ARG_unreachable)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000081 res = RTN_UNREACHABLE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000082 else if (key == ARG_blackhole)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000083 res = RTN_BLACKHOLE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000084 else if (key == ARG_xresolve)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085 res = RTN_XRESOLVE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000086 else if (key == ARG_unicast)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000087 res = RTN_UNICAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000088 else if (key == ARG_throw)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000089 res = RTN_THROW;
90 else {
91 res = strtoul(arg, &end, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020092 if (end == arg || *end || res > 255)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093 return -1;
94 }
95 *id = res;
96 return 0;
97}
98
Denys Vlasenkod31575a2009-10-13 17:58:24 +020099int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100{
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000101 uint32_t realm = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102 char *p = strchr(arg, '/');
103
104 *realms = 0;
105 if (p) {
106 *p = 0;
107 if (rtnl_rtrealm_a2n(realms, arg)) {
108 *p = '/';
109 return -1;
110 }
111 *realms <<= 16;
112 *p = '/';
113 arg = p+1;
114 }
115 if (*arg && rtnl_rtrealm_a2n(&realm, arg))
116 return -1;
117 *realms |= realm;
118 return 0;
119}