add random abstraction

This commit is contained in:
Love Hornquist Astrand
2010-11-29 09:18:19 -08:00
parent bad0e733c5
commit 987faedb80
7 changed files with 67 additions and 17 deletions

View File

@@ -92,6 +92,7 @@ libroken_la_SOURCES = \
parse_time.c \
parse_units.c \
qsort.c \
rand.c \
realloc.c \
resolve.c \
roken_gethostby.c \

View File

@@ -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 \

View File

@@ -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] != '-')

48
lib/roken/rand.c Normal file
View File

@@ -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
}

View File

@@ -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;

View File

@@ -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__ */

View File

@@ -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)