瀏覽代碼

geoip: new module for GeoIP checking

- get details about an ip address from GeoIP database
- results can be accessed via pseudo-variables: $gip(pvc=>key)
- ability to perform many searches and use results in parallel
- geoip db is loaded in cache at startup
Daniel-Constantin Mierla 15 年之前
父節點
當前提交
c13fefc2a8

+ 15 - 0
modules/geoip/Makefile

@@ -0,0 +1,15 @@
+# $Id$
+#
+# WARNING: do not run this directly, it should be run by the master Makefile
+
+include ../../Makefile.defs
+auto_gen=
+NAME=geoip.so
+LIBS= -lGeoIP
+
+DEFS+=-DOPENSER_MOD_INTERFACE
+
+SERLIBPATH=../../lib
+SER_LIBS+=$(SERLIBPATH)/kmi/kmi
+
+include ../../Makefile.modules

+ 135 - 0
modules/geoip/README

@@ -0,0 +1,135 @@
+geoip Module
+
+Daniel-Constantin Mierla
+
+   asipto.com
+
+Edited by
+
+Daniel-Constantin Mierla
+
+   <[email protected]>
+
+   Copyright © 2010 Daniel-Constantin Mierla (asipto.com)
+     __________________________________________________________________
+
+   Table of Contents
+
+   1. Admin Guide
+
+        1. Overview
+        2. Dependencies
+
+              2.1. Kamailio Modules
+              2.2. External Libraries or Applications
+
+        3. Exported Parameters
+
+              3.1. path (string)
+
+        4. Exported Functions
+
+              4.1. geoip_match(ipaddr, pvc)
+
+        5. Exported pseudo-variables
+
+   List of Examples
+
+   1.1. Set path parameter
+   1.2. geoip_match usage
+
+Chapter 1. Admin Guide
+
+   Table of Contents
+
+   1. Overview
+   2. Dependencies
+
+        2.1. Kamailio Modules
+        2.2. External Libraries or Applications
+
+   3. Exported Parameters
+
+        3.1. path (string)
+
+   4. Exported Functions
+
+        4.1. geoip_match(ipaddr, pvc)
+
+   5. Exported pseudo-variables
+
+1. Overview
+
+   The module loads exports a new class of pseudo-variables -
+   $gip(pvc=>key) - to access results upon a query against Max Mind GeoIP
+   database.
+
+   Many queries can be done and store results in different containers to
+   be able to use in parallel. Database is loaded at startup in chache.
+
+2. Dependencies
+
+   2.1. Kamailio Modules
+   2.2. External Libraries or Applications
+
+2.1. Kamailio Modules
+
+   The following modules must be loaded before this module:
+     * none.
+
+2.2. External Libraries or Applications
+
+   The following libraries or applications must be installed before
+   running Kamailio with this module loaded:
+     * libgeoip - the GeoIP library.
+
+3. Exported Parameters
+
+   3.1. path (string)
+
+3.1. path (string)
+
+   Path to GeoIP database file.
+
+   Default value is "null".
+
+   Example 1.1. Set path parameter
+...
+modparam("geoip", "path", "/usr/local/share/GeoLiteCity.dat")
+...
+
+4. Exported Functions
+
+   4.1. geoip_match(ipaddr, pvc)
+
+4.1.  geoip_match(ipaddr, pvc)
+
+   Match ipaddr against GeoIP database and set the pvc container. The
+   function has to be called before accessing any $gip(pvc=>key).
+
+   Example 1.2. geoip_match usage
+...
+if(geoip_match("$si", "src"))
+    xlog("SIP message from: $gip(src=>cc)\n");
+...
+
+5. Exported pseudo-variables
+
+     * $gip(pvc=>key) - pvc is second parameter of geoip_match() and key
+       can be:
+          + cc - country code
+          + tz - time zone
+          + zip - postal code
+          + lat - latitude
+          + lon - longitude
+          + dma - dma code
+          + ips - ip start
+          + ipe - ip end
+          + city - city
+          + area - area code
+          + regc - region
+          + regn - region name
+          + metro - metro code
+
+   Exported pseudo-variables are documented at
+   http://www.kamailio.org/dokuwiki/.

+ 4 - 0
modules/geoip/doc/Makefile

@@ -0,0 +1,4 @@
+docs = geoip.xml
+
+docbook_dir = ../../../docbook
+include $(docbook_dir)/Makefile.module

+ 36 - 0
modules/geoip/doc/geoip.xml

