diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-12-10 03:05:47 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-12-10 03:05:47 +0000 |
commit | 416ef5e2bf59bb2e45066a1d5d91ac677c0f48e5 (patch) | |
tree | 1c746d673dcec7a8488c6ac51db8245411034376 /src/clients/s6-randomip.c | |
download | s6-dns-416ef5e2bf59bb2e45066a1d5d91ac677c0f48e5.tar.xz |
Initial commit
Diffstat (limited to 'src/clients/s6-randomip.c')
-rw-r--r-- | src/clients/s6-randomip.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/clients/s6-randomip.c b/src/clients/s6-randomip.c new file mode 100644 index 0000000..b220374 --- /dev/null +++ b/src/clients/s6-randomip.c @@ -0,0 +1,62 @@ +/* ISC license. */ + +#include <errno.h> +#include <skalibs/uint.h> +#include <skalibs/sgetopt.h> +#include <skalibs/strerr2.h> +#include <skalibs/buffer.h> +#include <skalibs/fmtscan.h> +#include <skalibs/random.h> + +#define USAGE "s6-randomip [ -4 ] [ -6 ] [ -n number ]" +#define dieusage() strerr_dieusage(100, USAGE) + +int main (int argc, char const *const *argv) +{ + char fmt[IP6_FMT] ; + char ip[16] ; + unsigned int n ; + unsigned int i = 0 ; + unsigned int what = 0 ; + int finite = 0 ; + PROG = "s6-randomip" ; + for (;;) + { + register int opt = subgetopt(argc, argv, "46n:") ; + if (opt == -1) break ; + switch (opt) + { + case '4' : what |= 1 ; break ; + case '6' : what |= 2 ; break ; + case 'n' : + if (!uint0_scan(subgetopt_here.arg, &n)) dieusage() ; + finite = 1 ; + break ; + default : dieusage() ; + } + } + argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ; + if (!what) what = 1 ; + what = 1 << (1 << what) ; + if (!badrandom_init()) strerr_diefu1sys(111, "init RNG") ; + for (i = 0 ; !finite || (i < n) ; i++) + { + unsigned int len = what ; + if (len > 16) + { + char c ; + if (badrandom_string(&c, 1) < 1) + strerr_diefu1sys(111, "badrandom_string") ; + len = (c & 1) ? 16 : 4 ; + } + if (badrandom_string(ip, len) < 4) + strerr_diefu1sys(111, "badrandom_string") ; + len = (len == 16) ? ip6_fmt(fmt, ip) : ip4_fmt(fmt, ip) ; + fmt[len++] = '\n' ; + if (buffer_put(buffer_1, fmt, len) < (int)len) + strerr_diefu1sys(111, "write to stdout") ; + } + if (!buffer_flush(buffer_1)) + strerr_diefu1sys(111, "write to stdout") ; + return 0 ; +} |