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