Skip to content

Commit bd3751b

Browse files
committed
uac_registrant: update timer handling
- closes OpenSIPS#758 - uac_registrant wrong hash sequence (cherry picked from commit be371da)
1 parent 44cc594 commit bd3751b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

modules/uac_registrant/registrant.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static int mod_init(void);
7777
static void mod_destroy(void);
7878
static int child_init(int rank);
7979

80-
void timer_check(unsigned int ticks, void* param);
80+
void timer_check(unsigned int ticks, void* hash_counter);
8181

8282
static struct mi_root* mi_reg_list(struct mi_root* cmd, void* param);
8383
static struct mi_root* mi_reg_reload(struct mi_root* cmd, void* param);
@@ -93,7 +93,6 @@ unsigned int timer_interval = 100;
9393

9494
reg_table_t reg_htable = NULL;
9595
unsigned int reg_hsize = 1;
96-
unsigned int hash_index = 0;
9796

9897
static str db_url = {NULL, 0};
9998

@@ -186,6 +185,7 @@ struct module_exports exports= {
186185
static int mod_init(void)
187186
{
188187
unsigned int _timer;
188+
int *param;
189189

190190
if(load_uac_auth_api(&uac_auth_api)<0){
191191
LM_ERR("Failed to load uac_auth api\n");
@@ -234,9 +234,18 @@ static int mod_init(void)
234234
return -1;
235235
}
236236

237+
/* allocate a shm variable to keep the counter used by the timer
238+
* routine - it must be shared as the routine get executed
239+
* in different processes */
240+
if (NULL==(param=(int*) shm_malloc(sizeof(int)))) {
241+
LM_ERR("cannot allocate shm memory for keepalive counter\n");
242+
return -1;
243+
}
244+
*param = 0;
245+
237246
_timer = timer_interval/reg_hsize;
238247
if (_timer) {
239-
register_timer("uac_reg_check", timer_check, 0, _timer,
248+
register_timer("uac_reg_check", timer_check, (void*)(long)param, _timer,
240249
TIMER_FLAG_DELAY_ON_DELAY);
241250
} else {
242251
LM_ERR("timer_interval=[%d] MUST be bigger then reg_hsize=[%d]\n",
@@ -625,15 +634,16 @@ int send_register(unsigned int hash_index, reg_record_t *rec, str *auth_hdr)
625634
struct timer_check_data {
626635
time_t now;
627636
str *s_now;
637+
int hash_counter;
628638
};
629639

630640
int run_timer_check(void *e_data, void *data, void *r_data)
631641
{
632-
unsigned int i=hash_index;
633642
reg_record_t *rec = (reg_record_t*)e_data;
634643
struct timer_check_data *t_check_data = (struct timer_check_data*)data;
635644
time_t now = t_check_data->now;
636645
str *s_now = t_check_data->s_now;
646+
unsigned int i = t_check_data->hash_counter;
637647

638648
switch(rec->state){
639649
case REGISTERING_STATE:
@@ -675,16 +685,17 @@ int run_timer_check(void *e_data, void *data, void *r_data)
675685
}
676686

677687

678-
void timer_check(unsigned int ticks, void* param)
688+
void timer_check(unsigned int ticks, void* hash_counter)
679689
{
680-
unsigned int i=hash_index;
690+
unsigned int i=*(unsigned*)(unsigned long*)hash_counter;
681691
char *p;
682692
int len, ret;
683693
time_t now;
684694
str str_now = {NULL, 0};
685695
struct timer_check_data t_check_data;
686696

687697
now = time(0);
698+
*(unsigned*)(unsigned long*)hash_counter = (i+1)%reg_hsize;
688699

689700
p = int2str((unsigned long)(time(0)), &len);
690701
if (p && len>0) {
@@ -701,6 +712,7 @@ void timer_check(unsigned int ticks, void* param)
701712
/* Initialize slinkedl run traversal data */
702713
t_check_data.now = now;
703714
t_check_data.s_now = &str_now;
715+
t_check_data.hash_counter = i;
704716

705717
LM_DBG("checking ... [%d] on htable[%d]\n", (unsigned int)now, i);
706718
lock_get(&reg_htable[i].lock);
@@ -711,8 +723,6 @@ void timer_check(unsigned int ticks, void* param)
711723

712724
if (str_now.s) {pkg_free(str_now.s);}
713725

714-
hash_index = (++i)%reg_hsize;
715-
716726
return;
717727
}
718728

0 commit comments

Comments
 (0)