Improve random logic for hash tables.
Implement Windows random API if it is available.
#endif
]])
- AC_CHECK_HEADERS([windows.h winsock2.h ws2tcpip.h w32api/wtypes.h], [], [],
+ AC_CHECK_HEADERS([windows.h winsock2.h ws2tcpip.h w32api/wtypes.h wincrypt.h], [], [],
[[
#ifndef _X86_
#define _X86_
# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_REALLOC
- AC_CHECK_FUNCS([gettimeofday memset strcasecmp strchr strdup strerror strncasecmp strtol strtoul memchr memrchr])
+ AC_CHECK_FUNCS([gettimeofday memset strcasecmp strchr strdup strerror strncasecmp strtol strtoul memchr memrchr clock_gettime])
OCFLAGS=$CFLAGS
CFLAGS=""
DefragTrackerQueueInit(&defragtracker_spare_q);
#ifndef AFLFUZZ_NO_RANDOM
- unsigned int seed = RandomTimePreseed();
/* set defaults */
- defrag_config.hash_rand = (int)(DEFRAG_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0));
+ defrag_config.hash_rand = (uint32_t)RandomGet();
#endif
defrag_config.hash_size = DEFRAG_DEFAULT_HASHSIZE;
defrag_config.memcap = DEFRAG_DEFAULT_MEMCAP;
FlowQueueInit(&flow_recycle_q);
#ifndef AFLFUZZ_NO_RANDOM
- unsigned int seed = RandomTimePreseed();
/* set defaults */
- flow_config.hash_rand = (int)( FLOW_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0));
+ flow_config.hash_rand = (uint32_t)RandomGet();
#endif
flow_config.hash_size = FLOW_DEFAULT_HASHSIZE;
flow_config.memcap = FLOW_DEFAULT_MEMCAP;
HostQueueInit(&host_spare_q);
#ifndef AFLFUZZ_NO_RANDOM
- unsigned int seed = RandomTimePreseed();
/* set defaults */
- host_config.hash_rand = (int)( HOST_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0));
+ host_config.hash_rand = (uint32_t)RandomGet();
#endif
host_config.hash_size = HOST_DEFAULT_HASHSIZE;
host_config.memcap = HOST_DEFAULT_MEMCAP;
IPPairQueueInit(&ippair_spare_q);
#ifndef AFLFUZZ_NO_RANDOM
- unsigned int seed = RandomTimePreseed();
/* set defaults */
- ippair_config.hash_rand = (int)( IPPAIR_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0));
+ ippair_config.hash_rand = (uint32_t)RandomGet();
#endif
ippair_config.hash_size = IPPAIR_DEFAULT_HASHSIZE;
ippair_config.memcap = IPPAIR_DEFAULT_MEMCAP;
-/* Copyright (C) 2007-2010 Open Information Security Foundation
+/* Copyright (C) 2007-2017 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
/**
* \file
*
- * \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
+ * \author Victor Julien <victor@inliniac.net>
*
- * Utility function for seeding rand
+ * Functions for getting a random value based on
+ * SEI CERT C Coding Standard MSC30-C
*/
#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)
+#if defined(HAVE_WINCRYPT_H) && defined(OS_WIN32)
+#include <wincrypt.h>
+
+long int RandomGet(void)
{
- /* preseed rand() */
- time_t now = time ( 0 );
- unsigned char *p = (unsigned char *)&now;
- unsigned seed = 0;
- size_t ind;
+ HCRYPTPROV p;
+ if (!(CryptAcquireContext(&p, NULL, NULL,
+ PROV_RSA_FULL, 0))) {
+ return -1;
+ }
- for ( ind = 0; ind < sizeof now; ind++ )
- seed = seed * ( UCHAR_MAX + 2U ) + p[ind];
+ long int value = 0;
+ if (!CryptGenRandom(p, sizeof(value), (BYTE *)&value)) {
+ (void)CryptReleaseContext(p, 0);
+ return -1;
+ }
- return seed;
+ (void)CryptReleaseContext(prov, 0);
+
+ return value;
}
+#elif defined(HAVE_CLOCK_GETTIME)
+long int RandomGet(void)
+{
+ struct timespec ts;
+ clock_gettime(CLOCK_REALTIME, &ts);
+ srandom(ts.tv_nsec ^ ts.tv_sec);
+ long int value = random();
+ return value;
+}
+#else
+long int RandomGet(void)
+{
+ struct timeval tv;
+ memset(&tv, 0, sizeof(tv));
+ gettimeofday(&tv, NULL);
+
+ srandom(tv.tv_usec ^ tv.tv_sec);
+ long int value = random();
+ return value;
+}
+#endif
-/* Copyright (C) 2007-2010 Open Information Security Foundation
+/* Copyright (C) 2007-2017 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
/**
* \file
*
- * \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
+ * \author Victor Julien <victor@inliniac.net>
*/
#ifndef __UTIL_RANDOM_H__
#define __UTIL_RANDOM_H__
-unsigned int RandomTimePreseed(void);
+long int RandomGet(void);
#endif /* __UTIL_RANDOM_H__ */