-
Notifications
You must be signed in to change notification settings - Fork 909
/
cnxcc_mod.c
2079 lines (1684 loc) · 52.3 KB
/
cnxcc_mod.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) 2012 Carlos Ruiz Díaz (caruizdiaz.com),
* ConexionGroup (www.conexiongroup.com)
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include "../../core/sr_module.h"
#include "../../core/mod_fix.h"
#include "../../core/dprint.h"
#include "../../core/error.h"
#include "../../core/mem/mem.h"
#include "../../core/mem/shm_mem.h"
#include "../../core/lock_ops.h"
#include "../../core/timer_proc.h"
#include "../../core/parser/parse_from.h"
#include "../../core/parser/parse_to.h"
#include "../../core/parser/parse_uri.h"
#include "../../core/parser/parse_cseq.h"
#include "../../core/parser/contact/parse_contact.h"
#include "../../core/parser/contact/contact.h"
#include "../tm/tm_load.h"
#include "../dialog/dlg_load.h"
#include "../dialog/dlg_hash.h"
#include "../../core/rpc.h"
#include "../../core/rpc_lookup.h"
#include "../../core/kemi.h"
#include "cnxcc_mod.h"
#include "cnxcc.h"
#include "cnxcc_sip_msg_faker.h"
#include "cnxcc_check.h"
#include "cnxcc_rpc.h"
#include "cnxcc_select.h"
#include "cnxcc_redis.h"
MODULE_VERSION
#define HT_SIZE 229
#define MODULE_NAME "cnxcc"
#define NUMBER_OF_TIMERS 2
#define TRUE 1
#define FALSE !TRUE
data_t _data;
struct dlg_binds _dlgbinds;
static int cnxcc_set_max_credit_fixup(void **param, int param_no);
/*
* module core functions
*/
static int __mod_init(void);
static int __child_init(int);
static int __init_hashtable(struct str_hash_table *ht);
/*
* Memory management functions
*/
static int __shm_str_hash_alloc(struct str_hash_table *ht, int size);
static void __free_credit_data_hash_entry(struct str_hash_entry *e);
static void __free_credit_data(credit_data_t *credit_data, hash_tables_t *hts,
struct str_hash_entry *cd_entry);
/*
* PV management functions
*/
static int __pv_parse_calls_param(pv_spec_p sp, str *in);
static int __pv_get_calls(
struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
/*
* Billing management functions
*/
static int __set_max_credit(sip_msg_t *msg, char *pclient, char *pcredit,
char *pconnect, char *pcps, char *pinitp, char *pfinishp);
static int __set_max_time(sip_msg_t *msg, char *pclient, char *pmaxsecs);
static int __update_max_time(sip_msg_t *msg, char *pclient, char *psecs);
static int __set_max_channels(sip_msg_t *msg, char *pclient, char *pmaxchan);
static int __get_channel_count(sip_msg_t *msg, char *pclient, char *pcount);
static int __terminate_all(sip_msg_t *msg, char *pclient, char *p2);
static void __start_billing(
str *callid, str *from_uri, str *to_uri, str tags[2]);
static void __setup_billing(
str *callid, unsigned int h_entry, unsigned int h_id);
static void __stop_billing(str *callid);
static int __add_call_by_cid(str *cid, call_t *call, credit_type_t type);
static call_t *__alloc_new_call_by_time(
credit_data_t *credit_data, struct sip_msg *msg, int max_secs);
static call_t *__alloc_new_call_by_money(credit_data_t *credit_data,
struct sip_msg *msg, double credit, double connect_cost,
double cost_per_second, int initial_pulse, int final_pulse);
static void __notify_call_termination(sip_msg_t *msg);
static void __free_call(call_t *call);
static void __delete_call(call_t *call, credit_data_t *credit_data);
static int __has_to_tag(struct sip_msg *msg);
static credit_data_t *__alloc_new_credit_data(
str *client_id, credit_type_t type);
static credit_data_t *__get_or_create_credit_data_entry(
str *client_id, credit_type_t type);
/*
* control interface
*/
void rpc_credit_control_stats(rpc_t *rpc, void *ctx);
/*
* Dialog management callback functions
*/
static void __dialog_terminated_callback(
struct dlg_cell *cell, int type, struct dlg_cb_params *params);
static void __dialog_confirmed_callback(
struct dlg_cell *cell, int type, struct dlg_cb_params *params);
static void __dialog_created_callback(
struct dlg_cell *cell, int type, struct dlg_cb_params *params);
/* clang-format off */
static pv_export_t mod_pvs[] = {
{ {"cnxcc", sizeof("cnxcc")-1 }, PVT_OTHER, __pv_get_calls, 0,
__pv_parse_calls_param, 0, 0, 0 },
{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
};
static cmd_export_t cmds[] = {
{"cnxcc_set_max_credit", (cmd_function) __set_max_credit, 6,
cnxcc_set_max_credit_fixup, NULL, ANY_ROUTE},
{"cnxcc_set_max_time", (cmd_function) __set_max_time, 2,
fixup_spve_igp, fixup_free_spve_igp, ANY_ROUTE},
{"cnxcc_update_max_time", (cmd_function) __update_max_time, 2,
fixup_spve_igp, fixup_free_spve_igp, ANY_ROUTE},
{"cnxcc_set_max_channels", (cmd_function) __set_max_channels, 2,
fixup_spve_igp, fixup_free_spve_igp, ANY_ROUTE},
{"cnxcc_get_channel_count", (cmd_function) __get_channel_count, 2,
fixup_spve_pvar, fixup_free_spve_pvar, ANY_ROUTE},
{"cnxcc_terminate_all", (cmd_function) __terminate_all, 1,
fixup_spve_null, fixup_free_spve_null, ANY_ROUTE},
{0,0,0,0,0,0}
};
static param_export_t params[] = {
{"dlg_flag", INT_PARAM, &_data.ctrl_flag },
{"credit_check_period", INT_PARAM, &_data.check_period },
{"redis", PARAM_STR, &_data.redis_cnn_str },
{ 0, 0, 0 }
};
/* clang-format on */
static const char *rpc_active_clients_doc[2] = {
"List of clients with active calls", NULL};
static const char *rpc_check_client_stats_doc[2] = {
"Check specific client calls", NULL};
static const char *rpc_kill_call_doc[2] = {"Kill call using its call ID", NULL};
static const char *rpc_credit_control_stats_doc[2] = {
"List credit control stats", NULL};
/* clang-format off */
rpc_export_t cnxcc_rpc[] = {
{ "cnxcc.active_clients", rpc_active_clients, rpc_active_clients_doc, 0},
{ "cnxcc.check_client", rpc_check_client_stats, rpc_check_client_stats_doc, 0},
{ "cnxcc.kill_call", rpc_kill_call, rpc_kill_call_doc, 0},
{ "cnxcc.stats", rpc_credit_control_stats, rpc_credit_control_stats_doc, 0},
{ NULL, NULL, NULL, 0}
};
/* selects declaration */
select_row_t sel_declaration[] = {
{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cnxcc"), sel_root,
SEL_PARAM_EXPECTED},
{ sel_root, SEL_PARAM_STR, STR_STATIC_INIT("channels"), sel_channels,
SEL_PARAM_EXPECTED|CONSUME_NEXT_STR|FIXUP_CALL},
{ sel_channels, SEL_PARAM_STR, STR_STATIC_INIT("count"), sel_channels_count,
0},
{ NULL, SEL_PARAM_STR, STR_NULL, NULL, 0}
};
/** module exports */
struct module_exports exports = {
MODULE_NAME, /* module name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* cmd (cfg function) exports */
params, /* param exports */
0, /* RPC method exports */
mod_pvs, /* pseudo-variables exports */
0, /* response handling function */
__mod_init, /* module init function */
__child_init, /* per-child init function */
0 /* module destroy function */
};
/* clang-format on */
static int cnxcc_set_max_credit_fixup(void **param, int param_no)
{
switch(param_no) {
case 1:
case 2:
case 3:
return fixup_spve_all(param, param_no);
case 4:
return fixup_spve_all(param, param_no);
case 5:
case 6:
return fixup_igp_all(param, param_no);
default:
LM_ERR("unexpected parameter number: %d\n", param_no);
return E_CFG;
}
}
static int __mod_init(void)
{
int len;
char *chr;
LM_INFO("Loading " MODULE_NAME " module\n");
_data.cs_route_number = route_get(&event_rt, "cnxcc:call-shutdown");
if(_data.cs_route_number < 0)
LM_INFO("No cnxcc:call-shutdown event route found\n");
if(_data.cs_route_number > 0
&& event_rt.rlist[_data.cs_route_number] == NULL) {
LM_INFO("cnxcc:call-shutdown route is empty\n");
_data.cs_route_number = -1;
}
if(_data.check_period <= 0) {
LM_INFO("credit_check_period cannot be less than 1 second\n");
return -1;
}
_data.time.credit_data_by_client =
shm_malloc(sizeof(struct str_hash_table));
_data.time.call_data_by_cid = shm_malloc(sizeof(struct str_hash_table));
_data.money.credit_data_by_client =
shm_malloc(sizeof(struct str_hash_table));
_data.money.call_data_by_cid = shm_malloc(sizeof(struct str_hash_table));
_data.channel.credit_data_by_client =
shm_malloc(sizeof(struct str_hash_table));
_data.channel.call_data_by_cid = shm_malloc(sizeof(struct str_hash_table));
memset(_data.time.credit_data_by_client, 0, sizeof(struct str_hash_table));
memset(_data.time.call_data_by_cid, 0, sizeof(struct str_hash_table));
memset(_data.money.credit_data_by_client, 0, sizeof(struct str_hash_table));
memset(_data.money.call_data_by_cid, 0, sizeof(struct str_hash_table));
memset(_data.channel.credit_data_by_client, 0,
sizeof(struct str_hash_table));
memset(_data.channel.call_data_by_cid, 0, sizeof(struct str_hash_table));
_data.stats = (stats_t *)shm_malloc(sizeof(stats_t));
if(!_data.stats) {
SHM_MEM_ERROR;
return -1;
}
memset(_data.stats, 0, sizeof(stats_t));
if(__init_hashtable(_data.time.credit_data_by_client) != 0)
return -1;
if(__init_hashtable(_data.time.call_data_by_cid) != 0)
return -1;
if(__init_hashtable(_data.money.credit_data_by_client) != 0)
return -1;
if(__init_hashtable(_data.money.call_data_by_cid) != 0)
return -1;
if(__init_hashtable(_data.channel.credit_data_by_client) != 0)
return -1;
if(__init_hashtable(_data.channel.call_data_by_cid) != 0)
return -1;
cnxcc_lock_init(_data.lock);
cnxcc_lock_init(_data.time.lock);
cnxcc_lock_init(_data.money.lock);
cnxcc_lock_init(_data.channel.lock);
/*
* One for time based monitoring
* One for money based monitoring
*/
register_dummy_timers(NUMBER_OF_TIMERS);
if(rpc_register_array(cnxcc_rpc) != 0) {
LM_ERR("Failed registering RPC commands\n");
return -1;
}
if(load_dlg_api(&_dlgbinds) != 0) {
LM_ERR("Error loading dialog API\n");
return -1;
}
_dlgbinds.register_dlgcb(
NULL, DLGCB_CREATED, __dialog_created_callback, NULL, NULL);
register_select_table(sel_declaration);
// redis configuration setup
if(_data.redis_cnn_str.len <= 0)
return 0;
// replace ";" for " ", so we can use a simpler pattern in sscanf()
for(chr = _data.redis_cnn_str.s; *chr; chr++)
if(*chr == ';')
*chr = ' ';
memset(_data.redis_cnn_info.host, 0, sizeof(_data.redis_cnn_info.host));
sscanf(_data.redis_cnn_str.s, "addr=%s port=%d db=%d",
_data.redis_cnn_info.host, &_data.redis_cnn_info.port,
&_data.redis_cnn_info.db);
len = strlen(_data.redis_cnn_info.host);
//
// Redis modparam validations
//
if(len == 0) {
LM_ERR("invalid host address [%s]", _data.redis_cnn_info.host);
return -1;
}
if(_data.redis_cnn_info.port <= 0) {
LM_ERR("invalid port number [%d]", _data.redis_cnn_info.port);
return -1;
}
if(_data.redis_cnn_info.db < 0) {
LM_ERR("invalid db number [%d]", _data.redis_cnn_info.db);
return -1;
}
LM_INFO("Redis connection info: ip=[%s], port=[%d], database=[%d]",
_data.redis_cnn_info.host, _data.redis_cnn_info.port,
_data.redis_cnn_info.db);
register_procs(3 /* 2 timers + 1 redis async receiver */);
return 0;
}
static int __child_init(int rank)
{
int pid = 0;
if(rank != PROC_INIT && rank != PROC_MAIN && rank != PROC_TCP_MAIN) {
if(_data.redis_cnn_str.len <= 0)
return 0;
_data.redis = redis_connect(_data.redis_cnn_info.host,
_data.redis_cnn_info.port, _data.redis_cnn_info.db);
return (!_data.redis) ? -1 : 0;
}
if(rank != PROC_MAIN)
return 0;
if(fork_dummy_timer(PROC_TIMER, "CNXCC TB TIMER", 1, check_calls_by_money,
NULL, _data.check_period)
< 0) {
LM_ERR("Failed registering TB TIMER routine as process\n");
return -1;
}
if(fork_dummy_timer(PROC_TIMER, "CNXCC MB TIMER", 1, check_calls_by_time,
NULL, _data.check_period)
< 0) {
LM_ERR("Failed registering MB TIMER routine as process\n");
return -1;
}
if(_data.redis_cnn_str.len <= 0)
return 0;
pid = fork_process(PROC_NOCHLDINIT, "Redis Async receiver", 1);
if(pid < 0) {
LM_ERR("error forking Redis receiver\n");
return -1;
} else if(pid == 0) {
_data.redis = redis_connect_async(_data.redis_cnn_info.host,
_data.redis_cnn_info.port, _data.redis_cnn_info.db);
return (!_data.redis) ? -1 : 0;
;
}
return 0;
}
static int __init_hashtable(struct str_hash_table *ht)
{
if(ht == NULL)
return -1;
if(__shm_str_hash_alloc(ht, HT_SIZE) != 0)
return -1;
str_hash_init(ht);
return 0;
}
static void __dialog_created_callback(
struct dlg_cell *cell, int type, struct dlg_cb_params *_params)
{
struct sip_msg *msg = NULL;
msg = _params->direction == SIP_REPLY ? _params->rpl : _params->req;
if(msg == NULL) {
LM_ERR("Error getting direction of SIP msg\n");
return;
}
if(isflagset(msg, _data.ctrl_flag) == -1) {
LM_DBG("Flag is not set for this message. Ignoring\n");
return;
}
LM_DBG("Dialog created for CID [%.*s]\n", cell->callid.len, cell->callid.s);
_dlgbinds.register_dlgcb(
cell, DLGCB_CONFIRMED, __dialog_confirmed_callback, NULL, NULL);
_dlgbinds.register_dlgcb(cell,
DLGCB_TERMINATED | DLGCB_FAILED | DLGCB_EXPIRED,
__dialog_terminated_callback, NULL, NULL);
__setup_billing(&cell->callid, cell->h_entry, cell->h_id);
}
static void __dialog_confirmed_callback(
struct dlg_cell *cell, int type, struct dlg_cb_params *_params)
{
LM_DBG("Dialog confirmed for CID [%.*s]\n", cell->callid.len,
cell->callid.s);
__start_billing(&cell->callid, &cell->from_uri, &cell->to_uri, cell->tag);
}
static void __dialog_terminated_callback(
struct dlg_cell *cell, int type, struct dlg_cb_params *_params)
{
LM_DBG("Dialog terminated for CID [%.*s]\n", cell->callid.len,
cell->callid.s);
__stop_billing(&cell->callid);
}
static void __notify_call_termination(sip_msg_t *msg)
{
struct run_act_ctx ra_ctx;
init_run_actions_ctx(&ra_ctx);
//run_top_route(event_rt.rlist[_data.cs_route_number], msg, &ra_ctx);
if(run_actions(&ra_ctx, event_rt.rlist[_data.cs_route_number], msg) < 0)
LM_ERR("Error executing cnxcc:call-shutdown route\n");
}
int try_get_credit_data_entry(str *client_id, credit_data_t **credit_data)
{
struct str_hash_entry *cd_entry = NULL;
hash_tables_t *hts = NULL;
*credit_data = NULL;
/* by money */
hts = &_data.money;
cnxcc_lock(hts->lock);
cd_entry = str_hash_get(
hts->credit_data_by_client, client_id->s, client_id->len);
if(cd_entry != NULL) {
*credit_data = cd_entry->u.p;
cnxcc_unlock(hts->lock);
return 0;
}
cnxcc_unlock(hts->lock);
/* by time */
hts = &_data.time;
cnxcc_lock(hts->lock);
cd_entry = str_hash_get(
hts->credit_data_by_client, client_id->s, client_id->len);
if(cd_entry != NULL) {
*credit_data = cd_entry->u.p;
cnxcc_unlock(hts->lock);
return 0;
}
cnxcc_unlock(hts->lock);
/* by channel */
hts = &_data.channel;
cnxcc_lock(hts->lock);
cd_entry = str_hash_get(
hts->credit_data_by_client, client_id->s, client_id->len);
if(cd_entry != NULL) {
*credit_data = cd_entry->u.p;
cnxcc_unlock(hts->lock);
return 0;
}
cnxcc_unlock(hts->lock);
return -1;
}
int try_get_call_entry(str *callid, call_t **call, hash_tables_t **hts)
{
struct str_hash_entry *call_entry = NULL;
*call = NULL;
/* by money */
*hts = &_data.money;
cnxcc_lock((*hts)->lock);
call_entry = str_hash_get((*hts)->call_data_by_cid, callid->s, callid->len);
if(call_entry != NULL) {
*call = call_entry->u.p;
cnxcc_unlock((*hts)->lock);
return 0;
}
cnxcc_unlock((*hts)->lock);
/* by time */
*hts = &_data.time;
cnxcc_lock((*hts)->lock);
call_entry = str_hash_get((*hts)->call_data_by_cid, callid->s, callid->len);
if(call_entry != NULL) {
*call = call_entry->u.p;
cnxcc_unlock((*hts)->lock);
return 0;
}
cnxcc_unlock((*hts)->lock);
/* by channel */
*hts = &_data.channel;
cnxcc_lock((*hts)->lock);
call_entry = str_hash_get((*hts)->call_data_by_cid, callid->s, callid->len);
if(call_entry != NULL) {
*call = call_entry->u.p;
cnxcc_unlock((*hts)->lock);
return 0;
}
cnxcc_unlock((*hts)->lock);
return -1;
}
static void __stop_billing(str *callid)
{
struct str_hash_entry *cd_entry = NULL;
call_t *call = NULL;
hash_tables_t *hts = NULL;
credit_data_t *credit_data = NULL;
/*
* Search call data by call-id
*/
if(try_get_call_entry(callid, &call, &hts) != 0) {
LM_ERR("Call [%.*s] not found\n", callid->len, callid->s);
return;
}
if(call == NULL) {
LM_ERR("[%.*s] call pointer is null\n", callid->len, callid->s);
return;
}
if(hts == NULL) {
LM_ERR("[%.*s] result hashtable pointer is null\n", callid->len,
callid->s);
return;
}
cnxcc_lock(hts->lock);
// Search credit_data by client_id
cd_entry = str_hash_get(
hts->credit_data_by_client, call->client_id.s, call->client_id.len);
if(cd_entry == NULL) {
LM_ERR("Credit data not found for CID [%.*s], client-ID [%.*s]\n",
callid->len, callid->s, call->client_id.len, call->client_id.s);
cnxcc_unlock(hts->lock);
return;
}
credit_data = (credit_data_t *)cd_entry->u.p;
if(credit_data == NULL) {
LM_ERR("[%.*s]: credit_data pointer is null\n", callid->len, callid->s);
cnxcc_unlock(hts->lock);
return;
}
cnxcc_unlock(hts->lock);
LM_DBG("Call [%.*s] of client-ID [%.*s], ended\n", callid->len, callid->s,
call->client_id.len, call->client_id.s);
cnxcc_lock(credit_data->lock);
__delete_call(call, credit_data);
/*
* In case there are no active calls for a certain client, we remove the client-id from the hash table.
* This way, we can save memory for useful clients.
*/
if(credit_data->number_of_calls == 0) {
__free_credit_data(credit_data, hts, cd_entry);
return;
}
cnxcc_unlock(credit_data->lock);
}
static void __setup_billing(
str *callid, unsigned int h_entry, unsigned int h_id)
{
call_t *call = NULL;
hash_tables_t *hts = NULL;
LM_DBG("Creating dialog for [%.*s], h_id [%u], h_entry [%u]\n", callid->len,
callid->s, h_id, h_entry);
// cnxcc_lock(&_data);
/*
* Search call data by call-id
*/
if(try_get_call_entry(callid, &call, &hts) != 0) {
LM_ERR("Call [%.*s] not found\n", callid->len, callid->s);
return;
}
if(call == NULL) {
LM_ERR("[%.*s] call pointer is null\n", callid->len, callid->s);
return;
}
if(hts == NULL) {
LM_ERR("[%.*s] result hashtable pointer is null\n", callid->len,
callid->s);
return;
}
/*
* Update calls statistics
*/
cnxcc_lock(_data.lock);
_data.stats->active++;
_data.stats->total++;
cnxcc_unlock(_data.lock);
cnxcc_lock(call->lock);
call->dlg_h_entry = h_entry;
call->dlg_h_id = h_id;
LM_DBG("Call [%.*s] from client [%.*s], created\n", callid->len, callid->s,
call->client_id.len, call->client_id.s);
cnxcc_unlock(call->lock);
}
static void __start_billing(
str *callid, str *from_uri, str *to_uri, str tags[2])
{
struct str_hash_entry *cd_entry = NULL;
call_t *call = NULL;
hash_tables_t *hts = NULL;
credit_data_t *credit_data = NULL;
LM_DBG("Billing started for call [%.*s]\n", callid->len, callid->s);
// cnxcc_lock(&_data);
/*
* Search call data by call-id
*/
if(try_get_call_entry(callid, &call, &hts) != 0) {
LM_ERR("Call [%.*s] not found\n", callid->len, callid->s);
return;
}
if(call == NULL) {
LM_ERR("[%.*s] call pointer is null\n", callid->len, callid->s);
return;
}
if(hts == NULL) {
LM_ERR("[%.*s] result hashtable pointer is null", callid->len,
callid->s);
return;
}
cnxcc_lock(hts->lock);
/*
* Search credit_data by client_id
*/
cd_entry = str_hash_get(
hts->credit_data_by_client, call->client_id.s, call->client_id.len);
if(cd_entry == NULL) {
LM_ERR("Credit data not found for CID [%.*s], client-ID [%.*s]\n",
callid->len, callid->s, call->client_id.len, call->client_id.s);
cnxcc_unlock(hts->lock);
return;
}
credit_data = (credit_data_t *)cd_entry->u.p;
if(credit_data == NULL) {
LM_ERR("[%.*s]: credit_data pointer is null\n", callid->len, callid->s);
cnxcc_unlock(hts->lock);
return;
}
cnxcc_unlock(hts->lock);
cnxcc_lock(credit_data->lock);
/*
* Now that the call is confirmed, we can increase the count of "concurrent_calls".
* This will impact in the discount rate performed by the check_calls() function.
*
*/
credit_data->concurrent_calls++;
if(_data.redis)
redis_incr_by_int(credit_data, "concurrent_calls", 1);
if(credit_data->max_amount == 0) {
credit_data->max_amount = call->max_amount; // first time setup
if(_data.redis)
redis_insert_double_value(
credit_data, "max_amount", credit_data->max_amount);
}
if(call->max_amount > credit_data->max_amount) {
LM_ALERT("Maximum-talk-time/credit changed, maybe a credit reload? %f "
"> %f. Client [%.*s]\n",
call->max_amount, credit_data->max_amount, call->client_id.len,
call->client_id.s);
if(_data.redis)
redis_insert_double_value(credit_data, "max_amount",
call->max_amount - credit_data->max_amount);
credit_data->max_amount += call->max_amount - credit_data->max_amount;
}
/*
* Update max_amount, discounting what was already consumed by other calls of the same client
*/
call->max_amount = credit_data->max_amount - credit_data->consumed_amount;
cnxcc_unlock(credit_data->lock);
cnxcc_lock(call->lock);
/*
* Store from-tag value
*/
if(shm_str_dup(&call->sip_data.from_tag, &tags[0]) != 0) {
SHM_MEM_ERROR;
goto exit;
}
/*
* Store to-tag value
*/
if(shm_str_dup(&call->sip_data.to_tag, &tags[1]) != 0) {
SHM_MEM_ERROR;
goto exit;
}
if(shm_str_dup(&call->sip_data.from_uri, from_uri) != 0
|| shm_str_dup(&call->sip_data.to_uri, to_uri) != 0) {
SHM_MEM_ERROR;
goto exit;
}
call->start_timestamp = get_current_timestamp();
call->confirmed = TRUE;
LM_DBG("Call [%.*s] from client [%.*s], confirmed. from=<%.*s>;tag=%.*s, "
"to=<%.*s>;tag=%.*s\n",
callid->len, callid->s, call->client_id.len, call->client_id.s,
call->sip_data.from_uri.len, call->sip_data.from_uri.s,
call->sip_data.from_tag.len, call->sip_data.from_tag.s,
call->sip_data.to_uri.len, call->sip_data.to_uri.s,
call->sip_data.to_tag.len, call->sip_data.to_tag.s);
exit:
cnxcc_unlock(call->lock);
}
static void __delete_call(call_t *call, credit_data_t *credit_data)
{
// Update calls statistics
cnxcc_lock(_data.lock);
_data.stats->active--;
_data.stats->total--;
cnxcc_unlock(_data.lock);
// This call just ended and we need to remove it from the summ.
if(call->confirmed) {
credit_data->concurrent_calls--;
credit_data->ended_calls_consumed_amount += call->consumed_amount;
if(_data.redis) {
redis_incr_by_int(credit_data, "concurrent_calls", -1);
redis_incr_by_double(credit_data, "ended_calls_consumed_amount",
call->consumed_amount);
}
}
credit_data->number_of_calls--;
if(_data.redis)
redis_incr_by_int(credit_data, "number_of_calls", -1);
if(credit_data->concurrent_calls < 0) {
LM_BUG("number of concurrent calls dropped to negative value: %d\n",
credit_data->concurrent_calls);
}
if(credit_data->number_of_calls < 0) {
LM_BUG("number of calls dropped to negative value: %d\n",
credit_data->number_of_calls);
}
// Remove (and free) the call from the list of calls of the current credit_data
clist_rm(call, next, prev);
__free_call(call);
}
// must be called with lock held on credit_data
static void __free_credit_data(credit_data_t *credit_data, hash_tables_t *hts,
struct str_hash_entry *cd_entry)
{
if(credit_data->deallocating) {
LM_DBG("deallocating, skip\n");
return;
}
LM_DBG("Removing client [%.*s] and its calls from the list\n",
credit_data->call_list->client_id.len,
credit_data->call_list->client_id.s);
credit_data->deallocating = 1;
cnxcc_lock(hts->lock);
if(_data.redis) {
redis_clean_up_if_last(credit_data);
shm_free(credit_data->str_id);
}
// Remove the credit_data_t from the hash table
str_hash_del(cd_entry);
cnxcc_unlock(hts->lock);
// Free client_id in list's root
shm_free(credit_data->call_list->client_id.s);
shm_free(credit_data->call_list);
// Release the lock since we are going to free the entry down below
cnxcc_unlock(credit_data->lock);
// Free the whole entry
__free_credit_data_hash_entry(cd_entry);
}
// must be called with lock held on credit_data
/* terminate all calls and remove credit_data */
void terminate_all_calls(credit_data_t *credit_data)
{
call_t *call = NULL, *tmp = NULL;
struct str_hash_entry *cd_entry = NULL;
hash_tables_t *hts = NULL;
unsigned int pending = 0;
switch(credit_data->type) {
case CREDIT_MONEY:
hts = &_data.money;
break;
case CREDIT_TIME:
hts = &_data.time;
break;
case CREDIT_CHANNEL:
hts = &_data.channel;
break;
default:
LM_ERR("BUG: Something went terribly wrong\n");
return;
}
cd_entry = str_hash_get(hts->credit_data_by_client,
credit_data->call_list->client_id.s,
credit_data->call_list->client_id.len);
if(cd_entry == NULL) {
LM_WARN("credit data item not found\n");
return;
}
// tell __stop_billing() not to __free_credit_data
credit_data->deallocating = 1;
clist_foreach_safe(credit_data->call_list, call, tmp, next)
{
if(call->sip_data.callid.s != NULL) {
if(call->confirmed) {
LM_DBG("Killing call with CID [%.*s]\n",
call->sip_data.callid.len, call->sip_data.callid.s);
// Update number of calls forced to end
_data.stats->dropped++;
terminate_call(call);
// call memory will be cleaned by __stop_billing() when
// __dialog_terminated_callback() is triggered
} else {
LM_DBG("Non confirmed call with CID[%.*s], setting "
"max_amount:%f to 0\n",
call->sip_data.callid.len, call->sip_data.callid.s,
call->max_amount);
call->max_amount = 0;
pending = 1;
}
} else {
LM_WARN("invalid call structure %p\n", call);
}
}
credit_data->deallocating = 0;
if(!pending) {
__free_credit_data(credit_data, hts, cd_entry);
} else {
LM_DBG("credit data item left\n");
cnxcc_unlock(credit_data->lock);
}
}
/*
* WARNING: When calling this function, the proper lock should have been acquired
*/
static void __free_call(call_t *call)
{
struct str_hash_entry *e = NULL;
if(call->sip_data.callid.s == NULL)
return;
LM_DBG("Freeing call [%.*s]\n", call->sip_data.callid.len,
call->sip_data.callid.s);
e = str_hash_get(_data.money.call_data_by_cid, call->sip_data.callid.s,
call->sip_data.callid.len);
if(e == NULL) {
e = str_hash_get(_data.time.call_data_by_cid, call->sip_data.callid.s,