diff --git a/configure.ac b/configure.ac index 7a48665..823f6f1 100644 --- a/configure.ac +++ b/configure.ac @@ -191,7 +191,7 @@ dnl Check if we have seteuid system call AC_CHECK_FUNCS(seteuid) dnl Check if we have arc4random family routines available -AC_CHECK_FUNCS(arc4random) +AC_CHECK_FUNCS(arc4random arc4random_uniform) dnl check if we can use the pthread_library diff --git a/include/ortp/port.h b/include/ortp/port.h index 4ab33ba..6fda893 100644 --- a/include/ortp/port.h +++ b/include/ortp/port.h @@ -326,6 +326,7 @@ ORTP_PUBLIC uint64_t ortp_get_cur_time_ms(void); ORTP_PUBLIC void ortp_sleep_ms(int ms); ORTP_PUBLIC void ortp_sleep_until(const ortpTimeSpec *ts); ORTP_PUBLIC unsigned int ortp_random(void); +ORTP_PUBLIC unsigned int ortp_random_uniform(unsigned int); /* portable named pipes and shared memory*/ #if !defined(_WIN32_WCE) diff --git a/src/netsim.c b/src/netsim.c index 000d0aa..286429f 100644 --- a/src/netsim.c +++ b/src/netsim.c @@ -220,7 +220,7 @@ static mblk_t * simulate_latency(RtpSession *session, mblk_t *input){ } static int simulate_jitter_by_bit_budget_reduction(OrtpNetworkSimulatorCtx *sim, int budget_increase){ - unsigned int r=ortp_random()%1000; + unsigned int r=ortp_random_uniform(1000); float threshold,score; int budget_adjust=0; uint64_t now=ortp_get_cur_time_ms(); @@ -237,7 +237,7 @@ static int simulate_jitter_by_bit_budget_reduction(OrtpNetworkSimulatorCtx *sim, threshold=500; } if (score>(int)threshold){ - int64_t strength_rand=sim->params.jitter_strength * (float)(ortp_random()%1000); + int64_t strength_rand=sim->params.jitter_strength * (float)(ortp_random_uniform(1000)); sim->in_jitter_event=TRUE; budget_adjust=-((int64_t)budget_increase*strength_rand/1000LL); /*ortp_message("jitter in progress... bit_budget_adjustement=%i, bit_budget=%i",budget_adjust,sim->bit_budget);*/ @@ -315,7 +315,7 @@ static mblk_t *simulate_loss_rate(OrtpNetworkSimulatorCtx *net_sim_ctx, mblk_t * loss_rate=net_sim_ctx->params.consecutive_loss_probability*1000.0; } - rrate = ortp_random() % 1000; + rrate = ortp_random_uniform(1000); if (rrate >= loss_rate) { if (net_sim_ctx->consecutive_drops){ diff --git a/src/port.c b/src/port.c index a7668fa..b23a12a 100644 --- a/src/port.c +++ b/src/port.c @@ -898,3 +898,11 @@ unsigned int ortp_random(void){ #endif } +unsigned int ortp_random_uniform(unsigned int upper_bound){ + #ifdef HAVE_ARC4RANDOM_UNIFORM + return arc4random_uniform(upper_bound); + #else + return ( ortp_random() % upper_bound); + #endif + +}