Move rand seed code into util-random

remotes/origin/master-1.0.x
Victor Julien 15 years ago
parent 6224c30548
commit 6beee776ca

@ -117,6 +117,7 @@ util-host-os-info.c util-host-os-info.h \
util-rule-vars.c util-rule-vars.h \
util-fix_checksum.c util-fix_checksum.h \
util-daemon.c util-daemon.h \
util-random.c util-random.h \
tm-modules.c tm-modules.h \
tm-queues.c tm-queues.h \
tm-queuehandlers.c tm-queuehandlers.h \

@ -30,6 +30,7 @@
#include "util-print.h"
#include "util-debug.h"
#include "util-fix_checksum.h"
#include "util-random.h"
#ifdef UNITTESTS
#include "util-unittest.h"
@ -1266,9 +1267,9 @@ void
DefragInit(void)
{
/* Initialize random value for hashing and hash table size. */
unsigned int seed = TimeRandPreseed();
unsigned int seed = RandomTimePreseed();
/* set defaults */
defrag_hash_rand = (int)( DEFAULT_DEFRAG_HASH_SIZE * (rand_r(&seed) / RAND_MAX + 1.0)) ; /* XXX seed rand */
defrag_hash_rand = (int)( DEFAULT_DEFRAG_HASH_SIZE * (rand_r(&seed) / RAND_MAX + 1.0));
defrag_hash_size = DEFAULT_DEFRAG_HASH_SIZE;

@ -14,6 +14,7 @@
#include "tm-modules.h"
#include "tm-threads.h"
#include "util-random.h"
#include "util-time.h"
#include "flow.h"
@ -436,9 +437,9 @@ void FlowInitConfig (char quiet)
}
SCMutexInit(&flow_memuse_mutex, NULL);
unsigned int seed = TimeRandPreseed();
unsigned int seed = RandomTimePreseed();
/* set defaults */
flow_config.hash_rand = (int)( FLOW_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0)) ; /* XXX seed rand */
flow_config.hash_rand = (int)( FLOW_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0));
flow_config.hash_size = FLOW_DEFAULT_HASHSIZE;
flow_config.memcap = FLOW_DEFAULT_MEMCAP;

@ -0,0 +1,26 @@
/** \file
* \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
*/
#include "suricata-common.h"
#include "detect.h"
#include "threads.h"
#include "util-debug.h"
/**
* \brief create a seed number to pass to rand() , rand_r(), and similars
* \retval seed for rand()
*/
unsigned int RandomTimePreseed(void) {
/* preseed rand() */
time_t now = time ( 0 );
unsigned char *p = (unsigned char *)&now;
unsigned seed = 0;
size_t ind;
for ( ind = 0; ind < sizeof now; ind++ )
seed = seed * ( UCHAR_MAX + 2U ) + p[ind];
return seed;
}

@ -0,0 +1,7 @@
#ifndef __UTIL_RANDOM_H__
#define __UTIL_RANDOM_H__
unsigned int RandomTimePreseed(void);
#endif /* __UTIL_RANDOM_H__ */

@ -79,19 +79,3 @@ void TimeSetIncrementTime(uint32_t tv_sec) {
TimeSet(&tv);
}
/**
* \brief create a seed number to pass to rand() , rand_r(), and similars
*/
unsigned int TimeRandPreseed(void) {
/* preseed rand() */
time_t now = time ( 0 );
unsigned char *p = (unsigned char *)&now;
unsigned seed = 0;
size_t ind;
for ( ind = 0; ind < sizeof now; ind++ )
seed = seed * ( UCHAR_MAX + 2U ) + p[ind];
return seed;
}

@ -10,7 +10,5 @@ void TimeSetIncrementTime(uint32_t);
void TimeModeSetLive(void);
void TimeModeSetOffline (void);
unsigned int TimeRandPreseed(void);
#endif /* __UTIL_TIME_H__ */

Loading…
Cancel
Save