oops
[oweals/busybox.git] / networking / libiproute / rtm_map.c
1 /*
2  * rtm_map.c
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  */
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "rt_names.h"
17 #include "utils.h"
18
19 char *rtnl_rtntype_n2a(int id, char *buf, int len)
20 {
21         switch (id) {
22         case RTN_UNSPEC:
23                 return "none";
24         case RTN_UNICAST:
25                 return "unicast";
26         case RTN_LOCAL:
27                 return "local";
28         case RTN_BROADCAST:
29                 return "broadcast";
30         case RTN_ANYCAST:
31                 return "anycast";
32         case RTN_MULTICAST:
33                 return "multicast";
34         case RTN_BLACKHOLE:
35                 return "blackhole";
36         case RTN_UNREACHABLE:
37                 return "unreachable";
38         case RTN_PROHIBIT:
39                 return "prohibit";
40         case RTN_THROW:
41                 return "throw";
42         case RTN_NAT:
43                 return "nat";
44         case RTN_XRESOLVE:
45                 return "xresolve";
46         default:
47                 snprintf(buf, len, "%d", id);
48                 return buf;
49         }
50 }
51
52
53 int rtnl_rtntype_a2n(int *id, char *arg)
54 {
55         char *end;
56         unsigned long res;
57
58         if (strcmp(arg, "local") == 0)
59                 res = RTN_LOCAL;
60         else if (strcmp(arg, "nat") == 0)
61                 res = RTN_NAT;
62         else if (matches(arg, "broadcast") == 0 ||
63                  strcmp(arg, "brd") == 0)
64                 res = RTN_BROADCAST;
65         else if (matches(arg, "anycast") == 0)
66                 res = RTN_ANYCAST;
67         else if (matches(arg, "multicast") == 0)
68                 res = RTN_MULTICAST;
69         else if (matches(arg, "prohibit") == 0)
70                 res = RTN_PROHIBIT;
71         else if (matches(arg, "unreachable") == 0)
72                 res = RTN_UNREACHABLE;
73         else if (matches(arg, "blackhole") == 0)
74                 res = RTN_BLACKHOLE;
75         else if (matches(arg, "xresolve") == 0)
76                 res = RTN_XRESOLVE;
77         else if (matches(arg, "unicast") == 0)
78                 res = RTN_UNICAST;
79         else if (strcmp(arg, "throw") == 0)
80                 res = RTN_THROW;
81         else {
82                 res = strtoul(arg, &end, 0);
83                 if (!end || end == arg || *end || res > 255)
84                         return -1;
85         }
86         *id = res;
87         return 0;
88 }
89
90 int get_rt_realms(__u32 *realms, char *arg)
91 {
92         __u32 realm = 0;
93         char *p = strchr(arg, '/');
94
95         *realms = 0;
96         if (p) {
97                 *p = 0;
98                 if (rtnl_rtrealm_a2n(realms, arg)) {
99                         *p = '/';
100                         return -1;
101                 }
102                 *realms <<= 16;
103                 *p = '/';
104                 arg = p+1;
105         }
106         if (*arg && rtnl_rtrealm_a2n(&realm, arg))
107                 return -1;
108         *realms |= realm;
109         return 0;
110 }