-
Notifications
You must be signed in to change notification settings - Fork 0
/
subnetgen.c
41 lines (37 loc) · 931 Bytes
/
subnetgen.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
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef _WIN32
#include "getentropy_windows.h"
#endif
uint8_t subnetgen_random_uint8() {
uint8_t n;
assert(!getentropy(&n, sizeof(n)));
return n;
}
#define SUBNETGEN_IPV4_MASK "10.%u.%u.0/24"
#define SUBNETGEN_IPV6_MASK "fd%x:%x%x:%x%x::/48"
int main(int argc, char *argv[]) {
for (;;)
switch (getopt(argc, argv, "46")) {
case '4':
printf(SUBNETGEN_IPV4_MASK "\n",
subnetgen_random_uint8(),
subnetgen_random_uint8());
break;
case '6':
printf(SUBNETGEN_IPV6_MASK "\n",
subnetgen_random_uint8(),
subnetgen_random_uint8(),
subnetgen_random_uint8(),
subnetgen_random_uint8(),
subnetgen_random_uint8());
break;
case -1:
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
}