Commit 41175821 authored by Simon Morlat's avatar Simon Morlat

Use correct random functions on windows

parent a8383c3c
INSTALL
*.o
*.lo
*.la
......
Installation Instructions
*************************
Copyright (C) 1994-1996, 1999-2002, 2004-2013 Free Software Foundation,
Inc.
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'
instead.
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);
#endif
ORTP_PUBLIC long int ortp_random(void);
#define vsnprintf _vsnprintf
#define srandom srand
#define random ortp_random
typedef SOCKET ortp_socket_t;
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
......@@ -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;
err=srand_r(&value);
if (err!=0){
ortp_fatal("srand_r() failed !");
}
return (long)value;
}else{
unsigned int value1=0;
unsigned int value2=0;
err=srand_r(&value1);
if (err!=0){
ortp_fatal("srand_r() failed !");
}
err=srand_r(&value2);
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;
}
#endif
#if defined(WIN32) && !defined(_MSC_VER)
#include <wincrypt.h>
static int ortp_wincrypto_random(unsigned int *rand_number){
static HCRYPTPROV hProv=(HCRYPTPROV)-1;
static int initd=0;
if (!initd){
if (!CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)){
ortp_error("ortp_wincrypto_random(): Could not acquire a windows crypto context");
return -1;
}
initd=TRUE;
}
if (hProv==(HCRYPTPROV)-1)
return -1;
if (!CryptGenRandom(hProv,4,(BYTE*)rand_number)){
ortp_error("ortp_wincrypto_random(): CryptGenRandom() failed.");
return -1;
}
return 0;
}
#endif
unsigned int ortp_random(void){
#ifdef HAVE_ARC4RANDOM
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;
}
#else
if (ortp_wincrypto_random(&ret)==0){
return ret;
}
#endif
/* Windows's rand() is unsecure but is used as a fallback*/
if (!initd) {
struct timeval tv;
ortp_gettimeofday(&tv,NULL);
srand((unsigned int)tv.tv_sec+tv.tv_usec);
initd=1;
ortp_warning("ortp: Random generator is using rand(), this is unsecure !");
}
return rand()<<16 | rand();
#endif
/*fallback to UNIX random()*/
#ifndef WIN32
return (unsigned int) random();
#endif
}
......@@ -91,11 +91,7 @@ extern void rtp_parse(RtpSession *session, mblk_t *mp, uint32_t local_str_ts,
static uint32_t uint32_t_random(){
#ifdef HAVE_ARC4RANDOM
return arc4random();
#else
return random();
#endif
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