-
Notifications
You must be signed in to change notification settings - Fork 909
/
ims_usrloc_pcscf_mod.c
281 lines (240 loc) · 8.07 KB
/
ims_usrloc_pcscf_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
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
/*
* Copyright (C) 2012 Smile Communications, jason.penton@smilecoms.com
* Copyright (C) 2012 Smile Communications, richard.good@smilecoms.com
*
* The initial version of this code was written by Dragos Vingarzan
* (dragos(dot)vingarzan(at)fokus(dot)fraunhofer(dot)de and the
* Fruanhofer Institute. It was and still is maintained in a separate
* branch of the original SER. We are therefore migrating it to
* Kamailio/SR and look forward to maintaining it from here on out.
* 2011/2012 Smile Communications, Pty. Ltd.
* ported/maintained/improved by
* Jason Penton (jason(dot)penton(at)smilecoms.com and
* Richard Good (richard(dot)good(at)smilecoms.com) as part of an
* effort to add full IMS support to Kamailio/SR using a new and
* improved architecture
*
* NB: Alot of this code was originally part of OpenIMSCore,
* FhG Fokus.
* Copyright (C) 2004-2006 FhG Fokus
* Thanks for great work! This is an effort to
* break apart the various CSCF functions into logically separate
* components. We hope this will drive wider use. We also feel
* that in this way the architecture is more complete and thereby easier
* to manage in the Kamailio/SR environment
*
* 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
*
*/
#include <stdio.h>
#include "ims_usrloc_pcscf_mod.h"
#include "../../core/sr_module.h"
#include "../../core/dprint.h"
#include "../../core/rpc_lookup.h"
#include "../../core/timer.h" /* register_timer */
#include "../../core/globals.h"
#include "../../core/ut.h" /* str_init */
#include "dlist.h" /* register_udomain */
#include "udomain.h" /* {insert,delete,get,release}_urecord */
#include "pcontact.h" /* {insert,delete,get}_ucontact */
#include "ul_rpc.h"
#include "ul_callback.h"
#include "usrloc.h"
#include "usrloc_db.h"
MODULE_VERSION
#define DEFAULT_DBG_FILE "/var/log/usrloc_debug"
static FILE *debug_file;
static int mod_init(void); /*!< Module initialization function */
static void destroy(void); /*!< Module destroy function */
static void timer(unsigned int ticks, void* param); /*!< Timer handler */
static int child_init(int rank); /*!< Per-child init function */
extern int bind_usrloc(usrloc_api_t* api);
extern int ul_locks_no;
int expires_grace = 3600; //default is a grace period of 1 hour - after this contact is removed from P
/*
* Module parameters and their default values
*/
str usrloc_debug_file = str_init(DEFAULT_DBG_FILE);
int usrloc_debug = 0;
int ul_hash_size = 9;
int init_flag = 0;
str db_url = str_init(DEFAULT_DB_URL); /*!< Database URL */
int timer_interval = 60; /*!< Timer interval in seconds */
int db_mode = 0; /*!< Database sync scheme: 0-no db, 1-write through, 2-write back, 3-only db */
int ul_fetch_rows = 2000;
int match_contact_host_port = 1; /*!< Should we match contact just based on rui host and port*/
db1_con_t* ul_dbh = 0;
db_func_t ul_dbf;
/*! \brief
* Exported functions
*/
static cmd_export_t cmds[] = {
{"ul_bind_ims_usrloc_pcscf", (cmd_function)bind_usrloc, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
/*! \brief
* Exported parameters
*/
static param_export_t params[] = {
{"hash_size", INT_PARAM, &ul_hash_size },
{"timer_interval", INT_PARAM, &timer_interval },
{"usrloc_debug_file", PARAM_STR, &usrloc_debug_file },
{"enable_debug_file", INT_PARAM, &usrloc_debug},
{"db_url", PARAM_STR, &db_url },
{"timer_interval", INT_PARAM, &timer_interval },
{"db_mode", INT_PARAM, &db_mode },
{"match_contact_host_port", INT_PARAM, &match_contact_host_port },
{"expires_grace", INT_PARAM, &expires_grace },
{0, 0, 0}
};
stat_export_t mod_stats[] = {
{"registered_contacts" , STAT_IS_FUNC, (stat_var**)get_number_of_contacts },
{"registered_impus" , STAT_IS_FUNC, (stat_var**)get_number_of_impu },
{"expired_contacts" , STAT_IS_FUNC, (stat_var**)get_number_of_expired },
{0,0,0}
};
struct module_exports exports = {
"ims_usrloc_pcscf",
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* exported functions */
params, /* export parameters */
0, /* exported RPC functions */
0, /* exported pseudo-variables */
0, /* response·function */
mod_init, /* module initialization function */
child_init, /* per-child·init·function*/
destroy /* destroy function */
};
/*! \brief
* Module initialization function
*/
static int mod_init(void) {
if (usrloc_debug){
LM_INFO("Logging usrloc records to %.*s\n", usrloc_debug_file.len, usrloc_debug_file.s);
debug_file = fopen(usrloc_debug_file.s, "a");
fprintf(debug_file, "starting\n");
fflush(debug_file);
}
#ifdef STATISTICS
/* register statistics */
if (register_module_stats( exports.name, mod_stats)!=0 ) {
LM_ERR("failed to register core statistics\n");
return -1;
}
#endif
if (ul_hash_size <= 1)
ul_hash_size = 512;
else
ul_hash_size = 1 << ul_hash_size;
ul_locks_no = ul_hash_size;
if (ul_init_locks() != 0) {
LM_ERR("locks array initialization failed\n");
return -1;
}
/* Regsiter RPC */
if (rpc_register_array(ul_rpc) != 0) {
LM_ERR("failed to register RPC commands\n");
return -1;
}
/* Register cache timer */
LM_DBG("Registering cache timer");
register_timer(timer, 0, timer_interval);
/* init the callbacks list */
if (init_ulcb_list() < 0) {
LM_ERR("usrloc/callbacks initialization failed\n");
return -1;
}
/* Shall we use database ? */
if (db_mode != NO_DB) { /* Yes */
if(ul_fetch_rows<=0) {
LM_ERR("invalid fetch_rows number '%d'\n", ul_fetch_rows);
return -1;
}
if (init_db(&db_url, timer_interval, ul_fetch_rows) != 0) {
LM_ERR("Error initializing db connection\n");
return -1;
}
LM_DBG("Running in DB mode %i\n", db_mode);
}
init_flag = 1;
return 0;
}
static int child_init(int _rank)
{
dlist_t* ptr;
/* connecting to DB ? */
switch (db_mode) {
case NO_DB:
return 0;
case WRITE_THROUGH:
/* connect to db only from SIP workers, TIMER and MAIN processes */
if (_rank<=0 && _rank!=PROC_TIMER && _rank!=PROC_MAIN)
return 0;
break;
case WRITE_BACK:
/* connect to db only from TIMER (for flush), from MAIN (for
* final flush() and from child 1 for preload */
if (_rank!=PROC_TIMER && _rank!=PROC_MAIN && _rank!=PROC_SIPINIT)
return 0;
break;
}
LM_DBG("Connecting to usrloc_pcscf DB for rank %d\n", _rank);
if (connect_db(&db_url) != 0) {
LM_ERR("child(%d): failed to connect to database\n", _rank);
return -1;
}
/* _rank==PROC_SIPINIT is used even when fork is disabled */
if (_rank==PROC_SIPINIT && db_mode!=DB_ONLY) {
// if cache is used, populate domains from DB
for( ptr=root ; ptr ; ptr=ptr->next) {
LM_DBG("Preloading domain %.*s\n", ptr->name.len, ptr->name.s);
if (preload_udomain(ul_dbh, ptr->d) < 0) {
LM_ERR("child(%d): failed to preload domain '%.*s'\n",
_rank, ptr->name.len, ZSW(ptr->name.s));
return -1;
}
}
}
return 0;
}
/*! \brief
* Module destroy function
*/
static void destroy(void)
{
free_all_udomains();
ul_destroy_locks();
/* free callbacks list */
destroy_ulcb_list();
free_service_route_buf();
free_impu_buf();
if (db_mode)
destroy_db();
}
/*! \brief
* Timer handler
*/
static void timer(unsigned int ticks, void* param) {
LM_DBG("Syncing cache\n");
if (usrloc_debug) {
print_all_udomains(debug_file);
fflush(debug_file);
}
if (synchronize_all_udomains() != 0) {
LM_ERR("synchronizing cache failed\n");
}
}