Skip to content

Commit

Permalink
b2b_entities: fix incorrect entity key after loading from DB
Browse files Browse the repository at this point in the history
Do not generate a new random part for the entity key when loading from
DB.

(cherry picked from commit 1688087)
  • Loading branch information
rvlad-patrascu committed May 19, 2023
1 parent 99c9d66 commit 4c1b9a5
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 82 deletions.
8 changes: 4 additions & 4 deletions modules/b2b_entities/b2b_entities.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ int b2b_restore_logic_info(enum b2b_entity_type type, str* key,
{
table = client_htable;
}
if(b2b_parse_key(key, &hash_index, &local_index, NULL) < 0)
if(b2b_parse_key(key, &hash_index, &local_index) < 0)
{
LM_ERR("Wrong format for b2b key [%.*s]\n", key->len, key->s);
return -1;
Expand Down Expand Up @@ -707,7 +707,7 @@ int b2b_update_b2bl_param(enum b2b_entity_type type, str* key,
{
table = client_htable;
}
if(b2b_parse_key(key, &hash_index, &local_index, NULL) < 0)
if(b2b_parse_key(key, &hash_index, &local_index) < 0)
{
LM_ERR("Wrong format for b2b key [%.*s]\n", key->len, key->s);
return -1;
Expand Down Expand Up @@ -756,9 +756,9 @@ str *b2b_get_b2bl_key(str* callid, str* from_tag, str* to_tag, str* entity_key)
}
/* check if the to tag has the b2b key format
* -> meaning that it is a server request */
if(b2b_parse_key(to_tag, &hash_index, &local_index, NULL)>=0)
if(b2b_parse_key(to_tag, &hash_index, &local_index)>=0)
table = server_htable;
else if (b2b_parse_key(callid, &hash_index, &local_index, NULL)>=0)
else if (b2b_parse_key(callid, &hash_index, &local_index)>=0)
table = client_htable;
else
return NULL; /* to tag and/or callid are not part of this B2B */
Expand Down
31 changes: 14 additions & 17 deletions modules/b2b_entities/b2be_clustering.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,14 @@ static inline void unpack_update_fields(bin_packet_t *packet, b2b_dlg_t *dlg)
}

int receive_entity_create(bin_packet_t *packet, b2b_dlg_t *dlg, int type,
b2b_table htable, unsigned int hash_index, unsigned int local_index,
uint64_t timestamp)
b2b_table htable, unsigned int hash_index, unsigned int local_index)
{
b2b_dlg_t tmp_dlg, *new_dlg = NULL;
unsigned int h_idx, l_idx;
int rcv_type;
str *new_key;
str sock_str;
str b2be_key;
dlg_leg_t leg, *new_leg = NULL;
uint64_t ts;

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

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

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

hash_index = h_idx;
local_index = l_idx;
timestamp = ts;
dlg = &tmp_dlg;
type = rcv_type;
} else {
if (type == B2B_SERVER)
b2be_key = dlg->tag[1];
else
b2be_key = dlg->callid;
}

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

B2BE_LOCK_GET(htable, hash_index);

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

B2BE_LOCK_RELEASE(htable, hash_index);

pkg_free(new_key);

return 0;

error:
Expand Down Expand Up @@ -516,7 +514,6 @@ int receive_entity_update(bin_packet_t *packet)
int type;
str b2be_key;
b2b_table htable;
uint64_t timestamp;
int rc = 0;

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

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

if (packet->type == REPL_ENTITY_UPDATE)
return receive_entity_create(packet, &tmp_dlg, type, htable,
hash_index, local_index, timestamp);
hash_index, local_index);
else
return 0;
}
Expand Down Expand Up @@ -609,7 +606,7 @@ int receive_entity_delete(bin_packet_t *packet)
LM_DBG("Received replicated delete for entity [%.*s]\n",
b2be_key->len, b2be_key->s);

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

rc = receive_entity_create(pkt, NULL, B2B_NONE, NULL, 0, 0, 0);
rc = receive_entity_create(pkt, NULL, B2B_NONE, NULL, 0, 0);
break;
case REPL_ENTITY_UPDATE:
case REPL_ENTITY_PARAM_UPDATE:
Expand All @@ -665,7 +662,7 @@ void b2be_recv_bin_packets(bin_packet_t *pkt)
ensure_bin_version(pkt, B2BE_BIN_VERSION);

