]>
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>
18 #define IN_RANGE(val, low, high) ((val) <= (high) && (val) >= (low))
20 uint32_t str2addr(char *str
)
25 if (sscanf(str
, "%u.%u.%u%c", &z
, &c
, &n
, &dummy
) != 3) {
26 fprintf(stderr
, "invalid network address, syntax: Z.C.N\n");
30 if (IN_RANGE(z
, 0, 255) && IN_RANGE(c
, 0, 4095) && IN_RANGE(n
, 0, 4095))
31 return tipc_addr(z
, c
, n
);
33 fprintf(stderr
, "invalid network address \"%s\"\n", str
);
37 static int is_hex(char *arr
, int last
)
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'))
53 static int is_name(char *arr
, int last
)
64 for (i
= 0; i
<= last
; 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
!= '@')
74 int str2nodeid(char *str
, uint8_t *id
)
76 int len
= strlen(str
);
82 if (is_name(str
, len
- 1)) {
86 if (!is_hex(str
, len
- 1))
90 for (i
= 0; i
< 16; i
++) {
91 if (sscanf(&str
[2 * i
], "%2hhx", &id
[i
]) != 1)
97 void nodeid2str(uint8_t *id
, char *str
)
101 if (is_name((char *)id
, 15)) {
106 for (i
= 0; i
< 16; i
++)
107 sprintf(&str
[2 * i
], "%02x", id
[i
]);
109 for (i
= 31; str
[i
] == '0'; i
--)