-
Notifications
You must be signed in to change notification settings - Fork 909
/
cdp_mod.c
329 lines (291 loc) · 10.3 KB
/
cdp_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* 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 "cdp_mod.h"
#include "../../core/sr_module.h"
#include "../../core/globals.h"
#include "diameter_peer.h"
#include "config.h"
#include "cdp_load.h"
#include "cdp_rpc.h"
#include "../../core/rpc.h"
#include "../../core/rpc_lookup.h"
#include "../../core/cfg/cfg_struct.h"
#include "cdp_stats.h"
#include "cdp_functions.h"
#include "../../core/mod_fix.h"
MODULE_VERSION
char* config_file="DiameterPeer.xml"; /**< default DiameterPeer configuration filename */
unsigned int latency_threshold = 500; /**< default threshold for Diameter calls (ms) */
unsigned int *latency_threshold_p = &latency_threshold;
unsigned int workerq_latency_threshold = 100; /**< default threshold for putting a task into worker queue (ms) */
unsigned int workerq_length_threshold_percentage = 0; /**< default threshold for worker queue length, percentage of max queue length - by default disabled */
unsigned int debug_heavy = 0;
extern dp_config *config; /**< DiameterPeer configuration structure */
static int w_cdp_check_peer(sip_msg_t *msg, char *peer, char *p2);
static int w_cdp_has_app(sip_msg_t *msg, char *appid, char *param);
static int w_cdp_has_app2(sip_msg_t *msg, char *vendor, char *appid);
static int w_cdp_has_app(sip_msg_t *msg, char *appid, char *param);
#define EXP_FUNC(NAME) \
{#NAME, (cmd_function)NAME, NO_SCRIPT, 0, 0},
/**
* Exported functions. This is the API available for use from other SER modules.
* If you require more, please add them here.
* <p>
* - load_cdp() - find and load the CDiameterPeer function bindings
* <p>
* - AAACreateRequest() - create a diameter request #AAAMessage
* - AAACreateResponse() - create a diameter response #AAAMessage
* - AAAFreeMessage() - free up the memory used in a Diameter message
* <p>
* - AAASendMessage() - asynchronously send a message
* - AAASendMessageToPeer() - asynchronously send a message to a forced peer
* - AAASendRecvMessage() - synchronously send a message and get the response
* - AAASendRecvMessageToPeer() - synchronously send a message and get the response to a forced peer
* <p>
* - AAACreateSession() - create a diameter #AAASessionId
* - AAADropSession() - drop a diameter #AAASessionId
* <p>
* - AAACreateTransaction() - create a diameter #AAATransaction
* - AAADropTransaction() - drop a diameter #AAATransaction
* <p>
* - AAACreateAVP() - create an #AAA_AVP
* - AAAAddAVPToMessage() - add an #AAA_AVP to a #AAAMessage
* - AAAFindMatchingAVP() - find an #AAA_AVP inside a #AAAMessage
* - AAAGetNextAVP() - get the next #AAA_AVP from the #AAAMessage
* - AAAFreeAVP() - free the memory taken by the #AAA_AVP
* - AAAGroupAVPS() - group a #AAA_AVP_LIST of #AAA_AVP into a grouped #AAA_AVP
* - AAAUngroupAVPS() - ungroup a grouped #AAA_AVP into a #AAA_AVP_LIST of #AAA_AVP
* - AAAFindMatchingAVPList() - find an #AAA_AVP inside a #AAA_AVP_LIST
* - AAAFreeAVPList() - free the memory taken by all the members of #AAA_AVP_LIST
* <p>
* - AAAAddRequestHandler() - add a #AAARequestHandler_f callback to request being received
* - AAAAddResponseHandler() - add a #AAAResponseHandler_f callback to responses being received
*/
static cmd_export_t cdp_cmds[] = {
{"cdp_check_peer", (cmd_function)w_cdp_check_peer, 1, fixup_spve_null,
0, ANY_ROUTE},
{"cdp_has_app", (cmd_function)w_cdp_has_app, 1, fixup_igp_null,
0, ANY_ROUTE},
{"cdp_has_app", (cmd_function)w_cdp_has_app2, 2, fixup_igp_igp,
0, ANY_ROUTE},
{"load_cdp", (cmd_function)load_cdp, NO_SCRIPT, 0, 0},
EXP_FUNC(AAACreateRequest)
EXP_FUNC(AAACreateResponse)
EXP_FUNC(AAAFreeMessage)
EXP_FUNC(AAACreateAVP)
EXP_FUNC(AAAAddAVPToMessage)
EXP_FUNC(AAAAddAVPToList)
EXP_FUNC(AAAFindMatchingAVP)
EXP_FUNC(AAAFindMatchingAVPList)
EXP_FUNC(AAAGetNextAVP)
EXP_FUNC(AAAFreeAVP)
EXP_FUNC(AAAFreeAVPList)
EXP_FUNC(AAAGroupAVPS)
EXP_FUNC(AAAUngroupAVPS)
EXP_FUNC(AAASendMessage)
EXP_FUNC(AAASendMessageToPeer)
EXP_FUNC(AAASendRecvMessage)
EXP_FUNC(AAASendRecvMessageToPeer)
EXP_FUNC(AAAAddRequestHandler)
EXP_FUNC(AAAAddResponseHandler)
EXP_FUNC(AAACreateTransaction)
EXP_FUNC(AAADropTransaction)
EXP_FUNC(AAACreateSession)
EXP_FUNC(AAAMakeSession)
EXP_FUNC(AAAGetSession)
EXP_FUNC(AAADropSession)
EXP_FUNC(AAASessionsLock)
EXP_FUNC(AAASessionsUnlock)
EXP_FUNC(AAACreateClientAuthSession)
EXP_FUNC(AAACreateServerAuthSession)
EXP_FUNC(AAAGetAuthSession)
EXP_FUNC(AAADropAuthSession)
EXP_FUNC(AAATerminateAuthSession)
EXP_FUNC(AAACreateCCAccSession)
EXP_FUNC(AAAStartChargingCCAccSession)
EXP_FUNC(AAAGetCCAccSession)
EXP_FUNC(AAADropCCAccSession)
EXP_FUNC(AAATerminateCCAccSession)
{ 0, 0, 0, 0, 0 }
};
/**
* Exported parameters.
* - config_file - Configuration filename. See configdtd.h for the structure and ConfigExample.xml.
*/
static param_export_t cdp_params[] = {
{ "config_file", PARAM_STRING, &config_file}, /**< configuration filename */
{ "latency_threshold", PARAM_INT, &latency_threshold}, /**<threshold above which we will log*/
{ "workerq_latency_threshold", PARAM_INT, &workerq_latency_threshold},/**<time threshold putting job into queue*/
{ "workerq_length_threshold_percentage", PARAM_INT, &workerq_length_threshold_percentage},/**<queue length threshold - percentage of max queue length*/
{"debug_heavy", PARAM_INT, &debug_heavy},
{ 0, 0, 0 }
};
/**
* Exported module interface
*/
struct module_exports exports = {
"cdp",
DEFAULT_DLFLAGS,
cdp_cmds, /**< Exported functions */
cdp_params, /**< Exported parameters */
0, /**< RPC cmds */
0, /**< pseudovariables */
0,
cdp_init, /**< Module initialization function */
cdp_child_init, /**< per-child init function */
cdp_exit
};
/**
* Module init function.
*
* - Initializes the diameter peer using the provided configuration file.
* - Registers with pt the required number of processes.
*/
static int cdp_init( void )
{
if (rpc_register_array(cdp_rpc) != 0) {
LM_ERR("failed to register RPC commands for CDP module\n");
return -1;
}
if (cdp_init_counters() != 0) {
LM_ERR("Failed to register counters for CDP modules\n");
return -1;
}
if (!diameter_peer_init(config_file)){
LM_ERR("error initializing the diameter peer\n");
return 1;
}
register_procs(2+config->workers + 2 * config->peers_cnt);
cfg_register_child(2+config->workers + 2 * config->peers_cnt);
return 0;
}
/**
* Child init function.
* - starts the DiameterPeer by forking the processes
* @param rank - id of the child
*/
static int cdp_child_init( int rank )
{
if (rank == PROC_MAIN) {
LM_INFO("CDiameterPeer child starting ...\n");
diameter_peer_start(0);
LM_INFO("... CDiameterPeer child started\n");
}
return 0;
}
/**
* Module termination function.
* - stop the DiameterPeer processes in a civilized manner
*/
static void cdp_exit( void )
{
LM_INFO("CDiameterPeer child stopping ...\n");
diameter_peer_destroy();
LM_INFO("... CDiameterPeer child stopped\n");
return;
}
int w_cdp_check_peer(sip_msg_t *msg, char *peer, char *p2) {
str s;
if(fixup_get_svalue(msg, (gparam_p)peer, &s)<0)
{
LM_ERR("cannot get the peer\n");
return -1;
}
if (s.len > 0) {
return check_peer(&s);
}
return -1;
}
static int w_cdp_has_app(sip_msg_t *msg, char *appid, char *param) {
unsigned int app_flags;
str app_s = STR_NULL;
int a;
if(msg == NULL)
return -1;
if (get_is_fparam(&a, &app_s, msg, (fparam_t *)appid, &app_flags) != 0) {
LM_ERR("no Vendor-ID\n");
return -1;
}
if(!(app_flags & PARAM_INT)) {
if(app_flags & PARAM_STR)
LM_ERR("unable to get app from [%.*s]\n", app_s.len,
app_s.s);
else
LM_ERR("unable to get app\n");
return -1;
}
return check_application(-1, a);
}
static int w_cdp_has_app2(sip_msg_t *msg, char *vendor, char *appid) {
unsigned int vendor_flags, app_flags;
str vendor_s = STR_NULL;
str app_s = STR_NULL;
int v, a;
if(msg == NULL)
return -1;
if (get_is_fparam(&v, &vendor_s, msg, (fparam_t *)vendor, &vendor_flags) != 0) {
LM_ERR("no Vendor-ID\n");
return -1;
}
if(!(vendor_flags & PARAM_INT)) {
if(vendor_flags & PARAM_STR)
LM_ERR("unable to get vendor from [%.*s]\n", vendor_s.len,
vendor_s.s);
else
LM_ERR("unable to get vendor\n");
return -1;
}
if (get_is_fparam(&a, &app_s, msg, (fparam_t *)appid, &app_flags) != 0) {
LM_ERR("no Vendor-ID\n");
return -1;
}
if(!(app_flags & PARAM_INT)) {
if(app_flags & PARAM_STR)
LM_ERR("unable to get app from [%.*s]\n", app_s.len,
app_s.s);
else
LM_ERR("unable to get app\n");
return -1;
}
return check_application(v, a);
}