preparatory patch for -Wwrite-strings #2
[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 <stdlib.h>
15 #include <string.h>
16
17 #include "rt_names.h"
18 #include "utils.h"
19
20 char *rtnl_rtntype_n2a(int id, char *buf, int len)
21 {
22         switch (id) {
23         case RTN_UNSPEC:
24                 return "none";
25         case RTN_UNICAST:
26                 return "unicast";
27         case RTN_LOCAL:
28                 return "local";
29         case RTN_BROADCAST:
30                 return "broadcast";
31         case RTN_ANYCAST:
32                 return "anycast";
33         case RTN_MULTICAST:
34                 return "multicast";
35         case RTN_BLACKHOLE:
36                 return "blackhole";
37         case RTN_UNREACHABLE:
38                 return "unreachable";
39         case RTN_PROHIBIT:
40                 return "prohibit";
41         case RTN_THROW:
42                 return "throw";
43         case RTN_NAT:
44                 return "nat";
45         case RTN_XRESOLVE:
46                 return "xresolve";
47         default:
48                 snprintf(buf, len, "%d", id);
49                 return buf;
50         }
51 }
52
53
54 int rtnl_rtntype_a2n(int *id, char *arg)
55 {
56         char *end;
57         unsigned long res;
58
59         if (strcmp(arg, "local") == 0)
60                 res = RTN_LOCAL;
61         else if (strcmp(arg, "nat") == 0)
62                 res = RTN_NAT;
63         else if (matches(arg, "broadcast") == 0 ||
64                  strcmp(arg, "brd") == 0)
65                 res = RTN_BROADCAST;
66         else if (matches(arg, "anycast") == 0)
67                 res = RTN_ANYCAST;
68         else if (matches(arg, "multicast") == 0)
69                 res = RTN_MULTICAST;
70         else if (matches(arg, "prohibit") == 0)
71                 res = RTN_PROHIBIT;
72         else if (matches(arg, "unreachable") == 0)
73                 res = RTN_UNREACHABLE;
74         else if (matches(arg, "blackhole") == 0)
75                 res = RTN_BLACKHOLE;
76         else if (matches(arg, "xresolve") == 0)
77                 res = RTN_XRESOLVE;
78         else if (matches(arg, "unicast") == 0)
79                 res = RTN_UNICAST;
80         else if (strcmp(arg, "throw") == 0)
81                 res = RTN_THROW;
82         else {
83                 res = strtoul(arg, &end, 0);
84                 if (!end || end == arg || *end || res > 255)
85                         return -1;
86         }
87         *id = res;
88         return 0;
89 }
90
91 int get_rt_realms(uint32_t *realms, char *arg)
92 {
93         uint32_t realm = 0;
94         char *p = strchr(arg, '/');
95
96         *realms = 0;
97         if (p) {
98                 *p = 0;
99                 if (rtnl_rtrealm_a2n(realms, arg)) {
100                         *p = '/';
101                         return -1;
102                 }
103                 *realms <<= 16;
104                 *p = '/';
105                 arg = p+1;
106         }
107         if (*arg && rtnl_rtrealm_a2n(&realm, arg))
108                 return -1;
109         *realms |= realm;
110         return 0;
111 }