OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / utils / ranbits.c
1 /*
2  * random bit generation for scripts, control files, etc.
3  * Copyright (C) 1998, 1999, 2000  Henry Spencer.
4  * 
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  *
15  * RCSID $Id: ranbits.c,v 1.9 2002/03/08 00:46:57 henry Exp $
16  */
17
18 #include <sys/types.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <limits.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <netinet/in.h>
27 #include <freeswan.h>
28
29 #ifndef DEVICE
30 #define DEVICE  "/dev/random"
31 #endif
32 #ifndef QDEVICE
33 #define QDEVICE "/dev/urandom"
34 #endif
35 #ifndef MAXBITS
36 #define MAXBITS 20000
37 #endif
38
39 char usage[] = "Usage: ranbits [--quick] [--continuous] [--bytes] nbits";
40 struct option opts[] = {
41         "quick",        0,      NULL,   'q',
42         "continuous",   0,      NULL,   'c',
43         "bytes",        0,      NULL,   'b',
44         "help",         0,      NULL,   'h',
45         "version",      0,      NULL,   'v',
46         0,              0,      NULL,   0,
47 };
48 int quick = 0;                  /* quick and dirty? */
49 char format = 'h';              /* datatot() format code */
50 int isbytes = 0;                /* byte count rather than bits? */
51
52 char me[] = "ipsec ranbits";    /* for messages */
53 char cop[] = "See `ipsec --copyright' for copyright information.";
54
55 char buf[MAXBITS/CHAR_BIT];
56 char outbuf[3*sizeof(buf)];
57
58 main(argc, argv)
59 int argc;
60 char *argv[];
61 {
62         int opt;
63         extern int optind;
64         int errflg = 0;
65         int nbits;
66         size_t nbytes;
67         char *devname;
68         int dev;
69         size_t ndone;
70         size_t nneeded;
71         ssize_t got;
72
73         while ((opt = getopt_long(argc, argv, "", opts, NULL)) != EOF)
74                 switch (opt) {
75                 case 'q':       /* quick and dirty randomness */
76                         quick = 1;
77                         break;
78                 case 'c':       /* continuous hex, no underscores */
79                         format = 'x';
80                         break;
81                 case 'b':       /* byte count, not bit count */
82                         isbytes = 1;
83                         break;
84                 case 'h':       /* help */
85                         printf("%s\n", usage);
86                         exit(0);
87                         break;
88                 case 'v':       /* version */
89                         printf("%s %s\n", me, ipsec_version_code());
90                         printf("%s\n", cop);
91                         exit(0);
92                         break;
93                 case '?':
94                 default:
95                         errflg = 1;
96                         break;
97                 }
98         if (errflg || optind != argc-1) {
99                 fprintf(stderr, "%s\n", usage);
100                 exit(2);
101         }
102
103         nbits = atoi(argv[optind]);
104         if (isbytes)
105                 nbits *= CHAR_BIT;
106         if (nbits <= 0) {
107                 fprintf(stderr, "%s: invalid bit count (%d)\n", me, nbits);
108                 exit(1);
109         }
110         if (nbits > MAXBITS) {
111                 fprintf(stderr, "%s: overlarge bit count (max %d)\n", me,
112                                                                 MAXBITS);
113                 exit(1);
114         }
115         nbytes = (size_t)(nbits + CHAR_BIT - 1) / CHAR_BIT;
116
117         devname = (quick) ? QDEVICE : DEVICE;
118         dev = open(devname, 0);
119         if (dev < 0) {
120                 fprintf(stderr, "%s: could not open %s (%s)\n", me,
121                                                 devname, strerror(errno));
122                 exit(1);
123         }
124
125         ndone = 0;
126         while (ndone < nbytes) {
127                 got = read(dev, buf + ndone, nbytes - ndone);
128                 if (got < 0) {
129                         fprintf(stderr, "%s: read error on %s (%s)\n", me,
130                                                 devname, strerror(errno));
131                         exit(1);
132                 }
133                 if (got == 0) {
134                         fprintf(stderr, "%s: eof on %s!?!\n", me, devname);
135                         exit(1);
136                 }
137                 ndone += got;
138         }
139
140         nneeded = datatot(buf, nbytes, format, outbuf, sizeof(outbuf));
141         if (nneeded > sizeof(outbuf)) {
142                 fprintf(stderr, "%s: buffer overflow (need %ld bytes)?!?\n",
143                                                 me, (long)nneeded);
144                 exit(1);
145         }
146         printf("%s\n", outbuf);
147         exit(0);
148 }