/
geoip2_mod.c
154 lines (128 loc) · 3.64 KB
/
geoip2_mod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "../../core/sr_module.h"
#include "../../core/dprint.h"
#include "../../core/ut.h"
#include "../../core/pvar.h"
#include "../../core/kemi.h"
#include "../../core/mod_fix.h"
#include "geoip2_pv.h"
MODULE_VERSION
static char *geoip2_path = NULL;
static int mod_init(void);
static void mod_destroy(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 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},
{0, 0, 0}
};
struct module_exports exports = {
"geoip2", /* module name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* exported functions */
params, /* exported parameters */
0, /* RPC method exports */
mod_pvs, /* exported pseudo-variables */
0, /* response handling function */
mod_init, /* module initialization function */
0, /* per-child init function */
mod_destroy /* module destroy function */
};
/**
* init module function
*/
static int mod_init(void)
{
LM_INFO("using GeoIP library version %s\n", MMDB_lib_version());
if(geoip2_path==NULL || strlen(geoip2_path)==0)
{
LM_ERR("path to GeoIP database file not set\n");
return -1;
}
if(geoip2_init_pv(geoip2_path)!=0)
{
LM_ERR("cannot init for database file at: %s\n", geoip2_path);
return -1;
}
return 0;
}
/**
* destroy module function
*/
static void mod_destroy(void)
{
geoip2_destroy_pv();
}
static int geoip2_match(sip_msg_t *msg, str *tomatch, str *pvclass)
{
geoip2_pv_reset(pvclass);
return geoip2_update_pv(tomatch, pvclass);
}
static int w_geoip2_match(sip_msg_t* msg, char* target, char* pvname)
{
str tomatch = STR_NULL;
str pvclass = STR_NULL;
if(msg==NULL) {
LM_ERR("received null msg\n");
return -1;
}
if(fixup_get_svalue(msg, (gparam_t*)target, &tomatch)<0) {
LM_ERR("cannot get the address\n");
return -1;
}
if(fixup_get_svalue(msg, (gparam_t*)pvname, &pvclass)<0) {
LM_ERR("cannot get the pv class\n");
return -1;
}
return geoip2_match(msg, &tomatch, &pvclass);
}
/**
*
*/
/* clang-format off */
static sr_kemi_t sr_kemi_geoip2_exports[] = {
{ str_init("geoip2"), str_init("match"),
SR_KEMIP_INT, geoip2_match,
{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
},
{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
};
/* clang-format on */
int mod_register(char *path, int *dlflags, void *p1, void *p2) {
sr_kemi_modules_add(sr_kemi_geoip2_exports);
return 0;
}