Debian Codegraph @

tipc/misc.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * misc.c Miscellaneous TIPC helper functions.
4 *
5 * Authors: Richard Alpe <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <linux/tipc.h>
11 #include <string.h>
12 #include <sys/ioctl.h>
13 #include <sys/socket.h>
14 #include <unistd.h>
15 #include <errno.h>
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 }
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 int str2key(char *str, struct tipc_aead_key *key)
98 {
99 int len = strlen(str);
100 int ishex = 0;
101 int i;
102
103 /* Check if the input is a hex string (i.e. 0x...) */
104 if (len > 2 && strncmp(str, "0x", 2) == 0) {
105 ishex = is_hex(str + 2, len - 2 - 1);
106 if (ishex) {
107 len -= 2;
108 str += 2;
109 }
110 }
111
112 key->keylen = ishex ? (len + 1) / 2 : len;
113 if (key->keylen > TIPC_AEAD_KEYLEN_MAX)
114 return -1;
115
116 /* Obtain key: */
117 if (!ishex) {
118 memcpy(key->key, str, len);
119 } else {
120 /* Convert hex string to key */
121 for (i = 0; i < key->keylen; i++) {
122 if (i == 0 && len % 2 != 0) {
123 if (sscanf(str, "%1hhx", &key->key[0]) != 1)
124 return -1;
125 str += 1;
126 continue;
127 }
128 if (sscanf(str, "%2hhx", &key->key[i]) != 1)
129 return -1;
130 str += 2;
131 }
132 }
133
134 return 0;
135 }
136
137 void nodeid2str(uint8_t *id, char *str)
138 {
139 int i;
140
141 if (is_name((char *)id, 15)) {
142 memcpy(str, id, 16);
143 return;
144 }
145
146 for (i = 0; i < 16; i++)
147 sprintf(&str[2 * i], "%02x", id[i]);
148
149 for (i = 31; str[i] == '0'; i--)
150 str[i] = 0;
151 }
152
153 void hash2nodestr(uint32_t hash, char *str)
154 {
155 struct tipc_sioc_nodeid_req nr = {};
156 int sd;
157
158 sd = socket(AF_TIPC, SOCK_RDM, 0);
159 if (sd < 0) {
160 fprintf(stderr, "opening TIPC socket: %s\n", strerror(errno));
161 return;
162 }
163 nr.peer = hash;
164 if (!ioctl(sd, SIOCGETNODEID, &nr))
165 nodeid2str((uint8_t *)nr.node_id, str);
166 close(sd);
167 }