Skip to content

Commit

Permalink
[mmgeoip] allow chosing memory caching type
Browse files Browse the repository at this point in the history
	Before GEOIP_MMAP_CACHE was used causing a segmentation
fault when database file was changed. Now users can choose between
reading the database directly from file, cahing it into memory with
the possibility to change the database file at runtime.
  • Loading branch information
ionutrazvanionita committed May 23, 2016
1 parent b023c93 commit 66def87
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 7 deletions.
27 changes: 25 additions & 2 deletions modules/mmgeoip/README
Expand Up @@ -26,6 +26,7 @@ Kobi Eshun
1.3. Exported Parameters

1.3.1. mmgeoip_city_db_path (string)
1.3.2. cache_type (string)

1.4. Exported Functions

Expand All @@ -36,7 +37,8 @@ Kobi Eshun
List of Examples

1.1. Set “mmgeoip_city_db_path” parameter
1.2. mmg_lookup usage
1.2. Set “cache_type” parameter
1.3. mmg_lookup usage

Chapter 1. Admin Guide

Expand Down Expand Up @@ -79,6 +81,27 @@ modparam("mmgeoip", "mmgeoip_city_db_path",
"/usr/share/GeoIP/GeoLiteCity.dat")
...

1.3.2. cache_type (string)

Databse memory caching options. The following options are
available:
* STANDARD - Read database from file system; least memory
used;
* MMAP_CACHE - Load database into mmap allocated memory;
WARNING: this option will cause a segmentation fault if
database file is changed at runtime!
* MEM_CACHE_CHECK - Load database into memory; this mode
checks for database updates; if database was modified, the
file will be reloaded after 60 seconds; it will be slower
than MMAP_CACHE but it will allow reloads;

Default value for this parameter is MMAP_CACHE.

Example 1.2. Set “cache_type” parameter
...
modparam("mmgeoip", "cache_type","MEM_CACHE_CHECK")
...

1.4. Exported Functions

