Skip to content

Commit

Permalink
Merge pull request #701 from sjthomason/portable-rand
Browse files Browse the repository at this point in the history
Add kam_rand() macro for portability
  • Loading branch information
miconda committed Jul 8, 2016
2 parents 81b084e + db11fac commit 189cf64
Show file tree
Hide file tree
Showing 41 changed files with 134 additions and 69 deletions.
9 changes: 5 additions & 4 deletions main.c
Expand Up @@ -118,6 +118,7 @@
#include "dst_blacklist.h"
#endif
#include "rand/fastrand.h" /* seed */
#include "rand/kam_rand.h"

#include "stats.h"
#include "counters.h"
Expand Down Expand Up @@ -2103,10 +2104,10 @@ int main(int argc, char** argv)
}
seed+=getpid()+time(0);
LM_DBG("seeding PRNG with %u\n", seed);
srand(seed);
fastrand_seed(rand());
srandom(rand()+time(0));
LM_DBG("test random numbers %u %lu %u\n", rand(), random(), fastrand());
kam_srand(seed);
fastrand_seed(kam_rand());
srandom(kam_rand()+time(0));
LM_DBG("test random numbers %u %lu %u\n", kam_rand(), random(), fastrand());

/*register builtin modules*/
register_builtin_modules();
Expand Down
5 changes: 3 additions & 2 deletions modules/auth/auth_mod.c
Expand Up @@ -40,6 +40,7 @@
#include "../../lvalue.h"
#include "../../mod_fix.h"
#include "../../kemi.h"
#include "../../rand/kam_rand.h"
#include "../../modules/sl/sl.h"
#include "auth_mod.h"
#include "challenge.h"
Expand Down Expand Up @@ -248,14 +249,14 @@ static inline int generate_random_secret(void)
/* srandom(time(0)); -- seeded by core */

for(i = 0; i < RAND_SECRET_LEN; i++) {
sec_rand1[i] = 32 + (int)(95.0 * rand() / (RAND_MAX + 1.0));
sec_rand1[i] = 32 + (int)(95.0 * kam_rand() / (KAM_RAND_MAX + 1.0));
}

secret1.s = sec_rand1;
secret1.len = RAND_SECRET_LEN;

for(i = 0; i < RAND_SECRET_LEN; i++) {
sec_rand2[i] = 32 + (int)(95.0 * rand() / (RAND_MAX + 1.0));
sec_rand2[i] = 32 + (int)(95.0 * kam_rand() / (KAM_RAND_MAX + 1.0));
}

