blob: 3bab53baf2eb4e4c13e802a868acd6fab680c8f8 [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 */
10
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000011#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000012#include "rt_names.h"
13#include "utils.h"
14
Denys Vlasenkod31575a2009-10-13 17:58:24 +020015const char* FAST_FUNC rtnl_rtntype_n2a(int id, char *buf)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000016{
17 switch (id) {
18 case RTN_UNSPEC:
19 return "none";
20 case RTN_UNICAST:
21 return "unicast";
22 case RTN_LOCAL:
23 return "local";
24 case RTN_BROADCAST:
25 return "broadcast";
26 case RTN_ANYCAST:
27 return "anycast";
28 case RTN_MULTICAST:
29 return "multicast";
30 case RTN_BLACKHOLE:
31 return "blackhole";
32 case RTN_UNREACHABLE:
33 return "unreachable";
34 case RTN_PROHIBIT:
35 return "prohibit";
36 case RTN_THROW:
37 return "throw";
38 case RTN_NAT:
39 return "nat";
40 case RTN_XRESOLVE:
41 return "xresolve";
42 default:
Denys Vlasenkod31575a2009-10-13 17:58:24 +020043 /* buf is SPRINT_BSIZE big */
44 sprintf(buf, "%d", id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000045 return buf;
46 }
47}
48
49
Denys Vlasenkod31575a2009-10-13 17:58:24 +020050int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000052 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +000053 "local\0""nat\0""broadcast\0""brd\0""anycast\0"
54 "multicast\0""prohibit\0""unreachable\0""blackhole\0"
55 "xresolve\0""unicast\0""throw\0";
56 enum {
57 ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast,
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000058 ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole,
59 ARG_xresolve, ARG_unicast, ARG_throw
60 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +000061 const smalluint key = index_in_substrings(keywords, arg) + 1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000062 char *end;
63 unsigned long res;
64
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000065 if (key == ARG_local)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066 res = RTN_LOCAL;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000067 else if (key == ARG_nat)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068 res = RTN_NAT;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000069 else if (key == ARG_broadcast || key == ARG_brd)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000070 res = RTN_BROADCAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000071 else if (key == ARG_anycast)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000072 res = RTN_ANYCAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000073 else if (key == ARG_multicast)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000074 res = RTN_MULTICAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000075 else if (key == ARG_prohibit)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000076 res = RTN_PROHIBIT;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000077 else if (key == ARG_unreachable)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000078 res = RTN_UNREACHABLE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000079 else if (key == ARG_blackhole)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080 res = RTN_BLACKHOLE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000081 else if (key == ARG_xresolve)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000082 res = RTN_XRESOLVE;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000083 else if (key == ARG_unicast)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000084 res = RTN_UNICAST;
Bernhard Reutner-Fischer789b87e2007-06-21 10:20:13 +000085 else if (key == ARG_throw)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000086 res = RTN_THROW;
87 else {
88 res = strtoul(arg, &end, 0);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +020089 if (end == arg || *end || res > 255)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 return -1;
91 }
92 *id = res;
93 return 0;
94}
95
Denys Vlasenkod31575a2009-10-13 17:58:24 +020096int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097{
Denis Vlasenko98ee06d2006-12-31 18:57:37 +000098 uint32_t realm = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000099 char *p = strchr(arg, '/');
100
101 *realms = 0;
102 if (p) {
103 *p = 0;
104 if (rtnl_rtrealm_a2n(realms, arg)) {
105 *p = '/';
106 return -1;
107 }
108 *realms <<= 16;
109 *p = '/';
110 arg = p+1;
111 }
112 if (*arg && rtnl_rtrealm_a2n(&realm, arg))
113 return -1;
114 *realms |= realm;
115 return 0;
116}