2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Call Detail Record API
23 * \author Mark Spencer <markster@digium.com>
25 * \note Includes code and algorithms from the Zapata library.
27 * \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
28 * through our fingers somehow. If someone allocates a CDR, it must be completely handled normally
29 * or a WARNING shall be logged, so that we can best keep track of any escape condition where the CDR
30 * isn't properly generated and posted.
33 /*! \li \ref cdr.c uses the configuration file \ref cdr.conf
34 * \addtogroup configuration_file Configuration Files
38 * \page cdr.conf cdr.conf
39 * \verbinclude cdr.conf.sample
43 <support_level>core</support_level>
48 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
53 #include "asterisk/lock.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/cdr.h"
56 #include "asterisk/callerid.h"
57 #include "asterisk/manager.h"
58 #include "asterisk/causes.h"
59 #include "asterisk/linkedlists.h"
60 #include "asterisk/utils.h"
61 #include "asterisk/sched.h"
62 #include "asterisk/config.h"
63 #include "asterisk/cli.h"
64 #include "asterisk/stringfields.h"
65 #include "asterisk/data.h"
66 #include "asterisk/config_options.h"
67 #include "asterisk/json.h"
68 #include "asterisk/parking.h"
69 #include "asterisk/stasis.h"
70 #include "asterisk/stasis_channels.h"
71 #include "asterisk/stasis_bridges.h"
72 #include "asterisk/stasis_message_router.h"
73 #include "asterisk/astobj2.h"
76 <configInfo name="cdr" language="en_US">
77 <synopsis>Call Detail Record configuration</synopsis>
79 <para>CDR is Call Detail Record, which provides logging services via a variety of
80 pluggable backend modules. Detailed call information can be recorded to
81 databases, files, etc. Useful for billing, fraud prevention, compliance with
82 Sarbanes-Oxley aka The Enron Act, QOS evaluations, and more.</para>
84 <configFile name="cdr.conf">
85 <configObject name="general">
86 <synopsis>Global settings applied to the CDR engine.</synopsis>
87 <configOption name="debug">
88 <synopsis>Enable/disable verbose CDR debugging.</synopsis>
89 <description><para>When set to <literal>True</literal>, verbose updates
90 of changes in CDR information will be logged. Note that this is only
91 of use when debugging CDR behavior.</para>
94 <configOption name="enable">
95 <synopsis>Enable/disable CDR logging.</synopsis>
96 <description><para>Define whether or not to use CDR logging. Setting this to "no" will override
97 any loading of backend CDR modules. Default is "yes".</para>
100 <configOption name="unanswered">
101 <synopsis>Log calls that are never answered.</synopsis>
102 <description><para>Define whether or not to log unanswered calls. Setting this to "yes" will
103 report every attempt to ring a phone in dialing attempts, when it was not
104 answered. For example, if you try to dial 3 extensions, and this option is "yes",
105 you will get 3 CDR's, one for each phone that was rung. Some find this information horribly
106 useless. Others find it very valuable. Note, in "yes" mode, you will see one CDR, with one of
107 the call targets on one side, and the originating channel on the other, and then one CDR for
108 each channel attempted. This may seem redundant, but cannot be helped.</para>
109 <para>In brief, this option controls the reporting of unanswered calls which only have an A
110 party. Calls which get offered to an outgoing line, but are unanswered, are still
111 logged, and that is the intended behavior. (It also results in some B side CDRs being
112 output, as they have the B side channel as their source channel, and no destination
116 <configOption name="congestion">
117 <synopsis>Log congested calls.</synopsis>
118 <description><para>Define whether or not to log congested calls. Setting this to "yes" will
119 report each call that fails to complete due to congestion conditions.</para>
122 <configOption name="endbeforehexten">
123 <synopsis>Don't produce CDRs while executing hangup logic</synopsis>
125 <para>As each CDR for a channel is finished, its end time is updated
126 and the CDR is finalized. When a channel is hung up and hangup
127 logic is present (in the form of a hangup handler or the
128 <literal>h</literal> extension), a new CDR is generated for the
129 channel. Any statistics are gathered from this new CDR. By enabling
130 this option, no new CDR is created for the dialplan logic that is
131 executed in <literal>h</literal> extensions or attached hangup handler
132 subroutines. The default value is <literal>yes</literal>, indicating
133 that a CDR will be generated during hangup logic.</para>
136 <configOption name="initiatedseconds">
137 <synopsis>Count microseconds for billsec purposes</synopsis>
138 <description><para>Normally, the <literal>billsec</literal> field logged to the CDR backends
139 is simply the end time (hangup time) minus the answer time in seconds. Internally,
140 asterisk stores the time in terms of microseconds and seconds. By setting
141 initiatedseconds to <literal>yes</literal>, you can force asterisk to report any seconds
142 that were initiated (a sort of round up method). Technically, this is
143 when the microsecond part of the end time is greater than the microsecond
144 part of the answer time, then the billsec time is incremented one second.</para>
147 <configOption name="batch">
148 <synopsis>Submit CDRs to the backends for processing in batches</synopsis>
149 <description><para>Define the CDR batch mode, where instead of posting the CDR at the end of
150 every call, the data will be stored in a buffer to help alleviate load on the
151 asterisk server.</para>
152 <warning><para>Use of batch mode may result in data loss after unsafe asterisk termination,
153 i.e., software crash, power failure, kill -9, etc.</para>
157 <configOption name="size">
158 <synopsis>The maximum number of CDRs to accumulate before triggering a batch</synopsis>
159 <description><para>Define the maximum number of CDRs to accumulate in the buffer before posting
160 them to the backend engines. batch must be set to <literal>yes</literal>.</para>
163 <configOption name="time">
164 <synopsis>The maximum time to accumulate CDRs before triggering a batch</synopsis>
165 <description><para>Define the maximum time to accumulate CDRs before posting them in a batch to the
166 backend engines. If this time limit is reached, then it will post the records, regardless of the value
167 defined for size. batch must be set to <literal>yes</literal>.</para>
168 <note><para>Time is expressed in seconds.</para></note>
171 <configOption name="scheduleronly">
172 <synopsis>Post batched CDRs on their own thread instead of the scheduler</synopsis>
173 <description><para>The CDR engine uses the internal asterisk scheduler to determine when to post
174 records. Posting can either occur inside the scheduler thread, or a new
175 thread can be spawned for the submission of every batch. For small batches,
176 it might be acceptable to just use the scheduler thread, so set this to <literal>yes</literal>.
177 For large batches, say anything over size=10, a new thread is recommended, so
178 set this to <literal>no</literal>.</para>
181 <configOption name="safeshutdown">
182 <synopsis>Block shutdown of Asterisk until CDRs are submitted</synopsis>
183 <description><para>When shutting down asterisk, you can block until the CDRs are submitted. If
184 you don't, then data will likely be lost. You can always check the size of
185 the CDR batch buffer with the CLI <astcli>cdr status</astcli> command. To enable blocking on
186 submission of CDR data during asterisk shutdown, set this to <literal>yes</literal>.</para>
195 /* The prime here should be similar in size to the channel container. */
197 #define NUM_CDR_BUCKETS 61
199 #define NUM_CDR_BUCKETS 769
202 #define DEFAULT_ENABLED "1"
203 #define DEFAULT_BATCHMODE "0"
204 #define DEFAULT_UNANSWERED "0"
205 #define DEFAULT_CONGESTION "0"
206 #define DEFAULT_END_BEFORE_H_EXTEN "1"
207 #define DEFAULT_INITIATED_SECONDS "0"
209 #define DEFAULT_BATCH_SIZE "100"
210 #define MAX_BATCH_SIZE 1000
211 #define DEFAULT_BATCH_TIME "300"
212 #define MAX_BATCH_TIME 86400
213 #define DEFAULT_BATCH_SCHEDULER_ONLY "0"
214 #define DEFAULT_BATCH_SAFE_SHUTDOWN "1"
216 #define CDR_DEBUG(mod_cfg, fmt, ...) \
218 if (ast_test_flag(&(mod_cfg)->general->settings, CDR_DEBUG)) { \
219 ast_verb(1, (fmt), ##__VA_ARGS__); \
222 static void cdr_detach(struct ast_cdr *cdr);
223 static void cdr_submit_batch(int shutdown);
224 static int cdr_toggle_runtime_options(void);
226 /*! \brief The configuration settings for this module */
227 struct module_config {
228 struct ast_cdr_config *general; /*< CDR global settings */
231 /*! \brief The container for the module configuration */
232 static AO2_GLOBAL_OBJ_STATIC(module_configs);
234 /*! \brief The type definition for general options */
235 static struct aco_type general_option = {
238 .item_offset = offsetof(struct module_config, general),
239 .category = "^general$",
240 .category_match = ACO_WHITELIST,
243 static void *module_config_alloc(void);
244 static void module_config_destructor(void *obj);
246 /*! \brief The file definition */
247 static struct aco_file module_file_conf = {
248 .filename = "cdr.conf",
249 .skip_category = "(^csv$|^custom$|^manager$|^odbc$|^pgsql$|^radius$|^sqlite$|^tds$|^mysql$)",
250 .types = ACO_TYPES(&general_option),
253 CONFIG_INFO_CORE("cdr", cfg_info, module_configs, module_config_alloc,
254 .files = ACO_FILES(&module_file_conf),
257 static struct aco_type *general_options[] = ACO_TYPES(&general_option);
259 /*! \brief Dispose of a module config object */
260 static void module_config_destructor(void *obj)
262 struct module_config *cfg = obj;
267 ao2_ref(cfg->general, -1);
270 /*! \brief Create a new module config object */
271 static void *module_config_alloc(void)
273 struct module_config *mod_cfg;
274 struct ast_cdr_config *cdr_config;
276 mod_cfg = ao2_alloc(sizeof(*mod_cfg), module_config_destructor);
281 cdr_config = ao2_alloc(sizeof(*cdr_config), NULL);
283 ao2_ref(cdr_config, -1);
286 mod_cfg->general = cdr_config;
291 /*! \brief Registration object for CDR backends */
296 AST_RWLIST_ENTRY(cdr_beitem) list;
300 /*! \brief List of registered backends */
301 static AST_RWLIST_HEAD_STATIC(be_list, cdr_beitem);
303 /*! \brief Queued CDR waiting to be batched */
304 struct cdr_batch_item {
306 struct cdr_batch_item *next;
309 /*! \brief The actual batch queue */
310 static struct cdr_batch {
312 struct cdr_batch_item *head;
313 struct cdr_batch_item *tail;
316 /*! \brief The global sequence counter used for CDRs */
317 static int global_cdr_sequence = 0;
319 /*! \brief Scheduler items */
320 static struct ast_sched_context *sched;
321 static int cdr_sched = -1;
322 AST_MUTEX_DEFINE_STATIC(cdr_sched_lock);
323 static pthread_t cdr_thread = AST_PTHREADT_NULL;
325 /*! \brief Lock protecting modifications to the batch queue */
326 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
328 /*! \brief These are used to wake up the CDR thread when there's work to do */
329 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
330 static ast_cond_t cdr_pending_cond;
332 /*! \brief A container of the active CDRs indexed by Party A channel id */
333 static struct ao2_container *active_cdrs_by_channel;
335 /*! \brief Message router for stasis messages regarding channel state */
336 static struct stasis_message_router *stasis_router;
338 /*! \brief Our subscription for bridges */
339 static struct stasis_forward *bridge_subscription;
341 /*! \brief Our subscription for channels */
342 static struct stasis_forward *channel_subscription;
344 /*! \brief Our subscription for parking */
345 static struct stasis_forward *parking_subscription;
347 /*! \brief The parent topic for all topics we want to aggregate for CDRs */
348 static struct stasis_topic *cdr_topic;
350 /*! \brief A message type used to synchronize with the CDR topic */
351 STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_sync_message_type);
355 /*! \brief Return types for \ref process_bridge_enter functions */
356 enum process_bridge_enter_results {
358 * The CDR was the only party in the bridge.
360 BRIDGE_ENTER_ONLY_PARTY,
362 * The CDR was able to obtain a Party B from some other party already in the bridge
364 BRIDGE_ENTER_OBTAINED_PARTY_B,
366 * The CDR was not able to obtain a Party B
368 BRIDGE_ENTER_NO_PARTY_B,
370 * This CDR can't handle a bridge enter message and a new CDR needs to be created
372 BRIDGE_ENTER_NEED_CDR,
376 * \brief A virtual table used for \ref cdr_object.
378 * Note that all functions are optional - if a subclass does not need an
379 * implementation, it is safe to leave it NULL.
381 struct cdr_object_fn_table {
382 /*! \brief Name of the subclass */
386 * \brief An initialization function. This will be called automatically
387 * when a \ref cdr_object is switched to this type in
388 * \ref cdr_object_transition_state
390 * \param cdr The \ref cdr_object that was just transitioned
392 void (* const init_function)(struct cdr_object *cdr);
395 * \brief Process a Party A update for the \ref cdr_object
397 * \param cdr The \ref cdr_object to process the update
398 * \param snapshot The snapshot for the CDR's Party A
399 * \retval 0 the CDR handled the update or ignored it
400 * \retval 1 the CDR is finalized and a new one should be made to handle it
402 int (* const process_party_a)(struct cdr_object *cdr,
403 struct ast_channel_snapshot *snapshot);
406 * \brief Process a Party B update for the \ref cdr_object
408 * \param cdr The \ref cdr_object to process the update
409 * \param snapshot The snapshot for the CDR's Party B
411 void (* const process_party_b)(struct cdr_object *cdr,
412 struct ast_channel_snapshot *snapshot);
415 * \brief Process the beginning of a dial. A dial message implies one of two
417 * The \ref cdr_object's Party A has been originated
418 * The \ref cdr_object's Party A is dialing its Party B
420 * \param cdr The \ref cdr_object
421 * \param caller The originator of the dial attempt
422 * \param peer The destination of the dial attempt
424 * \retval 0 if the parties in the dial were handled by this CDR
425 * \retval 1 if the parties could not be handled by this CDR
427 int (* const process_dial_begin)(struct cdr_object *cdr,
428 struct ast_channel_snapshot *caller,
429 struct ast_channel_snapshot *peer);
432 * \brief Process the end of a dial. At the end of a dial, a CDR can be
433 * transitioned into one of two states - DialedPending
434 * (\ref dialed_pending_state_fn_table) or Finalized
435 * (\ref finalized_state_fn_table).
437 * \param cdr The \ref cdr_object
438 * \param caller The originator of the dial attempt
439 * \param peer the Destination of the dial attempt
440 * \param dial_status What happened
442 * \retval 0 if the parties in the dial were handled by this CDR
443 * \retval 1 if the parties could not be handled by this CDR
445 int (* const process_dial_end)(struct cdr_object *cdr,
446 struct ast_channel_snapshot *caller,
447 struct ast_channel_snapshot *peer,
448 const char *dial_status);
451 * \brief Process the entering of a bridge by this CDR. The purpose of this
452 * callback is to have the CDR prepare itself for the bridge and attempt to
453 * find a valid Party B. The act of creating new CDRs based on the entering
454 * of this channel into the bridge is handled by the higher level message
457 * Note that this handler is for when a channel enters into a "normal"
458 * bridge, where people actually talk to each other. Parking is its own
461 * \param cdr The \ref cdr_object
462 * \param bridge The bridge that the Party A just entered into
463 * \param channel The \ref ast_channel_snapshot for this CDR's Party A
465 * \retval process_bridge_enter_results Defines whether or not this CDR was able
466 * to fully handle the bridge enter message.
468 enum process_bridge_enter_results (* const process_bridge_enter)(
469 struct cdr_object *cdr,
470 struct ast_bridge_snapshot *bridge,
471 struct ast_channel_snapshot *channel);
474 * \brief Process entering into a parking bridge.
476 * \param cdr The \ref cdr_object
477 * \param bridge The parking bridge that Party A just entered into
478 * \param channel The \ref ast_channel_snapshot for this CDR's Party A
480 * \retval 0 This CDR successfully transitioned itself into the parked state
481 * \retval 1 This CDR couldn't handle the parking transition and we need a
484 int (* const process_parking_bridge_enter)(struct cdr_object *cdr,
485 struct ast_bridge_snapshot *bridge,
486 struct ast_channel_snapshot *channel);
489 * \brief Process the leaving of a bridge by this CDR.
491 * \param cdr The \ref cdr_object
492 * \param bridge The bridge that the Party A just left
493 * \param channel The \ref ast_channel_snapshot for this CDR's Party A
495 * \retval 0 This CDR left successfully
498 int (* const process_bridge_leave)(struct cdr_object *cdr,
499 struct ast_bridge_snapshot *bridge,
500 struct ast_channel_snapshot *channel);
503 * \brief Process an update informing us that the channel got itself parked
505 * \param cdr The \ref cdr_object
506 * \param channel The parking information for this CDR's party A
508 * \retval 0 This CDR successfully parked itself
509 * \retval 1 This CDR couldn't handle the park
511 int (* const process_parked_channel)(struct cdr_object *cdr,
512 struct ast_parked_call_payload *parking_info);
515 static int base_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
516 static enum process_bridge_enter_results base_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
517 static int base_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
518 static int base_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status);
519 static int base_process_parked_channel(struct cdr_object *cdr, struct ast_parked_call_payload *parking_info);
521 static void single_state_init_function(struct cdr_object *cdr);
522 static void single_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
523 static int single_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
524 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);
525 static int single_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
528 * \brief The virtual table for the Single state.
530 * A \ref cdr_object starts off in this state. This represents a channel that
531 * has no Party B information itself.
533 * A \ref cdr_object from this state can go into any of the following states:
534 * * \ref dial_state_fn_table
535 * * \ref bridge_state_fn_table
536 * * \ref finalized_state_fn_table
538 struct cdr_object_fn_table single_state_fn_table = {
540 .init_function = single_state_init_function,
541 .process_party_a = base_process_party_a,
542 .process_party_b = single_state_process_party_b,
543 .process_dial_begin = single_state_process_dial_begin,
544 .process_dial_end = base_process_dial_end,
545 .process_bridge_enter = single_state_process_bridge_enter,
546 .process_parking_bridge_enter = single_state_process_parking_bridge_enter,
547 .process_bridge_leave = base_process_bridge_leave,
548 .process_parked_channel = base_process_parked_channel,
551 static void dial_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
552 static int dial_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
553 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);
554 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);
557 * \brief The virtual table for the Dial state.
559 * A \ref cdr_object that has begun a dial operation. This state is entered when
560 * the Party A for a CDR is determined to be dialing out to a Party B or when
561 * a CDR is for an originated channel (in which case the Party A information is
562 * the originated channel, and there is no Party B).
564 * A \ref cdr_object from this state can go in any of the following states:
565 * * \ref dialed_pending_state_fn_table
566 * * \ref bridge_state_fn_table
567 * * \ref finalized_state_fn_table
569 struct cdr_object_fn_table dial_state_fn_table = {
571 .process_party_a = base_process_party_a,
572 .process_party_b = dial_state_process_party_b,
573 .process_dial_begin = dial_state_process_dial_begin,
574 .process_dial_end = dial_state_process_dial_end,
575 .process_bridge_enter = dial_state_process_bridge_enter,
576 .process_bridge_leave = base_process_bridge_leave,
579 static int dialed_pending_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
580 static int dialed_pending_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
581 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);
582 static int dialed_pending_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
585 * \brief The virtual table for the Dialed Pending state.
587 * A \ref cdr_object that has successfully finished a dial operation, but we
588 * don't know what they're going to do yet. It's theoretically possible to dial
589 * a party and then have that party not be bridged with the caller; likewise,
590 * an origination can complete and the channel go off and execute dialplan. The
591 * pending state acts as a bridge between either:
592 * * Entering a bridge
593 * * Getting a new CDR for new dialplan execution
594 * * Switching from being originated to executing dialplan
596 * A \ref cdr_object from this state can go in any of the following states:
597 * * \ref single_state_fn_table
598 * * \ref dialed_pending_state_fn_table
599 * * \ref bridge_state_fn_table
600 * * \ref finalized_state_fn_table
602 struct cdr_object_fn_table dialed_pending_state_fn_table = {
603 .name = "DialedPending",
604 .process_party_a = dialed_pending_state_process_party_a,
605 .process_dial_begin = dialed_pending_state_process_dial_begin,
606 .process_bridge_enter = dialed_pending_state_process_bridge_enter,
607 .process_parking_bridge_enter = dialed_pending_state_process_parking_bridge_enter,
608 .process_bridge_leave = base_process_bridge_leave,
609 .process_parked_channel = base_process_parked_channel,
612 static void bridge_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
613 static int bridge_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
616 * \brief The virtual table for the Bridged state
618 * A \ref cdr_object enters this state when it receives notification that the
619 * channel has entered a bridge.
621 * A \ref cdr_object from this state can go to:
622 * * \ref finalized_state_fn_table
624 struct cdr_object_fn_table bridge_state_fn_table = {
626 .process_party_a = base_process_party_a,
627 .process_party_b = bridge_state_process_party_b,
628 .process_bridge_leave = bridge_state_process_bridge_leave,
629 .process_parked_channel = base_process_parked_channel,
632 static int parked_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
635 * \brief The virtual table for the Parked state
637 * Parking is weird. Unlike typical bridges, it has to be treated somewhat
638 * uniquely - a channel in a parking bridge (which is a subclass of a holding
639 * bridge) has to be handled as if the channel went into an application.
640 * However, when the channel comes out, we need a new CDR - unlike the Single
643 struct cdr_object_fn_table parked_state_fn_table = {
645 .process_party_a = base_process_party_a,
646 .process_bridge_leave = parked_state_process_bridge_leave,
647 .process_parked_channel = base_process_parked_channel,
650 static void finalized_state_init_function(struct cdr_object *cdr);
651 static int finalized_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
654 * \brief The virtual table for the finalized state.
656 * Once in the finalized state, the CDR is done. No modifications can be made
659 struct cdr_object_fn_table finalized_state_fn_table = {
661 .init_function = finalized_state_init_function,
662 .process_party_a = finalized_state_process_party_a,
663 .process_bridge_enter = base_process_bridge_enter,
666 /*! \brief A wrapper object around a snapshot.
667 * Fields that are mutable by the CDR engine are replicated here.
669 struct cdr_object_snapshot {
670 struct ast_channel_snapshot *snapshot; /*!< The channel snapshot */
671 char userfield[AST_MAX_USER_FIELD]; /*!< Userfield for the channel */
672 unsigned int flags; /*!< Specific flags for this party */
673 struct varshead variables; /*!< CDR variables for the channel */
676 /*! \brief An in-memory representation of an active CDR */
678 struct cdr_object_snapshot party_a; /*!< The Party A information */
679 struct cdr_object_snapshot party_b; /*!< The Party B information */
680 struct cdr_object_fn_table *fn_table; /*!< The current virtual table */
682 enum ast_cdr_disposition disposition; /*!< The disposition of the CDR */
683 struct timeval start; /*!< When this CDR was created */
684 struct timeval answer; /*!< Either when the channel was answered, or when the path between channels was established */
685 struct timeval end; /*!< When this CDR was finalized */
686 unsigned int sequence; /*!< A monotonically increasing number for each CDR */
687 struct ast_flags flags; /*!< Flags on the CDR */
688 AST_DECLARE_STRING_FIELDS(
689 AST_STRING_FIELD(linkedid); /*!< Linked ID. Cached here as it may change out from party A, which must be immutable */
690 AST_STRING_FIELD(uniqueid); /*!< Unique id of party A. Cached here as it is the primary key of this CDR */
691 AST_STRING_FIELD(name); /*!< Channel name of party A. Cached here as the party A address may change */
692 AST_STRING_FIELD(bridge); /*!< The bridge the party A happens to be in. */
693 AST_STRING_FIELD(appl); /*!< The last accepted application party A was in */
694 AST_STRING_FIELD(data); /*!< The data for the last accepted application party A was in */
696 struct cdr_object *next; /*!< The next CDR object in the chain */
697 struct cdr_object *last; /*!< The last CDR object in the chain */
701 * \brief Copy variables from one list to another
702 * \param to_list destination
703 * \param from_list source
704 * \retval The number of copied variables
706 static int copy_variables(struct varshead *to_list, struct varshead *from_list)
708 struct ast_var_t *variables;
709 struct ast_var_t *newvariable;
714 AST_LIST_TRAVERSE(from_list, variables, entries) {
715 var = ast_var_name(variables);
716 if (ast_strlen_zero(var)) {
719 val = ast_var_value(variables);
720 if (ast_strlen_zero(val)) {
723 newvariable = ast_var_assign(var, val);
725 AST_LIST_INSERT_HEAD(to_list, newvariable, entries);
734 * \brief Delete all variables from a variable list
735 * \param headp The head pointer to the variable list to delete
737 static void free_variables(struct varshead *headp)
739 struct ast_var_t *vardata;
741 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries))) {
742 ast_var_delete(vardata);
747 * \brief Copy a snapshot and its details
748 * \param dst The destination
749 * \param src The source
751 static void cdr_object_snapshot_copy(struct cdr_object_snapshot *dst, struct cdr_object_snapshot *src)
754 ao2_t_ref(dst->snapshot, -1, "release old snapshot during copy");
756 dst->snapshot = src->snapshot;
757 ao2_t_ref(dst->snapshot, +1, "bump new snapshot during copy");
758 strcpy(dst->userfield, src->userfield);
759 dst->flags = src->flags;
760 copy_variables(&dst->variables, &src->variables);
764 * \brief Transition a \ref cdr_object to a new state
765 * \param cdr The \ref cdr_object to transition
766 * \param fn_table The \ref cdr_object_fn_table state to go to
768 static void cdr_object_transition_state(struct cdr_object *cdr, struct cdr_object_fn_table *fn_table)
770 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
772 CDR_DEBUG(mod_cfg, "%p - Transitioning CDR for %s from state %s to %s\n",
773 cdr, cdr->party_a.snapshot->name,
774 cdr->fn_table ? cdr->fn_table->name : "NONE", fn_table->name);
775 cdr->fn_table = fn_table;
776 if (cdr->fn_table->init_function) {
777 cdr->fn_table->init_function(cdr);
781 * \brief Hash function for containers of CDRs indexing by Party A uniqueid */
782 static int cdr_object_channel_hash_fn(const void *obj, const int flags)
784 const struct cdr_object *cdr;
787 switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
799 return ast_str_case_hash(key);
803 * \brief Comparison function for containers of CDRs indexing by Party A uniqueid
805 static int cdr_object_channel_cmp_fn(void *obj, void *arg, int flags)
807 struct cdr_object *left = obj;
808 struct cdr_object *right = arg;
809 const char *right_key = arg;
812 switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
814 right_key = right->uniqueid;
817 cmp = strcmp(left->uniqueid, right_key);
819 case OBJ_PARTIAL_KEY:
821 * We could also use a partial key struct containing a length
822 * so strlen() does not get called for every comparison instead.
824 cmp = strncmp(left->uniqueid, right_key, strlen(right_key));
827 /* Sort can only work on something with a full or partial key. */
832 return cmp ? 0 : CMP_MATCH;
836 * \brief \ref cdr_object Destructor
838 static void cdr_object_dtor(void *obj)
840 struct cdr_object *cdr = obj;
841 struct ast_var_t *it_var;
843 ao2_cleanup(cdr->party_a.snapshot);
844 ao2_cleanup(cdr->party_b.snapshot);
845 while ((it_var = AST_LIST_REMOVE_HEAD(&cdr->party_a.variables, entries))) {
846 ast_var_delete(it_var);
848 while ((it_var = AST_LIST_REMOVE_HEAD(&cdr->party_b.variables, entries))) {
849 ast_var_delete(it_var);
851 ast_string_field_free_memory(cdr);
853 ao2_cleanup(cdr->next);
857 * \brief \ref cdr_object constructor
858 * \param chan The \ref ast_channel_snapshot that is the CDR's Party A
860 * This implicitly sets the state of the newly created CDR to the Single state
861 * (\ref single_state_fn_table)
863 static struct cdr_object *cdr_object_alloc(struct ast_channel_snapshot *chan)
865 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
866 struct cdr_object *cdr;
868 ast_assert(chan != NULL);
870 cdr = ao2_alloc(sizeof(*cdr), cdr_object_dtor);
875 if (ast_string_field_init(cdr, 64)) {
879 ast_string_field_set(cdr, uniqueid, chan->uniqueid);
880 ast_string_field_set(cdr, name, chan->name);
881 ast_string_field_set(cdr, linkedid, chan->linkedid);
882 cdr->disposition = AST_CDR_NULL;
883 cdr->sequence = ast_atomic_fetchadd_int(&global_cdr_sequence, +1);
885 cdr->party_a.snapshot = chan;
886 ao2_t_ref(cdr->party_a.snapshot, +1, "bump snapshot during CDR creation");
888 CDR_DEBUG(mod_cfg, "%p - Created CDR for channel %s\n", cdr, chan->name);
890 cdr_object_transition_state(cdr, &single_state_fn_table);
896 * \brief Create a new \ref cdr_object and append it to an existing chain
897 * \param cdr The \ref cdr_object to append to
899 static struct cdr_object *cdr_object_create_and_append(struct cdr_object *cdr)
901 struct cdr_object *new_cdr;
902 struct cdr_object *it_cdr;
903 struct cdr_object *cdr_last;
905 cdr_last = cdr->last;
906 new_cdr = cdr_object_alloc(cdr_last->party_a.snapshot);
910 new_cdr->disposition = AST_CDR_NULL;
912 /* Copy over the linkedid, as it may have changed */
913 ast_string_field_set(new_cdr, linkedid, cdr_last->linkedid);
914 ast_string_field_set(new_cdr, appl, cdr_last->appl);
915 ast_string_field_set(new_cdr, data, cdr_last->data);
917 /* Copy over other Party A information */
918 cdr_object_snapshot_copy(&new_cdr->party_a, &cdr_last->party_a);
920 /* Append the CDR to the end of the list */
921 for (it_cdr = cdr; it_cdr->next; it_cdr = it_cdr->next) {
922 it_cdr->last = new_cdr;
924 it_cdr->last = new_cdr;
925 it_cdr->next = new_cdr;
931 * \brief Return whether or not a channel has changed its state in the dialplan, subject
932 * to endbeforehexten logic
934 * \param old_snapshot The previous state
935 * \param new_snapshot The new state
937 * \retval 0 if the state has not changed
938 * \retval 1 if the state changed
940 static int snapshot_cep_changed(struct ast_channel_snapshot *old_snapshot,
941 struct ast_channel_snapshot *new_snapshot)
943 RAII_VAR(struct module_config *, mod_cfg,
944 ao2_global_obj_ref(module_configs), ao2_cleanup);
946 /* If we ignore hangup logic, don't indicate that we're executing anything new */
947 if (ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)
948 && ast_test_flag(&new_snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)) {
952 /* When Party A is originated to an application and the application exits, the stack
953 * will attempt to clear the application and restore the dummy originate application
954 * of "AppDialX". Ignore application changes to AppDialX as a result.
956 if (strcmp(new_snapshot->appl, old_snapshot->appl) && strncasecmp(new_snapshot->appl, "appdial", 7)
957 && (strcmp(new_snapshot->context, old_snapshot->context)
958 || strcmp(new_snapshot->exten, old_snapshot->exten)
959 || new_snapshot->priority != old_snapshot->priority)) {
967 * \brief Return whether or not a \ref ast_channel_snapshot is for a channel
968 * that was created as the result of a dial operation
970 * \retval 0 the channel was not created as the result of a dial
971 * \retval 1 the channel was created as the result of a dial
973 static int snapshot_is_dialed(struct ast_channel_snapshot *snapshot)
975 return (ast_test_flag(&snapshot->flags, AST_FLAG_OUTGOING)
976 && !(ast_test_flag(&snapshot->flags, AST_FLAG_ORIGINATED)));
980 * \brief Given two CDR snapshots, figure out who should be Party A for the
982 * \param left One of the snapshots
983 * \param right The other snapshot
984 * \retval The snapshot that won
986 static struct cdr_object_snapshot *cdr_object_pick_party_a(struct cdr_object_snapshot *left, struct cdr_object_snapshot *right)
988 /* Check whether or not the party is dialed. A dialed party is never the
989 * Party A with a party that was not dialed.
991 if (!snapshot_is_dialed(left->snapshot) && snapshot_is_dialed(right->snapshot)) {
993 } else if (snapshot_is_dialed(left->snapshot) && !snapshot_is_dialed(right->snapshot)) {
997 /* Try the Party A flag */
998 if (ast_test_flag(left, AST_CDR_FLAG_PARTY_A) && !ast_test_flag(right, AST_CDR_FLAG_PARTY_A)) {
1000 } else if (!ast_test_flag(right, AST_CDR_FLAG_PARTY_A) && ast_test_flag(right, AST_CDR_FLAG_PARTY_A)) {
1004 /* Neither party is dialed and neither has the Party A flag - defer to
1006 if (left->snapshot->creationtime.tv_sec < right->snapshot->creationtime.tv_sec) {
1008 } else if (left->snapshot->creationtime.tv_sec > right->snapshot->creationtime.tv_sec) {
1010 } else if (left->snapshot->creationtime.tv_usec > right->snapshot->creationtime.tv_usec) {
1013 /* Okay, fine, take the left one */
1019 * Compute the duration for a \ref cdr_object
1021 static long cdr_object_get_duration(struct cdr_object *cdr)
1023 return (long)(ast_tvdiff_ms(ast_tvzero(cdr->end) ? ast_tvnow() : cdr->end, cdr->start) / 1000);
1027 * \brief Compute the billsec for a \ref cdr_object
1029 static long cdr_object_get_billsec(struct cdr_object *cdr)
1031 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1034 if (ast_tvzero(cdr->answer)) {
1037 ms = ast_tvdiff_ms(ast_tvzero(cdr->end) ? ast_tvnow() : cdr->end, cdr->answer);
1038 if (ast_test_flag(&mod_cfg->general->settings, CDR_INITIATED_SECONDS)
1039 && (ms % 1000 >= 500)) {
1040 ms = (ms / 1000) + 1;
1050 * \brief Set a variable on a CDR object
1052 * \param headp The header pointer to the variable to set
1053 * \param name The name of the variable
1054 * \param value The value of the variable
1056 static void set_variable(struct varshead *headp, const char *name, const char *value)
1058 struct ast_var_t *newvariable;
1060 AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
1061 if (!strcasecmp(ast_var_name(newvariable), name)) {
1062 AST_LIST_REMOVE_CURRENT(entries);
1063 ast_var_delete(newvariable);
1067 AST_LIST_TRAVERSE_SAFE_END;
1069 if (value && (newvariable = ast_var_assign(name, value))) {
1070 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
1075 * \brief Create a chain of \ref ast_cdr objects from a chain of \ref cdr_object
1076 * suitable for consumption by the registered CDR backends
1077 * \param cdr The \ref cdr_object to convert to a public record
1078 * \retval A chain of \ref ast_cdr objects on success
1079 * \retval NULL on failure
1081 static struct ast_cdr *cdr_object_create_public_records(struct cdr_object *cdr)
1083 struct ast_cdr *pub_cdr = NULL, *cdr_prev = NULL;
1084 struct cdr_object *it_cdr;
1085 struct ast_var_t *it_var, *it_copy_var;
1086 struct ast_channel_snapshot *party_a;
1087 struct ast_channel_snapshot *party_b;
1088 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1090 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
1091 struct ast_cdr *cdr_copy;
1093 /* Don't create records for CDRs where the party A was a dialed channel */
1094 if (snapshot_is_dialed(it_cdr->party_a.snapshot) && !it_cdr->party_b.snapshot) {
1095 ast_debug(1, "CDR for %s is dialed and has no Party B; discarding\n",
1096 it_cdr->party_a.snapshot->name);
1100 cdr_copy = ast_calloc(1, sizeof(*cdr_copy));
1106 party_a = it_cdr->party_a.snapshot;
1107 party_b = it_cdr->party_b.snapshot;
1110 ast_assert(party_a != NULL);
1111 ast_copy_string(cdr_copy->accountcode, party_a->accountcode, sizeof(cdr_copy->accountcode));
1112 cdr_copy->amaflags = party_a->amaflags;
1113 ast_copy_string(cdr_copy->channel, party_a->name, sizeof(cdr_copy->channel));
1114 ast_callerid_merge(cdr_copy->clid, sizeof(cdr_copy->clid), party_a->caller_name, party_a->caller_number, "");
1115 ast_copy_string(cdr_copy->src, party_a->caller_number, sizeof(cdr_copy->src));
1116 ast_copy_string(cdr_copy->uniqueid, party_a->uniqueid, sizeof(cdr_copy->uniqueid));
1117 ast_copy_string(cdr_copy->lastapp, it_cdr->appl, sizeof(cdr_copy->lastapp));
1118 ast_copy_string(cdr_copy->lastdata, it_cdr->data, sizeof(cdr_copy->lastdata));
1119 ast_copy_string(cdr_copy->dst, party_a->exten, sizeof(cdr_copy->dst));
1120 ast_copy_string(cdr_copy->dcontext, party_a->context, sizeof(cdr_copy->dcontext));
1124 ast_copy_string(cdr_copy->dstchannel, party_b->name, sizeof(cdr_copy->dstchannel));
1125 ast_copy_string(cdr_copy->peeraccount, party_b->accountcode, sizeof(cdr_copy->peeraccount));
1126 if (!ast_strlen_zero(it_cdr->party_b.userfield)) {
1127 snprintf(cdr_copy->userfield, sizeof(cdr_copy->userfield), "%s;%s", it_cdr->party_a.userfield, it_cdr->party_b.userfield);
1130 if (ast_strlen_zero(cdr_copy->userfield) && !ast_strlen_zero(it_cdr->party_a.userfield)) {
1131 ast_copy_string(cdr_copy->userfield, it_cdr->party_a.userfield, sizeof(cdr_copy->userfield));
1134 /* Timestamps/durations */
1135 cdr_copy->start = it_cdr->start;
1136 cdr_copy->answer = it_cdr->answer;
1137 cdr_copy->end = it_cdr->end;
1138 cdr_copy->billsec = cdr_object_get_billsec(it_cdr);
1139 cdr_copy->duration = cdr_object_get_duration(it_cdr);
1142 ast_copy_flags(cdr_copy, &it_cdr->flags, AST_FLAGS_ALL);
1143 ast_copy_string(cdr_copy->linkedid, it_cdr->linkedid, sizeof(cdr_copy->linkedid));
1144 cdr_copy->disposition = it_cdr->disposition;
1145 cdr_copy->sequence = it_cdr->sequence;
1148 copy_variables(&cdr_copy->varshead, &it_cdr->party_a.variables);
1149 AST_LIST_TRAVERSE(&it_cdr->party_b.variables, it_var, entries) {
1151 struct ast_var_t *newvariable;
1152 AST_LIST_TRAVERSE(&cdr_copy->varshead, it_copy_var, entries) {
1153 if (!strcasecmp(ast_var_name(it_var), ast_var_name(it_copy_var))) {
1158 if (!found && (newvariable = ast_var_assign(ast_var_name(it_var), ast_var_value(it_var)))) {
1159 AST_LIST_INSERT_TAIL(&cdr_copy->varshead, newvariable, entries);
1167 cdr_prev->next = cdr_copy;
1168 cdr_prev = cdr_copy;
1176 * \brief Dispatch a CDR.
1177 * \param cdr The \ref cdr_object to dispatch
1179 * This will create a \ref ast_cdr object and publish it to the various backends
1181 static void cdr_object_dispatch(struct cdr_object *cdr)
1183 RAII_VAR(struct module_config *, mod_cfg,
1184 ao2_global_obj_ref(module_configs), ao2_cleanup);
1185 struct ast_cdr *pub_cdr;
1187 CDR_DEBUG(mod_cfg, "%p - Dispatching CDR for Party A %s, Party B %s\n", cdr,
1188 cdr->party_a.snapshot->name,
1189 cdr->party_b.snapshot ? cdr->party_b.snapshot->name : "<none>");
1190 pub_cdr = cdr_object_create_public_records(cdr);
1191 cdr_detach(pub_cdr);
1195 * \brief Set the disposition on a \ref cdr_object based on a hangupcause code
1196 * \param cdr The \ref cdr_object
1197 * \param hangupcause The Asterisk hangup cause code
1199 static void cdr_object_set_disposition(struct cdr_object *cdr, int hangupcause)
1201 RAII_VAR(struct module_config *, mod_cfg,
1202 ao2_global_obj_ref(module_configs), ao2_cleanup);
1204 /* Change the disposition based on the hang up cause */
1205 switch (hangupcause) {
1206 case AST_CAUSE_BUSY:
1207 cdr->disposition = AST_CDR_BUSY;
1209 case AST_CAUSE_CONGESTION:
1210 if (!ast_test_flag(&mod_cfg->general->settings, CDR_CONGESTION)) {
1211 cdr->disposition = AST_CDR_FAILED;
1213 cdr->disposition = AST_CDR_CONGESTION;
1216 case AST_CAUSE_NO_ROUTE_DESTINATION:
1217 case AST_CAUSE_UNREGISTERED:
1218 cdr->disposition = AST_CDR_FAILED;
1220 case AST_CAUSE_NORMAL_CLEARING:
1221 case AST_CAUSE_NO_ANSWER:
1222 cdr->disposition = AST_CDR_NOANSWER;
1230 * \brief Finalize a CDR.
1232 * This function is safe to call multiple times. Note that you can call this
1233 * explicitly before going to the finalized state if there's a chance the CDR
1234 * will be re-activated, in which case the \ref cdr_object's end time should be
1235 * cleared. This function is implicitly called when a CDR transitions to the
1236 * finalized state and right before it is dispatched
1238 * \param cdr_object The CDR to finalize
1240 static void cdr_object_finalize(struct cdr_object *cdr)
1242 RAII_VAR(struct module_config *, mod_cfg,
1243 ao2_global_obj_ref(module_configs), ao2_cleanup);
1245 if (!ast_tvzero(cdr->end)) {
1248 cdr->end = ast_tvnow();
1250 if (cdr->disposition == AST_CDR_NULL) {
1251 if (!ast_tvzero(cdr->answer)) {
1252 cdr->disposition = AST_CDR_ANSWERED;
1253 } else if (cdr->party_a.snapshot->hangupcause) {
1254 cdr_object_set_disposition(cdr, cdr->party_a.snapshot->hangupcause);
1255 } else if (cdr->party_b.snapshot && cdr->party_b.snapshot->hangupcause) {
1256 cdr_object_set_disposition(cdr, cdr->party_b.snapshot->hangupcause);
1258 cdr->disposition = AST_CDR_FAILED;
1262 /* tv_usec is suseconds_t, which could be int or long */
1263 ast_debug(1, "Finalized CDR for %s - start %ld.%06ld answer %ld.%06ld end %ld.%06ld dispo %s\n",
1264 cdr->party_a.snapshot->name,
1266 (long)cdr->start.tv_usec,
1268 (long)cdr->answer.tv_usec,
1270 (long)cdr->end.tv_usec,
1271 ast_cdr_disp2str(cdr->disposition));
1275 * \brief Check to see if a CDR needs to move to the finalized state because
1276 * its Party A hungup.
1278 static void cdr_object_check_party_a_hangup(struct cdr_object *cdr)
1280 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1282 if (ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)
1283 && ast_test_flag(&cdr->party_a.snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)) {
1284 cdr_object_finalize(cdr);
1287 if (ast_test_flag(&cdr->party_a.snapshot->flags, AST_FLAG_DEAD)
1288 && cdr->fn_table != &finalized_state_fn_table) {
1289 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1294 * \brief Check to see if a CDR needs to be answered based on its Party A.
1295 * Note that this is safe to call as much as you want - we won't answer twice
1297 static void cdr_object_check_party_a_answer(struct cdr_object *cdr) {
1298 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1300 if (cdr->party_a.snapshot->state == AST_STATE_UP && ast_tvzero(cdr->answer)) {
1301 cdr->answer = ast_tvnow();
1302 /* tv_usec is suseconds_t, which could be int or long */
1303 CDR_DEBUG(mod_cfg, "%p - Set answered time to %ld.%06ld\n", cdr,
1305 (long)cdr->answer.tv_usec);
1309 /* \brief Set Caller ID information on a CDR */
1310 static void cdr_object_update_cid(struct cdr_object_snapshot *old_snapshot, struct ast_channel_snapshot *new_snapshot)
1312 if (!old_snapshot->snapshot) {
1313 set_variable(&old_snapshot->variables, "dnid", new_snapshot->caller_dnid);
1314 set_variable(&old_snapshot->variables, "callingsubaddr", new_snapshot->caller_subaddr);
1315 set_variable(&old_snapshot->variables, "calledsubaddr", new_snapshot->dialed_subaddr);
1318 if (!strcmp(old_snapshot->snapshot->caller_dnid, new_snapshot->caller_dnid)) {
1319 set_variable(&old_snapshot->variables, "dnid", new_snapshot->caller_dnid);
1321 if (!strcmp(old_snapshot->snapshot->caller_subaddr, new_snapshot->caller_subaddr)) {
1322 set_variable(&old_snapshot->variables, "callingsubaddr", new_snapshot->caller_subaddr);
1324 if (!strcmp(old_snapshot->snapshot->dialed_subaddr, new_snapshot->dialed_subaddr)) {
1325 set_variable(&old_snapshot->variables, "calledsubaddr", new_snapshot->dialed_subaddr);
1330 * \brief Swap an old \ref cdr_object_snapshot's \ref ast_channel_snapshot for
1331 * a new \ref ast_channel_snapshot
1332 * \param old_snapshot The old \ref cdr_object_snapshot
1333 * \param new_snapshot The new \ref ast_channel_snapshot for old_snapshot
1335 static void cdr_object_swap_snapshot(struct cdr_object_snapshot *old_snapshot,
1336 struct ast_channel_snapshot *new_snapshot)
1338 cdr_object_update_cid(old_snapshot, new_snapshot);
1339 if (old_snapshot->snapshot) {
1340 ao2_t_ref(old_snapshot->snapshot, -1, "Drop ref for swap");
1342 ao2_t_ref(new_snapshot, +1, "Bump ref for swap");
1343 old_snapshot->snapshot = new_snapshot;
1346 /* BASE METHOD IMPLEMENTATIONS */
1348 static int base_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1350 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1352 ast_assert(strcasecmp(snapshot->name, cdr->party_a.snapshot->name) == 0);
1354 /* Ignore any snapshots from a dead or dying channel */
1355 if (ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)
1356 && ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)) {
1357 cdr_object_check_party_a_hangup(cdr);
1360 cdr_object_swap_snapshot(&cdr->party_a, snapshot);
1362 /* When Party A is originated to an application and the application exits, the stack
1363 * will attempt to clear the application and restore the dummy originate application
1364 * of "AppDialX". Prevent that, and any other application changes we might not want
1367 if (!ast_strlen_zero(snapshot->appl)
1368 && (strncasecmp(snapshot->appl, "appdial", 7) || ast_strlen_zero(cdr->appl))
1369 && !ast_test_flag(&cdr->flags, AST_CDR_LOCK_APP)) {
1370 ast_string_field_set(cdr, appl, snapshot->appl);
1371 ast_string_field_set(cdr, data, snapshot->data);
1373 /* Dial (app_dial) is a special case. Because pre-dial handlers, which
1374 * execute before the dial begins, will alter the application/data to
1375 * something people typically don't want to see, if we see a channel enter
1376 * into Dial here, we set the appl/data accordingly and lock it.
1378 if (!strcmp(snapshot->appl, "Dial")) {
1379 ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP);
1383 ast_string_field_set(cdr, linkedid, snapshot->linkedid);
1384 cdr_object_check_party_a_answer(cdr);
1385 cdr_object_check_party_a_hangup(cdr);
1390 static int base_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1392 /* In general, most things shouldn't get a bridge leave */
1397 static int base_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status)
1399 /* In general, most things shouldn't get a dial end. */
1404 static enum process_bridge_enter_results base_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1406 /* Base process bridge enter simply indicates that we can't handle it */
1407 return BRIDGE_ENTER_NEED_CDR;
1410 static int base_process_parked_channel(struct cdr_object *cdr, struct ast_parked_call_payload *parking_info)
1412 char park_info[128];
1414 ast_assert(!strcasecmp(parking_info->parkee->name, cdr->party_a.snapshot->name));
1416 /* Update Party A information regardless */
1417 cdr->fn_table->process_party_a(cdr, parking_info->parkee);
1419 /* Fake out where we're parked */
1420 ast_string_field_set(cdr, appl, "Park");
1421 snprintf(park_info, sizeof(park_info), "%s:%u", parking_info->parkinglot, parking_info->parkingspace);
1422 ast_string_field_set(cdr, data, park_info);
1424 /* Prevent any further changes to the App/Data fields for this record */
1425 ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP);
1432 static void single_state_init_function(struct cdr_object *cdr) {
1433 cdr->start = ast_tvnow();
1434 cdr_object_check_party_a_answer(cdr);
1437 static void single_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1439 /* This should never happen! */
1440 ast_assert(cdr->party_b.snapshot == NULL);
1445 static int single_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer)
1447 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1449 if (caller && !strcasecmp(cdr->party_a.snapshot->name, caller->name)) {
1450 base_process_party_a(cdr, caller);
1451 CDR_DEBUG(mod_cfg, "%p - Updated Party A %s snapshot\n", cdr,
1452 cdr->party_a.snapshot->name);
1453 cdr_object_swap_snapshot(&cdr->party_b, peer);
1454 CDR_DEBUG(mod_cfg, "%p - Updated Party B %s snapshot\n", cdr,
1455 cdr->party_b.snapshot->name);
1457 /* If we have two parties, lock the application that caused the
1458 * two parties to be associated. This prevents mid-call event
1459 * macros/gosubs from perturbing the CDR application/data
1461 ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP);
1462 } else if (!strcasecmp(cdr->party_a.snapshot->name, peer->name)) {
1463 /* We're the entity being dialed, i.e., outbound origination */
1464 base_process_party_a(cdr, peer);
1465 CDR_DEBUG(mod_cfg, "%p - Updated Party A %s snapshot\n", cdr,
1466 cdr->party_a.snapshot->name);
1469 cdr_object_transition_state(cdr, &dial_state_fn_table);
1474 * \brief Handle a comparison between our \ref cdr_object and a \ref cdr_object
1475 * already in the bridge while in the Single state. The goal of this is to find
1476 * a Party B for our CDR.
1478 * \param cdr Our \ref cdr_object in the Single state
1479 * \param cand_cdr The \ref cdr_object already in the Bridge state
1481 * \retval 0 The cand_cdr had a Party A or Party B that we could use as our
1483 * \retval 1 No party in the cand_cdr could be used as our Party B
1485 static int single_state_bridge_enter_comparison(struct cdr_object *cdr,
1486 struct cdr_object *cand_cdr)
1488 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1489 struct cdr_object_snapshot *party_a;
1491 /* Don't match on ourselves */
1492 if (!strcasecmp(cdr->party_a.snapshot->name, cand_cdr->party_a.snapshot->name)) {
1496 /* Try the candidate CDR's Party A first */
1497 party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_a);
1498 if (!strcasecmp(party_a->snapshot->name, cdr->party_a.snapshot->name)) {
1499 CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
1500 cdr, cdr->party_a.snapshot->name, cand_cdr->party_a.snapshot->name);
1501 cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_a);
1502 if (!cand_cdr->party_b.snapshot) {
1503 /* We just stole them - finalize their CDR. Note that this won't
1504 * transition their state, it just sets the end time and the
1505 * disposition - if we need to re-activate them later, we can.
1507 cdr_object_finalize(cand_cdr);
1512 /* Try their Party B, unless it's us */
1513 if (!cand_cdr->party_b.snapshot
1514 || !strcasecmp(cdr->party_a.snapshot->name, cand_cdr->party_b.snapshot->name)) {
1517 party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_b);
1518 if (!strcasecmp(party_a->snapshot->name, cdr->party_a.snapshot->name)) {
1519 CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
1520 cdr, cdr->party_a.snapshot->name, cand_cdr->party_b.snapshot->name);
1521 cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_b);
1528 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)
1530 struct ao2_iterator it_cdrs;
1534 ast_string_field_set(cdr, bridge, bridge->uniqueid);
1536 if (ao2_container_count(bridge->channels) == 1) {
1537 /* No one in the bridge yet but us! */
1538 cdr_object_transition_state(cdr, &bridge_state_fn_table);
1539 return BRIDGE_ENTER_ONLY_PARTY;
1542 for (it_cdrs = ao2_iterator_init(bridge->channels, 0);
1543 !success && (channel_id = ao2_iterator_next(&it_cdrs));
1544 ao2_ref(channel_id, -1)) {
1545 RAII_VAR(struct cdr_object *, cand_cdr_master,
1546 ao2_find(active_cdrs_by_channel, channel_id, OBJ_KEY),
1548 struct cdr_object *cand_cdr;
1550 if (!cand_cdr_master) {
1554 ao2_lock(cand_cdr_master);
1555 for (cand_cdr = cand_cdr_master; cand_cdr; cand_cdr = cand_cdr->next) {
1556 /* Skip any records that are not in a bridge or in this bridge.
1557 * I'm not sure how that would happen, but it pays to be careful. */
1558 if (cand_cdr->fn_table != &bridge_state_fn_table ||
1559 strcmp(cdr->bridge, cand_cdr->bridge)) {
1563 if (single_state_bridge_enter_comparison(cdr, cand_cdr)) {
1566 /* We successfully got a party B - break out */
1570 ao2_unlock(cand_cdr_master);
1572 ao2_iterator_destroy(&it_cdrs);
1574 /* We always transition state, even if we didn't get a peer */
1575 cdr_object_transition_state(cdr, &bridge_state_fn_table);
1577 /* Success implies that we have a Party B */
1579 return BRIDGE_ENTER_OBTAINED_PARTY_B;
1582 return BRIDGE_ENTER_NO_PARTY_B;
1585 static int single_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1587 cdr_object_transition_state(cdr, &parked_state_fn_table);
1594 static void dial_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1596 ast_assert(snapshot != NULL);
1598 if (!cdr->party_b.snapshot
1599 || strcasecmp(cdr->party_b.snapshot->name, snapshot->name)) {
1602 cdr_object_swap_snapshot(&cdr->party_b, snapshot);
1604 /* If party B hangs up, finalize this CDR */
1605 if (ast_test_flag(&cdr->party_b.snapshot->flags, AST_FLAG_DEAD)) {
1606 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1610 static int dial_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer)
1612 /* Don't process a begin dial here. A party A already in the dial state will
1613 * who receives a dial begin for something else will be handled by the
1614 * message router callback and will add a new CDR for the party A */
1620 * \brief Convert a dial status to a CDR disposition
1622 static enum ast_cdr_disposition dial_status_to_disposition(const char *dial_status)
1624 RAII_VAR(struct module_config *, mod_cfg,
1625 ao2_global_obj_ref(module_configs), ao2_cleanup);
1627 if (!strcmp(dial_status, "ANSWER")) {
1628 return AST_CDR_ANSWERED;
1629 } else if (!strcmp(dial_status, "BUSY")) {
1630 return AST_CDR_BUSY;
1631 } else if (!strcmp(dial_status, "CANCEL") || !strcmp(dial_status, "NOANSWER")) {
1632 return AST_CDR_NOANSWER;
1633 } else if (!strcmp(dial_status, "CONGESTION")) {
1634 if (!ast_test_flag(&mod_cfg->general->settings, CDR_CONGESTION)) {
1635 return AST_CDR_FAILED;
1637 return AST_CDR_CONGESTION;
1639 } else if (!strcmp(dial_status, "FAILED")) {
1640 return AST_CDR_FAILED;
1642 return AST_CDR_FAILED;
1645 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)
1647 RAII_VAR(struct module_config *, mod_cfg,
1648 ao2_global_obj_ref(module_configs), ao2_cleanup);
1649 struct ast_channel_snapshot *party_a;
1656 ast_assert(!strcasecmp(cdr->party_a.snapshot->name, party_a->name));
1657 cdr_object_swap_snapshot(&cdr->party_a, party_a);
1659 if (cdr->party_b.snapshot) {
1660 if (strcasecmp(cdr->party_b.snapshot->name, peer->name)) {
1661 /* Not the status for this CDR - defer back to the message router */
1664 cdr_object_swap_snapshot(&cdr->party_b, peer);
1667 /* Set the disposition based on the dial string. */
1668 cdr->disposition = dial_status_to_disposition(dial_status);
1669 if (cdr->disposition == AST_CDR_ANSWERED) {
1670 /* Switch to dial pending to wait and see what the caller does */
1671 cdr_object_transition_state(cdr, &dialed_pending_state_fn_table);
1673 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1679 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)
1681 struct ao2_iterator it_cdrs;
1685 ast_string_field_set(cdr, bridge, bridge->uniqueid);
1687 /* Get parties in the bridge */
1688 if (ao2_container_count(bridge->channels) == 1) {
1689 /* No one in the bridge yet but us! */
1690 cdr_object_transition_state(cdr, &bridge_state_fn_table);
1691 return BRIDGE_ENTER_ONLY_PARTY;
1694 for (it_cdrs = ao2_iterator_init(bridge->channels, 0);
1695 !success && (channel_id = ao2_iterator_next(&it_cdrs));
1696 ao2_ref(channel_id, -1)) {
1697 RAII_VAR(struct cdr_object *, cand_cdr_master,
1698 ao2_find(active_cdrs_by_channel, channel_id, OBJ_KEY),
1700 struct cdr_object *cand_cdr;
1702 if (!cand_cdr_master) {
1706 ao2_lock(cand_cdr_master);
1707 for (cand_cdr = cand_cdr_master; cand_cdr; cand_cdr = cand_cdr->next) {
1708 /* Skip any records that are not in a bridge or in this bridge.
1709 * I'm not sure how that would happen, but it pays to be careful. */
1710 if (cand_cdr->fn_table != &bridge_state_fn_table ||
1711 strcmp(cdr->bridge, cand_cdr->bridge)) {
1715 /* If we don't have a Party B (originated channel), skip it */
1716 if (!cdr->party_b.snapshot) {
1720 /* Skip any records that aren't our Party B */
1721 if (strcasecmp(cdr->party_b.snapshot->name, cand_cdr->party_a.snapshot->name)) {
1724 cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_a);
1725 /* If they have a Party B, they joined up with someone else as their
1726 * Party A. Don't finalize them as they're active. Otherwise, we
1727 * have stolen them so they need to be finalized.
1729 if (!cand_cdr->party_b.snapshot) {
1730 cdr_object_finalize(cand_cdr);
1735 ao2_unlock(cand_cdr_master);
1737 ao2_iterator_destroy(&it_cdrs);
1739 /* We always transition state, even if we didn't get a peer */
1740 cdr_object_transition_state(cdr, &bridge_state_fn_table);
1742 /* Success implies that we have a Party B */
1744 return BRIDGE_ENTER_OBTAINED_PARTY_B;
1746 return BRIDGE_ENTER_NO_PARTY_B;
1749 /* DIALED PENDING STATE */
1751 static int dialed_pending_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1753 /* If we get a CEP change, we're executing dialplan. If we have a Party B
1754 * that means we need a new CDR; otherwise, switch us over to single.
1756 if (snapshot_cep_changed(cdr->party_a.snapshot, snapshot)) {
1757 if (cdr->party_b.snapshot) {
1758 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1759 cdr->fn_table->process_party_a(cdr, snapshot);
1762 cdr_object_transition_state(cdr, &single_state_fn_table);
1763 cdr->fn_table->process_party_a(cdr, snapshot);
1767 base_process_party_a(cdr, snapshot);
1771 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)
1773 cdr_object_transition_state(cdr, &dial_state_fn_table);
1774 return cdr->fn_table->process_bridge_enter(cdr, bridge, channel);
1777 static int dialed_pending_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1779 if (cdr->party_b.snapshot) {
1780 /* We can't handle this as we have a Party B - ask for a new one */
1783 cdr_object_transition_state(cdr, &parked_state_fn_table);
1787 static int dialed_pending_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer)
1789 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1791 /* Ask for a new CDR */
1797 static void bridge_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1799 if (!cdr->party_b.snapshot
1800 || strcasecmp(cdr->party_b.snapshot->name, snapshot->name)) {
1803 cdr_object_swap_snapshot(&cdr->party_b, snapshot);
1805 /* If party B hangs up, finalize this CDR */
1806 if (ast_test_flag(&cdr->party_b.snapshot->flags, AST_FLAG_DEAD)) {
1807 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1811 static int bridge_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1813 if (strcmp(cdr->bridge, bridge->uniqueid)) {
1816 if (strcasecmp(cdr->party_a.snapshot->name, channel->name)
1817 && cdr->party_b.snapshot
1818 && strcasecmp(cdr->party_b.snapshot->name, channel->name)) {
1821 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1828 static int parked_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1830 if (strcasecmp(cdr->party_a.snapshot->name, channel->name)) {
1833 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1838 /* FINALIZED STATE */
1840 static void finalized_state_init_function(struct cdr_object *cdr)
1842 cdr_object_finalize(cdr);
1845 static int finalized_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1847 RAII_VAR(struct module_config *, mod_cfg,
1848 ao2_global_obj_ref(module_configs), ao2_cleanup);
1850 if (ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)
1851 && ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)) {
1855 /* Indicate that, if possible, we should get a new CDR */
1861 * \brief Filter channel snapshots by technology
1863 static int filter_channel_snapshot(struct ast_channel_snapshot *snapshot)
1865 return snapshot->tech_properties & AST_CHAN_TP_INTERNAL;
1870 * \brief Filter a channel cache update
1872 static int filter_channel_cache_message(struct ast_channel_snapshot *old_snapshot,
1873 struct ast_channel_snapshot *new_snapshot)
1877 /* Drop cache updates from certain channel technologies */
1879 ret |= filter_channel_snapshot(old_snapshot);
1882 ret |= filter_channel_snapshot(new_snapshot);
1888 /* TOPIC ROUTER CALLBACKS */
1891 * \brief Handler for Stasis-Core dial messages
1892 * \param data Passed on
1893 * \param sub The stasis subscription for this message callback
1894 * \param topic The topic this message was published for
1895 * \param message The message
1897 static void handle_dial_message(void *data, struct stasis_subscription *sub, struct stasis_message *message)
1899 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1900 RAII_VAR(struct cdr_object *, cdr, NULL, ao2_cleanup);
1901 struct ast_multi_channel_blob *payload = stasis_message_data(message);
1902 struct ast_channel_snapshot *caller;
1903 struct ast_channel_snapshot *peer;
1904 struct cdr_object *it_cdr;
1905 struct ast_json *dial_status_blob;
1906 const char *dial_status = NULL;
1909 caller = ast_multi_channel_blob_get_channel(payload, "caller");
1910 peer = ast_multi_channel_blob_get_channel(payload, "peer");
1911 if (!peer && !caller) {
1914 dial_status_blob = ast_json_object_get(ast_multi_channel_blob_get_json(payload), "dialstatus");
1915 if (dial_status_blob) {
1916 dial_status = ast_json_string_get(dial_status_blob);
1919 CDR_DEBUG(mod_cfg, "Dial %s message for %s, %s: %u.%08u\n",
1920 ast_strlen_zero(dial_status) ? "Begin" : "End",
1921 caller ? caller->name : "(none)",
1922 peer ? peer->name : "(none)",
1923 (unsigned int)stasis_message_timestamp(message)->tv_sec,
1924 (unsigned int)stasis_message_timestamp(message)->tv_usec);
1926 if (filter_channel_snapshot(peer) || (caller && filter_channel_snapshot(caller))) {
1930 /* Figure out who is running this show */
1932 cdr = ao2_find(active_cdrs_by_channel, caller->uniqueid, OBJ_KEY);
1934 cdr = ao2_find(active_cdrs_by_channel, peer->uniqueid, OBJ_KEY);
1938 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", caller ? caller->name : peer->name);
1944 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
1945 if (ast_strlen_zero(dial_status)) {
1946 if (!it_cdr->fn_table->process_dial_begin) {
1949 CDR_DEBUG(mod_cfg, "%p - Processing Dial Begin message for channel %s, peer %s\n",
1951 caller ? caller->name : "(none)",
1952 peer ? peer->name : "(none)");
1953 res &= it_cdr->fn_table->process_dial_begin(it_cdr,
1957 if (!it_cdr->fn_table->process_dial_end) {
1960 CDR_DEBUG(mod_cfg, "%p - Processing Dial End message for channel %s, peer %s\n",
1962 caller ? caller->name : "(none)",
1963 peer ? peer->name : "(none)");
1964 it_cdr->fn_table->process_dial_end(it_cdr,
1971 /* If no CDR handled a dial begin message, make a new one */
1972 if (res && ast_strlen_zero(dial_status)) {
1973 struct cdr_object *new_cdr;
1975 new_cdr = cdr_object_create_and_append(cdr);
1980 new_cdr->fn_table->process_dial_begin(new_cdr,
1987 static int cdr_object_finalize_party_b(void *obj, void *arg, int flags)
1989 struct cdr_object *cdr = obj;
1990 struct ast_channel_snapshot *party_b = arg;
1991 struct cdr_object *it_cdr;
1992 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
1993 if (it_cdr->party_b.snapshot
1994 && !strcasecmp(it_cdr->party_b.snapshot->name, party_b->name)) {
1995 /* Don't transition to the finalized state - let the Party A do
1996 * that when its ready
1998 cdr_object_finalize(it_cdr);
2004 static int cdr_object_update_party_b(void *obj, void *arg, int flags)
2006 struct cdr_object *cdr = obj;
2007 struct ast_channel_snapshot *party_b = arg;
2008 struct cdr_object *it_cdr;
2009 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2010 if (!it_cdr->fn_table->process_party_b) {
2013 if (it_cdr->party_b.snapshot
2014 && !strcasecmp(it_cdr->party_b.snapshot->name, party_b->name)) {
2015 it_cdr->fn_table->process_party_b(it_cdr, party_b);
2021 /*! \brief Determine if we need to add a new CDR based on snapshots */
2022 static int check_new_cdr_needed(struct ast_channel_snapshot *old_snapshot,
2023 struct ast_channel_snapshot *new_snapshot)
2025 RAII_VAR(struct module_config *, mod_cfg,
2026 ao2_global_obj_ref(module_configs), ao2_cleanup);
2028 /* If we're dead, we don't need a new CDR */
2030 || (ast_test_flag(&new_snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)
2031 && ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN))) {
2035 /* Auto-fall through will increment the priority but have no application */
2036 if (ast_strlen_zero(new_snapshot->appl)) {
2040 if (old_snapshot && !snapshot_cep_changed(old_snapshot, new_snapshot)) {
2048 * \brief Handler for Stasis-Core channel cache update messages
2049 * \param data Passed on
2050 * \param sub The stasis subscription for this message callback
2051 * \param topic The topic this message was published for
2052 * \param message The message
2054 static void handle_channel_cache_message(void *data, struct stasis_subscription *sub, struct stasis_message *message)
2056 RAII_VAR(struct cdr_object *, cdr, NULL, ao2_cleanup);
2057 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
2058 struct stasis_cache_update *update = stasis_message_data(message);
2059 struct ast_channel_snapshot *old_snapshot;
2060 struct ast_channel_snapshot *new_snapshot;
2061 const char *uniqueid;
2063 struct cdr_object *it_cdr;
2065 ast_assert(update != NULL);
2066 ast_assert(ast_channel_snapshot_type() == update->type);
2068 old_snapshot = stasis_message_data(update->old_snapshot);
2069 new_snapshot = stasis_message_data(update->new_snapshot);
2070 uniqueid = new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid;
2071 name = new_snapshot ? new_snapshot->name : old_snapshot->name;
2073 if (filter_channel_cache_message(old_snapshot, new_snapshot)) {
2077 if (new_snapshot && !old_snapshot) {
2078 cdr = cdr_object_alloc(new_snapshot);
2082 ao2_link(active_cdrs_by_channel, cdr);
2085 /* Handle Party A */
2087 cdr = ao2_find(active_cdrs_by_channel, uniqueid, OBJ_KEY);
2090 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", name);
2096 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2097 if (!it_cdr->fn_table->process_party_a) {
2100 all_reject &= it_cdr->fn_table->process_party_a(it_cdr, new_snapshot);
2102 if (all_reject && check_new_cdr_needed(old_snapshot, new_snapshot)) {
2103 /* We're not hung up and we have a new snapshot - we need a new CDR */
2104 struct cdr_object *new_cdr;
2105 new_cdr = cdr_object_create_and_append(cdr);
2107 new_cdr->fn_table->process_party_a(new_cdr, new_snapshot);
2111 CDR_DEBUG(mod_cfg, "%p - Beginning finalize/dispatch for %s\n", cdr, old_snapshot->name);
2112 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2113 cdr_object_finalize(it_cdr);
2115 cdr_object_dispatch(cdr);
2116 ao2_unlink(active_cdrs_by_channel, cdr);
2121 /* Handle Party B */
2123 ao2_callback(active_cdrs_by_channel, OBJ_NODATA, cdr_object_update_party_b,
2126 ao2_callback(active_cdrs_by_channel, OBJ_NODATA, cdr_object_finalize_party_b,
2132 struct bridge_leave_data {
2133 struct ast_bridge_snapshot *bridge;
2134 struct ast_channel_snapshot *channel;
2137 /*! \brief Callback used to notify CDRs of a Party B leaving the bridge */
2138 static int cdr_object_party_b_left_bridge_cb(void *obj, void *arg, int flags)
2140 struct cdr_object *cdr = obj;
2141 struct bridge_leave_data *leave_data = arg;
2142 struct cdr_object *it_cdr;
2144 if (strcmp(cdr->bridge, leave_data->bridge->uniqueid)) {
2147 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2148 if (it_cdr->fn_table != &bridge_state_fn_table) {
2151 if (!it_cdr->party_b.snapshot) {
2154 if (strcasecmp(it_cdr->party_b.snapshot->name, leave_data->channel->name)) {
2157 /* It is our Party B, in our bridge. Set the end time and let the handler
2158 * transition our CDR appropriately when we leave the bridge.
2160 cdr_object_finalize(it_cdr);
2165 /*! \brief Filter bridge messages based on bridge technology */
2166 static int filter_bridge_messages(struct ast_bridge_snapshot *bridge)
2168 /* Ignore holding bridge technology messages. We treat this simply as an application
2169 * that a channel enters into.
2171 if (!strcmp(bridge->technology, "holding_bridge") && strcmp(bridge->subclass, "parking")) {
2178 * \brief Handler for when a channel leaves a bridge
2179 * \param data Passed on
2180 * \param sub The stasis subscription for this message callback
2181 * \param topic The topic this message was published for
2182 * \param message The message - hopefully a bridge one!
2184 static void handle_bridge_leave_message(void *data, struct stasis_subscription *sub,
2185 struct stasis_message *message)
2187 struct ast_bridge_blob *update = stasis_message_data(message);
2188 struct ast_bridge_snapshot *bridge = update->bridge;
2189 struct ast_channel_snapshot *channel = update->channel;
2190 RAII_VAR(struct module_config *, mod_cfg,
2191 ao2_global_obj_ref(module_configs), ao2_cleanup);
2192 RAII_VAR(struct cdr_object *, cdr,
2193 ao2_find(active_cdrs_by_channel, channel->uniqueid, OBJ_KEY),
2195 struct cdr_object *it_cdr;
2196 struct bridge_leave_data leave_data = {
2200 int left_bridge = 0;
2202 if (filter_bridge_messages(bridge)) {
2206 if (filter_channel_snapshot(channel)) {
2210 CDR_DEBUG(mod_cfg, "Bridge Leave message for %s: %u.%08u\n",
2212 (unsigned int)stasis_message_timestamp(message)->tv_sec,
2213 (unsigned int)stasis_message_timestamp(message)->tv_usec);
2216 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->name);
2223 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2224 if (!it_cdr->fn_table->process_bridge_leave) {
2227 CDR_DEBUG(mod_cfg, "%p - Processing Bridge Leave for %s\n",
2228 it_cdr, channel->name);
2229 if (!it_cdr->fn_table->process_bridge_leave(it_cdr, bridge, channel)) {
2230 ast_string_field_set(it_cdr, bridge, "");
2239 if (strcmp(bridge->subclass, "parking")) {
2241 ao2_callback(active_cdrs_by_channel, OBJ_NODATA,
2242 cdr_object_party_b_left_bridge_cb,
2249 * \brief Create a new CDR, append it to an existing CDR, and update its snapshots
2251 * \note The new CDR will be automatically transitioned to the bridge state
2253 static void bridge_candidate_add_to_cdr(struct cdr_object *cdr,
2254 struct cdr_object_snapshot *party_b)
2256 RAII_VAR(struct module_config *, mod_cfg,
2257 ao2_global_obj_ref(module_configs), ao2_cleanup);
2258 struct cdr_object *new_cdr;
2260 new_cdr = cdr_object_create_and_append(cdr);
2264 cdr_object_snapshot_copy(&new_cdr->party_b, party_b);
2265 cdr_object_check_party_a_answer(new_cdr);
2266 ast_string_field_set(new_cdr, bridge, cdr->bridge);
2267 cdr_object_transition_state(new_cdr, &bridge_state_fn_table);
2268 CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
2269 new_cdr, new_cdr->party_a.snapshot->name,
2270 party_b->snapshot->name);
2274 * \brief Process a single \ref bridge_candidate
2276 * When a CDR enters a bridge, it needs to make pairings with everyone else
2277 * that it is not currently paired with. This function determines, for the
2278 * CDR for the channel that entered the bridge and the CDR for every other
2279 * channel currently in the bridge, who is Party A and makes new CDRs.
2281 * \param cdr The \ref cdr_obj being processed
2282 * \param cand_cdr The \ref cdr_object that is a candidate
2285 static int bridge_candidate_process(struct cdr_object *cdr, struct cdr_object *base_cand_cdr)
2287 RAII_VAR(struct module_config *, mod_cfg,
2288 ao2_global_obj_ref(module_configs), ao2_cleanup);
2289 struct cdr_object_snapshot *party_a;
2290 struct cdr_object *cand_cdr;
2292 SCOPED_AO2LOCK(lock, base_cand_cdr);
2294 for (cand_cdr = base_cand_cdr; cand_cdr; cand_cdr = cand_cdr->next) {
2295 /* Skip any records that are not in this bridge */
2296 if (strcmp(cand_cdr->bridge, cdr->bridge)) {
2300 /* If the candidate is us or someone we've taken on, pass on by */
2301 if (!strcasecmp(cdr->party_a.snapshot->name, cand_cdr->party_a.snapshot->name)
2302 || (cdr->party_b.snapshot
2303 && !strcasecmp(cdr->party_b.snapshot->name, cand_cdr->party_a.snapshot->name))) {
2307 party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_a);
2308 /* We're party A - make a new CDR, append it to us, and set the candidate as
2310 if (!strcasecmp(party_a->snapshot->name, cdr->party_a.snapshot->name)) {
2311 bridge_candidate_add_to_cdr(cdr, &cand_cdr->party_a);
2315 /* We're Party B. Check if we can add ourselves immediately or if we need
2316 * a new CDR for them (they already have a Party B) */
2317 if (cand_cdr->party_b.snapshot
2318 && strcasecmp(cand_cdr->party_b.snapshot->name, cdr->party_a.snapshot->name)) {
2319 bridge_candidate_add_to_cdr(cand_cdr, &cdr->party_a);
2321 CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
2322 cand_cdr, cand_cdr->party_a.snapshot->name,
2323 cdr->party_a.snapshot->name);
2324 cdr_object_snapshot_copy(&cand_cdr->party_b, &cdr->party_a);
2325 /* It's possible that this joined at one point and was never chosen
2326 * as party A. Clear their end time, as it would be set in such a
2329 memset(&cand_cdr->end, 0, sizeof(cand_cdr->end));
2336 * \brief Handle creating bridge pairings for the \ref cdr_object that just
2338 * \param cdr The \ref cdr_object that just entered the bridge
2339 * \param bridge The \ref ast_bridge_snapshot representing the bridge it just entered
2341 static void handle_bridge_pairings(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge)
2343 struct ao2_iterator it_channels;
2346 it_channels = ao2_iterator_init(bridge->channels, 0);
2347 while ((channel_id = ao2_iterator_next(&it_channels))) {
2348 RAII_VAR(struct cdr_object *, cand_cdr,
2349 ao2_find(active_cdrs_by_channel, channel_id, OBJ_KEY),
2353 ao2_ref(channel_id, -1);
2357 bridge_candidate_process(cdr, cand_cdr);
2359 ao2_ref(channel_id, -1);
2361 ao2_iterator_destroy(&it_channels);
2364 /*! \brief Handle entering into a parking bridge
2365 * \param cdr The CDR to operate on
2366 * \param bridge The bridge the channel just entered
2367 * \param channel The channel snapshot
2369 static void handle_parking_bridge_enter_message(struct cdr_object *cdr,
2370 struct ast_bridge_snapshot *bridge,
2371 struct ast_channel_snapshot *channel)
2373 RAII_VAR(struct module_config *, mod_cfg,
2374 ao2_global_obj_ref(module_configs), ao2_cleanup);
2376 struct cdr_object *it_cdr;
2377 struct cdr_object *new_cdr;
2381 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2382 if (it_cdr->fn_table->process_parking_bridge_enter) {
2383 res &= it_cdr->fn_table->process_parking_bridge_enter(it_cdr, bridge, channel);
2385 if (it_cdr->fn_table->process_party_a) {
2386 CDR_DEBUG(mod_cfg, "%p - Updating Party A %s snapshot\n", it_cdr,
2388 it_cdr->fn_table->process_party_a(it_cdr, channel);
2393 /* No one handled it - we need a new one! */
2394 new_cdr = cdr_object_create_and_append(cdr);
2396 /* Let the single state transition us to Parked */
2397 cdr_object_transition_state(new_cdr, &single_state_fn_table);
2398 new_cdr->fn_table->process_parking_bridge_enter(new_cdr, bridge, channel);
2404 /*! \brief Handle a bridge enter message for a 'normal' bridge
2405 * \param cdr The CDR to operate on
2406 * \param bridge The bridge the channel just entered
2407 * \param channel The channel snapshot
2409 static void handle_standard_bridge_enter_message(struct cdr_object *cdr,
2410 struct ast_bridge_snapshot *bridge,
2411 struct ast_channel_snapshot *channel)
2413 RAII_VAR(struct module_config *, mod_cfg,
2414 ao2_global_obj_ref(module_configs), ao2_cleanup);
2415 enum process_bridge_enter_results result;
2416 struct cdr_object *it_cdr;
2417 struct cdr_object *new_cdr;
2418 struct cdr_object *handled_cdr = NULL;
2422 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2423 if (it_cdr->fn_table->process_party_a) {
2424 CDR_DEBUG(mod_cfg, "%p - Updating Party A %s snapshot\n", it_cdr,
2426 it_cdr->fn_table->process_party_a(it_cdr, channel);
2429 /* Notify all states that they have entered a bridge */
2430 if (it_cdr->fn_table->process_bridge_enter) {
2431 CDR_DEBUG(mod_cfg, "%p - Processing bridge enter for %s\n", it_cdr,
2433 result = it_cdr->fn_table->process_bridge_enter(it_cdr, bridge, channel);
2435 case BRIDGE_ENTER_ONLY_PARTY:
2437 case BRIDGE_ENTER_OBTAINED_PARTY_B:
2439 handled_cdr = it_cdr;
2442 case BRIDGE_ENTER_NEED_CDR:
2445 case BRIDGE_ENTER_NO_PARTY_B:
2446 /* We didn't win on any - end this CDR. If someone else comes in later
2447 * that is Party B to this CDR, it can re-activate this CDR.
2450 handled_cdr = it_cdr;
2452 cdr_object_finalize(cdr);
2458 /* Create the new matchings, but only for either:
2459 * * The first CDR in the chain that handled it. This avoids issues with
2461 * * If no one handled it, the last CDR in the chain. This would occur if
2462 * a CDR joined a bridge and it wasn't Party A for anyone. We still need
2463 * to make pairings with everyone in the bridge.
2466 handle_bridge_pairings(handled_cdr, bridge);
2468 /* Nothing handled it - we need a new one! */
2469 new_cdr = cdr_object_create_and_append(cdr);
2471 /* This is guaranteed to succeed: the new CDR is created in the single state
2472 * and will be able to handle the bridge enter message
2474 handle_standard_bridge_enter_message(cdr, bridge, channel);
2482 * \brief Handler for Stasis-Core bridge enter messages
2483 * \param data Passed on
2484 * \param sub The stasis subscription for this message callback
2485 * \param topic The topic this message was published for
2486 * \param message The message - hopefully a bridge one!
2488 static void handle_bridge_enter_message(void *data, struct stasis_subscription *sub,
2489 struct stasis_message *message)
2491 struct ast_bridge_blob *update = stasis_message_data(message);
2492 struct ast_bridge_snapshot *bridge = update->bridge;
2493 struct ast_channel_snapshot *channel = update->channel;
2494 RAII_VAR(struct cdr_object *, cdr,
2495 ao2_find(active_cdrs_by_channel, channel->uniqueid, OBJ_KEY),
2497 RAII_VAR(struct module_config *, mod_cfg,
2498 ao2_global_obj_ref(module_configs), ao2_cleanup);
2500 if (filter_bridge_messages(bridge)) {
2504 if (filter_channel_snapshot(channel)) {
2508 CDR_DEBUG(mod_cfg, "Bridge Enter message for channel %s: %u.%08u\n",
2510 (unsigned int)stasis_message_timestamp(message)->tv_sec,
2511 (unsigned int)stasis_message_timestamp(message)->tv_usec);
2514 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->name);
2519 if (!strcmp(bridge->subclass, "parking")) {
2520 handle_parking_bridge_enter_message(cdr, bridge, channel);
2522 handle_standard_bridge_enter_message(cdr, bridge, channel);
2527 * \brief Handler for when a channel is parked
2528 * \param data Passed on
2529 * \param sub The stasis subscription for this message callback
2530 * \param topic The topic this message was published for
2531 * \param message The message about who got parked
2533 static void handle_parked_call_message(void *data, struct stasis_subscription *sub,
2534 struct stasis_message *message)
2536 struct ast_parked_call_payload *payload = stasis_message_data(message);
2537 struct ast_channel_snapshot *channel = payload->parkee;
2538 RAII_VAR(struct cdr_object *, cdr, NULL, ao2_cleanup);
2539 RAII_VAR(struct module_config *, mod_cfg,
2540 ao2_global_obj_ref(module_configs), ao2_cleanup);
2542 struct cdr_object *it_cdr;
2544 /* Anything other than getting parked will be handled by other updates */
2545 if (payload->event_type != PARKED_CALL) {
2549 /* No one got parked? */
2554 if (filter_channel_snapshot(channel)) {
2558 CDR_DEBUG(mod_cfg, "Parked Call message for channel %s: %u.%08u\n",
2560 (unsigned int)stasis_message_timestamp(message)->tv_sec,
2561 (unsigned int)stasis_message_timestamp(message)->tv_usec);
2563 cdr = ao2_find(active_cdrs_by_channel, channel->uniqueid, OBJ_KEY);
2565 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->name);
2572 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2573 if (it_cdr->fn_table->process_parked_channel) {
2574 unhandled &= it_cdr->fn_table->process_parked_channel(it_cdr, payload);
2579 /* Nothing handled the messgae - we need a new one! */
2580 struct cdr_object *new_cdr = cdr_object_create_and_append(cdr);
2582 /* As the new CDR is created in the single state, it is guaranteed
2583 * to have a function for the parked call message and will handle
2585 new_cdr->fn_table->process_parked_channel(new_cdr, payload);
2594 * \brief Handler for a synchronization message
2595 * \param data Passed on
2596 * \param sub The stasis subscription for this message callback
2597 * \param topic The topic this message was published for
2598 * \param message A blank ao2 object
2600 static void handle_cdr_sync_message(void *data, struct stasis_subscription *sub,
2601 struct stasis_message *message)
2606 struct ast_cdr_config *ast_cdr_get_config(void)
2608 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
2609 ao2_ref(mod_cfg->general, +1);
2610 return mod_cfg->general;
2613 void ast_cdr_set_config(struct ast_cdr_config *config)
2615 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
2617 ao2_cleanup(mod_cfg->general);
2618 mod_cfg->general = config;
2619 ao2_ref(mod_cfg->general, +1);
2621 cdr_toggle_runtime_options();
2624 int ast_cdr_is_enabled(void)
2626 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
2627 return ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED);
2630 int ast_cdr_backend_suspend(const char *name)
2633 struct cdr_beitem *i = NULL;
2635 AST_RWLIST_WRLOCK(&be_list);
2636 AST_RWLIST_TRAVERSE(&be_list, i, list) {
2637 if (!strcasecmp(name, i->name)) {
2638 ast_debug(3, "Suspending CDR backend %s\n", i->name);
2643 AST_RWLIST_UNLOCK(&be_list);
2648 int ast_cdr_backend_unsuspend(const char *name)
2651 struct cdr_beitem *i = NULL;
2653 AST_RWLIST_WRLOCK(&be_list);
2654 AST_RWLIST_TRAVERSE(&be_list, i, list) {
2655 if (!strcasecmp(name, i->name)) {
2656 ast_debug(3, "Unsuspending CDR backend %s\n", i->name);
2661 AST_RWLIST_UNLOCK(&be_list);
2666 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
2668 struct cdr_beitem *i = NULL;
2674 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
2678 AST_RWLIST_WRLOCK(&be_list);
2679 AST_RWLIST_TRAVERSE(&be_list, i, list) {
2680 if (!strcasecmp(name, i->name)) {
2681 ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
2682 AST_RWLIST_UNLOCK(&be_list);
2687 if (!(i = ast_calloc(1, sizeof(*i))))
2691 ast_copy_string(i->name, name, sizeof(i->name));
2692 ast_copy_string(i->desc, desc, sizeof(i->desc));
2694 AST_RWLIST_INSERT_HEAD(&be_list, i, list);
2695 AST_RWLIST_UNLOCK(&be_list);
2700 int ast_cdr_unregister(const char *name)
2702 struct cdr_beitem *match = NULL;
2705 AST_RWLIST_WRLOCK(&be_list);
2706 AST_RWLIST_TRAVERSE(&be_list, match, list) {
2707 if (!strcasecmp(name, match->name)) {
2713 AST_RWLIST_UNLOCK(&be_list);
2717 active_count = ao2_container_count(active_cdrs_by_channel);
2719 if (!match->suspended && active_count != 0) {
2720 AST_RWLIST_UNLOCK(&be_list);
2721 ast_log(AST_LOG_WARNING, "Unable to unregister CDR backend %s; %d CDRs are still active\n",
2722 name, active_count);
2726 AST_RWLIST_REMOVE(&be_list, match, list);
2727 AST_RWLIST_UNLOCK(&be_list);
2729 ast_verb(2, "Unregistered '%s' CDR backend\n", name);
2735 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
2737 struct ast_cdr *newcdr;
2742 newcdr = ast_cdr_alloc();
2748 AST_LIST_HEAD_INIT_NOLOCK(&newcdr->varshead);
2749 copy_variables(&newcdr->varshead, &cdr->varshead);
2750 newcdr->next = NULL;
2755 static const char *cdr_format_var_internal(struct ast_cdr *cdr, const char *name)
2757 struct ast_var_t *variables;
2759 if (ast_strlen_zero(name)) {
2763 AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
2764 if (!strcasecmp(name, ast_var_name(variables))) {
2765 return ast_var_value(variables);
2772 static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
2774 if (fmt == NULL) { /* raw mode */
2775 snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
2777 buf[0] = '\0';/* Ensure the buffer is initialized. */
2781 ast_localtime(&when, &tm, NULL);
2782 ast_strftime(buf, bufsize, fmt, &tm);
2787 void ast_cdr_format_var(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int raw)
2789 const char *fmt = "%Y-%m-%d %T";
2798 if (!strcasecmp(name, "clid")) {
2799 ast_copy_string(workspace, cdr->clid, workspacelen);
2800 } else if (!strcasecmp(name, "src")) {
2801 ast_copy_string(workspace, cdr->src, workspacelen);
2802 } else if (!strcasecmp(name, "dst")) {
2803 ast_copy_string(workspace, cdr->dst, workspacelen);
2804 } else if (!strcasecmp(name, "dcontext")) {
2805 ast_copy_string(workspace, cdr->dcontext, workspacelen);
2806 } else if (!strcasecmp(name, "channel")) {
2807 ast_copy_string(workspace, cdr->channel, workspacelen);
2808 } else if (!strcasecmp(name, "dstchannel")) {
2809 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
2810 } else if (!strcasecmp(name, "lastapp")) {
2811 ast_copy_string(workspace, cdr->lastapp, workspacelen);
2812 } else if (!strcasecmp(name, "lastdata")) {
2813 ast_copy_string(workspace, cdr->lastdata, workspacelen);
2814 } else if (!strcasecmp(name, "start")) {
2815 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
2816 } else if (!strcasecmp(name, "answer")) {
2817 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
2818 } else if (!strcasecmp(name, "end")) {
2819 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
2820 } else if (!strcasecmp(name, "duration")) {
2821 snprintf(workspace, workspacelen, "%ld", cdr->end.tv_sec != 0 ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
2822 } else if (!strcasecmp(name, "billsec")) {
2823 snprintf(workspace, workspacelen, "%ld", (cdr->billsec || !ast_tvzero(cdr->end) || ast_tvzero(cdr->answer)) ? cdr->billsec : (long)ast_tvdiff_ms(ast_tvnow(), cdr->answer) / 1000);
2824 } else if (!strcasecmp(name, "disposition")) {
2826 snprintf(workspace, workspacelen, "%ld", cdr->disposition);
2828 ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
2830 } else if (!strcasecmp(name, "amaflags")) {
2832 snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
2834 ast_copy_string(workspace, ast_channel_amaflags2string(cdr->amaflags), workspacelen);
2836 } else if (!strcasecmp(name, "accountcode")) {
2837 ast_copy_string(workspace, cdr->accountcode, workspacelen);
2838 } else if (!strcasecmp(name, "peeraccount")) {
2839 ast_copy_string(workspace, cdr->peeraccount, workspacelen);
2840 } else if (!strcasecmp(name, "uniqueid")) {
2841 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
2842 } else if (!strcasecmp(name, "linkedid")) {
2843 ast_copy_string(workspace, cdr->linkedid, workspacelen);
2844 } else if (!strcasecmp(name, "userfield")) {
2845 ast_copy_string(workspace, cdr->userfield, workspacelen);
2846 } else if (!strcasecmp(name, "sequence")) {
2847 snprintf(workspace, workspacelen, "%d", cdr->sequence);
2848 } else if ((varbuf = cdr_format_var_internal(cdr, name))) {
2849 ast_copy_string(workspace, varbuf, workspacelen);
2851 workspace[0] = '\0';
2854 if (!ast_strlen_zero(workspace)) {
2861 * \brief Callback that finds all CDRs that reference a particular channel by name
2863 static int cdr_object_select_all_by_name_cb(void *obj, void *arg, int flags)
2865 struct cdr_object *cdr = obj;
2866 const char *name = arg;
2868 if (!strcasecmp(cdr->party_a.snapshot->name, name) ||
2869 (cdr->party_b.snapshot && !strcasecmp(cdr->party_b.snapshot->name, name))) {
2877 * \brief Callback that finds a CDR by channel name
2879 static int cdr_object_get_by_name_cb(void *obj, void *arg, int flags)
2881 struct cdr_object *cdr = obj;
2882 const char *name = arg;
2884 if (!strcasecmp(cdr->party_a.snapshot->name, name)) {
2890 /* Read Only CDR variables */
2891 static const char * const cdr_readonly_vars[] = {
2915 int ast_cdr_setvar(const char *channel_name, const char *name, const char *value)
2917 struct cdr_object *cdr;
2918 struct cdr_object *it_cdr;
2919 struct ao2_iterator *it_cdrs;
2920 char *arg = ast_strdupa(channel_name);
2923 for (x = 0; cdr_readonly_vars[x]; x++) {
2924 if (!strcasecmp(name, cdr_readonly_vars[x])) {
2925 ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!\n", name);
2930 it_cdrs = ao2_callback(active_cdrs_by_channel, OBJ_MULTIPLE, cdr_object_select_all_by_name_cb, arg);
2932 ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name);
2936 for (; (cdr = ao2_iterator_next(it_cdrs)); ao2_unlock(cdr), ao2_cleanup(cdr)) {
2938 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2939 struct varshead *headp = NULL;
2941 if (it_cdr->fn_table == &finalized_state_fn_table) {
2944 if (!strcasecmp(channel_name, it_cdr->party_a.snapshot->name)) {
2945 headp = &it_cdr->party_a.variables;
2946 } else if (it_cdr->party_b.snapshot
2947 && !strcasecmp(channel_name, it_cdr->party_b.snapshot->name)) {
2948 headp = &it_cdr->party_b.variables;
2951 set_variable(headp, name, value);
2955 ao2_iterator_destroy(it_cdrs);
2961 * \brief Format a variable on a \ref cdr_object
2963 static void cdr_object_format_var_internal(struct cdr_object *cdr, const char *name, char *value, size_t length)
2965 struct ast_var_t *variable;
2967 AST_LIST_TRAVERSE(&cdr->party_a.variables, variable, entries) {
2968 if (!strcasecmp(name, ast_var_name(variable))) {
2969 ast_copy_string(value, ast_var_value(variable), length);
2978 * \brief Format one of the standard properties on a \ref cdr_object
2980 static int cdr_object_format_property(struct cdr_object *cdr_obj, const char *name, char *value, size_t length)
2982 struct ast_channel_snapshot *party_a = cdr_obj->party_a.snapshot;
2983 struct ast_channel_snapshot *party_b = cdr_obj->party_b.snapshot;
2985 if (!strcasecmp(name, "clid")) {
2986 ast_callerid_merge(value, length, party_a->caller_name, party_a->caller_number, "");
2987 } else if (!strcasecmp(name, "src")) {
2988 ast_copy_string(value, party_a->caller_number, length);
2989 } else if (!strcasecmp(name, "dst")) {
2990 ast_copy_string(value, party_a->exten, length);
2991 } else if (!strcasecmp(name, "dcontext")) {
2992 ast_copy_string(value, party_a->context, length);
2993 } else if (!strcasecmp(name, "channel")) {
2994 ast_copy_string(value, party_a->name, length);
2995 } else if (!strcasecmp(name, "dstchannel")) {
2997 ast_copy_string(value, party_b->name, length);
2999 ast_copy_string(value, "", length);
3001 } else if (!strcasecmp(name, "lastapp")) {
3002 ast_copy_string(value, party_a->appl, length);
3003 } else if (!strcasecmp(name, "lastdata")) {
3004 ast_copy_string(value, party_a->data, length);
3005 } else if (!strcasecmp(name, "start")) {
3006 cdr_get_tv(cdr_obj->start, NULL, value, length);
3007 } else if (!strcasecmp(name, "answer")) {
3008 cdr_get_tv(cdr_obj->answer, NULL, value, length);
3009 } else if (!strcasecmp(name, "end")) {
3010 cdr_get_tv(cdr_obj->end, NULL, value, length);
3011 } else if (!strcasecmp(name, "duration")) {
3012 snprintf(value, length, "%ld", cdr_object_get_duration(cdr_obj));
3013 } else if (!strcasecmp(name, "billsec")) {
3014 snprintf(value, length, "%ld", cdr_object_get_billsec(cdr_obj));
3015 } else if (!strcasecmp(name, "disposition")) {
3016 snprintf(value, length, "%d", cdr_obj->disposition);
3017 } else if (!strcasecmp(name, "amaflags")) {
3018 snprintf(value, length, "%d", party_a->amaflags);
3019 } else if (!strcasecmp(name, "accountcode")) {
3020 ast_copy_string(value, party_a->accountcode, length);
3021 } else if (!strcasecmp(name, "peeraccount")) {
3023 ast_copy_string(value, party_b->accountcode, length);
3025 ast_copy_string(value, "", length);
3027 } else if (!strcasecmp(name, "uniqueid")) {
3028 ast_copy_string(value, party_a->uniqueid, length);
3029 } else if (!strcasecmp(name, "linkedid")) {
3030 ast_copy_string(value, cdr_obj->linkedid, length);
3031 } else if (!strcasecmp(name, "userfield")) {
3032 ast_copy_string(value, cdr_obj->party_a.userfield, length);
3033 } else if (!strcasecmp(name, "sequence")) {
3034 snprintf(value, length, "%d", cdr_obj->sequence);
3043 * \brief Look up and retrieve a CDR object by channel name
3044 * \param name The name of the channel
3045 * \retval NULL on error
3046 * \retval The \ref cdr_object for the channel on success, with the reference
3047 * count bumped by one.
3049 static struct cdr_object *cdr_object_get_by_name(const char *name)
3053 if (ast_strlen_zero(name)) {
3057 param = ast_strdupa(name);
3058 return ao2_callback(active_cdrs_by_channel, 0, cdr_object_get_by_name_cb, param);
3061 int ast_cdr_getvar(const char *channel_name, const char *name, char *value, size_t length)
3063 RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
3064 struct cdr_object *cdr_obj;
3067 ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name);
3071 if (ast_strlen_zero(name)) {
3077 cdr_obj = cdr->last;
3078 if (cdr_object_format_property(cdr_obj, name, value, length)) {
3079 /* Property failed; attempt variable */
3080 cdr_object_format_var_internal(cdr_obj, name, value, length);
3088 int ast_cdr_serialize_variables(const char *channel_name, struct ast_str **buf, char delim, char sep)
3090 RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
3091 struct cdr_object *it_cdr;
3092 struct ast_var_t *variable;
3094 RAII_VAR(char *, workspace, ast_malloc(256), ast_free);
3095 int total = 0, x = 0, i;
3102 RAII_VAR(struct module_config *, mod_cfg,
3103 ao2_global_obj_ref(module_configs), ao2_cleanup);
3105 if (ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
3106 ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name);
3112 ast_str_reset(*buf);
3115 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3117 ast_str_append(buf, 0, "\n");
3119 AST_LIST_TRAVERSE(&it_cdr->party_a.variables, variable, entries) {
3120 if (!(var = ast_var_name(variable))) {
3124 if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variable), ""), sep) < 0) {
3125 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
3132 for (i = 0; cdr_readonly_vars[i]; i++) {
3133 if (cdr_object_format_property(it_cdr, cdr_readonly_vars[i], workspace, sizeof(workspace))) {
3134 /* Unhandled read-only CDR variable. */
3139 if (!ast_strlen_zero(workspace)
3140 && ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, workspace, sep) < 0) {
3141 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
3151 void ast_cdr_free(struct ast_cdr *cdr)
3154 struct ast_cdr *next = cdr->next;
3156 free_variables(&cdr->varshead);
3162 struct ast_cdr *ast_cdr_alloc(void)
3166 x = ast_calloc(1, sizeof(*x));
3170 const char *ast_cdr_disp2str(int disposition)
3172 switch (disposition) {
3174 return "NO ANSWER"; /* by default, for backward compatibility */
3175 case AST_CDR_NOANSWER:
3177 case AST_CDR_FAILED:
3181 case AST_CDR_ANSWERED:
3183 case AST_CDR_CONGESTION:
3184 return "CONGESTION";
3189 struct party_b_userfield_update {
3190 const char *channel_name;
3191 const char *userfield;
3194 /*! \brief Callback used to update the userfield on Party B on all CDRs */
3195 static int cdr_object_update_party_b_userfield_cb(void *obj, void *arg, int flags)
3197 struct cdr_object *cdr = obj;
3198 struct party_b_userfield_update *info = arg;
3199 struct cdr_object *it_cdr;
3200 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3201 if (it_cdr->fn_table == &finalized_state_fn_table) {
3204 if (it_cdr->party_b.snapshot
3205 && !strcasecmp(it_cdr->party_b.snapshot->name, info->channel_name)) {
3206 strcpy(it_cdr->party_b.userfield, info->userfield);
3212 void ast_cdr_setuserfield(const char *channel_name, const char *userfield)
3214 RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
3215 struct party_b_userfield_update party_b_info = {
3216 .channel_name = channel_name,
3217 .userfield = userfield,
3219 struct cdr_object *it_cdr;
3221 /* Handle Party A */
3224 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3225 if (it_cdr->fn_table == &finalized_state_fn_table) {
3228 strcpy(it_cdr->party_a.userfield, userfield);
3233 /* Handle Party B */
3234 ao2_callback(active_cdrs_by_channel, OBJ_NODATA,
3235 cdr_object_update_party_b_userfield_cb,
3240 static void post_cdr(struct ast_cdr *cdr)
3242 RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
3243 struct cdr_beitem *i;
3245 for (; cdr ; cdr = cdr->next) {
3246 /* For people, who don't want to see unanswered single-channel events */
3247 if (!ast_test_flag(&mod_cfg->general->settings, CDR_UNANSWERED) &&
3248 cdr->disposition < AST_CDR_ANSWERED &&
3249 (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
3250 ast_debug(1, "Skipping CDR for %s since we weren't answered\n", cdr->channel);
3254 if (ast_test_flag(cdr, AST_CDR_FLAG_DISABLE)) {
3257 AST_RWLIST_RDLOCK(&be_list);
3258 AST_RWLIST_TRAVERSE(&be_list, i, list) {
3259 if (!i->suspended) {
3263 AST_RWLIST_UNLOCK(&be_list);
3267 int ast_cdr_set_property(const char *channel_name, enum ast_cdr_options option)
3269 RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
3270 struct cdr_object *it_cdr;
3277 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3278 if (it_cdr->fn_table == &finalized_state_fn_table) {