Commit 41175821 authored by Simon Morlat's avatar Simon Morlat

Use correct random functions on windows

parent a8383c3c
Installation Instructions
Copyright (C) 1994-1996, 1999-2002, 2004-2013 Free Software Foundation,
Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005,
2006, 2007, 2008, 2009 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
......@@ -12,8 +12,8 @@ without warranty of any kind.
Basic Installation
Briefly, the shell command `./configure && make && make install'
should configure, build, and install this package. The following
Briefly, the shell commands `./configure; make; make install' should
configure, build, and install this package. The following
more-detailed instructions are generic; see the `README' file for
instructions specific to this package. Some packages provide this
`INSTALL' file but do not implement all of the features documented
......@@ -226,11 +226,6 @@ order to use an ANSI C compiler:
and if that doesn't work, install pre-built binaries of GCC for HP-UX.
HP-UX `make' updates targets which have the same time stamps as
their prerequisites, which makes it generally unusable when shipped
generated files such as `configure' are involved. Use GNU `make'
On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot
parse its `<wchar.h>' header file. The option `-nodtk' can be used as
a workaround. If GNU CC is not installed, it is therefore recommended
......@@ -309,10 +304,9 @@ causes the specified `gcc' to be used as the C compiler (unless it is
overridden in the site shell script).
Unfortunately, this technique does not work for `CONFIG_SHELL' due to
an Autoconf limitation. Until the limitation is lifted, you can use
this workaround:
an Autoconf bug. Until the bug is fixed you can use this workaround:
CONFIG_SHELL=/bin/bash ./configure CONFIG_SHELL=/bin/bash
CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash
`configure' Invocation
......@@ -368,3 +362,4 @@ operates.
`configure' also accepts some other, not widely useful, options. Run
`configure --help' for more details.
......@@ -162,11 +162,7 @@ typedef __int16 int16_t;
ORTP_PUBLIC char* strtok_r(char *str, const char *delim, char **nextp);
ORTP_PUBLIC long int ortp_random(void);
#define vsnprintf _vsnprintf
#define srandom srand
#define random ortp_random
typedef SOCKET ortp_socket_t;
......@@ -312,6 +308,7 @@ ORTP_PUBLIC int ortp_file_exist(const char *pathname);
ORTP_PUBLIC void ortp_get_cur_time(ortpTimeSpec *ret);
ORTP_PUBLIC uint64_t ortp_get_cur_time_ms(void);
ORTP_PUBLIC unsigned int ortp_random(void);
/* portable named pipes and shared memory*/
#if !defined(_WIN32_WCE)
......@@ -327,30 +327,6 @@ int WIN_cond_destroy(ortp_cond_t * hCond)
return 0;
long int ortp_random(void){
errno_t err;
if (sizeof(long)==4){
unsigned int value=0;
if (err!=0){
ortp_fatal("srand_r() failed !");
return (long)value;
unsigned int value1=0;
unsigned int value2=0;
if (err!=0){
ortp_fatal("srand_r() failed !");
if (err!=0){
ortp_fatal("srand_r() failed !");
return ((long)value1) << 32 || ((long)value2);
#if defined(_WIN32_WCE)
#include <time.h>
......@@ -802,3 +778,71 @@ char* strtok_r(char *str, const char *delim, char **nextp){
return ret;
#if defined(WIN32) && !defined(_MSC_VER)
#include <wincrypt.h>
static int ortp_wincrypto_random(unsigned int *rand_number){
static int initd=0;
if (!initd){
ortp_error("ortp_wincrypto_random(): Could not acquire a windows crypto context");
return -1;
if (hProv==(HCRYPTPROV)-1)
return -1;
if (!CryptGenRandom(hProv,4,(BYTE*)rand_number)){
ortp_error("ortp_wincrypto_random(): CryptGenRandom() failed.");
return -1;
return 0;
unsigned int ortp_random(void){
return arc4random();
#elif defined(__linux) || defined(__APPLE__)
static int fd=-1;
if (fd==-1) fd=open("/dev/urandom",O_RDONLY);
if (fd!=-1){
unsigned int tmp;
if (read(fd,&tmp,4)!=4){
ortp_error("Reading /dev/urandom failed.");
}else return tmp;
}else ortp_error("Could not open /dev/urandom");
#elif defined(WIN32)
static int initd=0;
unsigned int ret;
#ifdef _MSC_VER
/*rand_s() is pretty nice and simple function but is not wrapped by mingw.*/
if (rand_s(&ret)==0){
return ret;
if (ortp_wincrypto_random(&ret)==0){
return ret;
/* Windows's rand() is unsecure but is used as a fallback*/
if (!initd) {
struct timeval tv;
srand((unsigned int)tv.tv_sec+tv.tv_usec);
ortp_warning("ortp: Random generator is using rand(), this is unsecure !");
return rand()<<16 | rand();
/*fallback to UNIX random()*/
#ifndef WIN32
return (unsigned int) random();
......@@ -91,11 +91,7 @@ extern void rtp_parse(RtpSession *session, mblk_t *mp, uint32_t local_str_ts,
static uint32_t uint32_t_random(){
return arc4random();
return random();
return ortp_random();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment