forked from OpenSIPS/opensips
/
dlg_db_handler.c
2092 lines (1711 loc) · 59.7 KB
/
dlg_db_handler.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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2009-2014 OpenSIPS Solutions
* Copyright (C) 2007-2009 Voice System 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* --------
* 2007-05-10 initial version (ancuta)
* 2007-07-06 additional information saved in the database: cseq, contact,
* route set and socket_info for both caller and callee (ancuta)
* 2009-09-09 support for early dialogs added; proper handling of cseq
* while PRACK is used (bogdan)
*/
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "../../dprint.h"
#include "../../ut.h"
#include "../../timer.h"
#include "../../db/db.h"
#include "../../db/db_insertq.h"
#include "../../str.h"
#include "../../socket_info.h"
#include "dlg_hash.h"
#include "dlg_db_handler.h"
#include "dlg_cb.h"
#include "dlg_profile.h"
str dlg_id_column = str_init(DLG_ID_COL);
str call_id_column = str_init(CALL_ID_COL);
str from_uri_column = str_init(FROM_URI_COL);
str from_tag_column = str_init(FROM_TAG_COL);
str to_uri_column = str_init(TO_URI_COL);
str to_tag_column = str_init(TO_TAG_COL);
str state_column = str_init(STATE_COL);
str start_time_column = str_init(START_TIME_COL);
str timeout_column = str_init(TIMEOUT_COL);
str to_cseq_column = str_init(TO_CSEQ_COL);
str from_cseq_column = str_init(FROM_CSEQ_COL);
str to_ping_cseq_column = str_init(TO_PING_CSEQ_COL);
str from_ping_cseq_column = str_init(FROM_PING_CSEQ_COL);
str to_route_column = str_init(TO_ROUTE_COL);
str from_route_column = str_init(FROM_ROUTE_COL);
str to_contact_column = str_init(TO_CONTACT_COL);
str from_contact_column = str_init(FROM_CONTACT_COL);
str to_sock_column = str_init(TO_SOCK_COL);
str from_sock_column = str_init(FROM_SOCK_COL);
str mangled_fu_column = str_init(MANGLED_FU_COL);
str mangled_tu_column = str_init(MANGLED_TU_COL);
str vars_column = str_init(VARS_COL);
str profiles_column = str_init(PROFILES_COL);
str sflags_column = str_init(SFLAGS_COL);
str mflags_column = str_init(MFLAGS_COL);
str flags_column = str_init(FLAGS_COL);
str dialog_table_name = str_init(DIALOG_TABLE_NAME);
int dlg_db_mode = DB_MODE_NONE;
static db_con_t* dialog_db_handle = 0; /* database connection handle */
static db_func_t dialog_dbf;
extern int dlg_enable_stats;
extern int active_dlgs_cnt;
extern int early_dlgs_cnt;
extern stat_var *active_dlgs;
extern stat_var *early_dlgs;
extern int dlg_bulk_del_no;
static inline void set_final_update_cols(db_val_t *, struct dlg_cell *, int);
#define SET_BIGINT_VALUE(_val, _bigint)\
do{\
VAL_BIGINT(_val) = _bigint;\
VAL_NULL(_val) = 0;\
}while(0);
#define SET_INT_VALUE(_val, _int)\
do{\
VAL_INT(_val) = _int;\
VAL_NULL(_val) = 0;\
}while(0);
#define SET_STR_VALUE(_val, _str)\
do{\
if ( (_str).len != 0) { \
VAL_STR((_val)).s = (_str).s;\
VAL_STR((_val)).len = (_str).len;\
VAL_NULL(_val) = 0;\
} else { \
VAL_STR((_val)).s = NULL;\
VAL_STR((_val)).len = 0;\
VAL_NULL(_val) = 1;\
}\
}while(0);
#define GET_STR_VALUE(_res, _values, _index, _not_null, _unref)\
do{\
if (VAL_NULL((_values)+ (_index))) { \
if (_not_null) {\
if (_unref) unref_dlg(dlg,1);\
goto next_dialog; \
} else { \
(_res).s = 0; \
(_res).len = 0; \
}\
} else { \
(_res).s = VAL_STR((_values)+ (_index)).s;\
(_res).len = strlen(VAL_STR((_values)+ (_index)).s);\
} \
}while(0);
static int load_dialog_info_from_db(int dlg_hash_size);
int dlg_connect_db(const str *db_url)
{
if (dialog_db_handle) {
LM_CRIT("BUG - db connection found already open\n");
return -1;
}
if ((dialog_db_handle = dialog_dbf.init(db_url)) == 0)
return -1;
return 0;
}
static int use_dialog_table(void)
{
if(!dialog_db_handle){
LM_ERR("invalid database handle\n");
return -1;
}
dialog_dbf.use_table(dialog_db_handle, &dialog_table_name);
return 0;
}
static int remove_all_dialogs_from_db(void)
{
if (use_dialog_table()!=0)
return -1;
if(dialog_dbf.delete(dialog_db_handle, NULL, NULL, NULL, 0) < 0) {
LM_ERR("failed to delete database information\n");
return -1;
}
return 0;
}
int init_dlg_db(const str *db_url, int dlg_hash_size , int db_update_period)
{
/* Find a database module */
if (db_bind_mod(db_url, &dialog_dbf) < 0){
LM_ERR("Unable to bind to a database driver\n");
return -1;
}
if (dlg_connect_db(db_url)!=0){
LM_ERR("unable to connect to the database\n");
return -1;
}
if(db_check_table_version(&dialog_dbf, dialog_db_handle,
&dialog_table_name, DLG_TABLE_VERSION) < 0) {
LM_ERR("error during table version check.\n");
return -1;
}
if (dlg_db_mode == DB_MODE_DELAYED) {
if (register_timer("dlg-dbupdate",dialog_update_db, 0,
db_update_period, TIMER_FLAG_SKIP_ON_DELAY)<0 ) {
LM_ERR("failed to register update db\n");
return -1;
}
}
if( (load_dialog_info_from_db(dlg_hash_size) ) !=0 ){
LM_ERR("unable to load the dialog data\n");
return -1;
}
if (dlg_db_mode==DB_MODE_SHUTDOWN && remove_all_dialogs_from_db()!=0) {
LM_WARN("failed to properly remove all the dialogs form DB\n");
}
dialog_dbf.close(dialog_db_handle);
dialog_db_handle = 0;
return 0;
}
void destroy_dlg_db(void)
{
/* close the DB connection */
if (dialog_db_handle) {
dialog_dbf.close(dialog_db_handle);
dialog_db_handle = 0;
}
}
static int select_entire_dialog_table(db_res_t ** res, int *no_rows)
{
db_key_t query_cols[DIALOG_TABLE_TOTAL_COL_NO] = {
&dlg_id_column, &call_id_column, &from_uri_column,
&from_tag_column, &to_uri_column, &to_tag_column,
&start_time_column, &state_column, &timeout_column,
&from_cseq_column, &to_cseq_column, &from_route_column,
&to_route_column, &from_contact_column, &to_contact_column,
&from_sock_column, &to_sock_column, &vars_column,
&profiles_column, &sflags_column, &from_ping_cseq_column,
&to_ping_cseq_column,&flags_column, &mangled_fu_column,&mangled_tu_column,
&mflags_column};
if(use_dialog_table() != 0){
return -1;
}
/* select the whole tabel and all the columns */
if (DB_CAPABILITY(dialog_dbf, DB_CAP_FETCH)) {
if(dialog_dbf.query(dialog_db_handle,0,0,0,query_cols, 0,
DIALOG_TABLE_TOTAL_COL_NO, 0, 0) < 0) {
LM_ERR("Error while querying (fetch) database\n");
return -1;
}
*no_rows = estimate_available_rows( 4+255+128+64+128+64+64+64+11+11+4+4
+512+512+128+128+64+64+4+4+4+4096+512+4+4+4 ,DIALOG_TABLE_TOTAL_COL_NO );
if (*no_rows==0) *no_rows = 10;
if(dialog_dbf.fetch_result(dialog_db_handle,res,*no_rows)<0){
LM_ERR("fetching rows failed\n");
return -1;
}
} else {
if(dialog_dbf.query(dialog_db_handle,0,0,0,query_cols, 0,
DIALOG_TABLE_TOTAL_COL_NO, 0, res) < 0) {
LM_ERR("Error while querying database\n");
return -1;
}
}
return 0;
}
struct socket_info * create_socket_info(db_val_t * vals, int n){
struct socket_info * sock;
str host, p;
int port, proto;
/* socket name */
p.s = (VAL_STR(vals+n)).s;
p.len = strlen(p.s);
if (VAL_NULL(vals+n) || p.s==0 || p.s[0]==0){
sock = 0;
} else {
if (parse_phostport( p.s, p.len, &host.s, &host.len,
&port, &proto)!=0) {
LM_ERR("bad socket <%.*s>\n", p.len, p.s);
return 0;
}
sock = grep_sock_info( &host, (unsigned short)port, proto);
if (sock==0) {
LM_WARN("non-local socket <%.*s>...ignoring\n", p.len, p.s);
}
}
return sock;
}
static inline void strip_esc(str *s)
{
char *c = s->s;
int len = s->len;
for ( ; len > 0; len--, c++) {
if (*c == '\\' && len > 0 &&
(*(c+1)=='\\' || *(c+1)=='#' || *(c+1)=='|')) {
memmove(c, c + 1, len - 1);
s->len--;
len--;
}
}
}
static inline char* read_pair(char *b, char *end, str *name, str *val)
{
/* read name */
name->s = b;
while( b<end && !( (*b=='|'|| *b=='#') &&
(*(b-1)!='\\' || *(b-2)=='\\')) )
b++;
if (b==end) return NULL;
if (*b=='|') goto skip;
name->len = b - name->s;
if (name->len==0) goto skip;
strip_esc(name);
/*LM_DBG("-----read name <%.*s>(%d)\n",name->len,name->s,name->len);*/
/* read # */
b++;
/* read value */
val->s = b;
while( b<end && !( (*b=='|'|| *b=='#') &&
(*(b-1)!='\\' || *(b-2)=='\\')) )
b++;
if (b==end) return NULL;
if (*b=='#') goto skip;
val->len = b - val->s;
if (val->len==0) val->s = 0;
strip_esc(val);
/*LM_DBG("-----read value <%.*s>(%d)\n",val->len,val->s,val->len);*/
/* read | */
b++;
return b;
skip:
while(b<end && *b=='|' && *(b-1)!='\\') b++;
if (b!=end) b++;
return (b==end)?NULL:b;
}
/* The function is always considered to be lock-less ( safe )
* it's either called when dialog is not linked yes, or is under the dialog lock */
void read_dialog_vars(char *b, int l, struct dlg_cell *dlg)
{
str name, val;
char *end;
char *p;
end = b + l;
p = b;
do {
/* read a new pair from input string */
p = read_pair( p, end, &name, &val);
if (p==NULL) break;
if (val.len==0) continue;
LM_DBG("new var found <%.*s>=<%.*s>\n",name.len,name.s,val.len,val.s);
/* add the variable */
if (store_dlg_value_unsafe( dlg, &name, &val)!=0)
LM_ERR("failed to add val, skipping...\n");
} while(p!=end);
}
void read_dialog_profiles(char *b, int l, struct dlg_cell *dlg,int double_check,
char is_replicated)
{
struct dlg_profile_table *profile;
struct dlg_profile_link *it;
str name, val,double_check_name;
char *end;
char *p,*s,*e;
char bk;
unsigned repl_type;
end = b + l;
p = b;
do {
/* read a new pair from input string */
p = read_pair( p, end, &name, &val);
if (p==NULL) break;
LM_DBG("new profile found <%.*s>=<%.*s>\n",name.len,name.s,val.len,val.s);
if (double_check) {
LM_DBG("Double checking profile - if it exists we'll skip it \n");
repl_type = REPL_NONE;
/* check if this is a shared profile, and remove /s for manual
* matching */
double_check_name = name;
s = memchr(name.s, '/', name.len);
if (s) {
e = double_check_name.s + double_check_name.len;
double_check_name.len = s - double_check_name.s;
trim_spaces_lr( double_check_name );
/* skip spaces after p */
for (++s; *s == ' ' && s < e; s++);
if ( s < e && *s == 's')
repl_type = REPL_CACHEDB;
else if (s < e && *s == 'b')
repl_type = REPL_PROTOBIN;
}
for (it=dlg->profile_links;it;it=it->next) {
if (it->profile->repl_type == repl_type &&
it->profile->name.len == double_check_name.len &&
memcmp(it->profile->name.s,double_check_name.s,
double_check_name.len) == 0) {
LM_DBG("Profile is already linked into the dlg\n");
goto next;
}
}
}
/* add to the profile */
profile = search_dlg_profile( &name );
if (profile==NULL) {
LM_DBG("profile <%.*s> does not exist now, creating it\n",name.len,name.s);
/* create a new one */
bk = name.s[name.len];
name.s[name.len] = 0;
if (add_profile_definitions(name.s, (val.len && val.s)?1:0 ) != 0) {
LM_ERR("failed to add dialog profile <%.*s>\n", name.len, name.s);
name.s[name.len] = bk;
continue;
}
name.s[name.len] = bk;
/* double check the created profile */
profile = search_dlg_profile(&name);
if (profile == NULL) {
LM_CRIT("BUG - cannot find just added dialog profile <%.*s>\n", name.len, name.s);
continue;
}
}
if (set_dlg_profile( dlg, profile->has_value ? &val : NULL, profile,
is_replicated) < 0 )
LM_ERR("failed to add to profile, skipping....\n");
next:
;
} while(p!=end);
return;
}
int remove_ended_dlgs_from_db(void)
{
static db_ps_t my_ps = NULL;
db_val_t values[1];
db_key_t match_keys[1] = { &state_column};
if (use_dialog_table()!=0)
return -1;
VAL_TYPE(values) = DB_INT;
VAL_NULL(values) = 0;
VAL_INT(values) = DLG_STATE_DELETED ;
CON_PS_REFERENCE(dialog_db_handle) = &my_ps;
if(dialog_dbf.delete(dialog_db_handle, match_keys, 0, values, 1) < 0) {
LM_ERR("failed to delete database information\n");
return -1;
}
return 0;
}
static int load_dialog_info_from_db(int dlg_hash_size)
{
db_res_t * res;
db_val_t * values;
db_row_t * rows;
int i, nr_rows;
struct dlg_cell *dlg;
str callid, from_uri, to_uri, from_tag, to_tag;
str cseq1,cseq2,contact1,contact2,rroute1,rroute2,mangled_fu,mangled_tu;
unsigned int next_id;
int no_rows = 10;
struct socket_info *caller_sock,*callee_sock;
int found_ended_dlgs=0;
unsigned int hash_entry,hash_id;
res = 0;
if((nr_rows = select_entire_dialog_table(&res,&no_rows)) < 0)
goto error;
nr_rows = RES_ROW_N(res);
do {
LM_DBG("loading information from database for %i dialogs\n", nr_rows);
rows = RES_ROWS(res);
/* for every row---dialog */
for(i=0; i<nr_rows; i++){
values = ROW_VALUES(rows + i);
if (VAL_NULL(values) || VAL_TYPE(values) != DB_BIGINT) {
LM_ERR("column %.*s cannot be null/has wrong type %d -> skipping\n",
dlg_id_column.len,dlg_id_column.s,VAL_TYPE(values));
continue;
}
hash_entry = (unsigned int )(VAL_BIGINT(values) >> 32);
hash_id = (unsigned int)(VAL_BIGINT(values) & 0x00000000ffffffff);
if (VAL_NULL(values+6) || VAL_NULL(values+7)) {
LM_ERR("columns %.*s or/and %.*s cannot be null -> skipping\n",
start_time_column.len, start_time_column.s,
state_column.len, state_column.s);
continue;
}
if ( VAL_INT(values+7) == DLG_STATE_DELETED ) {
LM_INFO("dialog already terminated -> skipping\n");
found_ended_dlgs=1;
continue;
}
caller_sock = create_socket_info(values, 15);
callee_sock = create_socket_info(values, 16);
if (caller_sock == NULL || callee_sock == NULL) {
LM_ERR("Dialog in DB doesn't match any listening sockets");
continue;
}
/*restore the dialog info*/
GET_STR_VALUE(callid, values, 1, 1, 0);
GET_STR_VALUE(from_uri, values, 2, 1, 0);
GET_STR_VALUE(from_tag, values, 3, 1, 0);
GET_STR_VALUE(to_uri, values, 4, 1, 0);
if((dlg=build_new_dlg(&callid, &from_uri, &to_uri, &from_tag))==0){
LM_ERR("failed to build new dialog\n");
goto error;
}
if(dlg->h_entry != hash_entry){
LM_ERR("inconsistent hash data in the dialog database: "
"you may have restarted opensips using a different "
"hash_size: please erase %.*s database and restart\n"
"dlg : %u, db : %u\n",
dialog_table_name.len, dialog_table_name.s,
dlg->h_entry,hash_entry);
shm_free(dlg);
goto error;
}
/*link the dialog*/
link_dlg(dlg, 0);
dlg->h_id = hash_id;
next_id = d_table->entries[dlg->h_entry].next_id;
d_table->entries[dlg->h_entry].next_id =
(next_id <= dlg->h_id) ? (dlg->h_id+1) : next_id;
GET_STR_VALUE(to_tag, values, 5, 1, 1);
dlg->start_ts = VAL_INT(values+6);
dlg->state = VAL_INT(values+7);
if (dlg->state==DLG_STATE_CONFIRMED_NA ||
dlg->state==DLG_STATE_CONFIRMED) {
active_dlgs_cnt++;
} else if (dlg->state==DLG_STATE_EARLY) {
early_dlgs_cnt++;
}
GET_STR_VALUE(cseq1, values, 9 , 1, 1);
GET_STR_VALUE(cseq2, values, 10 , 1, 1);
GET_STR_VALUE(rroute1, values, 11, 0, 0);
GET_STR_VALUE(rroute2, values, 12, 0, 0);
GET_STR_VALUE(contact1, values, 13, 0, 1);
GET_STR_VALUE(contact2, values, 14, 0, 1);
GET_STR_VALUE(mangled_fu, values, 23,0,1);
GET_STR_VALUE(mangled_tu, values, 24,0,1);
/* add the 2 legs */
/* TODO - store SDP */
if ( (dlg_add_leg_info( dlg, &from_tag, &rroute1, &contact1,
&cseq1, caller_sock,0,0,0)!=0) ||
(dlg_add_leg_info( dlg, &to_tag, &rroute2, &contact2,
&cseq2, callee_sock,&mangled_fu,&mangled_tu,0)!=0) ) {
LM_ERR("dlg_set_leg_info failed\n");
/* destroy the dialog */
unref_dlg(dlg,1);
continue;
}
dlg->legs_no[DLG_LEG_200OK] = DLG_FIRST_CALLEE_LEG;
/* script variables */
if (!VAL_NULL(values+17)) {
if (VAL_TYPE(values+17) == DB_BLOB) {
read_dialog_vars( VAL_BLOB(values+17).s,
VAL_BLOB(values+17).len, dlg);
} else {
LM_ERR("non-blob variables column - cannot store dialog variables\n");
}
}
/* profiles */
if (!VAL_NULL(values+18))
read_dialog_profiles( VAL_STR(values+18).s,
strlen(VAL_STR(values+18).s), dlg, 0, 0);
/* script flags */
if (!VAL_NULL(values+19)) {
dlg->user_flags = VAL_INT(values+19);
}
/* module flags */
if (!VAL_NULL(values+25)) {
dlg->mod_flags = VAL_INT(values+25);
}
/* top hiding */
dlg->flags = VAL_INT(values+22);
if (dlg_db_mode==DB_MODE_SHUTDOWN)
dlg->flags |= DLG_FLAG_NEW;
/* calculcate timeout */
dlg->tl.timeout = (unsigned int)(VAL_INT(values+8)) + get_ticks();
if (dlg->tl.timeout<=(unsigned int)time(0))
dlg->tl.timeout = 0;
else
dlg->tl.timeout -= (unsigned int)time(0);
/* restore the timer values */
if (0 != insert_dlg_timer( &(dlg->tl), (int)dlg->tl.timeout )) {
LM_CRIT("Unable to insert dlg %p [%u:%u] "
"with clid '%.*s' and tags '%.*s' '%.*s'\n",
dlg, dlg->h_entry, dlg->h_id,
dlg->callid.len, dlg->callid.s,
dlg->legs[DLG_CALLER_LEG].tag.len,
dlg->legs[DLG_CALLER_LEG].tag.s,
dlg->legs[callee_idx(dlg)].tag.len,
ZSW(dlg->legs[callee_idx(dlg)].tag.s));
/* destroy the dialog */
unref_dlg(dlg,1);
continue;
}
/* reference the dialog as kept in the timer list */
ref_dlg(dlg,1);
LM_DBG("current dialog timeout is %u\n", dlg->tl.timeout);
dlg->lifetime = 0;
dlg->legs[DLG_CALLER_LEG].last_gen_cseq =
(unsigned int)(VAL_INT(values+20));
dlg->legs[callee_idx(dlg)].last_gen_cseq =
(unsigned int)(VAL_INT(values+21));
if (dlg->flags & DLG_FLAG_PING_CALLER || dlg->flags & DLG_FLAG_PING_CALLEE) {
if (0 != insert_ping_timer(dlg))
LM_CRIT("Unable to insert dlg %p into ping timer\n",dlg);
else {
/* reference dialog as kept in ping timer list */
ref_dlg(dlg,1);
}
}
if (dlg_db_mode == DB_MODE_DELAYED) {
/* to be later removed by timer */
ref_dlg(dlg,1);
}
next_dialog:
;
}
/* any more data to be fetched ?*/
if (DB_CAPABILITY(dialog_dbf, DB_CAP_FETCH)) {
if (dialog_dbf.fetch_result( dialog_db_handle, &res,no_rows) < 0) {
LM_ERR("fetching more rows failed\n");
goto error;
}
nr_rows = RES_ROW_N(res);
} else {
nr_rows = 0;
}
}while (nr_rows>0);
dialog_dbf.free_result(dialog_db_handle, res);
if (found_ended_dlgs)
remove_ended_dlgs_from_db();
return 0;
error:
dialog_dbf.free_result(dialog_db_handle, res);
if (found_ended_dlgs)
remove_ended_dlgs_from_db();
return -1;
}
static struct dlg_cell **dlg_del_holder=NULL;
static int dlg_del_curr_no=0;
static db_val_t *dlg_del_values=NULL;
static db_key_t *dlg_del_keys=NULL;
int dlg_timer_remove_from_db(struct dlg_cell *cell)
{
static db_ps_t my_ps = NULL;
int i;
/* here we are in the context of the dlg timer
* dialog d_entry is locked and we also have an extra ref
* that must be released when we attempt the delete */
if (dlg_del_holder == NULL) {
LM_DBG("First time dialog del is attempted\n");
/* allocate all needed structures */
dlg_del_holder = pkg_malloc(dlg_bulk_del_no *
sizeof(struct dlg_cell *));
if (!dlg_del_holder) {
LM_ERR("No more pkg for dlg delete holders\n");
return -1;
}
memset(dlg_del_holder,0,dlg_bulk_del_no*sizeof(struct dlg_cell *));
dlg_del_values = pkg_malloc(dlg_bulk_del_no *
sizeof(db_val_t));
if (!dlg_del_values) {
LM_ERR("No more pkg for dlg delete values\n");
pkg_free(dlg_del_holder);
return -1;
}
for (i=0;i<dlg_bulk_del_no;i++) {
VAL_TYPE(dlg_del_values+i) = DB_BIGINT;
VAL_NULL(dlg_del_values+i) = 0;
}
dlg_del_keys = pkg_malloc(dlg_bulk_del_no *
sizeof(db_key_t));
if (!dlg_del_keys) {
LM_ERR("No more pkg for dlg_delete keys\n");
pkg_free(dlg_del_holder);
pkg_free(dlg_del_values);
return -1;
}
for (i=0;i<dlg_bulk_del_no;i++)
dlg_del_keys[i] = &dlg_id_column;
}
/* store info in del holders */
VAL_BIGINT(dlg_del_values+dlg_del_curr_no) =
((long long)cell->h_entry << 32) | (cell->h_id);
dlg_del_holder[dlg_del_curr_no]=cell;
/* mark is as deleted so we don't care about it later
* in the timer */
cell->flags |= DLG_FLAG_DB_DELETED;
dlg_del_curr_no++;
if (dlg_del_curr_no == dlg_bulk_del_no) {
LM_DBG("triggering delete for %d dialogs\n",dlg_del_curr_no);
CON_PS_REFERENCE(dialog_db_handle) = &my_ps;
CON_USE_OR_OP(dialog_db_handle);
if(dialog_dbf.delete(dialog_db_handle, dlg_del_keys,
0, dlg_del_values, dlg_bulk_del_no) < 0)
LM_ERR("failed to delete bulk database information !!!\n");
/* from timer point of view, we are done with the dialogs */
for (i=0;i<dlg_bulk_del_no;i++) {
cell = dlg_del_holder[i];
unref_dlg_unsafe(cell,1,&(d_table->entries[cell->h_entry]));
}
dlg_del_curr_no = 0;
}
/* still not enough piled up dialogs - wait for more */
return 0;
}
int dlg_timer_flush_del(void)
{
struct dlg_cell *cell;
int i;
/* here we are in the context of the dlg timer
* we also have an extra ref
* that must be released when we attempt the delete */
if (dlg_del_curr_no > 0) {
CON_USE_OR_OP(dialog_db_handle);
if(dialog_dbf.delete(dialog_db_handle, dlg_del_keys,
0, dlg_del_values, dlg_del_curr_no) < 0)
LM_ERR("failed to delete bulk database information !!!\n");
/* from timer point of view, we are done with the dialogs */
for (i=0;i<dlg_del_curr_no;i++) {
cell = dlg_del_holder[i];
unref_dlg(cell,1);
}
dlg_del_curr_no = 0;
}
return 0;
}
int remove_dialog_from_db(struct dlg_cell * cell)
{
static db_ps_t my_ps = NULL;
db_val_t values[1];
db_key_t match_keys[1] = { &dlg_id_column };
/*if the dialog hasn 't been yet inserted in the database*/
LM_DBG("trying to remove a dialog, update_flag is %i\n", cell->flags);
if (cell->flags & DLG_FLAG_NEW)
return 0;
if (use_dialog_table()!=0)
return -1;
VAL_TYPE(values) = DB_BIGINT;
VAL_NULL(values) = 0;
VAL_BIGINT(values) = ((long long)cell->h_entry << 32) | (cell->h_id);
CON_PS_REFERENCE(dialog_db_handle) = &my_ps;
if(dialog_dbf.delete(dialog_db_handle, match_keys, 0, values, 1) < 0) {
LM_ERR("failed to delete database information\n");
return -1;
}
LM_DBG("callid was %.*s\n", cell->callid.len, cell->callid.s );
/* dialog saved */
run_dlg_callbacks( DLGCB_SAVED, cell, 0, DLG_DIR_NONE, 0);
return 0;
}
int update_dialog_timeout_info(struct dlg_cell * cell)
{
static db_ps_t my_ps_update = NULL;
struct dlg_entry entry;
db_val_t values[2];
db_key_t insert_keys[DIALOG_TABLE_TOTAL_COL_NO] = {
&dlg_id_column, &timeout_column};
if(use_dialog_table()!=0)
return -1;
if (!(cell->flags & DLG_FLAG_CHANGED))
return 0;
/* save only dialog's state and timeout */
VAL_TYPE(values) = DB_BIGINT;
VAL_TYPE(values+1) = DB_INT;
/* lock the entry */
entry = (d_table->entries)[cell->h_entry];
dlg_lock( d_table, &entry);
SET_BIGINT_VALUE(values, (((long long)cell->h_entry << 32) |
cell->h_id));
SET_INT_VALUE(values+1, (unsigned int)( (unsigned int)time(0) +
cell->tl.timeout - get_ticks()) );
CON_PS_REFERENCE(dialog_db_handle) = &my_ps_update;
if((dialog_dbf.update(dialog_db_handle, (insert_keys), 0,
(values), (insert_keys+1), (values+1), 1, 1)) !=0){
LM_ERR("could not update database timeout info\n");
goto error;
}
/* dialog saved */
run_dlg_callbacks( DLGCB_SAVED, cell, 0, DLG_DIR_NONE, 0);
cell->flags &= ~(DLG_FLAG_CHANGED);
dlg_unlock( d_table, &entry);
return 0;
error:
dlg_unlock( d_table, &entry);
return -1;
}
int update_dialog_dbinfo(struct dlg_cell * cell)
{
static db_ps_t my_ps_insert = NULL;
static db_ps_t my_ps_update = NULL;
static db_ps_t my_ps_update_vp = NULL;
struct dlg_entry entry;
db_val_t values[DIALOG_TABLE_TOTAL_COL_NO];
int callee_leg;
db_key_t insert_keys[DIALOG_TABLE_TOTAL_COL_NO] = {
&dlg_id_column, &call_id_column, &from_uri_column,
&from_tag_column, &to_uri_column, &to_tag_column,
&from_sock_column, &to_sock_column,
&start_time_column, &mangled_fu_column, &mangled_tu_column,
&state_column, &timeout_column,
&from_cseq_column, &to_cseq_column, &from_ping_cseq_column,
&to_ping_cseq_column,&flags_column,
&vars_column, &profiles_column, &sflags_column,
&mflags_column, &from_route_column,
&to_route_column, &from_contact_column,&to_contact_column};
if(use_dialog_table()!=0)
return -1;
callee_leg= callee_idx(cell);
if((cell->flags & DLG_FLAG_NEW) != 0){
/* save all the current dialogs information*/
VAL_TYPE(values) = DB_BIGINT;
VAL_TYPE(values+8) = VAL_TYPE(values+11) = VAL_TYPE(values+12) =
VAL_TYPE(values+15) =VAL_TYPE(values+16) = VAL_TYPE(values+17) =
VAL_TYPE(values+20) = VAL_TYPE(values+21) = DB_INT;
VAL_TYPE(values+1) = VAL_TYPE(values+2) = VAL_TYPE(values+3) =
VAL_TYPE(values+4) = VAL_TYPE(values+5) = VAL_TYPE(values+6) =
VAL_TYPE(values+7) = VAL_TYPE(values+9) = VAL_TYPE(values+10) =
VAL_TYPE(values+13) = VAL_TYPE(values+14) = VAL_TYPE(values+19) =
VAL_TYPE(values+22) = VAL_TYPE(values+23) = VAL_TYPE(values+24) =
VAL_TYPE(values+25) = DB_STR;
VAL_TYPE(values+18) = DB_BLOB;
/* lock the entry */
entry = (d_table->entries)[cell->h_entry];
dlg_lock( d_table, &entry);
SET_BIGINT_VALUE(values, (((long long)cell->h_entry << 32) |
cell->h_id));
/* to be later removed by timer */SET_STR_VALUE(values+1, cell->callid);
SET_STR_VALUE(values+2, cell->from_uri);
SET_STR_VALUE(values+3, cell->legs[DLG_CALLER_LEG].tag);
SET_STR_VALUE(values+4, cell->to_uri);
SET_STR_VALUE(values+5, cell->legs[callee_leg].tag);
SET_STR_VALUE(values+6, cell->legs[DLG_CALLER_LEG].bind_addr->sock_str);
if (cell->legs[callee_leg].bind_addr) {
SET_STR_VALUE(values+7,
cell->legs[callee_leg].bind_addr->sock_str);
} else {
VAL_NULL(values+7) = 1;
}
SET_INT_VALUE(values+8, cell->start_ts);
SET_STR_VALUE(values+9,cell->legs[callee_leg].from_uri);
SET_STR_VALUE(values+10,cell->legs[callee_leg].to_uri);
SET_INT_VALUE(values+11, cell->state);
SET_INT_VALUE(values+12, (unsigned int)( (unsigned int)time(0) +
cell->tl.timeout - get_ticks()) );
SET_STR_VALUE(values+13, cell->legs[DLG_CALLER_LEG].r_cseq);
SET_STR_VALUE(values+14, cell->legs[callee_leg].r_cseq);
SET_INT_VALUE(values+15,cell->legs[DLG_CALLER_LEG].last_gen_cseq);
SET_INT_VALUE(values+16,cell->legs[callee_leg].last_gen_cseq);
SET_INT_VALUE(values+17, cell->flags & ~(DLG_FLAG_NEW|DLG_FLAG_CHANGED|DLG_FLAG_VP_CHANGED));
set_final_update_cols(values+18, cell, 0);
SET_STR_VALUE(values+22, cell->legs[DLG_CALLER_LEG].route_set);
SET_STR_VALUE(values+23, cell->legs[callee_leg].route_set);
SET_STR_VALUE(values+24, cell->legs[DLG_CALLER_LEG].contact);
SET_STR_VALUE(values+25, cell->legs[callee_leg].contact);
CON_PS_REFERENCE(dialog_db_handle) = &my_ps_insert;