Skip to content

Commit

Permalink
pv_headers: rearrange module parameters
Browse files Browse the repository at this point in the history
* pvh_ prefix for global variables
  • Loading branch information
linuxmaniac committed Dec 7, 2023
1 parent 890dc5f commit 964c21e
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 230 deletions.
143 changes: 73 additions & 70 deletions src/modules/pv_headers/pv_headers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* pv_headers
*
* Copyright (C)
* 2020 Victor Seva <vseva@sipwise.com>
* 2020-2023 Victor Seva <vseva@sipwise.com>
* 2018 Kirill Solomko <ksolomko@sipwise.com>
*
* This file is part of Kamailio, a free SIP server.
Expand Down Expand Up @@ -39,27 +39,29 @@ MODULE_VERSION
#define MODULE_NAME "pv_headers"
#define XAVP_NAME "headers"

uac_api_t uac;
static tm_api_t tmb;

str xavi_name = str_init(XAVP_NAME);
str xavi_helper_xname = str_init("modparam_pv_headers");
str xavi_parsed_xname = str_init("parsed_pv_headers");
unsigned int header_name_size = 255;
unsigned int header_value_size = 1024;
int FL_PV_HDRS_COLLECTED = 27;
int FL_PV_HDRS_APPLIED = 28;
static str skip_headers_param =
str_init("Record-Route,Via,Route,Content-Length,Max-Forwards,CSeq");
static str split_headers_param = STR_NULL;
static str single_headers_param = str_init("");
static int auto_msg_param = 1;

str _hdr_from = {"From", 4};
str _hdr_to = {"To", 2};
str _hdr_reply_reason = {"@Reply-Reason", 13};
int _branch = T_BR_UNDEFINED;
int _reply_counter = -1;
uac_api_t pvh_uac;
static tm_api_t pvh_tmb;

_pvh_params_t _pvh_params = {
.xavi_name = str_init(XAVP_NAME),
.xavi_helper_xname = str_init("modparam_pv_headers"),
.xavi_parsed_xname = str_init("parsed_pv_headers"),
.hdr_value_size = 1024,
.flags[PVH_HDRS_COLLECTED] = 27,
.flags[PVH_HDRS_APPLIED] = 28,
.skip_hdrs = str_init(
"Record-Route,Via,Route,Content-Length,Max-Forwards,CSeq"),
.split_hdrs = STR_NULL,
.single_hdrs = str_init(""),
.auto_msg = 1,
};

unsigned int pvh_hdr_name_size = 255;
str pvh_hdr_from = {"From", 4};
str pvh_hdr_to = {"To", 2};
str pvh_hdr_reply_reason = {"@Reply-Reason", 13};
int pvh_branch = T_BR_UNDEFINED;
int pvh_reply_counter = -1;

