]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * utils.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 | * | |
aba5acdf SH |
11 | */ |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
16 | #include <syslog.h> | |
17 | #include <fcntl.h> | |
18 | #include <sys/socket.h> | |
19 | #include <netinet/in.h> | |
20 | #include <string.h> | |
21 | #include <netdb.h> | |
22 | #include <arpa/inet.h> | |
23 | #include <resolv.h> | |
5dfe5561 | 24 | #include <asm/types.h> |
aba5acdf | 25 | #include <linux/pkt_sched.h> |
90f93024 SH |
26 | #include <time.h> |
27 | #include <sys/time.h> | |
28 | ||
aba5acdf SH |
29 | |
30 | #include "utils.h" | |
31 | ||
4094db72 | 32 | int get_integer(int *val, const char *arg, int base) |
aba5acdf SH |
33 | { |
34 | long res; | |
35 | char *ptr; | |
36 | ||
37 | if (!arg || !*arg) | |
38 | return -1; | |
39 | res = strtol(arg, &ptr, base); | |
40 | if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN) | |
41 | return -1; | |
42 | *val = res; | |
43 | return 0; | |
44 | } | |
45 | ||
6b1ac654 | 46 | int mask2bits(__u32 netmask) |
4b270b17 SH |
47 | { |
48 | unsigned bits = 0; | |
6b1ac654 SH |
49 | __u32 mask = ntohl(netmask); |
50 | __u32 host = ~mask; | |
4b270b17 | 51 | |
6b1ac654 SH |
52 | /* a valid netmask must be 2^n - 1 */ |
53 | if ((host & (host + 1)) != 0) | |
54 | return -1; | |
4b270b17 | 55 | |
6b1ac654 SH |
56 | for (; mask; mask <<= 1) |
57 | ++bits; | |
4b270b17 SH |
58 | return bits; |
59 | } | |
60 | ||
5a0d1cbf AH |
61 | static int get_netmask(unsigned *val, const char *arg, int base) |
62 | { | |
63 | inet_prefix addr; | |
64 | ||
65 | if (!get_unsigned(val, arg, base)) | |
66 | return 0; | |
67 | ||
68 | /* try coverting dotted quad to CIDR */ | |
6b1ac654 SH |
69 | if (!get_addr_1(&addr, arg, AF_INET) && addr.family == AF_INET) { |
70 | int b = mask2bits(addr.data[0]); | |
71 | ||
72 | if (b >= 0) { | |
73 | *val = b; | |
5a0d1cbf | 74 | return 0; |
6b1ac654 | 75 | } |
5a0d1cbf AH |
76 | } |
77 | ||
78 | return -1; | |
79 | } | |
80 | ||
4094db72 | 81 | int get_unsigned(unsigned *val, const char *arg, int base) |
aba5acdf SH |
82 | { |
83 | unsigned long res; | |
84 | char *ptr; | |
85 | ||
86 | if (!arg || !*arg) | |
87 | return -1; | |
88 | res = strtoul(arg, &ptr, base); | |
89 | if (!ptr || ptr == arg || *ptr || res > UINT_MAX) | |
90 | return -1; | |
91 | *val = res; | |
92 | return 0; | |
93 | } | |
94 | ||
54e0b2e7 | 95 | /* |
db6b0cfa GR |
96 | * get_time_rtt is "translated" from a similar routine "get_time" in |
97 | * tc_util.c. We don't use the exact same routine because tc passes | |
98 | * microseconds to the kernel and the callers of get_time_rtt want to | |
99 | * pass milliseconds (standard unit for rtt values since 2.6.27), and | |
100 | * have a different assumption for the units of a "raw" number. | |
54e0b2e7 | 101 | */ |
db6b0cfa | 102 | int get_time_rtt(unsigned *val, const char *arg, int *raw) |
54e0b2e7 RJ |
103 | { |
104 | double t; | |
105 | unsigned long res; | |
106 | char *p; | |
107 | ||
108 | if (strchr(arg,'.') != NULL) { | |
109 | t = strtod(arg,&p); | |
110 | if (t < 0.0) | |
111 | return -1; | |
112 | } | |
113 | else { | |
897fb84f | 114 | res = strtoul(arg, &p, 0); |
54e0b2e7 RJ |
115 | if (res > UINT_MAX) |
116 | return -1; | |
117 | t = (double)res; | |
118 | } | |
119 | if (p == arg) | |
120 | return -1; | |
54e0b2e7 RJ |
121 | *raw = 1; |
122 | ||
123 | if (*p) { | |
124 | *raw = 0; | |
125 | if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 || | |
126 | strcasecmp(p, "secs")==0) | |
fca1dae8 | 127 | t *= 1000; |
54e0b2e7 RJ |
128 | else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 || |
129 | strcasecmp(p, "msecs") == 0) | |
54e0b2e7 RJ |
130 | t *= 1.0; /* allow suffix, do nothing */ |
131 | else | |
132 | return -1; | |
133 | } | |
134 | ||
135 | /* emulate ceil() without having to bring-in -lm and always be >= 1 */ | |
136 | ||
db6b0cfa GR |
137 | *val = t; |
138 | if (*val < t) | |
139 | *val += 1; | |
54e0b2e7 RJ |
140 | |
141 | return 0; | |
142 | ||
143 | } | |
144 | ||
c7699875 | 145 | int get_u64(__u64 *val, const char *arg, int base) |
146 | { | |
147 | unsigned long long res; | |
148 | char *ptr; | |
149 | ||
150 | if (!arg || !*arg) | |
151 | return -1; | |
152 | res = strtoull(arg, &ptr, base); | |
153 | if (!ptr || ptr == arg || *ptr || res == 0xFFFFFFFFULL) | |
154 | return -1; | |
155 | *val = res; | |
156 | return 0; | |
157 | } | |
158 | ||
4094db72 | 159 | int get_u32(__u32 *val, const char *arg, int base) |
aba5acdf SH |
160 | { |
161 | unsigned long res; | |
162 | char *ptr; | |
163 | ||
164 | if (!arg || !*arg) | |
165 | return -1; | |
166 | res = strtoul(arg, &ptr, base); | |
167 | if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL) | |
168 | return -1; | |
169 | *val = res; | |
170 | return 0; | |
171 | } | |
172 | ||
4094db72 | 173 | int get_u16(__u16 *val, const char *arg, int base) |
aba5acdf SH |
174 | { |
175 | unsigned long res; | |
176 | char *ptr; | |
177 | ||
178 | if (!arg || !*arg) | |
179 | return -1; | |
180 | res = strtoul(arg, &ptr, base); | |
181 | if (!ptr || ptr == arg || *ptr || res > 0xFFFF) | |
182 | return -1; | |
183 | *val = res; | |
184 | return 0; | |
185 | } | |
186 | ||
4094db72 | 187 | int get_u8(__u8 *val, const char *arg, int base) |
aba5acdf SH |
188 | { |
189 | unsigned long res; | |
190 | char *ptr; | |
191 | ||
192 | if (!arg || !*arg) | |
193 | return -1; | |
194 | res = strtoul(arg, &ptr, base); | |
195 | if (!ptr || ptr == arg || *ptr || res > 0xFF) | |
196 | return -1; | |
197 | *val = res; | |
198 | return 0; | |
199 | } | |
200 | ||
4094db72 | 201 | int get_s16(__s16 *val, const char *arg, int base) |
aba5acdf SH |
202 | { |
203 | long res; | |
204 | char *ptr; | |
205 | ||
206 | if (!arg || !*arg) | |
207 | return -1; | |
208 | res = strtol(arg, &ptr, base); | |
209 | if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000) | |
210 | return -1; | |
211 | *val = res; | |
212 | return 0; | |
213 | } | |
214 | ||
4094db72 | 215 | int get_s8(__s8 *val, const char *arg, int base) |
aba5acdf SH |
216 | { |
217 | long res; | |
218 | char *ptr; | |
219 | ||
220 | if (!arg || !*arg) | |
221 | return -1; | |
222 | res = strtol(arg, &ptr, base); | |
223 | if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80) | |
224 | return -1; | |
225 | *val = res; | |
226 | return 0; | |
227 | } | |
228 | ||
cafa6c8e SH |
229 | /* This uses a non-standard parsing (ie not inet_aton, or inet_pton) |
230 | * because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8 | |
231 | */ | |
232 | static int get_addr_ipv4(__u8 *ap, const char *cp) | |
aba5acdf | 233 | { |
cafa6c8e SH |
234 | int i; |
235 | ||
236 | for (i = 0; i < 4; i++) { | |
237 | unsigned long n; | |
238 | char *endp; | |
239 | ||
240 | n = strtoul(cp, &endp, 0); | |
241 | if (n > 255) | |
242 | return -1; /* bogus network value */ | |
243 | ||
244 | if (endp == cp) /* no digits */ | |
245 | return -1; | |
94afda75 | 246 | |
cafa6c8e SH |
247 | ap[i] = n; |
248 | ||
249 | if (*endp == '\0') | |
250 | break; | |
251 | ||
252 | if (i == 3 || *endp != '.') | |
253 | return -1; /* extra characters */ | |
254 | cp = endp + 1; | |
255 | } | |
256 | ||
257 | return 1; | |
258 | } | |
259 | ||
260 | int get_addr_1(inet_prefix *addr, const char *name, int family) | |
261 | { | |
aba5acdf SH |
262 | memset(addr, 0, sizeof(*addr)); |
263 | ||
264 | if (strcmp(name, "default") == 0 || | |
265 | strcmp(name, "all") == 0 || | |
266 | strcmp(name, "any") == 0) { | |
267 | if (family == AF_DECnet) | |
268 | return -1; | |
269 | addr->family = family; | |
270 | addr->bytelen = (family == AF_INET6 ? 16 : 4); | |
271 | addr->bitlen = -1; | |
272 | return 0; | |
273 | } | |
274 | ||
275 | if (strchr(name, ':')) { | |
276 | addr->family = AF_INET6; | |
277 | if (family != AF_UNSPEC && family != AF_INET6) | |
278 | return -1; | |
279 | if (inet_pton(AF_INET6, name, addr->data) <= 0) | |
280 | return -1; | |
281 | addr->bytelen = 16; | |
282 | addr->bitlen = -1; | |
283 | return 0; | |
284 | } | |
285 | ||
286 | if (family == AF_DECnet) { | |
287 | struct dn_naddr dna; | |
288 | addr->family = AF_DECnet; | |
289 | if (dnet_pton(AF_DECnet, name, &dna) <= 0) | |
290 | return -1; | |
291 | memcpy(addr->data, dna.a_addr, 2); | |
292 | addr->bytelen = 2; | |
293 | addr->bitlen = -1; | |
294 | return 0; | |
295 | } | |
296 | ||
297 | addr->family = AF_INET; | |
298 | if (family != AF_UNSPEC && family != AF_INET) | |
299 | return -1; | |
94afda75 | 300 | |
cafa6c8e | 301 | if (get_addr_ipv4((__u8 *)addr->data, name) <= 0) |
516ffb6b | 302 | return -1; |
94afda75 | 303 | |
aba5acdf SH |
304 | addr->bytelen = 4; |
305 | addr->bitlen = -1; | |
aba5acdf SH |
306 | return 0; |
307 | } | |
308 | ||
fb9b1d0f | 309 | int get_prefix_1(inet_prefix *dst, char *arg, int family) |
aba5acdf SH |
310 | { |
311 | int err; | |
312 | unsigned plen; | |
313 | char *slash; | |
314 | ||
315 | memset(dst, 0, sizeof(*dst)); | |
316 | ||
317 | if (strcmp(arg, "default") == 0 || | |
318 | strcmp(arg, "any") == 0 || | |
319 | strcmp(arg, "all") == 0) { | |
320 | if (family == AF_DECnet) | |
321 | return -1; | |
322 | dst->family = family; | |
323 | dst->bytelen = 0; | |
324 | dst->bitlen = 0; | |
325 | return 0; | |
326 | } | |
327 | ||
328 | slash = strchr(arg, '/'); | |
329 | if (slash) | |
330 | *slash = 0; | |
fb9b1d0f | 331 | |
aba5acdf SH |
332 | err = get_addr_1(dst, arg, family); |
333 | if (err == 0) { | |
334 | switch(dst->family) { | |
335 | case AF_INET6: | |
336 | dst->bitlen = 128; | |
337 | break; | |
338 | case AF_DECnet: | |
339 | dst->bitlen = 16; | |
340 | break; | |
341 | default: | |
342 | case AF_INET: | |
343 | dst->bitlen = 32; | |
344 | } | |
345 | if (slash) { | |
5a0d1cbf AH |
346 | if (get_netmask(&plen, slash+1, 0) |
347 | || plen > dst->bitlen) { | |
aba5acdf SH |
348 | err = -1; |
349 | goto done; | |
350 | } | |
f082b64f | 351 | dst->flags |= PREFIXLEN_SPECIFIED; |
aba5acdf SH |
352 | dst->bitlen = plen; |
353 | } | |
354 | } | |
355 | done: | |
356 | if (slash) | |
357 | *slash = '/'; | |
358 | return err; | |
359 | } | |
360 | ||
c7699875 | 361 | int get_addr(inet_prefix *dst, const char *arg, int family) |
aba5acdf SH |
362 | { |
363 | if (family == AF_PACKET) { | |
364 | fprintf(stderr, "Error: \"%s\" may be inet address, but it is not allowed in this context.\n", arg); | |
365 | exit(1); | |
366 | } | |
367 | if (get_addr_1(dst, arg, family)) { | |
368 | fprintf(stderr, "Error: an inet address is expected rather than \"%s\".\n", arg); | |
369 | exit(1); | |
370 | } | |
371 | return 0; | |
372 | } | |
373 | ||
fb9b1d0f | 374 | int get_prefix(inet_prefix *dst, char *arg, int family) |
aba5acdf SH |
375 | { |
376 | if (family == AF_PACKET) { | |
377 | fprintf(stderr, "Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n", arg); | |
378 | exit(1); | |
379 | } | |
380 | if (get_prefix_1(dst, arg, family)) { | |
381 | fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", arg); | |
382 | exit(1); | |
383 | } | |
384 | return 0; | |
385 | } | |
386 | ||
c7699875 | 387 | __u32 get_addr32(const char *name) |
aba5acdf SH |
388 | { |
389 | inet_prefix addr; | |
390 | if (get_addr_1(&addr, name, AF_INET)) { | |
391 | fprintf(stderr, "Error: an IP address is expected rather than \"%s\"\n", name); | |
392 | exit(1); | |
393 | } | |
394 | return addr.data[0]; | |
395 | } | |
396 | ||
4094db72 | 397 | void incomplete_command(void) |
aba5acdf SH |
398 | { |
399 | fprintf(stderr, "Command line is not complete. Try option \"help\"\n"); | |
400 | exit(-1); | |
401 | } | |
402 | ||
c7699875 | 403 | void missarg(const char *key) |
404 | { | |
405 | fprintf(stderr, "Error: argument \"%s\" is required\n", key); | |
406 | exit(-1); | |
407 | } | |
408 | ||
4094db72 | 409 | void invarg(const char *msg, const char *arg) |
aba5acdf SH |
410 | { |
411 | fprintf(stderr, "Error: argument \"%s\" is wrong: %s\n", arg, msg); | |
412 | exit(-1); | |
413 | } | |
414 | ||
4094db72 | 415 | void duparg(const char *key, const char *arg) |
aba5acdf SH |
416 | { |
417 | fprintf(stderr, "Error: duplicate \"%s\": \"%s\" is the second value.\n", key, arg); | |
418 | exit(-1); | |
419 | } | |
420 | ||
4094db72 | 421 | void duparg2(const char *key, const char *arg) |
aba5acdf SH |
422 | { |
423 | fprintf(stderr, "Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n", key, arg); | |
424 | exit(-1); | |
425 | } | |
426 | ||
4094db72 | 427 | int matches(const char *cmd, const char *pattern) |
aba5acdf SH |
428 | { |
429 | int len = strlen(cmd); | |
430 | if (len > strlen(pattern)) | |
431 | return -1; | |
432 | return memcmp(pattern, cmd, len); | |
433 | } | |
434 | ||
4094db72 | 435 | int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits) |
aba5acdf | 436 | { |
9626dfd5 SH |
437 | const __u32 *a1 = a->data; |
438 | const __u32 *a2 = b->data; | |
aba5acdf SH |
439 | int words = bits >> 0x05; |
440 | ||
441 | bits &= 0x1f; | |
442 | ||
443 | if (words) | |
444 | if (memcmp(a1, a2, words << 2)) | |
445 | return -1; | |
446 | ||
447 | if (bits) { | |
448 | __u32 w1, w2; | |
449 | __u32 mask; | |
450 | ||
451 | w1 = a1[words]; | |
452 | w2 = a2[words]; | |
453 | ||
454 | mask = htonl((0xffffffff) << (0x20 - bits)); | |
455 | ||
456 | if ((w1 ^ w2) & mask) | |
457 | return 1; | |
458 | } | |
459 | ||
460 | return 0; | |
461 | } | |
462 | ||
463 | int __iproute2_hz_internal; | |
464 | ||
465 | int __get_hz(void) | |
466 | { | |
467 | char name[1024]; | |
468 | int hz = 0; | |
469 | FILE *fp; | |
470 | ||
471 | if (getenv("HZ")) | |
472 | return atoi(getenv("HZ")) ? : HZ; | |
473 | ||
474 | if (getenv("PROC_NET_PSCHED")) { | |
475 | snprintf(name, sizeof(name)-1, "%s", getenv("PROC_NET_PSCHED")); | |
c7699875 | 476 | } else if (getenv("PROC_ROOT")) { |
aba5acdf SH |
477 | snprintf(name, sizeof(name)-1, "%s/net/psched", getenv("PROC_ROOT")); |
478 | } else { | |
479 | strcpy(name, "/proc/net/psched"); | |
480 | } | |
481 | fp = fopen(name, "r"); | |
482 | ||
483 | if (fp) { | |
484 | unsigned nom, denom; | |
485 | if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) | |
486 | if (nom == 1000000) | |
487 | hz = denom; | |
488 | fclose(fp); | |
489 | } | |
490 | if (hz) | |
491 | return hz; | |
492 | return HZ; | |
493 | } | |
494 | ||
5e8bc631 | 495 | int __iproute2_user_hz_internal; |
496 | ||
497 | int __get_user_hz(void) | |
498 | { | |
499 | return sysconf(_SC_CLK_TCK); | |
500 | } | |
501 | ||
4094db72 | 502 | const char *rt_addr_n2a(int af, int len, const void *addr, char *buf, int buflen) |
aba5acdf SH |
503 | { |
504 | switch (af) { | |
505 | case AF_INET: | |
506 | case AF_INET6: | |
507 | return inet_ntop(af, addr, buf, buflen); | |
508 | case AF_IPX: | |
509 | return ipx_ntop(af, addr, buf, buflen); | |
510 | case AF_DECnet: | |
511 | { | |
512 | struct dn_naddr dna = { 2, { 0, 0, }}; | |
513 | memcpy(dna.a_addr, addr, 2); | |
514 | return dnet_ntop(af, &dna, buf, buflen); | |
515 | } | |
516 | default: | |
517 | return "???"; | |
518 | } | |
519 | } | |
520 | ||
521 | #ifdef RESOLVE_HOSTNAMES | |
522 | struct namerec | |
523 | { | |
524 | struct namerec *next; | |
7dec1bf8 | 525 | const char *name; |
aba5acdf | 526 | inet_prefix addr; |
aba5acdf SH |
527 | }; |
528 | ||
7dec1bf8 SH |
529 | #define NHASH 257 |
530 | static struct namerec *nht[NHASH]; | |
aba5acdf | 531 | |
7dec1bf8 | 532 | static const char *resolve_address(const void *addr, int len, int af) |
aba5acdf SH |
533 | { |
534 | struct namerec *n; | |
535 | struct hostent *h_ent; | |
536 | unsigned hash; | |
537 | static int notfirst; | |
538 | ||
539 | ||
540 | if (af == AF_INET6 && ((__u32*)addr)[0] == 0 && | |
541 | ((__u32*)addr)[1] == 0 && ((__u32*)addr)[2] == htonl(0xffff)) { | |
542 | af = AF_INET; | |
543 | addr += 12; | |
544 | len = 4; | |
545 | } | |
546 | ||
7dec1bf8 | 547 | hash = *(__u32 *)(addr + len - 4) % NHASH; |
aba5acdf SH |
548 | |
549 | for (n = nht[hash]; n; n = n->next) { | |
550 | if (n->addr.family == af && | |
551 | n->addr.bytelen == len && | |
552 | memcmp(n->addr.data, addr, len) == 0) | |
553 | return n->name; | |
554 | } | |
555 | if ((n = malloc(sizeof(*n))) == NULL) | |
556 | return NULL; | |
557 | n->addr.family = af; | |
558 | n->addr.bytelen = len; | |
559 | n->name = NULL; | |
560 | memcpy(n->addr.data, addr, len); | |
561 | n->next = nht[hash]; | |
562 | nht[hash] = n; | |
563 | if (++notfirst == 1) | |
564 | sethostent(1); | |
565 | fflush(stdout); | |
566 | ||
c7699875 | 567 | if ((h_ent = gethostbyaddr(addr, len, af)) != NULL) |
aba5acdf SH |
568 | n->name = strdup(h_ent->h_name); |
569 | ||
570 | /* Even if we fail, "negative" entry is remembered. */ | |
571 | return n->name; | |
572 | } | |
573 | #endif | |
574 | ||
575 | ||
c7699875 | 576 | const char *format_host(int af, int len, const void *addr, |
4094db72 | 577 | char *buf, int buflen) |
aba5acdf SH |
578 | { |
579 | #ifdef RESOLVE_HOSTNAMES | |
580 | if (resolve_hosts) { | |
7dec1bf8 SH |
581 | const char *n; |
582 | ||
aba5acdf SH |
583 | if (len <= 0) { |
584 | switch (af) { | |
585 | case AF_INET: | |
586 | len = 4; | |
587 | break; | |
588 | case AF_INET6: | |
589 | len = 16; | |
590 | break; | |
591 | case AF_IPX: | |
592 | len = 10; | |
593 | break; | |
594 | #ifdef AF_DECnet | |
595 | /* I see no reasons why gethostbyname | |
596 | may not work for DECnet */ | |
597 | case AF_DECnet: | |
598 | len = 2; | |
599 | break; | |
600 | #endif | |
601 | default: ; | |
602 | } | |
603 | } | |
604 | if (len > 0 && | |
605 | (n = resolve_address(addr, len, af)) != NULL) | |
606 | return n; | |
607 | } | |
608 | #endif | |
609 | return rt_addr_n2a(af, len, addr, buf, buflen); | |
610 | } | |
611 | ||
612 | ||
f332d169 | 613 | char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen) |
aba5acdf | 614 | { |
f332d169 | 615 | char *ptr = buf; |
aba5acdf SH |
616 | int i; |
617 | ||
618 | for (i=0; i<len; i++) { | |
619 | if (blen < 3) | |
620 | break; | |
621 | sprintf(ptr, "%02x", str[i]); | |
622 | ptr += 2; | |
623 | blen -= 2; | |
624 | if (i != len-1 && blen > 1) { | |
625 | *ptr++ = ':'; | |
626 | blen--; | |
627 | } | |
628 | } | |
629 | return buf; | |
630 | } | |
631 | ||
f332d169 | 632 | __u8* hexstring_a2n(const char *str, __u8 *buf, int blen) |
aba5acdf SH |
633 | { |
634 | int cnt = 0; | |
635 | ||
636 | for (;;) { | |
637 | unsigned acc; | |
638 | char ch; | |
639 | ||
640 | acc = 0; | |
641 | ||
642 | while ((ch = *str) != ':' && ch != 0) { | |
643 | if (ch >= '0' && ch <= '9') | |
644 | ch -= '0'; | |
645 | else if (ch >= 'a' && ch <= 'f') | |
646 | ch -= 'a'-10; | |
647 | else if (ch >= 'A' && ch <= 'F') | |
648 | ch -= 'A'-10; | |
649 | else | |
650 | return NULL; | |
651 | acc = (acc<<4) + ch; | |
652 | str++; | |
653 | } | |
654 | ||
655 | if (acc > 255) | |
656 | return NULL; | |
657 | if (cnt < blen) { | |
658 | buf[cnt] = acc; | |
659 | cnt++; | |
660 | } | |
661 | if (ch == 0) | |
662 | break; | |
663 | ++str; | |
664 | } | |
665 | if (cnt < blen) | |
666 | memset(buf+cnt, 0, blen-cnt); | |
667 | return buf; | |
668 | } | |
90f93024 SH |
669 | |
670 | int print_timestamp(FILE *fp) | |
671 | { | |
672 | struct timeval tv; | |
673 | char *tstr; | |
674 | ||
675 | memset(&tv, 0, sizeof(tv)); | |
676 | gettimeofday(&tv, NULL); | |
677 | ||
678 | tstr = asctime(localtime(&tv.tv_sec)); | |
679 | tstr[strlen(tstr)-1] = 0; | |
680 | fprintf(fp, "Timestamp: %s %lu usec\n", tstr, tv.tv_usec); | |
681 | return 0; | |
682 | } | |
351efcde SH |
683 | |
684 | int cmdlineno; | |
685 | ||
686 | /* Like glibc getline but handle continuation lines and comments */ | |
d21e8835 | 687 | ssize_t getcmdline(char **linep, size_t *lenp, FILE *in) |
351efcde | 688 | { |
d21e8835 | 689 | ssize_t cc; |
351efcde | 690 | char *cp; |
ae665a52 | 691 | |
351efcde SH |
692 | if ((cc = getline(linep, lenp, in)) < 0) |
693 | return cc; /* eof or error */ | |
694 | ++cmdlineno; | |
695 | ||
696 | cp = strchr(*linep, '#'); | |
ae665a52 | 697 | if (cp) |
351efcde | 698 | *cp = '\0'; |
ae665a52 | 699 | |
351efcde SH |
700 | while ((cp = strstr(*linep, "\\\n")) != NULL) { |
701 | char *line1 = NULL; | |
702 | size_t len1 = 0; | |
703 | size_t cc1; | |
704 | ||
705 | if ((cc1 = getline(&line1, &len1, in)) < 0) { | |
706 | fprintf(stderr, "Missing continuation line\n"); | |
707 | return cc1; | |
708 | } | |
709 | ||
710 | ++cmdlineno; | |
711 | *cp = 0; | |
712 | ||
713 | cp = strchr(line1, '#'); | |
ae665a52 | 714 | if (cp) |
351efcde SH |
715 | *cp = '\0'; |
716 | ||
d21e8835 AH |
717 | *lenp = strlen(*linep) + strlen(line1) + 1; |
718 | *linep = realloc(*linep, *lenp); | |
351efcde SH |
719 | if (!*linep) { |
720 | fprintf(stderr, "Out of memory\n"); | |
d21e8835 | 721 | *lenp = 0; |
351efcde SH |
722 | return -1; |
723 | } | |
724 | cc += cc1 - 2; | |
725 | strcat(*linep, line1); | |
726 | free(line1); | |
727 | } | |
728 | return cc; | |
729 | } | |
730 | ||
731 | /* split command line into argument vector */ | |
732 | int makeargs(char *line, char *argv[], int maxargs) | |
733 | { | |
734 | static const char ws[] = " \t\r\n"; | |
735 | char *cp; | |
736 | int argc = 0; | |
737 | ||
738 | for (cp = strtok(line, ws); cp; cp = strtok(NULL, ws)) { | |
739 | if (argc >= (maxargs - 1)) { | |
740 | fprintf(stderr, "Too many arguments to command\n"); | |
741 | exit(1); | |
742 | } | |
743 | argv[argc++] = cp; | |
744 | } | |
745 | argv[argc] = NULL; | |
746 | ||
747 | return argc; | |
748 | } |