@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding='ISO-8859-1'?>
+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
+"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
+
+<!-- Include general documentation entities -->
+<!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
+%docentities;
+
+]>
+
+<book xmlns:xi="http://www.w3.org/2001/XInclude">
+    <bookinfo>
+	<title>geoip Module</title>
+	<productname class="trade">&kamailioname;</productname>
+	<authorgroup>
+	    <author>
+		<firstname>Daniel-Constantin</firstname>
+		<surname>Mierla</surname>
+		<affiliation><orgname>asipto.com</orgname></affiliation>
+	    </author>
+	    <editor>
+		<firstname>Daniel-Constantin</firstname>
+		<surname>Mierla</surname>
+		    <email>[email protected]</email>
+	    </editor>
+	</authorgroup>
+	<copyright>
+	    <year>2010</year>
+	    <holder>Daniel-Constantin Mierla (asipto.com)</holder>
+	</copyright>
+    </bookinfo>
+    <toc></toc>
+    
+	<xi:include href="geoip_admin.xml"/>
+    
+</book>

+ 162 - 0
modules/geoip/doc/geoip_admin.xml

@@ -0,0 +1,162 @@
+<?xml version="1.0" encoding='ISO-8859-1'?>
+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
+"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
+
+<!-- Include general documentation entities -->
+<!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
+%docentities;
+
+]>
+
+<!-- Module User's Guide -->
+
+<chapter>
+    
+    <title>&adminguide;</title>
+    
+    <section>
+	<title>Overview</title>
+	<para>
+		The module loads exports a new class of pseudo-variables -
+		$gip(pvc=&gt;key) - to access results upon a query against Max Mind
+		GeoIP database.
+	</para>
+	<para>
+		Many queries can be done and store results in different containers to
+		be able to use in parallel. Database is loaded at startup in chache.
+	</para>
+    </section>
+    <section>
+	<title>Dependencies</title>
+	<section>
+	    <title>&kamailio; Modules</title>
+	    <para>
+		The following modules must be loaded before this module:
+	    	<itemizedlist>
+		    <listitem>
+			<para>
+			    <emphasis>none</emphasis>.
+			</para>
+		    </listitem>
+	    	</itemizedlist>
+	    </para>
+	</section>
+	<section>
+	    <title>External Libraries or Applications</title>
+	    <para>
+		The following libraries or applications must be installed before running
+		&kamailio; with this module loaded:
+	    	<itemizedlist>
+		    <listitem>
+			<para>
+			    <emphasis>libgeoip</emphasis> - the GeoIP library.
+			</para>
+		    </listitem>
+	    	</itemizedlist>
+	    </para>
+	</section>
+    </section>
+    <section>
+	<title>Exported Parameters</title>
+	<section>
+	    <title><varname>path</varname> (string)</title>
+	    <para>
+		Path to GeoIP database file.
+	    </para>
+	    <para>
+		<emphasis>
+		    Default value is <quote>null</quote>.
+		</emphasis>
+	    </para>
+	    <example>
+		<title>Set <varname>path</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("geoip", "path", "/usr/local/share/GeoLiteCity.dat")
+...
+</programlisting>
+	    </example>
+	</section>
+
+	</section>
+	
+    <section>
+	<title>Exported Functions</title>
+ 	<section>
+	    <title>
+		<function moreinfo="none">geoip_match(ipaddr, pvc)</function>
+	    </title>
+	    <para>
+			Match ipaddr against GeoIP database and set the pvc container. The
+			function has to be called before accessing any $gip(pvc=&gt;key).
+	    </para>
+		<example>
+		<title><function>geoip_match</function> usage</title>
+		<programlisting format="linespecific">
+...
+if(geoip_match("$si", "src"))
+    xlog("SIP message from: $gip(src=&gt;cc)\n");
+...
+</programlisting>
+	    </example>
+	</section>
+	
+    </section>
+	
+	<section>
+		<title>Exported pseudo-variables</title>
+		<itemizedlist>
+			<listitem><para>
+				<emphasis>$gip(pvc=&gt;key)</emphasis> - pvc is second
+				parameter of geoip_match() and key can be:
+				</para>
+			<itemizedlist>
+				<listitem><para>
+					<emphasis>cc</emphasis> - country code
+				</para></listitem>
+				<listitem><para>
+					<emphasis>tz</emphasis> - time zone
+				</para></listitem>
+				<listitem><para>
+					<emphasis>zip</emphasis> - postal code
+				</para></listitem>
+				<listitem><para>
+					<emphasis>lat</emphasis> - latitude
+				</para></listitem>
+				<listitem><para>
+					<emphasis>lon</emphasis> - longitude
+				</para></listitem>
+				<listitem><para>
+					<emphasis>dma</emphasis> - dma code
+				</para></listitem>
+				<listitem><para>
+					<emphasis>ips</emphasis> - ip start
+				</para></listitem>
+				<listitem><para>
+					<emphasis>ipe</emphasis> - ip end
+				</para></listitem>
+				<listitem><para>
+					<emphasis>city</emphasis> - city
+				</para></listitem>
+				<listitem><para>
+					<emphasis>area</emphasis> - area code
+				</para></listitem>
+				<listitem><para>
+					<emphasis>regc</emphasis> - region
+				</para></listitem>
+				<listitem><para>
+					<emphasis>regn</emphasis> - region name
+				</para></listitem>
+				<listitem><para>
+					<emphasis>metro</emphasis> - metro code
+				</para></listitem>
+			</itemizedlist>
+			</listitem>
+		</itemizedlist>
+		<para>
+		Exported pseudo-variables are documented at &kamwikilink;.
+		</para>
+	</section>
+
+</chapter>
+