static void mod_destroy(void);
static int mod_init(void);
Expand All @@ -80,9 +82,9 @@ static int pvh_get_branch_index(struct sip_msg *msg, int *br_idx)
{
int os = 0;
int len = 0;
char parsed_br_idx[header_value_size];
char parsed_br_idx[_pvh_params.hdr_value_size];

if(msg->add_to_branch_len > header_value_size) {
if(msg->add_to_branch_len > _pvh_params.hdr_value_size) {
LM_ERR("branch name is too long\n");
return -1;
}
Expand All @@ -106,10 +108,10 @@ static int w_pvh_collect_headers(struct sip_msg *msg, char *p1, char *p2)
{
sr_xavp_t **backup_xavis = NULL;

if(pvh_get_branch_index(msg, &_branch) < 0)
if(pvh_get_branch_index(msg, &pvh_branch) < 0)
return -1;
if(msg->first_line.type == SIP_REPLY) {
if((_reply_counter = pvh_reply_append(backup_xavis)) < 0) {
if((pvh_reply_counter = pvh_reply_append(backup_xavis)) < 0) {
return -1;
}
}
Expand All @@ -120,10 +122,10 @@ static int ki_pvh_collect_headers(struct sip_msg *msg)
{
sr_xavp_t **backup_xavis = NULL;

if(pvh_get_branch_index(msg, &_branch) < 0)
if(pvh_get_branch_index(msg, &pvh_branch) < 0)
return -1;
if(msg->first_line.type == SIP_REPLY) {
if((_reply_counter = pvh_reply_append(backup_xavis)) < 0) {
if((pvh_reply_counter = pvh_reply_append(backup_xavis)) < 0) {
return -1;
}
}
Expand All @@ -132,21 +134,21 @@ static int ki_pvh_collect_headers(struct sip_msg *msg)

static int w_pvh_apply_headers(struct sip_msg *msg, char *p1, char *p2)
{
if(pvh_get_branch_index(msg, &_branch) < 0)
if(pvh_get_branch_index(msg, &pvh_branch) < 0)
return -1;
return pvh_apply_headers(msg);
}

static int ki_pvh_apply_headers(struct sip_msg *msg)
{
if(pvh_get_branch_index(msg, &_branch) < 0)
if(pvh_get_branch_index(msg, &pvh_branch) < 0)
return -1;
return pvh_apply_headers(msg);
}

static int w_pvh_reset_headers(struct sip_msg *msg, char *p1, char *p2)
{
if(pvh_get_branch_index(msg, &_branch) < 0)
if(pvh_get_branch_index(msg, &pvh_branch) < 0)
return -1;
return pvh_reset_headers(msg);
}
Expand Down Expand Up @@ -228,7 +230,7 @@ static int ki_pvh_remove_header_param(
int idx = 0;
int new_size;
str dst = STR_NULL;
sr_xavp_t *avi = pvh_xavi_get_child(msg, &xavi_name, hname);
sr_xavp_t *avi = pvh_xavi_get_child(msg, &_pvh_params.xavi_name, hname);

while(avi) {
next = 1;
Expand All @@ -242,7 +244,8 @@ static int ki_pvh_remove_header_param(
STR_FMT(hname), idx);
if(pvh_remove_header(msg, hname, idx) < 0)
return -1;
avi = pvh_xavi_get_child(msg, &xavi_name, hname);
avi = pvh_xavi_get_child(
msg, &_pvh_params.xavi_name, hname);
if(idx > 0)
idx = 0;
next = 0;
Expand All @@ -252,8 +255,8 @@ static int ki_pvh_remove_header_param(
} else {
LM_DBG("old_value:'%.*s' new_value:'%.*s'\n",
STR_FMT(&avi->val.v.s), STR_FMT(&dst));
avi = pvh_set_xavi(
msg, &xavi_name, hname, &dst, SR_XTYPE_STR, idx, 0);
avi = pvh_set_xavi(msg, &_pvh_params.xavi_name, hname, &dst,
SR_XTYPE_STR, idx, 0);
if(avi == NULL) {
LM_ERR("can't set new value\n");
return -1;
Expand Down Expand Up @@ -344,13 +347,13 @@ static pv_export_t mod_pvs[] = {
};

static param_export_t params[] = {
{"xavi_name", PARAM_STR, &xavi_name},
{"header_value_size", PARAM_INT, &header_value_size},
{"header_collect_flag", PARAM_INT, &FL_PV_HDRS_COLLECTED},
{"header_apply_flag", PARAM_INT, &FL_PV_HDRS_APPLIED},
{"skip_headers", PARAM_STR, &skip_headers_param},
{"split_headers", PARAM_STR, &split_headers_param},
{"auto_msg", PARAM_INT, &auto_msg_param},
{"xavi_name", PARAM_STR, &_pvh_params.xavi_name},
{"header_value_size", PARAM_INT, &_pvh_params.hdr_value_size},
{"header_collect_flag", PARAM_INT, &_pvh_params.flags[PVH_HDRS_COLLECTED]},
{"header_apply_flag", PARAM_INT, &_pvh_params.flags[PVH_HDRS_APPLIED]},
{"skip_headers", PARAM_STR, &_pvh_params.skip_hdrs},
{"split_headers", PARAM_STR, &_pvh_params.split_hdrs},
{"auto_msg", PARAM_INT, &_pvh_params.auto_msg},
{0, 0, 0}
};

Expand All @@ -372,17 +375,17 @@ int mod_init(void)
{
LM_INFO("%s module init...\n", MODULE_NAME);

if(load_uac_api(&uac) < 0) {
if(load_uac_api(&pvh_uac) < 0) {
LM_NOTICE("could not bind to the 'uac' module, From/To headers will "
"not be modified\n");
}

if(load_tm_api(&tmb) < 0) {
if(load_tm_api(&pvh_tmb) < 0) {
LM_NOTICE("could not bind to the 'tm' module, automatic headers "
"collect/apply is disabled\n");
auto_msg_param = 0;
_pvh_params.auto_msg = 0;
}
if(auto_msg_param) {
if(_pvh_params.auto_msg) {
if(register_script_cb(handle_msg_cb, PRE_SCRIPT_CB | REQUEST_CB, 0)
< 0) {
LM_ERR("cannot register PRE_SCRIPT_CB REQUEST_CB callbacks\n");
Expand All @@ -408,9 +411,9 @@ int mod_init(void)
}
}

pvh_str_hash_init(&skip_headers, &skip_headers_param, "skip_headers");
pvh_str_hash_init(&split_headers, &split_headers_param, "split_headers");
pvh_str_hash_init(&single_headers, &single_headers_param, "single_headers");
pvh_str_hash_init(&skip_hdrs, &_pvh_params.skip_hdrs, "skip_headers");
pvh_str_hash_init(&split_hdrs, &_pvh_params.split_hdrs, "split_headers");
pvh_str_hash_init(&single_hdrs, &_pvh_params.single_hdrs, "single_headers");

return 0;
}
Expand Down Expand Up @@ -525,11 +528,11 @@ void handle_tm_t(tm_cell_t *t, int type, struct tmcb_params *params)


LM_DBG("T:%p picked_branch:%d label:%d branches:%d\n", t,
tmb.t_get_picked_branch(), t->label, t->nr_of_outgoings);
pvh_tmb.t_get_picked_branch(), t->label, t->nr_of_outgoings);

if(msg != NULL && msg != FAKED_REPLY) {
pvh_get_branch_index(msg, &_branch);
LM_DBG("T:%p set branch:%d\n", t, _branch);
pvh_get_branch_index(msg, &pvh_branch);
LM_DBG("T:%p set branch:%d\n", t, pvh_branch);
pvh_apply_headers(msg);
}
return;
Expand All @@ -542,8 +545,8 @@ int handle_msg_failed_cb(struct sip_msg *msg, unsigned int flags, void *cb)
if(pvh_parse_msg(msg) != 0)
return 1;

_branch = 0;
LM_DBG("msg:%p set branch:%d\n", msg, _branch);
pvh_branch = 0;
LM_DBG("msg:%p set branch:%d\n", msg, pvh_branch);
return 1;
}

Expand All @@ -557,27 +560,27 @@ int handle_msg_cb(struct sip_msg *msg, unsigned int flags, void *cb)
if(pvh_parse_msg(msg) != 0)
return 1;

if(tmb.register_tmcb(msg, 0, msg_cbs, handle_tm_t, 0, 0) <= 0) {
if(pvh_tmb.register_tmcb(msg, 0, msg_cbs, handle_tm_t, 0, 0) <= 0) {
LM_ERR("cannot register TM callbacks\n");
return -1;
}

_branch = 0;
LM_DBG("msg:%p set branch:%d\n", msg, _branch);
pvh_branch = 0;
LM_DBG("msg:%p set branch:%d\n", msg, pvh_branch);
pvh_collect_headers(msg);
return 1;
}

int handle_msg_branch_cb(struct sip_msg *msg, unsigned int flags, void *cb)
{

LM_DBG("msg:%p previous branch:%d\n", msg, _branch);
LM_DBG("msg:%p previous branch:%d\n", msg, pvh_branch);
print_cb_flags(flags);

if(flags & PRE_SCRIPT_CB) {
pvh_get_branch_index(msg, &_branch);
LM_DBG("msg:%p set branch:%d\n", msg, _branch);
pvh_clone_branch_xavi(msg, &xavi_name);
pvh_get_branch_index(msg, &pvh_branch);
LM_DBG("msg:%p set branch:%d\n", msg, pvh_branch);
pvh_clone_branch_xavi(msg, &_pvh_params.xavi_name);
}

return 1;
Expand All @@ -595,22 +598,22 @@ int handle_msg_reply_cb(struct sip_msg *msg, unsigned int flags, void *cb)
if(msg == FAKED_REPLY) {
LM_DBG("FAKED_REPLY\n");
}
LM_DBG("msg:%p previous branch:%d\n", msg, _branch);
LM_DBG("msg:%p previous branch:%d\n", msg, pvh_branch);
print_cb_flags(flags);

t = tmb.t_find(msg, &_branch, &vref);
t = pvh_tmb.t_find(msg, &pvh_branch, &vref);
if(t != NULL && t != T_UNDEFINED) {
LM_DBG("T:%p t_check-branch:%d xavi_list:%p branches:%d\n", t, _branch,
&t->xavis_list, t->nr_of_outgoings);
LM_DBG("T:%p t_check-branch:%d xavi_list:%p branches:%d\n", t,
pvh_branch, &t->xavis_list, t->nr_of_outgoings);
list = &t->xavis_list;
backup_xavis = xavi_set_list(&t->xavis_list);
}

pvh_get_branch_index(msg, &_branch);
LM_DBG("T:%p set branch:%d picked_branch:%d\n", t, _branch,
tmb.t_get_picked_branch());
pvh_get_branch_index(msg, &pvh_branch);
LM_DBG("T:%p set branch:%d picked_branch:%d\n", t, pvh_branch,
pvh_tmb.t_get_picked_branch());

if((_reply_counter = pvh_reply_append(list)) < 0) {
if((pvh_reply_counter = pvh_reply_append(list)) < 0) {
return -1;
}
pvh_collect_headers(msg);
Expand All @@ -621,7 +624,7 @@ int handle_msg_reply_cb(struct sip_msg *msg, unsigned int flags, void *cb)
if(t != NULL && t != T_UNDEFINED && vref != 0) {
/* t_find() above has the side effect of setting T and
REFerencing T => we must unref and unset it */
tmb.t_unset();
pvh_tmb.t_unset();
LM_DBG("T:%p unref\n", t);
}

Expand Down
39 changes: 23 additions & 16 deletions src/modules/pv_headers/pv_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* pv_headers
*
* Copyright (C)
* 2020 Victor Seva <vseva@sipwise.com>
* 2020-2023 Victor Seva <vseva@sipwise.com>
* 2018 Kirill Solomko <ksolomko@sipwise.com>
*
* This file is part of Kamailio, a free SIP server.
Expand Down Expand Up @@ -36,22 +36,29 @@ typedef struct _xavp_c_data
str value;
} xavp_c_data_t;

extern uac_api_t uac;
extern uac_api_t pvh_uac;

extern str xavi_name;
extern str xavi_parsed_xname;
extern str xavi_helper_xname;

extern unsigned int header_name_size;
extern unsigned int header_value_size;

extern str _hdr_from;
extern str _hdr_to;
extern str _hdr_reply_reason;
extern int _branch;
extern int _reply_counter;
#define PVH_HDRS_COLLECTED 0
#define PVH_HDRS_APPLIED 1
typedef struct _pvh_params
{
str xavi_name;
str xavi_helper_xname;
str xavi_parsed_xname;
unsigned int hdr_value_size;
int flags[2];
str skip_hdrs;
str split_hdrs;
str single_hdrs;
unsigned int auto_msg;
} _pvh_params_t;
extern _pvh_params_t _pvh_params;

extern int FL_PV_HDRS_COLLECTED;
extern int FL_PV_HDRS_APPLIED;
extern unsigned int pvh_hdr_name_size;
extern str pvh_hdr_from;
extern str pvh_hdr_to;
extern str pvh_hdr_reply_reason;
extern int pvh_branch;
extern int pvh_reply_counter;

#endif /* PV_HEADERS_H */

0 comments on commit 964c21e

Please sign in to comment.