-
Notifications
You must be signed in to change notification settings - Fork 564
/
acc_logic.c
1427 lines (1157 loc) · 35.3 KB
/
acc_logic.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
/*
* Accounting module logic
*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* -------
* 2006-09-19 forked from the acc_mod.c file during a big re-structuring
* of acc module (bogdan)
*/
#include <stdio.h>
#include <string.h>
#include "../../dprint.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_content.h"
#include "../tm/tm_load.h"
#include "../rr/api.h"
#include "../dialog/dlg_load.h"
#include "../dialog/dlg_hash.h"
#include "../../aaa/aaa.h"
#include "../../mod_fix.h"
#include "../../dprint.h"
#include "acc.h"
#include "acc_mod.h"
#include "acc_logic.h"
#include "acc_extra.h"
#include "acc_vars.h"
extern struct tm_binds tmb;
extern struct rr_binds rrb;
extern str flags_str;
extern str table_str;
extern str extra_str;
extern str leg_str;
extern str created_str;
extern str acc_created_avp_name;
extern int acc_created_avp_id;
extern int acc_flags_ctx_idx;
extern int acc_tm_flags_ctx_idx;
extern int acc_dlg_ctx_idx;
extern tag_t* extra_tags;
extern int extra_tgs_len;
extern tag_t* leg_tags;
extern int leg_tgs_len;
struct acc_enviroment acc_env;
static query_list_t *acc_ins_list = NULL;
static query_list_t *mc_ins_list = NULL;
static int is_cdr_enabled=0;
#define is_acc_flag_set(_mask, _type, _flag) ( _mask & ((_type * _flag)))
#define is_log_flag_on(_mask, _flag) is_acc_flag_set(_mask, DO_ACC_LOG, _flag)
#define is_log_acc_on(_mask) is_log_flag_on(_mask, DO_ACC)
#define is_log_cdr_on(_mask) is_log_flag_on(_mask, DO_ACC_CDR)
#define is_log_mc_on(_mask) is_log_flag_on(_mask, DO_ACC_MISSED)
#define is_log_failed_on(_mask) is_log_flag_on(_mask, DO_ACC_FAILED)
#define is_aaa_flag_on(_mask, _flag) is_acc_flag_set(_mask, DO_ACC_AAA, _flag)
#define is_aaa_acc_on(_mask) is_aaa_flag_on(_mask, DO_ACC)
#define is_aaa_cdr_on(_mask) is_aaa_flag_on(_mask, DO_ACC_CDR)
#define is_aaa_mc_on(_mask) is_aaa_flag_on(_mask, DO_ACC_MISSED)
#define is_aaa_failed_on(_mask) is_aaa_flag_on(_mask, DO_ACC_FAILED)
#define is_db_flag_on(_mask, _flag) is_acc_flag_set(_mask, DO_ACC_DB, _flag)
#define is_db_acc_on(_mask) is_db_flag_on(_mask, DO_ACC)
#define is_db_cdr_on(_mask) is_db_flag_on(_mask, DO_ACC_CDR)
#define is_db_mc_on(_mask) is_db_flag_on(_mask, DO_ACC_MISSED)
#define is_db_failed_on(_mask) is_db_flag_on(_mask, DO_ACC_FAILED)
#define is_evi_flag_on(_mask, _flag) is_acc_flag_set(_mask, DO_ACC_EVI, _flag)
#define is_evi_acc_on(_mask) is_evi_flag_on(_mask, DO_ACC)
#define is_evi_cdr_on(_mask) is_evi_flag_on(_mask, DO_ACC_CDR)
#define is_evi_mc_on(_mask) is_evi_flag_on(_mask, DO_ACC_MISSED)
#define is_evi_failed_on(_mask) is_evi_flag_on(_mask, DO_ACC_FAILED)
#define is_acc_on(_mask) \
( (is_log_acc_on(_mask)) || (is_db_acc_on(_mask)) \
|| (is_aaa_acc_on(_mask)) || (is_evi_acc_on(_mask)) )
#define is_cdr_acc_on(_mask) (is_log_cdr_on(_mask) || \
is_aaa_cdr_on(_mask) || is_db_cdr_on(_mask) || \
is_evi_cdr_on(_mask))
#define is_mc_acc_on(_mask) (is_log_mc_on(_mask) || \
is_aaa_mc_on(_mask) || is_db_mc_on(_mask) || \
is_evi_mc_on(_mask))
#define is_failed_acc_on(_mask) (is_log_failed_on(_mask) || \
is_aaa_failed_on(_mask) || is_db_failed_on(_mask) || \
is_evi_failed_on(_mask))
#define is_dialog_context(_mask) ((_mask)&ACC_DIALOG_CONTEXT)
#define set_cdr_values_registered(_mask) \
(_mask) |= ACC_CDR_REGISTERED;
#define set_dlg_cb_used(_mask) \
(_mask) |= ACC_DLG_CB_USED;
#define was_dlg_cb_used(_mask) (_mask&ACC_DLG_CB_USED)
#define cdr_values_registered(_mask) ((_mask)&ACC_CDR_REGISTERED)
#define acc_ref_unsafe(_ctx, _val) (_ctx)->ref_no += (_val)
#define acc_ref_ex(_ctx, _val) \
do { \
accX_lock(&(_ctx)->lock); \
acc_ref_unsafe(_ctx, _val); \
accX_unlock(&(_ctx)->lock); \
} while(0)
#define acc_unref_ex(_ctx, _val) \
do { \
accX_lock(&(_ctx)->lock); \
acc_ref_unsafe(_ctx, -(_val)); \
if ((_ctx)->ref_no == 0) {\
accX_unlock(&(_ctx)->lock); \
free_acc_ctx(_ctx); \
} else { \
if ((_ctx)->ref_no < 0) \
LM_BUG("ref=%d ctx=%p gone negative! (%s:%d)\n", \
(_ctx)->ref_no, (_ctx), __FILE__, __LINE__); \
accX_unlock(&(_ctx)->lock); \
} \
} while(0)
#define acc_ref(_ctx) acc_ref_ex(_ctx, 1)
#define acc_unref(_ctx) acc_unref_ex(_ctx, 1)
#define reset_flags(_flags, _flags_to_reset) \
_flags &= ~_flags_to_reset;
#define skip_cancel(_rq) \
(((_rq)->REQ_METHOD==METHOD_CANCEL) && report_cancels==0)
static void tmcb_func( struct cell* t, int type, struct tmcb_params *ps );
static void acc_dlg_ended(struct dlg_cell *dlg, int type,
struct dlg_cb_params *_params);
static void acc_dlg_onwrite(struct dlg_cell *dlg, int type,
struct dlg_cb_params *_params);
static void acc_cdr_cb( struct cell* t, int type, struct tmcb_params *ps );
void free_extra_array(extra_value_t* array, int array_len)
{
int i;
for (i=0; i < array_len; i++) {
if (array[i].value.s)
shm_free(array[i].value.s);
}
shm_free(array);
}
static inline void free_acc_ctx(acc_ctx_t* ctx)
{
int i;
struct dlg_cell *dlg;
if (ctx->extra_values)
free_extra_array(ctx->extra_values, extra_tgs_len);
if (ctx->leg_values) {
for (i=0; i<ctx->legs_no; i++) {
free_extra_array(ctx->leg_values[i], leg_tgs_len);
}
shm_free(ctx->leg_values);
}
if (ctx->acc_table.s)
shm_free(ctx->acc_table.s);
shm_free(ctx);
/* also cleanup dialog */
dlg = dlg_api.get_dlg ? dlg_api.get_dlg() : NULL;
if (dlg)
dlg_api.dlg_ctx_put_ptr(dlg, acc_dlg_ctx_idx, NULL);
}
static inline struct hdr_field* get_rpl_to( struct cell *t,
struct sip_msg *reply)
{
if (reply==FAKED_REPLY || !reply || !reply->to)
return t->uas.request->to;
else
return reply->to;
}
acc_ctx_t* try_fetch_ctx(void)
{
acc_ctx_t* ret=NULL;
struct cell* t;
struct dlg_cell* dlg;
t = tmb.t_gett ? tmb.t_gett() : NULL;
t = t==T_UNDEFINED ? NULL : t;
if ((ret = ACC_GET_CTX()) == NULL) {
t = tmb.t_gett ? tmb.t_gett() : NULL;
t = (t==T_UNDEFINED) ? NULL : t;
dlg = dlg_api.get_dlg ? dlg_api.get_dlg() : NULL;
/* search for the context in transaction context */
if (t && (ret=ACC_GET_TM_CTX(t))==NULL) {
/* try fetching the context from dialog only if dialog exists */
if (!dlg)
return NULL;
ret = dlg_api.dlg_ctx_get_ptr(dlg, acc_dlg_ctx_idx);
if (!ret)
/* can't find the context anywhere */
return NULL;
acc_ref_ex(ret, 2);
ACC_PUT_TM_CTX(t, ret);
ACC_PUT_CTX(ret);
} else if (ret) { /* we have the context in transaction */
/* in transaction; put them in dialog(if possible) and in processing context */
acc_ref(ret);
ACC_PUT_CTX(ret);
} else if (dlg) { /* no (context in) transaction; search only in dialog*/
ret = dlg_api.dlg_ctx_get_ptr(dlg, acc_dlg_ctx_idx);
if (!ret)
/* can't find the context anywhere */
return NULL;
if (t) {
acc_ref_ex(ret, 2); /* ref twice - for local and tm ctx */
ACC_PUT_TM_CTX(t, ret);
} else
acc_ref(ret); /* ref only once, for local ctx */
ACC_PUT_CTX(ret);
}
}
return ret;
}
static inline void env_set_to(struct hdr_field *to)
{
acc_env.to = to;
}
static inline void env_set_text(char *p, int len)
{
acc_env.text.s = p;
acc_env.text.len = len;
}
static inline void env_set_code_status( int code, struct sip_msg *reply)
{
static char code_buf[INT2STR_MAX_LEN];
acc_env.code = code;
if (reply==FAKED_REPLY || reply==NULL) {
/* code */
acc_env.code_s.s =
int2bstr((unsigned long)code, code_buf, &acc_env.code_s.len);
/* reason */
acc_env.reason.s = error_text(code);
acc_env.reason.len = strlen(acc_env.reason.s);
} else {
acc_env.code_s = reply->first_line.u.reply.status;
acc_env.reason = reply->first_line.u.reply.reason;
}
}
static inline void env_set_comment(struct acc_param *accp)
{
acc_env.code = accp->code;
acc_env.code_s = accp->code_s;
acc_env.reason = accp->reason;
}
static inline void env_set_event(event_id_t ev)
{
acc_env.event = ev;
}
static inline int acc_preparse_req(struct sip_msg *req)
{
if ( (parse_headers(req,HDR_CALLID_F|HDR_CSEQ_F|HDR_FROM_F|HDR_TO_F,0)<0)
|| (parse_from_header(req)<0 ) ) {
LM_ERR("failed to preparse request\n");
return -1;
}
return 0;
}
int w_acc_log_request(struct sip_msg *rq, str* comment)
{
struct acc_param accp;
if (acc_preparse_req(rq)<0)
return -1;
acc_comm_to_acc_param(rq, comment, &accp);
env_set_to( rq->to );
env_set_comment( &accp );
env_set_text( ACC_REQUEST, ACC_REQUEST_LEN);
return acc_log_request( rq, NULL, 0);
}
int w_acc_aaa_request(struct sip_msg *rq, str* comment)
{
struct acc_param accp;
if (!aaa_proto_url) {
LM_ERR("aaa support not configured\n");
return -1;
}
if (acc_preparse_req(rq)<0)
return -1;
acc_comm_to_acc_param(rq, comment, &accp);
env_set_to( rq->to );
env_set_comment( &accp );
return acc_aaa_request( rq, NULL, 0);
}
int w_acc_db_request(struct sip_msg *rq, str* comment, str *table)
{
struct acc_param accp;
if (!table) {
LM_ERR("db support not configured\n");
return -1;
}
if (acc_preparse_req(rq)<0)
return -1;
acc_comm_to_acc_param(rq, comment, &accp);
env_set_to( rq->to );
env_set_comment( &accp );
env_set_text(table->s, table->len);
if (str_strcmp(table, &db_table_mc) == 0) {
return acc_db_request(rq, NULL, &mc_ins_list, 0);
}
if (str_strcmp(table, &db_table_acc) == 0) {
return acc_db_request(rq, NULL, &acc_ins_list, 0);
}
return acc_db_request( rq, NULL,NULL, 0);
}
int w_acc_evi_request(struct sip_msg *rq, str* comment)
{
struct acc_param accp;
if (acc_preparse_req(rq)<0)
return -1;
acc_comm_to_acc_param(rq, comment, &accp);
env_set_to( rq->to );
env_set_comment( &accp );
#if 0
if (is_cdr_acc_on(rq) && is_evi_acc_on(rq)) {
env_set_event(acc_cdr_event);
} else if (is_evi_acc_on(rq) && acc_env.code < 300) {
env_set_event(acc_event);
} else if (is_evi_mc_on(rq)) {
env_set_event(acc_missed_event);
} else {
LM_WARN("evi request flags not set\n");
return 1;
}
#endif
if (acc_env.code < 300) {
env_set_event(acc_event);
} else {
env_set_event(acc_missed_event);
}
return acc_evi_request( rq, NULL, 0);
}
int acc_comm_to_acc_param(struct sip_msg* rq, str* comm, struct acc_param* accp)
{
accp->reason = *comm;
if (accp->reason.len>=3 && isdigit((int)comm->s[0])
&& isdigit((int)comm->s[1]) && isdigit((int)comm->s[2]) ) {
/* reply code is in the comment string */
accp->code = (comm->s[0]-'0')*100 + (comm->s[1]-'0')*10 + (comm->s[2]-'0');
accp->code_s.s = comm->s;
accp->code_s.len = 3;
accp->reason.s += 3;
accp->reason.len -= 3;
for( ; isspace((int)accp->reason.s[0]) ; accp->reason.s++,accp->reason.len-- );
} else {
/* no reply code */
accp->code = 0;
accp->code_s.s = NULL;
accp->code_s.len = 0;
}
/*Default comment if none supplied*/
if (accp->reason.len <= 0) {
accp->reason.s = error_text(accp->code);
accp->reason.len = strlen(accp->reason.s);
}
return 0;
}
static inline int has_totag(struct sip_msg *msg)
{
/* check if it has to tag */
if ( (!msg->to && parse_headers(msg, HDR_TO_F,0)<0) || !msg->to ) {
LM_ERR("bad request or missing TO hdr :-/\n");
return 0;
}
if (get_to(msg)->tag_value.s != 0 && get_to(msg)->tag_value.len != 0)
return 1;
return 0;
}
/* is this reply of interest for accounting ? */
static inline int should_acc_reply(struct sip_msg *req,struct sip_msg *rpl,
int code, unsigned long long* flags)
{
/* negative transactions reported otherwise only if explicitly
* demanded */
if ( !is_failed_acc_on(*flags) && code >=300 ) {
return 0;
}
if ( !is_acc_on(*flags) ) {
return 0;
}
if ( code<200 && !(early_media && rpl!=FAKED_REPLY &&
parse_headers(rpl,HDR_CONTENTLENGTH_F, 0)==0 && rpl->content_length &&
get_content_length(rpl)>0 ) ) {
return 0;
}
return 1; /* seed is through, we will account this reply */
}
/* parse incoming replies before cloning */
static inline void acc_onreply_in(struct cell *t, struct sip_msg *req,
struct sip_msg *reply, int code, acc_ctx_t* ctx)
{
/* don't parse replies in which we are not interested */
/* missed calls enabled ? */
if (reply
&& reply != FAKED_REPLY
&& (should_acc_reply(req,reply,code, &ctx->flags)
|| (is_invite(t)
&& code >= 300
&& is_mc_acc_on(ctx->flags)))) {
parse_headers(reply, HDR_TO_F, 0 );
}
}
/* initiate a report if we previously enabled MC accounting for this t */
static inline void on_missed(struct cell *t, struct sip_msg *req,
struct sip_msg *reply, int code, acc_ctx_t *ctx)
{
str new_uri_bk={0,0};
str dst_uri_bk={0,0};
unsigned long long flags_to_reset=0;
unsigned long long *flags = &ctx->flags;
if (t->nr_of_outgoings) {
/* set as new_uri the last branch */
new_uri_bk = req->new_uri;
dst_uri_bk = req->dst_uri;
req->new_uri = t->uac[t->nr_of_outgoings-1].uri;
req->dst_uri = t->uac[t->nr_of_outgoings-1].duri;
req->parsed_uri_ok = 0;
}
/* set env variables */
env_set_to( get_rpl_to(t,reply) );
env_set_code_status( code, reply);
/* we report on missed calls when the first
* forwarding attempt fails; we do not wish to
* report on every attempt; so we clear the flags;
*/
if (is_evi_mc_on(*flags)) {
env_set_event(acc_missed_event);
acc_evi_request( req, reply, is_evi_cdr_on(*flags) );
flags_to_reset |= DO_ACC_EVI * DO_ACC_MISSED;
}
if (is_log_mc_on(*flags)) {
env_set_text( ACC_MISSED, ACC_MISSED_LEN);
acc_log_request( req, reply, is_log_cdr_on(*flags) );
flags_to_reset |= DO_ACC_LOG * DO_ACC_MISSED;
}
if (is_aaa_mc_on(*flags)) {
acc_aaa_request( req, reply, is_aaa_cdr_on(*flags) );
flags_to_reset |= DO_ACC_AAA * DO_ACC_MISSED;
}
if (is_db_mc_on(*flags)) {
env_set_text(db_table_mc.s, db_table_mc.len);
acc_db_request( req, reply,&mc_ins_list, is_db_cdr_on(*flags));
flags_to_reset |= DO_ACC_DB * DO_ACC_MISSED;
}
/* Reset the accounting missed_flags
* These can't be reset in the blocks above, because
* it would skip accounting if the flags are identical
*/
if (t->nr_of_outgoings) {
req->new_uri = new_uri_bk;
req->dst_uri = dst_uri_bk;
req->parsed_uri_ok = 0;
}
reset_flags(*flags, flags_to_reset);
}
/*
* This mid-dialog request callback ensures that any extra/leg settings done
* at script level before dialog matching is performed will get properly
* transferred into the dialog context, once the dialog is matched
*/
static void acc_merge_contexts(struct dlg_cell *dlg, int type,
struct dlg_cb_params *_params)
{
acc_ctx_t *ctx;
/* if there is already a acc context in the processing
* context, be sure to destroy it for now */
if ((ctx = ACC_GET_CTX())) {
push_ctx_to_ctx( ctx, (acc_ctx_t *)(*_params->param));
acc_unref(ctx); /* unref it now beause it will disapear from local ctx */
}
ctx = (acc_ctx_t *)(*_params->param);
acc_ref(ctx);
ACC_PUT_CTX(ctx);
}
/* restore callbacks */
void acc_update_ctx_callback(struct dlg_cell *dlg, int type,
struct dlg_cb_params *_params)
{
str flags_s;
acc_ctx_t* ctx = (acc_ctx_t *)(*_params->param);
unsigned long long flags;
if (!dlg) {
LM_ERR("null dialog - cannot fetch message flags\n");
return;
}
/* check if the ctx exists in the dialog */
if (!ctx) {
LM_DBG("there's no dialog ctx created before - can't do anything\n");
return;
}
flags_s.s = (char *)&flags;
flags_s.len = sizeof(flags);
if (dlg_api.fetch_dlg_value(dlg, &flags_str, &flags_s, 1) < 0) {
LM_DBG("flags were not saved in dialog\n");
return;
}
ctx->flags = flags;
if (restore_dlg_extra_ctx(dlg, ctx)) {
LM_ERR("failed to rebuild acc context!\n");
return;
}
}
void acc_loaded_callback(struct dlg_cell *dlg, int type,
struct dlg_cb_params *_params) {
str flags_s, table_s, created_s;
acc_ctx_t* ctx;
time_t created;
unsigned long long flags;
if (!dlg) {
LM_ERR("null dialog - cannot fetch message flags\n");
return;
}
flags_s.s = (char *)&flags;
flags_s.len = sizeof(flags);
if (dlg_api.fetch_dlg_value(dlg, &flags_str, &flags_s, 1) < 0) {
LM_DBG("flags were not saved in dialog\n");
return;
}
created_s.s = (char *)&created;
created_s.len = sizeof(created);
if (dlg_api.fetch_dlg_value(dlg, &created_str, &created_s, 1) < 0) {
LM_DBG("created time was not saved in dialog\n");
return;
}
/**
* restore acc extra context(extra and leg values)
*/
if (restore_dlg_extra(dlg, &ctx)) {
LM_ERR("failed to rebuild acc context!\n");
return;
}
/* copy flags value into the context */
ctx->flags = flags;
/* copy created value into the context */
ctx->created = created;
/* restore accounting table if db accounting is used */
if (is_db_acc_on(ctx->flags)) {
if (dlg_api.fetch_dlg_value(dlg, &table_str, &table_s, 0) < 0) {
LM_DBG("table was not saved in dialog\n");
return;
}
if ((ctx->acc_table.s=shm_malloc(table_s.len)) == NULL) {
LM_ERR("no more shm!\n");
return;
}
memcpy(ctx->acc_table.s, table_s.s, table_s.len);
ctx->acc_table.len = table_s.len;
}
/* replace the context value with a good pointer */
dlg_api.dlg_ctx_put_ptr(dlg, acc_dlg_ctx_idx, ctx);
/* register database callbacks */
acc_ref_ex(ctx, 2);
if (dlg_api.register_dlgcb(dlg, DLGCB_TERMINATED |
DLGCB_EXPIRED, acc_dlg_ended, ctx, unref_acc_ctx)){
LM_ERR("cannot register callback for database accounting\n");
acc_unref_ex(ctx, 2);
return;
}
/* register dlg callbacks for ctx management */
if (dlg_api.register_dlgcb(dlg, DLGCB_REQ_WITHIN,
acc_merge_contexts, ctx, unref_acc_ctx) != 0) {
acc_unref(ctx); /* only one, the other one was successful */
LM_ERR("cannot register callback ctx management\n");
return;
}
/* register dlg callbacks for updating extra vars */
if (dlg_api.register_dlgcb(dlg, DLGCB_PROCESS_VARS,
acc_update_ctx_callback, ctx, NULL) != 0) {
acc_unref(ctx); /* only one, the other one was successful */
LM_ERR("cannot register callback ctx management\n");
return;
}
}
/* initiate a report if we previously enabled accounting for this t */
static inline void acc_onreply( struct cell* t, struct sip_msg *req,
struct sip_msg *reply, int code, acc_ctx_t* ctx)
{
str new_uri_bk;
str dst_uri_bk;
struct dlg_cell *dlg = NULL;
str table;
unsigned long long* flags = &ctx->flags;
/* acc_onreply is bound to TMCB_REPLY which may be called
from _reply, like when FR hits; we should not miss this
event for missed calls either */
if (is_invite(t) && code>=300 && is_mc_acc_on(*flags) ) {
on_missed(t, req, reply, code, ctx);
}
if (!should_acc_reply(req, reply, code, flags))
return;
/* for reply processing, set as new_uri the winning branch */
if (t->relaied_reply_branch>=0) {
new_uri_bk = req->new_uri;
dst_uri_bk = req->dst_uri;
req->new_uri = t->uac[t->relaied_reply_branch].uri;
req->dst_uri = t->uac[t->relaied_reply_branch].duri;
req->parsed_uri_ok = 0;
} else {
new_uri_bk.len = dst_uri_bk.len = -1;
new_uri_bk.s = dst_uri_bk.s = NULL;
}
/* set env variables */
env_set_to( get_rpl_to(t,reply) );
env_set_code_status( code, reply);
/* search for table avp */
if (is_db_acc_on(ctx->flags))
table = ctx->acc_table;
else {
table.s = 0;
table.len = 0;
}
if (is_invite(t) && !has_totag(req) && is_cdr_acc_on(ctx->flags) &&
code >= 200 && code < 300 && (dlg=dlg_api.get_dlg()) != NULL) {
/* if dialog module loaded and INVITE and success reply */
if (store_core_leg_values(dlg, req) < 0) {
LM_ERR("cannot store core and leg values\n");
goto restore;
}
/* store context pointer into dialog */
dlg_api.dlg_ctx_put_ptr(dlg, acc_dlg_ctx_idx, ctx);
/* register callback for program shutdown or dialog replication
* won't register free function since TERMINATED|EXPIRED callback
* free function will be called to free */
if (dlg_api.register_dlgcb(dlg, DLGCB_WRITE_VP,
acc_dlg_onwrite, ctx, NULL) != 0) {
LM_ERR("cannot register callback for context serialization\n");
goto restore;
}
/* register database callbacks */
acc_ref_ex(ctx, 2);
if (dlg_api.register_dlgcb(dlg, DLGCB_TERMINATED|DLGCB_EXPIRED,
acc_dlg_ended, ctx, unref_acc_ctx) != 0) {
acc_unref_ex(ctx, 2);
LM_ERR("cannot register callback for database accounting\n");
goto restore;
}
/* register dlg callbacks for ctx management */
if (dlg_api.register_dlgcb(dlg, DLGCB_REQ_WITHIN,
acc_merge_contexts, ctx, unref_acc_ctx) != 0) {
acc_unref(ctx); /* only one, the other one was successful */
LM_ERR("cannot register callback ctx management\n");
goto restore;
}
} else {
/* do old accounting */
if ( is_evi_acc_on(*flags) ) {
env_set_event(acc_event);
acc_evi_request( req, reply, 0 );
}
if ( is_log_acc_on(*flags) ) {
env_set_text( ACC_ANSWERED, ACC_ANSWERED_LEN);
acc_log_request( req, reply, 0 );
}
if (is_aaa_acc_on(*flags))
acc_aaa_request( req, reply, 0 );
if (is_db_acc_on(*flags)) {
env_set_text( table.s, table.len);
acc_db_request( req, reply, &acc_ins_list, 0);
}
}
restore:
if (new_uri_bk.len>=0) {
req->new_uri = new_uri_bk;
req->dst_uri = dst_uri_bk;
req->parsed_uri_ok = 0;
}
}
static void acc_dlg_ended(struct dlg_cell *dlg, int type,
struct dlg_cb_params *_params)
{
struct cell* t;
acc_ctx_t* ctx;
if (!_params) {
LM_ERR("not enough info\n");
return;
}
/* resolve local/dlg ctx conflict by merging them together */
acc_merge_contexts(dlg, type, _params);
ctx = (acc_ctx_t *)(*_params->param);
/*
* this way we "enable" the refcount
* if opensips shuts down before dialog terminated then the refcount
* won't be enabled
*/
if (was_dlg_cb_used(ctx->flags)) {
LM_INFO("CDR callback already registered [%p|%u] - do not run it again!\n",
*_params->param, ctx->ref_no);
return;
}
set_dlg_cb_used(ctx->flags);
/* this time will be used to set */
gettimeofday(&ctx->bye_time, NULL);
/* if it's not a local transaction we do the accounting on the tm callbacks */
if (((t=tmb.t_gett()) == T_UNDEFINED) || !t ||
!tmb.t_is_local(_params->msg)) {
/* normal dialogs will have to do accounting when the response for
* the bye will come since users should be able to populate extra
* vars and leg vars */
acc_ref(ctx);
if (tmb.register_tmcb( _params->msg, NULL,
TMCB_RESPONSE_OUT, acc_cdr_cb, ctx, unref_acc_ctx) < 0) {
acc_unref(ctx);
LM_ERR("failed to register cdr callback!\n");
return;
}
/* for local transactions we do the accounting here since all the messages
* have been processed */
} else {
/* expired dialogs will be handled here */
if (is_log_acc_on(ctx->flags)) {
env_set_text( ACC_ENDED, ACC_ENDED_LEN);
if (acc_log_cdrs(dlg, _params->msg, ctx) < 0) {
LM_ERR("Cannot log values\n");
return;
}
}
if (is_db_acc_on(ctx->flags)) {
env_set_text( db_table_acc.s, db_table_acc.len);
if (acc_db_cdrs(dlg, _params->msg, ctx) < 0) {
LM_ERR("Cannot insert into database\n");
return;
}
}
if (is_aaa_acc_on(ctx->flags) && acc_aaa_cdrs(dlg, _params->msg, ctx) < 0) {
LM_ERR("Cannot create radius accounting\n");
return;
}
if (is_evi_acc_on(ctx->flags)) {
env_set_event(acc_cdr_event);
if (acc_evi_cdrs(dlg, _params->msg, ctx) < 0) {
LM_ERR("cannot send accounting events\n");
return;
}
}
}
}
static void acc_dlg_onwrite(struct dlg_cell *dlg, int type,
struct dlg_cb_params *_params)
{
str flags_s;
acc_ctx_t* ctx;
str created_s;
if (!_params) {
LM_ERR("not enough info!\n");
return;
}
ctx = *_params->param;
if (ctx->extra_values &&
store_extra_values(ctx->extra_values, &extra_str, dlg) < 0) {
LM_ERR("cannot store extra values!\n");
return;
}
if (ctx->leg_values &&
store_leg_values(ctx, &leg_str, dlg) < 0) {
LM_ERR("cannot store leg values!\n");
return;
}
flags_s.s = (char*)(&ctx->flags);
flags_s.len = sizeof(unsigned long long);
/* store flags into dlg */
if ( dlg_api.store_dlg_value(dlg, &flags_str, &flags_s) < 0) {
LM_ERR("cannot store flag value into dialog\n");
return;
}
created_s.s = (char*)(&ctx->created);
created_s.len = sizeof(time_t);
if ( dlg_api.store_dlg_value(dlg,&created_str,&created_s) < 0) {
LM_ERR("cannot store created value!\n");
return;
}
if (is_db_acc_on(ctx->flags) && ctx->acc_table.s && ctx->acc_table.len) {
if ( dlg_api.store_dlg_value(dlg, &table_str, &ctx->acc_table) < 0) {
LM_ERR("cannot store table name into dialog\n");
return;
}
}
}
static void acc_cdr_cb( struct cell* t, int type, struct tmcb_params *ps )
{
acc_ctx_t* ctx = *ps->param;
struct dlg_cell *dlg;
dlg = dlg_api.get_dlg();
if (dlg == NULL) {
LM_DBG("dlg is null!\n");
return;
}
if (is_log_acc_on(ctx->flags)) {
env_set_text( ACC_ENDED, ACC_ENDED_LEN);
if (acc_log_cdrs(dlg, ps->req, ctx) < 0) {
LM_ERR("Cannot log values\n");
return;
}
}
if (is_db_acc_on(ctx->flags)) {
env_set_text( db_table_acc.s, db_table_acc.len);
if (acc_db_cdrs(dlg, ps->req, ctx) < 0) {
LM_ERR("Cannot insert into database\n");
return;
}
}
if (is_aaa_acc_on(ctx->flags) && acc_aaa_cdrs(dlg, ps->req, ctx) < 0) {
LM_ERR("Cannot create radius accounting\n");
return;
}
if (is_evi_acc_on(ctx->flags)) {
env_set_event(acc_cdr_event);
if (acc_evi_cdrs(dlg, ps->req, ctx) < 0) {
LM_ERR("cannot send accounting events\n");
return;
}
}