+ 141 - 0
modules/geoip/geoip_mod.c

@@ -0,0 +1,141 @@
+/**
+ * $Id$
+ *
+ * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../../sr_module.h"
+#include "../../dprint.h"
+#include "../../ut.h"
+#include "../../pvar.h"
+#include "../../mod_fix.h"
+
+#include "geoip_pv.h"
+
+MODULE_VERSION
+
+static char *geoip_path = NULL;
+
+static int  mod_init(void);
+static void mod_destroy(void);
+
+static int w_geoip_match(struct sip_msg* msg, char* str1, char* str2);
+static int geoip_match(struct sip_msg *msg, gparam_t *target, gparam_t *pvname);
+
+static pv_export_t mod_pvs[] = {
+	{ {"gip", sizeof("git")-1}, PVT_OTHER, pv_get_geoip, 0,
+		pv_parse_geoip_name, 0, 0, 0 },
+	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
+};
+
+
+static cmd_export_t cmds[]={
+	{"geoip_match", (cmd_function)w_geoip_match, 2, fixup_spve_spve,
+		0, ANY_ROUTE},
+	{0, 0, 0, 0, 0, 0}
+};
+
+static param_export_t params[]={
+	{"path",     STR_PARAM, &geoip_path},
+	{0, 0, 0}
+};
+
+struct module_exports exports = {
+	"geoip",
+	DEFAULT_DLFLAGS, /* dlopen flags */
+	cmds,
+	params,
+	0,
+	0,              /* exported MI functions */
+	mod_pvs,        /* exported pseudo-variables */
+	0,              /* extra processes */
+	mod_init,       /* module initialization function */
+	0,              /* response function */
+	mod_destroy,    /* destroy function */
+	0               /* per child init function */
+};
+
+
+
+/**
+ * init module function
+ */
+static int mod_init(void)
+{
+
+	if(geoip_path==NULL || strlen(geoip_path)==0)
+	{
+		LM_ERR("path to GeoIP database file not set\n");
+		return -1;
+	}
+
+	if(geoip_init_pv(geoip_path)!=0)
+	{
+		LM_ERR("cannot init for database file at: %s\n", geoip_path);
+		return -1;
+	}
+	return 0;
+}
+
+/**
+ * destroy module function
+ */
+static void mod_destroy(void)
+{
+	geoip_destroy_pv();
+}
+
+static int w_geoip_match(struct sip_msg* msg, char* str1, char* str2)
+{
+	return geoip_match(msg, (gparam_t*)str1, (gparam_t*)str2);
+}
+
+static int geoip_match(struct sip_msg *msg, gparam_t *target, gparam_t *pvname)
+{
+	str tomatch;
+	str pvclass;
+	
+	if(msg==NULL)
+	{
+		LM_ERR("received null msg\n");
+		return -1;
+	}
+
+	if(fixup_get_svalue(msg, target, &tomatch)<0)
+	{
+		LM_ERR("cannot get the address\n");
+		return -1;
+	}
+	if(fixup_get_svalue(msg, pvname, &pvclass)<0)
+	{
+		LM_ERR("cannot get the pv class\n");
+		return -1;
+	}
+	geoip_pv_reset(&pvclass);
+
+	return geoip_update_pv(&tomatch, &pvclass);
+}
+

