/
dialplan.c
672 lines (554 loc) · 15.1 KB
/
dialplan.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
* Copyright (C) 2007-2008 Voice Sistem SRL
*
* Copyright (C) 2008 Juha Heinanen
*
* Copyright (C) 2014 Olle E. Johansson, Edvina AB
*
* 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
* \brief Kamailio dialplan :: Module interface
* \ingroup dialplan
* Module: \ref dialplan
*/
/*! \defgroup dialplan Kamailio dialplan transformations module
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "../../core/sr_module.h"
#include "../../lib/srdb1/db.h"
#include "../../core/dprint.h"
#include "../../core/error.h"
#include "../../core/ut.h"
#include "../../core/action.h"
#include "../../core/pvar.h"
#include "../../core/dset.h"
#include "../../core/mem/mem.h"
#include "../../core/parser/parse_to.h"
#include "../../core/rpc.h"
#include "../../core/rpc_lookup.h"
#include "../../core/lvalue.h"
#include "dialplan.h"
#include "dp_db.h"
MODULE_VERSION
#define DEFAULT_PARAM "$rU"
static int mod_init(void);
static int child_init(int rank);
static void mod_destroy();
static int dialplan_init_rpc(void);
static int dp_translate_f(struct sip_msg* msg, char* str1, char* str2);
static int dp_trans_fixup(void ** param, int param_no);
static int dp_reload_f(struct sip_msg* msg);
str attr_pvar_s = STR_NULL;
pv_spec_t * attr_pvar = NULL;
str default_param_s = str_init(DEFAULT_PARAM);
dp_param_p default_par2 = NULL;
int dp_fetch_rows = 1000;
int dp_match_dynamic = 0;
static param_export_t mod_params[]={
{ "db_url", PARAM_STR, &dp_db_url },
{ "table_name", PARAM_STR, &dp_table_name },
{ "dpid_col", PARAM_STR, &dpid_column },
{ "pr_col", PARAM_STR, &pr_column },
{ "match_op_col", PARAM_STR, &match_op_column },
{ "match_exp_col", PARAM_STR, &match_exp_column },
{ "match_len_col", PARAM_STR, &match_len_column },
{ "subst_exp_col", PARAM_STR, &subst_exp_column },
{ "repl_exp_col", PARAM_STR, &repl_exp_column },
{ "attrs_col", PARAM_STR, &attrs_column },
{ "attrs_pvar", PARAM_STR, &attr_pvar_s },
{ "fetch_rows", PARAM_INT, &dp_fetch_rows },
{ "match_dynamic", PARAM_INT, &dp_match_dynamic },
{0,0,0}
};
static cmd_export_t cmds[]={
{"dp_translate",(cmd_function)dp_translate_f, 2, dp_trans_fixup, 0,
ANY_ROUTE},
{"dp_translate",(cmd_function)dp_translate_f, 1, dp_trans_fixup, 0,
ANY_ROUTE},
{"dp_reload",(cmd_function)dp_reload_f, 0, 0, 0,
ANY_ROUTE},
{0,0,0,0,0,0}
};
struct module_exports exports= {
"dialplan", /* module's name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* exported functions */
mod_params, /* param exports */
0, /* exported statistics */
0, /* exported MI functions */
0, /* exported pseudo-variables */
0, /* additional processes */
mod_init, /* module initialization function */
0, /* reply processing function */
mod_destroy,
child_init /* per-child init function */
};
static int mod_init(void)
{
if(dialplan_init_rpc()!=0)
{
LM_ERR("failed to register RPC commands\n");
return -1;
}
LM_DBG("db_url=%s/%d/%p\n", ZSW(dp_db_url.s), dp_db_url.len,dp_db_url.s);
if(attr_pvar_s.s && attr_pvar_s.len>0) {
attr_pvar = pv_cache_get(&attr_pvar_s);
if( (attr_pvar==NULL) ||
((attr_pvar->type != PVT_AVP) &&
(attr_pvar->type != PVT_XAVP) &&
(attr_pvar->type!=PVT_SCRIPTVAR))) {
LM_ERR("invalid pvar name\n");
return -1;
}
}
default_par2 = (dp_param_p)shm_malloc(sizeof(dp_param_t));
if(default_par2 == NULL){
LM_ERR("no shm more memory\n");
return -1;
}
memset(default_par2, 0, sizeof(dp_param_t));
/* emulate "$rU/$rU" as second parameter for dp_translate() */
default_param_s.len = strlen(default_param_s.s);
default_par2->v.sp[0] = pv_cache_get(&default_param_s);
if (default_par2->v.sp[0]==NULL) {
LM_ERR("input pv is invalid\n");
return -1;
}
default_param_s.len = strlen(default_param_s.s);
default_par2->v.sp[1] = pv_cache_get(&default_param_s);
if (default_par2->v.sp[1]==NULL) {
LM_ERR("output pv is invalid\n");
return -1;
}
if(dp_fetch_rows<=0)
dp_fetch_rows = 1000;
if(init_data() != 0) {
LM_ERR("could not initialize data\n");
return -1;
}
return 0;
}
static int child_init(int rank)
{
return 0;
}
static void mod_destroy(void)
{
/*destroy shared memory*/
if(default_par2){
shm_free(default_par2);
default_par2 = NULL;
}
destroy_data();
}
static int dp_get_ivalue(struct sip_msg* msg, dp_param_p dp, int *val)
{
pv_value_t value;
if(dp->type==DP_VAL_INT) {
*val = dp->v.id;
LM_DBG("dpid is %d from constant argument\n", *val);
return 0;
}
LM_DBG("searching %d\n",dp->v.sp[0]->type);
if( pv_get_spec_value( msg, dp->v.sp[0], &value)!=0
|| value.flags&(PV_VAL_NULL|PV_VAL_EMPTY) || !(value.flags&PV_VAL_INT)) {
LM_ERR("no AVP, XAVP or SCRIPTVAR found (error in scripts)\n");
return -1;
}
*val = value.ri;
LM_DBG("dpid is %d from pv argument\n", *val);
return 0;
}
static int dp_get_svalue(struct sip_msg * msg, pv_spec_t *spec, str* val)
{
pv_value_t value;
LM_DBG("searching %d \n", spec->type);
if ( pv_get_spec_value(msg,spec,&value)!=0 || value.flags&PV_VAL_NULL
|| value.flags&PV_VAL_EMPTY || !(value.flags&PV_VAL_STR)){
LM_ERR("no AVP, XAVP or SCRIPTVAR found (error in scripts)\n");
return -1;
}
*val = value.rs;
return 0;
}
static int dp_update(struct sip_msg * msg, pv_spec_t * src, pv_spec_t * dest,
str * repl, str * attrs)
{
int no_change;
pv_value_t val;
memset(&val, 0, sizeof(pv_value_t));
val.flags = PV_VAL_STR;
no_change = (dest==NULL) || (dest->type == PVT_NONE) || (!repl->s) || (!repl->len);
if (no_change)
goto set_attr_pvar;
val.rs = *repl;
if(dest->setf(msg, &dest->pvp, (int)EQ_T, &val)<0)
{
LM_ERR("setting dst pseudo-variable failed\n");
return -1;
}
if(is_route_type(FAILURE_ROUTE)
&& (dest->type==PVT_RURI || dest->type==PVT_RURI_USERNAME)) {
if (append_branch(msg, 0, 0, 0, Q_UNSPECIFIED, 0, 0, 0, 0, 0, 0) != 1) {
LM_ERR("append_branch action failed\n");
return -1;
}
}
set_attr_pvar:
if(attr_pvar==NULL || attrs==NULL)
return 0;
val.rs = *attrs;
if(attr_pvar->setf(msg, &attr_pvar->pvp, (int)EQ_T, &val)<0)
{
LM_ERR("setting attr pseudo-variable failed\n");
return -1;
}
return 0;
}
static int dp_translate_f(struct sip_msg* msg, char* str1, char* str2)
{
int dpid;
str input, output;
dpl_id_p idp;
dp_param_p id_par, repl_par;
str attrs, * attrs_par;
if(!msg)
return -1;
/*verify first param's value*/
id_par = (dp_param_p) str1;
if (dp_get_ivalue(msg, id_par, &dpid) != 0){
LM_ERR("no dpid value\n");
return -1;
}
if ((idp = select_dpid(dpid)) ==0 ){
LM_DBG("no information available for dpid %i\n", dpid);
return -2;
}
repl_par = (str2!=NULL)? ((dp_param_p)str2):default_par2;
if (dp_get_svalue(msg, repl_par->v.sp[0], &input)!=0){
LM_ERR("invalid param 2\n");
return -1;
}
LM_DBG("input is %.*s\n", input.len, input.s);
attrs_par = (!attr_pvar)?NULL:&attrs;
if (translate(msg, input, &output, idp, attrs_par)!=0){
LM_DBG("could not translate %.*s "
"with dpid %i\n", input.len, input.s, idp->dp_id);
return -1;
}
LM_DBG("input %.*s with dpid %i => output %.*s\n",
input.len, input.s, idp->dp_id, output.len, output.s);
/* set the output */
if (dp_update(msg, repl_par->v.sp[0], repl_par->v.sp[1],
&output, attrs_par) !=0){
LM_ERR("cannot set the output\n");
return -1;
}
return 1;
}
#define verify_par_type(_par_no, _spec)\
do{\
if( ((_par_no == 1) \
&& (_spec->type != PVT_AVP) && (_spec->type != PVT_XAVP) && \
(_spec->type!=PVT_SCRIPTVAR) )\
||((_par_no == 2) \
&& (_spec->type != PVT_AVP) && (_spec->type != PVT_XAVP) && \
(_spec->type!=PVT_SCRIPTVAR) \
&& (_spec->type!=PVT_RURI) && (_spec->type!=PVT_RURI_USERNAME))){\
\
LM_ERR("Unsupported Parameter TYPE[%d]\n", _spec->type);\
return E_UNSPEC;\
}\
}while(0);
/* first param: DPID: type: INT, AVP, XAVP, SVAR
* second param: SRC type: any psedo variable type
* second param: DST type: RURI, RURI_USERNAME, AVP, XAVP, SVAR, N/A
* default value for the second param: $ru.user/$ru.user
*/
static int dp_trans_fixup(void ** param, int param_no){
int dpid;
dp_param_p dp_par= NULL;
char *p, *s=NULL;
str lstr;
if(param_no!=1 && param_no!=2)
return 0;
p = (char*)*param;
if(!p || (*p == '\0')){
LM_DBG("null param %i\n", param_no);
return E_CFG;
}
LM_DBG("param_no is %i\n", param_no);
dp_par = (dp_param_p)pkg_malloc(sizeof(dp_param_t));
if(dp_par == NULL){
LM_ERR("no more pkg memory\n");
return E_OUT_OF_MEM;
}
memset(dp_par, 0, sizeof(dp_param_t));
if(param_no == 1) {
if(*p != '$') {
dp_par->type = DP_VAL_INT;
lstr.s = *param; lstr.len = strlen(*param);
if(str2sint(&lstr, &dpid) != 0) {
LM_ERR("bad number <%s>\n",(char *)(*param));
pkg_free(dp_par);
return E_CFG;
}
dp_par->type = DP_VAL_INT;
dp_par->v.id = dpid;
}else{
lstr.s = p; lstr.len = strlen(p);
dp_par->v.sp[0] = pv_cache_get(&lstr);
if (dp_par->v.sp[0]==NULL) {
pkg_free(dp_par);
goto error;
}
verify_par_type(param_no, dp_par->v.sp[0]);
dp_par->type = DP_VAL_SPEC;
}
} else {
if (((s = strchr(p, '/')) != 0) && (*(s+1)=='\0'))
goto error;
if (s != 0) {
*s = '\0'; s++;
}
lstr.s = p; lstr.len = strlen(p);
dp_par->v.sp[0] = pv_cache_get(&lstr);
if(dp_par->v.sp[0]==NULL) {
pkg_free(dp_par);
goto error;
}
if (s != 0) {
lstr.s = s; lstr.len = strlen(s);
dp_par->v.sp[1] = pv_cache_get(&lstr);
if (dp_par->v.sp[1]==NULL) {
pkg_free(dp_par);
goto error;
}
verify_par_type(param_no, dp_par->v.sp[1]);
}
dp_par->type = DP_VAL_SPEC;
}
*param = (void *)dp_par;
return 0;
error:
LM_ERR("failed to parse param %i\n", param_no);
return E_INVALID_PARAMS;
}
/**
* trigger reload of dialplan db records from config file
*/
static int dp_reload_f(struct sip_msg* msg)
{
if (dp_connect_db() < 0) {
LM_ERR("failed to reload rules fron database (db connect)\n");
return -1;
}
if(dp_load_db() != 0){
LM_ERR("failed to reload rules fron database (db load)\n");
dp_disconnect_db();
return -1;
}
dp_disconnect_db();
LM_DBG("reloaded dialplan\n");
return 1;
}
static const char* dialplan_rpc_reload_doc[2] = {
"Reload dialplan table from database",
0
};
/*
* RPC command to reload dialplan table
*/
static void dialplan_rpc_reload(rpc_t* rpc, void* ctx)
{
if (dp_connect_db() < 0) {
LM_ERR("failed to reload rules fron database (db connect)\n");
rpc->fault(ctx, 500, "DB Connection Error");
return;
}
if(dp_load_db() != 0){
LM_ERR("failed to reload rules fron database (db load)\n");
dp_disconnect_db();
rpc->fault(ctx, 500, "Dialplan Reload Failed");
return;
}
dp_disconnect_db();
return;
}
static const char* dialplan_rpc_translate_doc[2] = {
"Perform dialplan translation",
0
};
/*
* RPC command to perform dialplan translation
*/
static void dialplan_rpc_translate(rpc_t* rpc, void* ctx)
{
dpl_id_p idp;
str input;
int dpid;
str attrs = {"", 0};
str output = {0, 0};
void* th;
if (rpc->scan(ctx, "dS", &dpid, &input) < 2)
{
rpc->fault(ctx, 500, "Invalid parameters");
return;
}
if ((idp = select_dpid(dpid)) == 0 ){
LM_ERR("no information available for dpid %i\n", dpid);
rpc->fault(ctx, 500, "Dialplan ID not matched");
return;
}
if(input.s == NULL || input.len== 0) {
LM_ERR("empty input parameter\n");
rpc->fault(ctx, 500, "Empty input parameter");
return;
}
LM_DBG("trying to translate %.*s with dpid %i\n",
input.len, input.s, idp->dp_id);
if (translate(NULL, input, &output, idp, &attrs)!=0){
LM_DBG("could not translate %.*s with dpid %i\n",
input.len, input.s, idp->dp_id);
rpc->fault(ctx, 500, "No translation");
return;
}
LM_DBG("input %.*s with dpid %i => output %.*s\n",
input.len, input.s, idp->dp_id, output.len, output.s);
if (rpc->add(ctx, "{", &th) < 0)
{
rpc->fault(ctx, 500, "Internal error creating rpc");
return;
}
if(rpc->struct_add(th, "SS",
"Output", &output,
"Attributes", &attrs)<0)
{
rpc->fault(ctx, 500, "Internal error creating rpc");
return;
}
return;
}
/*
* RPC command to dump dialplan
*/
static void dialplan_rpc_dump(rpc_t* rpc, void* ctx)
{
dpl_id_p idp;
dpl_index_p indexp;
dpl_node_p rulep;
int dpid;
void* th;
void* ih;
void* sh;
if (rpc->scan(ctx, "d", &dpid) < 1)
{
rpc->fault(ctx, 500, "Missing parameter");
return;
}
if ((idp = select_dpid(dpid)) == 0 ) {
LM_ERR("no information available for dpid %i\n", dpid);
rpc->fault(ctx, 500, "Dialplan ID not matched");
return;
}
LM_DBG("trying to dump dpid %i\n", idp->dp_id);
/* add entry node */
if (rpc->add(ctx, "{", &th) < 0)
{
rpc->fault(ctx, 500, "Internal error root reply");
return;
}
if(rpc->struct_add(th, "d[",
"DPID", dpid,
"ENTRIES", &ih)<0)
{
rpc->fault(ctx, 500, "Internal error sets structure");
return;
}
for(indexp=idp->first_index; indexp!=NULL;indexp=indexp->next) {
LM_DBG("INDEX LEN: %i\n", indexp->len);
for(rulep = indexp->first_rule; rulep!= NULL;rulep = rulep->next) {
LM_DBG("DPID: %i PRIO : %i\n", rulep->dpid, rulep->pr);
if (rpc->struct_add(ih, "{","ENTRY", &sh) < 0)
{
rpc->fault(ctx, 500, "Internal error root reply");
return;
}
if (rpc->struct_add(sh, "dd", "PRIO", rulep->pr,
"MATCHOP", rulep->matchop)<0)
{
rpc->fault(ctx, 500, "Internal error adding prio");
return;
}
if (rpc->struct_add(sh, "s", "MATCHEXP", rulep->match_exp) < 0 )
{
rpc->fault(ctx, 500, "Internal error adding match exp");
return;
}
if (rpc->struct_add(sh, "d", "MATCHLEN", rulep->matchlen) < 0 )
{
rpc->fault(ctx, 500, "Internal error adding expression data and attribute");
return;
}
if (rpc->struct_add(sh, "s", "SUBSTEXP", rulep->subst_exp) < 0 )
{
rpc->fault(ctx, 500, "Internal error adding subst exp");
return;
}
if (rpc->struct_add(sh, "s", "REPLEXP", rulep->repl_exp) < 0 )
{
rpc->fault(ctx, 500, "Internal error adding replace exp ");
return;
}
if (rpc->struct_add(sh, "s", "ATTRS", rulep->attrs) < 0 )
{
rpc->fault(ctx, 500, "Internal error adding attribute");
return;
}
}
}
return;
}
static const char* dialplan_rpc_dump_doc[2] = {
"Dump dialplan content",
0
};
rpc_export_t dialplan_rpc_list[] = {
{"dialplan.reload", dialplan_rpc_reload,
dialplan_rpc_reload_doc, 0},
{"dialplan.translate", dialplan_rpc_translate,
dialplan_rpc_translate_doc, 0},
{"dialplan.dump", dialplan_rpc_dump,
dialplan_rpc_dump_doc, 0},
{0, 0, 0, 0}
};
static int dialplan_init_rpc(void)
{
if (rpc_register_array(dialplan_rpc_list)!=0)
{
LM_ERR("failed to register RPC commands\n");
return -1;
}
return 0;
}