]>
git.proxmox.com Git - mirror_iproute2.git/blob - tipc/misc.c
2 * misc.c Miscellaneous TIPC helper functions.
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.
9 * Authors: Richard Alpe <richard.alpe@ericsson.com>
14 #include <linux/tipc.h>
16 #include <sys/ioctl.h>
17 #include <sys/socket.h>
22 #define IN_RANGE(val, low, high) ((val) <= (high) && (val) >= (low))
24 uint32_t str2addr(char *str
)
29 if (sscanf(str
, "%u.%u.%u%c", &z
, &c
, &n
, &dummy
) != 3) {
30 fprintf(stderr
, "invalid network address, syntax: Z.C.N\n");
34 if (IN_RANGE(z
, 0, 255) && IN_RANGE(c
, 0, 4095) && IN_RANGE(n
, 0, 4095))
35 return tipc_addr(z
, c
, n
);
37 fprintf(stderr
, "invalid network address \"%s\"\n", str
);
41 static int is_hex(char *arr
, int last
)
48 for (i
= 0; i
<= last
; i
++) {
49 if (!IN_RANGE(arr
[i
], '0', '9') &&
50 !IN_RANGE(arr
[i
], 'a', 'f') &&
51 !IN_RANGE(arr
[i
], 'A', 'F'))
57 static int is_name(char *arr
, int last
)
68 for (i
= 0; i
<= last
; i
++) {
70 if (!IN_RANGE(c
, '0', '9') && !IN_RANGE(c
, 'a', 'z') &&
71 !IN_RANGE(c
, 'A', 'Z') && c
!= '-' && c
!= '_' &&
72 c
!= '.' && c
!= ':' && c
!= '@')
78 int str2nodeid(char *str
, uint8_t *id
)
80 int len
= strlen(str
);
86 if (is_name(str
, len
- 1)) {
90 if (!is_hex(str
, len
- 1))
94 for (i
= 0; i
< 16; i
++) {
95 if (sscanf(&str
[2 * i
], "%2hhx", &id
[i
]) != 1)
101 int str2key(char *str
, struct tipc_aead_key
*key
)
103 int len
= strlen(str
);
107 /* Check if the input is a hex string (i.e. 0x...) */
108 if (len
> 2 && strncmp(str
, "0x", 2) == 0) {
109 ishex
= is_hex(str
+ 2, len
- 2 - 1);
119 memcpy(key
->key
, str
, len
);
121 /* Convert hex string to key */
122 key
->keylen
= (len
+ 1) / 2;
123 for (i
= 0; i
< key
->keylen
; i
++) {
124 if (i
== 0 && len
% 2 != 0) {
125 if (sscanf(str
, "%1hhx", &key
->key
[0]) != 1)
130 if (sscanf(str
, "%2hhx", &key
->key
[i
]) != 1)
139 void nodeid2str(uint8_t *id
, char *str
)
143 if (is_name((char *)id
, 15)) {
148 for (i
= 0; i
< 16; i
++)
149 sprintf(&str
[2 * i
], "%02x", id
[i
]);
151 for (i
= 31; str
[i
] == '0'; i
--)
155 void hash2nodestr(uint32_t hash
, char *str
)
157 struct tipc_sioc_nodeid_req nr
= {};
160 sd
= socket(AF_TIPC
, SOCK_RDM
, 0);
162 fprintf(stderr
, "opening TIPC socket: %s\n", strerror(errno
));
166 if (!ioctl(sd
, SIOCGETNODEID
, &nr
))
167 nodeid2str((uint8_t *)nr
.node_id
, str
);