geoip_mod.c

00001 
00025 #include <stdio.h>
00026 #include <unistd.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 
00030 #include "../../sr_module.h"
00031 #include "../../dprint.h"
00032 #include "../../ut.h"
00033 #include "../../pvar.h"
00034 #include "../../mod_fix.h"
00035 
00036 #include "geoip_pv.h"
00037 
00038 MODULE_VERSION
00039 
00040 static char *geoip_path = NULL;
00041 
00042 static int  mod_init(void);
00043 static void mod_destroy(void);
00044 
00045 static int w_geoip_match(struct sip_msg* msg, char* str1, char* str2);
00046 static int geoip_match(struct sip_msg *msg, gparam_t *target, gparam_t *pvname);
00047 
00048 static pv_export_t mod_pvs[] = {
00049         { {"gip", sizeof("git")-1}, PVT_OTHER, pv_get_geoip, 0,
00050                 pv_parse_geoip_name, 0, 0, 0 },
00051         { {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
00052 };
00053 
00054 
00055 static cmd_export_t cmds[]={
00056         {"geoip_match", (cmd_function)w_geoip_match, 2, fixup_spve_spve,
00057                 0, ANY_ROUTE},
00058         {0, 0, 0, 0, 0, 0}
00059 };
00060 
00061 static param_export_t params[]={
00062         {"path",     STR_PARAM, &geoip_path},
00063         {0, 0, 0}
00064 };
00065 
00066 struct module_exports exports = {
00067         "geoip",
00068         DEFAULT_DLFLAGS, /* dlopen flags */
00069         cmds,
00070         params,
00071         0,
00072         0,              /* exported MI functions */
00073         mod_pvs,        /* exported pseudo-variables */
00074         0,              /* extra processes */
00075         mod_init,       /* module initialization function */
00076         0,              /* response function */
00077         mod_destroy,    /* destroy function */
00078         0               /* per child init function */
00079 };
00080 
00081 
00082 
00086 static int mod_init(void)
00087 {
00088 
00089         if(geoip_path==NULL || strlen(geoip_path)==0)
00090         {
00091                 LM_ERR("path to GeoIP database file not set\n");
00092                 return -1;
00093         }
00094 
00095         if(geoip_init_pv(geoip_path)!=0)
00096         {
00097                 LM_ERR("cannot init for database file at: %s\n", geoip_path);
00098                 return -1;
00099         }
00100         return 0;
00101 }
00102 
00106 static void mod_destroy(void)
00107 {
00108         geoip_destroy_pv();
00109 }
00110 
00111 static int w_geoip_match(struct sip_msg* msg, char* str1, char* str2)
00112 {
00113         return geoip_match(msg, (gparam_t*)str1, (gparam_t*)str2);
00114 }
00115 
00116 static int geoip_match(struct sip_msg *msg, gparam_t *target, gparam_t *pvname)
00117 {
00118         str tomatch;
00119         str pvclass;
00120         
00121         if(msg==NULL)
00122         {
00123                 LM_ERR("received null msg\n");
00124                 return -1;
00125         }
00126 
00127         if(fixup_get_svalue(msg, target, &tomatch)<0)
00128         {
00129                 LM_ERR("cannot get the address\n");
00130                 return -1;
00131         }
00132         if(fixup_get_svalue(msg, pvname, &pvclass)<0)
00133         {
00134                 LM_ERR("cannot get the pv class\n");
00135                 return -1;
00136         }
00137         geoip_pv_reset(&pvclass);
00138 
00139         return geoip_update_pv(&tomatch, &pvclass);
00140 }
00141