aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-03-22 18:14:39 -0800
committerDavid M. Syzdek <david@syzdek.net>2022-03-22 21:01:45 -0800
commit9fd0295c20cb9cbe5d1d7ee2a62cbae28ca250e2 (patch)
treeb17ee828471e65d7d9f60e3f7d104c5680d3cf91
parent14c8a3e4feb6192dddd61544112a289a2c98ae63 (diff)
downloadtinyrad-9fd0295c20cb9cbe5d1d7ee2a62cbae28ca250e2.tar.gz
tinyrad-9fd0295c20cb9cbe5d1d7ee2a62cbae28ca250e2.tar.bz2
tinyrad-9fd0295c20cb9cbe5d1d7ee2a62cbae28ca250e2.tar.xz
tinyrad-9fd0295c20cb9cbe5d1d7ee2a62cbae28ca250e2.zip
examples: adding seed-by-clock
-rw-r--r--Makefile.am8
-rw-r--r--examples/seed-by-clock.c224
2 files changed, 231 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index c10a08d..4ff1eca 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -86,7 +86,8 @@ pkgdata_DATA =
86rfcdoc_DATA = 86rfcdoc_DATA =
87sbin_SCRIPTS = 87sbin_SCRIPTS =
88sbin_PROGRAMS = 88sbin_PROGRAMS =
89EXTRA_PROGRAMS = examples/socket-example \ 89EXTRA_PROGRAMS = examples/seed-by-clock \
90 examples/socket-example \
90 tests/test-array-sorted \ 91 tests/test-array-sorted \
91 tests/test-array-stack \ 92 tests/test-array-stack \
92 tests/test-array-queue \ 93 tests/test-array-queue \
@@ -189,6 +190,7 @@ if ENABLE_DOCUMENATION
189 rfcdoc_DATA += doc/rfc/rfc8044.txt 190 rfcdoc_DATA += doc/rfc/rfc8044.txt
190endif 191endif
191if ENABLE_EXAMPLES 192if ENABLE_EXAMPLES
193 noinst_PROGRAMS += examples/seed-by-clock
192 noinst_PROGRAMS += examples/socket-example 194 noinst_PROGRAMS += examples/socket-example
193endif 195endif
194if ENABLE_LIBTINYRAD 196if ENABLE_LIBTINYRAD
@@ -221,6 +223,10 @@ if ENABLE_TINYRAD_COMMON
221endif 223endif
222 224
223 225
226# macros for examples/seed-by-clock
227examples_seed_by_clock_SOURCES = examples/seed-by-clock.c
228
229
224# macros for examples/socket-example 230# macros for examples/socket-example
225examples_socket_example_SOURCES = examples/socket-example.c 231examples_socket_example_SOURCES = examples/socket-example.c
226 232
diff --git a/examples/seed-by-clock.c b/examples/seed-by-clock.c
new file mode 100644
index 0000000..4ea6b8f
--- /dev/null
+++ b/examples/seed-by-clock.c
@@ -0,0 +1,224 @@
1/*
2 * Tiny RADIUS Client Library
3 * Copyright (C) 2022 David M. Syzdek <david@syzdek.net>.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of David M. Syzdek nor the
16 * names of its contributors may be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31#define _SRC_SEED_BY_CLOCK_C 1
32
33///////////////
34// //
35// Headers //
36// //
37///////////////
38#pragma mark - Headers
39
40//#include <tinyrad_compat.h>
41
42#ifdef HAVE_CONFIG_H
43# include "config.h"
44#endif
45
46#include <stdio.h>
47#include <stdlib.h>
48#include <time.h>
49#include <getopt.h>
50
51
52///////////////////
53// //
54// Definitions //
55// //
56///////////////////
57#pragma mark - Definitions
58
59#undef PROGRAM_NAME
60#define PROGRAM_NAME "seed-by-clock"
61#ifndef PACKAGE_BUGREPORT
62# define PACKAGE_BUGREPORT "unknown"
63#endif
64#ifndef PACKAGE_COPYRIGHT
65# define PACKAGE_COPYRIGHT "unknown"
66#endif
67#ifndef PACKAGE_NAME
68# define PACKAGE_NAME "Tiny RADIUS Client Library"
69#endif
70#ifndef PACKAGE_VERSION
71# define PACKAGE_VERSION "unknown"
72#endif
73
74#undef _U
75#define _U( num ) ((unsigned) num )
76
77
78//////////////////
79// //
80// Prototypes //
81// //
82//////////////////
83#pragma mark - Prototypes
84
85int main( int argc, char * argv[] );
86
87
88/////////////////
89// //
90// Functions //
91// //
92/////////////////
93#pragma mark - Functions
94
95int main( int argc, char * argv[] )
96{
97 int c;
98 int opt_index;
99 unsigned rnd;
100 unsigned tmp;
101 struct timespec ts_rt1;
102 struct timespec ts_mono;
103 struct timespec ts_cpu;
104 struct timespec ts_comb;
105
106 // getopt options
107 static char short_opt[] = "hqVv";
108 static struct option long_opt[] =
109 {
110 {"help", no_argument, NULL, 'h' },
111 {"quiet", no_argument, NULL, 'q' },
112 {"silent", no_argument, NULL, 'q' },
113 {"version", no_argument, NULL, 'V' },
114 {"verbose", no_argument, NULL, 'v' },
115 { NULL, 0, NULL, 0 }
116 };
117
118 while((c = getopt_long(argc, argv, short_opt, long_opt, &opt_index)) != -1)
119 {
120 switch(c)
121 {
122 case -1: /* no more arguments */
123 case 0: /* long options toggles */
124 break;
125
126 case 'h':
127 printf("Usage: %s [OPTIONS]\n", PROGRAM_NAME);
128 printf("Options:\n");
129 printf(" -h, --help print this help and exit\n");
130 printf(" -q, --quiet, --silent do not print messages\n");
131 printf(" -V, --version print version number and exit\n");
132 printf(" -v, --verbose print verbose messages\n");
133 printf("\n");
134 return(0);
135
136 case 'q':
137 break;
138
139 case 'V':
140 printf("%s (%s) %s\n", PROGRAM_NAME, PACKAGE_NAME, PACKAGE_VERSION);
141 return(0);
142
143 case 'v':
144 break;
145
146 case '?':
147 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
148 return(1);
149
150 default:
151 fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c);
152 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
153 return(1);
154 };
155 };
156
157 // read initial clocks
158 clock_gettime(CLOCK_REALTIME, &ts_rt1);
159 clock_gettime(CLOCK_MONOTONIC, &ts_mono);
160 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_cpu);
161
162 // add seconds and nanoseconds
163 ts_comb.tv_sec = ts_rt1.tv_sec + ts_mono.tv_sec + ts_cpu.tv_sec;
164 ts_comb.tv_nsec = ts_rt1.tv_nsec + ts_mono.tv_nsec + ts_cpu.tv_nsec;
165 rnd = (unsigned)(ts_comb.tv_sec + ts_comb.tv_nsec);
166 tmp = rnd;
167
168 // reverse bits
169 rnd = ((rnd & 0xffff0000) >> 16) | ((rnd & 0x0000ffff) << 16);
170 rnd = ((rnd & 0xff00ff00) >> 8) | ((rnd & 0x00ff00ff) << 8);
171 rnd = ((rnd & 0xf0f0f0f0) >> 4) | ((rnd & 0x0f0f0f0f) << 4);
172 rnd = ((rnd & 0xcccccccc) >> 2) | ((rnd & 0x33333333) << 2);
173 rnd = ((rnd & 0xaaaaaaaa) >> 1) | ((rnd & 0x55555555) << 1);
174
175 // print initial results
176 printf("Round 1:\n");
177 printf(" realtime: %08x %08x\n", (unsigned)ts_rt1.tv_sec, (unsigned)ts_rt1.tv_nsec);
178 printf(" monotonic: %08x %08x\n", (unsigned)ts_mono.tv_sec, (unsigned)ts_mono.tv_nsec);
179 printf(" cputime: %08x %08x\n", (unsigned)ts_cpu.tv_sec, (unsigned)ts_cpu.tv_nsec);
180 printf(" combined: %08x %08x\n", (unsigned)ts_comb.tv_sec, (unsigned)ts_comb.tv_nsec);
181 printf(" rnd: %08x\n", tmp);
182 printf(" rnd (rev): %08x\n", rnd);
183
184 // read clocks second time
185 clock_gettime(CLOCK_REALTIME, &ts_rt1);
186 clock_gettime(CLOCK_MONOTONIC, &ts_mono);
187 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_cpu);
188
189 // add seconds and nanoseconds to initial result
190 ts_comb.tv_sec = ts_rt1.tv_sec + ts_mono.tv_sec + ts_cpu.tv_sec;
191 ts_comb.tv_nsec = ts_rt1.tv_nsec + ts_mono.tv_nsec + ts_cpu.tv_nsec;
192 rnd += (unsigned)(ts_comb.tv_sec + ts_comb.tv_nsec);
193
194 // print final results
195 printf("\n");
196 printf("Round 2:\n");
197 printf(" realtime: %08x %08x\n", (unsigned)ts_rt1.tv_sec, (unsigned)ts_rt1.tv_nsec);
198 printf(" monotonic: %08x %08x\n", (unsigned)ts_mono.tv_sec, (unsigned)ts_mono.tv_nsec);
199 printf(" cputime: %08x %08x\n", (unsigned)ts_cpu.tv_sec, (unsigned)ts_cpu.tv_nsec);
200 printf(" combined: %08x %08x\n", (unsigned)ts_comb.tv_sec, (unsigned)ts_comb.tv_nsec);
201 printf(" rnd: %08x\n", rnd);
202
203 // seed random number generator
204 srandom(rnd);
205
206 // print random numbers
207 printf("\n");
208 printf("psuedo random numbers:\n");
209 for(c = 0; (c < 14); c++)
210 {
211 printf(
212 " %08x %08x %08x %08x %08x %08x %08x\n",
213 (unsigned)random(), (unsigned)random(),
214 (unsigned)random(), (unsigned)random(),
215 (unsigned)random(), (unsigned)random(),
216 (unsigned)random()
217 );
218 };
219
220 return(0);
221}
222
223
224/* end of source */