From 987faedb800b5fc872a57f8c2fbb27e954a4dfeb Mon Sep 17 00:00:00 2001 From: Love Hornquist Astrand Date: Mon, 29 Nov 2010 09:18:19 -0800 Subject: [PATCH] add random abstraction --- lib/roken/Makefile.am | 1 + lib/roken/NTMakefile | 1 + lib/roken/getarg.c | 14 ++---------- lib/roken/rand.c | 48 ++++++++++++++++++++++++++++++++++++++++ lib/roken/resolve.c | 8 +++---- lib/roken/roken-common.h | 3 +++ lib/roken/roken.h.in | 9 ++++++++ 7 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 lib/roken/rand.c diff --git a/lib/roken/Makefile.am b/lib/roken/Makefile.am index 7e521842a..6c203e692 100644 --- a/lib/roken/Makefile.am +++ b/lib/roken/Makefile.am @@ -92,6 +92,7 @@ libroken_la_SOURCES = \ parse_time.c \ parse_units.c \ qsort.c \ + rand.c \ realloc.c \ resolve.c \ roken_gethostby.c \ diff --git a/lib/roken/NTMakefile b/lib/roken/NTMakefile index 1d4ac6452..00031c5cb 100644 --- a/lib/roken/NTMakefile +++ b/lib/roken/NTMakefile @@ -79,6 +79,7 @@ libroken_la_OBJS = \ $(OBJ)\realloc.obj \ $(OBJ)\rename.obj \ $(OBJ)\resolve.obj \ + $(OBJ)\rand.obj \ $(OBJ)\roken_gethostby.obj \ $(OBJ)\rtbl.obj \ $(OBJ)\sendmsg.obj \ diff --git a/lib/roken/getarg.c b/lib/roken/getarg.c index e7dc74b7b..a96e5c85b 100644 --- a/lib/roken/getarg.c +++ b/lib/roken/getarg.c @@ -435,11 +435,7 @@ arg_match_long(struct getargs *args, size_t num_args, *flag = !negate; return 0; } else if (*goptarg && strcmp(goptarg + 1, "maybe") == 0) { -#ifdef HAVE_RANDOM - *flag = random() & 1; -#else - *flag = rand() & 1; -#endif + *flag = rk_random() & 1; } else { *flag = negate; return 0; @@ -554,13 +550,7 @@ getarg(struct getargs *args, size_t num_args, int i; int ret = 0; -#if defined(HAVE_SRANDOMDEV) - srandomdev(); -#elif defined(HAVE_RANDOM) - srandom(time(NULL)); -#else - srand ((int) time(NULL)); -#endif + rk_random_init(); (*goptind)++; for(i = *goptind; i < argc; i++) { if(argv[i][0] != '-') diff --git a/lib/roken/rand.c b/lib/roken/rand.c new file mode 100644 index 000000000..ef92c2052 --- /dev/null +++ b/lib/roken/rand.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "roken.h" + +void ROKEN_LIB_FUNCTION +rk_random_init(void) +{ +#if defined(HAVE_ARC4RANDOM) + arc4random_stir(); +#elif defined(HAVE_SRANDOMDEV) + srandomdev(); +#elif defined(HAVE_RANDOM) + srandom(time(NULL)); +#else + srand (time(NULL)); +#endif +} diff --git a/lib/roken/resolve.c b/lib/roken/resolve.c index 97edda471..03715e5ff 100644 --- a/lib/roken/resolve.c +++ b/lib/roken/resolve.c @@ -619,10 +619,6 @@ compare_srv(const void *a, const void *b) return ((*aa)->u.srv->priority - (*bb)->u.srv->priority); } -#ifndef HAVE_RANDOM -#define random() rand() -#endif - /* try to rearrange the srv-records by the algorithm in RFC2782 */ ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL rk_dns_srv_order(struct rk_dns_reply *r) @@ -636,6 +632,8 @@ rk_dns_srv_order(struct rk_dns_reply *r) char *oldstate; #endif + rk_random_init(); + for(rr = r->head; rr; rr = rr->next) if(rr->type == rk_ns_t_srv) num_srv++; @@ -682,7 +680,7 @@ rk_dns_srv_order(struct rk_dns_reply *r) /* ss is now the first record of this priority and ee is the first of the next */ while(ss < ee) { - rnd = random() % (sum + 1); + rnd = rk_random() % (sum + 1); for(count = 0, tt = ss; ; tt++) { if(*tt == NULL) continue; diff --git a/lib/roken/roken-common.h b/lib/roken/roken-common.h index 0eed27709..d9369a3e1 100644 --- a/lib/roken/roken-common.h +++ b/lib/roken/roken-common.h @@ -495,6 +495,9 @@ rk_cloexec_dir(DIR *); ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL ct_memcmp(const void *, const void *, size_t); +void ROKEN_LIB_FUNCTION +rk_random_init(void); + ROKEN_CPP_END #endif /* __ROKEN_COMMON_H__ */ diff --git a/lib/roken/roken.h.in b/lib/roken/roken.h.in index 69fd64e33..e7cb1598f 100644 --- a/lib/roken/roken.h.in +++ b/lib/roken/roken.h.in @@ -1084,6 +1084,15 @@ void rk_qsort(void *, size_t, size_t, int (*)(const void *, const void *)); #endif +#if defined(HAVE_ARC4RANDOM) +#define rk_random() arc4random() +#elif defined(HAVE_RANDOM) +#define rk_random() random() +#else +#define rk_random() rand() +#endif + + #if defined(__linux__) && defined(SOCK_CLOEXEC) && !defined(SOCKET_WRAPPER_REPLACE) && !defined(__SOCKET_WRAPPER_H__) #undef socket #define socket(_fam,_type,_prot) rk_socket(_fam,_type,_prot)