forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdr.c
4630 lines (4036 loc) · 143 KB
/
cdr.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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2006, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Call Detail Record API
*
* \author Mark Spencer <markster@digium.com>
*
* \note Includes code and algorithms from the Zapata library.
*
* \note We do a lot of checking here in the CDR code to try to be sure we don't ever let a CDR slip
* through our fingers somehow. If someone allocates a CDR, it must be completely handled normally
* or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
* isn't properly generated and posted.
*/
/*! \li \ref cdr.c uses the configuration file \ref cdr.conf
* \addtogroup configuration_file Configuration Files
*/
/*!
* \page cdr.conf cdr.conf
* \verbinclude cdr.conf.sample
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <signal.h>
#include <inttypes.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/callerid.h"
#include "asterisk/manager.h"
#include "asterisk/module.h"
#include "asterisk/causes.h"
#include "asterisk/linkedlists.h"
#include "asterisk/utils.h"
#include "asterisk/sched.h"
#include "asterisk/config.h"
#include "asterisk/cli.h"
#include "asterisk/stringfields.h"
#include "asterisk/config_options.h"
#include "asterisk/json.h"
#include "asterisk/parking.h"
#include "asterisk/stasis.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/stasis_bridges.h"
#include "asterisk/stasis_message_router.h"
#include "asterisk/astobj2.h"
#include "asterisk/taskprocessor.h"
/*** DOCUMENTATION
<configInfo name="cdr" language="en_US">
<synopsis>Call Detail Record configuration</synopsis>
<description>
<para>CDR is Call Detail Record, which provides logging services via a variety of
pluggable backend modules. Detailed call information can be recorded to
databases, files, etc. Useful for billing, fraud prevention, compliance with
Sarbanes-Oxley aka The Enron Act, QOS evaluations, and more.</para>
</description>
<configFile name="cdr.conf">
<configObject name="general">
<synopsis>Global settings applied to the CDR engine.</synopsis>
<configOption name="debug">
<synopsis>Enable/disable verbose CDR debugging.</synopsis>
<description><para>When set to <literal>True</literal>, verbose updates
of changes in CDR information will be logged. Note that this is only
of use when debugging CDR behavior.</para>
</description>
</configOption>
<configOption name="enable">
<synopsis>Enable/disable CDR logging.</synopsis>
<description><para>Define whether or not to use CDR logging. Setting this to "no" will override
any loading of backend CDR modules. Default is "yes".</para>
</description>
</configOption>
<configOption name="unanswered">
<synopsis>Log calls that are never answered and don't set an outgoing party.</synopsis>
<description><para>
Define whether or not to log unanswered calls that don't involve an outgoing party. Setting
this to "yes" will make calls to extensions that don't answer and don't set a side B channel
(such as by using the Dial application) receive CDR log entries. If this option is set to
"no", then those log entries will not be created. Unanswered calls which get offered to an
outgoing line will always receive log entries regardless of this option, and that is the
intended behavior.
</para>
</description>
</configOption>
<configOption name="congestion">
<synopsis>Log congested calls.</synopsis>
<description><para>Define whether or not to log congested calls. Setting this to "yes" will
report each call that fails to complete due to congestion conditions.</para>
</description>
</configOption>
<configOption name="endbeforehexten">
<synopsis>Don't produce CDRs while executing hangup logic</synopsis>
<description>
<para>As each CDR for a channel is finished, its end time is updated
and the CDR is finalized. When a channel is hung up and hangup
logic is present (in the form of a hangup handler or the
<literal>h</literal> extension), a new CDR is generated for the
channel. Any statistics are gathered from this new CDR. By enabling
this option, no new CDR is created for the dialplan logic that is
executed in <literal>h</literal> extensions or attached hangup handler
subroutines. The default value is <literal>yes</literal>, indicating
that a CDR will be generated during hangup logic.</para>
</description>
</configOption>
<configOption name="initiatedseconds">
<synopsis>Count microseconds for billsec purposes</synopsis>
<description><para>Normally, the <literal>billsec</literal> field logged to the CDR backends
is simply the end time (hangup time) minus the answer time in seconds. Internally,
asterisk stores the time in terms of microseconds and seconds. By setting
initiatedseconds to <literal>yes</literal>, you can force asterisk to report any seconds
that were initiated (a sort of round up method). Technically, this is
when the microsecond part of the end time is greater than the microsecond
part of the answer time, then the billsec time is incremented one second.</para>
</description>
</configOption>
<configOption name="batch">
<synopsis>Submit CDRs to the backends for processing in batches</synopsis>
<description><para>Define the CDR batch mode, where instead of posting the CDR at the end of
every call, the data will be stored in a buffer to help alleviate load on the
asterisk server.</para>
<warning><para>Use of batch mode may result in data loss after unsafe asterisk termination,
i.e., software crash, power failure, kill -9, etc.</para>
</warning>
</description>
</configOption>
<configOption name="size">
<synopsis>The maximum number of CDRs to accumulate before triggering a batch</synopsis>
<description><para>Define the maximum number of CDRs to accumulate in the buffer before posting
them to the backend engines. batch must be set to <literal>yes</literal>.</para>
</description>
</configOption>
<configOption name="time">
<synopsis>The maximum time to accumulate CDRs before triggering a batch</synopsis>
<description><para>Define the maximum time to accumulate CDRs before posting them in a batch to the
backend engines. If this time limit is reached, then it will post the records, regardless of the value
defined for size. batch must be set to <literal>yes</literal>.</para>
<note><para>Time is expressed in seconds.</para></note>
</description>
</configOption>
<configOption name="scheduleronly">
<synopsis>Post batched CDRs on their own thread instead of the scheduler</synopsis>
<description><para>The CDR engine uses the internal asterisk scheduler to determine when to post
records. Posting can either occur inside the scheduler thread, or a new
thread can be spawned for the submission of every batch. For small batches,
it might be acceptable to just use the scheduler thread, so set this to <literal>yes</literal>.
For large batches, say anything over size=10, a new thread is recommended, so
set this to <literal>no</literal>.</para>
</description>
</configOption>
<configOption name="safeshutdown">
<synopsis>Block shutdown of Asterisk until CDRs are submitted</synopsis>
<description><para>When shutting down asterisk, you can block until the CDRs are submitted. If
you don't, then data will likely be lost. You can always check the size of
the CDR batch buffer with the CLI <astcli>cdr status</astcli> command. To enable blocking on
submission of CDR data during asterisk shutdown, set this to <literal>yes</literal>.</para>
</description>
</configOption>
</configObject>
</configFile>
</configInfo>
***/
#define DEFAULT_ENABLED "1"
#define DEFAULT_BATCHMODE "0"
#define DEFAULT_UNANSWERED "0"
#define DEFAULT_CONGESTION "0"
#define DEFAULT_END_BEFORE_H_EXTEN "1"
#define DEFAULT_INITIATED_SECONDS "0"
#define DEFAULT_BATCH_SIZE "100"
#define MAX_BATCH_SIZE 1000
#define DEFAULT_BATCH_TIME "300"
#define MAX_BATCH_TIME 86400
#define DEFAULT_BATCH_SCHEDULER_ONLY "0"
#define DEFAULT_BATCH_SAFE_SHUTDOWN "1"
#define cdr_set_debug_mode(mod_cfg) \
do { \
cdr_debug_enabled = ast_test_flag(&(mod_cfg)->general->settings, CDR_DEBUG); \
} while (0)
static int cdr_debug_enabled;
#define CDR_DEBUG(fmt, ...) \
do { \
if (cdr_debug_enabled) { \
ast_verbose((fmt), ##__VA_ARGS__); \
} \
} while (0)
static void cdr_detach(struct ast_cdr *cdr);
static void cdr_submit_batch(int shutdown);
static int cdr_toggle_runtime_options(void);
/*! \brief The configuration settings for this module */
struct module_config {
struct ast_cdr_config *general; /*!< CDR global settings */
};
/*! \brief The container for the module configuration */
static AO2_GLOBAL_OBJ_STATIC(module_configs);
/*! \brief The type definition for general options */
static struct aco_type general_option = {
.type = ACO_GLOBAL,
.name = "general",
.item_offset = offsetof(struct module_config, general),
.category = "general",
.category_match = ACO_WHITELIST_EXACT,
};
/*! Config sections used by existing modules. Do not add to this list. */
static const char *ignore_categories[] = {
"csv",
"custom",
"manager",
"odbc",
"pgsql",
"radius",
"sqlite",
"tds",
"mysql",
NULL,
};
static struct aco_type ignore_option = {
.type = ACO_IGNORE,
.name = "modules",
.category = (const char*)ignore_categories,
.category_match = ACO_WHITELIST_ARRAY,
};
static void *module_config_alloc(void);
static void module_config_destructor(void *obj);
static void module_config_post_apply(void);
/*! \brief The file definition */
static struct aco_file module_file_conf = {
.filename = "cdr.conf",
.types = ACO_TYPES(&general_option, &ignore_option),
};
CONFIG_INFO_CORE("cdr", cfg_info, module_configs, module_config_alloc,
.files = ACO_FILES(&module_file_conf),
.post_apply_config = module_config_post_apply,
);
static struct aco_type *general_options[] = ACO_TYPES(&general_option);
static void module_config_post_apply(void)
{
struct module_config *mod_cfg;
mod_cfg = ao2_global_obj_ref(module_configs);
if (!mod_cfg) {
return;
}
cdr_set_debug_mode(mod_cfg);
ao2_cleanup(mod_cfg);
}
/*! \brief Dispose of a module config object */
static void module_config_destructor(void *obj)
{
struct module_config *cfg = obj;
if (!cfg) {
return;
}
ao2_ref(cfg->general, -1);
}
/*! \brief Create a new module config object */
static void *module_config_alloc(void)
{
struct module_config *mod_cfg;
struct ast_cdr_config *cdr_config;
mod_cfg = ao2_alloc(sizeof(*mod_cfg), module_config_destructor);
if (!mod_cfg) {
return NULL;
}
cdr_config = ao2_alloc(sizeof(*cdr_config), NULL);
if (!cdr_config) {
ao2_ref(cdr_config, -1);
return NULL;
}
mod_cfg->general = cdr_config;
return mod_cfg;
}
/*! \brief Registration object for CDR backends */
struct cdr_beitem {
char name[20];
char desc[80];
ast_cdrbe be;
AST_RWLIST_ENTRY(cdr_beitem) list;
int suspended:1;
};
/*! \brief List of registered backends */
static AST_RWLIST_HEAD_STATIC(be_list, cdr_beitem);
/*! \brief List of registered modifiers */
static AST_RWLIST_HEAD_STATIC(mo_list, cdr_beitem);
/*! \brief Queued CDR waiting to be batched */
struct cdr_batch_item {
struct ast_cdr *cdr;
struct cdr_batch_item *next;
};
/*! \brief The actual batch queue */
static struct cdr_batch {
int size;
struct cdr_batch_item *head;
struct cdr_batch_item *tail;
} *batch = NULL;
/*! \brief The global sequence counter used for CDRs */
static int global_cdr_sequence = 0;
/*! \brief Scheduler items */
static struct ast_sched_context *sched;
static int cdr_sched = -1;
AST_MUTEX_DEFINE_STATIC(cdr_sched_lock);
static pthread_t cdr_thread = AST_PTHREADT_NULL;
/*! \brief Lock protecting modifications to the batch queue */
AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
/*! \brief These are used to wake up the CDR thread when there's work to do */
AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
static ast_cond_t cdr_pending_cond;
/*! \brief A container of the active master CDRs indexed by Party A channel uniqueid */
static struct ao2_container *active_cdrs_master;
/*! \brief A container of all active CDRs with a Party B indexed by Party B channel name */
static struct ao2_container *active_cdrs_all;
/*! \brief Message router for stasis messages regarding channel state */
static struct stasis_message_router *stasis_router;
/*! \brief Our subscription for bridges */
static struct stasis_forward *bridge_subscription;
/*! \brief Our subscription for channels */
static struct stasis_forward *channel_subscription;
/*! \brief Our subscription for parking */
static struct stasis_forward *parking_subscription;
/*! \brief The parent topic for all topics we want to aggregate for CDRs */
static struct stasis_topic *cdr_topic;
/*! \brief A message type used to synchronize with the CDR topic */
STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_sync_message_type);
struct cdr_object;
/*! \brief Return types for \ref process_bridge_enter functions */
enum process_bridge_enter_results {
/*!
* The CDR was the only party in the bridge.
*/
BRIDGE_ENTER_ONLY_PARTY,
/*!
* The CDR was able to obtain a Party B from some other party already in the bridge
*/
BRIDGE_ENTER_OBTAINED_PARTY_B,
/*!
* The CDR was not able to obtain a Party B
*/
BRIDGE_ENTER_NO_PARTY_B,
/*!
* This CDR can't handle a bridge enter message and a new CDR needs to be created
*/
BRIDGE_ENTER_NEED_CDR,
};
/*!
* \brief A virtual table used for \ref cdr_object.
*
* Note that all functions are optional - if a subclass does not need an
* implementation, it is safe to leave it NULL.
*/
struct cdr_object_fn_table {
/*! \brief Name of the subclass */
const char *name;
/*!
* \brief An initialization function. This will be called automatically
* when a \ref cdr_object is switched to this type in
* \ref cdr_object_transition_state
*
* \param cdr The \ref cdr_object that was just transitioned
*/
void (* const init_function)(struct cdr_object *cdr);
/*!
* \brief Process a Party A update for the \ref cdr_object
*
* \param cdr The \ref cdr_object to process the update
* \param snapshot The snapshot for the CDR's Party A
* \retval 0 the CDR handled the update or ignored it
* \retval 1 the CDR is finalized and a new one should be made to handle it
*/
int (* const process_party_a)(struct cdr_object *cdr,
struct ast_channel_snapshot *snapshot);
/*!
* \brief Process a Party B update for the \ref cdr_object
*
* \param cdr The \ref cdr_object to process the update
* \param snapshot The snapshot for the CDR's Party B
*/
void (* const process_party_b)(struct cdr_object *cdr,
struct ast_channel_snapshot *snapshot);
/*!
* \brief Process the beginning of a dial. A dial message implies one of two
* things:
* The \ref cdr_object's Party A has been originated
* The \ref cdr_object's Party A is dialing its Party B
*
* \param cdr The \ref cdr_object
* \param caller The originator of the dial attempt
* \param peer The destination of the dial attempt
*
* \retval 0 if the parties in the dial were handled by this CDR
* \retval 1 if the parties could not be handled by this CDR
*/
int (* const process_dial_begin)(struct cdr_object *cdr,
struct ast_channel_snapshot *caller,
struct ast_channel_snapshot *peer);
/*!
* \brief Process the end of a dial. At the end of a dial, a CDR can be
* transitioned into one of two states - DialedPending
* (\ref dialed_pending_state_fn_table) or Finalized
* (\ref finalized_state_fn_table).
*
* \param cdr The \ref cdr_object
* \param caller The originator of the dial attempt
* \param peer the Destination of the dial attempt
* \param dial_status What happened
*
* \retval 0 if the parties in the dial were handled by this CDR
* \retval 1 if the parties could not be handled by this CDR
*/
int (* const process_dial_end)(struct cdr_object *cdr,
struct ast_channel_snapshot *caller,
struct ast_channel_snapshot *peer,
const char *dial_status);
/*!
* \brief Process the entering of a bridge by this CDR. The purpose of this
* callback is to have the CDR prepare itself for the bridge and attempt to
* find a valid Party B. The act of creating new CDRs based on the entering
* of this channel into the bridge is handled by the higher level message
* handler.
*
* Note that this handler is for when a channel enters into a "normal"
* bridge, where people actually talk to each other. Parking is its own
* thing.
*
* \param cdr The \ref cdr_object
* \param bridge The bridge that the Party A just entered into
* \param channel The \ref ast_channel_snapshot for this CDR's Party A
*
* \retval process_bridge_enter_results Defines whether or not this CDR was able
* to fully handle the bridge enter message.
*/
enum process_bridge_enter_results (* const process_bridge_enter)(
struct cdr_object *cdr,
struct ast_bridge_snapshot *bridge,
struct ast_channel_snapshot *channel);
/*!
* \brief Process entering into a parking bridge.
*
* \param cdr The \ref cdr_object
* \param bridge The parking bridge that Party A just entered into
* \param channel The \ref ast_channel_snapshot for this CDR's Party A
*
* \retval 0 This CDR successfully transitioned itself into the parked state
* \retval 1 This CDR couldn't handle the parking transition and we need a
* new CDR.
*/
int (* const process_parking_bridge_enter)(struct cdr_object *cdr,
struct ast_bridge_snapshot *bridge,
struct ast_channel_snapshot *channel);
/*!
* \brief Process the leaving of a bridge by this CDR.
*
* \param cdr The \ref cdr_object
* \param bridge The bridge that the Party A just left
* \param channel The \ref ast_channel_snapshot for this CDR's Party A
*
* \retval 0 This CDR left successfully
* \retval 1 Error
*/
int (* const process_bridge_leave)(struct cdr_object *cdr,
struct ast_bridge_snapshot *bridge,
struct ast_channel_snapshot *channel);
/*!
* \brief Process an update informing us that the channel got itself parked
*
* \param cdr The \ref cdr_object
* \param channel The parking information for this CDR's party A
*
* \retval 0 This CDR successfully parked itself
* \retval 1 This CDR couldn't handle the park
*/
int (* const process_parked_channel)(struct cdr_object *cdr,
struct ast_parked_call_payload *parking_info);
};
static int base_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
static enum process_bridge_enter_results base_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
static int base_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
static int base_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status);
static int base_process_parked_channel(struct cdr_object *cdr, struct ast_parked_call_payload *parking_info);
static void single_state_init_function(struct cdr_object *cdr);
static void single_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
static int single_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
static enum process_bridge_enter_results single_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
static int single_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
/*!
* \brief The virtual table for the Single state.
*
* A \ref cdr_object starts off in this state. This represents a channel that
* has no Party B information itself.
*
* A \ref cdr_object from this state can go into any of the following states:
* * \ref dial_state_fn_table
* * \ref bridge_state_fn_table
* * \ref finalized_state_fn_table
*/
struct cdr_object_fn_table single_state_fn_table = {
.name = "Single",
.init_function = single_state_init_function,
.process_party_a = base_process_party_a,
.process_party_b = single_state_process_party_b,
.process_dial_begin = single_state_process_dial_begin,
.process_dial_end = base_process_dial_end,
.process_bridge_enter = single_state_process_bridge_enter,
.process_parking_bridge_enter = single_state_process_parking_bridge_enter,
.process_bridge_leave = base_process_bridge_leave,
.process_parked_channel = base_process_parked_channel,
};
static void dial_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
static int dial_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
static int dial_state_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status);
static enum process_bridge_enter_results dial_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
/*!
* \brief The virtual table for the Dial state.
*
* A \ref cdr_object that has begun a dial operation. This state is entered when
* the Party A for a CDR is determined to be dialing out to a Party B or when
* a CDR is for an originated channel (in which case the Party A information is
* the originated channel, and there is no Party B).
*
* A \ref cdr_object from this state can go in any of the following states:
* * \ref dialed_pending_state_fn_table
* * \ref bridge_state_fn_table
* * \ref finalized_state_fn_table
*/
struct cdr_object_fn_table dial_state_fn_table = {
.name = "Dial",
.process_party_a = base_process_party_a,
.process_party_b = dial_state_process_party_b,
.process_dial_begin = dial_state_process_dial_begin,
.process_dial_end = dial_state_process_dial_end,
.process_bridge_enter = dial_state_process_bridge_enter,
.process_bridge_leave = base_process_bridge_leave,
};
static int dialed_pending_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
static int dialed_pending_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
static enum process_bridge_enter_results dialed_pending_state_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
static int dialed_pending_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
/*!
* \brief The virtual table for the Dialed Pending state.
*
* A \ref cdr_object that has successfully finished a dial operation, but we
* don't know what they're going to do yet. It's theoretically possible to dial
* a party and then have that party not be bridged with the caller; likewise,
* an origination can complete and the channel go off and execute dialplan. The
* pending state acts as a bridge between either:
* * Entering a bridge
* * Getting a new CDR for new dialplan execution
* * Switching from being originated to executing dialplan
*
* A \ref cdr_object from this state can go in any of the following states:
* * \ref single_state_fn_table
* * \ref dialed_pending_state_fn_table
* * \ref bridge_state_fn_table
* * \ref finalized_state_fn_table
*/
struct cdr_object_fn_table dialed_pending_state_fn_table = {
.name = "DialedPending",
.process_party_a = dialed_pending_state_process_party_a,
.process_dial_begin = dialed_pending_state_process_dial_begin,
.process_bridge_enter = dialed_pending_state_process_bridge_enter,
.process_parking_bridge_enter = dialed_pending_state_process_parking_bridge_enter,
.process_bridge_leave = base_process_bridge_leave,
.process_parked_channel = base_process_parked_channel,
};
static void bridge_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
static int bridge_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
/*!
* \brief The virtual table for the Bridged state
*
* A \ref cdr_object enters this state when it receives notification that the
* channel has entered a bridge.
*
* A \ref cdr_object from this state can go to:
* * \ref finalized_state_fn_table
*/
struct cdr_object_fn_table bridge_state_fn_table = {
.name = "Bridged",
.process_party_a = base_process_party_a,
.process_party_b = bridge_state_process_party_b,
.process_bridge_leave = bridge_state_process_bridge_leave,
.process_parked_channel = base_process_parked_channel,
};
static int parked_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
/*!
* \brief The virtual table for the Parked state
*
* Parking is weird. Unlike typical bridges, it has to be treated somewhat
* uniquely - a channel in a parking bridge (which is a subclass of a holding
* bridge) has to be handled as if the channel went into an application.
* However, when the channel comes out, we need a new CDR - unlike the Single
* state.
*/
struct cdr_object_fn_table parked_state_fn_table = {
.name = "Parked",
.process_party_a = base_process_party_a,
.process_bridge_leave = parked_state_process_bridge_leave,
.process_parked_channel = base_process_parked_channel,
};
static void finalized_state_init_function(struct cdr_object *cdr);
static int finalized_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
/*!
* \brief The virtual table for the finalized state.
*
* Once in the finalized state, the CDR is done. No modifications can be made
* to the CDR.
*/
struct cdr_object_fn_table finalized_state_fn_table = {
.name = "Finalized",
.init_function = finalized_state_init_function,
.process_party_a = finalized_state_process_party_a,
.process_bridge_enter = base_process_bridge_enter,
};
/*! \brief A wrapper object around a snapshot.
* Fields that are mutable by the CDR engine are replicated here.
*/
struct cdr_object_snapshot {
struct ast_channel_snapshot *snapshot; /*!< The channel snapshot */
char userfield[AST_MAX_USER_FIELD]; /*!< Userfield for the channel */
unsigned int flags; /*!< Specific flags for this party */
struct varshead variables; /*!< CDR variables for the channel */
};
/*! \brief An in-memory representation of an active CDR */
struct cdr_object {
struct cdr_object_snapshot party_a; /*!< The Party A information */
struct cdr_object_snapshot party_b; /*!< The Party B information */
struct cdr_object_fn_table *fn_table; /*!< The current virtual table */
enum ast_cdr_disposition disposition; /*!< The disposition of the CDR */
struct timeval start; /*!< When this CDR was created */
struct timeval answer; /*!< Either when the channel was answered, or when the path between channels was established */
struct timeval end; /*!< When this CDR was finalized */
unsigned int sequence; /*!< A monotonically increasing number for each CDR */
struct ast_flags flags; /*!< Flags on the CDR */
AST_DECLARE_STRING_FIELDS(
AST_STRING_FIELD(linkedid); /*!< Linked ID. Cached here as it may change out from party A, which must be immutable */
AST_STRING_FIELD(uniqueid); /*!< Unique id of party A. Cached here as it is the master CDR container key */
AST_STRING_FIELD(name); /*!< Channel name of party A. Cached here as the party A address may change */
AST_STRING_FIELD(bridge); /*!< The bridge the party A happens to be in. */
AST_STRING_FIELD(appl); /*!< The last accepted application party A was in */
AST_STRING_FIELD(data); /*!< The data for the last accepted application party A was in */
AST_STRING_FIELD(context); /*!< The accepted context for Party A */
AST_STRING_FIELD(exten); /*!< The accepted extension for Party A */
AST_STRING_FIELD(party_b_name); /*!< Party B channel name. Cached here as it is the all CDRs container key */
);
struct cdr_object *next; /*!< The next CDR object in the chain */
struct cdr_object *last; /*!< The last CDR object in the chain */
int is_root; /*!< True if this is the first CDR in the chain */
};
/*!
* \brief Copy variables from one list to another
* \param to_list destination
* \param from_list source
* \retval The number of copied variables
*/
static int copy_variables(struct varshead *to_list, struct varshead *from_list)
{
struct ast_var_t *variables;
struct ast_var_t *newvariable;
const char *var;
const char *val;
int x = 0;
AST_LIST_TRAVERSE(from_list, variables, entries) {
var = ast_var_name(variables);
if (ast_strlen_zero(var)) {
continue;
}
val = ast_var_value(variables);
if (ast_strlen_zero(val)) {
continue;
}
newvariable = ast_var_assign(var, val);
if (newvariable) {
AST_LIST_INSERT_HEAD(to_list, newvariable, entries);
++x;
}
}
return x;
}
/*!
* \brief Delete all variables from a variable list
* \param headp The head pointer to the variable list to delete
*/
static void free_variables(struct varshead *headp)
{
struct ast_var_t *vardata;
while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries))) {
ast_var_delete(vardata);
}
}
/*!
* \brief Copy a snapshot and its details
* \param dst The destination
* \param src The source
*/
static void cdr_object_snapshot_copy(struct cdr_object_snapshot *dst, struct cdr_object_snapshot *src)
{
ao2_t_replace(dst->snapshot, src->snapshot, "CDR snapshot copy");
strcpy(dst->userfield, src->userfield);
dst->flags = src->flags;
copy_variables(&dst->variables, &src->variables);
}
/*!
* \brief Transition a \ref cdr_object to a new state with initiation flag
* \param cdr The \ref cdr_object to transition
* \param fn_table The \ref cdr_object_fn_table state to go to
*/
static void cdr_object_transition_state_init(struct cdr_object *cdr, struct cdr_object_fn_table *fn_table, int do_init)
{
CDR_DEBUG("%p - Transitioning CDR for %s from state %s to %s\n",
cdr, cdr->party_a.snapshot->base->name,
cdr->fn_table ? cdr->fn_table->name : "NONE", fn_table->name);
cdr->fn_table = fn_table;
if (cdr->fn_table->init_function && do_init) {
cdr->fn_table->init_function(cdr);
}
}
/*!
* \brief Transition a \ref cdr_object to a new state
* \param cdr The \ref cdr_object to transition
* \param fn_table The \ref cdr_object_fn_table state to go to
*/
static void cdr_object_transition_state(struct cdr_object *cdr, struct cdr_object_fn_table *fn_table)
{
cdr_object_transition_state_init(cdr, fn_table, 1);
}
/*!
* \internal
* \brief Hash function for master CDR container indexed by Party A uniqueid.
*/
static int cdr_master_hash_fn(const void *obj, const int flags)
{
const struct cdr_object *cdr;
const char *key;
switch (flags & OBJ_SEARCH_MASK) {
case OBJ_SEARCH_KEY:
key = obj;
break;
case OBJ_SEARCH_OBJECT:
cdr = obj;
key = cdr->uniqueid;
break;
default:
ast_assert(0);
return 0;
}
return ast_str_case_hash(key);
}
/*!
* \internal
* \brief Comparison function for master CDR container indexed by Party A uniqueid.
*/
static int cdr_master_cmp_fn(void *obj, void *arg, int flags)
{
struct cdr_object *left = obj;
struct cdr_object *right = arg;
const char *right_key = arg;
int cmp;
switch (flags & OBJ_SEARCH_MASK) {
case OBJ_SEARCH_OBJECT:
right_key = right->uniqueid;
/* Fall through */
case OBJ_SEARCH_KEY:
cmp = strcmp(left->uniqueid, right_key);
break;
case OBJ_SEARCH_PARTIAL_KEY:
/*
* We could also use a partial key struct containing a length
* so strlen() does not get called for every comparison instead.
*/
cmp = strncmp(left->uniqueid, right_key, strlen(right_key));
break;
default:
/* Sort can only work on something with a full or partial key. */
ast_assert(0);
cmp = 0;
break;
}
return cmp ? 0 : CMP_MATCH;
}
/*!
* \internal
* \brief Hash function for all CDR container indexed by Party B channel name.
*/
static int cdr_all_hash_fn(const void *obj, const int flags)
{
const struct cdr_object *cdr;
const char *key;
switch (flags & OBJ_SEARCH_MASK) {
case OBJ_SEARCH_KEY:
key = obj;
break;
case OBJ_SEARCH_OBJECT:
cdr = obj;
key = cdr->party_b_name;
break;
default:
ast_assert(0);
return 0;
}
return ast_str_case_hash(key);
}
/*!
* \internal
* \brief Comparison function for all CDR container indexed by Party B channel name.
*/
static int cdr_all_cmp_fn(void *obj, void *arg, int flags)
{
struct cdr_object *left = obj;
struct cdr_object *right = arg;
const char *right_key = arg;
int cmp;
switch (flags & OBJ_SEARCH_MASK) {
case OBJ_SEARCH_OBJECT:
right_key = right->party_b_name;
/* Fall through */
case OBJ_SEARCH_KEY:
cmp = strcasecmp(left->party_b_name, right_key);
break;
case OBJ_SEARCH_PARTIAL_KEY:
/*
* We could also use a partial key struct containing a length
* so strlen() does not get called for every comparison instead.
*/
cmp = strncasecmp(left->party_b_name, right_key, strlen(right_key));
break;
default:
/* Sort can only work on something with a full or partial key. */
ast_assert(0);
cmp = 0;
break;
}
return cmp ? 0 : CMP_MATCH;
}
/*!
* \internal
* \brief Relink the CDR because Party B's snapshot changed.
* \since 13.19.0
*
* \return Nothing
*/
static void cdr_all_relink(struct cdr_object *cdr)
{
ao2_lock(active_cdrs_all);
if (cdr->party_b.snapshot) {
if (strcasecmp(cdr->party_b_name, cdr->party_b.snapshot->base->name)) {
ao2_unlink_flags(active_cdrs_all, cdr, OBJ_NOLOCK);
ast_string_field_set(cdr, party_b_name, cdr->party_b.snapshot->base->name);
ao2_link_flags(active_cdrs_all, cdr, OBJ_NOLOCK);
}
} else {
ao2_unlink_flags(active_cdrs_all, cdr, OBJ_NOLOCK);
ast_string_field_set(cdr, party_b_name, "");
}
ao2_unlock(active_cdrs_all);
}
/*!
* \internal
* \brief Unlink the master CDR and chained records from the active_cdrs_all container.
* \since 13.19.0
*
* \return Nothing
*/
static void cdr_all_unlink(struct cdr_object *cdr)
{
struct cdr_object *cur;
struct cdr_object *next;
ast_assert(cdr->is_root);
/* Hold a ref to the root CDR to ensure the list members don't go away on us. */
ao2_ref(cdr, +1);
ao2_lock(active_cdrs_all);
for (cur = cdr; cur; cur = next) {
next = cur->next;
ao2_unlink_flags(active_cdrs_all, cur, OBJ_NOLOCK);
/*
* It is safe to still use cur after unlinking because the
* root CDR holds a ref to all the CDRs in the list and we
* have a ref to the root CDR.
*/
ast_string_field_set(cur, party_b_name, "");
}
ao2_unlock(active_cdrs_all);
ao2_ref(cdr, -1);
}
/*!
* \brief \ref cdr_object Destructor
*/
static void cdr_object_dtor(void *obj)
{
struct cdr_object *cdr = obj;