+ 405 - 0
modules/geoip/geoip_pv.c

@@ -0,0 +1,405 @@
+/**
+ * $Id$
+ *
+ * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+
+#include <assert.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include "../../dprint.h"
+#include "../../hashes.h"
+#include "../../pvar.h"
+
+#include "geoip_pv.h"
+
+typedef struct _sr_geoip_record {
+	GeoIPRecord *record;
+	char *time_zone;
+	char *region_name;
+	char **range;
+	char latitude[16];
+	char longitude[16];
+	char tomatch[256];
+	int flags;
+} sr_geoip_record_t;
+
+typedef struct _sr_geoip_item {
+	str pvclass;
+	unsigned int hashid;
+	sr_geoip_record_t r;
+	struct _sr_geoip_item *next;
+} sr_geoip_item_t;
+
+typedef struct _geoip_pv {
+	sr_geoip_item_t *item;
+	int type;
+} geoip_pv_t;
+
+static GeoIP *_handle_GeoIP = NULL;
+
+static sr_geoip_item_t *_sr_geoip_list = NULL;
+
+sr_geoip_record_t *sr_geoip_get_record(str *name)
+{
+	sr_geoip_item_t *it = NULL;
+	unsigned int hashid = 0;
+
+	hashid =  get_hash1_raw(name->s, name->len);
+
+	it = _sr_geoip_list;
+	while(it!=NULL)
+	{
+		if(it->hashid==hashid && it->pvclass.len == name->len
+				&& strncmp(it->pvclass.s, name->s, name->len)==0)
+			return &it->r;
+		it = it->next;
+	}
+	return NULL;
+}
+
+sr_geoip_item_t *sr_geoip_add_item(str *name)
+{
+	sr_geoip_item_t *it = NULL;
+	unsigned int hashid = 0;
+
+	hashid =  get_hash1_raw(name->s, name->len);
+
+	it = _sr_geoip_list;
+	while(it!=NULL)
+	{
+		if(it->hashid==hashid && it->pvclass.len == name->len
+				&& strncmp(it->pvclass.s, name->s, name->len)==0)
+			return it;
+		it = it->next;
+	}
+	/* add new */
+	it = (sr_geoip_item_t*)pkg_malloc(sizeof(sr_geoip_item_t));
+	if(it==NULL)
+	{
+		LM_ERR("no more pkg\n");
+		return NULL;
+	}
+	memset(it, 0, sizeof(sr_geoip_item_t));
+	it->pvclass.s = (char*)pkg_malloc(name->len+1);
+	if(it->pvclass.s==NULL)
+	{
+		LM_ERR("no more pkg.\n");
+		return NULL;
+	}
+	memcpy(it->pvclass.s, name->s, name->len);
+	it->pvclass.s[name->len] = '\0';
+	it->pvclass.len = name->len;
+	it->hashid = hashid;
+	it->next = _sr_geoip_list;
+	_sr_geoip_list = it;
+	return it;
+}
+
+
+int pv_parse_geoip_name(pv_spec_p sp, str *in)
+{
+	geoip_pv_t *gpv=NULL;
+	char *p;
+	str pvc;
+	str pvs;
+	if(sp==NULL || in==NULL || in->len<=0)
+		return -1;
+
+	gpv = (geoip_pv_t*)pkg_malloc(sizeof(geoip_pv_t));
+	if(gpv==NULL)
+		return -1;
+
+	memset(gpv, 0, sizeof(geoip_pv_t));
+
+	p = in->s;
+
+	while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
+		p++;
+	if(p>in->s+in->len || *p=='\0')
+		goto error;
+	pvc.s = p;
+	while(p < in->s + in->len)
+	{
+		if(*p=='=' || *p==' ' || *p=='\t' || *p=='\n' || *p=='\r')
+			break;
+		p++;
+	}
+	if(p>in->s+in->len || *p=='\0')
+		goto error;
+	pvc.len = p - pvc.s;
+	if(*p!='=')
+	{
+		while(p<in->s+in->len && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
+			p++;
+		if(p>in->s+in->len || *p=='\0' || *p!='=')
+			goto error;
+	}
+	p++;
+	if(*p!='>')
+		goto error;
+	p++;
+
+	pvs.len = in->len - (int)(p - in->s);
+	pvs.s = p;
+	LM_DBG("geoip [%.*s] - key [%.*s]\n", pvc.len, pvc.s,
+			pvs.len, pvs.s);
+
+	gpv->item = sr_geoip_add_item(&pvc);
+	if(gpv->item==NULL)
+		goto error;
+
+	switch(pvs.len)
+	{
+		case 2: 
+			if(strncmp(pvs.s, "cc", 2)==0)
+				gpv->type = 0;
+			else if(strncmp(pvs.s, "tz", 2)==0)
+				gpv->type = 1;
+			else goto error;
+		break;
+		case 3: 
+			if(strncmp(pvs.s, "zip", 3)==0)
+				gpv->type = 2;
+			else if(strncmp(pvs.s, "lat", 3)==0)
+				gpv->type = 3;
+			else if(strncmp(pvs.s, "lon", 3)==0)
+				gpv->type = 4;
+			else if(strncmp(pvs.s, "dma", 3)==0)
+				gpv->type = 5;
+			else if(strncmp(pvs.s, "ips", 3)==0)
+				gpv->type = 6;
+			else if(strncmp(pvs.s, "ipe", 3)==0)
+				gpv->type = 7;
+			else goto error;
+		break;
+		case 4: 
+			if(strncmp(pvs.s, "city", 4)==0)
+				gpv->type = 8;
+			else if(strncmp(pvs.s, "area", 4)==0)
+				gpv->type = 9;
+			else if(strncmp(pvs.s, "regc", 4)==0)
+				gpv->type = 10;
+			else if(strncmp(pvs.s, "regn", 4)==0)
+				gpv->type = 11;
+			else goto error;
+		break;
+		case 5: 
+			if(strncmp(pvs.s, "metro", 5)==0)
+				gpv->type = 12;
+			else goto error;
+		break;
+		default:
+			goto error;
+	}
+	sp->pvp.pvn.u.dname = (void*)gpv;
+	sp->pvp.pvn.type = PV_NAME_OTHER;
+
+	return 0;
+
+error:
+	LM_ERR("error at PV geoip name: %.*s\n", in->len, in->s);
+	return -1;
+}
+
+int pv_geoip_get_strzval(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res, char *sval)
+{
+	str s;
+	if(sval==NULL)
+		return pv_get_null(msg, param, res);
+
+	s.s = sval;
+	s.len = strlen(s.s);
+	return pv_get_strval(msg, param, res, &s);
+}
+
+int pv_get_geoip(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res)
+{
+	geoip_pv_t *gpv;
+
+	if(msg==NULL || param==NULL)
+		return -1;
+
+	gpv = (geoip_pv_t*)param->pvn.u.dname;
+	if(gpv==NULL)
+		return -1;
+
+	switch(gpv->type)
+	{
+		case 1: /* tz */
+			if(gpv->item->r.time_zone==NULL)
+			{
+				if(gpv->item->r.flags&1)
+					return pv_get_null(msg, param, res);
+				gpv->item->r.time_zone
+					= (char*)GeoIP_time_zone_by_country_and_region(
+						gpv->item->r.record->country_code,
+						gpv->item->r.record->region);
+				gpv->item->r.flags |= 1;
+			}
+			return pv_geoip_get_strzval(msg, param, res,
+					gpv->item->r.time_zone);
+		case 2: /* zip */
+			return pv_geoip_get_strzval(msg, param, res,
+					gpv->item->r.record->postal_code);
+		case 3: /* lat */
+			if((gpv->item->r.flags&2)==0)
+			{
+				snprintf(gpv->item->r.latitude, 15, "%f",
+						gpv->item->r.record->latitude);
+				gpv->item->r.flags |= 2;
+			}
+			return pv_geoip_get_strzval(msg, param, res,
+					gpv->item->r.latitude);
+		case 4: /* lon */
+			if((gpv->item->r.flags&4)==0)
+			{
+				snprintf(gpv->item->r.longitude, 15, "%f",
+						gpv->item->r.record->longitude);
+				gpv->item->r.flags |= 4;
+			}
+			return pv_geoip_get_strzval(msg, param, res,
+					gpv->item->r.longitude);
+		case 5: /* dma */
+			return pv_get_sintval(msg, param, res,
+					gpv->item->r.record->dma_code);
+		case 6: /* ips */
+		case 7: /* ipe */
+			if((gpv->item->r.flags&8)==0)
+			{
+				gpv->item->r.range = GeoIP_range_by_ip(_handle_GeoIP,
+					gpv->item->r.tomatch);
+				gpv->item->r.flags |= 8;
+			}
+			if(gpv->item->r.range==NULL)
+				return pv_get_null(msg, param, res);
+			if(gpv->type==6)
+				return pv_geoip_get_strzval(msg, param, res,
+						gpv->item->r.range[0]);
+			return pv_geoip_get_strzval(msg, param, res,
+					gpv->item->r.range[1]);
+		case 8: /* city */
+			return pv_geoip_get_strzval(msg, param, res,
+					gpv->item->r.record->city);
+		case 9: /* area */
+			return pv_get_sintval(msg, param, res,
+					gpv->item->r.record->area_code);
+		case 10: /* regc */
+			return pv_geoip_get_strzval(msg, param, res,
+					gpv->item->r.record->region);
+		case 11: /* regn */
+			if((gpv->item->r.flags&16)==0)
+			{
+				gpv->item->r.region_name
+						= (char*)GeoIP_region_name_by_code(
+							gpv->item->r.record->country_code,
+							gpv->item->r.record->region);
+				gpv->item->r.flags |= 16;
+			}
+			return pv_geoip_get_strzval(msg, param, res,
+					gpv->item->r.region_name);
+		case 12: /* metro */
+			return pv_get_sintval(msg, param, res,
+					gpv->item->r.record->metro_code);
+		default: /* cc */
+			return pv_geoip_get_strzval(msg, param, res,
+					gpv->item->r.record->country_code);
+	}
+}
+
+int geoip_init_pv(char *path)
+{
+	_handle_GeoIP = GeoIP_open(path, GEOIP_MMAP_CACHE);
+	
+	if(_handle_GeoIP==NULL)
+	{
+		LM_ERR("cannot open GeoIP database file at: %s\n", path);
+		return -1;
+	}
+	return 0;
+}
+
+void geoip_destroy_list(void)
+{
+}
+
+void geoip_destroy_pv(void)
+{
+	if(_handle_GeoIP!=NULL)
+	{
+		GeoIP_delete(_handle_GeoIP);
+		_handle_GeoIP=NULL;
+	}
+}
+
+void geoip_pv_reset(str *name)
+{
+	sr_geoip_record_t *gr = NULL;
+	
+	gr = sr_geoip_get_record(name);
+
+	if(gr==NULL)
+		return;
+	if(gr->range!=NULL)
+		GeoIP_range_by_ip_delete(gr->range);
+	if(gr->record!=NULL)
+		GeoIPRecord_delete(gr->record);
+	memset(gr, 0, sizeof(struct _sr_geoip_record));
+}
+
+int geoip_update_pv(str *tomatch, str *name)
+{
+	sr_geoip_record_t *gr = NULL;
+	
+	if(tomatch->len>255)
+	{
+		LM_DBG("target too long (max 255): %s\n", tomatch->s);
+		return -3;
+	}
+	
+	gr = sr_geoip_get_record(name);
+	if(gr==NULL)
+	{
+		LM_DBG("container not found: %s\n", tomatch->s);
+		return - 4;
+	}
+
+	strncpy(gr->tomatch, tomatch->s, tomatch->len);
+	tomatch->s[tomatch->len] = '\0';
+	gr->record = GeoIP_record_by_name(_handle_GeoIP,
+			(const char*)gr->tomatch);
+	LM_DBG("attempt to match: %s\n", gr->tomatch);
+	if (gr->record == NULL)
+	{
+		LM_DBG("no match for: %s\n", gr->tomatch);
+		return -2;
+	}
+	LM_DBG("geoip PV updated for: %s\n", gr->tomatch);
+
+	return 1;
+}
+

+ 43 - 0
modules/geoip/geoip_pv.h

@@ -0,0 +1,43 @@
+/**
+ * $Id$
+ *
+ * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ *
+ * This file is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#ifndef _GEOIP_PV_H_
+#define _GEOIP_PV_H_
+
+#include <GeoIP.h>
+#include <GeoIPCity.h>
+
+#include "../../pvar.h"
+
+int pv_parse_geoip_name(pv_spec_p sp, str *in);
+int pv_get_geoip(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res);
+
+int geoip_init_pv(char *path);
+void geoip_destroy_pv(void);
+void geoip_pv_reset(str *pvclass);
+int geoip_update_pv(str *tomatch, str *pvclass);
+
+#endif
+