]> git.proxmox.com Git - mirror_iproute2.git/blob - tipc/misc.c
ll_map: Add function to remove link cache entry by index
[mirror_iproute2.git] / tipc / misc.c
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>
15 #include <string.h>
16 #include <sys/ioctl.h>
17 #include <sys/socket.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include "misc.h"
21
22 #define IN_RANGE(val, low, high) ((val) <= (high) && (val) >= (low))
23
24 uint32_t str2addr(char *str)
25 {
26 unsigned int z, c, n;
27 char dummy;
28
29 if (sscanf(str, "%u.%u.%u%c", &z, &c, &n, &dummy) != 3) {
30 fprintf(stderr, "invalid network address, syntax: Z.C.N\n");
31 return 0;
32 }
33
34 if (IN_RANGE(z, 0, 255) && IN_RANGE(c, 0, 4095) && IN_RANGE(n, 0, 4095))
35 return tipc_addr(z, c, n);
36
37 fprintf(stderr, "invalid network address \"%s\"\n", str);
38 return 0;
39 }
40
41 static int is_hex(char *arr, int last)
42 {
43 int i;
44
45 while (!arr[last])
46 last--;
47
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'))
52 return 0;
53 }
54 return 1;
55 }
56
57 static int is_name(char *arr, int last)
58 {
59 int i;
60 char c;
61
62 while (!arr[last])
63 last--;
64
65 if (last > 15)
66 return 0;
67
68 for (i = 0; i <= last; i++) {
69 c = arr[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 != '@')
73 return 0;
74 }
75 return 1;
76 }
77
78 int str2nodeid(char *str, uint8_t *id)
79 {
80 int len = strlen(str);
81 int i;
82
83 if (len > 32)
84 return -1;
85
86 if (is_name(str, len - 1)) {
87 memcpy(id, str, len);
88 return 0;
89 }
90 if (!is_hex(str, len - 1))
91 return -1;
92
93 str[len] = '0';
94 for (i = 0; i < 16; i++) {
95 if (sscanf(&str[2 * i], "%2hhx", &id[i]) != 1)
96 break;
97 }
98 return 0;
99 }
100
101 void nodeid2str(uint8_t *id, char *str)
102 {
103 int i;
104
105 if (is_name((char *)id, 15)) {
106 memcpy(str, id, 16);
107 return;
108 }
109
110 for (i = 0; i < 16; i++)
111 sprintf(&str[2 * i], "%02x", id[i]);
112
113 for (i = 31; str[i] == '0'; i--)
114 str[i] = 0;
115 }
116
117 void hash2nodestr(uint32_t hash, char *str)
118 {
119 struct tipc_sioc_nodeid_req nr = {};
120 int sd;
121
122 sd = socket(AF_TIPC, SOCK_RDM, 0);
123 if (sd < 0) {
124 fprintf(stderr, "opening TIPC socket: %s\n", strerror(errno));
125 return;
126 }
127 nr.peer = hash;
128 if (!ioctl(sd, SIOCGETNODEID, &nr))
129 nodeid2str((uint8_t *)nr.node_id, str);
130 close(sd);
131 }