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
|
/* 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 ;
}
|