Skip to content

Commit

Permalink
geoip2: new parameter to register result id to get pv work in kemi
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Gaysin authored and miconda committed Nov 30, 2022
1 parent 3cecd97 commit 2181790
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
28 changes: 26 additions & 2 deletions src/modules/geoip2/doc/geoip2_admin.xml
Expand Up @@ -81,7 +81,7 @@
</section>
<section>
<title>Parameters</title>
<section>
<section id="geoip2.p.path">
<title><varname>path</varname> (string)</title>
<para>
Path to the GeoIP2 database file.
Expand All @@ -100,7 +100,31 @@ modparam("geoip2", "path", "/usr/local/share/GeoIP/GeoLite2-City.mmdb")
</programlisting>
</example>
</section>

<section id="geoip2.p.resid">
<title><varname>resid</varname> (str)</title>
<para>
Preregister result container id during initialization, enabling the use
of the module in KEMI scripts. In native &kamailio;.cfg file, registration
is done when parsing config and finding variables.
</para>
<para>
<emphasis>
Default value is <quote></quote> (empty).
</emphasis>
</para>
<example>
<title>Set <varname>resid</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("geoip2", "resid", "src")
...
if(geoip2_match("$si", "src")) {
...
}
...
</programlisting>
</example>
</section>
</section>

<section>
Expand Down
21 changes: 17 additions & 4 deletions src/modules/geoip2/geoip2_mod.c
Expand Up @@ -44,22 +44,23 @@ static int geoip2_rpc_init(void);

static int w_geoip2_match(struct sip_msg* msg, char* str1, char* str2);
static int geoip2_match(sip_msg_t *msg, str *tomatch, str *pvclass);
static int geoip2_resid_param(modparam_t type, void* val);

static pv_export_t mod_pvs[] = {
{ {"gip2", sizeof("gip2")-1}, PVT_OTHER, pv_get_geoip2, 0,
pv_parse_geoip2_name, 0, 0, 0 },
{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
};


static cmd_export_t cmds[]={
{"geoip2_match", (cmd_function)w_geoip2_match, 2, fixup_spve_spve,
0, ANY_ROUTE},
{0, 0, 0, 0, 0, 0}
};

static param_export_t params[]={
{"path", PARAM_STRING, &geoip2_path},
{"path", PARAM_STRING, &geoip2_path},
{"resid", PARAM_STR|PARAM_USE_FUNC, &geoip2_resid_param},
{0, 0, 0}
};

Expand All @@ -76,7 +77,6 @@ struct module_exports exports = {
mod_destroy /* module destroy function */
};


/**
* init module function
*/
Expand Down Expand Up @@ -113,6 +113,19 @@ static void mod_destroy(void)
geoip2_destroy_pv();
}

static int geoip2_resid_param(modparam_t type, void* val)
{
str rname;

rname = *((str*)val);
if(sr_geoip2_add_resid(&rname) < 0) {
LM_ERR("failed to register result container with id: %.*s\n",
rname.len, rname.s);
return -1;
}
return 0;
}

static int geoip2_match(sip_msg_t *msg, str *tomatch, str *pvclass)
{
geoip2_pv_reset(pvclass);
Expand Down Expand Up @@ -192,4 +205,4 @@ static sr_kemi_t sr_kemi_geoip2_exports[] = {
int mod_register(char *path, int *dlflags, void *p1, void *p2) {
sr_kemi_modules_add(sr_kemi_geoip2_exports);
return 0;
}
}
7 changes: 7 additions & 0 deletions src/modules/geoip2/geoip2_pv.c
Expand Up @@ -124,6 +124,13 @@ sr_geoip2_item_t *sr_geoip2_add_item(str *name)
return it;
}

int sr_geoip2_add_resid(str *rname)
{
if(sr_geoip2_add_item(rname)==NULL) {
return -1;
}
return 0;
}

int pv_parse_geoip2_name(pv_spec_p sp, str *in)
{
Expand Down
1 change: 1 addition & 0 deletions src/modules/geoip2/geoip2_pv.h
Expand Up @@ -35,6 +35,7 @@ void geoip2_destroy_pv(void);
int geoip2_reload_pv(char *path);
void geoip2_pv_reset(str *pvclass);
int geoip2_update_pv(str *tomatch, str *pvclass);
int sr_geoip2_add_resid(str *rname);

#endif

0 comments on commit 2181790

Please sign in to comment.