diff --git a/modules/b2b_logic/b2b_load.h b/modules/b2b_logic/b2b_load.h index 7b42b230e21..60eaf12c28b 100644 --- a/modules/b2b_logic/b2b_load.h +++ b/modules/b2b_logic/b2b_load.h @@ -95,6 +95,32 @@ typedef int (*b2bl_register_cb_f)(str* key, b2bl_cback_f, void* param, unsigned typedef str *(*b2bl_get_key_f)(void); +struct b2b_entity_info_t { + str key; + str callid, fromtag, totag; +}; +/* + * Retrieves information about an entity + * Params: + * * key: the key of the b2b logic - if NULL, the current logic is retrieved + * * msg: message to retrieve the peer from + * * entity: + * 1: second entity according to the b2b logic + * 0: first entity according to the b2b logic + * -1: current entity involved (if exists) + * -2: current entity's peer (if exists) + * * info: structure to filled in with information + * + * Returns: + * * 0: structure has been properly filled in + * * -1: internal error + * * -2: no b2b logic tuple available + * * -3: no entity available + * * -4: bad parameters + */ +typedef int (*b2bl_get_entity_info_f)(str *key, struct sip_msg *msg, int entity, struct b2b_entity_info_t *info); +typedef void (*b2bl_release_entity_info_f)(struct b2b_entity_info_t *info); + typedef struct b2b_tracer* (*b2bl_set_tracer_f)(void); typedef int (*b2bl_register_set_tracer_cb_f)(b2bl_set_tracer_f cb, unsigned int msg_flag_filter); @@ -114,6 +140,9 @@ typedef struct b2bl_api b2bl_register_set_tracer_cb_f register_set_tracer_cb; b2bl_restore_upper_info_f restore_upper_info; + b2bl_get_entity_info_f get_entity_info; + b2bl_release_entity_info_f release_entity_info; + b2bl_ctx_register_int_f ctx_register_int; b2bl_ctx_register_str_f ctx_register_str; b2bl_ctx_register_ptr_f ctx_register_ptr; diff --git a/modules/b2b_logic/b2b_logic.c b/modules/b2b_logic/b2b_logic.c index 89ffb4b199d..97bd8f73e72 100644 --- a/modules/b2b_logic/b2b_logic.c +++ b/modules/b2b_logic/b2b_logic.c @@ -2226,6 +2226,95 @@ static str *b2bl_get_key(void) return &ret; } +static int b2bl_get_entity_info(str *key, struct sip_msg *msg, int entity, struct b2b_entity_info_t *info) +{ + str callid; + b2bl_tuple_t *tuple; + b2bl_entity_id_t *bentity = NULL; + + if (!info) + return -4; + memset(info, 0, sizeof *info); + if (entity < -2 || entity > 1) + return -4; + + if (key) + tuple = b2bl_get_tuple(key); + else + tuple = get_ctx_tuple(); + if (!tuple) + return -2; + + if (entity < 0) { + if (cur_route_ctx.flags & (B2BL_RT_REQ_CTX|B2BL_RT_RPL_CTX)) { + if (tuple->bridge_entities[0]) { + if (!str_strcmp(&cur_route_ctx.entity_key, + &tuple->bridge_entities[0]->key)) + bentity = tuple->bridge_entities[(entity == -2?1:0)]; + } + if (!bentity && tuple->bridge_entities[1]) { + if (!str_strcmp(&cur_route_ctx.entity_key, + &tuple->bridge_entities[1]->key)) + bentity = tuple->bridge_entities[(entity == -2?0:1)]; + } + } else { + if (!msg) + return -2; + if (get_callid(msg, &callid) < 0) { + LM_ERR("Failed to get callid from SIP message\n"); + return -1; + } + if (tuple->bridge_entities[0] && tuple->bridge_entities[0]->dlginfo) { + if (!str_strcmp(&callid, + &tuple->bridge_entities[0]->dlginfo->callid)) + bentity = tuple->bridge_entities[(entity == -2?1:0)]; + } + if (!bentity && tuple->bridge_entities[1] && tuple->bridge_entities[1]->dlginfo) { + if (!str_strcmp(&callid, + &tuple->bridge_entities[1]->dlginfo->callid)) + bentity = tuple->bridge_entities[(entity == -2?0:1)]; + } + } + /* if we still don't have an entity, most likely we are in a + * client_new callback, with a single server entity */ + if (entity == -2) + bentity = tuple->servers[0]; + + } else { + bentity = tuple->bridge_entities[entity]; + } + if (!bentity) + return -3; + if (bentity->key.len && pkg_str_dup(&info->key, &bentity->key) < 0) + return -1; + if (bentity->dlginfo) { + if (bentity->dlginfo->callid.len + && pkg_str_dup(&info->callid, &bentity->dlginfo->callid) < 0) + return -1; + if (bentity->dlginfo->fromtag.len + && pkg_str_dup(&info->fromtag, &bentity->dlginfo->fromtag) < 0) + return -1; + if (bentity->dlginfo->totag.len + && pkg_str_dup(&info->totag, &bentity->dlginfo->totag) < 0) + return -1; + } + return 0; +} + +static void b2bl_release_entity_info(struct b2b_entity_info_t *info) +{ + if (!info) + return; + if (info->key.s) + pkg_free(info->key.s); + if (info->callid.s) + pkg_free(info->callid.s); + if (info->fromtag.s) + pkg_free(info->fromtag.s); + if (info->totag.s) + pkg_free(info->totag.s); +} + int b2b_logic_bind(b2bl_api_t* api) { @@ -2245,6 +2334,9 @@ int b2b_logic_bind(b2bl_api_t* api) api->register_set_tracer_cb = b2bl_register_set_tracer_cb; api->restore_upper_info = b2bl_restore_upper_info; + api->get_entity_info = b2bl_get_entity_info; + api->release_entity_info = b2bl_release_entity_info; + api->ctx_register_int = b2bl_ctx_register_int; api->ctx_register_str = b2bl_ctx_register_str; api->ctx_register_ptr = b2bl_ctx_register_ptr; diff --git a/modules/b2b_logic/b2b_logic_ctx.c b/modules/b2b_logic/b2b_logic_ctx.c index 995bd48494e..0267d1844ee 100644 --- a/modules/b2b_logic/b2b_logic_ctx.c +++ b/modules/b2b_logic/b2b_logic_ctx.c @@ -21,23 +21,17 @@ #include "records.h" #include "b2b_logic_ctx.h" -static b2bl_tuple_t *b2bl_get_tuple(str *key) +static b2bl_tuple_t *b2bl_ctx_get_tuple(str *key) { - b2bl_tuple_t *tuple; - unsigned int hash_index, local_index; - if (b2bl_parse_key(key, &hash_index, &local_index) < 0) - goto error; - tuple = b2bl_search_tuple_safe(hash_index, local_index); - if (!tuple) - goto error; + b2bl_tuple_t *tuple = b2bl_get_tuple(key); + if (!tuple) { + LM_BUG("could not find logic tuple [%.*s]\n", key->len, key->s); + abort(); + } return tuple; -error: - LM_BUG("could not find logic tuple [%.*s]\n", key->len, key->s); - abort(); - return NULL; } -static void b2bl_release_tuple(b2bl_tuple_t *tuple) +static void b2bl_ctx_release_tuple(b2bl_tuple_t *tuple) { lock_release(&b2bl_htable[tuple->hash_index].lock); } @@ -60,48 +54,48 @@ int b2bl_ctx_register_ptr(context_destroy_f f) void b2bl_ctx_put_int(str *key, int pos, int data) { - b2bl_tuple_t *tuple = b2bl_get_tuple(key); + b2bl_tuple_t *tuple = b2bl_ctx_get_tuple(key); context_put_int(CONTEXT_B2B_LOGIC, context_of(tuple), pos, data); - b2bl_release_tuple(tuple); + b2bl_ctx_release_tuple(tuple); } void b2bl_ctx_put_str(str *key, int pos, str *data) { - b2bl_tuple_t *tuple = b2bl_get_tuple(key); + b2bl_tuple_t *tuple = b2bl_ctx_get_tuple(key); context_put_str(CONTEXT_B2B_LOGIC, context_of(tuple), pos, data); - b2bl_release_tuple(tuple); + b2bl_ctx_release_tuple(tuple); } void b2bl_ctx_put_ptr(str *key, int pos, void *data) { - b2bl_tuple_t *tuple = b2bl_get_tuple(key); + b2bl_tuple_t *tuple = b2bl_ctx_get_tuple(key); context_put_ptr(CONTEXT_B2B_LOGIC, context_of(tuple), pos, data); - b2bl_release_tuple(tuple); + b2bl_ctx_release_tuple(tuple); } int b2bl_ctx_get_int(str *key, int pos) { int ret; - b2bl_tuple_t *tuple = b2bl_get_tuple(key); + b2bl_tuple_t *tuple = b2bl_ctx_get_tuple(key); ret = context_get_int(CONTEXT_B2B_LOGIC, context_of(tuple), pos); - b2bl_release_tuple(tuple); + b2bl_ctx_release_tuple(tuple); return ret; } str *b2bl_ctx_get_str(str *key, int pos) { str *ret; - b2bl_tuple_t *tuple = b2bl_get_tuple(key); + b2bl_tuple_t *tuple = b2bl_ctx_get_tuple(key); ret = context_get_str(CONTEXT_B2B_LOGIC, context_of(tuple), pos); - b2bl_release_tuple(tuple); + b2bl_ctx_release_tuple(tuple); return ret; } void *b2bl_ctx_get_ptr(str *key, int pos) { void *ret; - b2bl_tuple_t *tuple = b2bl_get_tuple(key); + b2bl_tuple_t *tuple = b2bl_ctx_get_tuple(key); ret = context_get_ptr(CONTEXT_B2B_LOGIC, context_of(tuple), pos); - b2bl_release_tuple(tuple); + b2bl_ctx_release_tuple(tuple); return ret; } diff --git a/modules/b2b_logic/records.c b/modules/b2b_logic/records.c index 6ff0a5d8f60..04431e4e556 100644 --- a/modules/b2b_logic/records.c +++ b/modules/b2b_logic/records.c @@ -1016,3 +1016,13 @@ int b2b_extra_headers(struct sip_msg* msg, str* b2bl_key, str* custom_hdrs, return 0; } + +b2bl_tuple_t *b2bl_get_tuple(str *key) +{ + unsigned int hash_index, local_index; + if (b2bl_parse_key(key, &hash_index, &local_index) < 0) + goto error; + return b2bl_search_tuple_safe(hash_index, local_index); +error: + return NULL; +} diff --git a/modules/b2b_logic/records.h b/modules/b2b_logic/records.h index 095070ccb4e..a458bf516c9 100644 --- a/modules/b2b_logic/records.h +++ b/modules/b2b_logic/records.h @@ -235,4 +235,6 @@ int b2bl_register_new_tuple_cb(b2bl_cback_f f, void *param); int b2bl_run_new_tuple_cb(str *key); +b2bl_tuple_t *b2bl_get_tuple(str *key); + #endif