secret2.s = sec_rand2;
Expand Down
3 changes: 2 additions & 1 deletion modules/carrierroute/cr_func.c
Expand Up @@ -40,6 +40,7 @@
#include "../../mem/mem.h"
#include "../../qvalue.h"
#include "../../dset.h"
#include "../../rand/kam_rand.h"
#include "cr_map.h"
#include "cr_rule.h"
#include "cr_domain.h"
Expand Down Expand Up @@ -510,7 +511,7 @@ static int rewrite_on_rule(struct route_flags *rf_head, flag_t flags, str * dest
* was previously tried */

do {
int rule_no = rand() % rf->rule_num;
int rule_no = kam_rand() % rf->rule_num;
//LM_DBG("CR: trying rule_no=%d \n", rule_no);
for (rr = rf->rule_list; (rule_no > 0) && (rr->next!=NULL) ; rule_no-- , rr = rr->next) {}
} while (cr_uri_already_used(rr->host, used_dests, no_dests));
Expand Down
4 changes: 2 additions & 2 deletions modules/carrierroute/prime_hash.c
Expand Up @@ -30,10 +30,10 @@
#include "../../parser/parse_to.h"
#include "../../parser/parse_from.h"
#include "../../crc.h"
#include "../../rand/kam_rand.h"

#include <ctype.h>
#include <stdio.h> /* for snprintf */
#include <stdlib.h> /* for rand */

#include "prime_hash.h"

Expand Down Expand Up @@ -153,7 +153,7 @@ static int determine_fromto_user (struct to_body *fromto, str *source_string) {

static int determine_fromrand(str* source_string){

snprintf(&cr_randbuf[0], CR_RANDBUF_S , "%d", rand());
snprintf(&cr_randbuf[0], CR_RANDBUF_S , "%d", kam_rand());

LM_NOTICE("randbuf is %s\n", cr_randbuf);
source_string->s = cr_randbuf;
Expand Down
6 changes: 3 additions & 3 deletions modules/cdp/peermanager.c
Expand Up @@ -84,10 +84,10 @@ int peer_manager_init(dp_config *config)
msg_id_lock = lock_alloc();
msg_id_lock = lock_init(msg_id_lock);

srand((unsigned int)time(0));
*hopbyhop_id = rand();
kam_srand((unsigned int)time(0));
*hopbyhop_id = kam_rand();
*endtoend_id = (time(0)&0xFFF)<<20;
*endtoend_id |= rand() & 0xFFFFF;
*endtoend_id |= kam_rand() & 0xFFFFF;

for(i=0;i<config->peers_cnt;i++){
p = new_peer(config->peers[i].fqdn,config->peers[i].realm,config->peers[i].port,config->peers[i].src_addr);
Expand Down
4 changes: 2 additions & 2 deletions modules/cdp/session.c
Expand Up @@ -172,8 +172,8 @@ int cdp_sessions_init(int hash_size)
LOG_NO_MEM("shm",sizeof(unsigned int));
goto error;
}
srand((unsigned int)time(0));
*session_id1 = rand();
kam_srand((unsigned int)time(0));
*session_id1 = kam_rand();
*session_id1 <<= 16;
*session_id1 += time(0)&0xFFFF;
*session_id2 = 0;
Expand Down
1 change: 1 addition & 0 deletions modules/cdp/utils.h
Expand Up @@ -50,6 +50,7 @@
#include "../../mem/shm_mem.h"
#include "../../locking.h"
#include "../../pt.h"
#include "../../rand/kam_rand.h"
#include "sem.h"

#ifndef LOG_NO_MEM
Expand Down
5 changes: 3 additions & 2 deletions modules/cfgutils/cfgutils.c
Expand Up @@ -74,6 +74,7 @@
#include "../../route.h"
#include "../../rpc_lookup.h"
#include "../../kemi.h"
#include "../../rand/kam_rand.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
Expand Down Expand Up @@ -736,7 +737,7 @@ static int rand_event(struct sip_msg *bar, char *foo1, char *foo2)
if ((*probability) == 0) return -1;
if ((*probability) == 100) return 1;

tmp = ((double) rand() / RAND_MAX);
tmp = ((double) kam_rand() / KAM_RAND_MAX);
LM_DBG("generated random %f\n", tmp);
if (tmp < ((double) (*probability) / 100)) {
LM_DBG("return true\n");
Expand All @@ -758,7 +759,7 @@ static int pv_get_random_val(struct sip_msg *msg, pv_param_t *param,
if(msg==NULL || res==NULL)
return -1;

n = rand();
n = kam_rand();
ch = int2str(n , &l);
res->rs.s = ch;
res->rs.len = l;
Expand Down
3 changes: 2 additions & 1 deletion modules/dialog/dlg_hash.c
Expand Up @@ -36,6 +36,7 @@
#include "../../hashes.h"
#include "../../lib/kmi/mi.h"
#include "../../lib/kcore/statistics.h"
#include "../../rand/kam_rand.h"
#include "dlg_timer.h"
#include "dlg_var.h"
#include "dlg_hash.h"
Expand Down Expand Up @@ -298,7 +299,7 @@ int init_dlg_table(unsigned int size)
LM_ERR("failed to init lock for slot: %d\n", i);
goto error1;
}
d_table->entries[i].next_id = rand() % (3*size);
d_table->entries[i].next_id = kam_rand() % (3*size);
}

return 0;
Expand Down
6 changes: 3 additions & 3 deletions modules/dispatcher/dispatch.c
Expand Up @@ -472,10 +472,10 @@ void shuffle_uint100array(unsigned int* arr){
int k;
int j;
unsigned int t;
srand(time(0));
kam_srand(time(0));
for (j=0; j<100; j++)
{
k = j + (rand() % (100-j));
k = j + (kam_rand() % (100-j));
t = arr[j];
arr[j] = arr[k];
arr[k] = t;
Expand Down Expand Up @@ -1897,7 +1897,7 @@ int ds_select_dst_limit(sip_msg_t *msg, int set, int alg, unsigned int limit, in
}
break;
case 6: /* random selection */
hash = rand() % idx->nr;
hash = kam_rand() % idx->nr;
break;
case 7: /* hash on PV value */
if (ds_hash_pvar(msg, &hash)!=0)
Expand Down
1 change: 1 addition & 0 deletions modules/dispatcher/dispatch.h
Expand Up @@ -31,6 +31,7 @@
#include <stdio.h>
#include "../../pvar.h"
#include "../../parser/msg_parser.h"
#include "../../rand/kam_rand.h"
#include "../../modules/tm/tm_load.h"


Expand Down
2 changes: 1 addition & 1 deletion modules/dispatcher/dispatcher.c
Expand Up @@ -571,7 +571,7 @@ static int mod_init(void)
*/
static int child_init(int rank)
{
srand((11+rank)*getpid()*7);
kam_srand((11+rank)*getpid()*7);

return 0;
}
Expand Down
11 changes: 6 additions & 5 deletions modules/drouting/drouting.c
Expand Up @@ -47,6 +47,7 @@
#include "../../parser/parse_uri.h"
#include "../../dset.h"
#include "../../rpc_lookup.h"
#include "../../rand/kam_rand.h"

#include "dr_load.h"
#include "prefix_tree.h"
Expand Down Expand Up @@ -403,7 +404,7 @@ static int dr_child_init(int rank)
LM_ERR("cannot select table \"%.*s\"\n", drg_table.len, drg_table.s);
return -1;
}
srand(getpid()+time(0)+rank);
kam_srand(getpid()+time(0)+rank);
return 0;
}

Expand Down Expand Up @@ -774,7 +775,7 @@ static int do_routing(struct sip_msg* msg, dr_group_t *drg)
} else {
if(i-j==2)
{
local_gwlist[t++] = j + rand()%2;
local_gwlist[t++] = j + kam_rand()%2;
if(sort_order==1)
{
local_gwlist[t++] = j + (local_gwlist[j]-j+1)%2;
Expand All @@ -783,11 +784,11 @@ static int do_routing(struct sip_msg* msg, dr_group_t *drg)
* local_gwlist[j+1]);*/
}
} else {
local_gwlist[t++] = j + rand()%(i-j);
local_gwlist[t++] = j + kam_rand()%(i-j);
if(sort_order==1)
{
do{
local_gwlist[t] = j + rand()%(i-j);
local_gwlist[t] = j + kam_rand()%(i-j);
}while(local_gwlist[t]==local_gwlist[t-1]);
t++;

Expand Down Expand Up @@ -824,7 +825,7 @@ static int do_routing(struct sip_msg* msg, dr_group_t *drg)
}
}
while ( dr_already_choosen(rt_info, local_gwlist, t-1, local_gwlist[t-1]) ) {
local_gwlist[t-1] = j + rand()%(i-j);
local_gwlist[t-1] = j + kam_rand()%(i-j);
}
}
LM_DBG("The %d gateway is %.*s [%d]\n", t, rt_info->pgwl[local_gwlist[t-1]].pgw->ip.len,
Expand Down
5 changes: 3 additions & 2 deletions modules/ims_auth/authorize.c
Expand Up @@ -57,6 +57,7 @@
#include "../../usr_avp.h"
#include "../../mod_fix.h"
#include "../../mem/mem.h"
#include "../../rand/kam_rand.h"
#include "../cdp/diameter.h"
#include "../cdp/diameter_ims_code_result.h"

Expand Down Expand Up @@ -933,7 +934,7 @@ int authenticate(struct sip_msg* msg, char* _realm, char* str2, int is_proxy_aut
int i;
char y[NONCE_LEN];
for (i = 0; i < NONCE_LEN; i++)
y[i] = (unsigned char) ((int) (256.0 * rand() / (RAND_MAX + 1.0)));
y[i] = (unsigned char) ((int) (256.0 * kam_rand() / (KAM_RAND_MAX + 1.0)));

if (unlikely((av->authenticate.len < 2 * NONCE_LEN))) {
if (av->authenticate.s) {
Expand Down Expand Up @@ -1222,7 +1223,7 @@ auth_vector * new_auth_vector(int item_number, str auth_scheme, str authenticate
int i;
char y[NONCE_LEN];
for (i = 0; i < NONCE_LEN; i++)
y[i] = (unsigned char) ((int) (256.0 * rand() / (RAND_MAX + 1.0)));
y[i] = (unsigned char) ((int) (256.0 * kam_rand() / (KAM_RAND_MAX + 1.0)));
x->authenticate.len = 2 * NONCE_LEN;
x->authenticate.s = shm_malloc(x->authenticate.len);
if (!x->authenticate.s) {
Expand Down
2 changes: 1 addition & 1 deletion modules/ims_charging/ro_session_hash.c
Expand Up @@ -117,7 +117,7 @@ int init_ro_session_table(unsigned int size) {

for (i = 0; i < size; i++) {
memset(&(ro_session_table->entries[i]), 0, sizeof (struct ro_session_entry));
ro_session_table->entries[i].next_id = rand() % (3*size);
ro_session_table->entries[i].next_id = kam_rand() % (3*size);
ro_session_table->entries[i].lock_idx = i % ro_session_table->locks_no;
}

Expand Down
1 change: 1 addition & 0 deletions modules/ims_charging/ro_session_hash.h
Expand Up @@ -10,6 +10,7 @@

#include "ro_timer.h"
#include "../../mem/shm_mem.h"
#include "../../rand/kam_rand.h"
#include "ims_charging_stats.h"
#include <stdlib.h>

Expand Down
3 changes: 2 additions & 1 deletion modules/ims_dialog/dlg_hash.c
Expand Up @@ -11,6 +11,7 @@
#include "../../dprint.h"
#include "../../ut.h"
#include "../../lib/kmi/mi.h"
#include "../../rand/kam_rand.h"
#include "dlg_timer.h"
#include "dlg_var.h"
#include "dlg_hash.h"
Expand Down Expand Up @@ -130,7 +131,7 @@ int init_dlg_table(unsigned int size) {

for (i = 0; i < size; i++) {
memset(&(d_table->entries[i]), 0, sizeof (struct dlg_entry));
d_table->entries[i].next_id = rand() % (3*size);
d_table->entries[i].next_id = kam_rand() % (3*size);
d_table->entries[i].lock_idx = i % d_table->locks_no;
}

Expand Down
1 change: 1 addition & 0 deletions modules/ims_registrar_scscf/reg_mod.h
Expand Up @@ -51,6 +51,7 @@
#include "../../parser/msg_parser.h"
#include "../../qvalue.h"
#include "../../usr_avp.h"
#include "../../rand/kam_rand.h"
#include "../ims_usrloc_scscf/usrloc.h"
#include "../../modules/sl/sl.h"
#include "../../modules/tm/tm_load.h"
Expand Down
2 changes: 1 addition & 1 deletion modules/ims_registrar_scscf/registrar_notify.c
Expand Up @@ -109,7 +109,7 @@ static inline int randomize_expires(int expires, int range) {

int range_min = expires - (float) range / 100 * expires;

return range_min + (float) (rand() % 100) / 100 * (expires - range_min);
return range_min + (float) (kam_rand() % 100) / 100 * (expires - range_min);
}

int notify_init() {
Expand Down
2 changes: 1 addition & 1 deletion modules/ims_registrar_scscf/save.c
Expand Up @@ -96,7 +96,7 @@ static inline int randomize_expires(int expires, int range) {

int range_min = expires - (float) range / 100 * expires;

return range_min + (float) (rand() % 100) / 100 * (expires - range_min);
return range_min + (float) (kam_rand() % 100) / 100 * (expires - range_min);
}

/*! \brief
Expand Down
2 changes: 1 addition & 1 deletion modules/ims_registrar_scscf/sip_msg.c
Expand Up @@ -53,7 +53,7 @@ static inline int get_expire_val(void) {
return expires;
/* select a random value in the range */
return expires - (float) range / 100 * expires
+ (float) (rand() % 100) / 100 * 2 * (float) range / 100 * expires;
+ (float) (kam_rand() % 100) / 100 * 2 * (float) range / 100 * expires;
}

/*! \brief
Expand Down
3 changes: 2 additions & 1 deletion modules/iptrtpproxy/iptrtpproxy.c
Expand Up @@ -48,6 +48,7 @@
#include "../../select_buf.h"
#include "../../script_cb.h"
#include "../../cfg_parser.h"
#include "../../rand/kam_rand.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
Expand Down Expand Up @@ -1438,7 +1439,7 @@ ERR("RTPPROXY_DEBUG: session_count: %d, reuse_existing_count: %d\n", global_sdp_

} else if (ok_stamp == 0) {
/* not yet acquired, highest */
w = 100000000 + (rand() & 0xFFFF); /* randomize not yet asked or being hartbeated */
w = 100000000 + (kam_rand() & 0xFFFF); /* randomize not yet asked or being hartbeated */
} else {
/* middle */
w = 1000;
Expand Down
3 changes: 2 additions & 1 deletion modules/lcr/lcr_mod.c
Expand Up @@ -64,6 +64,7 @@
#include "../../socket_info.h"
#include "../../pvar.h"
#include "../../mod_fix.h"
#include "../../rand/kam_rand.h"
#include "hash.h"
#include "lcr_rpc.h"
#include "../../rpc_lookup.h"
Expand Down Expand Up @@ -1992,7 +1993,7 @@ static int load_gws(struct sip_msg* _m, int argc, action_u_t argv[])
matched_gws[gw_index].prefix_len = pl->prefix_len;
matched_gws[gw_index].priority = t->priority;
matched_gws[gw_index].weight = t->weight *
(rand() >> 8);
(kam_rand() >> 8);
matched_gws[gw_index].duplicate = 0;
LM_DBG("added matched_gws[%d]=[%u, %u, %u, %u]\n",
gw_index, t->gw_index, pl->prefix_len,
Expand Down

0 comments on commit 189cf64

Please sign in to comment.