/
dp_db.c
702 lines (552 loc) · 15.2 KB
/
dp_db.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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
* $Id$
*
* Copyright (C) 2007-2008 Voice Sistem SRL
*
* This file is part of opensips, a free SIP server.
*
* opensips 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
*
* opensips 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2007-08-01 initial version (ancuta onofrei)
*/
#include <stdlib.h>
#include <string.h>
#include "../../dprint.h"
#include "../../ut.h"
#include "../../db/db.h"
#include "dp_db.h"
#include "dialplan.h"
str dp_db_url = {NULL, 0};
str dp_table_name = str_init(DP_TABLE_NAME);
str dpid_column = str_init(DPID_COL);
str pr_column = str_init(PR_COL);
str match_op_column = str_init(MATCH_OP_COL);
str match_exp_column = str_init(MATCH_EXP_COL);
str match_flags_column = str_init(MATCH_FLAGS_COL);
str subst_exp_column = str_init(SUBST_EXP_COL);
str repl_exp_column = str_init(REPL_EXP_COL);
str disabled_column = str_init(DISABLED_COL);
str attrs_column = str_init(ATTRS_COL);
static db_con_t* dp_db_handle = 0; /* database connection handle */
static db_func_t dp_dbf;
#define GET_STR_VALUE(_res, _values, _index)\
do{\
if ( VAL_NULL((_values)+ (_index)) ) { \
LM_ERR(" values %d is NULL - not allowed\n",_index);\
goto err;\
} \
(_res).s = VAL_STR((_values)+ (_index)).s;\
(_res).len = strlen(VAL_STR((_values)+ (_index)).s);\
}while(0);
void destroy_rule(dpl_node_t * rule);
void destroy_hash(dpl_id_t **rules_hash);
dpl_node_t * build_rule(db_val_t * values);
int add_rule2hash(dpl_node_t * rule, dp_table_list_t *table, int index);
void list_rule(dpl_node_t * );
void list_hash(dpl_id_t * , rw_lock_t *);
dp_table_list_p dp_tables = NULL;
dp_table_list_p dp_default_table = NULL;
dp_table_list_p dp_get_default_table(void)
{
return dp_default_table;
}
int init_db_data(dp_table_list_p dp_table)
{
if(dp_table_name.s == 0){
LM_ERR("invalid database table name\n");
return -1;
}
/* Find a database module */
if (db_bind_mod(&dp_db_url, &dp_dbf) < 0){
LM_ERR("unable to bind to a database driver\n");
return -1;
}
if(dp_connect_db() !=0)
return -1;
if(db_check_table_version(&dp_dbf, dp_db_handle, &dp_table->table_name,
DP_TABLE_VERSION) < 0) {
LM_ERR("error during table version check.\n");
goto error;
}
if(dp_load_db(dp_table) != 0){
LM_ERR("failed to load database data\n");
goto error;
}
dp_disconnect_db();
return 0;
error:
dp_disconnect_db();
return -1;
}
int dp_connect_db(void)
{
if(dp_db_handle){
LM_CRIT("BUG: connection to DB already open\n");
return -1;
}
if ((dp_db_handle = dp_dbf.init(&dp_db_url)) == 0){
LM_ERR("unable to connect to the database\n");
return -1;
}
return 0;
}
void dp_disconnect_db(void)
{
if(dp_db_handle){
dp_dbf.close(dp_db_handle);
dp_db_handle = 0;
}
}
int init_data(void)
{
dp_default_table = dp_add_table(&dp_table_name);
if (!dp_default_table) {
LM_ERR("couldn't add the default table\n");
return -1;
}
return 0;
}
void destroy_data(void)
{
dp_table_list_t *el, *next;
for (el = dp_tables; el && (next = el->next, 1); el = next) {
destroy_hash(&el->hash[0]);
destroy_hash(&el->hash[1]);
lock_destroy_rw(el->ref_lock);
shm_free(el);
el = 0;
}
}
int dp_load_all_db(void)
{
dp_table_list_t *el;
for (el = dp_tables; el; el = el->next) {
if (dp_load_db(el) < 0) {
LM_ERR("unable to load %.*s table\n",
el->table_name.len, el->table_name.s);
return -1;
}
}
return 0;
}
/*load rules from DB*/
int dp_load_db(dp_table_list_p dp_table)
{
int i, nr_rows;
db_res_t * res = 0;
db_val_t * values;
db_row_t * rows;
db_key_t query_cols[DP_TABLE_COL_NO] = {
&dpid_column, &pr_column,
&match_op_column, &match_exp_column, &match_flags_column,
&subst_exp_column, &repl_exp_column, &attrs_column };
db_key_t order = &pr_column;
/* disabled condition */
db_key_t cond_cols[1] = { &disabled_column };
db_val_t cond_val[1];
dpl_node_t *rule;
int no_rows = 10;
if( dp_table->crt_index != dp_table->next_index){
LM_WARN("a load command already generated, aborting reload...\n");
return 0;
}
if (dp_dbf.use_table(dp_db_handle, &dp_table->table_name) < 0){
LM_ERR("error in use_table\n");
return -1;
}
VAL_TYPE(cond_val) = DB_INT;
VAL_NULL(cond_val) = 0;
VAL_INT(cond_val) = 0;
if (DB_CAPABILITY(dp_dbf, DB_CAP_FETCH)) {
if(dp_dbf.query(dp_db_handle,cond_cols,0,cond_val,query_cols,1,
DP_TABLE_COL_NO, order, 0) < 0){
LM_ERR("failed to query database!\n");
return -1;
}
no_rows = estimate_available_rows( 4+4+4+64+4+64+64+128,
DP_TABLE_COL_NO);
if (no_rows==0) no_rows = 10;
if(dp_dbf.fetch_result(dp_db_handle, &res, no_rows)<0) {
LM_ERR("failed to fetch\n");
if (res)
dp_dbf.free_result(dp_db_handle, res);
return -1;
}
} else {
/*select the whole table and all the columns*/
if(dp_dbf.query(dp_db_handle,cond_cols,0,cond_val,query_cols,1,
DP_TABLE_COL_NO, order, &res) < 0){
LM_ERR("failed to query database\n");
return -1;
}
}
nr_rows = RES_ROW_N(res);
lock_start_write( dp_table->ref_lock );
dp_table->next_index = dp_table->crt_index == 0 ? 1 : 0;
if(nr_rows == 0){
LM_WARN("no data in the db\n");
goto end;
}
do {
for(i=0; i<RES_ROW_N(res); i++){
rows = RES_ROWS(res);
values = ROW_VALUES(rows+i);
if ((rule = build_rule(values)) == NULL ) {
LM_WARN(" failed to build rule -> skipping\n");
continue;
}
rule->table_id = i;
if(add_rule2hash(rule , dp_table, dp_table->next_index) != 0) {
LM_ERR("add_rule2hash failed\n");
goto err2;
}
}
if (DB_CAPABILITY(dp_dbf, DB_CAP_FETCH)) {
if(dp_dbf.fetch_result(dp_db_handle, &res, no_rows)<0) {
LM_ERR("failure while fetching!\n");
if (res)
dp_dbf.free_result(dp_db_handle, res);
lock_stop_write( dp_table->ref_lock );
return -1;
}
} else {
break;
}
} while(RES_ROW_N(res)>0);
end:
destroy_hash(&dp_table->hash[dp_table->crt_index]);
/*update data*/
dp_table->crt_index = dp_table->next_index;
/* release the exclusive writing access */
lock_stop_write( dp_table->ref_lock );
list_hash(dp_table->hash[dp_table->crt_index], dp_table->ref_lock);
dp_dbf.free_result(dp_db_handle, res);
return 0;
err2:
if(rule) destroy_rule(rule);
destroy_hash(&dp_table->hash[dp_table->next_index]);
dp_dbf.free_result(dp_db_handle, res);
dp_table->next_index = dp_table->crt_index;
/* if lock defined - release the exclusive writing access */
if(dp_table->ref_lock)
/* release the readers */
lock_stop_write( dp_table->ref_lock );
return -1;
}
int str_to_shm(str src, str * dest)
{
if(src.len ==0 || src.s ==0)
return 0;
dest->s = (char*)shm_malloc((src.len+1) * sizeof(char));
if(!dest->s){
LM_ERR("out of shm memory\n");
return -1;
}
memcpy(dest->s, src.s, src.len);
dest->s[src.len] = '\0';
dest->len = src.len;
return 0;
}
/*compile the expressions, and if ok, build the rule */
dpl_node_t * build_rule(db_val_t * values)
{
pcre * match_comp, *subst_comp;
struct subst_expr * repl_comp;
dpl_node_t * new_rule;
str match_exp, subst_exp, repl_exp, attrs;
int matchop;
int namecount;
matchop = VAL_INT(values+2);
if((matchop != REGEX_OP) && (matchop!=EQUAL_OP)){
LM_ERR("invalid value for match operator\n");
return NULL;
}
match_comp = subst_comp =0;
repl_comp = 0;
new_rule = 0;
GET_STR_VALUE(match_exp, values, 3);
if(matchop == REGEX_OP){
LM_DBG("Compiling %.*s expression with flag: %d\n",
match_exp.len, match_exp.s, VAL_INT(values+4));
match_comp = wrap_pcre_compile(match_exp.s, VAL_INT(values+4));
if(!match_comp){
LM_ERR("failed to compile match expression %.*s\n",
match_exp.len, match_exp.s);
goto err;
}
}
LM_DBG("building subst rule\n");
GET_STR_VALUE(subst_exp, values, 5);
if(subst_exp.s && subst_exp.len){
/* subst regexp */
subst_comp = wrap_pcre_compile(subst_exp.s, VAL_INT(values+4));
if(subst_comp == NULL){
LM_ERR("failed to compile subst expression\n");
goto err;
}
}
/* replace exp */
GET_STR_VALUE(repl_exp, values, 6);
if(repl_exp.len && repl_exp.s){
repl_comp = repl_exp_parse(repl_exp);
if(!repl_comp){
LM_ERR("failed to compile replacing expression %.*s\n",
repl_exp.len, repl_exp.s);
goto err;
}
}
pcre_fullinfo(
subst_comp, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
PCRE_INFO_CAPTURECOUNT, /* number of named substrings */
&namecount); /* where to put the answer */
LM_DBG("references:%d , max:%d\n",namecount,
repl_comp?repl_comp->max_pmatch:0);
if ( (repl_comp!=NULL) && (namecount<repl_comp->max_pmatch) &&
(repl_comp->max_pmatch!=0) ){
LM_ERR("repl_exp uses a non existing subexpression\n");
goto err;
}
new_rule = (dpl_node_t *)shm_malloc(sizeof(dpl_node_t));
if(!new_rule){
LM_ERR("out of shm memory(new_rule)\n");
goto err;
}
memset(new_rule, 0, sizeof(dpl_node_t));
if(str_to_shm(match_exp, &new_rule->match_exp)!=0)
goto err;
if (subst_comp)
if(str_to_shm(subst_exp, &new_rule->subst_exp)!=0)
goto err;
if (repl_comp)
if(str_to_shm(repl_exp, &new_rule->repl_exp)!=0)
goto err;
/*set the rest of the rule fields*/
new_rule->dpid = VAL_INT(values);
new_rule->pr = VAL_INT(values+1);
new_rule->match_flags = VAL_INT(values+4);
new_rule->matchop = matchop;
GET_STR_VALUE(attrs, values, 7);
if(str_to_shm(attrs, &new_rule->attrs)!=0)
goto err;
LM_DBG("attrs are %.*s\n",
new_rule->attrs.len, new_rule->attrs.s);
if (match_comp)
new_rule->match_comp = match_comp;
if (subst_comp)
new_rule->subst_comp = subst_comp;
if (repl_comp)
new_rule->repl_comp = repl_comp;
return new_rule;
err:
if(match_comp) wrap_pcre_free(match_comp);
if(subst_comp) wrap_pcre_free(subst_comp);
if(repl_comp) repl_expr_free(repl_comp);
if(new_rule) destroy_rule(new_rule);
return NULL;
}
int add_rule2hash(dpl_node_t * rule, dp_table_list_t *table, int index)
{
dpl_id_p crt_idp;
dpl_index_p indexp;
int new_id, bucket = 0;
if(!table){
LM_ERR("data not allocated\n");
return -1;
}
new_id = 0;
crt_idp = select_dpid(table, rule->dpid, index);
/*didn't find a dpl_id*/
if(!crt_idp){
crt_idp = shm_malloc(sizeof(dpl_id_t) +
(DP_INDEX_HASH_SIZE+1) * sizeof(dpl_index_t));
if(!crt_idp){
LM_ERR("out of shm memory (crt_idp)\n");
return -1;
}
memset(crt_idp, 0, sizeof(dpl_id_t) +
(DP_INDEX_HASH_SIZE+1) * sizeof(dpl_index_t));
crt_idp->dp_id = rule->dpid;
crt_idp->rule_hash = (dpl_index_t*)(crt_idp + 1);
new_id = 1;
LM_DBG("new dpl_id %i\n", rule->dpid);
}
switch (rule->matchop) {
case REGEX_OP:
indexp = &crt_idp->rule_hash[DP_INDEX_HASH_SIZE];
break;
case EQUAL_OP:
bucket = core_case_hash(&rule->match_exp, NULL, DP_INDEX_HASH_SIZE);
indexp = &crt_idp->rule_hash[bucket];
break;
default:
LM_ERR("SKIPPED RULE. Unsupported match operator (%d).\n",
rule->matchop);
goto err;
}
/* Add the new rule to the corresponding bucket */
rule->next = 0;
if(!indexp->first_rule)
indexp->first_rule = rule;
if(indexp->last_rule)
indexp->last_rule->next = rule;
indexp->last_rule = rule;
if(new_id){
crt_idp->next = table->hash[table->next_index];
table->hash[table->next_index] = crt_idp;
}
LM_DBG("added the rule id %i pr %i next %p to the "
" %i bucket\n", rule->dpid,
rule->pr, rule->next, rule->matchop == REGEX_OP?
DP_INDEX_HASH_SIZE : bucket);
return 0;
err:
if(new_id)
shm_free(crt_idp);
return -1;
}
void destroy_hash(dpl_id_t **rules_hash)
{
dpl_id_p crt_idp;
dpl_index_p indexp;
dpl_node_p rulep;
int i;
if(!rules_hash || !*rules_hash)
return;
for(crt_idp = *rules_hash; crt_idp; crt_idp = *rules_hash) {
for (i = 0, indexp = &crt_idp->rule_hash[i];
i <= DP_INDEX_HASH_SIZE;
i++, indexp = &crt_idp->rule_hash[i]) {
for (rulep = indexp->first_rule; rulep; rulep=indexp->first_rule) {
destroy_rule(rulep);
indexp->first_rule = rulep->next;
shm_free(rulep);
rulep = NULL;
}
}
*rules_hash = crt_idp->next;
shm_free(crt_idp);
crt_idp = NULL;
}
*rules_hash = NULL;
}
void destroy_rule(dpl_node_t * rule){
if(!rule)
return;
LM_DBG("destroying rule with priority %i\n",
rule->pr);
if(rule->match_comp)
wrap_pcre_free(rule->match_comp);
if(rule->subst_comp)
wrap_pcre_free(rule->subst_comp);
/*destroy repl_exp*/
if(rule->repl_comp)
repl_expr_free(rule->repl_comp);
if(rule->match_exp.s)
shm_free(rule->match_exp.s);
if(rule->subst_exp.s)
shm_free(rule->subst_exp.s);
if(rule->repl_exp.s)
shm_free(rule->repl_exp.s);
if(rule->attrs.s)
shm_free(rule->attrs.s);
}
dpl_id_p select_dpid(dp_table_list_p table, int id, int index)
{
dpl_id_p idp;
if(!table || !table->hash[index])
return NULL;
for(idp = table->hash[index]; idp!=NULL; idp = idp->next)
if(idp->dp_id == id)
return idp;
return NULL;
}
/* FOR DEBUG PURPOSES */
void list_hash(dpl_id_t * hash, rw_lock_t * ref_lock)
{
dpl_id_p crt_idp;
dpl_node_p rulep;
int i;
if(!hash)
return;
/* lock the data for reading */
lock_start_read( ref_lock );
for(crt_idp = hash; crt_idp; crt_idp = crt_idp->next) {
LM_DBG("DPID: %i, pointer %p\n", crt_idp->dp_id, crt_idp);
for (i = 0; i <= DP_INDEX_HASH_SIZE; i++) {
LM_DBG("BUCKET %d rules:\n", i);
for(rulep = crt_idp->rule_hash[i].first_rule; rulep;
rulep = rulep->next) {
list_rule(rulep);
}
}
}
/* we are done reading -> unref the data */
lock_stop_read( ref_lock );
}
void list_rule(dpl_node_t * rule)
{
LM_DBG("RULE %p: pr %i next %p match_exp %.*s match_flags %d, "
"subst_exp %.*s, repl_exp %.*s and attrs %.*s\n", rule,
rule->pr, rule->next,
rule->match_exp.len, rule->match_exp.s,
rule->match_flags,
rule->subst_exp.len, rule->subst_exp.s,
rule->repl_exp.len, rule->repl_exp.s,
rule->attrs.len, rule->attrs.s);
}
/* Retrieves the corresponding entry of the given table name */
dp_table_list_p dp_get_table(str * table)
{
dp_table_list_t *el;
el = dp_tables;
while (el && str_strcmp(table, &el->table_name)) {
el = el->next;
}
return el;
}
/* Adds a new separate table and loads all its rules in shm */
dp_table_list_p dp_add_table(str * table)
{
dp_table_list_t *el;
if ((el = dp_get_table(table)) != NULL)
return el;
el = shm_malloc(sizeof(*el) + table->len);
if (el == NULL) {
LM_ERR("No more shm mem\n");
return NULL;
}
memset(el, 0, sizeof(*el));
/* create & init lock */
if((el->ref_lock = lock_init_rw()) == NULL) {
LM_ERR("Failed to init lock\n");
shm_free(el);
return NULL;
}
el->table_name.s = (char *)(el + 1);
el->table_name.len = table->len;
memcpy(el->table_name.s, table->s, table->len);
if (init_db_data(el) != 0) {
LM_ERR("Unable to init db data\n");
shm_free(el);
return NULL;
}
el->next = dp_tables;
dp_tables = el;
LM_DBG("Added dialplan table [%.*s].\n", table->len, table->s);
return el;
}