Skip to content

Commit

Permalink
b2b_logic: add support for fetching entity info
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed May 13, 2022
1 parent 9ad05ce commit 513a9e6
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 25 deletions.
29 changes: 29 additions & 0 deletions modules/b2b_logic/b2b_load.h
Expand Up @@ -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);

Expand All @@ -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;
Expand Down
92 changes: 92 additions & 0 deletions modules/b2b_logic/b2b_logic.c
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
44 changes: 19 additions & 25 deletions modules/b2b_logic/b2b_logic_ctx.c
Expand Up @@ -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);
}
Expand All @@ -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;
}
10 changes: 10 additions & 0 deletions modules/b2b_logic/records.c
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions modules/b2b_logic/records.h
Expand Up @@ -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

0 comments on commit 513a9e6

Please sign in to comment.