]>
Commit | Line | Data |
---|---|---|
f043759d RA |
1 | /* |
2 | * misc.c Miscellaneous TIPC helper functions. | |
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: Richard Alpe <richard.alpe@ericsson.com> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdint.h> | |
14 | #include <linux/tipc.h> | |
725ebfbf | 15 | #include <string.h> |
f043759d RA |
16 | #include "misc.h" |
17 | ||
18 | #define IN_RANGE(val, low, high) ((val) <= (high) && (val) >= (low)) | |
19 | ||
20 | uint32_t str2addr(char *str) | |
21 | { | |
22 | unsigned int z, c, n; | |
23 | char dummy; | |
24 | ||
25 | if (sscanf(str, "%u.%u.%u%c", &z, &c, &n, &dummy) != 3) { | |
26 | fprintf(stderr, "invalid network address, syntax: Z.C.N\n"); | |
27 | return 0; | |
28 | } | |
29 | ||
30 | if (IN_RANGE(z, 0, 255) && IN_RANGE(c, 0, 4095) && IN_RANGE(n, 0, 4095)) | |
31 | return tipc_addr(z, c, n); | |
32 | ||
33 | fprintf(stderr, "invalid network address \"%s\"\n", str); | |
34 | return 0; | |
35 | } | |
725ebfbf JM |
36 | |
37 | static int is_hex(char *arr, int last) | |
38 | { | |
39 | int i; | |
40 | ||
41 | while (!arr[last]) | |
42 | last--; | |
43 | ||
44 | for (i = 0; i <= last; i++) { | |
45 | if (!IN_RANGE(arr[i], '0', '9') && | |
46 | !IN_RANGE(arr[i], 'a', 'f') && | |
47 | !IN_RANGE(arr[i], 'A', 'F')) | |
48 | return 0; | |
49 | } | |
50 | return 1; | |
51 | } | |
52 | ||
53 | static int is_name(char *arr, int last) | |
54 | { | |
55 | int i; | |
56 | char c; | |
57 | ||
58 | while (!arr[last]) | |
59 | last--; | |
60 | ||
61 | if (last > 15) | |
62 | return 0; | |
63 | ||
64 | for (i = 0; i <= last; i++) { | |
65 | c = arr[i]; | |
66 | if (!IN_RANGE(c, '0', '9') && !IN_RANGE(c, 'a', 'z') && | |
67 | !IN_RANGE(c, 'A', 'Z') && c != '-' && c != '_' && | |
68 | c != '.' && c != ':' && c != '@') | |
69 | return 0; | |
70 | } | |
71 | return 1; | |
72 | } | |
73 | ||
74 | int str2nodeid(char *str, uint8_t *id) | |
75 | { | |
76 | int len = strlen(str); | |
77 | int i; | |
78 | ||
79 | if (len > 32) | |
80 | return -1; | |
81 | ||
82 | if (is_name(str, len - 1)) { | |
83 | memcpy(id, str, len); | |
84 | return 0; | |
85 | } | |
86 | if (!is_hex(str, len - 1)) | |
87 | return -1; | |
88 | ||
89 | str[len] = '0'; | |
90 | for (i = 0; i < 16; i++) { | |
91 | if (sscanf(&str[2 * i], "%2hhx", &id[i]) != 1) | |
92 | break; | |
93 | } | |
94 | return 0; | |
95 | } | |
96 | ||
97 | void nodeid2str(uint8_t *id, char *str) | |
98 | { | |
99 | int i; | |
100 | ||
101 | if (is_name((char *)id, 15)) { | |
102 | memcpy(str, id, 16); | |
103 | return; | |
104 | } | |
105 | ||
106 | for (i = 0; i < 16; i++) | |
107 | sprintf(&str[2 * i], "%02x", id[i]); | |
108 | ||
109 | for (i = 31; str[i] == '0'; i--) | |
110 | str[i] = 0; | |
111 | } |