]>
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 void nodeid2str(uint8_t *id
, char *str
)
105 if (is_name((char *)id
, 15)) {
110 for (i
= 0; i
< 16; i
++)
111 sprintf(&str
[2 * i
], "%02x", id
[i
]);
113 for (i
= 31; str
[i
] == '0'; i
--)
117 void hash2nodestr(uint32_t hash
, char *str
)
119 struct tipc_sioc_nodeid_req nr
= {};
122 sd
= socket(AF_TIPC
, SOCK_RDM
, 0);
124 fprintf(stderr
, "opening TIPC socket: %s\n", strerror(errno
));
128 if (!ioctl(sd
, SIOCGETNODEID
, &nr
))
129 nodeid2str((uint8_t *)nr
.node_id
, str
);