delete tons of extra #includes
[oweals/busybox.git] / networking / libiproute / rtm_map.c
1 /* vi: set sw=4 ts=4: */
2 /*
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
14 #include "libbb.h"
15 #include "rt_names.h"
16 #include "utils.h"
17
18 const char *rtnl_rtntype_n2a(int id, char *buf, int len)
19 {
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:
46                 snprintf(buf, len, "%d", id);
47                 return buf;
48         }
49 }
50
51
52 int rtnl_rtntype_a2n(int *id, char *arg)
53 {
54         char *end;
55         unsigned long res;
56
57         if (strcmp(arg, "local") == 0)
58                 res = RTN_LOCAL;
59         else if (strcmp(arg, "nat") == 0)
60                 res = RTN_NAT;
61         else if (matches(arg, "broadcast") == 0 ||
62                  strcmp(arg, "brd") == 0)
63                 res = RTN_BROADCAST;
64         else if (matches(arg, "anycast") == 0)
65                 res = RTN_ANYCAST;
66         else if (matches(arg, "multicast") == 0)
67                 res = RTN_MULTICAST;
68         else if (matches(arg, "prohibit") == 0)
69                 res = RTN_PROHIBIT;
70         else if (matches(arg, "unreachable") == 0)
71                 res = RTN_UNREACHABLE;
72         else if (matches(arg, "blackhole") == 0)
73                 res = RTN_BLACKHOLE;
74         else if (matches(arg, "xresolve") == 0)
75                 res = RTN_XRESOLVE;
76         else if (matches(arg, "unicast") == 0)
77                 res = RTN_UNICAST;
78         else if (strcmp(arg, "throw") == 0)
79                 res = RTN_THROW;
80         else {
81                 res = strtoul(arg, &end, 0);
82                 if (!end || end == arg || *end || res > 255)
83                         return -1;
84         }
85         *id = res;
86         return 0;
87 }
88
89 int get_rt_realms(uint32_t *realms, char *arg)
90 {
91         uint32_t realm = 0;
92         char *p = strchr(arg, '/');
93
94         *realms = 0;
95         if (p) {
96                 *p = 0;
97                 if (rtnl_rtrealm_a2n(realms, arg)) {
98                         *p = '/';
99                         return -1;
100                 }
101                 *realms <<= 16;
102                 *p = '/';
103                 arg = p+1;
104         }
105         if (*arg && rtnl_rtrealm_a2n(&realm, arg))
106                 return -1;
107         *realms |= realm;
108         return 0;
109 }