Skip to content

Commit 1688087

Browse files
b2b_entities: fix incorrect entity key after loading from DB
Do not generate a new random part for the entity key when loading from DB.
1 parent a940151 commit 1688087

File tree

8 files changed

+63
-82
lines changed

8 files changed

+63
-82
lines changed

modules/b2b_entities/b2b_entities.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ int b2b_restore_logic_info(enum b2b_entity_type type, str* key,
668668
{
669669
table = client_htable;
670670
}
671-
if(b2b_parse_key(key, &hash_index, &local_index, NULL) < 0)
671+
if(b2b_parse_key(key, &hash_index, &local_index) < 0)
672672
{
673673
LM_ERR("Wrong format for b2b key [%.*s]\n", key->len, key->s);
674674
return -1;
@@ -707,7 +707,7 @@ int b2b_update_b2bl_param(enum b2b_entity_type type, str* key,
707707
{
708708
table = client_htable;
709709
}
710-
if(b2b_parse_key(key, &hash_index, &local_index, NULL) < 0)
710+
if(b2b_parse_key(key, &hash_index, &local_index) < 0)
711711
{
712712
LM_ERR("Wrong format for b2b key [%.*s]\n", key->len, key->s);
713713
return -1;
@@ -756,9 +756,9 @@ str *b2b_get_b2bl_key(str* callid, str* from_tag, str* to_tag, str* entity_key)
756756
}
757757
/* check if the to tag has the b2b key format
758758
* -> meaning that it is a server request */
759-
if(b2b_parse_key(to_tag, &hash_index, &local_index, NULL)>=0)
759+
if(b2b_parse_key(to_tag, &hash_index, &local_index)>=0)
760760
table = server_htable;
761-
else if (b2b_parse_key(callid, &hash_index, &local_index, NULL)>=0)
761+
else if (b2b_parse_key(callid, &hash_index, &local_index)>=0)
762762
table = client_htable;
763763
else
764764
return NULL; /* to tag and/or callid are not part of this B2B */

modules/b2b_entities/b2be_clustering.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,14 @@ static inline void unpack_update_fields(bin_packet_t *packet, b2b_dlg_t *dlg)
352352
}
353353

354354
int receive_entity_create(bin_packet_t *packet, b2b_dlg_t *dlg, int type,
355-
b2b_table htable, unsigned int hash_index, unsigned int local_index,
356-
uint64_t timestamp)
355+
b2b_table htable, unsigned int hash_index, unsigned int local_index)
357356
{
358357
b2b_dlg_t tmp_dlg, *new_dlg = NULL;
359358
unsigned int h_idx, l_idx;
360359
int rcv_type;
361-
str *new_key;
362360
str sock_str;
363361
str b2be_key;
364362
dlg_leg_t leg, *new_leg = NULL;
365-
uint64_t ts;
366363

367364
if (!dlg) {
368365
memset(&tmp_dlg, 0, sizeof(b2b_dlg_t));
@@ -384,7 +381,7 @@ int receive_entity_create(bin_packet_t *packet, b2b_dlg_t *dlg, int type,
384381

385382
LM_DBG("Received replicated entity [%.*s]\n", b2be_key.len, b2be_key.s);
386383

387-
if (b2b_parse_key(&b2be_key, &h_idx, &l_idx, &ts) < 0) {
384+
if (b2b_parse_key(&b2be_key, &h_idx, &l_idx) < 0) {
388385
LM_ERR("Wrong format for b2b key [%.*s]\n",
389386
b2be_key.len, b2be_key.s);
390387
return -1;
@@ -407,9 +404,13 @@ int receive_entity_create(bin_packet_t *packet, b2b_dlg_t *dlg, int type,
407404

408405
hash_index = h_idx;
409406
local_index = l_idx;
410-
timestamp = ts;
411407
dlg = &tmp_dlg;
412408
type = rcv_type;
409+
} else {
410+
if (type == B2B_SERVER)
411+
b2be_key = dlg->tag[1];
412+
else
413+
b2be_key = dlg->callid;
413414
}
414415

415416
dlg->id = local_index;
@@ -469,9 +470,8 @@ int receive_entity_create(bin_packet_t *packet, b2b_dlg_t *dlg, int type,
469470

470471
B2BE_LOCK_GET(htable, hash_index);
471472

472-
new_key = b2b_htable_insert(htable, new_dlg, hash_index, (time_t)timestamp,
473-
type, 1, 1, 0);
474-
if (new_key == NULL) {
473+
if (!b2b_htable_insert(htable, new_dlg, hash_index, &b2be_key,
474+
type, 1, 1, 0)) {
475475
B2BE_LOCK_RELEASE(htable, hash_index);
476476
LM_ERR("Failed to insert new record\n");
477477
goto error;
@@ -484,8 +484,6 @@ int receive_entity_create(bin_packet_t *packet, b2b_dlg_t *dlg, int type,
484484

485485
B2BE_LOCK_RELEASE(htable, hash_index);
486486

487-
pkg_free(new_key);
488-
489487
return 0;
490488

491489
error:
@@ -516,7 +514,6 @@ int receive_entity_update(bin_packet_t *packet)
516514
int type;
517515
str b2be_key;
518516
b2b_table htable;
519-
uint64_t timestamp;
520517
int rc = 0;
521518

522519
memset(&tmp_dlg, 0, sizeof(b2b_dlg_t));
@@ -539,7 +536,7 @@ int receive_entity_update(bin_packet_t *packet)
539536
LM_DBG("Received replicated update for entity [%.*s]\n",
540537
b2be_key.len, b2be_key.s);
541538

542-
if (b2b_parse_key(&b2be_key, &hash_index, &local_index, &timestamp) < 0) {
539+
if (b2b_parse_key(&b2be_key, &hash_index, &local_index) < 0) {
543540
LM_ERR("Wrong format for b2b key [%.*s]\n", b2be_key.len, b2be_key.s);
544541
return -1;
545542
}
@@ -553,7 +550,7 @@ int receive_entity_update(bin_packet_t *packet)
553550

554551
if (packet->type == REPL_ENTITY_UPDATE)
555552
return receive_entity_create(packet, &tmp_dlg, type, htable,
556-
hash_index, local_index, timestamp);
553+
hash_index, local_index);
557554
else
558555
return 0;
559556
}
@@ -609,7 +606,7 @@ int receive_entity_delete(bin_packet_t *packet)
609606
LM_DBG("Received replicated delete for entity [%.*s]\n",
610607
b2be_key->len, b2be_key->s);
611608

612-
if (b2b_parse_key(b2be_key, &hash_index, &local_index, NULL) < 0) {
609+
if (b2b_parse_key(b2be_key, &hash_index, &local_index) < 0) {
613610
LM_ERR("Wrong format for b2b key [%.*s]\n", b2be_key->len, b2be_key->s);
614611
return -1;
615612
}
@@ -647,7 +644,7 @@ void b2be_recv_bin_packets(bin_packet_t *pkt)
647644
case REPL_ENTITY_CREATE:
648645
ensure_bin_version(pkt, B2BE_BIN_VERSION);
649646

650-
rc = receive_entity_create(pkt, NULL, B2B_NONE, NULL, 0, 0, 0);
647+
rc = receive_entity_create(pkt, NULL, B2B_NONE, NULL, 0, 0);
651648
break;
652649
case REPL_ENTITY_UPDATE:
653650
case REPL_ENTITY_PARAM_UPDATE:
@@ -665,7 +662,7 @@ void b2be_recv_bin_packets(bin_packet_t *pkt)
665662
ensure_bin_version(pkt, B2BE_BIN_VERSION);
666663

667664
while (cl_api.sync_chunk_iter(pkt))
668-
if (receive_entity_create(pkt, NULL, B2B_NONE, NULL, 0, 0, 0) < 0) {
665+
if (receive_entity_create(pkt, NULL, B2B_NONE, NULL, 0, 0) < 0) {
669666
LM_ERR("Failed to process sync packet\n");
670667
return;
671668
}

modules/b2b_entities/b2be_db.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,6 @@ static int load_entity(int_str_t *vals)
604604
int port, proto;
605605
b2b_table htable;
606606
int type;
607-
uint64_t ts = 0;
608607

609608
memset(&dlg, 0, sizeof(b2b_dlg_t));
610609

@@ -614,8 +613,9 @@ static int load_entity(int_str_t *vals)
614613

615614
if(type == B2B_SERVER)/* extract hash and local index */
616615
{
616+
b2b_key = &vals[2].s;
617617
htable = server_htable;
618-
if(b2b_parse_key(&dlg.tag[1], &hash_index, &local_index, &ts) < 0)
618+
if(b2b_parse_key(&dlg.tag[1], &hash_index, &local_index) < 0)
619619
{
620620
LM_ERR("Wrong format for b2b key [%.*s]\n", dlg.tag[1].len, dlg.tag[1].s);
621621
return -1;
@@ -631,9 +631,10 @@ static int load_entity(int_str_t *vals)
631631
}
632632
else
633633
{
634+
b2b_key = &vals[3].s;
634635
htable = client_htable;
635636

636-
if(b2b_parse_key(&dlg.callid, &hash_index, &local_index, NULL) < 0)
637+
if(b2b_parse_key(&dlg.callid, &hash_index, &local_index) < 0)
637638
{
638639
LM_ERR("Wrong format for b2b key [%.*s]\n", dlg.callid.len, dlg.callid.s);
639640
return -1;
@@ -687,13 +688,11 @@ static int load_entity(int_str_t *vals)
687688
LM_ERR("Failed to create new dialog structure\n");
688689
return -1;
689690
}
690-
b2b_key= b2b_htable_insert(htable,shm_dlg,hash_index, ts, type, 1, 0, 0);
691-
if(b2b_key == NULL)
691+
if (!b2b_htable_insert(htable,shm_dlg,hash_index, b2b_key, type, 1, 0, 0))
692692
{
693693
LM_ERR("Failed to insert new record\n");
694694
return -1;
695695
}
696-
pkg_free(b2b_key);
697696

698697
if (vals[14].s.len) {
699698
if (shm_str_dup(&shm_dlg->storage, &vals[14].s) < 0) {

modules/b2b_entities/client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ str* _client_new(client_info_t* ci,b2b_notify_t b2b_cback,
163163

164164
/* callid must have the special format */
165165
dlg->db_flag = NO_UPDATEDB_FLAG;
166-
callid = b2b_htable_insert(client_htable, dlg, hash_index, 0, B2B_CLIENT, 0, 0,
166+
callid = b2b_htable_insert(client_htable, dlg, hash_index, NULL, B2B_CLIENT, 0, 0,
167167
init_params?init_params->timeout:0);
168168
if(callid == NULL)
169169
{

modules/b2b_entities/dlg.c

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ b2b_dlg_t* b2b_search_htable(b2b_table table, unsigned int hash_index,
218218

219219
/* this is only called by server new */
220220
str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index,
221-
time_t timestamp, int src, int safe, int db_insert, unsigned int ua_timeout)
221+
str *init_b2b_key, int src, int safe, int db_insert, unsigned int ua_timeout)
222222
{
223223
b2b_dlg_t * it, *prev_it= NULL;
224224
str* b2b_key;
@@ -243,14 +243,18 @@ str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index,
243243
prev_it->next = dlg;
244244
dlg->prev = prev_it;
245245
}
246-
/* if an insert in server_htable -> copy the b2b_key in the to_tag */
247-
b2b_key = b2b_generate_key(hash_index, dlg->id, timestamp);
248-
if(b2b_key == NULL)
249-
{
250-
if(!safe)
251-
B2BE_LOCK_RELEASE(table, hash_index);
252-
LM_ERR("Failed to generate b2b key\n");
253-
return NULL;
246+
if (!init_b2b_key) {
247+
/* if an insert in server_htable -> copy the b2b_key in the to_tag */
248+
b2b_key = b2b_generate_key(hash_index, dlg->id);
249+
if(b2b_key == NULL)
250+
{
251+
if(!safe)
252+
B2BE_LOCK_RELEASE(table, hash_index);
253+
LM_ERR("Failed to generate b2b key\n");
254+
return NULL;
255+
}
256+
} else {
257+
b2b_key = init_b2b_key;
254258
}
255259

256260
if(src == B2B_SERVER)
@@ -261,8 +265,7 @@ str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index,
261265
LM_ERR("No more shared memory\n");
262266
if(!safe)
263267
B2BE_LOCK_RELEASE(table, hash_index);
264-
pkg_free(b2b_key);
265-
return 0;
268+
goto err_free;
266269
}
267270
memcpy(dlg->tag[CALLEE_LEG].s, b2b_key->s, b2b_key->len);
268271
dlg->tag[CALLEE_LEG].len = b2b_key->len;
@@ -276,8 +279,7 @@ str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index,
276279
LM_ERR("Failed to insert into timer list\n");
277280
if(!safe)
278281
B2BE_LOCK_RELEASE(table, hash_index);
279-
pkg_free(b2b_key);
280-
return 0;
282+
goto err_free;
281283
}
282284
}
283285

@@ -288,13 +290,16 @@ str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index,
288290
B2BE_LOCK_RELEASE(table, hash_index);
289291

290292
return b2b_key;
293+
err_free:
294+
if (!init_b2b_key)
295+
pkg_free(b2b_key);
296+
return NULL;
291297
}
292298

293299
/* key format : B2B.hash_index.local_index.timestamp.random *
294300
*/
295301

296-
int b2b_parse_key(str* key, unsigned int* hash_index, unsigned int* local_index,
297-
uint64_t *timestamp)
302+
int b2b_parse_key(str* key, unsigned int* hash_index, unsigned int* local_index)
298303
{
299304
char* p;
300305
str s;
@@ -340,40 +345,20 @@ int b2b_parse_key(str* key, unsigned int* hash_index, unsigned int* local_index,
340345
return -1;
341346
}
342347

343-
if (timestamp) {
344-
s.s = p+1;
345-
// There might be a random number after timestamp, we want to ignore it.
346-
p= strchr(s.s, '.');
347-
if (p != NULL) {
348-
s.len= p - s.s;
349-
} else {
350-
s.len = key->len - (s.s - key->s);
351-
}
352-
if(str2int64(&s, timestamp) < 0)
353-
{
354-
LM_DBG("Could not extract timestamp [%.*s] from key [%.*s]\n", s.len, s.s, key->len, key->s);
355-
return -1;
356-
}
357-
358-
LM_DBG("hash_index = [%d] - local_index = [%d] - timestamp = %ld\n",
359-
*hash_index, *local_index, (time_t)*timestamp);
360-
} else {
361-
/* we do not really care about the third part of the key */
362-
LM_DBG("hash_index = [%d] - local_index= [%d]\n", *hash_index, *local_index);
363-
}
348+
/* we do not really care about the last parts of the key */
349+
LM_DBG("hash_index = [%d] - local_index= [%d]\n", *hash_index, *local_index);
364350

365351
return 0;
366352
}
367353

368-
str* b2b_generate_key(unsigned int hash_index, unsigned int local_index,
369-
time_t timestamp)
354+
str* b2b_generate_key(unsigned int hash_index, unsigned int local_index)
370355
{
371356
char buf[B2B_MAX_KEY_SIZE];
372357
str* b2b_key;
373358
int len;
374359

375360
len = sprintf(buf, "%s.%d.%d.%ld.%d", b2b_key_prefix.s, hash_index, local_index,
376-
timestamp ? timestamp : startup_time+get_ticks(), rand());
361+
startup_time+get_ticks(), rand());
377362

378363
b2b_key = (str*)pkg_malloc(sizeof(str)+ len);
379364
if(b2b_key== NULL)
@@ -959,7 +944,7 @@ int b2b_prescript_f(struct sip_msg *msg, void *uparam)
959944
b2b_key = to_tag;
960945
/* check if the to tag has the b2b key format -> meaning
961946
* that it is a server request */
962-
if(b2b_key.s && b2b_parse_key(&b2b_key, &hash_index, &local_index,NULL)>=0)
947+
if(b2b_key.s && b2b_parse_key(&b2b_key, &hash_index, &local_index)>=0)
963948
{
964949
LM_DBG("Received a b2b server request [%.*s]\n",
965950
msg->first_line.u.request.method.len,
@@ -971,7 +956,7 @@ int b2b_prescript_f(struct sip_msg *msg, void *uparam)
971956
/* check if the callid is in b2b format -> meaning
972957
* that this is a client request */
973958
b2b_key = msg->callid->body;
974-
if(b2b_parse_key(&b2b_key, &hash_index, &local_index, NULL) >= 0)
959+
if(b2b_parse_key(&b2b_key, &hash_index, &local_index) >= 0)
975960
{
976961
LM_DBG("received a b2b client request [%.*s]\n",
977962
msg->first_line.u.request.method.len,
@@ -1670,7 +1655,7 @@ int _b2b_send_reply(b2b_dlg_t* dlg, b2b_rpl_data_t* rpl_data)
16701655
}
16711656

16721657
/* parse the key and find the position in hash table */
1673-
if(b2b_parse_key(b2b_key, &hash_index, &local_index, NULL) < 0)
1658+
if(b2b_parse_key(b2b_key, &hash_index, &local_index) < 0)
16741659
{
16751660
LM_ERR("Wrong format for b2b key\n");
16761661
return -1;
@@ -1999,7 +1984,7 @@ void b2b_entity_delete(enum b2b_entity_type et, str* b2b_key,
19991984
table = client_htable;
20001985

20011986
/* parse the key and find the position in hash table */
2002-
if(b2b_parse_key(b2b_key, &hash_index, &local_index, NULL) < 0)
1987+
if(b2b_parse_key(b2b_key, &hash_index, &local_index) < 0)
20031988
{
20041989
LM_ERR("Wrong format for b2b key\n");
20051990
return;
@@ -2079,7 +2064,7 @@ int b2b_entity_exists(enum b2b_entity_type et, str* b2b_key)
20792064
table = client_htable;
20802065

20812066
/* parse the key and find the position in hash table */
2082-
if(b2b_parse_key(b2b_key, &hash_index, &local_index, NULL) < 0)
2067+
if(b2b_parse_key(b2b_key, &hash_index, &local_index) < 0)
20832068
{
20842069
LM_ERR("Wrong format for b2b key\n");
20852070
return 0;
@@ -2266,7 +2251,7 @@ int _b2b_send_request(b2b_dlg_t* dlg, b2b_req_data_t* req_data)
22662251
}
22672252

22682253
/* parse the key and find the position in hash table */
2269-
if(b2b_parse_key(b2b_key, &hash_index, &local_index, NULL) < 0)
2254+
if(b2b_parse_key(b2b_key, &hash_index, &local_index) < 0)
22702255
{
22712256
LM_ERR("Wrong format for b2b key [%.*s]\n", b2b_key->len, b2b_key->s);
22722257
return -1;
@@ -2834,7 +2819,7 @@ void b2b_tm_cback(struct cell *t, b2b_table htable, struct tmcb_params *ps)
28342819
msg = ps->rpl;
28352820
b2b_key = (str*)*ps->param;
28362821

2837-
if(b2b_parse_key(b2b_key, &hash_index, &local_index, NULL)< 0)
2822+
if(b2b_parse_key(b2b_key, &hash_index, &local_index)< 0)
28382823
{
28392824
LM_ERR("Failed to parse b2b logic key [%.*s]\n",b2b_key->len,b2b_key->s);
28402825
return;

0 commit comments

Comments
 (0)