-
Notifications
You must be signed in to change notification settings - Fork 908
/
carrierroute.c
298 lines (252 loc) · 8.68 KB
/
carrierroute.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (C) 2007-2008 1&1 Internet AG
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio 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
*
* Kamailio 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
*/
/**
* \file carrierroute.c
* \brief Contains the functions exported by the module.
* \ingroup carrierroute
* - Module; \ref carrierroute
*/
/*!
* \defgroup carrierroute CARRIERROUTE :: The Kamailio carrierroute Module
* The module provides routing, balancing and blocklisting capabilities.
* It reads routing entries from a database source or from a config file
* at Kamailio startup. It can use one routing tree (for one carrier),
* or if needed for every user a different routing tree (unique for each carrier)
* for number prefix based routing. It supports several routing domains, e.g.
* for failback routes or different routing rules for VoIP and PSTN targets.
*/
#include "../../core/sr_module.h"
#include "../../core/str.h"
#include "../../core/mem/mem.h"
#include "../../core/ut.h" /* for user2uid() */
#include "../../core/kemi.h"
#include "carrierroute.h"
#include "cr_fixup.h"
#include "cr_map.h"
#include "cr_data.h"
#include "cr_func.h"
#include "db_carrierroute.h"
#include "config.h"
#include "cr_db.h"
#include "cr_rpc.h"
#include <sys/stat.h>
#define AVP_CR_URIS "_cr_uris"
int_str cr_uris_avp; // contains all PSTN destinations
MODULE_VERSION
str carrierroute_db_url = str_init(DEFAULT_RODB_URL);
str subscriber_table = str_init("subscriber");
static str subscriber_username_col = str_init("username");
static str subscriber_domain_col = str_init("domain");
static str cr_preferred_carrier_col = str_init("cr_preferred_carrier");
static int cr_load_comments = 1;
str * subscriber_columns[SUBSCRIBER_COLUMN_NUM] = {
&subscriber_username_col,
&subscriber_domain_col,
&cr_preferred_carrier_col
};
char * config_source = "file";
char * config_file = CFG_DIR"carrierroute.conf";
str default_tree = str_init("default");
const str CR_EMPTY_PREFIX = str_init("null");
int mode = 0;
int cr_match_mode = 10;
int cr_avoid_failed_dests = 1;
/************* Declaration of Interface Functions **************************/
static int mod_init(void);
static int child_init(int);
static void mod_destroy(void);
/************* Module Exports **********************************************/
static cmd_export_t cmds[]={
{"cr_user_carrier", (cmd_function)cr_load_user_carrier, 3,
cr_load_user_carrier_fixup, cr_load_user_carrier_fixup_free,
REQUEST_ROUTE | FAILURE_ROUTE },
{"cr_route", (cmd_function)cr_route5, 5,
cr_route_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
{"cr_route", (cmd_function)cr_route, 6,
cr_route_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
{"cr_nofallback_route",(cmd_function)cr_nofallback_route5, 5,
cr_route_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
{"cr_nofallback_route",(cmd_function)cr_nofallback_route, 6,
cr_route_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
{"cr_next_domain", (cmd_function)cr_load_next_domain, 6,
cr_load_next_domain_fixup, 0, REQUEST_ROUTE | FAILURE_ROUTE },
{0, 0, 0, 0, 0, 0}
};
static param_export_t params[]= {
carrierroute_DB_URL
carrierroute_DB_TABLE
carrierfailureroute_DB_TABLE
carrier_name_DB_TABLE
domain_name_DB_TABLE
carrierroute_DB_COLS
carrierfailureroute_DB_COLS
carrier_name_DB_COLS
domain_name_DB_COLS
{"subscriber_table", PARAM_STR, &subscriber_table },
{"subscriber_user_col", PARAM_STR, &subscriber_username_col },
{"subscriber_domain_col", PARAM_STR, &subscriber_domain_col },
{"subscriber_carrier_col", PARAM_STR, &cr_preferred_carrier_col },
{"config_source", PARAM_STRING, &config_source },
{"default_tree", PARAM_STR, &default_tree },
{"config_file", PARAM_STRING, &config_file },
{"use_domain", INT_PARAM, &default_carrierroute_cfg.use_domain },
{"fallback_default", INT_PARAM, &default_carrierroute_cfg.fallback_default },
{"fetch_rows", INT_PARAM, &default_carrierroute_cfg.fetch_rows },
{"db_load_description", INT_PARAM, &cr_load_comments },
{"match_mode", INT_PARAM, &cr_match_mode },
{"avoid_failed_destinations", INT_PARAM, &cr_avoid_failed_dests },
{0,0,0}
};
struct module_exports exports = {
"carrierroute", /* module name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* Exported functions */
params, /* Export parameters */
0, /* RPC method exports */
0, /* exported pseudo-variables */
0, /* Response function */
mod_init, /* Module initialization function */
child_init, /* Child initialization function */
mod_destroy /* Destroy function */
};
/************* Interface Functions *****************************************/
/**
* Initialises the module, i.e. it binds the necessary API functions
* and registers the fifo commands
*
* @return 0 on success, -1 on failure
*/
static int mod_init(void) {
struct stat fs;
extern char* user; /*from main.c*/
int uid, gid;
if(rpc_register_array(cr_rpc_methods)!=0) {
LM_ERR("failed to register RPC commands\n");
return -1;
}
if (cr_match_mode != 10 && cr_match_mode != 128) {
LM_ERR("invalid matching mode %d specific, please use 10 or 128\n", cr_match_mode);
return -1;
}
if (cr_avoid_failed_dests != 0 && cr_avoid_failed_dests != 1) {
LM_ERR("avoid_failed_dests must be 0 or 1");
return -1;
}
if (cr_load_comments != 0 && cr_load_comments != 1) {
LM_ERR("db_load_comments must be 0 or 1");
return -1;
}
if (strcmp(config_source, "db") == 0) {
mode = CARRIERROUTE_MODE_DB;
set_load_comments_params(cr_load_comments);
LM_INFO("use database as configuration source\n");
if(carrierroute_db_init() < 0){
return -1;
}
// FIXME, move data initialization into child process
if(carrierroute_db_open() < 0){
return -1;
}
}
else if (strcmp(config_source, "file") == 0) {
mode = CARRIERROUTE_MODE_FILE;
LM_INFO("use file as configuration source\n");
if(stat(config_file, &fs) != 0){
LM_ERR("can't stat config file\n");
return -1;
}
if(fs.st_mode & S_IWOTH){
LM_WARN("insecure file permissions, routing data is world writeable\n");
}
if (user){
if (user2uid(&uid, &gid, user)<0){
LM_ERR("bad user name/uid number: -u %s\n", user);
return -1;
}
} else {
uid = geteuid();
gid = getegid();
}
if( !( fs.st_mode & S_IWOTH) &&
!((fs.st_mode & S_IWGRP) && (fs.st_gid == gid)) &&
!((fs.st_mode & S_IWUSR) && (fs.st_uid == uid))) {
LM_ERR("config file %s not writable or not owned by uid: %d and gid: %d\n",
config_file, uid, gid);
return -1;
}
}
else {
LM_ERR("invalid config_source parameter: %s\n", config_source);
return -1;
}
if(cfg_declare("carrierroute", carrierroute_cfg_def, &default_carrierroute_cfg, cfg_sizeof(carrierroute), &carrierroute_cfg)){
LM_ERR("Fail to declare the configuration\n");
return -1;
}
if (init_route_data() < 0) {
LM_ERR("could not init route data\n");
return -1;
}
if (reload_route_data() == -1) {
LM_ERR("could not prepare route data\n");
return -1;
}
if(mode == CARRIERROUTE_MODE_DB){
carrierroute_db_close();
}
cr_uris_avp.s.s = AVP_CR_URIS;
cr_uris_avp.s.len = sizeof(AVP_CR_URIS) -1;
return 0;
}
static int child_init(int rank) {
if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
return 0; /* do nothing for the main process */
if(mode == CARRIERROUTE_MODE_DB){
return carrierroute_db_open();
}
return 0;
}
static void mod_destroy(void) {
if(mode == CARRIERROUTE_MODE_DB){
carrierroute_db_close();
}
destroy_route_data();
}
/**
*
*/
/* clang-format off */
static sr_kemi_t sr_kemi_carrierroute_exports[] = {
{ str_init("carrierroute"), str_init("cr_user_carrier"),
SR_KEMIP_INT, ki_cr_load_user_carrier,
{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
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_carrierroute_exports);
return 0;
}