Skip to content

Commit

Permalink
b2b_entities: add support for custom params in callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Feb 24, 2022
1 parent 6b49736 commit a1e74ef
Show file tree
Hide file tree
Showing 26 changed files with 299 additions and 295 deletions.
2 changes: 0 additions & 2 deletions modules/b2b_entities/b2b_common.h
Expand Up @@ -27,8 +27,6 @@
#ifndef _B2B_COMMON_H_
#define _B2B_COMMON_H_

#define B2BL_MAX_KEY_LEN 21

struct b2b_context {
str b2bl_key;
void *data;
Expand Down
57 changes: 26 additions & 31 deletions modules/b2b_entities/b2b_entities.c
Expand Up @@ -478,7 +478,7 @@ static void mod_destroy(void)
}

int b2b_restore_logic_info(enum b2b_entity_type type, str* key,
b2b_notify_t cback)
b2b_notify_t cback, void *param, b2b_param_free_cb free_param)
{
b2b_dlg_t* dlg;
b2b_table table;
Expand Down Expand Up @@ -510,26 +510,22 @@ int b2b_restore_logic_info(enum b2b_entity_type type, str* key,
return -1;
}
dlg->b2b_cback = cback;
dlg->param = param;
dlg->free_param = free_param;
return 0;
}

int b2b_update_b2bl_param(enum b2b_entity_type type, str* key,
str* param, int replicate)
str* logic_key, int replicate)
{
b2b_dlg_t* dlg;
b2b_table table;
unsigned int hash_index, local_index;
int unlock = 1;

if(!param)
if(!logic_key)
{
LM_ERR("NULL param\n");
return -1;
}
if(param->len > B2BL_MAX_KEY_LEN)
{
LM_ERR("parameter too long, received [%d], maximum [%d]\n",
param->len, B2BL_MAX_KEY_LEN);
LM_ERR("NULL logic_key\n");
return -1;
}

Expand Down Expand Up @@ -559,39 +555,35 @@ int b2b_update_b2bl_param(enum b2b_entity_type type, str* key,
lock_release(&table[hash_index].lock);
return -1;
}
memcpy(dlg->param.s, param->s, param->len);
dlg->param.len = param->len;
memcpy(dlg->logic_key.s, logic_key->s, logic_key->len);
dlg->logic_key.len = logic_key->len;
if (unlock)
lock_release(&table[hash_index].lock);

if (b2be_cluster && replicate)
replicate_entity_update(dlg, type, hash_index, param, -1, NULL);
replicate_entity_update(dlg, type, hash_index, logic_key, -1, NULL);

return 0;
}

