forked from troydhanson/network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cidr-tool.c
333 lines (278 loc) · 7.82 KB
/
cidr-tool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*
* cidr-util
* a CIDR calculator
* by Troy D. Hanson
*/
#define MODE_UNKNOWN 0
#define MODE_MASK_TO_N 1
#define MODE_N_TO_MASK 2
#define MODE_IN_SAME_NET 3
#define MODE_CIDR_EXPAND 4
#define MODE_RANGE_TO_CIDRS 5
/* standard bit vector macros */
#define BIT_TEST(c,i) ((c[(i)/8] & (1 << ((i) % 8))) ? 1 : 0)
#define BIT_SET(c,i) (c[(i)/8] |= (1 << ((i) % 8)))
#define BIT_CLEAR(c,i) (c[(i)/8] &= ~(1 << ((i) % 8)))
struct {
int mode;
int verbose;
/* mode specific */
int slash_n;
uint32_t cidr;
} CF;
void usage() {
fprintf(stderr,"usage: cidr-tool <operation>\n\n");
fprintf(stderr," operations:\n\n");
fprintf(stderr," <netmask> (netmask to /N)\n");
fprintf(stderr," /N (/N to netmask)\n");
fprintf(stderr," <netmask>|/N <IP> <IP> ... (test if IPs in same network)\n");
fprintf(stderr," <CIDR/N> (print IP's in CIDR)\n");
fprintf(stderr," <start-IP> <end-IP> (make CIDR ranges from IP range)\n");
fprintf(stderr,"\n");
exit(-1);
}
/* netmask is an IP with contiguous set bits then contiguous clear bits */
int is_netmask(char *w) {
unsigned a, b, c, d, j;
int rc = -1, sc;
union {
uint32_t i;
uint8_t c[4];
} addr, tmp;
sc = sscanf(w, "%u.%u.%u.%u", &a, &b, &c, &d);
if (sc != 4) goto done;
struct in_addr ia;
if (inet_aton(w, &ia) == 0) goto done;
/* addr.i in network order. we want to convert it to little endian (which may
* or may not be the same as host byte order, so we explicitly reverse it
* instead of ntohl). we want it in little endian because our bit tests below
* treat the MSB of the first byte as preceding the LSB of the second byte.
*/
addr.i = ia.s_addr;
tmp.i = addr.i;
addr.c[0] = tmp.c[3];
addr.c[1] = tmp.c[2];
addr.c[2] = tmp.c[1];
addr.c[3] = tmp.c[0];
/* addr.i is now little endian, meaning that c[0] is the least significant
* octet. we verify that the address consists of a run of clear bits then a
* run of set bits. Either run can have zero length.
*/
int in_clear=1;
for(j=0; j < 32; j++) {
if (BIT_TEST(addr.c, j)) {
CF.slash_n++;
in_clear = 0;
} else {
if (in_clear) continue;
goto done; /* invalid; a clear bit in set region */
}
}
rc = 0;
done:
return !rc;
}
/* slash followed by one or two digits <= 32 */
int is_slash_n(char *w) {
unsigned n;
int rc = -1, sc;
sc = sscanf(w, "/%u", &n);
if (sc < 1) goto done;
if (n > 32) goto done;
CF.slash_n = n;
rc = 0;
done:
return !rc;
}
/* IP followed by a /N */
int is_cidr(char *w) {
unsigned a, b, c, d, n;
int rc = -1, sc;
sc = sscanf(w, "%u.%u.%u.%u/%u", &a, &b, &c, &d, &n);
if (sc != 5) goto done;
if ((a > 255) || (b > 255) || (c > 255) || (d > 255)) goto done;
if (n > 32) goto done;
CF.cidr = (a << 24) | (b << 16) | (c << 8) | d;
CF.slash_n = n;
rc = 0;
done:
return !rc;
}
/* four octets as dotted quad */
int is_ip(char *w) {
unsigned a, b, c, d;
int rc = -1, sc;
sc = sscanf(w, "%u.%u.%u.%u", &a, &b, &c, &d);
if (sc != 4) goto done;
if ((a > 255) || (b > 255) || (c > 255) || (d > 255)) goto done;
rc = 0;
done:
return !rc;
}
int infer_mode(int argc, char **argv) {
int rc = -1;
/* one argument? should be netmask or /N or cidr */
if (argc == 1) {
if (is_netmask(*argv)) CF.mode = MODE_MASK_TO_N;
else if (is_slash_n(*argv)) CF.mode = MODE_N_TO_MASK;
else if (is_cidr(*argv)) CF.mode = MODE_CIDR_EXPAND;
else goto done;
}
/* two arguments? should be start-ip end-ip */
if (argc == 2) {
if (is_ip(argv[0]) && is_ip(argv[1])) CF.mode = MODE_RANGE_TO_CIDRS;
else goto done;
}
/* three+ arguments? should be netmask|/N ip1 ... */
if (argc >= 3) {
if (is_netmask(*argv)) CF.mode = MODE_IN_SAME_NET;
else if (is_slash_n(*argv)) CF.mode = MODE_IN_SAME_NET;
else goto done;
}
rc = 0;
done:
return rc;
}
void print_cidr(uint32_t cidr, uint32_t n) {
uint64_t mask = ~((1UL << (32-n)) - 1);
struct in_addr ia = {.s_addr = htonl(cidr & mask)};
printf("%s/%u\n", inet_ntoa(ia), n);
}
uint32_t min_cidr(uint32_t cidr, uint32_t n) {
uint64_t mask = ~((1UL << (32-n)) - 1);
return cidr & mask;
}
uint32_t max_cidr(uint32_t cidr, uint32_t n) {
uint64_t mask = ~((1UL << (32-n)) - 1);
return (cidr & mask) | ~mask;
}
int in_cidr(uint32_t ip, uint32_t cidr, uint32_t n) {
uint64_t mask = ~((1UL << (32-n)) - 1);
return ((cidr & mask) == (ip & mask)) ? 1 : 0;
}
/* attempt to widen CIDR/n by one or more bits (shrinking /N)
* until it encompasses ip; succeed only if the new CIDR range
* is inside of bounds ip_A (min) and ip_B (max) inclusive.
*
* returns 0 on failure, or the new /N on success
*
*/
uint32_t widen_cidr(uint32_t cidr, uint32_t n, uint32_t ip,
uint32_t ip_A, uint32_t ip_B) {
int rc = -1;
while(n > 1) {
n--;
if (!in_cidr(ip, cidr, n)) continue;
if (min_cidr(cidr, n) < ip_A) goto done;
if (max_cidr(cidr, n) > ip_B) goto done;
rc = 0;
break;
}
done:
return (rc < 0) ? 0 : n;
}
int generate_result(int argc, char *argv[]) {
uint32_t n=0,c=0,network=0,m;
uint64_t mask;
int rc = -1;
switch (CF.mode) {
case MODE_MASK_TO_N:
printf("/%u\n", CF.slash_n);
break;
case MODE_N_TO_MASK:
mask = ~((1UL << (32-CF.slash_n)) - 1);
struct in_addr ia = {.s_addr = htonl(mask)};
printf("%s\n", inet_ntoa(ia));
break;
case MODE_IN_SAME_NET:
mask = ~((1UL << (32-CF.slash_n)) - 1);
assert(argc);
argc--;
argv++;
while(c < argc) {
struct in_addr ia;
if (inet_aton( argv[c], &ia) == 0) goto done;
if (c++ == 0) network = mask & ntohl(ia.s_addr);
if ((mask & ntohl(ia.s_addr)) != network) {
printf("Addresses in different networks\n");
rc = 0;
goto done;
}
}
printf("Addresses in same network\n");
break;
case MODE_CIDR_EXPAND:
mask = ~((1UL << (32-CF.slash_n)) - 1);
uint32_t num_host_bits = 32 - CF.slash_n;
uint64_t num_permutations = 1U << num_host_bits;
uint64_t h = 0;
while(h < num_permutations) {
uint32_t ip = (CF.cidr & mask) | h++;
struct in_addr ia = {.s_addr = htonl(ip)};
printf("%s\n", inet_ntoa(ia));
}
break;
case MODE_RANGE_TO_CIDRS:
if (argc != 2) goto done;
struct in_addr a, b;
uint64_t ip_A, ip_B, ip; /* prevents rollover */
uint32_t cidr;
if (inet_aton(argv[0], &a) == 0) goto done;
if (inet_aton(argv[1], &b) == 0) goto done;
ip_A = ntohl(a.s_addr);
ip_B = ntohl(b.s_addr);
if (ip_A > ip_B) goto done;
cidr = ip_A;
n = 32;
for(ip = ip_A; ip <= ip_B; ip++) {
if (in_cidr(ip, cidr, n)) continue;
m = widen_cidr(cidr, n, ip, ip_A, ip_B);
if (m == 0) {
print_cidr(cidr, n); /* close prior range */
cidr = ip; /* start a new range */
n = 32;
} else n = m;
}
print_cidr(cidr, n);
break;
default:
goto done;
break;
}
rc = 0;
done:
return rc;
}
int main(int argc, char *argv[]) {
int opt, rc=-1, sc;
while ( (opt = getopt(argc,argv,"vh")) > 0) {
switch(opt) {
case 'v': CF.verbose++; break;
case 'h': default: usage(argv[0]); break;
}
}
assert(argc >= optind);
argv += optind;
argc -= optind;
if (CF.mode == MODE_UNKNOWN) {
sc = infer_mode(argc, argv);
if (sc < 0) goto done;
}
sc = generate_result(argc,argv);
if (sc < 0) goto done;
rc = 0;
done:
if (rc < 0) usage();
return rc;
}