while (cl_api.sync_chunk_iter(pkt))
if (receive_entity_create(pkt, NULL, B2B_NONE, NULL, 0, 0, 0) < 0) {
if (receive_entity_create(pkt, NULL, B2B_NONE, NULL, 0, 0) < 0) {
LM_ERR("Failed to process sync packet\n");
return;
}
Expand Down
11 changes: 5 additions & 6 deletions modules/b2b_entities/b2be_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,6 @@ static int load_entity(int_str_t *vals)
int port, proto;
b2b_table htable;
int type;
uint64_t ts = 0;

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

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

if(type == B2B_SERVER)/* extract hash and local index */
{
b2b_key = &vals[2].s;
htable = server_htable;
if(b2b_parse_key(&dlg.tag[1], &hash_index, &local_index, &ts) < 0)
if(b2b_parse_key(&dlg.tag[1], &hash_index, &local_index) < 0)
{
LM_ERR("Wrong format for b2b key [%.*s]\n", dlg.tag[1].len, dlg.tag[1].s);
return -1;
Expand All @@ -631,9 +631,10 @@ static int load_entity(int_str_t *vals)
}
else
{
b2b_key = &vals[3].s;
htable = client_htable;

if(b2b_parse_key(&dlg.callid, &hash_index, &local_index, NULL) < 0)
if(b2b_parse_key(&dlg.callid, &hash_index, &local_index) < 0)
{
LM_ERR("Wrong format for b2b key [%.*s]\n", dlg.callid.len, dlg.callid.s);
return -1;
Expand Down Expand Up @@ -687,13 +688,11 @@ static int load_entity(int_str_t *vals)
LM_ERR("Failed to create new dialog structure\n");
return -1;
}
b2b_key= b2b_htable_insert(htable,shm_dlg,hash_index, ts, type, 1, 0, 0);
if(b2b_key == NULL)
if (!b2b_htable_insert(htable,shm_dlg,hash_index, b2b_key, type, 1, 0, 0))
{
LM_ERR("Failed to insert new record\n");
return -1;
}
pkg_free(b2b_key);

if (vals[14].s.len) {
if (shm_str_dup(&shm_dlg->storage, &vals[14].s) < 0) {
Expand Down
2 changes: 1 addition & 1 deletion modules/b2b_entities/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ str* _client_new(client_info_t* ci,b2b_notify_t b2b_cback,

/* callid must have the special format */
dlg->db_flag = NO_UPDATEDB_FLAG;
callid = b2b_htable_insert(client_htable, dlg, hash_index, 0, B2B_CLIENT, 0, 0,
callid = b2b_htable_insert(client_htable, dlg, hash_index, NULL, B2B_CLIENT, 0, 0,
init_params?init_params->timeout:0);
if(callid == NULL)
{
Expand Down
77 changes: 31 additions & 46 deletions modules/b2b_entities/dlg.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ b2b_dlg_t* b2b_search_htable(b2b_table table, unsigned int hash_index,

/* this is only called by server new */
str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index,
time_t timestamp, int src, int safe, int db_insert, unsigned int ua_timeout)
str *init_b2b_key, int src, int safe, int db_insert, unsigned int ua_timeout)
{
b2b_dlg_t * it, *prev_it= NULL;
str* b2b_key;
Expand All @@ -243,14 +243,18 @@ str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index,
prev_it->next = dlg;
dlg->prev = prev_it;
}
/* if an insert in server_htable -> copy the b2b_key in the to_tag */
b2b_key = b2b_generate_key(hash_index, dlg->id, timestamp);
if(b2b_key == NULL)
{
if(!safe)
B2BE_LOCK_RELEASE(table, hash_index);
LM_ERR("Failed to generate b2b key\n");
return NULL;
if (!init_b2b_key) {
/* if an insert in server_htable -> copy the b2b_key in the to_tag */
b2b_key = b2b_generate_key(hash_index, dlg->id);
if(b2b_key == NULL)
{
if(!safe)
B2BE_LOCK_RELEASE(table, hash_index);
LM_ERR("Failed to generate b2b key\n");
return NULL;
}
} else {
b2b_key = init_b2b_key;
}

if(src == B2B_SERVER)
Expand All @@ -261,8 +265,7 @@ str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index,
LM_ERR("No more shared memory\n");
if(!safe)
B2BE_LOCK_RELEASE(table, hash_index);
pkg_free(b2b_key);
return 0;
goto err_free;
}
memcpy(dlg->tag[CALLEE_LEG].s, b2b_key->s, b2b_key->len);
dlg->tag[CALLEE_LEG].len = b2b_key->len;
Expand All @@ -276,8 +279,7 @@ str* b2b_htable_insert(b2b_table table, b2b_dlg_t* dlg, int hash_index,
LM_ERR("Failed to insert into timer list\n");
if(!safe)
B2BE_LOCK_RELEASE(table, hash_index);
pkg_free(b2b_key);
return 0;
goto err_free;
}
}

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

return b2b_key;
err_free:
if (!init_b2b_key)
pkg_free(b2b_key);
return NULL;
}

/* key format : B2B.hash_index.local_index.timestamp.random *
*/

int b2b_parse_key(str* key, unsigned int* hash_index, unsigned int* local_index,
uint64_t *timestamp)
int b2b_parse_key(str* key, unsigned int* hash_index, unsigned int* local_index)
{
char* p;
str s;
Expand Down Expand Up @@ -340,40 +345,20 @@ int b2b_parse_key(str* key, unsigned int* hash_index, unsigned int* local_index,
return -1;
}

if (timestamp) {
s.s = p+1;
// There might be a random number after timestamp, we want to ignore it.
p= strchr(s.s, '.');
if (p != NULL) {
s.len= p - s.s;
} else {
s.len = key->len - (s.s - key->s);
}
if(str2int64(&s, timestamp) < 0)
{
LM_DBG("Could not extract timestamp [%.*s] from key [%.*s]\n", s.len, s.s, key->len, key->s);
return -1;
}

LM_DBG("hash_index = [%d] - local_index = [%d] - timestamp = %ld\n",
*hash_index, *local_index, (time_t)*timestamp);
} else {
/* we do not really care about the third part of the key */
LM_DBG("hash_index = [%d] - local_index= [%d]\n", *hash_index, *local_index);
}
/* we do not really care about the last parts of the key */
LM_DBG("hash_index = [%d] - local_index= [%d]\n", *hash_index, *local_index);

return 0;
}

str* b2b_generate_key(unsigned int hash_index, unsigned int local_index,
time_t timestamp)
str* b2b_generate_key(unsigned int hash_index, unsigned int local_index)
{
char buf[B2B_MAX_KEY_SIZE];
str* b2b_key;
int len;

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

b2b_key = (str*)pkg_malloc(sizeof(str)+ len);
if(b2b_key== NULL)
Expand Down Expand Up @@ -959,7 +944,7 @@ int b2b_prescript_f(struct sip_msg *msg, void *uparam)
b2b_key = to_tag;
/* check if the to tag has the b2b key format -> meaning
* that it is a server request */
if(b2b_key.s && b2b_parse_key(&b2b_key, &hash_index, &local_index,NULL)>=0)
if(b2b_key.s && b2b_parse_key(&b2b_key, &hash_index, &local_index)>=0)
{
LM_DBG("Received a b2b server request [%.*s]\n",
msg->first_line.u.request.method.len,
Expand All @@ -971,7 +956,7 @@ int b2b_prescript_f(struct sip_msg *msg, void *uparam)
/* check if the callid is in b2b format -> meaning
* that this is a client request */
b2b_key = msg->callid->body;
if(b2b_parse_key(&b2b_key, &hash_index, &local_index, NULL) >= 0)
if(b2b_parse_key(&b2b_key, &hash_index, &local_index) >= 0)
{
LM_DBG("received a b2b client request [%.*s]\n",
msg->first_line.u.request.method.len,
Expand Down Expand Up @@ -1670,7 +1655,7 @@ int _b2b_send_reply(b2b_dlg_t* dlg, b2b_rpl_data_t* rpl_data)
}

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

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

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

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

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

0 comments on commit 4c1b9a5

Please sign in to comment.