1
2
3
4
5
6
7
8
9
10
11#include "libbb.h"
12#include "utils.h"
13#include "inet_common.h"
14
15unsigned FAST_FUNC get_hz(void)
16{
17 static unsigned hz_internal;
18 FILE *fp;
19
20 if (hz_internal)
21 return hz_internal;
22
23 fp = fopen_for_read("/proc/net/psched");
24 if (fp) {
25 unsigned nom, denom;
26
27 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
28 if (nom == 1000000)
29 hz_internal = denom;
30 fclose(fp);
31 }
32 if (!hz_internal)
33 hz_internal = bb_clk_tck();
34 return hz_internal;
35}
36
37unsigned FAST_FUNC get_unsigned(char *arg, const char *errmsg)
38{
39 unsigned long res;
40 char *ptr;
41
42 if (*arg) {
43 res = strtoul(arg, &ptr, 0);
44
45 if (!*ptr && res <= UINT_MAX) {
46 return res;
47 }
48 }
49 invarg_1_to_2(arg, errmsg);
50}
51
52uint32_t FAST_FUNC get_u32(char *arg, const char *errmsg)
53{
54 unsigned long res;
55 char *ptr;
56
57 if (*arg) {
58 res = strtoul(arg, &ptr, 0);
59
60 if (!*ptr && res <= 0xFFFFFFFFUL) {
61 return res;
62 }
63 }
64 invarg_1_to_2(arg, errmsg);
65}
66
67uint16_t FAST_FUNC get_u16(char *arg, const char *errmsg)
68{
69 unsigned long res;
70 char *ptr;
71
72 if (*arg) {
73 res = strtoul(arg, &ptr, 0);
74
75 if (!*ptr && res <= 0xFFFF) {
76 return res;
77 }
78 }
79 invarg_1_to_2(arg, errmsg);
80}
81
82int FAST_FUNC get_addr_1(inet_prefix *addr, char *name, int family)
83{
84 memset(addr, 0, sizeof(*addr));
85
86 if (strcmp(name, "default") == 0
87 || strcmp(name, "all") == 0
88 || strcmp(name, "any") == 0
89 ) {
90 addr->family = family;
91 addr->bytelen = (family == AF_INET6 ? 16 : 4);
92 addr->bitlen = -1;
93 return 0;
94 }
95
96 if (strchr(name, ':')) {
97 addr->family = AF_INET6;
98 if (family != AF_UNSPEC && family != AF_INET6)
99 return -1;
100 if (inet_pton(AF_INET6, name, addr->data) <= 0)
101 return -1;
102 addr->bytelen = 16;
103 addr->bitlen = -1;
104 return 0;
105 }
106
107 if (family != AF_UNSPEC && family != AF_INET)
108 return -1;
109
110
111 addr->family = AF_INET;
112#if 0
113 if (inet_pton(AF_INET, name, addr->data) <= 0)
114 return -1;
115#else
116 {
117 unsigned i = 0;
118 unsigned n = 0;
119 const char *cp = name - 1;
120 while (*++cp) {
121 if ((unsigned char)(*cp - '0') <= 9) {
122 n = 10 * n + (unsigned char)(*cp - '0');
123 if (n >= 256)
124 return -1;
125 ((uint8_t*)addr->data)[i] = n;
126 continue;
127 }
128 if (*cp == '.' && ++i <= 3) {
129 n = 0;
130 continue;
131 }
132 return -1;
133 }
134 }
135#endif
136 addr->bytelen = 4;
137 addr->bitlen = -1;
138
139 return 0;
140}
141
142static void get_prefix_1(inet_prefix *dst, char *arg, int family)
143{
144 char *slash;
145
146 memset(dst, 0, sizeof(*dst));
147
148 if (strcmp(arg, "default") == 0
149 || strcmp(arg, "all") == 0
150 || strcmp(arg, "any") == 0
151 ) {
152 dst->family = family;
153
154
155 return;
156 }
157
158 slash = strchr(arg, '/');
159 if (slash)
160 *slash = '\0';
161
162 if (get_addr_1(dst, arg, family) == 0) {
163 dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
164 if (slash) {
165 unsigned plen;
166 inet_prefix netmask_pfx;
167
168 netmask_pfx.family = AF_UNSPEC;
169 plen = bb_strtou(slash + 1, NULL, 0);
170 if ((errno || plen > dst->bitlen)
171 && get_addr_1(&netmask_pfx, slash + 1, family) != 0
172 ) {
173 goto bad;
174 }
175 if (netmask_pfx.family == AF_INET) {
176
177 uint32_t mask = ntohl(netmask_pfx.data[0]);
178 uint32_t inv = ~mask;
179
180
181 if (inv & (inv + 1))
182 goto bad;
183
184 plen = bb_popcnt_32(mask);
185 if (plen > dst->bitlen)
186 goto bad;
187
188 }
189 dst->bitlen = plen;
190 }
191 }
192
193 if (slash)
194 *slash = '/';
195 return;
196 bad:
197 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
198}
199
200int FAST_FUNC get_addr(inet_prefix *dst, char *arg, int family)
201{
202 if (family == AF_PACKET) {
203 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "address");
204 }
205 if (get_addr_1(dst, arg, family)) {
206 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "address", arg);
207 }
208 return 0;
209}
210
211void FAST_FUNC get_prefix(inet_prefix *dst, char *arg, int family)
212{
213 if (family == AF_PACKET) {
214 bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
215 }
216 get_prefix_1(dst, arg, family);
217}
218
219uint32_t FAST_FUNC get_addr32(char *name)
220{
221 inet_prefix addr;
222
223 if (get_addr_1(&addr, name, AF_INET)) {
224 bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "IP", "address", name);
225 }
226 return addr.data[0];
227}
228
229char** FAST_FUNC next_arg(char **argv)
230{
231 if (!*++argv)
232 bb_simple_error_msg_and_die("command line is not complete, try \"help\"");
233 return argv;
234}
235
236void FAST_FUNC invarg_1_to_2(const char *arg, const char *opt)
237{
238 bb_error_msg_and_die(bb_msg_invalid_arg_to, arg, opt);
239}
240
241void FAST_FUNC duparg(const char *key, const char *arg)
242{
243 bb_error_msg_and_die("duplicate \"%s\": \"%s\" is the second value", key, arg);
244}
245
246void FAST_FUNC duparg2(const char *key, const char *arg)
247{
248 bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
249}
250
251int FAST_FUNC inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
252{
253 const uint32_t *a1 = a->data;
254 const uint32_t *a2 = b->data;
255 int words = bits >> 5;
256
257 bits &= 0x1f;
258
259 if (words)
260 if (memcmp(a1, a2, words << 2))
261 return -1;
262
263 if (bits) {
264 uint32_t w1, w2;
265 uint32_t mask;
266
267 w1 = a1[words];
268 w2 = a2[words];
269
270 mask = htonl((0xffffffff) << (0x20 - bits));
271
272 if ((w1 ^ w2) & mask)
273 return 1;
274 }
275
276 return 0;
277}
278
279const char* FAST_FUNC rt_addr_n2a(int af, void *addr)
280{
281 switch (af) {
282 case AF_INET:
283 case AF_INET6:
284 return inet_ntop(af, addr,
285 auto_string(xzalloc(INET6_ADDRSTRLEN)), INET6_ADDRSTRLEN
286 );
287 default:
288 return "???";
289 }
290}
291
292#ifdef RESOLVE_HOSTNAMES
293const char* FAST_FUNC format_host(int af, int len, void *addr)
294{
295 if (resolve_hosts) {
296 struct hostent *h_ent;
297
298 if (len <= 0) {
299 switch (af) {
300 case AF_INET:
301 len = 4;
302 break;
303 case AF_INET6:
304 len = 16;
305 break;
306 default:;
307 }
308 }
309 if (len > 0) {
310 h_ent = gethostbyaddr(addr, len, af);
311 if (h_ent != NULL) {
312 return auto_string(xstrdup(h_ent->h_name));
313 }
314 }
315 }
316 return rt_addr_n2a(af, addr);
317}
318#endif
319