-
Notifications
You must be signed in to change notification settings - Fork 565
/
cgrates_acc.c
1468 lines (1307 loc) · 40.7 KB
/
cgrates_acc.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) 2017 Răzvan Crainea <razvan@opensips.org>
*
* 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
*
*/
#include "../../ut.h"
#include "../../mod_fix.h"
#include "cgrates_acc.h"
#define CGR_REF_DBG(_c, _s) LM_DBG("%s ref=%d ctx=%p\n", _s, _c->ref_no, _c)
#define CGR_SESS_ON(_s) ((_s) && (_s)->branch_mask)
#define CGR_SESS_ON_BRANCH(_s, _b) ((_s) && (_s)->branch_mask & (1 << (_b)))
struct dlg_binds cgr_dlgb;
struct tm_binds cgr_tmb;
static gen_lock_t *cgrates_contexts_lock;
static struct list_head *cgrates_contexts;
static inline void cgr_free_acc_ctx(struct cgr_acc_ctx *ctx);
static void cgr_tmcb_func( struct cell* t, int type, struct tmcb_params *ps);
static void cgr_tmcb_func_free(void *param);
static void cgr_dlg_callback(struct dlg_cell *dlg, int type,
struct dlg_cb_params *_params);
static str cgr_ctx_str = str_init("cgrX_ctx");
static str cgr_serial_str = str_init("cgrX_serial");
int cgr_acc_init(void)
{
cgrates_contexts_lock = lock_alloc();
if (!cgrates_contexts_lock || !lock_init(cgrates_contexts_lock)) {
LM_ERR("cannot create lock for cgrates lists\n");
return -1;
}
cgrates_contexts = shm_malloc(sizeof *cgrates_contexts);
if (!cgrates_contexts) {
LM_ERR("cannot create cgrates contexts list\n");
return -1;
}
INIT_LIST_HEAD(cgrates_contexts);
return 0;
}
static inline struct cgr_acc_ctx *cgr_new_acc_ctx(struct dlg_cell *dlg)
{
str ctxstr;
struct cgr_acc_ctx *ctx = shm_malloc(sizeof(*ctx));
if (!ctx) {
LM_ERR("cannot create acc context\n");
return NULL;
}
memset(ctx, 0, sizeof(*ctx));
LM_DBG("new acc ctx=%p\n", ctx);
ctxstr.len = sizeof(ctx);
ctxstr.s = (char *)&ctx;
lock_get(cgrates_contexts_lock);
list_add(&ctx->link, cgrates_contexts);
lock_release(cgrates_contexts_lock);
ctx->ref_no = 1;
CGR_REF_DBG(ctx, "init");
lock_init(&ctx->ref_lock);
if (cgr_dlgb.store_dlg_value(dlg, &cgr_ctx_str, &ctxstr))
LM_ERR("cannot store context in dialog!\n");
return ctx;
}
static inline struct cgr_acc_ctx *cgr_get_acc_ctx(void)
{
struct dlg_cell *dlg;
struct cgr_ctx *ctx = cgr_get_ctx();
if (!ctx) {
LM_ERR("cannot create global context\n");
return NULL;
}
if (!ctx->acc) {
dlg = cgr_dlgb.get_dlg();
if (!dlg) {
LM_ERR("cannot find a dialog!\n");
return NULL;
}
if ((ctx->acc = cgr_new_acc_ctx(dlg)) != NULL) {
/* point to the same sessions */
ctx->acc->sessions = ctx->sessions;
cgr_ref_acc_ctx(ctx->acc, 1, "general ctx");
}
} else {
LM_DBG("same acc ctx=%p\n", ctx->acc);
}
return ctx->acc;
}
struct cgr_acc_ctx *cgr_tryget_acc_ctx(void)
{
struct cgr_acc_ctx *acc_ctx;
str ctxstr;
struct cgr_kv *kv;
struct list_head *l, *sl;
struct list_head *t, *st;
struct cgr_session *s, *sa;
struct dlg_cell *dlg;
struct cgr_ctx *ctx = CGR_GET_CTX();
if (ctx && ctx->acc)
return ctx->acc;
if (!cgr_dlgb.get_dlg) /* dlg not even loaded */
return NULL;
dlg = cgr_dlgb.get_dlg();
if (!dlg) /* dialog not found yet, moving later */
return NULL;
/* search for the accounting ctx */
if (cgr_dlgb.fetch_dlg_value(dlg, &cgr_ctx_str, &ctxstr, 0) < 0)
return NULL;
if (ctxstr.len != sizeof(struct cgr_acc_ctx *)) {
LM_BUG("Invalid ctx pointer size %d\n", ctxstr.len);
return NULL;
}
acc_ctx = *(struct cgr_acc_ctx **)ctxstr.s;
if (!acc_ctx) /* nothing to do now */
return NULL;
/* if there is a context, move everything from static ctx to the shared
* one, but keep the newever values in the store */
if (ctx) {
list_for_each_safe(sl, st, acc_ctx->sessions) {
sa = list_entry(sl, struct cgr_session, list);
s = cgr_get_sess(ctx, (sa->tag.len ? &sa->tag : NULL));
/* if there is a matching session. move everything from
* the one in accounting to the one in local ctx */
if (s) {
list_for_each_safe(l, t, &sa->event_kvs) {
kv = list_entry(l, struct cgr_kv, list);
if (cgr_get_kv(&s->event_kvs, kv->key))
cgr_free_kv(kv);
else {
list_del(&kv->list);
list_add(&kv->list, &s->event_kvs);
}
}
if (s->acc_info) {
LM_WARN("found session info in a local context - discarding it!\n");
shm_free(s->acc_info);
}
s->acc_info = sa->acc_info;
sa->acc_info = 0;
cgr_free_sess(sa);
} else {
/* move the dict in the local ctx */
list_del(&sa->list);
list_add(&sa->list, ctx->sessions);
}
}
shm_free(acc_ctx->sessions);
acc_ctx->sessions = ctx->sessions;
}
return acc_ctx;
}
static inline void cgr_free_acc_ctx(struct cgr_acc_ctx *ctx)
{
struct list_head *l;
struct list_head *t;
struct dlg_cell *dlg;
str ctxstr;
LM_DBG("release acc ctx=%p\n", ctx);
/* remove all elements */
if (ctx->sessions) {
list_for_each_safe(l, t, ctx->sessions)
cgr_free_sess(list_entry(l, struct cgr_session, list));
shm_free(ctx->sessions);
ctx->sessions = 0;
}
lock_get(cgrates_contexts_lock);
list_del(&ctx->link);
lock_release(cgrates_contexts_lock);
shm_free(ctx);
ctx = 0;
ctxstr.len = sizeof(ctx);
ctxstr.s = (char *)&ctx;
dlg = cgr_dlgb.get_dlg();
if (dlg && cgr_dlgb.store_dlg_value(dlg, &cgr_ctx_str, &ctxstr) < 0)
LM_ERR("cannot reset context in dialog %p!\n", dlg);
}
void cgr_ref_acc_ctx(struct cgr_acc_ctx *ctx, int how, const char *who)
{
lock_get(&ctx->ref_lock);
ctx->ref_no += how;
CGR_REF_DBG(ctx, who);
if (ctx->ref_no == 0) {
lock_release(&ctx->ref_lock);
cgr_free_acc_ctx(ctx);
} else {
if (ctx->ref_no < 0)
LM_BUG("ref=%d ctx=%p gone negative!\n", ctx->ref_no, ctx);
lock_release(&ctx->ref_lock);
}
}
static int cgr_proc_start_acc_reply(struct cgr_conn *c, json_object *jobj,
void *p, char *error)
{
unsigned long long timeout;
int_str val;
struct dlg_cell *dlg = (struct dlg_cell *)p;
/* we cannot set in the context, because we don't have access to it */
if (error) /* XXX: should we set a terminate reason? */
return -1;
if (!cgre_compat_mode) {
json_object *jorig = jobj;
if (json_object_get_type(jobj) != json_type_object) {
LM_ERR("CGRateS did not return an object in InitiateSession reply: %d %s\n",
json_object_get_type(jobj), json_object_to_json_string(jobj));
return -4;
}
/* we are only interested in the MaxUsage token */
if (!json_object_object_get_ex(jorig, "MaxUsage", &jobj)) {
LM_DBG("CGRateS did not return an MaxUsage in InitiateSession reply: %d %s"
"- allow unlimited!\n", json_object_get_type(jorig),
json_object_to_json_string(jorig));
return 1;
}
if (json_object_get_type(jobj) != json_type_int) {
LM_ERR("CGRateS returned a non-int type for MaxUsage InitiateSession reply: %d %s\n",
json_object_get_type(jobj), json_object_to_json_string(jobj));
return -4;
}
timeout = json_object_get_int64(jobj);
if (timeout != 0 && timeout != -1)
val.n = timeout / 1000000000;
else
val.n = timeout;
} else if (!jobj) {
LM_DBG("CGRateS did not return an MaxUsage in InitiateSession reply "
"- allow unlimited!\n");
return 1;
} else {
if (json_object_get_type(jobj) != json_type_int) {
LM_ERR("CGRateS returned a non-int type for InitiateSession reply: %d %s\n",
json_object_get_type(jobj), json_object_to_json_string(jobj));
return -4;
}
val.n = json_object_get_int(jobj);
}
/* -1: always allowed (postpaid)
* 0: not allowed to call
* *: allowed
*/
if (val.n == 0)
return -1;
if (val.n == -1)
return 1;
/* update only if the timeout is smaller than the existing one */
if (dlg->lifetime > val.n) {
dlg->lifetime = val.n;
dlg->lifetime_dirty = 1;
LM_DBG("setting dialog timeout to %d\n", val.n);
} else {
LM_DBG("dialog timeout %d lower or equal than %d\n",
dlg->lifetime, val.n);
}
return 1;
}
static int cgr_proc_stop_acc_reply(struct cgr_conn *c, json_object *jobj,
void *p, char *error)
{
if (error) {
/* suppress SESSION_NOT_FOUND error for terminate, because most likely
* it comes for a postpaid account */
if (strcmp(error, "SESSION_NOT_FOUND") == 0)
return 1;
LM_ERR("got CDR error: %s\n", error);
return -1;
}
LM_DBG("got reply from cgrates: %s\n", json_object_to_json_string(jobj));
return 1;
}
static int cgr_proc_cdr_acc_reply(struct cgr_conn *c, json_object *jobj,
void *p, char *error)
{
int_str val;
if (error) {
val.s.s = error;
val.s.len = strlen(error);
if (cgrates_set_reply(CGR_KVF_TYPE_STR, &val) < 0) {
LM_ERR("cannot set the reply code!\n");
return -2;
}
return -1;
}
LM_DBG("got reply from cgrates: %s\n", json_object_to_json_string(jobj));
return 1;
}
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;
}
static inline str *cgr_get_sess_callid(struct sip_msg *msg,
struct cgr_session *s, str *msg_cid)
{
static str callid;
char *tmp;
int len;
/* if the default tag, simply return the initial callid */
if (!s->tag.len)
return msg_cid;
len = msg_cid->len + 1 /* separator */ + s->tag.len;
tmp = pkg_realloc(callid.s, len);
if (!tmp) {
LM_ERR("cannot realloc callid buffer with len=%d\n", len);
return NULL;
}
callid.s = tmp;
callid.len = len;
memcpy(callid.s, msg_cid->s, msg_cid->len);
callid.s[msg_cid->len] = '|';
memcpy(callid.s + msg_cid->len + 1, s->tag.s, s->tag.len);
return &callid;
}
static json_object *cgr_get_start_acc_msg(struct sip_msg *msg,
struct dlg_cell *dlg, struct cgr_acc_ctx *ctx, struct cgr_session *s)
{
struct cgr_msg *cmsg;
struct cgr_acc_sess *si = (struct cgr_acc_sess *)s->acc_info;
static str cmd_compat = str_init("SMGenericV1.InitiateSession");
static str cmd_ng = str_init("SessionSv1.InitiateSession");
struct cgr_kv *kv;
str stime;
str static_callid;
str originhost = str_init("");
str *callid = NULL;
str *cmd = (cgre_compat_mode ? &cmd_compat: &cmd_ng);
cmsg = cgr_get_generic_msg(cmd, s);
if (!cmsg) {
LM_ERR("cannot create generic cgrates message!\n");
return NULL;
}
/* ask to init the call */
if (!cgre_compat_mode &&
!cgr_get_const_kv(&s->req_kvs, "InitSession") &&
cgr_obj_push_bool(cmsg->opts, "InitSession", 1) < 0) {
LM_ERR("cannot push InitSession to request opts!\n");
goto error;
}
/* OriginID */
/* if origin was not added from script, add it now */
if (ctx && (kv = cgr_get_const_kv(&s->event_kvs, "OriginID"))) {
if (kv->flags & CGR_KVF_TYPE_INT) {
static_callid.s = int2str(kv->value.n, &static_callid.len);
callid = &static_callid;
} else
callid = &kv->value.s;
} else {
if (msg->callid==NULL && ((parse_headers(msg, HDR_CALLID_F, 0)==-1) ||
(msg->callid==NULL)) ) {
LM_ERR("Cannot get callid of the message!\n");
goto error;
} else {
callid = cgr_get_sess_callid(msg, s, &msg->callid->body);
if (!callid || cgr_obj_push_str(cmsg->params, "OriginID", callid) < 0) {
LM_ERR("cannot push OriginID!\n");
goto error;
}
}
}
if (ctx && (kv = cgr_get_const_kv(&s->event_kvs, "OriginHost")) &&
kv->flags & CGR_KVF_TYPE_STR)
originhost = kv->value.s;
/* figured out the OriginID - now store it in session */
si->originid.s = shm_malloc(callid->len + originhost.len);
if (!si->originid.s) {
LM_ERR("no more memory for callid!\n");
goto error;
}
memcpy(si->originid.s, callid->s, callid->len);
si->originid.len = callid->len;
/* originhost now */
si->originhost.s = si->originid.s + callid->len;
si->originhost.len = originhost.len;
memcpy(si->originhost.s, originhost.s, originhost.len);
if (ctx && !cgr_get_const_kv(&s->event_kvs, "DialogID") &&
cgr_obj_push_int(cmsg->params, "DialogID", dlg->h_id) < 0) {
LM_ERR("cannot push DialogID!\n");
goto error;
}
if (ctx && !cgr_get_const_kv(&s->event_kvs, "DialogEntry") &&
cgr_obj_push_int(cmsg->params, "DialogEntry", dlg->h_entry) < 0) {
LM_ERR("cannot push DialogEntry!\n");
goto error;
}
/* Account */
if (cgr_obj_push_str(cmsg->params, "Account", &si->acc) < 0) {
LM_ERR("cannot push Account info!\n");
goto error;
}
/* SetupTime */
stime.s = int2str(si->start_time, &stime.len);
if (cgr_obj_push_str(cmsg->params, "SetupTime", &stime) < 0) {
LM_ERR("cannot push SetupTime info!\n");
goto error;
}
/* AnswerTime */
if (ctx) {
stime.s = int2str(ctx->answer_time, &stime.len);
if (cgr_obj_push_str(cmsg->params, "AnswerTime", &stime) < 0) {
LM_ERR("cannot push AnswerTime info!\n");
goto error;
}
}
/* Destination */
if (cgr_obj_push_str(cmsg->params, "Destination", &si->dst) < 0) {
LM_ERR("cannot push Destination info!\n");
goto error;
}
return cmsg->msg;
error:
json_object_put(cmsg->msg);
return NULL;
}
static json_object *cgr_get_stop_acc_msg(struct sip_msg *msg,
struct cgr_acc_ctx *ctx, struct cgr_session *s)
{
struct cgr_acc_sess *si = (struct cgr_acc_sess *)s->acc_info;
struct dlg_cell *dlg;
struct cgr_msg *cmsg = NULL;
char int2str_buf[INT2STR_MAX_LEN + 1];
time_t now = time(NULL);
static str cmd_compat = str_init("SMGenericV1.TerminateSession");
static str cmd_ng = str_init("SessionSv1.TerminateSession");
str *cmd = (cgre_compat_mode ? &cmd_compat: &cmd_ng);
str *callid;
str tmp;
if (!ctx) {
LM_BUG("ended up in stop accounting with no context!\n");
return NULL;
}
ctx->duration = now - ctx->answer_time;
/* OriginID */
if ((dlg = cgr_dlgb.get_dlg()) == NULL) {
LM_ERR("cannot retrieve dialog!\n");
return NULL;
}
cmsg = cgr_get_generic_msg(cmd, s);
if (!cmsg) {
LM_ERR("cannot create generic cgrates message!\n");
return NULL;
}
/* ask to terminate the call */
if (!cgre_compat_mode &&
!cgr_get_const_kv(&s->req_kvs, "TerminateSession") &&
cgr_obj_push_bool(cmsg->opts, "TerminateSession", 1) < 0) {
LM_ERR("cannot push TerminateSession to request opts!\n");
goto error;
}
/* OriginID */
/* if origin was not added from script, add it now */
if (ctx && !cgr_get_const_kv(&s->event_kvs, "OriginID")) {
callid = cgr_get_sess_callid(msg, s, &dlg->callid);
if (cgr_obj_push_str(cmsg->params, "OriginID", callid) < 0) {
LM_ERR("cannot push OriginID!\n");
goto error;
}
}
/* Account */
if (cgr_obj_push_str(cmsg->params, "Account", &si->acc) < 0) {
LM_ERR("cannot push Account info!\n");
goto error;
}
/* SetupTime */
if (ctx->answer_time != si->start_time) {
tmp.s = int2str(si->start_time, &tmp.len);
if (cgr_obj_push_str(cmsg->params, "SetupTime", &tmp) < 0) {
LM_ERR("cannot push SetupTime info!\n");
goto error;
}
}
/* AnswerTime */
tmp.s = int2str(ctx->answer_time, &tmp.len);
if (cgr_obj_push_str(cmsg->params, "AnswerTime", &tmp) < 0) {
LM_ERR("cannot push AnswerTime info!\n");
goto error;
}
tmp.s = int2bstr(ctx->duration, int2str_buf, &tmp.len);
/* add an s at the end */
tmp.s[tmp.len] = 's';
tmp.len++;
tmp.s[tmp.len] = 0;
if (cgr_obj_push_str(cmsg->params, "Usage", &tmp) < 0) {
LM_ERR("cannot add Usage node\n");
goto error;
}
return cmsg->msg;
error:
json_object_put(cmsg->msg);
return NULL;
}
static json_object *cgr_get_cdr_acc_msg(struct sip_msg *msg,
struct cgr_acc_ctx *ctx, struct cgr_session *s, str *callid)
{
str tmp;
struct cgr_msg *cmsg = NULL;
char int2str_buf[INT2STR_MAX_LEN + 1];
static str cmd_compat = str_init("SMGenericV1.ProcessCDR");
static str cmd_ng = str_init("SessionSv1.ProcessCDR");
struct cgr_acc_sess *si = (struct cgr_acc_sess *)s->acc_info;
struct dlg_cell *dlg = cgr_dlgb.get_dlg();
str *cmd = (cgre_compat_mode ? &cmd_compat: &cmd_ng);
cmsg = cgr_get_generic_msg(cmd, s);
if (!cmsg) {
LM_ERR("cannot create generic cgrates message!\n");
return NULL;
}
if (cgr_obj_push_str(cmsg->params, "OriginID", callid) < 0) {
LM_ERR("cannot add OriginID node\n");
goto error;
}
if (cgr_obj_push_str(cmsg->params, "Account", &si->acc) < 0) {
LM_ERR("cannot add Account node\n");
goto error;
}
if (cgr_obj_push_str(cmsg->params, "Destination", &si->dst) < 0) {
LM_ERR("cannot add Destination node\n");
goto error;
}
tmp.s = int2bstr(ctx->duration, int2str_buf, &tmp.len);
/* add an s at the end */
tmp.s[tmp.len] = 's';
tmp.len++;
tmp.s[tmp.len] = 0;
if (cgr_obj_push_str(cmsg->params, "Usage", &tmp) < 0) {
LM_ERR("cannot add Usage node\n");
goto error;
}
if (ctx->answer_time) {
tmp.s = int2str(ctx->answer_time, &tmp.len);
if (cgr_obj_push_str(cmsg->params, "AnswerTime", &tmp) < 0) {
LM_ERR("cannot add AnswerTime node\n");
goto error;
}
}
if (si->start_time && si->start_time != ctx->answer_time) {
tmp.s = int2str(si->start_time, &tmp.len);
if (cgr_obj_push_str(cmsg->params, "SetupTime", &tmp) < 0) {
LM_ERR("cannot add SetupTime node\n");
goto error;
}
}
if (dlg && cgr_obj_push_str(cmsg->params, "DisconnectCause", &dlg->terminate_reason) < 0) {
LM_ERR("cannot add DisconnectCause node\n");
goto error;
}
return cmsg->msg;
error:
json_object_put(cmsg->msg);
return NULL;
}
static void cgr_cdr(struct sip_msg *msg, struct cgr_acc_ctx *ctx,
struct cgr_session *s, str *callid)
{
json_object *jmsg;
jmsg = cgr_get_cdr_acc_msg(msg, ctx, s, callid);
if (!jmsg) {
LM_ERR("cannot build the json to send to cgrates\n");
return;
}
cgr_handle_cmd(msg, jmsg, cgr_proc_cdr_acc_reply, ctx);
}
static void cgr_dlg_onwrite(struct dlg_cell *dlg, int type,
struct dlg_cb_params *_params)
{
struct _cgr_sess_no {
unsigned keys;
unsigned options;
} *sessions_kvs = NULL, *tmp;
struct cgr_acc_ctx *ctx;
struct cgr_session *s;
struct list_head *ls;
struct list_head *l;
struct cgr_kv *kv;
int sessions_no = 0, i;
str buf;
char *p;
/* no need to dump variables for deleted, since these have already been processed */
if (dlg->state == DLG_STATE_DELETED)
return;
ctx = *_params->param;
LM_DBG("storing in dialog acc ctx=%p\n", ctx);
buf.len = sizeof(ctx->start_time) + sizeof(ctx->answer_time) + sizeof(sessions_no);
if (ctx->sessions) {
list_for_each(l, ctx->sessions) {
sessions_no++;
s = list_entry(l, struct cgr_session, list);
tmp = pkg_realloc(sessions_kvs, (sessions_no) * sizeof(struct _cgr_sess_no));
if (!tmp) {
if (sessions_kvs)
pkg_free(sessions_kvs);
return;
}
sessions_kvs = tmp;
sessions_kvs[sessions_no-1].keys = 0;
sessions_kvs[sessions_no-1].options = 0;
/* if it was not yet engaged, skip it */
if (!s->acc_info) {
buf.len += sizeof(time_t); /* store 0 for start_time */
continue;
}
/* we have it engaged - store the tag and the info */
buf.len += sizeof(time_t) /* start_time */ +
sizeof(unsigned) + s->tag.len /* tag */ +
sizeof(unsigned) + s->acc_info->acc.len /* acc */ +
sizeof(unsigned) + s->acc_info->dst.len /* dst */ +
sizeof(branch_bm_t) /* branch_mask */ +
sizeof(unsigned) /* flags */ +
sizeof(unsigned) /* number of keys */ +
sizeof(unsigned) /* number of opts */;
/* count the keys now */
list_for_each(ls, &s->event_kvs) {
sessions_kvs[sessions_no-1].keys++;
kv = list_entry(ls, struct cgr_kv, list);
buf.len += sizeof(unsigned) + kv->key.len + sizeof(unsigned char);
if (kv->flags & CGR_KVF_TYPE_INT)
buf.len += sizeof(int);
else if (kv->flags & CGR_KVF_TYPE_STR)
buf.len += sizeof(unsigned) + kv->value.s.len;
}
/* count the request opts now */
list_for_each(ls, &s->req_kvs) {
sessions_kvs[sessions_no-1].options++;
kv = list_entry(ls, struct cgr_kv, list);
buf.len += sizeof(unsigned) + kv->key.len + sizeof(unsigned char);
if (kv->flags & CGR_KVF_TYPE_INT)
buf.len += sizeof(int);
else if (kv->flags & CGR_KVF_TYPE_STR)
buf.len += sizeof(unsigned) + kv->value.s.len;
}
}
}
buf.s = pkg_malloc(buf.len);
if (!buf.s) {
LM_ERR("cannot allocate buffer for context serialization!\n");
return;
}
p = buf.s;
/* start_time */
memcpy(p, &ctx->start_time, sizeof(ctx->start_time));
p += sizeof(ctx->start_time);
/* answer_time */
memcpy(p, &ctx->answer_time, sizeof(ctx->answer_time));
p += sizeof(ctx->answer_time);
/* sessions_no */
memcpy(p, &sessions_no, sizeof(sessions_no));
p += sizeof(sessions_no);
/* kv */
if (ctx->sessions) {
i = -1;
list_for_each(l, ctx->sessions) {
i++;
s = list_entry(l, struct cgr_session, list);
if (!s->acc_info) {
memset(p, 0, sizeof(time_t));
p += sizeof(time_t);
continue;
}
/* start_time */
memcpy(p, &s->acc_info->start_time, sizeof(time_t));
p += sizeof(time_t);
/* tag */
memcpy(p, &s->tag.len, sizeof(unsigned));
p += sizeof(unsigned);
memcpy(p, s->tag.s, s->tag.len);
p += s->tag.len;
/* acc */
memcpy(p, &s->acc_info->acc.len, sizeof(unsigned));
p += sizeof(unsigned);
memcpy(p, s->acc_info->acc.s, s->acc_info->acc.len);
p += s->acc_info->acc.len;
/* dst */
memcpy(p, &s->acc_info->dst.len, sizeof(unsigned));
p += sizeof(unsigned);
memcpy(p, s->acc_info->dst.s, s->acc_info->dst.len);
p += s->acc_info->dst.len;
/* branch_mask */
memcpy(p, &s->acc_info->branch_mask, sizeof(branch_bm_t));
p += sizeof(branch_bm_t);
/* flags */
memcpy(p, &s->acc_info->flags, sizeof(unsigned));
p += sizeof(unsigned);
/* number of event keys */
memcpy(p, &sessions_kvs[i].keys, sizeof(unsigned));
p += sizeof(unsigned);
list_for_each(ls, &s->event_kvs) {
/* if does not have acc_info, it does not have start_time */
kv = list_entry(ls, struct cgr_kv, list);
/* kv->key */
memcpy(p, &kv->key.len, sizeof(unsigned));
p += sizeof(unsigned);
memcpy(p, kv->key.s, kv->key.len);
p += kv->key.len;
LM_DBG("storing key %d [%.*s]\n", kv->key.len,kv->key.len,kv->key.s);
/* kv->flags */
memcpy(p, &kv->flags, sizeof(unsigned char));
p += sizeof(unsigned char);
/* kv->value */
if (kv->flags & CGR_KVF_TYPE_INT) {
memcpy(p, &kv->value.n, sizeof(int));
p += sizeof(int);
} else if (kv->flags & CGR_KVF_TYPE_STR) {
memcpy(p, &kv->value.s.len, sizeof(unsigned));
p += sizeof(unsigned);
memcpy(p, kv->value.s.s, kv->value.s.len);
p += kv->value.s.len;
}
}
/* number of options keys */
memcpy(p, &sessions_kvs[i].options, sizeof(unsigned));
p += sizeof(unsigned);
list_for_each(ls, &s->req_kvs) {
/* if does not have acc_info, it does not have start_time */
kv = list_entry(ls, struct cgr_kv, list);
/* kv->key */
memcpy(p, &kv->key.len, sizeof(unsigned));
p += sizeof(unsigned);
memcpy(p, kv->key.s, kv->key.len);
p += kv->key.len;
LM_DBG("storing opt key %d [%.*s]\n", kv->key.len,kv->key.len,kv->key.s);
/* kv->flags */
memcpy(p, &kv->flags, sizeof(unsigned char));
p += sizeof(unsigned char);
/* kv->value */
if (kv->flags & CGR_KVF_TYPE_INT) {
memcpy(p, &kv->value.n, sizeof(int));
p += sizeof(int);
} else if (kv->flags & CGR_KVF_TYPE_STR) {
memcpy(p, &kv->value.s.len, sizeof(unsigned));
p += sizeof(unsigned);
memcpy(p, kv->value.s.s, kv->value.s.len);
p += kv->value.s.len;
}
}
}
}
/* sanity check :D */
if (buf.len != p - buf.s)
LM_BUG("length mismatch between computed and result: %d != %d\n",
buf.len, (int)(p - buf.s));
if (cgr_dlgb.store_dlg_value(dlg, &cgr_serial_str, &buf) < 0)
LM_ERR("cannot store the serialized context value!\n");
pkg_free(buf.s);
if (sessions_no)
pkg_free(sessions_kvs);
}
int w_cgr_acc(struct sip_msg* msg, void *flag_c, str* acc_c, str *dst_c,
str *tag_c)
{
str *acc;
str *dst;
struct cgr_acc_sess *si;
struct cgr_acc_ctx *ctx;
struct cgr_session *s;
struct dlg_cell *dlg;
branch_bm_t branch_mask = 0;
if (msg->REQ_METHOD != METHOD_INVITE) {
LM_DBG("accounting not called on INVITE\n");
return -3;
}
/* find out where we are to see if it makes sense to engage anything */
if (route_type == REQUEST_ROUTE || route_type == FAILURE_ROUTE) {
branch_mask = (unsigned)-1;
LM_DBG("engaging accounting for all branches!\n");
} else if (route_type == BRANCH_ROUTE || route_type == ONREPLY_ROUTE) {
branch_mask = 1 << cgr_tmb.get_branch_index();
LM_DBG("engaging accounting for branch %d!\n", cgr_tmb.get_branch_index());
} else {
LM_ERR("cannot engage accounting in route type %d\n", route_type);
return -3;
}
if (!cgr_dlgb.get_dlg) {
LM_ERR("cannot do cgrates accounting without dialog support!\n");
return -2;
}
if ((acc = cgr_get_acc(msg, acc_c)) == NULL)
return -2;
if ((dst = cgr_get_dst(msg, dst_c)) == NULL)
return -2;
/* get the dialog */
if (!cgr_dlgb.get_dlg() && cgr_dlgb.create_dlg(msg, 0) < 0) {
LM_ERR("Cannot create dialog!\n");
return -1;
}
dlg = cgr_dlgb.get_dlg();
ctx = cgr_get_acc_ctx();
if (!ctx) {
LM_ERR("cannot create acc context\n");
return -1;
}
s = cgr_get_sess_new(cgr_get_ctx(), tag_c);
if (!s) {
LM_ERR("cannot create a new session!\n");
return -1;
}
if (!s->acc_info) {
si = shm_malloc(sizeof(struct cgr_acc_sess) + acc->len + dst->len);
if (!si) {
LM_ERR("cannot create new session information!\n");
return -1;
}
memset(si, 0, sizeof(struct cgr_acc_sess)); /* no need to clean the rest */
si->acc.s = (char *)si + sizeof(struct cgr_acc_sess);
si->dst.s = si->acc.s + acc->len;
si->flags = (unsigned long)flag_c;
time(&si->start_time);
si->acc.len = acc->len;
memcpy(si->acc.s, acc->s, acc->len);
si->dst.len = dst->len;
memcpy(si->dst.s, dst->s, dst->len);
s->acc_info = si;
} else {
LM_DBG("session already engaged! nothing updated...\n");
si = s->acc_info;
}
if (si->branch_mask & branch_mask) {
LM_DBG("session already engaged on this branch\n");
/* nothing more to do - no ref, no nothing */
return 1;
}
si->branch_mask |= branch_mask;
LM_DBG("session info tag=%.*s acc=%.*s dst=%.*s mask=%X\n",
s->tag.len, s->tag.s, si->acc.len, si->acc.s,
si->dst.len, si->dst.s, si->branch_mask);
if (!ctx->engaged) {
time(&ctx->start_time);
if (cgr_tmb.register_tmcb( msg, 0, TMCB_RESPONSE_OUT,
cgr_tmcb_func, ctx, cgr_tmcb_func_free)<=0) {
LM_ERR("cannot register tm callbacks\n");
cgr_ref_acc_ctx(ctx, -1, "acc");
return -1;
}
cgr_ref_acc_ctx(ctx, 1, "tm");
if (cgr_dlgb.register_dlgcb(dlg, DLGCB_TERMINATED|DLGCB_EXPIRED,
cgr_dlg_callback, ctx, 0)){
LM_ERR("cannot register callback for database accounting\n");
return -1;
}
if (cgr_dlgb.register_dlgcb(dlg, DLGCB_WRITE_VP,
cgr_dlg_onwrite, ctx, NULL) != 0) {
LM_ERR("cannot register callback for context serialization!\n");
return -1;
}
ctx->engaged = 1;
}
return 1;
}
static void cgr_tmcb_func_free(void *param)
{
cgr_ref_acc_ctx((struct cgr_acc_ctx *)param, -1, "tm");
}
static void cgr_tmcb_func(struct cell* t, int type, struct tmcb_params *ps)
{
struct cgr_acc_ctx *ctx;
struct cgr_acc_sess *si;
struct cgr_session *s;
struct dlg_cell *dlg;
struct list_head *l;
json_object *jmsg;
str terminate_str;
int branch;
str callid;
/* send commands only for engaged branches */
branch = cgr_tmb.get_branch_index();
LM_DBG("Called callback for transaction %p type %d reply_code=%d branch=%d\n",