1.4.1. mmg_lookup([fields,]src,dst)
Expand All @@ -104,7 +127,7 @@ modparam("mmgeoip", "mmgeoip_city_db_path",
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
ONREPLY_ROUTE, BRANCH_ROUTE,ERROR_ROUTE, and LOCAL_ROUTE.

Example 1.2. mmg_lookup usage
Example 1.3. mmg_lookup usage
...
if(mmg_lookup("lon:lat","$si","$avp(lat_lon)")) {
xlog("L_INFO","Source IP latitude:$(avp(lat_lon)[0])\n");
Expand Down
56 changes: 52 additions & 4 deletions modules/mmgeoip/doc/mmgeoip_admin.xml
@@ -1,9 +1,9 @@
<!-- Module User's Guide -->

<chapter>

<title>&adminguide;</title>

<section>
<title>Overview</title>
<para>
Expand All @@ -18,7 +18,7 @@
by the script. Visit the
<ulink
url="http://www.maxmind.com/app/geolitecity"><citetitle>MaxMind
website</citetitle></ulink> for more information on the location
website</citetitle></ulink> for more information on the location
databases.
</para>
</section>
Expand Down Expand Up @@ -55,6 +55,7 @@

<section>
<title>Exported Parameters</title>

<section>
<title><varname>mmgeoip_city_db_path</varname> (string)</title>
<para>
Expand All @@ -75,6 +76,53 @@ modparam("mmgeoip", "mmgeoip_city_db_path",
</programlisting>
</example>
</section>

<section>
<title><varname>cache_type</varname> (string)</title>
<para>
Databse memory caching options. The following options are available:
<itemizedlist>

<listitem>
<para>
<emphasis>STANDARD</emphasis> - Read database from file system;
least memory used;
</para>
</listitem>

<listitem>
<para>
<emphasis>MMAP_CACHE</emphasis> - Load database into mmap allocated
memory;
<para><emphasis>WARNING: this option will cause a segmentation
fault if database file is changed at runtime!</emphasis></para>
</para>
</listitem>

<listitem>
<para>
<emphasis>MEM_CACHE_CHECK</emphasis> - Load database into memory;
this mode checks for database updates; if database was modified,
the file will be reloaded after 60 seconds; it will be slower than
<emphasis>MMAP_CACHE</emphasis> but it will allow reloads;
</para>
</listitem>

</itemizedlist>
</para>
<para>
Default value for this parameter is <emphasis>MMAP_CACHE</emphasis>.
</para>
<example>
<title>Set <quote>cache_type</quote> parameter</title>
<programlisting format="linespecific">
...
modparam("mmgeoip", "cache_type","MEM_CACHE_CHECK")
...
</programlisting>
</example>
</section>

</section>
<section>
<title>Exported Functions</title>
Expand Down Expand Up @@ -107,7 +155,7 @@ modparam("mmgeoip", "mmgeoip_city_db_path",
<listitem> <para> <varname>ac</varname> Area Code</para></listitem>
<listitem> <para> <varname>TZ</varname> Time Zone</para></listitem>
</itemizedlist>

<para>
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
ONREPLY_ROUTE, BRANCH_ROUTE,ERROR_ROUTE, and LOCAL_ROUTE.
Expand Down
37 changes: 36 additions & 1 deletion modules/mmgeoip/mmgeoip.c
Expand Up @@ -33,6 +33,7 @@
#include "../../str.h"
#include "../../usr_avp.h"
#include "../../mod_fix.h"
#include "../../ut.h"
#include "GeoIP.h"
#include "GeoIPCity.h"

Expand All @@ -41,6 +42,38 @@
static str MMG_city_db_path = {NULL, 0};
static GeoIP *MMG_gi = NULL;

static int geoip_cache_option = GEOIP_MMAP_CACHE;


int parse_mem_option( unsigned int type, void *val)
{
str opt_s;

static const str opt_STANDARD = str_init("STANDARD");
static const str opt_MMAP = str_init("MMAP_CACHE");
static const str opt_MEM_CHECK = str_init("MEM_CACHE_CHECK");

opt_s.s = (char *) val;
opt_s.len = strlen(opt_s.s);


if (opt_s.len == opt_STANDARD.len &&
!strncasecmp(opt_s.s, opt_STANDARD.s, opt_s.len)) {
geoip_cache_option = GEOIP_STANDARD;
} else if (opt_s.len == opt_MMAP.len &&
!strncasecmp(opt_s.s, opt_MMAP.s, opt_s.len)) {
geoip_cache_option = GEOIP_MMAP_CACHE;
} else if (opt_s.len == opt_MEM_CHECK.len &&
!strncasecmp(opt_s.s, opt_MEM_CHECK.s, opt_s.len)) {
geoip_cache_option = GEOIP_MEMORY_CACHE|GEOIP_CHECK_CACHE;
} else {
LM_ERR("Invalid cache option!\n");
return -1;
}

return 0;
}

static int
mod_init(void)
{
Expand All @@ -52,7 +85,8 @@ mod_init(void)
}

MMG_city_db_path.len=strlen(MMG_city_db_path.s);
if(0==(MMG_gi = GeoIP_open(MMG_city_db_path.s, GEOIP_MMAP_CACHE))){
if(0==(MMG_gi = GeoIP_open(MMG_city_db_path.s,
geoip_cache_option))){
LM_ERR("Unable to open City DB at path '%.*s'.\n",
MMG_city_db_path.len,MMG_city_db_path.s);
return -1;
Expand Down Expand Up @@ -234,6 +268,7 @@ w_lookup_cmd2(struct sip_msg *m, char *ipaddr, char *dst)
*/
static param_export_t mod_params[]={
{"mmgeoip_city_db_path", STR_PARAM, &MMG_city_db_path.s},
{"cache_type", STR_PARAM|USE_FUNC_PARAM, parse_mem_option},
{ 0,0,0 }
};

Expand Down

0 comments on commit 66def87

Please sign in to comment.