int b2b_get_b2bl_key(str* callid, str* from_tag, str* to_tag, str* entity_key, str* tuple_key)
str *b2b_get_b2bl_key(str* callid, str* from_tag, str* to_tag, str* entity_key)
{
b2b_dlg_t* dlg;
unsigned int hash_index, local_index;
b2b_table table;
int ret;
str *tuple_key = NULL;

if(!callid || !callid->s || !callid->len){
LM_ERR("Wrong callid param\n");
return -1;
return NULL;
}
if(!from_tag || !from_tag->s || !from_tag->len){
LM_ERR("Wrong from_tag param\n");
return -1;
return NULL;
}
if(!to_tag){
LM_ERR("Wrong to_tag param\n");
return -1;
}
if(!tuple_key || !tuple_key->s || tuple_key->len<B2BL_MAX_KEY_LEN) {
LM_ERR("Wrong tuple param\n");
return -1;
return NULL;
}
/* check if the to tag has the b2b key format
* -> meaning that it is a server request */
Expand All @@ -600,13 +592,19 @@ int b2b_get_b2bl_key(str* callid, str* from_tag, str* to_tag, str* entity_key, s
else if (b2b_parse_key(callid, &hash_index, &local_index, NULL)>=0)
table = client_htable;
else
return -1; /* to tag and/or callid are not part of this B2B */
return NULL; /* to tag and/or callid are not part of this B2B */
lock_get(&table[hash_index].lock);
dlg=b2b_search_htable_dlg(table, hash_index, local_index,
to_tag, from_tag, callid);
if(dlg){
memcpy(tuple_key->s, dlg->param.s, dlg->param.len);
tuple_key->len = dlg->param.len;
tuple_key = pkg_malloc(sizeof(str) + dlg->logic_key.len);
if (!tuple_key) {
LM_ERR("cannot duplicate logic\n");
return NULL;
}
tuple_key->s = (char *)(tuple_key + 1);
memcpy(tuple_key->s, dlg->logic_key.s, dlg->logic_key.len);
tuple_key->len = dlg->logic_key.len;
if (entity_key) {
if (table == server_htable) {
entity_key->s = to_tag->s;
Expand All @@ -620,12 +618,9 @@ int b2b_get_b2bl_key(str* callid, str* from_tag, str* to_tag, str* entity_key, s
tuple_key->len, tuple_key->s,
(entity_key?entity_key->len:0),
(entity_key?entity_key->s:NULL));
ret = 0;
} else {
ret = -1;
}
lock_release(&table[hash_index].lock);
return ret;
return tuple_key;
}

void *b2b_get_context(void)
Expand Down Expand Up @@ -700,10 +695,10 @@ static inline int mi_print_b2be_dlg(mi_item_t *resp_arr, b2b_table htable, unsig
if (add_mi_number(arr_item, MI_SSTR("dlg"), dlg->id) < 0)
goto error;
/* check if param is printable */
param = dlg->param;
param = dlg->logic_key;
if (!str_check_token(&param))
init_str(&param, "");
if (add_mi_string(arr_item, MI_SSTR("param"),
if (add_mi_string(arr_item, MI_SSTR("logic_key"),
param.s, param.len) < 0)
goto error;
if (add_mi_string(arr_item, MI_SSTR("mod_name"),
Expand Down
16 changes: 6 additions & 10 deletions modules/b2b_entities/b2be_clustering.c
Expand Up @@ -97,7 +97,7 @@ void bin_pack_entity(bin_packet_t *packet, b2b_dlg_t *dlg, int etype)
bin_push_str(packet, &dlg->route_set[1]);
bin_push_str(packet, dlg->send_sock ?
get_socket_internal_name(dlg->send_sock) : NULL);
bin_push_str(packet, &dlg->param);
bin_push_str(packet, &dlg->logic_key);
bin_push_str(packet, &dlg->mod_name);

bin_push_int(packet, dlg->state);
Expand Down Expand Up @@ -428,7 +428,7 @@ int receive_entity_create(bin_packet_t *packet, b2b_dlg_t *dlg, int type,
return -1;
}

bin_pop_str(packet, &dlg->param);
bin_pop_str(packet, &dlg->logic_key);
bin_pop_str(packet, &dlg->mod_name);

bin_pop_int(packet, &dlg->state);
Expand Down Expand Up @@ -497,17 +497,13 @@ int receive_entity_create(bin_packet_t *packet, b2b_dlg_t *dlg, int type,

static inline int recv_b2bl_param_update(bin_packet_t *packet, b2b_dlg_t *dlg)
{
str param;
str logic_key;

bin_pop_str(packet, &param);

if (param.len > B2BL_MAX_KEY_LEN) {
LM_ERR("b2bl parameter too long, received [%d], maximum [%d]\n",
param.len, B2BL_MAX_KEY_LEN);
bin_pop_str(packet, &logic_key);
if (shm_str_sync(&dlg->logic_key, &logic_key) < 0) {
LM_ERR("cannot duplicate logic key!\n");
return -1;
}
memcpy(dlg->param.s, param.s, param.len);
dlg->param.len = param.len;

return 0;
}
Expand Down
14 changes: 7 additions & 7 deletions modules/b2b_entities/b2be_db.c
Expand Up @@ -258,11 +258,11 @@ int b2be_db_insert(b2b_dlg_t* dlg, int type)
qvals[11].val.str_val.s = 0;
qvals[11].val.str_val.len = 0;
}
if (!str_check_token(&dlg->param)) {
if (!str_check_token(&dlg->logic_key)) {
qvals[12].val.str_val.s = NULL;
qvals[12].val.str_val.len = 0;
} else {
qvals[12].val.str_val = dlg->param;
qvals[12].val.str_val = dlg->logic_key;
}
qvals[13].val.str_val= dlg->mod_name;

Expand Down Expand Up @@ -327,10 +327,10 @@ static void b2b_entity_cdb_delete(int type, b2b_dlg_t* dlg)
return;
}

if (!str_check_token(&dlg->param)) {
if (!str_check_token(&dlg->logic_key)) {
cdb_subkey = NULL;
} else {
cdb_subkey = get_b2be_map_subkey(&dlg->param);
cdb_subkey = get_b2be_map_subkey(&dlg->logic_key);
if (!cdb_subkey) {
LM_ERR("Failed to build map key\n");
return;
Expand Down Expand Up @@ -503,11 +503,11 @@ void store_b2b_dlg(b2b_table htable, unsigned int hsize, int type, int no_lock)
qvals[11].val.str_val.s = 0;
qvals[11].val.str_val.len = 0;
}
if (!str_check_token(&dlg->param)) {
if (!str_check_token(&dlg->logic_key)) {
qvals[12].val.str_val.s = NULL;
qvals[12].val.str_val.len = 0;
} else {
qvals[12].val.str_val = dlg->param;
qvals[12].val.str_val = dlg->logic_key;
}
qvals[13].val.str_val= dlg->mod_name;
}
Expand Down Expand Up @@ -662,7 +662,7 @@ static int load_entity(int_str_t *vals)
dlg.last_method = vals[18].i;
dlg.last_reply_code = vals[19].i;
dlg.last_invite_cseq = vals[20].i;
dlg.param = vals[12].s;
dlg.logic_key = vals[12].s;
dlg.mod_name = vals[13].s;
sockinfo_str = vals[11].s;
if(sockinfo_str.s)
Expand Down
20 changes: 11 additions & 9 deletions modules/b2b_entities/b2be_load.h
Expand Up @@ -120,17 +120,19 @@ typedef void (*b2b_cb_t)(enum b2b_entity_type entity_type, str* entity_key,
str *param, enum b2b_event_type event_type, bin_packet_t *storage,
int backend);

typedef int (*b2b_notify_t)(struct sip_msg* msg, str* key, int type, void* param,
int flags);
typedef int (*b2b_notify_t)(struct sip_msg* msg, str* key, int type,
str *logic_key, void* param, int flags);
typedef int (*b2b_add_dlginfo_t)(str* key, str* entity_key, int src,
b2b_dlginfo_t* info);
b2b_dlginfo_t* info, void *);


typedef void (*b2b_param_free_cb)(void *param);
typedef str* (*b2b_server_new_t) (struct sip_msg* , str* local_contact,
b2b_notify_t , str *mod_name, str* param, struct b2b_tracer *tracer);
b2b_notify_t , str *mod_name, str* logic_key, struct b2b_tracer *tracer,
void *param, b2b_param_free_cb free_param);
typedef str* (*b2b_client_new_t) (client_info_t* , b2b_notify_t b2b_cback,
b2b_add_dlginfo_t add_dlginfo_f, str *mod_name, str* param,
struct b2b_tracer *tracer);
b2b_add_dlginfo_t add_dlginfo_f, str *mod_name, str* logic_key,
struct b2b_tracer *tracer, void *param, b2b_param_free_cb free_param);

typedef int (*b2b_send_request_t)(b2b_req_data_t*);
typedef int (*b2b_send_reply_t)(b2b_rpl_data_t*);
Expand All @@ -141,14 +143,14 @@ typedef int (*b2b_entity_exists_t)(enum b2b_entity_type et, str* b2b_key);
typedef void (*b2b_db_delete_t)(str param);

typedef int (*b2b_restore_linfo_t)(enum b2b_entity_type type, str* key,
b2b_notify_t cback);
b2b_notify_t cback, void *param, b2b_param_free_cb free_param);

typedef int (*b2b_reg_cb_t) (b2b_cb_t cb, int cb_type, str *mod_name);

typedef int (*b2b_update_b2bl_param_t)(enum b2b_entity_type type, str* key,
str* param, int replicate);
typedef int (*b2b_get_b2bl_key_t)(str* callid, str* from_tag, str* to_tag,
str* entity_key, str* tuple_key);
typedef str *(*b2b_get_b2bl_key_t)(str* callid, str* from_tag, str* to_tag,
str* entity_key);

typedef int (*b2b_apply_lumps_t)(struct sip_msg* msg);

Expand Down
37 changes: 17 additions & 20 deletions modules/b2b_entities/client.c
Expand Up @@ -72,14 +72,17 @@ static void generate_tag(str* tag, str* src, str* callid)
* from_uri: the source URI
* extra_headers: the extra headers to be added in the request
* b2b_cback : callback function to notify the logic about a change in dialog
* param : the parameter that will be used when calling b2b_cback function
* logic_key : the logic identifier
* tracer : structure used to instruct how the client should be traced
* param : optional, the parameter that will be used when calling b2b_cback function
* free_param: an optional function to free the parameter
*
* Return value: dialog key allocated in private memory
* */
#define HASH_SIZE 1<<23
str* client_new(client_info_t* ci,b2b_notify_t b2b_cback,
b2b_add_dlginfo_t add_dlginfo, str *mod_name, str* param,
struct b2b_tracer *tracer)
b2b_add_dlginfo_t add_dlginfo, str *mod_name, str* logic_key,
struct b2b_tracer *tracer, void *param, b2b_param_free_cb free_param)
{
int result;
b2b_dlg_t* dlg;
Expand All @@ -92,17 +95,11 @@ str* client_new(client_info_t* ci,b2b_notify_t b2b_cback,
str from_tag;
str random_info = {0, 0};

if(ci == NULL || b2b_cback == NULL || param== NULL)
if(ci == NULL || b2b_cback == NULL || logic_key == NULL)
{
LM_ERR("Wrong parameters.\n");
return NULL;
}
if(param && param->len > B2BL_MAX_KEY_LEN)
{
LM_ERR("parameter too long, received [%d], maximum [%d]\n",
param->len, B2BL_MAX_KEY_LEN);
return 0;
}

hash_index = core_hash(&ci->from_uri, &ci->to_uri, client_hsize);

Expand All @@ -115,7 +112,7 @@ str* client_new(client_info_t* ci,b2b_notify_t b2b_cback,
size = sizeof(b2b_dlg_t) + ci->to_uri.len + ci->from_uri.len
+ ci->from_dname.len + ci->to_dname.len +
from_tag.len + ci->local_contact.len + B2B_MAX_KEY_SIZE +
B2BL_MAX_KEY_LEN + mod_name->len;
mod_name->len;

/* create record in hash table */
dlg = (b2b_dlg_t*)shm_malloc(size);
Expand All @@ -136,14 +133,13 @@ str* client_new(client_info_t* ci,b2b_notify_t b2b_cback,
CONT_COPY(dlg, dlg->tag[CALLER_LEG], from_tag);
CONT_COPY(dlg, dlg->contact[CALLER_LEG], ci->local_contact);

if(param && param->s)
{
dlg->param.s = (char*)dlg + size;
memcpy(dlg->param.s, param->s, param->len);
dlg->param.len = param->len;
size+= B2BL_MAX_KEY_LEN;
if(logic_key->s && shm_str_dup(&dlg->logic_key, logic_key) < 0) {
LM_ERR("not enough shm memory\n");
goto error;
}
dlg->b2b_cback = b2b_cback;
dlg->param = param;
dlg->free_param = free_param;
dlg->add_dlginfo = add_dlginfo;
dlg->tracer = tracer;

Expand All @@ -152,7 +148,6 @@ str* client_new(client_info_t* ci,b2b_notify_t b2b_cback,
if(parse_method(ci->method.s, ci->method.s+ci->method.len, &dlg->last_method) == 0)
{
LM_ERR("wrong method %.*s\n", ci->method.len, ci->method.s);
shm_free(dlg);
goto error;
}
dlg->state = B2B_NEW;
Expand All @@ -170,7 +165,6 @@ str* client_new(client_info_t* ci,b2b_notify_t b2b_cback,
if(callid == NULL)
{
LM_ERR("Inserting new record in hash table failed\n");
shm_free(dlg);
goto error;
}

Expand Down Expand Up @@ -257,11 +251,14 @@ str* client_new(client_info_t* ci,b2b_notify_t b2b_cback,
" last method=[%d] dlg->uac_tran=[%p]\n",
dlg, callid->len, callid->s,
dlg->tag[CALLER_LEG].len, dlg->tag[CALLER_LEG].s,
dlg->param.len, dlg->param.s, dlg->last_method, dlg->uac_tran);
dlg->logic_key.len, dlg->logic_key.s, dlg->last_method, dlg->uac_tran);

return callid;

error:
if (dlg->logic_key.s)
shm_free(dlg->logic_key.s);
shm_free(dlg);
if(callid)
pkg_free(callid);
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion modules/b2b_entities/client.h
Expand Up @@ -35,7 +35,7 @@

str* client_new(client_info_t* ci, b2b_notify_t b2b_cback,
b2b_add_dlginfo_t add_dlginfo, str *mod_name, str* param,
struct b2b_tracer *tracer);
struct b2b_tracer*, void *, b2b_param_free_cb);

void b2b_client_tm_cback( struct cell *t, int type, struct tmcb_params *ps);

Expand Down

0 comments on commit a1e74ef

Please sign in to comment.