Skip to content

Commit

Permalink
mid_registrar: port script functions to new param interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rvlad-patrascu authored and liviuchircu committed Apr 4, 2019
1 parent 487429b commit 135486e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 109 deletions.
55 changes: 22 additions & 33 deletions modules/mid_registrar/lookup.c
Expand Up @@ -68,17 +68,16 @@ unsigned int nbranches;
static char urimem[MAX_BRANCHES-1][MAX_URI_SIZE];
static str branch_uris[MAX_BRANCHES-1];

int mid_reg_lookup(struct sip_msg* req, char* _t, char* _f, char* _s)
int mid_reg_lookup(struct sip_msg* req, void* _t, str* flags_s, str* uri)
{
static str unescape_buf;
unsigned int flags;
urecord_t* r;
str aor, uri, unesc_aor;
str aor, unesc_aor;
ucontact_t* ptr,*it;
int res, pos, remote_cts_done = 0;
int ret, bak;
str path_dst;
str flags_s;
char* ua = NULL;
char* re_end = NULL;
int re_len = 0;
Expand All @@ -103,41 +102,37 @@ int mid_reg_lookup(struct sip_msg* req, char* _t, char* _f, char* _s)
LM_DBG("mid_reg_lookup ... \n");

flags = 0;
if (_f && _f[0]!=0) {
if (fixup_get_svalue(req, (gparam_p)_f, &flags_s)!=0) {
LM_ERR("failed to get a string value for the 'flags' parameter\n");
return -1;
}
for( res=0 ; res< flags_s.len ; res++ ) {
switch (flags_s.s[res]) {
if (flags_s && flags_s->s[0]!=0) {
for( res=0 ; res< flags_s->len ; res++ ) {
switch (flags_s->s[res]) {
case 'm': flags |= REG_LOOKUP_METHODFILTER_FLAG; break;
case 'b': flags |= REG_LOOKUP_NOBRANCH_FLAG; break;
case 'g': flags |= REG_LOOKUP_GLOBAL_FLAG; break;
case 'r': flags |= REG_BRANCH_AOR_LOOKUP_FLAG; break;
case 'u':
if (flags_s.s[res+1] != '/') {
if (flags_s->s[res+1] != '/') {
LM_ERR("no regexp after 'u' flag\n");
break;
}
res++;
if ((re_end = strrchr(flags_s.s+res+1, '/')) == NULL) {
if ((re_end = strrchr(flags_s->s+res+1, '/')) == NULL) {
LM_ERR("no regexp after 'u' flag\n");
break;
}
res++;
re_len = re_end-flags_s.s-res;
re_len = re_end-flags_s->s-res;
if (re_len == 0) {
LM_ERR("empty regexp\n");
break;
}
ua = flags_s.s+res;
ua = flags_s->s+res;
flags |= REG_LOOKUP_UAFILTER_FLAG;
LM_DBG("found regexp /%.*s/", re_len, ua);
res += re_len;
break;
case 'i': regexp_flags |= REG_ICASE; break;
case 'e': regexp_flags |= REG_EXTENDED; break;
default: LM_WARN("unsupported flag %c \n",flags_s.s[res]);
default: LM_WARN("unsupported flag %c \n",flags_s->s[res]);
}
}
}
Expand All @@ -163,14 +158,8 @@ int mid_reg_lookup(struct sip_msg* req, char* _t, char* _f, char* _s)
idx=0;
}

if (_s) {
if (fixup_get_svalue(req, (gparam_p)_s, &uri) != 0) {
LM_ERR("failed to get a string value for the 'AoR' parameter\n");
return -1;
}
} else {
uri = *GET_RURI(req);
}
if (!uri)
uri = GET_RURI(req);

if (flags & REG_LOOKUP_UAFILTER_FLAG) {
tmp = *(ua+re_len);
Expand All @@ -190,9 +179,9 @@ int mid_reg_lookup(struct sip_msg* req, char* _t, char* _f, char* _s)
return -1;
}

if (parse_uri(uri.s, uri.len, &puri) < 0) {
LM_ERR("failed to parse R-URI <%.*s>, ci: %.*s\n", uri.len,
uri.s, req->callid->body.len, req->callid->body.s);
if (parse_uri(uri->s, uri->len, &puri) < 0) {
LM_ERR("failed to parse R-URI <%.*s>, ci: %.*s\n", uri->len,
uri->s, req->callid->body.len, req->callid->body.s);
return -1;
}

Expand All @@ -201,20 +190,20 @@ int mid_reg_lookup(struct sip_msg* req, char* _t, char* _f, char* _s)
if (pos < 0) {
LM_ERR("failed to locate our ';%.*s=' param in %sURI '%.*s', "
"ci = %.*s!\n", ctid_param.len, ctid_param.s,
_s ? "" : "R-", uri.len, uri.s, req->callid->body.len,
uri ? "" : "R-", uri->len, uri->s, req->callid->body.len,
req->callid->body.s);
return -1;
}
if (str2int64(&puri.u_val[pos], &contact_id) != 0) {
LM_ERR("invalid contact_id in %sURI '%.*s', ci: %.*s\n",
_s ? "" : "R-", uri.len, uri.s, req->callid->body.len,
uri ? "" : "R-", uri->len, uri->s, req->callid->body.len,
req->callid->body.s);
return -1;
}
} else {
if (str2int64(&puri.user, &contact_id) != 0) {
LM_ERR("invalid contact_id in %sURI '%.*s', ci: %.*s\n",
_s ? "" : "R-", uri.len, uri.s, req->callid->body.len,
uri ? "" : "R-", uri->len, uri->s, req->callid->body.len,
req->callid->body.s);
return -1;
}
Expand All @@ -224,7 +213,7 @@ int mid_reg_lookup(struct sip_msg* req, char* _t, char* _f, char* _s)

ptr = ul_api.get_ucontact_from_id((udomain_t *)_t, contact_id, &r);
if (!ptr) {
LM_DBG("no record found for %.*s, ci: %.*s\n", uri.len, uri.s,
LM_DBG("no record found for %.*s, ci: %.*s\n", uri->len, uri->s,
req->callid->body.len, req->callid->body.s);
return -1;
}
Expand All @@ -235,7 +224,7 @@ int mid_reg_lookup(struct sip_msg* req, char* _t, char* _f, char* _s)

bak = reg_use_domain;
reg_use_domain = 0;
if (extract_aor(&uri, &aor, &sip_instance, &call_id) < 0) {
if (extract_aor(uri, &aor, &sip_instance, &call_id) < 0) {
LM_ERR("failed to extract address of record\n");
reg_use_domain = bak;
return -3;
Expand Down Expand Up @@ -457,8 +446,8 @@ int mid_reg_lookup(struct sip_msg* req, char* _t, char* _f, char* _s)
ul_api.release_urecord(r, 0);

/* idx starts from -1 */
uri = branch_uris[idx];
if (extract_aor(&uri, &aor, NULL, &call_id) < 0) {
uri = &branch_uris[idx];
if (extract_aor(uri, &aor, NULL, &call_id) < 0) {
LM_ERR("failed to extract address of record for branch uri\n");
return -3;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/mid_registrar/lookup.h
Expand Up @@ -34,6 +34,6 @@

#include "../../parser/msg_parser.h"

int mid_reg_lookup(struct sip_msg* req, char* _t, char* _f, char* _s);
int mid_reg_lookup(struct sip_msg* req, void* _t, str* flags_s, str* uri);

#endif /* __MID_REG_LOOKUP_ */
66 changes: 21 additions & 45 deletions modules/mid_registrar/mid_registrar.c
Expand Up @@ -34,7 +34,6 @@
#include "../../sr_module.h"
#include "../../ut.h"
#include "../../timer.h"
#include "../../mod_fix.h"
#include "../../data_lump.h"
#include "../../rw_locking.h"

Expand Down Expand Up @@ -114,7 +113,6 @@ int reg_use_domain = 0;
static int mod_init(void);

static int domain_fixup(void** param);
static int registrar_fixup(void** param, int param_no);

int solve_avp_defs(void);

Expand All @@ -134,23 +132,19 @@ char *mp_ctid_insertion = "ct-param";
str ctid_param = str_init("ctid");

static cmd_export_t cmds[] = {
{ "mid_registrar_save", (cmd_function)mid_reg_save, 1,
registrar_fixup, NULL, REQUEST_ROUTE },
{ "mid_registrar_save", (cmd_function)mid_reg_save, 2,
registrar_fixup, NULL, REQUEST_ROUTE },
{ "mid_registrar_save", (cmd_function)mid_reg_save, 3,
registrar_fixup, NULL, REQUEST_ROUTE },
{ "mid_registrar_save", (cmd_function)mid_reg_save, 4,
registrar_fixup, NULL, REQUEST_ROUTE },
{ "mid_registrar_save", (cmd_function)mid_reg_save, 5,
registrar_fixup, NULL, REQUEST_ROUTE },
{ "mid_registrar_lookup", (cmd_function)mid_reg_lookup, 1,
registrar_fixup, NULL, REQUEST_ROUTE },
{ "mid_registrar_lookup", (cmd_function)mid_reg_lookup, 2,
registrar_fixup, NULL, REQUEST_ROUTE },
{ "mid_registrar_lookup", (cmd_function)mid_reg_lookup, 3,
registrar_fixup, NULL, REQUEST_ROUTE },
{ NULL, NULL, 0, NULL, NULL, 0 }
{"mid_registrar_save", (cmd_function)mid_reg_save, {
{CMD_PARAM_STR, domain_fixup, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0 ,0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_INT|CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE},
{"mid_registrar_lookup", (cmd_function)mid_reg_lookup, {
{CMD_PARAM_STR, domain_fixup, 0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0 ,0},
{CMD_PARAM_STR|CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE},
{0,0,{{0,0,0}},0}
};

static param_export_t mod_params[] = {
Expand Down Expand Up @@ -214,12 +208,19 @@ struct module_exports exports= {
static int domain_fixup(void** param)
{
udomain_t* d;
str dom_s;

if (ul_api.register_udomain((char*)*param, &d) < 0) {
if (pkg_nt_str_dup(&dom_s, (str*)*param) < 0)
return E_OUT_OF_MEM;

if (ul_api.register_udomain(dom_s.s, &d) < 0) {
LM_ERR("failed to register domain\n");
pkg_free(dom_s.s);
return E_UNSPEC;
}

pkg_free(dom_s.s);

*param = (void*)d;
return 0;
}
Expand All @@ -237,31 +238,6 @@ static int mid_reg_post_script(struct sip_msg *foo, void *bar)
return SCB_RUN_ALL;
}

/*! \brief
* Fixup for "save"+"lookup" functions - domain, flags, AOR params
*/
static int registrar_fixup(void** param, int param_no)
{
switch (param_no) {
case 1:
/* table name */
return domain_fixup(param);
case 2:
/* flags */
return fixup_spve(param);
case 3:
/* AoR */
return fixup_sgp(param);
case 4:
/* outgoing registration interval */
return fixup_igp(param);
case 5:
/* ownership tag */
return fixup_sgp(param);
}

return E_BUG;
}

static int mod_init(void)
{
Expand Down
39 changes: 11 additions & 28 deletions modules/mid_registrar/save.c
Expand Up @@ -2284,12 +2284,10 @@ static void parse_save_flags(str *flags_s, struct save_ctx *out_sctx)
}
}

int mid_reg_save(struct sip_msg *msg, char *dom, char *flags_gp,
char *to_uri_gp, char *expires_gp, char *owtag_gp)
int mid_reg_save(struct sip_msg *msg, udomain_t *ud, str *flags_str,
str *to_uri, int *expires, str *owtag)
{
udomain_t *ud = (udomain_t *)dom;
urecord_t *rec = NULL;
str flags_str = { NULL, 0 }, to_uri = { NULL, 0 };
struct save_ctx sctx;
int rc = -1, st;

Expand All @@ -2303,41 +2301,26 @@ int mid_reg_save(struct sip_msg *msg, char *dom, char *flags_gp,

LM_DBG("saving to %.*s...\n", ud->name->len, ud->name->s);

if (flags_gp) {
if (fixup_get_svalue(msg, (gparam_p)flags_gp, &flags_str)) {
LM_ERR("invalid flags parameter\n");
return -1;
}

if (flags_str)
parse_save_flags(&flags_str, &sctx);
}

if (parse_reg_headers(msg) != 0) {
LM_ERR("failed to parse req headers\n");
return -1;
}

if (!to_uri_gp) {
to_uri = get_to(msg)->uri;
} else if (fixup_get_svalue(msg, (gparam_p)to_uri_gp, &to_uri)) {
LM_ERR("invalid AoR parameter\n");
return -1;
}
if (!to_uri)
to_uri = &get_to(msg)->uri;

if (!expires_gp) {
if (!expires)
sctx.expires_out = outgoing_expires;
} else if (fixup_get_ivalue(msg, (gparam_p)expires_gp, &sctx.expires_out)) {
LM_ERR("invalid outgoing_expires parameter\n");
return -1;
}
else
sctx.expires_out = *expires;

if (owtag_gp && fixup_get_svalue(msg, (gparam_p)owtag_gp,
&sctx.ownership_tag) < 0) {
LM_ERR("failed to extract the ownership tag!\n");
return -1;
}
if (owtag)
sctx.ownership_tag = *owtag;

if (extract_aor(&to_uri, &sctx.aor, 0, 0) < 0) {
if (extract_aor(to_uri, &sctx.aor, 0, 0) < 0) {
LM_ERR("failed to extract Address Of Record\n");
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions modules/mid_registrar/save.h
Expand Up @@ -75,7 +75,7 @@
#define CONTACT_SEP ", "
#define CONTACT_SEP_LEN (sizeof(CONTACT_SEP) - 1)

int mid_reg_save(struct sip_msg *msg, char *dom, char *flags_gp,
char *to_uri_gp, char *expires_gp, char *owtag_gp);
int mid_reg_save(struct sip_msg *msg, udomain_t *ud, str *flags_str,
str *to_uri, int *expires, str *owtag);

#endif /* __MID_REG_SAVE_ */

0 comments on commit 135486e

Please sign in to comment.