cdr.c: Fix setting dnid, callingsubaddr, and calledsubaddr
[asterisk/asterisk.git] / main / cdr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Call Detail Record API
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \note Includes code and algorithms from the Zapata library.
26  *
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.
31  */
32
33 /*! \li \ref cdr.c uses the configuration file \ref cdr.conf
34  * \addtogroup configuration_file Configuration Files
35  */
36
37 /*!
38  * \page cdr.conf cdr.conf
39  * \verbinclude cdr.conf.sample
40  */
41
42 /*** MODULEINFO
43         <support_level>core</support_level>
44  ***/
45
46 #include "asterisk.h"
47
48 #include <signal.h>
49 #include <inttypes.h>
50
51 #include "asterisk/lock.h"
52 #include "asterisk/channel.h"
53 #include "asterisk/cdr.h"
54 #include "asterisk/callerid.h"
55 #include "asterisk/manager.h"
56 #include "asterisk/causes.h"
57 #include "asterisk/linkedlists.h"
58 #include "asterisk/utils.h"
59 #include "asterisk/sched.h"
60 #include "asterisk/config.h"
61 #include "asterisk/cli.h"
62 #include "asterisk/stringfields.h"
63 #include "asterisk/config_options.h"
64 #include "asterisk/json.h"
65 #include "asterisk/parking.h"
66 #include "asterisk/stasis.h"
67 #include "asterisk/stasis_channels.h"
68 #include "asterisk/stasis_bridges.h"
69 #include "asterisk/stasis_message_router.h"
70 #include "asterisk/astobj2.h"
71 #include "asterisk/taskprocessor.h"
72
73 /*** DOCUMENTATION
74         <configInfo name="cdr" language="en_US">
75                 <synopsis>Call Detail Record configuration</synopsis>
76                 <description>
77                         <para>CDR is Call Detail Record, which provides logging services via a variety of
78                         pluggable backend modules. Detailed call information can be recorded to
79                         databases, files, etc. Useful for billing, fraud prevention, compliance with
80                         Sarbanes-Oxley aka The Enron Act, QOS evaluations, and more.</para>
81                 </description>
82                 <configFile name="cdr.conf">
83                         <configObject name="general">
84                                 <synopsis>Global settings applied to the CDR engine.</synopsis>
85                                 <configOption name="debug">
86                                         <synopsis>Enable/disable verbose CDR debugging.</synopsis>
87                                         <description><para>When set to <literal>True</literal>, verbose updates
88                                         of changes in CDR information will be logged. Note that this is only
89                                         of use when debugging CDR behavior.</para>
90                                         </description>
91                                 </configOption>
92                                 <configOption name="enable">
93                                         <synopsis>Enable/disable CDR logging.</synopsis>
94                                         <description><para>Define whether or not to use CDR logging. Setting this to "no" will override
95                                         any loading of backend CDR modules.  Default is "yes".</para>
96                                         </description>
97                                 </configOption>
98                                 <configOption name="unanswered">
99                                         <synopsis>Log calls that are never answered and don't set an outgoing party.</synopsis>
100                                         <description><para>
101                                         Define whether or not to log unanswered calls that don't involve an outgoing party. Setting
102                                         this to "yes" will make calls to extensions that don't answer and don't set a side B channel
103                                         (such as by using the Dial application) receive CDR log entries. If this option is set to
104                                         "no", then those log entries will not be created. Unanswered calls which get offered to an
105                                         outgoing line will always receive log entries regardless of this option, and that is the
106                                         intended behavior.
107                                         </para>
108                                         </description>
109                                 </configOption>
110                                 <configOption name="congestion">
111                                         <synopsis>Log congested calls.</synopsis>
112                                         <description><para>Define whether or not to log congested calls. Setting this to "yes" will
113                                         report each call that fails to complete due to congestion conditions.</para>
114                                         </description>
115                                 </configOption>
116                                 <configOption name="endbeforehexten">
117                                         <synopsis>Don't produce CDRs while executing hangup logic</synopsis>
118                                         <description>
119                                                 <para>As each CDR for a channel is finished, its end time is updated
120                                                 and the CDR is finalized. When a channel is hung up and hangup
121                                                 logic is present (in the form of a hangup handler or the
122                                                 <literal>h</literal> extension), a new CDR is generated for the
123                                                 channel. Any statistics are gathered from this new CDR. By enabling
124                                                 this option, no new CDR is created for the dialplan logic that is
125                                                 executed in <literal>h</literal> extensions or attached hangup handler
126                                                 subroutines. The default value is <literal>yes</literal>, indicating
127                                                 that a CDR will be generated during hangup logic.</para>
128                                         </description>
129                                 </configOption>
130                                 <configOption name="initiatedseconds">
131                                         <synopsis>Count microseconds for billsec purposes</synopsis>
132                                         <description><para>Normally, the <literal>billsec</literal> field logged to the CDR backends
133                                         is simply the end time (hangup time) minus the answer time in seconds. Internally,
134                                         asterisk stores the time in terms of microseconds and seconds. By setting
135                                         initiatedseconds to <literal>yes</literal>, you can force asterisk to report any seconds
136                                         that were initiated (a sort of round up method). Technically, this is
137                                         when the microsecond part of the end time is greater than the microsecond
138                                         part of the answer time, then the billsec time is incremented one second.</para>
139                                         </description>
140                                 </configOption>
141                                 <configOption name="batch">
142                                         <synopsis>Submit CDRs to the backends for processing in batches</synopsis>
143                                         <description><para>Define the CDR batch mode, where instead of posting the CDR at the end of
144                                         every call, the data will be stored in a buffer to help alleviate load on the
145                                         asterisk server.</para>
146                                         <warning><para>Use of batch mode may result in data loss after unsafe asterisk termination,
147                                         i.e., software crash, power failure, kill -9, etc.</para>
148                                         </warning>
149                                         </description>
150                                 </configOption>
151                                 <configOption name="size">
152                                         <synopsis>The maximum number of CDRs to accumulate before triggering a batch</synopsis>
153                                         <description><para>Define the maximum number of CDRs to accumulate in the buffer before posting
154                                         them to the backend engines. batch must be set to <literal>yes</literal>.</para>
155                                         </description>
156                                 </configOption>
157                                 <configOption name="time">
158                                         <synopsis>The maximum time to accumulate CDRs before triggering a batch</synopsis>
159                                         <description><para>Define the maximum time to accumulate CDRs before posting them in a batch to the
160                                         backend engines. If this time limit is reached, then it will post the records, regardless of the value
161                                         defined for size. batch must be set to <literal>yes</literal>.</para>
162                                         <note><para>Time is expressed in seconds.</para></note>
163                                         </description>
164                                 </configOption>
165                                 <configOption name="scheduleronly">
166                                         <synopsis>Post batched CDRs on their own thread instead of the scheduler</synopsis>
167                                         <description><para>The CDR engine uses the internal asterisk scheduler to determine when to post
168                                         records.  Posting can either occur inside the scheduler thread, or a new
169                                         thread can be spawned for the submission of every batch.  For small batches,
170                                         it might be acceptable to just use the scheduler thread, so set this to <literal>yes</literal>.
171                                         For large batches, say anything over size=10, a new thread is recommended, so
172                                         set this to <literal>no</literal>.</para>
173                                         </description>
174                                 </configOption>
175                                 <configOption name="safeshutdown">
176                                         <synopsis>Block shutdown of Asterisk until CDRs are submitted</synopsis>
177                                         <description><para>When shutting down asterisk, you can block until the CDRs are submitted.  If
178                                         you don't, then data will likely be lost.  You can always check the size of
179                                         the CDR batch buffer with the CLI <astcli>cdr status</astcli> command.  To enable blocking on
180                                         submission of CDR data during asterisk shutdown, set this to <literal>yes</literal>.</para>
181                                         </description>
182                                 </configOption>
183                         </configObject>
184                 </configFile>
185         </configInfo>
186  ***/
187
188
189 /* The prime here should be similar in size to the channel container. */
190 #ifdef LOW_MEMORY
191 #define NUM_CDR_BUCKETS 61
192 #else
193 #define NUM_CDR_BUCKETS 769
194 #endif
195
196 #define DEFAULT_ENABLED "1"
197 #define DEFAULT_BATCHMODE "0"
198 #define DEFAULT_UNANSWERED "0"
199 #define DEFAULT_CONGESTION "0"
200 #define DEFAULT_END_BEFORE_H_EXTEN "1"
201 #define DEFAULT_INITIATED_SECONDS "0"
202
203 #define DEFAULT_BATCH_SIZE "100"
204 #define MAX_BATCH_SIZE 1000
205 #define DEFAULT_BATCH_TIME "300"
206 #define MAX_BATCH_TIME 86400
207 #define DEFAULT_BATCH_SCHEDULER_ONLY "0"
208 #define DEFAULT_BATCH_SAFE_SHUTDOWN "1"
209
210 #define CDR_DEBUG(mod_cfg, fmt, ...) \
211         do { \
212                 if (ast_test_flag(&(mod_cfg)->general->settings, CDR_DEBUG)) { \
213                         ast_verbose((fmt), ##__VA_ARGS__); \
214                 } \
215         } while (0)
216
217 static void cdr_detach(struct ast_cdr *cdr);
218 static void cdr_submit_batch(int shutdown);
219 static int cdr_toggle_runtime_options(void);
220
221 /*! \brief The configuration settings for this module */
222 struct module_config {
223         struct ast_cdr_config *general;         /*!< CDR global settings */
224 };
225
226 /*! \brief The container for the module configuration */
227 static AO2_GLOBAL_OBJ_STATIC(module_configs);
228
229 /*! \brief The type definition for general options */
230 static struct aco_type general_option = {
231         .type = ACO_GLOBAL,
232         .name = "general",
233         .item_offset = offsetof(struct module_config, general),
234         .category = "^general$",
235         .category_match = ACO_WHITELIST,
236 };
237
238 static void *module_config_alloc(void);
239 static void module_config_destructor(void *obj);
240
241 /*! \brief The file definition */
242 static struct aco_file module_file_conf = {
243         .filename = "cdr.conf",
244         .skip_category = "(^csv$|^custom$|^manager$|^odbc$|^pgsql$|^radius$|^sqlite$|^tds$|^mysql$)",
245         .types = ACO_TYPES(&general_option),
246 };
247
248 CONFIG_INFO_CORE("cdr", cfg_info, module_configs, module_config_alloc,
249         .files = ACO_FILES(&module_file_conf),
250 );
251
252 static struct aco_type *general_options[] = ACO_TYPES(&general_option);
253
254 /*! \brief Dispose of a module config object */
255 static void module_config_destructor(void *obj)
256 {
257         struct module_config *cfg = obj;
258
259         if (!cfg) {
260                 return;
261         }
262         ao2_ref(cfg->general, -1);
263 }
264
265 /*! \brief Create a new module config object */
266 static void *module_config_alloc(void)
267 {
268         struct module_config *mod_cfg;
269         struct ast_cdr_config *cdr_config;
270
271         mod_cfg = ao2_alloc(sizeof(*mod_cfg), module_config_destructor);
272         if (!mod_cfg) {
273                 return NULL;
274         }
275
276         cdr_config = ao2_alloc(sizeof(*cdr_config), NULL);
277         if (!cdr_config) {
278                 ao2_ref(cdr_config, -1);
279                 return NULL;
280         }
281         mod_cfg->general = cdr_config;
282
283         return mod_cfg;
284 }
285
286 /*! \brief Registration object for CDR backends */
287 struct cdr_beitem {
288         char name[20];
289         char desc[80];
290         ast_cdrbe be;
291         AST_RWLIST_ENTRY(cdr_beitem) list;
292         int suspended:1;
293 };
294
295 /*! \brief List of registered backends */
296 static AST_RWLIST_HEAD_STATIC(be_list, cdr_beitem);
297
298 /*! \brief List of registered modifiers */
299 static AST_RWLIST_HEAD_STATIC(mo_list, cdr_beitem);
300
301 /*! \brief Queued CDR waiting to be batched */
302 struct cdr_batch_item {
303         struct ast_cdr *cdr;
304         struct cdr_batch_item *next;
305 };
306
307 /*! \brief The actual batch queue */
308 static struct cdr_batch {
309         int size;
310         struct cdr_batch_item *head;
311         struct cdr_batch_item *tail;
312 } *batch = NULL;
313
314 /*! \brief The global sequence counter used for CDRs */
315 static int global_cdr_sequence =  0;
316
317 /*! \brief Scheduler items */
318 static struct ast_sched_context *sched;
319 static int cdr_sched = -1;
320 AST_MUTEX_DEFINE_STATIC(cdr_sched_lock);
321 static pthread_t cdr_thread = AST_PTHREADT_NULL;
322
323 /*! \brief Lock protecting modifications to the batch queue */
324 AST_MUTEX_DEFINE_STATIC(cdr_batch_lock);
325
326 /*! \brief These are used to wake up the CDR thread when there's work to do */
327 AST_MUTEX_DEFINE_STATIC(cdr_pending_lock);
328 static ast_cond_t cdr_pending_cond;
329
330 /*! \brief A container of the active CDRs indexed by Party A channel id */
331 static struct ao2_container *active_cdrs_by_channel;
332
333 /*! \brief Message router for stasis messages regarding channel state */
334 static struct stasis_message_router *stasis_router;
335
336 /*! \brief Our subscription for bridges */
337 static struct stasis_forward *bridge_subscription;
338
339 /*! \brief Our subscription for channels */
340 static struct stasis_forward *channel_subscription;
341
342 /*! \brief Our subscription for parking */
343 static struct stasis_forward *parking_subscription;
344
345 /*! \brief The parent topic for all topics we want to aggregate for CDRs */
346 static struct stasis_topic *cdr_topic;
347
348 /*! \brief A message type used to synchronize with the CDR topic */
349 STASIS_MESSAGE_TYPE_DEFN_LOCAL(cdr_sync_message_type);
350
351 struct cdr_object;
352
353 /*! \brief Return types for \ref process_bridge_enter functions */
354 enum process_bridge_enter_results {
355         /*!
356          * The CDR was the only party in the bridge.
357          */
358         BRIDGE_ENTER_ONLY_PARTY,
359         /*!
360          * The CDR was able to obtain a Party B from some other party already in the bridge
361          */
362         BRIDGE_ENTER_OBTAINED_PARTY_B,
363         /*!
364          * The CDR was not able to obtain a Party B
365          */
366         BRIDGE_ENTER_NO_PARTY_B,
367         /*!
368          * This CDR can't handle a bridge enter message and a new CDR needs to be created
369          */
370         BRIDGE_ENTER_NEED_CDR,
371 };
372
373 /*!
374  * \brief A virtual table used for \ref cdr_object.
375  *
376  * Note that all functions are optional - if a subclass does not need an
377  * implementation, it is safe to leave it NULL.
378  */
379 struct cdr_object_fn_table {
380         /*! \brief Name of the subclass */
381         const char *name;
382
383         /*!
384          * \brief An initialization function. This will be called automatically
385          * when a \ref cdr_object is switched to this type in
386          * \ref cdr_object_transition_state
387          *
388          * \param cdr The \ref cdr_object that was just transitioned
389          */
390         void (* const init_function)(struct cdr_object *cdr);
391
392         /*!
393          * \brief Process a Party A update for the \ref cdr_object
394          *
395          * \param cdr The \ref cdr_object to process the update
396          * \param snapshot The snapshot for the CDR's Party A
397          * \retval 0 the CDR handled the update or ignored it
398          * \retval 1 the CDR is finalized and a new one should be made to handle it
399          */
400         int (* const process_party_a)(struct cdr_object *cdr,
401                         struct ast_channel_snapshot *snapshot);
402
403         /*!
404          * \brief Process a Party B update for the \ref cdr_object
405          *
406          * \param cdr The \ref cdr_object to process the update
407          * \param snapshot The snapshot for the CDR's Party B
408          */
409         void (* const process_party_b)(struct cdr_object *cdr,
410                         struct ast_channel_snapshot *snapshot);
411
412         /*!
413          * \brief Process the beginning of a dial. A dial message implies one of two
414          * things:
415          * The \ref cdr_object's Party A has been originated
416          * The \ref cdr_object's Party A is dialing its Party B
417          *
418          * \param cdr The \ref cdr_object
419          * \param caller The originator of the dial attempt
420          * \param peer The destination of the dial attempt
421          *
422          * \retval 0 if the parties in the dial were handled by this CDR
423          * \retval 1 if the parties could not be handled by this CDR
424          */
425         int (* const process_dial_begin)(struct cdr_object *cdr,
426                         struct ast_channel_snapshot *caller,
427                         struct ast_channel_snapshot *peer);
428
429         /*!
430          * \brief Process the end of a dial. At the end of a dial, a CDR can be
431          * transitioned into one of two states - DialedPending
432          * (\ref dialed_pending_state_fn_table) or Finalized
433          * (\ref finalized_state_fn_table).
434          *
435          * \param cdr The \ref cdr_object
436          * \param caller The originator of the dial attempt
437          * \param peer the Destination of the dial attempt
438          * \param dial_status What happened
439          *
440          * \retval 0 if the parties in the dial were handled by this CDR
441          * \retval 1 if the parties could not be handled by this CDR
442          */
443         int (* const process_dial_end)(struct cdr_object *cdr,
444                         struct ast_channel_snapshot *caller,
445                         struct ast_channel_snapshot *peer,
446                         const char *dial_status);
447
448         /*!
449          * \brief Process the entering of a bridge by this CDR. The purpose of this
450          * callback is to have the CDR prepare itself for the bridge and attempt to
451          * find a valid Party B. The act of creating new CDRs based on the entering
452          * of this channel into the bridge is handled by the higher level message
453          * handler.
454          *
455          * Note that this handler is for when a channel enters into a "normal"
456          * bridge, where people actually talk to each other. Parking is its own
457          * thing.
458          *
459          * \param cdr The \ref cdr_object
460          * \param bridge The bridge that the Party A just entered into
461          * \param channel The \ref ast_channel_snapshot for this CDR's Party A
462          *
463          * \retval process_bridge_enter_results Defines whether or not this CDR was able
464          * to fully handle the bridge enter message.
465          */
466         enum process_bridge_enter_results (* const process_bridge_enter)(
467                         struct cdr_object *cdr,
468                         struct ast_bridge_snapshot *bridge,
469                         struct ast_channel_snapshot *channel);
470
471         /*!
472          * \brief Process entering into a parking bridge.
473          *
474          * \param cdr The \ref cdr_object
475          * \param bridge The parking bridge that Party A just entered into
476          * \param channel The \ref ast_channel_snapshot for this CDR's Party A
477          *
478          * \retval 0 This CDR successfully transitioned itself into the parked state
479          * \retval 1 This CDR couldn't handle the parking transition and we need a
480          *  new CDR.
481          */
482         int (* const process_parking_bridge_enter)(struct cdr_object *cdr,
483                         struct ast_bridge_snapshot *bridge,
484                         struct ast_channel_snapshot *channel);
485
486         /*!
487          * \brief Process the leaving of a bridge by this CDR.
488          *
489          * \param cdr The \ref cdr_object
490          * \param bridge The bridge that the Party A just left
491          * \param channel The \ref ast_channel_snapshot for this CDR's Party A
492          *
493          * \retval 0 This CDR left successfully
494          * \retval 1 Error
495          */
496         int (* const process_bridge_leave)(struct cdr_object *cdr,
497                         struct ast_bridge_snapshot *bridge,
498                         struct ast_channel_snapshot *channel);
499
500         /*!
501          * \brief Process an update informing us that the channel got itself parked
502          *
503          * \param cdr The \ref cdr_object
504          * \param channel The parking information for this CDR's party A
505          *
506          * \retval 0 This CDR successfully parked itself
507          * \retval 1 This CDR couldn't handle the park
508          */
509         int (* const process_parked_channel)(struct cdr_object *cdr,
510                         struct ast_parked_call_payload *parking_info);
511 };
512
513 static int base_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
514 static enum process_bridge_enter_results base_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
515 static int base_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
516 static int base_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status);
517 static int base_process_parked_channel(struct cdr_object *cdr, struct ast_parked_call_payload *parking_info);
518
519 static void single_state_init_function(struct cdr_object *cdr);
520 static void single_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
521 static int single_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
522 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);
523 static int single_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
524
525 /*!
526  * \brief The virtual table for the Single state.
527  *
528  * A \ref cdr_object starts off in this state. This represents a channel that
529  * has no Party B information itself.
530  *
531  * A \ref cdr_object from this state can go into any of the following states:
532  * * \ref dial_state_fn_table
533  * * \ref bridge_state_fn_table
534  * * \ref finalized_state_fn_table
535  */
536 struct cdr_object_fn_table single_state_fn_table = {
537         .name = "Single",
538         .init_function = single_state_init_function,
539         .process_party_a = base_process_party_a,
540         .process_party_b = single_state_process_party_b,
541         .process_dial_begin = single_state_process_dial_begin,
542         .process_dial_end = base_process_dial_end,
543         .process_bridge_enter = single_state_process_bridge_enter,
544         .process_parking_bridge_enter = single_state_process_parking_bridge_enter,
545         .process_bridge_leave = base_process_bridge_leave,
546         .process_parked_channel = base_process_parked_channel,
547 };
548
549 static void dial_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
550 static int dial_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
551 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);
552 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);
553
554 /*!
555  * \brief The virtual table for the Dial state.
556  *
557  * A \ref cdr_object that has begun a dial operation. This state is entered when
558  * the Party A for a CDR is determined to be dialing out to a Party B or when
559  * a CDR is for an originated channel (in which case the Party A information is
560  * the originated channel, and there is no Party B).
561  *
562  * A \ref cdr_object from this state can go in any of the following states:
563  * * \ref dialed_pending_state_fn_table
564  * * \ref bridge_state_fn_table
565  * * \ref finalized_state_fn_table
566  */
567 struct cdr_object_fn_table dial_state_fn_table = {
568         .name = "Dial",
569         .process_party_a = base_process_party_a,
570         .process_party_b = dial_state_process_party_b,
571         .process_dial_begin = dial_state_process_dial_begin,
572         .process_dial_end = dial_state_process_dial_end,
573         .process_bridge_enter = dial_state_process_bridge_enter,
574         .process_bridge_leave = base_process_bridge_leave,
575 };
576
577 static int dialed_pending_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
578 static int dialed_pending_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer);
579 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);
580 static int dialed_pending_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
581
582 /*!
583  * \brief The virtual table for the Dialed Pending state.
584  *
585  * A \ref cdr_object that has successfully finished a dial operation, but we
586  * don't know what they're going to do yet. It's theoretically possible to dial
587  * a party and then have that party not be bridged with the caller; likewise,
588  * an origination can complete and the channel go off and execute dialplan. The
589  * pending state acts as a bridge between either:
590  * * Entering a bridge
591  * * Getting a new CDR for new dialplan execution
592  * * Switching from being originated to executing dialplan
593  *
594  * A \ref cdr_object from this state can go in any of the following states:
595  * * \ref single_state_fn_table
596  * * \ref dialed_pending_state_fn_table
597  * * \ref bridge_state_fn_table
598  * * \ref finalized_state_fn_table
599  */
600 struct cdr_object_fn_table dialed_pending_state_fn_table = {
601         .name = "DialedPending",
602         .process_party_a = dialed_pending_state_process_party_a,
603         .process_dial_begin = dialed_pending_state_process_dial_begin,
604         .process_bridge_enter = dialed_pending_state_process_bridge_enter,
605         .process_parking_bridge_enter = dialed_pending_state_process_parking_bridge_enter,
606         .process_bridge_leave = base_process_bridge_leave,
607         .process_parked_channel = base_process_parked_channel,
608 };
609
610 static void bridge_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
611 static int bridge_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
612
613 /*!
614  * \brief The virtual table for the Bridged state
615  *
616  * A \ref cdr_object enters this state when it receives notification that the
617  * channel has entered a bridge.
618  *
619  * A \ref cdr_object from this state can go to:
620  * * \ref finalized_state_fn_table
621  */
622 struct cdr_object_fn_table bridge_state_fn_table = {
623         .name = "Bridged",
624         .process_party_a = base_process_party_a,
625         .process_party_b = bridge_state_process_party_b,
626         .process_bridge_leave = bridge_state_process_bridge_leave,
627         .process_parked_channel = base_process_parked_channel,
628 };
629
630 static int parked_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel);
631
632 /*!
633  * \brief The virtual table for the Parked state
634  *
635  * Parking is weird. Unlike typical bridges, it has to be treated somewhat
636  * uniquely - a channel in a parking bridge (which is a subclass of a holding
637  * bridge) has to be handled as if the channel went into an application.
638  * However, when the channel comes out, we need a new CDR - unlike the Single
639  * state.
640  */
641 struct cdr_object_fn_table parked_state_fn_table = {
642         .name = "Parked",
643         .process_party_a = base_process_party_a,
644         .process_bridge_leave = parked_state_process_bridge_leave,
645         .process_parked_channel = base_process_parked_channel,
646 };
647
648 static void finalized_state_init_function(struct cdr_object *cdr);
649 static int finalized_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot);
650
651 /*!
652  * \brief The virtual table for the finalized state.
653  *
654  * Once in the finalized state, the CDR is done. No modifications can be made
655  * to the CDR.
656  */
657 struct cdr_object_fn_table finalized_state_fn_table = {
658         .name = "Finalized",
659         .init_function = finalized_state_init_function,
660         .process_party_a = finalized_state_process_party_a,
661         .process_bridge_enter = base_process_bridge_enter,
662 };
663
664 /*! \brief A wrapper object around a snapshot.
665  * Fields that are mutable by the CDR engine are replicated here.
666  */
667 struct cdr_object_snapshot {
668         struct ast_channel_snapshot *snapshot;  /*!< The channel snapshot */
669         char userfield[AST_MAX_USER_FIELD];     /*!< Userfield for the channel */
670         unsigned int flags;                     /*!< Specific flags for this party */
671         struct varshead variables;              /*!< CDR variables for the channel */
672 };
673
674 /*! \brief An in-memory representation of an active CDR */
675 struct cdr_object {
676         struct cdr_object_snapshot party_a;     /*!< The Party A information */
677         struct cdr_object_snapshot party_b;     /*!< The Party B information */
678         struct cdr_object_fn_table *fn_table;   /*!< The current virtual table */
679
680         enum ast_cdr_disposition disposition;   /*!< The disposition of the CDR */
681         struct timeval start;                   /*!< When this CDR was created */
682         struct timeval answer;                  /*!< Either when the channel was answered, or when the path between channels was established */
683         struct timeval end;                     /*!< When this CDR was finalized */
684         unsigned int sequence;                  /*!< A monotonically increasing number for each CDR */
685         struct ast_flags flags;                 /*!< Flags on the CDR */
686         AST_DECLARE_STRING_FIELDS(
687                 AST_STRING_FIELD(linkedid);         /*!< Linked ID. Cached here as it may change out from party A, which must be immutable */
688                 AST_STRING_FIELD(uniqueid);                     /*!< Unique id of party A. Cached here as it is the primary key of this CDR */
689                 AST_STRING_FIELD(name);             /*!< Channel name of party A. Cached here as the party A address may change */
690                 AST_STRING_FIELD(bridge);           /*!< The bridge the party A happens to be in. */
691                 AST_STRING_FIELD(appl);             /*!< The last accepted application party A was in */
692                 AST_STRING_FIELD(data);             /*!< The data for the last accepted application party A was in */
693                 AST_STRING_FIELD(context);          /*!< The accepted context for Party A */
694                 AST_STRING_FIELD(exten);            /*!< The accepted extension for Party A */
695         );
696         struct cdr_object *next;                /*!< The next CDR object in the chain */
697         struct cdr_object *last;                /*!< The last CDR object in the chain */
698         int is_root;                            /*!< True if this is the first CDR in the chain */
699 };
700
701 /*!
702  * \brief Copy variables from one list to another
703  * \param to_list destination
704  * \param from_list source
705  * \retval The number of copied variables
706  */
707 static int copy_variables(struct varshead *to_list, struct varshead *from_list)
708 {
709         struct ast_var_t *variables;
710         struct ast_var_t *newvariable;
711         const char *var;
712         const char *val;
713         int x = 0;
714
715         AST_LIST_TRAVERSE(from_list, variables, entries) {
716                 var = ast_var_name(variables);
717                 if (ast_strlen_zero(var)) {
718                         continue;
719                 }
720                 val = ast_var_value(variables);
721                 if (ast_strlen_zero(val)) {
722                         continue;
723                 }
724                 newvariable = ast_var_assign(var, val);
725                 if (newvariable) {
726                         AST_LIST_INSERT_HEAD(to_list, newvariable, entries);
727                         ++x;
728                 }
729         }
730
731         return x;
732 }
733
734 /*!
735  * \brief Delete all variables from a variable list
736  * \param headp The head pointer to the variable list to delete
737  */
738 static void free_variables(struct varshead *headp)
739 {
740         struct ast_var_t *vardata;
741
742         while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries))) {
743                 ast_var_delete(vardata);
744         }
745 }
746
747 /*!
748  * \brief Copy a snapshot and its details
749  * \param dst The destination
750  * \param src The source
751  */
752 static void cdr_object_snapshot_copy(struct cdr_object_snapshot *dst, struct cdr_object_snapshot *src)
753 {
754         ao2_t_replace(dst->snapshot, src->snapshot, "CDR snapshot copy");
755         strcpy(dst->userfield, src->userfield);
756         dst->flags = src->flags;
757         copy_variables(&dst->variables, &src->variables);
758 }
759
760 /*!
761  * \brief Transition a \ref cdr_object to a new state
762  * \param cdr The \ref cdr_object to transition
763  * \param fn_table The \ref cdr_object_fn_table state to go to
764  */
765 static void cdr_object_transition_state(struct cdr_object *cdr, struct cdr_object_fn_table *fn_table)
766 {
767         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
768
769         CDR_DEBUG(mod_cfg, "%p - Transitioning CDR for %s from state %s to %s\n",
770                 cdr, cdr->party_a.snapshot->name,
771                 cdr->fn_table ? cdr->fn_table->name : "NONE", fn_table->name);
772         cdr->fn_table = fn_table;
773         if (cdr->fn_table->init_function) {
774                 cdr->fn_table->init_function(cdr);
775         }
776 }
777 /*! \internal
778  * \brief Hash function for containers of CDRs indexing by Party A uniqueid */
779 static int cdr_object_channel_hash_fn(const void *obj, const int flags)
780 {
781         const struct cdr_object *cdr;
782         const char *key;
783
784         switch (flags & OBJ_SEARCH_MASK) {
785         case OBJ_SEARCH_KEY:
786                 key = obj;
787                 break;
788         case OBJ_SEARCH_OBJECT:
789                 cdr = obj;
790                 key = cdr->uniqueid;
791                 break;
792         default:
793                 ast_assert(0);
794                 return 0;
795         }
796         return ast_str_case_hash(key);
797 }
798
799 /*! \internal
800  * \brief Comparison function for containers of CDRs indexing by Party A uniqueid
801  */
802 static int cdr_object_channel_cmp_fn(void *obj, void *arg, int flags)
803 {
804     struct cdr_object *left = obj;
805     struct cdr_object *right = arg;
806     const char *right_key = arg;
807     int cmp;
808
809     switch (flags & OBJ_SEARCH_MASK) {
810     case OBJ_SEARCH_OBJECT:
811         right_key = right->uniqueid;
812         /* Fall through */
813     case OBJ_SEARCH_KEY:
814         cmp = strcmp(left->uniqueid, right_key);
815         break;
816     case OBJ_SEARCH_PARTIAL_KEY:
817         /*
818          * We could also use a partial key struct containing a length
819          * so strlen() does not get called for every comparison instead.
820          */
821         cmp = strncmp(left->uniqueid, right_key, strlen(right_key));
822         break;
823     default:
824         /* Sort can only work on something with a full or partial key. */
825         ast_assert(0);
826         cmp = 0;
827         break;
828     }
829     return cmp ? 0 : CMP_MATCH;
830 }
831
832 /*!
833  * \brief \ref cdr_object Destructor
834  */
835 static void cdr_object_dtor(void *obj)
836 {
837         struct cdr_object *cdr = obj;
838         struct ast_var_t *it_var;
839
840         ao2_cleanup(cdr->party_a.snapshot);
841         ao2_cleanup(cdr->party_b.snapshot);
842         while ((it_var = AST_LIST_REMOVE_HEAD(&cdr->party_a.variables, entries))) {
843                 ast_var_delete(it_var);
844         }
845         while ((it_var = AST_LIST_REMOVE_HEAD(&cdr->party_b.variables, entries))) {
846                 ast_var_delete(it_var);
847         }
848         ast_string_field_free_memory(cdr);
849
850         /* CDR destruction used to work by calling ao2_cleanup(next) and
851          * allowing the chain to destroy itself neatly. Unfortunately, for
852          * really long chains, this can result in a stack overflow. So now
853          * when the root CDR is destroyed, it is responsible for unreffing
854          * all CDRs in the chain
855          */
856         if (cdr->is_root) {
857                 struct cdr_object *curr = cdr->next;
858                 struct cdr_object *next;
859
860                 while (curr) {
861                         next = curr->next;
862                         ao2_cleanup(curr);
863                         curr = next;
864                 }
865         }
866 }
867
868 /*!
869  * \brief \ref cdr_object constructor
870  * \param chan The \ref ast_channel_snapshot that is the CDR's Party A
871  *
872  * This implicitly sets the state of the newly created CDR to the Single state
873  * (\ref single_state_fn_table)
874  */
875 static struct cdr_object *cdr_object_alloc(struct ast_channel_snapshot *chan)
876 {
877         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
878         struct cdr_object *cdr;
879
880         ast_assert(chan != NULL);
881
882         cdr = ao2_alloc(sizeof(*cdr), cdr_object_dtor);
883         if (!cdr) {
884                 return NULL;
885         }
886         cdr->last = cdr;
887         if (ast_string_field_init(cdr, 64)) {
888                 ao2_cleanup(cdr);
889                 return NULL;
890         }
891         ast_string_field_set(cdr, uniqueid, chan->uniqueid);
892         ast_string_field_set(cdr, name, chan->name);
893         ast_string_field_set(cdr, linkedid, chan->linkedid);
894         cdr->disposition = AST_CDR_NULL;
895         cdr->sequence = ast_atomic_fetchadd_int(&global_cdr_sequence, +1);
896
897         cdr->party_a.snapshot = chan;
898         ao2_t_ref(cdr->party_a.snapshot, +1, "bump snapshot during CDR creation");
899
900         CDR_DEBUG(mod_cfg, "%p - Created CDR for channel %s\n", cdr, chan->name);
901
902         cdr_object_transition_state(cdr, &single_state_fn_table);
903
904         return cdr;
905 }
906
907 /*!
908  * \brief Create a new \ref cdr_object and append it to an existing chain
909  * \param cdr The \ref cdr_object to append to
910  */
911 static struct cdr_object *cdr_object_create_and_append(struct cdr_object *cdr)
912 {
913         struct cdr_object *new_cdr;
914         struct cdr_object *it_cdr;
915         struct cdr_object *cdr_last;
916
917         cdr_last = cdr->last;
918         new_cdr = cdr_object_alloc(cdr_last->party_a.snapshot);
919         if (!new_cdr) {
920                 return NULL;
921         }
922         new_cdr->disposition = AST_CDR_NULL;
923
924         /* Copy over the linkedid, as it may have changed */
925         ast_string_field_set(new_cdr, linkedid, cdr_last->linkedid);
926         ast_string_field_set(new_cdr, appl, cdr_last->appl);
927         ast_string_field_set(new_cdr, data, cdr_last->data);
928         ast_string_field_set(new_cdr, context, cdr_last->context);
929         ast_string_field_set(new_cdr, exten, cdr_last->exten);
930
931         /*
932          * If the current CDR says to disable all future ones,
933          * keep the disable chain going
934          */
935         if (ast_test_flag(&cdr_last->flags, AST_CDR_FLAG_DISABLE_ALL)) {
936                 ast_set_flag(&new_cdr->flags, AST_CDR_FLAG_DISABLE_ALL);
937         }
938
939         /* Copy over other Party A information */
940         cdr_object_snapshot_copy(&new_cdr->party_a, &cdr_last->party_a);
941
942         /* Append the CDR to the end of the list */
943         for (it_cdr = cdr; it_cdr->next; it_cdr = it_cdr->next) {
944                 it_cdr->last = new_cdr;
945         }
946         it_cdr->last = new_cdr;
947         it_cdr->next = new_cdr;
948
949         return new_cdr;
950 }
951
952 /*!
953  * \brief Return whether or not a channel has changed its state in the dialplan, subject
954  * to endbeforehexten logic
955  *
956  * \param old_snapshot The previous state
957  * \param new_snapshot The new state
958  *
959  * \retval 0 if the state has not changed
960  * \retval 1 if the state changed
961  */
962 static int snapshot_cep_changed(struct ast_channel_snapshot *old_snapshot,
963         struct ast_channel_snapshot *new_snapshot)
964 {
965         RAII_VAR(struct module_config *, mod_cfg,
966                 ao2_global_obj_ref(module_configs), ao2_cleanup);
967
968         /* If we ignore hangup logic, don't indicate that we're executing anything new */
969         if (ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)
970                 && ast_test_flag(&new_snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)) {
971                 return 0;
972         }
973
974         /* When Party A is originated to an application and the application exits, the stack
975          * will attempt to clear the application and restore the dummy originate application
976          * of "AppDialX". Ignore application changes to AppDialX as a result.
977          */
978         if (strcmp(new_snapshot->appl, old_snapshot->appl) && strncasecmp(new_snapshot->appl, "appdial", 7)
979                 && (strcmp(new_snapshot->context, old_snapshot->context)
980                 || strcmp(new_snapshot->exten, old_snapshot->exten)
981                 || new_snapshot->priority != old_snapshot->priority)) {
982                 return 1;
983         }
984
985         return 0;
986 }
987
988 /*!
989  * \brief Return whether or not a \ref ast_channel_snapshot is for a channel
990  * that was created as the result of a dial operation
991  *
992  * \retval 0 the channel was not created as the result of a dial
993  * \retval 1 the channel was created as the result of a dial
994  */
995 static int snapshot_is_dialed(struct ast_channel_snapshot *snapshot)
996 {
997         return (ast_test_flag(&snapshot->flags, AST_FLAG_OUTGOING)
998                         && !(ast_test_flag(&snapshot->flags, AST_FLAG_ORIGINATED)));
999 }
1000
1001 /*!
1002  * \brief Given two CDR snapshots, figure out who should be Party A for the
1003  * resulting CDR
1004  * \param left One of the snapshots
1005  * \param right The other snapshot
1006  * \retval The snapshot that won
1007  */
1008 static struct cdr_object_snapshot *cdr_object_pick_party_a(struct cdr_object_snapshot *left, struct cdr_object_snapshot *right)
1009 {
1010         /* Check whether or not the party is dialed. A dialed party is never the
1011          * Party A with a party that was not dialed.
1012          */
1013         if (!snapshot_is_dialed(left->snapshot) && snapshot_is_dialed(right->snapshot)) {
1014                 return left;
1015         } else if (snapshot_is_dialed(left->snapshot) && !snapshot_is_dialed(right->snapshot)) {
1016                 return right;
1017         }
1018
1019         /* Try the Party A flag */
1020         if (ast_test_flag(left, AST_CDR_FLAG_PARTY_A) && !ast_test_flag(right, AST_CDR_FLAG_PARTY_A)) {
1021                 return left;
1022         } else if (!ast_test_flag(right, AST_CDR_FLAG_PARTY_A) && ast_test_flag(right, AST_CDR_FLAG_PARTY_A)) {
1023                 return right;
1024         }
1025
1026         /* Neither party is dialed and neither has the Party A flag - defer to
1027          * creation time */
1028         if (left->snapshot->creationtime.tv_sec < right->snapshot->creationtime.tv_sec) {
1029                 return left;
1030         } else if (left->snapshot->creationtime.tv_sec > right->snapshot->creationtime.tv_sec) {
1031                 return right;
1032         } else if (left->snapshot->creationtime.tv_usec > right->snapshot->creationtime.tv_usec) {
1033                 return right;
1034         } else {
1035                 /* Okay, fine, take the left one */
1036                 return left;
1037         }
1038 }
1039
1040 /*!
1041  * Compute the duration for a \ref cdr_object
1042  */
1043 static long cdr_object_get_duration(struct cdr_object *cdr)
1044 {
1045         return (long)(ast_tvdiff_ms(ast_tvzero(cdr->end) ? ast_tvnow() : cdr->end, cdr->start) / 1000);
1046 }
1047
1048 /*!
1049  * \brief Compute the billsec for a \ref cdr_object
1050  */
1051 static long cdr_object_get_billsec(struct cdr_object *cdr)
1052 {
1053         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1054         long int ms;
1055
1056         if (ast_tvzero(cdr->answer)) {
1057                 return 0;
1058         }
1059         ms = ast_tvdiff_ms(ast_tvzero(cdr->end) ? ast_tvnow() : cdr->end, cdr->answer);
1060         if (ast_test_flag(&mod_cfg->general->settings, CDR_INITIATED_SECONDS)
1061                 && (ms % 1000 >= 500)) {
1062                 ms = (ms / 1000) + 1;
1063         } else {
1064                 ms = ms / 1000;
1065         }
1066
1067         return ms;
1068 }
1069
1070 /*!
1071  * \internal
1072  * \brief Set a variable on a CDR object
1073  *
1074  * \param headp The header pointer to the variable to set
1075  * \param name The name of the variable
1076  * \param value The value of the variable
1077  */
1078 static void set_variable(struct varshead *headp, const char *name, const char *value)
1079 {
1080         struct ast_var_t *newvariable;
1081
1082         AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) {
1083                 if (!strcasecmp(ast_var_name(newvariable), name)) {
1084                         AST_LIST_REMOVE_CURRENT(entries);
1085                         ast_var_delete(newvariable);
1086                         break;
1087                 }
1088         }
1089         AST_LIST_TRAVERSE_SAFE_END;
1090
1091         if (value && (newvariable = ast_var_assign(name, value))) {
1092                 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
1093         }
1094 }
1095
1096 /*!
1097  * \brief Create a chain of \ref ast_cdr objects from a chain of \ref cdr_object
1098  * suitable for consumption by the registered CDR backends
1099  * \param cdr The \ref cdr_object to convert to a public record
1100  * \retval A chain of \ref ast_cdr objects on success
1101  * \retval NULL on failure
1102  */
1103 static struct ast_cdr *cdr_object_create_public_records(struct cdr_object *cdr)
1104 {
1105         struct ast_cdr *pub_cdr = NULL, *cdr_prev = NULL;
1106         struct cdr_object *it_cdr;
1107         struct ast_var_t *it_var, *it_copy_var;
1108         struct ast_channel_snapshot *party_a;
1109         struct ast_channel_snapshot *party_b;
1110
1111         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
1112                 struct ast_cdr *cdr_copy;
1113
1114                 /* Don't create records for CDRs where the party A was a dialed channel */
1115                 if (snapshot_is_dialed(it_cdr->party_a.snapshot) && !it_cdr->party_b.snapshot) {
1116                         ast_debug(1, "CDR for %s is dialed and has no Party B; discarding\n",
1117                                 it_cdr->party_a.snapshot->name);
1118                         continue;
1119                 }
1120
1121                 cdr_copy = ast_calloc(1, sizeof(*cdr_copy));
1122                 if (!cdr_copy) {
1123                         ast_free(pub_cdr);
1124                         return NULL;
1125                 }
1126
1127                 party_a = it_cdr->party_a.snapshot;
1128                 party_b = it_cdr->party_b.snapshot;
1129
1130                 /* Party A */
1131                 ast_assert(party_a != NULL);
1132                 ast_copy_string(cdr_copy->accountcode, party_a->accountcode, sizeof(cdr_copy->accountcode));
1133                 cdr_copy->amaflags = party_a->amaflags;
1134                 ast_copy_string(cdr_copy->channel, party_a->name, sizeof(cdr_copy->channel));
1135                 ast_callerid_merge(cdr_copy->clid, sizeof(cdr_copy->clid), party_a->caller_name, party_a->caller_number, "");
1136                 ast_copy_string(cdr_copy->src, party_a->caller_number, sizeof(cdr_copy->src));
1137                 ast_copy_string(cdr_copy->uniqueid, party_a->uniqueid, sizeof(cdr_copy->uniqueid));
1138                 ast_copy_string(cdr_copy->lastapp, it_cdr->appl, sizeof(cdr_copy->lastapp));
1139                 ast_copy_string(cdr_copy->lastdata, it_cdr->data, sizeof(cdr_copy->lastdata));
1140                 ast_copy_string(cdr_copy->dst, it_cdr->exten, sizeof(cdr_copy->dst));
1141                 ast_copy_string(cdr_copy->dcontext, it_cdr->context, sizeof(cdr_copy->dcontext));
1142
1143                 /* Party B */
1144                 if (party_b) {
1145                         ast_copy_string(cdr_copy->dstchannel, party_b->name, sizeof(cdr_copy->dstchannel));
1146                         ast_copy_string(cdr_copy->peeraccount, party_b->accountcode, sizeof(cdr_copy->peeraccount));
1147                         if (!ast_strlen_zero(it_cdr->party_b.userfield)) {
1148                                 snprintf(cdr_copy->userfield, sizeof(cdr_copy->userfield), "%s;%s", it_cdr->party_a.userfield, it_cdr->party_b.userfield);
1149                         }
1150                 }
1151                 if (ast_strlen_zero(cdr_copy->userfield) && !ast_strlen_zero(it_cdr->party_a.userfield)) {
1152                         ast_copy_string(cdr_copy->userfield, it_cdr->party_a.userfield, sizeof(cdr_copy->userfield));
1153                 }
1154
1155                 /* Timestamps/durations */
1156                 cdr_copy->start = it_cdr->start;
1157                 cdr_copy->answer = it_cdr->answer;
1158                 cdr_copy->end = it_cdr->end;
1159                 cdr_copy->billsec = cdr_object_get_billsec(it_cdr);
1160                 cdr_copy->duration = cdr_object_get_duration(it_cdr);
1161
1162                 /* Flags and IDs */
1163                 ast_copy_flags(cdr_copy, &it_cdr->flags, AST_FLAGS_ALL);
1164                 ast_copy_string(cdr_copy->linkedid, it_cdr->linkedid, sizeof(cdr_copy->linkedid));
1165                 cdr_copy->disposition = it_cdr->disposition;
1166                 cdr_copy->sequence = it_cdr->sequence;
1167
1168                 /* Variables */
1169                 copy_variables(&cdr_copy->varshead, &it_cdr->party_a.variables);
1170                 AST_LIST_TRAVERSE(&it_cdr->party_b.variables, it_var, entries) {
1171                         int found = 0;
1172                         struct ast_var_t *newvariable;
1173                         AST_LIST_TRAVERSE(&cdr_copy->varshead, it_copy_var, entries) {
1174                                 if (!strcasecmp(ast_var_name(it_var), ast_var_name(it_copy_var))) {
1175                                         found = 1;
1176                                         break;
1177                                 }
1178                         }
1179                         if (!found && (newvariable = ast_var_assign(ast_var_name(it_var), ast_var_value(it_var)))) {
1180                                 AST_LIST_INSERT_TAIL(&cdr_copy->varshead, newvariable, entries);
1181                         }
1182                 }
1183
1184                 if (!pub_cdr) {
1185                         pub_cdr = cdr_copy;
1186                         cdr_prev = pub_cdr;
1187                 } else {
1188                         cdr_prev->next = cdr_copy;
1189                         cdr_prev = cdr_copy;
1190                 }
1191         }
1192
1193         return pub_cdr;
1194 }
1195
1196 /*!
1197  * \brief Dispatch a CDR.
1198  * \param cdr The \ref cdr_object to dispatch
1199  *
1200  * This will create a \ref ast_cdr object and publish it to the various backends
1201  */
1202 static void cdr_object_dispatch(struct cdr_object *cdr)
1203 {
1204         RAII_VAR(struct module_config *, mod_cfg,
1205                         ao2_global_obj_ref(module_configs), ao2_cleanup);
1206         struct ast_cdr *pub_cdr;
1207
1208         CDR_DEBUG(mod_cfg, "%p - Dispatching CDR for Party A %s, Party B %s\n", cdr,
1209                         cdr->party_a.snapshot->name,
1210                         cdr->party_b.snapshot ? cdr->party_b.snapshot->name : "<none>");
1211         pub_cdr = cdr_object_create_public_records(cdr);
1212         cdr_detach(pub_cdr);
1213 }
1214
1215 /*!
1216  * \brief Set the disposition on a \ref cdr_object based on a hangupcause code
1217  * \param cdr The \ref cdr_object
1218  * \param hangupcause The Asterisk hangup cause code
1219  */
1220 static void cdr_object_set_disposition(struct cdr_object *cdr, int hangupcause)
1221 {
1222         RAII_VAR(struct module_config *, mod_cfg,
1223                         ao2_global_obj_ref(module_configs), ao2_cleanup);
1224
1225         /* Change the disposition based on the hang up cause */
1226         switch (hangupcause) {
1227         case AST_CAUSE_BUSY:
1228                 cdr->disposition = AST_CDR_BUSY;
1229                 break;
1230         case AST_CAUSE_CONGESTION:
1231                 if (!ast_test_flag(&mod_cfg->general->settings, CDR_CONGESTION)) {
1232                         cdr->disposition = AST_CDR_FAILED;
1233                 } else {
1234                         cdr->disposition = AST_CDR_CONGESTION;
1235                 }
1236                 break;
1237         case AST_CAUSE_NO_ROUTE_DESTINATION:
1238         case AST_CAUSE_UNREGISTERED:
1239                 cdr->disposition = AST_CDR_FAILED;
1240                 break;
1241         case AST_CAUSE_NORMAL_CLEARING:
1242         case AST_CAUSE_NO_ANSWER:
1243                 cdr->disposition = AST_CDR_NOANSWER;
1244                 break;
1245         default:
1246                 break;
1247         }
1248 }
1249
1250 /*!
1251  * \brief Finalize a CDR.
1252  *
1253  * This function is safe to call multiple times. Note that you can call this
1254  * explicitly before going to the finalized state if there's a chance the CDR
1255  * will be re-activated, in which case the \ref cdr_object's end time should be
1256  * cleared. This function is implicitly called when a CDR transitions to the
1257  * finalized state and right before it is dispatched
1258  *
1259  * \param cdr_object The CDR to finalize
1260  */
1261 static void cdr_object_finalize(struct cdr_object *cdr)
1262 {
1263         if (!ast_tvzero(cdr->end)) {
1264                 return;
1265         }
1266         cdr->end = ast_tvnow();
1267
1268         if (cdr->disposition == AST_CDR_NULL) {
1269                 if (!ast_tvzero(cdr->answer)) {
1270                         cdr->disposition = AST_CDR_ANSWERED;
1271                 } else if (cdr->party_a.snapshot->hangupcause) {
1272                         cdr_object_set_disposition(cdr, cdr->party_a.snapshot->hangupcause);
1273                 } else if (cdr->party_b.snapshot && cdr->party_b.snapshot->hangupcause) {
1274                         cdr_object_set_disposition(cdr, cdr->party_b.snapshot->hangupcause);
1275                 } else {
1276                         cdr->disposition = AST_CDR_FAILED;
1277                 }
1278         }
1279
1280         /* tv_usec is suseconds_t, which could be int or long */
1281         ast_debug(1, "Finalized CDR for %s - start %ld.%06ld answer %ld.%06ld end %ld.%06ld dispo %s\n",
1282                         cdr->party_a.snapshot->name,
1283                         (long)cdr->start.tv_sec,
1284                         (long)cdr->start.tv_usec,
1285                         (long)cdr->answer.tv_sec,
1286                         (long)cdr->answer.tv_usec,
1287                         (long)cdr->end.tv_sec,
1288                         (long)cdr->end.tv_usec,
1289                         ast_cdr_disp2str(cdr->disposition));
1290 }
1291
1292 /*!
1293  * \brief Check to see if a CDR needs to move to the finalized state because
1294  * its Party A hungup.
1295  */
1296 static void cdr_object_check_party_a_hangup(struct cdr_object *cdr)
1297 {
1298         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1299
1300         if (ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)
1301                 && ast_test_flag(&cdr->party_a.snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)) {
1302                 cdr_object_finalize(cdr);
1303         }
1304
1305         if (ast_test_flag(&cdr->party_a.snapshot->flags, AST_FLAG_DEAD)
1306                 && cdr->fn_table != &finalized_state_fn_table) {
1307                 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1308         }
1309 }
1310
1311 /*!
1312  * \brief Check to see if a CDR needs to be answered based on its Party A.
1313  * Note that this is safe to call as much as you want - we won't answer twice
1314  */
1315 static void cdr_object_check_party_a_answer(struct cdr_object *cdr) {
1316         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1317
1318         if (cdr->party_a.snapshot->state == AST_STATE_UP && ast_tvzero(cdr->answer)) {
1319                 cdr->answer = ast_tvnow();
1320                 /* tv_usec is suseconds_t, which could be int or long */
1321                 CDR_DEBUG(mod_cfg, "%p - Set answered time to %ld.%06ld\n", cdr,
1322                         (long)cdr->answer.tv_sec,
1323                         (long)cdr->answer.tv_usec);
1324         }
1325 }
1326
1327 /* \brief Set Caller ID information on a CDR */
1328 static void cdr_object_update_cid(struct cdr_object_snapshot *old_snapshot, struct ast_channel_snapshot *new_snapshot)
1329 {
1330         if (!old_snapshot->snapshot) {
1331                 set_variable(&old_snapshot->variables, "dnid", new_snapshot->caller_dnid);
1332                 set_variable(&old_snapshot->variables, "callingsubaddr", new_snapshot->caller_subaddr);
1333                 set_variable(&old_snapshot->variables, "calledsubaddr", new_snapshot->dialed_subaddr);
1334                 return;
1335         }
1336         if (strcmp(old_snapshot->snapshot->caller_dnid, new_snapshot->caller_dnid)) {
1337                 set_variable(&old_snapshot->variables, "dnid", new_snapshot->caller_dnid);
1338         }
1339         if (strcmp(old_snapshot->snapshot->caller_subaddr, new_snapshot->caller_subaddr)) {
1340                 set_variable(&old_snapshot->variables, "callingsubaddr", new_snapshot->caller_subaddr);
1341         }
1342         if (strcmp(old_snapshot->snapshot->dialed_subaddr, new_snapshot->dialed_subaddr)) {
1343                 set_variable(&old_snapshot->variables, "calledsubaddr", new_snapshot->dialed_subaddr);
1344         }
1345 }
1346
1347 /*!
1348  * \brief Swap an old \ref cdr_object_snapshot's \ref ast_channel_snapshot for
1349  * a new \ref ast_channel_snapshot
1350  * \param old_snapshot The old \ref cdr_object_snapshot
1351  * \param new_snapshot The new \ref ast_channel_snapshot for old_snapshot
1352  */
1353 static void cdr_object_swap_snapshot(struct cdr_object_snapshot *old_snapshot,
1354                 struct ast_channel_snapshot *new_snapshot)
1355 {
1356         cdr_object_update_cid(old_snapshot, new_snapshot);
1357         ao2_t_replace(old_snapshot->snapshot, new_snapshot, "Swap CDR shapshot");
1358 }
1359
1360 /* BASE METHOD IMPLEMENTATIONS */
1361
1362 static int base_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1363 {
1364         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1365
1366         ast_assert(strcasecmp(snapshot->name, cdr->party_a.snapshot->name) == 0);
1367
1368         /* Finalize the CDR if we're in hangup logic and we're set to do so */
1369         if (ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)
1370                 && ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)) {
1371                 cdr_object_finalize(cdr);
1372                 return 0;
1373         }
1374
1375         /*
1376          * Only record the context and extension if we aren't in a subroutine, or if
1377          * we are executing hangup logic.
1378          */
1379         if (!ast_test_flag(&snapshot->flags, AST_FLAG_SUBROUTINE_EXEC)
1380                 || ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)) {
1381                 ast_string_field_set(cdr, context, snapshot->context);
1382                 ast_string_field_set(cdr, exten, snapshot->exten);
1383         }
1384
1385         cdr_object_swap_snapshot(&cdr->party_a, snapshot);
1386
1387         /* When Party A is originated to an application and the application exits, the stack
1388          * will attempt to clear the application and restore the dummy originate application
1389          * of "AppDialX". Prevent that, and any other application changes we might not want
1390          * here.
1391          */
1392         if (!ast_strlen_zero(snapshot->appl)
1393                         && (strncasecmp(snapshot->appl, "appdial", 7) || ast_strlen_zero(cdr->appl))
1394                         && !ast_test_flag(&cdr->flags, AST_CDR_LOCK_APP)) {
1395                 ast_string_field_set(cdr, appl, snapshot->appl);
1396                 ast_string_field_set(cdr, data, snapshot->data);
1397
1398                 /* Dial (app_dial) is a special case. Because pre-dial handlers, which
1399                  * execute before the dial begins, will alter the application/data to
1400                  * something people typically don't want to see, if we see a channel enter
1401                  * into Dial here, we set the appl/data accordingly and lock it.
1402                  */
1403                 if (!strcmp(snapshot->appl, "Dial")) {
1404                         ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP);
1405                 }
1406         }
1407
1408         ast_string_field_set(cdr, linkedid, snapshot->linkedid);
1409         cdr_object_check_party_a_answer(cdr);
1410         cdr_object_check_party_a_hangup(cdr);
1411
1412         return 0;
1413 }
1414
1415 static int base_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1416 {
1417         return 0;
1418 }
1419
1420 static int base_process_dial_end(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer, const char *dial_status)
1421 {
1422         return 0;
1423 }
1424
1425 static enum process_bridge_enter_results base_process_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1426 {
1427         /* Base process bridge enter simply indicates that we can't handle it */
1428         return BRIDGE_ENTER_NEED_CDR;
1429 }
1430
1431 static int base_process_parked_channel(struct cdr_object *cdr, struct ast_parked_call_payload *parking_info)
1432 {
1433         char park_info[128];
1434
1435         ast_assert(!strcasecmp(parking_info->parkee->name, cdr->party_a.snapshot->name));
1436
1437         /* Update Party A information regardless */
1438         cdr->fn_table->process_party_a(cdr, parking_info->parkee);
1439
1440         /* Fake out where we're parked */
1441         ast_string_field_set(cdr, appl, "Park");
1442         snprintf(park_info, sizeof(park_info), "%s:%u", parking_info->parkinglot, parking_info->parkingspace);
1443         ast_string_field_set(cdr, data, park_info);
1444
1445         /* Prevent any further changes to the App/Data fields for this record */
1446         ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP);
1447
1448         return 0;
1449 }
1450
1451 /* SINGLE STATE */
1452
1453 static void single_state_init_function(struct cdr_object *cdr)
1454 {
1455         cdr->start = ast_tvnow();
1456         cdr_object_check_party_a_answer(cdr);
1457 }
1458
1459 static void single_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1460 {
1461         /* This should never happen! */
1462         ast_assert(cdr->party_b.snapshot == NULL);
1463         ast_assert(0);
1464         return;
1465 }
1466
1467 static int single_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer)
1468 {
1469         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1470
1471         if (caller && !strcasecmp(cdr->party_a.snapshot->name, caller->name)) {
1472                 base_process_party_a(cdr, caller);
1473                 CDR_DEBUG(mod_cfg, "%p - Updated Party A %s snapshot\n", cdr,
1474                                 cdr->party_a.snapshot->name);
1475                 cdr_object_swap_snapshot(&cdr->party_b, peer);
1476                 CDR_DEBUG(mod_cfg, "%p - Updated Party B %s snapshot\n", cdr,
1477                                 cdr->party_b.snapshot->name);
1478
1479                 /* If we have two parties, lock the application that caused the
1480                  * two parties to be associated. This prevents mid-call event
1481                  * macros/gosubs from perturbing the CDR application/data
1482                  */
1483                 ast_set_flag(&cdr->flags, AST_CDR_LOCK_APP);
1484         } else if (!strcasecmp(cdr->party_a.snapshot->name, peer->name)) {
1485                 /* We're the entity being dialed, i.e., outbound origination */
1486                 base_process_party_a(cdr, peer);
1487                 CDR_DEBUG(mod_cfg, "%p - Updated Party A %s snapshot\n", cdr,
1488                                 cdr->party_a.snapshot->name);
1489         }
1490
1491         cdr_object_transition_state(cdr, &dial_state_fn_table);
1492         return 0;
1493 }
1494
1495 /*!
1496  * \brief Handle a comparison between our \ref cdr_object and a \ref cdr_object
1497  * already in the bridge while in the Single state. The goal of this is to find
1498  * a Party B for our CDR.
1499  *
1500  * \param cdr Our \ref cdr_object in the Single state
1501  * \param cand_cdr The \ref cdr_object already in the Bridge state
1502  *
1503  * \retval 0 The cand_cdr had a Party A or Party B that we could use as our
1504  * Party B
1505  * \retval 1 No party in the cand_cdr could be used as our Party B
1506  */
1507 static int single_state_bridge_enter_comparison(struct cdr_object *cdr,
1508                 struct cdr_object *cand_cdr)
1509 {
1510         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1511         struct cdr_object_snapshot *party_a;
1512
1513         /* Don't match on ourselves */
1514         if (!strcasecmp(cdr->party_a.snapshot->name, cand_cdr->party_a.snapshot->name)) {
1515                 return 1;
1516         }
1517
1518         /* Try the candidate CDR's Party A first */
1519         party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_a);
1520         if (!strcasecmp(party_a->snapshot->name, cdr->party_a.snapshot->name)) {
1521                 CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
1522                         cdr, cdr->party_a.snapshot->name, cand_cdr->party_a.snapshot->name);
1523                 cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_a);
1524                 if (!cand_cdr->party_b.snapshot) {
1525                         /* We just stole them - finalize their CDR. Note that this won't
1526                          * transition their state, it just sets the end time and the
1527                          * disposition - if we need to re-activate them later, we can.
1528                          */
1529                         cdr_object_finalize(cand_cdr);
1530                 }
1531                 return 0;
1532         }
1533
1534         /* Try their Party B, unless it's us */
1535         if (!cand_cdr->party_b.snapshot
1536                 || !strcasecmp(cdr->party_a.snapshot->name, cand_cdr->party_b.snapshot->name)) {
1537                 return 1;
1538         }
1539         party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_b);
1540         if (!strcasecmp(party_a->snapshot->name, cdr->party_a.snapshot->name)) {
1541                 CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
1542                         cdr, cdr->party_a.snapshot->name, cand_cdr->party_b.snapshot->name);
1543                 cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_b);
1544                 return 0;
1545         }
1546
1547         return 1;
1548 }
1549
1550 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)
1551 {
1552         struct ao2_iterator it_cdrs;
1553         char *channel_id;
1554         int success = 0;
1555
1556         ast_string_field_set(cdr, bridge, bridge->uniqueid);
1557
1558         if (ao2_container_count(bridge->channels) == 1) {
1559                 /* No one in the bridge yet but us! */
1560                 cdr_object_transition_state(cdr, &bridge_state_fn_table);
1561                 return BRIDGE_ENTER_ONLY_PARTY;
1562         }
1563
1564         for (it_cdrs = ao2_iterator_init(bridge->channels, 0);
1565                 !success && (channel_id = ao2_iterator_next(&it_cdrs));
1566                 ao2_ref(channel_id, -1)) {
1567                 struct cdr_object *cand_cdr_master;
1568                 struct cdr_object *cand_cdr;
1569
1570                 cand_cdr_master = ao2_find(active_cdrs_by_channel, channel_id, OBJ_SEARCH_KEY);
1571                 if (!cand_cdr_master) {
1572                         continue;
1573                 }
1574
1575                 ao2_lock(cand_cdr_master);
1576                 for (cand_cdr = cand_cdr_master; cand_cdr; cand_cdr = cand_cdr->next) {
1577                         /* Skip any records that are not in a bridge or in this bridge.
1578                          * I'm not sure how that would happen, but it pays to be careful. */
1579                         if (cand_cdr->fn_table != &bridge_state_fn_table ||
1580                                         strcmp(cdr->bridge, cand_cdr->bridge)) {
1581                                 continue;
1582                         }
1583
1584                         if (single_state_bridge_enter_comparison(cdr, cand_cdr)) {
1585                                 continue;
1586                         }
1587                         /* We successfully got a party B - break out */
1588                         success = 1;
1589                         break;
1590                 }
1591                 ao2_unlock(cand_cdr_master);
1592                 ao2_cleanup(cand_cdr_master);
1593         }
1594         ao2_iterator_destroy(&it_cdrs);
1595
1596         /* We always transition state, even if we didn't get a peer */
1597         cdr_object_transition_state(cdr, &bridge_state_fn_table);
1598
1599         /* Success implies that we have a Party B */
1600         if (success) {
1601                 return BRIDGE_ENTER_OBTAINED_PARTY_B;
1602         }
1603
1604         return BRIDGE_ENTER_NO_PARTY_B;
1605 }
1606
1607 static int single_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1608 {
1609         cdr_object_transition_state(cdr, &parked_state_fn_table);
1610         return 0;
1611 }
1612
1613
1614 /* DIAL STATE */
1615
1616 static void dial_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1617 {
1618         ast_assert(snapshot != NULL);
1619         ast_assert(cdr->party_b.snapshot
1620                 && !strcasecmp(cdr->party_b.snapshot->name, snapshot->name));
1621
1622         cdr_object_swap_snapshot(&cdr->party_b, snapshot);
1623
1624         /* If party B hangs up, finalize this CDR */
1625         if (ast_test_flag(&cdr->party_b.snapshot->flags, AST_FLAG_DEAD)) {
1626                 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1627         }
1628 }
1629
1630 static int dial_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer)
1631 {
1632         /* Don't process a begin dial here. A party A already in the dial state will
1633          * who receives a dial begin for something else will be handled by the
1634          * message router callback and will add a new CDR for the party A */
1635         return 1;
1636 }
1637
1638 /*!
1639  * \internal
1640  * \brief Convert a dial status to a CDR disposition
1641  */
1642 static enum ast_cdr_disposition dial_status_to_disposition(const char *dial_status)
1643 {
1644         RAII_VAR(struct module_config *, mod_cfg,
1645                 ao2_global_obj_ref(module_configs), ao2_cleanup);
1646
1647         if (!strcmp(dial_status, "ANSWER")) {
1648                 return AST_CDR_ANSWERED;
1649         } else if (!strcmp(dial_status, "BUSY")) {
1650                 return AST_CDR_BUSY;
1651         } else if (!strcmp(dial_status, "CANCEL") || !strcmp(dial_status, "NOANSWER")) {
1652                 return AST_CDR_NOANSWER;
1653         } else if (!strcmp(dial_status, "CONGESTION")) {
1654                 if (!ast_test_flag(&mod_cfg->general->settings, CDR_CONGESTION)) {
1655                         return AST_CDR_FAILED;
1656                 } else {
1657                         return AST_CDR_CONGESTION;
1658                 }
1659         } else if (!strcmp(dial_status, "FAILED")) {
1660                 return AST_CDR_FAILED;
1661         }
1662         return AST_CDR_FAILED;
1663 }
1664
1665 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)
1666 {
1667         struct ast_channel_snapshot *party_a;
1668
1669         if (caller) {
1670                 party_a = caller;
1671         } else {
1672                 party_a = peer;
1673         }
1674         ast_assert(!strcasecmp(cdr->party_a.snapshot->name, party_a->name));
1675         cdr_object_swap_snapshot(&cdr->party_a, party_a);
1676
1677         if (cdr->party_b.snapshot) {
1678                 if (strcasecmp(cdr->party_b.snapshot->name, peer->name)) {
1679                         /* Not the status for this CDR - defer back to the message router */
1680                         return 1;
1681                 }
1682                 cdr_object_swap_snapshot(&cdr->party_b, peer);
1683         }
1684
1685         /* Set the disposition based on the dial string. */
1686         cdr->disposition = dial_status_to_disposition(dial_status);
1687         if (cdr->disposition == AST_CDR_ANSWERED) {
1688                 /* Switch to dial pending to wait and see what the caller does */
1689                 cdr_object_transition_state(cdr, &dialed_pending_state_fn_table);
1690         } else {
1691                 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1692         }
1693
1694         return 0;
1695 }
1696
1697 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)
1698 {
1699         struct ao2_iterator it_cdrs;
1700         char *channel_id;
1701         int success = 0;
1702
1703         ast_string_field_set(cdr, bridge, bridge->uniqueid);
1704
1705         /* Get parties in the bridge */
1706         if (ao2_container_count(bridge->channels) == 1) {
1707                 /* No one in the bridge yet but us! */
1708                 cdr_object_transition_state(cdr, &bridge_state_fn_table);
1709                 return BRIDGE_ENTER_ONLY_PARTY;
1710         }
1711
1712         for (it_cdrs = ao2_iterator_init(bridge->channels, 0);
1713                 !success && (channel_id = ao2_iterator_next(&it_cdrs));
1714                 ao2_ref(channel_id, -1)) {
1715                 struct cdr_object *cand_cdr_master;
1716                 struct cdr_object *cand_cdr;
1717
1718                 cand_cdr_master = ao2_find(active_cdrs_by_channel, channel_id, OBJ_SEARCH_KEY);
1719                 if (!cand_cdr_master) {
1720                         continue;
1721                 }
1722
1723                 ao2_lock(cand_cdr_master);
1724                 for (cand_cdr = cand_cdr_master; cand_cdr; cand_cdr = cand_cdr->next) {
1725                         /* Skip any records that are not in a bridge or in this bridge.
1726                          * I'm not sure how that would happen, but it pays to be careful. */
1727                         if (cand_cdr->fn_table != &bridge_state_fn_table ||
1728                                         strcmp(cdr->bridge, cand_cdr->bridge)) {
1729                                 continue;
1730                         }
1731
1732                         /* If we don't have a Party B (originated channel), skip it */
1733                         if (!cdr->party_b.snapshot) {
1734                                 continue;
1735                         }
1736
1737                         /* Skip any records that aren't our Party B */
1738                         if (strcasecmp(cdr->party_b.snapshot->name, cand_cdr->party_a.snapshot->name)) {
1739                                 continue;
1740                         }
1741                         cdr_object_snapshot_copy(&cdr->party_b, &cand_cdr->party_a);
1742                         /* If they have a Party B, they joined up with someone else as their
1743                          * Party A. Don't finalize them as they're active. Otherwise, we
1744                          * have stolen them so they need to be finalized.
1745                          */
1746                         if (!cand_cdr->party_b.snapshot) {
1747                                 cdr_object_finalize(cand_cdr);
1748                         }
1749                         success = 1;
1750                         break;
1751                 }
1752                 ao2_unlock(cand_cdr_master);
1753                 ao2_cleanup(cand_cdr_master);
1754         }
1755         ao2_iterator_destroy(&it_cdrs);
1756
1757         /* We always transition state, even if we didn't get a peer */
1758         cdr_object_transition_state(cdr, &bridge_state_fn_table);
1759
1760         /* Success implies that we have a Party B */
1761         if (success) {
1762                 return BRIDGE_ENTER_OBTAINED_PARTY_B;
1763         }
1764         return BRIDGE_ENTER_NO_PARTY_B;
1765 }
1766
1767 /* DIALED PENDING STATE */
1768
1769 static int dialed_pending_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1770 {
1771         /* If we get a CEP change, we're executing dialplan. If we have a Party B
1772          * that means we need a new CDR; otherwise, switch us over to single.
1773          */
1774         if (snapshot_cep_changed(cdr->party_a.snapshot, snapshot)) {
1775                 if (cdr->party_b.snapshot) {
1776                         cdr_object_transition_state(cdr, &finalized_state_fn_table);
1777                         cdr->fn_table->process_party_a(cdr, snapshot);
1778                         return 1;
1779                 } else {
1780                         cdr_object_transition_state(cdr, &single_state_fn_table);
1781                         cdr->fn_table->process_party_a(cdr, snapshot);
1782                         return 0;
1783                 }
1784         }
1785         base_process_party_a(cdr, snapshot);
1786         return 0;
1787 }
1788
1789 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)
1790 {
1791         cdr_object_transition_state(cdr, &dial_state_fn_table);
1792         return cdr->fn_table->process_bridge_enter(cdr, bridge, channel);
1793 }
1794
1795 static int dialed_pending_state_process_parking_bridge_enter(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1796 {
1797         if (cdr->party_b.snapshot) {
1798                 /* We can't handle this as we have a Party B - ask for a new one */
1799                 return 1;
1800         }
1801         cdr_object_transition_state(cdr, &parked_state_fn_table);
1802         return 0;
1803 }
1804
1805 static int dialed_pending_state_process_dial_begin(struct cdr_object *cdr, struct ast_channel_snapshot *caller, struct ast_channel_snapshot *peer)
1806 {
1807         cdr_object_transition_state(cdr, &finalized_state_fn_table);
1808
1809         /* Ask for a new CDR */
1810         return 1;
1811 }
1812
1813 /* BRIDGE STATE */
1814
1815 static void bridge_state_process_party_b(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1816 {
1817         ast_assert(cdr->party_b.snapshot
1818                 && !strcasecmp(cdr->party_b.snapshot->name, snapshot->name));
1819
1820         cdr_object_swap_snapshot(&cdr->party_b, snapshot);
1821
1822         /* If party B hangs up, finalize this CDR */
1823         if (ast_test_flag(&cdr->party_b.snapshot->flags, AST_FLAG_DEAD)) {
1824                 cdr_object_transition_state(cdr, &finalized_state_fn_table);
1825         }
1826 }
1827
1828 static int bridge_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1829 {
1830         if (strcmp(cdr->bridge, bridge->uniqueid)) {
1831                 return 1;
1832         }
1833         if (strcasecmp(cdr->party_a.snapshot->name, channel->name)
1834                 && cdr->party_b.snapshot
1835                 && strcasecmp(cdr->party_b.snapshot->name, channel->name)) {
1836                 return 1;
1837         }
1838         cdr_object_transition_state(cdr, &finalized_state_fn_table);
1839
1840         return 0;
1841 }
1842
1843 /* PARKED STATE */
1844
1845 static int parked_state_process_bridge_leave(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge, struct ast_channel_snapshot *channel)
1846 {
1847         if (strcasecmp(cdr->party_a.snapshot->name, channel->name)) {
1848                 return 1;
1849         }
1850         cdr_object_transition_state(cdr, &finalized_state_fn_table);
1851
1852         return 0;
1853 }
1854
1855 /* FINALIZED STATE */
1856
1857 static void finalized_state_init_function(struct cdr_object *cdr)
1858 {
1859         cdr_object_finalize(cdr);
1860 }
1861
1862 static int finalized_state_process_party_a(struct cdr_object *cdr, struct ast_channel_snapshot *snapshot)
1863 {
1864         RAII_VAR(struct module_config *, mod_cfg,
1865                 ao2_global_obj_ref(module_configs), ao2_cleanup);
1866
1867         if (ast_test_flag(&snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)
1868                         && ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN)) {
1869                 return 0;
1870         }
1871
1872         /* Indicate that, if possible, we should get a new CDR */
1873         return 1;
1874 }
1875
1876 /*!
1877  * \internal
1878  * \brief Filter channel snapshots by technology
1879  */
1880 static int filter_channel_snapshot(struct ast_channel_snapshot *snapshot)
1881 {
1882         return snapshot->tech_properties & AST_CHAN_TP_INTERNAL;
1883 }
1884
1885 /*!
1886  * \internal
1887  * \brief Filter a channel cache update
1888  */
1889 static int filter_channel_cache_message(struct ast_channel_snapshot *old_snapshot,
1890                 struct ast_channel_snapshot *new_snapshot)
1891 {
1892         int ret = 0;
1893
1894         /* Drop cache updates from certain channel technologies */
1895         if (old_snapshot) {
1896                 ret |= filter_channel_snapshot(old_snapshot);
1897         }
1898         if (new_snapshot) {
1899                 ret |= filter_channel_snapshot(new_snapshot);
1900         }
1901
1902         return ret;
1903 }
1904
1905 static int dial_status_end(const char *dialstatus)
1906 {
1907         return (strcmp(dialstatus, "RINGING") &&
1908                         strcmp(dialstatus, "PROCEEDING") &&
1909                         strcmp(dialstatus, "PROGRESS"));
1910 }
1911
1912 /* TOPIC ROUTER CALLBACKS */
1913
1914 /*!
1915  * \brief Handler for Stasis-Core dial messages
1916  * \param data Passed on
1917  * \param sub The stasis subscription for this message callback
1918  * \param topic The topic this message was published for
1919  * \param message The message
1920  */
1921 static void handle_dial_message(void *data, struct stasis_subscription *sub, struct stasis_message *message)
1922 {
1923         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
1924         struct cdr_object *cdr;
1925         struct ast_multi_channel_blob *payload = stasis_message_data(message);
1926         struct ast_channel_snapshot *caller;
1927         struct ast_channel_snapshot *peer;
1928         struct cdr_object *it_cdr;
1929         struct ast_json *dial_status_blob;
1930         const char *dial_status = NULL;
1931         int res = 1;
1932
1933         caller = ast_multi_channel_blob_get_channel(payload, "caller");
1934         peer = ast_multi_channel_blob_get_channel(payload, "peer");
1935         if (!peer && !caller) {
1936                 return;
1937         }
1938         if (filter_channel_snapshot(peer) || (caller && filter_channel_snapshot(caller))) {
1939                 return;
1940         }
1941
1942         dial_status_blob = ast_json_object_get(ast_multi_channel_blob_get_json(payload), "dialstatus");
1943         if (dial_status_blob) {
1944                 dial_status = ast_json_string_get(dial_status_blob);
1945         }
1946
1947         CDR_DEBUG(mod_cfg, "Dial %s message for %s, %s: %u.%08u\n",
1948                         ast_strlen_zero(dial_status) ? "Begin" : "End",
1949                         caller ? caller->name : "(none)",
1950                         peer ? peer->name : "(none)",
1951                         (unsigned int)stasis_message_timestamp(message)->tv_sec,
1952                         (unsigned int)stasis_message_timestamp(message)->tv_usec);
1953
1954         /* Figure out who is running this show */
1955         if (caller) {
1956                 cdr = ao2_find(active_cdrs_by_channel, caller->uniqueid, OBJ_SEARCH_KEY);
1957         } else {
1958                 cdr = ao2_find(active_cdrs_by_channel, peer->uniqueid, OBJ_SEARCH_KEY);
1959         }
1960         if (!cdr) {
1961                 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", caller ? caller->name : peer->name);
1962                 ast_assert(0);
1963                 return;
1964         }
1965
1966         ao2_lock(cdr);
1967         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
1968                 if (ast_strlen_zero(dial_status)) {
1969                         if (!it_cdr->fn_table->process_dial_begin) {
1970                                 continue;
1971                         }
1972                         CDR_DEBUG(mod_cfg, "%p - Processing Dial Begin message for channel %s, peer %s\n",
1973                                         it_cdr,
1974                                         caller ? caller->name : "(none)",
1975                                         peer ? peer->name : "(none)");
1976                         res &= it_cdr->fn_table->process_dial_begin(it_cdr,
1977                                         caller,
1978                                         peer);
1979                 } else if (dial_status_end(dial_status)) {
1980                         if (!it_cdr->fn_table->process_dial_end) {
1981                                 continue;
1982                         }
1983                         CDR_DEBUG(mod_cfg, "%p - Processing Dial End message for channel %s, peer %s\n",
1984                                         it_cdr,
1985                                         caller ? caller->name : "(none)",
1986                                         peer ? peer->name : "(none)");
1987                         it_cdr->fn_table->process_dial_end(it_cdr,
1988                                         caller,
1989                                         peer,
1990                                         dial_status);
1991                 }
1992         }
1993
1994         /* If no CDR handled a dial begin message, make a new one */
1995         if (res && ast_strlen_zero(dial_status)) {
1996                 struct cdr_object *new_cdr;
1997
1998                 new_cdr = cdr_object_create_and_append(cdr);
1999                 if (new_cdr) {
2000                         new_cdr->fn_table->process_dial_begin(new_cdr, caller, peer);
2001                 }
2002         }
2003         ao2_unlock(cdr);
2004         ao2_cleanup(cdr);
2005 }
2006
2007 static int cdr_object_finalize_party_b(void *obj, void *arg, int flags)
2008 {
2009         struct cdr_object *cdr = obj;
2010         struct ast_channel_snapshot *party_b = arg;
2011         struct cdr_object *it_cdr;
2012
2013         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2014                 if (it_cdr->party_b.snapshot
2015                         && !strcasecmp(it_cdr->party_b.snapshot->name, party_b->name)) {
2016                         /* Don't transition to the finalized state - let the Party A do
2017                          * that when its ready
2018                          */
2019                         cdr_object_finalize(it_cdr);
2020                 }
2021         }
2022         return 0;
2023 }
2024
2025 static int cdr_object_update_party_b(void *obj, void *arg, int flags)
2026 {
2027         struct cdr_object *cdr = obj;
2028         struct ast_channel_snapshot *party_b = arg;
2029         struct cdr_object *it_cdr;
2030
2031         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2032                 if (!it_cdr->fn_table->process_party_b) {
2033                         continue;
2034                 }
2035                 if (it_cdr->party_b.snapshot
2036                         && !strcasecmp(it_cdr->party_b.snapshot->name, party_b->name)) {
2037                         it_cdr->fn_table->process_party_b(it_cdr, party_b);
2038                 }
2039         }
2040         return 0;
2041 }
2042
2043 /*! \brief Determine if we need to add a new CDR based on snapshots */
2044 static int check_new_cdr_needed(struct ast_channel_snapshot *old_snapshot,
2045                 struct ast_channel_snapshot *new_snapshot)
2046 {
2047         RAII_VAR(struct module_config *, mod_cfg,
2048                         ao2_global_obj_ref(module_configs), ao2_cleanup);
2049
2050         /* If we're dead, we don't need a new CDR */
2051         if (!new_snapshot
2052                 || (ast_test_flag(&new_snapshot->softhangup_flags, AST_SOFTHANGUP_HANGUP_EXEC)
2053                         && ast_test_flag(&mod_cfg->general->settings, CDR_END_BEFORE_H_EXTEN))) {
2054                 return 0;
2055         }
2056
2057         /* Auto-fall through will increment the priority but have no application */
2058         if (ast_strlen_zero(new_snapshot->appl)) {
2059                 return 0;
2060         }
2061
2062         if (old_snapshot && !snapshot_cep_changed(old_snapshot, new_snapshot)) {
2063                 return 0;
2064         }
2065
2066         return 1;
2067 }
2068
2069 /*!
2070  * \brief Handler for Stasis-Core channel cache update messages
2071  * \param data Passed on
2072  * \param sub The stasis subscription for this message callback
2073  * \param topic The topic this message was published for
2074  * \param message The message
2075  */
2076 static void handle_channel_cache_message(void *data, struct stasis_subscription *sub, struct stasis_message *message)
2077 {
2078         struct cdr_object *cdr;
2079         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
2080         struct stasis_cache_update *update = stasis_message_data(message);
2081         struct ast_channel_snapshot *old_snapshot;
2082         struct ast_channel_snapshot *new_snapshot;
2083         struct cdr_object *it_cdr;
2084
2085         ast_assert(update != NULL);
2086         ast_assert(ast_channel_snapshot_type() == update->type);
2087
2088         old_snapshot = stasis_message_data(update->old_snapshot);
2089         new_snapshot = stasis_message_data(update->new_snapshot);
2090
2091         if (filter_channel_cache_message(old_snapshot, new_snapshot)) {
2092                 return;
2093         }
2094
2095         if (new_snapshot && !old_snapshot) {
2096                 cdr = cdr_object_alloc(new_snapshot);
2097                 if (!cdr) {
2098                         return;
2099                 }
2100                 cdr->is_root = 1;
2101                 ao2_link(active_cdrs_by_channel, cdr);
2102         } else {
2103                 const char *uniqueid;
2104
2105                 uniqueid = new_snapshot ? new_snapshot->uniqueid : old_snapshot->uniqueid;
2106                 cdr = ao2_find(active_cdrs_by_channel, uniqueid, OBJ_SEARCH_KEY);
2107         }
2108
2109         /* Handle Party A */
2110         if (!cdr) {
2111                 const char *name;
2112
2113                 name = new_snapshot ? new_snapshot->name : old_snapshot->name;
2114                 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", name);
2115                 ast_assert(0);
2116         } else {
2117                 ao2_lock(cdr);
2118                 if (new_snapshot) {
2119                         int all_reject = 1;
2120
2121                         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2122                                 if (!it_cdr->fn_table->process_party_a) {
2123                                         continue;
2124                                 }
2125                                 all_reject &= it_cdr->fn_table->process_party_a(it_cdr, new_snapshot);
2126                         }
2127                         if (all_reject && check_new_cdr_needed(old_snapshot, new_snapshot)) {
2128                                 /* We're not hung up and we have a new snapshot - we need a new CDR */
2129                                 struct cdr_object *new_cdr;
2130
2131                                 new_cdr = cdr_object_create_and_append(cdr);
2132                                 if (new_cdr) {
2133                                         new_cdr->fn_table->process_party_a(new_cdr, new_snapshot);
2134                                 }
2135                         }
2136                 } else {
2137                         CDR_DEBUG(mod_cfg, "%p - Beginning finalize/dispatch for %s\n", cdr, old_snapshot->name);
2138                         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2139                                 cdr_object_finalize(it_cdr);
2140                         }
2141                         cdr_object_dispatch(cdr);
2142                         ao2_unlink(active_cdrs_by_channel, cdr);
2143                 }
2144                 ao2_unlock(cdr);
2145         }
2146
2147         /* Handle Party B */
2148         if (new_snapshot) {
2149                 ao2_callback(active_cdrs_by_channel, OBJ_NODATA, cdr_object_update_party_b,
2150                         new_snapshot);
2151         } else {
2152                 ao2_callback(active_cdrs_by_channel, OBJ_NODATA, cdr_object_finalize_party_b,
2153                         old_snapshot);
2154         }
2155
2156         ao2_cleanup(cdr);
2157 }
2158
2159 struct bridge_leave_data {
2160         struct ast_bridge_snapshot *bridge;
2161         struct ast_channel_snapshot *channel;
2162 };
2163
2164 /*! \brief Callback used to notify CDRs of a Party B leaving the bridge */
2165 static int cdr_object_party_b_left_bridge_cb(void *obj, void *arg, int flags)
2166 {
2167         struct cdr_object *cdr = obj;
2168         struct bridge_leave_data *leave_data = arg;
2169         struct cdr_object *it_cdr;
2170
2171         if (strcmp(cdr->bridge, leave_data->bridge->uniqueid)) {
2172                 return 0;
2173         }
2174         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2175                 if (it_cdr->fn_table != &bridge_state_fn_table) {
2176                         continue;
2177                 }
2178                 if (!it_cdr->party_b.snapshot) {
2179                         continue;
2180                 }
2181                 if (strcasecmp(it_cdr->party_b.snapshot->name, leave_data->channel->name)) {
2182                         continue;
2183                 }
2184                 /* It is our Party B, in our bridge. Set the end time and let the handler
2185                  * transition our CDR appropriately when we leave the bridge.
2186                  */
2187                 cdr_object_finalize(it_cdr);
2188         }
2189         return 0;
2190 }
2191
2192 /*! \brief Filter bridge messages based on bridge technology */
2193 static int filter_bridge_messages(struct ast_bridge_snapshot *bridge)
2194 {
2195         /* Ignore holding bridge technology messages. We treat this simply as an application
2196          * that a channel enters into.
2197          */
2198         if (!strcmp(bridge->technology, "holding_bridge") && strcmp(bridge->subclass, "parking")) {
2199                 return 1;
2200         }
2201         return 0;
2202 }
2203
2204 /*!
2205  * \brief Handler for when a channel leaves a bridge
2206  * \param data Passed on
2207  * \param sub The stasis subscription for this message callback
2208  * \param topic The topic this message was published for
2209  * \param message The message - hopefully a bridge one!
2210  */
2211 static void handle_bridge_leave_message(void *data, struct stasis_subscription *sub,
2212                 struct stasis_message *message)
2213 {
2214         struct ast_bridge_blob *update = stasis_message_data(message);
2215         struct ast_bridge_snapshot *bridge = update->bridge;
2216         struct ast_channel_snapshot *channel = update->channel;
2217         RAII_VAR(struct module_config *, mod_cfg,
2218                         ao2_global_obj_ref(module_configs), ao2_cleanup);
2219         struct cdr_object *cdr;
2220         struct cdr_object *it_cdr;
2221         struct bridge_leave_data leave_data = {
2222                 .bridge = bridge,
2223                 .channel = channel,
2224         };
2225         int left_bridge = 0;
2226
2227         if (filter_bridge_messages(bridge)) {
2228                 return;
2229         }
2230
2231         if (filter_channel_snapshot(channel)) {
2232                 return;
2233         }
2234
2235         CDR_DEBUG(mod_cfg, "Bridge Leave message for %s: %u.%08u\n",
2236                         channel->name,
2237                         (unsigned int)stasis_message_timestamp(message)->tv_sec,
2238                         (unsigned int)stasis_message_timestamp(message)->tv_usec);
2239
2240         cdr = ao2_find(active_cdrs_by_channel, channel->uniqueid, OBJ_SEARCH_KEY);
2241         if (!cdr) {
2242                 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->name);
2243                 ast_assert(0);
2244                 return;
2245         }
2246
2247         /* Party A */
2248         ao2_lock(cdr);
2249         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2250                 if (!it_cdr->fn_table->process_bridge_leave) {
2251                         continue;
2252                 }
2253                 CDR_DEBUG(mod_cfg, "%p - Processing Bridge Leave for %s\n",
2254                                 it_cdr, channel->name);
2255                 if (!it_cdr->fn_table->process_bridge_leave(it_cdr, bridge, channel)) {
2256                         ast_string_field_set(it_cdr, bridge, "");
2257                         left_bridge = 1;
2258                 }
2259         }
2260         ao2_unlock(cdr);
2261
2262         /* Party B */
2263         if (left_bridge
2264                 && strcmp(bridge->subclass, "parking")) {
2265                 ao2_callback(active_cdrs_by_channel, OBJ_NODATA,
2266                         cdr_object_party_b_left_bridge_cb,
2267                         &leave_data);
2268         }
2269
2270         ao2_cleanup(cdr);
2271 }
2272
2273 /*!
2274  * \internal
2275  * \brief Create a new CDR, append it to an existing CDR, and update its snapshots
2276  *
2277  * \note The new CDR will be automatically transitioned to the bridge state
2278  */
2279 static void bridge_candidate_add_to_cdr(struct cdr_object *cdr,
2280                 struct cdr_object_snapshot *party_b)
2281 {
2282         RAII_VAR(struct module_config *,  mod_cfg,
2283                 ao2_global_obj_ref(module_configs), ao2_cleanup);
2284         struct cdr_object *new_cdr;
2285
2286         new_cdr = cdr_object_create_and_append(cdr);
2287         if (!new_cdr) {
2288                 return;
2289         }
2290         cdr_object_snapshot_copy(&new_cdr->party_b, party_b);
2291         cdr_object_check_party_a_answer(new_cdr);
2292         ast_string_field_set(new_cdr, bridge, cdr->bridge);
2293         cdr_object_transition_state(new_cdr, &bridge_state_fn_table);
2294         CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
2295                 new_cdr, new_cdr->party_a.snapshot->name,
2296                 party_b->snapshot->name);
2297 }
2298
2299 /*!
2300  * \brief Process a single \ref bridge_candidate
2301  *
2302  * When a CDR enters a bridge, it needs to make pairings with everyone else
2303  * that it is not currently paired with. This function determines, for the
2304  * CDR for the channel that entered the bridge and the CDR for every other
2305  * channel currently in the bridge, who is Party A and makes new CDRs.
2306  *
2307  * \param cdr The \ref cdr_obj being processed
2308  * \param cand_cdr The \ref cdr_object that is a candidate
2309  *
2310  */
2311 static int bridge_candidate_process(struct cdr_object *cdr, struct cdr_object *base_cand_cdr)
2312 {
2313         RAII_VAR(struct module_config *, mod_cfg,
2314                 ao2_global_obj_ref(module_configs), ao2_cleanup);
2315         struct cdr_object_snapshot *party_a;
2316         struct cdr_object *cand_cdr;
2317
2318         SCOPED_AO2LOCK(lock, base_cand_cdr);
2319
2320         for (cand_cdr = base_cand_cdr; cand_cdr; cand_cdr = cand_cdr->next) {
2321                 /* Skip any records that are not in this bridge */
2322                 if (strcmp(cand_cdr->bridge, cdr->bridge)) {
2323                         continue;
2324                 }
2325
2326                 /* If the candidate is us or someone we've taken on, pass on by */
2327                 if (!strcasecmp(cdr->party_a.snapshot->name, cand_cdr->party_a.snapshot->name)
2328                         || (cdr->party_b.snapshot
2329                                 && !strcasecmp(cdr->party_b.snapshot->name, cand_cdr->party_a.snapshot->name))) {
2330                         return 0;
2331                 }
2332
2333                 party_a = cdr_object_pick_party_a(&cdr->party_a, &cand_cdr->party_a);
2334                 /* We're party A - make a new CDR, append it to us, and set the candidate as
2335                  * Party B */
2336                 if (!strcasecmp(party_a->snapshot->name, cdr->party_a.snapshot->name)) {
2337                         bridge_candidate_add_to_cdr(cdr, &cand_cdr->party_a);
2338                         return 0;
2339                 }
2340
2341                 /* We're Party B. Check if we can add ourselves immediately or if we need
2342                  * a new CDR for them (they already have a Party B) */
2343                 if (cand_cdr->party_b.snapshot
2344                         && strcasecmp(cand_cdr->party_b.snapshot->name, cdr->party_a.snapshot->name)) {
2345                         bridge_candidate_add_to_cdr(cand_cdr, &cdr->party_a);
2346                 } else {
2347                         CDR_DEBUG(mod_cfg, "%p - Party A %s has new Party B %s\n",
2348                                 cand_cdr, cand_cdr->party_a.snapshot->name,
2349                                 cdr->party_a.snapshot->name);
2350                         cdr_object_snapshot_copy(&cand_cdr->party_b, &cdr->party_a);
2351                         /* It's possible that this joined at one point and was never chosen
2352                          * as party A. Clear their end time, as it would be set in such a
2353                          * case.
2354                          */
2355                         memset(&cand_cdr->end, 0, sizeof(cand_cdr->end));
2356                 }
2357                 return 0;
2358         }
2359         return 0;
2360 }
2361
2362 /*!
2363  * \brief Handle creating bridge pairings for the \ref cdr_object that just
2364  * entered a bridge
2365  * \param cdr The \ref cdr_object that just entered the bridge
2366  * \param bridge The \ref ast_bridge_snapshot representing the bridge it just entered
2367  */
2368 static void handle_bridge_pairings(struct cdr_object *cdr, struct ast_bridge_snapshot *bridge)
2369 {
2370         struct ao2_iterator it_channels;
2371         char *channel_id;
2372
2373         it_channels = ao2_iterator_init(bridge->channels, 0);
2374         while ((channel_id = ao2_iterator_next(&it_channels))) {
2375                 struct cdr_object *cand_cdr;
2376
2377                 cand_cdr = ao2_find(active_cdrs_by_channel, channel_id, OBJ_SEARCH_KEY);
2378                 if (cand_cdr) {
2379                         bridge_candidate_process(cdr, cand_cdr);
2380                         ao2_ref(cand_cdr, -1);
2381                 }
2382
2383                 ao2_ref(channel_id, -1);
2384         }
2385         ao2_iterator_destroy(&it_channels);
2386 }
2387
2388 /*! \brief Handle entering into a parking bridge
2389  * \param cdr The CDR to operate on
2390  * \param bridge The bridge the channel just entered
2391  * \param channel The channel snapshot
2392  */
2393 static void handle_parking_bridge_enter_message(struct cdr_object *cdr,
2394                 struct ast_bridge_snapshot *bridge,
2395                 struct ast_channel_snapshot *channel)
2396 {
2397         RAII_VAR(struct module_config *, mod_cfg,
2398                         ao2_global_obj_ref(module_configs), ao2_cleanup);
2399         int res = 1;
2400         struct cdr_object *it_cdr;
2401         struct cdr_object *new_cdr;
2402
2403         ao2_lock(cdr);
2404
2405         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2406                 if (it_cdr->fn_table->process_parking_bridge_enter) {
2407                         res &= it_cdr->fn_table->process_parking_bridge_enter(it_cdr, bridge, channel);
2408                 }
2409                 if (it_cdr->fn_table->process_party_a) {
2410                         CDR_DEBUG(mod_cfg, "%p - Updating Party A %s snapshot\n", it_cdr,
2411                                         channel->name);
2412                         it_cdr->fn_table->process_party_a(it_cdr, channel);
2413                 }
2414         }
2415
2416         if (res) {
2417                 /* No one handled it - we need a new one! */
2418                 new_cdr = cdr_object_create_and_append(cdr);
2419                 if (new_cdr) {
2420                         /* Let the single state transition us to Parked */
2421                         cdr_object_transition_state(new_cdr, &single_state_fn_table);
2422                         new_cdr->fn_table->process_parking_bridge_enter(new_cdr, bridge, channel);
2423                 }
2424         }
2425         ao2_unlock(cdr);
2426 }
2427
2428 /*! \brief Handle a bridge enter message for a 'normal' bridge
2429  * \param cdr The CDR to operate on
2430  * \param bridge The bridge the channel just entered
2431  * \param channel The channel snapshot
2432  */
2433 static void handle_standard_bridge_enter_message(struct cdr_object *cdr,
2434                 struct ast_bridge_snapshot *bridge,
2435                 struct ast_channel_snapshot *channel)
2436 {
2437         RAII_VAR(struct module_config *, mod_cfg,
2438                         ao2_global_obj_ref(module_configs), ao2_cleanup);
2439         enum process_bridge_enter_results result;
2440         struct cdr_object *it_cdr;
2441         struct cdr_object *new_cdr;
2442         struct cdr_object *handled_cdr = NULL;
2443
2444         ao2_lock(cdr);
2445
2446         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2447                 if (it_cdr->fn_table->process_party_a) {
2448                         CDR_DEBUG(mod_cfg, "%p - Updating Party A %s snapshot\n", it_cdr,
2449                                         channel->name);
2450                         it_cdr->fn_table->process_party_a(it_cdr, channel);
2451                 }
2452
2453                 /* Notify all states that they have entered a bridge */
2454                 if (it_cdr->fn_table->process_bridge_enter) {
2455                         CDR_DEBUG(mod_cfg, "%p - Processing bridge enter for %s\n", it_cdr,
2456                                         channel->name);
2457                         result = it_cdr->fn_table->process_bridge_enter(it_cdr, bridge, channel);
2458                         switch (result) {
2459                         case BRIDGE_ENTER_ONLY_PARTY:
2460                                 /* Fall through */
2461                         case BRIDGE_ENTER_OBTAINED_PARTY_B:
2462                                 if (!handled_cdr) {
2463                                         handled_cdr = it_cdr;
2464                                 }
2465                                 break;
2466                         case BRIDGE_ENTER_NEED_CDR:
2467                                 /* Pass */
2468                                 break;
2469                         case BRIDGE_ENTER_NO_PARTY_B:
2470                                 /* We didn't win on any - end this CDR. If someone else comes in later
2471                                  * that is Party B to this CDR, it can re-activate this CDR.
2472                                  */
2473                                 if (!handled_cdr) {
2474                                         handled_cdr = it_cdr;
2475                                 }
2476                                 cdr_object_finalize(cdr);
2477                                 break;
2478                         }
2479                 }
2480         }
2481
2482         /* Create the new matchings, but only for either:
2483          *  * The first CDR in the chain that handled it. This avoids issues with
2484          *    forked CDRs.
2485          *  * If no one handled it, the last CDR in the chain. This would occur if
2486          *    a CDR joined a bridge and it wasn't Party A for anyone. We still need
2487          *    to make pairings with everyone in the bridge.
2488          */
2489         if (handled_cdr) {
2490                 handle_bridge_pairings(handled_cdr, bridge);
2491         } else {
2492                 /* Nothing handled it - we need a new one! */
2493                 new_cdr = cdr_object_create_and_append(cdr);
2494                 if (new_cdr) {
2495                         /* This is guaranteed to succeed: the new CDR is created in the single state
2496                          * and will be able to handle the bridge enter message
2497                          */
2498                         handle_standard_bridge_enter_message(cdr, bridge, channel);
2499                 }
2500         }
2501         ao2_unlock(cdr);
2502 }
2503
2504 /*!
2505  * \internal
2506  * \brief Handler for Stasis-Core bridge enter messages
2507  * \param data Passed on
2508  * \param sub The stasis subscription for this message callback
2509  * \param topic The topic this message was published for
2510  * \param message The message - hopefully a bridge one!
2511  */
2512 static void handle_bridge_enter_message(void *data, struct stasis_subscription *sub,
2513                 struct stasis_message *message)
2514 {
2515         struct ast_bridge_blob *update = stasis_message_data(message);
2516         struct ast_bridge_snapshot *bridge = update->bridge;
2517         struct ast_channel_snapshot *channel = update->channel;
2518         struct cdr_object *cdr;
2519         RAII_VAR(struct module_config *, mod_cfg,
2520                         ao2_global_obj_ref(module_configs), ao2_cleanup);
2521
2522         if (filter_bridge_messages(bridge)) {
2523                 return;
2524         }
2525
2526         if (filter_channel_snapshot(channel)) {
2527                 return;
2528         }
2529
2530         CDR_DEBUG(mod_cfg, "Bridge Enter message for channel %s: %u.%08u\n",
2531                         channel->name,
2532                         (unsigned int)stasis_message_timestamp(message)->tv_sec,
2533                         (unsigned int)stasis_message_timestamp(message)->tv_usec);
2534
2535         cdr = ao2_find(active_cdrs_by_channel, channel->uniqueid, OBJ_SEARCH_KEY);
2536         if (!cdr) {
2537                 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->name);
2538                 ast_assert(0);
2539                 return;
2540         }
2541
2542         if (!strcmp(bridge->subclass, "parking")) {
2543                 handle_parking_bridge_enter_message(cdr, bridge, channel);
2544         } else {
2545                 handle_standard_bridge_enter_message(cdr, bridge, channel);
2546         }
2547         ao2_cleanup(cdr);
2548 }
2549
2550 /*!
2551  * \brief Handler for when a channel is parked
2552  * \param data Passed on
2553  * \param sub The stasis subscription for this message callback
2554  * \param topic The topic this message was published for
2555  * \param message The message about who got parked
2556  * */
2557 static void handle_parked_call_message(void *data, struct stasis_subscription *sub,
2558                 struct stasis_message *message)
2559 {
2560         struct ast_parked_call_payload *payload = stasis_message_data(message);
2561         struct ast_channel_snapshot *channel = payload->parkee;
2562         struct cdr_object *cdr;
2563         RAII_VAR(struct module_config *, mod_cfg,
2564                         ao2_global_obj_ref(module_configs), ao2_cleanup);
2565         int unhandled = 1;
2566         struct cdr_object *it_cdr;
2567
2568         /* Anything other than getting parked will be handled by other updates */
2569         if (payload->event_type != PARKED_CALL) {
2570                 return;
2571         }
2572
2573         /* No one got parked? */
2574         if (!channel) {
2575                 return;
2576         }
2577
2578         if (filter_channel_snapshot(channel)) {
2579                 return;
2580         }
2581
2582         CDR_DEBUG(mod_cfg, "Parked Call message for channel %s: %u.%08u\n",
2583                         channel->name,
2584                         (unsigned int)stasis_message_timestamp(message)->tv_sec,
2585                         (unsigned int)stasis_message_timestamp(message)->tv_usec);
2586
2587         cdr = ao2_find(active_cdrs_by_channel, channel->uniqueid, OBJ_SEARCH_KEY);
2588         if (!cdr) {
2589                 ast_log(AST_LOG_WARNING, "No CDR for channel %s\n", channel->name);
2590                 ast_assert(0);
2591                 return;
2592         }
2593
2594         ao2_lock(cdr);
2595
2596         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2597                 if (it_cdr->fn_table->process_parked_channel) {
2598                         unhandled &= it_cdr->fn_table->process_parked_channel(it_cdr, payload);
2599                 }
2600         }
2601
2602         if (unhandled) {
2603                 /* Nothing handled the messgae - we need a new one! */
2604                 struct cdr_object *new_cdr;
2605
2606                 new_cdr = cdr_object_create_and_append(cdr);
2607                 if (new_cdr) {
2608                         /* As the new CDR is created in the single state, it is guaranteed
2609                          * to have a function for the parked call message and will handle
2610                          * the message */
2611                         new_cdr->fn_table->process_parked_channel(new_cdr, payload);
2612                 }
2613         }
2614
2615         ao2_unlock(cdr);
2616
2617         ao2_cleanup(cdr);
2618 }
2619
2620 /*!
2621  * \brief Handler for a synchronization message
2622  * \param data Passed on
2623  * \param sub The stasis subscription for this message callback
2624  * \param topic The topic this message was published for
2625  * \param message A blank ao2 object
2626  * */
2627 static void handle_cdr_sync_message(void *data, struct stasis_subscription *sub,
2628                 struct stasis_message *message)
2629 {
2630         return;
2631 }
2632
2633 struct ast_cdr_config *ast_cdr_get_config(void)
2634 {
2635         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
2636         ao2_ref(mod_cfg->general, +1);
2637         return mod_cfg->general;
2638 }
2639
2640 void ast_cdr_set_config(struct ast_cdr_config *config)
2641 {
2642         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
2643
2644         ao2_cleanup(mod_cfg->general);
2645         mod_cfg->general = config;
2646         ao2_ref(mod_cfg->general, +1);
2647
2648         cdr_toggle_runtime_options();
2649 }
2650
2651 int ast_cdr_is_enabled(void)
2652 {
2653         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
2654         return ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED);
2655 }
2656
2657 int ast_cdr_backend_suspend(const char *name)
2658 {
2659         int success = -1;
2660         struct cdr_beitem *i = NULL;
2661
2662         AST_RWLIST_WRLOCK(&be_list);
2663         AST_RWLIST_TRAVERSE(&be_list, i, list) {
2664                 if (!strcasecmp(name, i->name)) {
2665                         ast_debug(3, "Suspending CDR backend %s\n", i->name);
2666                         i->suspended = 1;
2667                         success = 0;
2668                 }
2669         }
2670         AST_RWLIST_UNLOCK(&be_list);
2671
2672         return success;
2673 }
2674
2675 int ast_cdr_backend_unsuspend(const char *name)
2676 {
2677         int success = -1;
2678         struct cdr_beitem *i = NULL;
2679
2680         AST_RWLIST_WRLOCK(&be_list);
2681         AST_RWLIST_TRAVERSE(&be_list, i, list) {
2682                 if (!strcasecmp(name, i->name)) {
2683                         ast_debug(3, "Unsuspending CDR backend %s\n", i->name);
2684                         i->suspended = 0;
2685                         success = 0;
2686                 }
2687         }
2688         AST_RWLIST_UNLOCK(&be_list);
2689
2690         return success;
2691 }
2692
2693 static int cdr_generic_register(struct be_list *generic_list, const char *name, const char *desc, ast_cdrbe be)
2694 {
2695         struct cdr_beitem *i = NULL;
2696
2697         if (!name)
2698                 return -1;
2699
2700         if (!be) {
2701                 ast_log(LOG_WARNING, "CDR engine '%s' lacks backend\n", name);
2702                 return -1;
2703         }
2704
2705         AST_RWLIST_WRLOCK(generic_list);
2706         AST_RWLIST_TRAVERSE(generic_list, i, list) {
2707                 if (!strcasecmp(name, i->name)) {
2708                         ast_log(LOG_WARNING, "Already have a CDR backend called '%s'\n", name);
2709                         AST_RWLIST_UNLOCK(generic_list);
2710                         return -1;
2711                 }
2712         }
2713
2714         if (!(i = ast_calloc(1, sizeof(*i))))
2715                 return -1;
2716
2717         i->be = be;
2718         ast_copy_string(i->name, name, sizeof(i->name));
2719         ast_copy_string(i->desc, desc, sizeof(i->desc));
2720
2721         AST_RWLIST_INSERT_HEAD(generic_list, i, list);
2722         AST_RWLIST_UNLOCK(generic_list);
2723
2724         return 0;
2725 }
2726
2727 int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
2728 {
2729         return cdr_generic_register(&be_list, name, desc, be);
2730 }
2731
2732 int ast_cdr_modifier_register(const char *name, const char *desc, ast_cdrbe be)
2733 {
2734         return cdr_generic_register((struct be_list *)&mo_list, name, desc, be);
2735 }
2736
2737 static int ast_cdr_generic_unregister(struct be_list *generic_list, const char *name)
2738 {
2739         struct cdr_beitem *match = NULL;
2740         int active_count;
2741
2742         AST_RWLIST_WRLOCK(generic_list);
2743         AST_RWLIST_TRAVERSE(generic_list, match, list) {
2744                 if (!strcasecmp(name, match->name)) {
2745                         break;
2746                 }
2747         }
2748
2749         if (!match) {
2750                 AST_RWLIST_UNLOCK(generic_list);
2751                 return 0;
2752         }
2753
2754         active_count = ao2_container_count(active_cdrs_by_channel);
2755
2756         if (!match->suspended && active_count != 0) {
2757                 AST_RWLIST_UNLOCK(generic_list);
2758                 ast_log(AST_LOG_WARNING, "Unable to unregister CDR backend %s; %d CDRs are still active\n",
2759                         name, active_count);
2760                 return -1;
2761         }
2762
2763         AST_RWLIST_REMOVE(generic_list, match, list);
2764         AST_RWLIST_UNLOCK(generic_list);
2765
2766         ast_verb(2, "Unregistered '%s' CDR backend\n", name);
2767         ast_free(match);
2768
2769         return 0;
2770 }
2771
2772 int ast_cdr_unregister(const char *name)
2773 {
2774         return ast_cdr_generic_unregister(&be_list, name);
2775 }
2776
2777 int ast_cdr_modifier_unregister(const char *name)
2778 {
2779         return ast_cdr_generic_unregister((struct be_list *)&mo_list, name);
2780 }
2781
2782 struct ast_cdr *ast_cdr_dup(struct ast_cdr *cdr)
2783 {
2784         struct ast_cdr *newcdr;
2785
2786         if (!cdr) {
2787                 return NULL;
2788         }
2789         newcdr = ast_cdr_alloc();
2790         if (!newcdr) {
2791                 return NULL;
2792         }
2793
2794         *newcdr = *cdr;
2795         AST_LIST_HEAD_INIT_NOLOCK(&newcdr->varshead);
2796         copy_variables(&newcdr->varshead, &cdr->varshead);
2797         newcdr->next = NULL;
2798
2799         return newcdr;
2800 }
2801
2802 static const char *cdr_format_var_internal(struct ast_cdr *cdr, const char *name)
2803 {
2804         struct ast_var_t *variables;
2805
2806         if (ast_strlen_zero(name)) {
2807                 return NULL;
2808         }
2809
2810         AST_LIST_TRAVERSE(&cdr->varshead, variables, entries) {
2811                 if (!strcasecmp(name, ast_var_name(variables))) {
2812                         return ast_var_value(variables);
2813                 }
2814         }
2815
2816         return NULL;
2817 }
2818
2819 static void cdr_get_tv(struct timeval when, const char *fmt, char *buf, int bufsize)
2820 {
2821         if (fmt == NULL) {      /* raw mode */
2822                 snprintf(buf, bufsize, "%ld.%06ld", (long)when.tv_sec, (long)when.tv_usec);
2823         } else {
2824                 buf[0] = '\0';/* Ensure the buffer is initialized. */
2825                 if (when.tv_sec) {
2826                         struct ast_tm tm;
2827
2828                         ast_localtime(&when, &tm, NULL);
2829                         ast_strftime(buf, bufsize, fmt, &tm);
2830                 }
2831         }
2832 }
2833
2834 void ast_cdr_format_var(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int raw)
2835 {
2836         const char *fmt = "%Y-%m-%d %T";
2837         const char *varbuf;
2838
2839         if (!cdr) {
2840                 return;
2841         }
2842
2843         *ret = NULL;
2844
2845         if (!strcasecmp(name, "clid")) {
2846                 ast_copy_string(workspace, cdr->clid, workspacelen);
2847         } else if (!strcasecmp(name, "src")) {
2848                 ast_copy_string(workspace, cdr->src, workspacelen);
2849         } else if (!strcasecmp(name, "dst")) {
2850                 ast_copy_string(workspace, cdr->dst, workspacelen);
2851         } else if (!strcasecmp(name, "dcontext")) {
2852                 ast_copy_string(workspace, cdr->dcontext, workspacelen);
2853         } else if (!strcasecmp(name, "channel")) {
2854                 ast_copy_string(workspace, cdr->channel, workspacelen);
2855         } else if (!strcasecmp(name, "dstchannel")) {
2856                 ast_copy_string(workspace, cdr->dstchannel, workspacelen);
2857         } else if (!strcasecmp(name, "lastapp")) {
2858                 ast_copy_string(workspace, cdr->lastapp, workspacelen);
2859         } else if (!strcasecmp(name, "lastdata")) {
2860                 ast_copy_string(workspace, cdr->lastdata, workspacelen);
2861         } else if (!strcasecmp(name, "start")) {
2862                 cdr_get_tv(cdr->start, raw ? NULL : fmt, workspace, workspacelen);
2863         } else if (!strcasecmp(name, "answer")) {
2864                 cdr_get_tv(cdr->answer, raw ? NULL : fmt, workspace, workspacelen);
2865         } else if (!strcasecmp(name, "end")) {
2866                 cdr_get_tv(cdr->end, raw ? NULL : fmt, workspace, workspacelen);
2867         } else if (!strcasecmp(name, "duration")) {
2868                 snprintf(workspace, workspacelen, "%ld", cdr->end.tv_sec != 0 ? cdr->duration : (long)ast_tvdiff_ms(ast_tvnow(), cdr->start) / 1000);
2869         } else if (!strcasecmp(name, "billsec")) {
2870                 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);
2871         } else if (!strcasecmp(name, "disposition")) {
2872                 if (raw) {
2873                         snprintf(workspace, workspacelen, "%ld", cdr->disposition);
2874                 } else {
2875                         ast_copy_string(workspace, ast_cdr_disp2str(cdr->disposition), workspacelen);
2876                 }
2877         } else if (!strcasecmp(name, "amaflags")) {
2878                 if (raw) {
2879                         snprintf(workspace, workspacelen, "%ld", cdr->amaflags);
2880                 } else {
2881                         ast_copy_string(workspace, ast_channel_amaflags2string(cdr->amaflags), workspacelen);
2882                 }
2883         } else if (!strcasecmp(name, "accountcode")) {
2884                 ast_copy_string(workspace, cdr->accountcode, workspacelen);
2885         } else if (!strcasecmp(name, "peeraccount")) {
2886                 ast_copy_string(workspace, cdr->peeraccount, workspacelen);
2887         } else if (!strcasecmp(name, "uniqueid")) {
2888                 ast_copy_string(workspace, cdr->uniqueid, workspacelen);
2889         } else if (!strcasecmp(name, "linkedid")) {
2890                 ast_copy_string(workspace, cdr->linkedid, workspacelen);
2891         } else if (!strcasecmp(name, "userfield")) {
2892                 ast_copy_string(workspace, cdr->userfield, workspacelen);
2893         } else if (!strcasecmp(name, "sequence")) {
2894                 snprintf(workspace, workspacelen, "%d", cdr->sequence);
2895         } else if ((varbuf = cdr_format_var_internal(cdr, name))) {
2896                 ast_copy_string(workspace, varbuf, workspacelen);
2897         } else {
2898                 workspace[0] = '\0';
2899         }
2900
2901         if (!ast_strlen_zero(workspace)) {
2902                 *ret = workspace;
2903         }
2904 }
2905
2906 /*!
2907  * \internal
2908  * \brief Callback that finds all CDRs that reference a particular channel by name
2909  */
2910 static int cdr_object_select_all_by_name_cb(void *obj, void *arg, int flags)
2911 {
2912         struct cdr_object *cdr = obj;
2913         const char *name = arg;
2914
2915         if (!strcasecmp(cdr->party_a.snapshot->name, name) ||
2916                         (cdr->party_b.snapshot && !strcasecmp(cdr->party_b.snapshot->name, name))) {
2917                 return CMP_MATCH;
2918         }
2919         return 0;
2920 }
2921
2922 /*!
2923  * \internal
2924  * \brief Callback that finds a CDR by channel name
2925  */
2926 static int cdr_object_get_by_name_cb(void *obj, void *arg, int flags)
2927 {
2928         struct cdr_object *cdr = obj;
2929         const char *name = arg;
2930
2931         if (!strcasecmp(cdr->party_a.snapshot->name, name)) {
2932                 return CMP_MATCH;
2933         }
2934         return 0;
2935 }
2936
2937 /* Read Only CDR variables */
2938 static const char * const cdr_readonly_vars[] = {
2939         "clid",
2940         "src",
2941         "dst",
2942         "dcontext",
2943         "channel",
2944         "dstchannel",
2945         "lastapp",
2946         "lastdata",
2947         "start",
2948         "answer",
2949         "end",
2950         "duration",
2951         "billsec",
2952         "disposition",
2953         "amaflags",
2954         "accountcode",
2955         "uniqueid",
2956         "linkedid",
2957         "userfield",
2958         "sequence",
2959         NULL
2960 };
2961
2962 int ast_cdr_setvar(const char *channel_name, const char *name, const char *value)
2963 {
2964         struct cdr_object *cdr;
2965         struct cdr_object *it_cdr;
2966         struct ao2_iterator *it_cdrs;
2967         char *arg = ast_strdupa(channel_name);
2968         int x;
2969
2970         for (x = 0; cdr_readonly_vars[x]; x++) {
2971                 if (!strcasecmp(name, cdr_readonly_vars[x])) {
2972                         ast_log(LOG_ERROR, "Attempt to set the '%s' read-only variable!\n", name);
2973                         return -1;
2974                 }
2975         }
2976
2977         it_cdrs = ao2_callback(active_cdrs_by_channel, OBJ_MULTIPLE, cdr_object_select_all_by_name_cb, arg);
2978         if (!it_cdrs) {
2979                 ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name);
2980                 return -1;
2981         }
2982
2983         for (; (cdr = ao2_iterator_next(it_cdrs)); ao2_unlock(cdr), ao2_cleanup(cdr)) {
2984                 ao2_lock(cdr);
2985                 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
2986                         struct varshead *headp = NULL;
2987
2988                         if (it_cdr->fn_table == &finalized_state_fn_table && it_cdr->next != NULL) {
2989                                 continue;
2990                         }
2991                         if (!strcasecmp(channel_name, it_cdr->party_a.snapshot->name)) {
2992                                 headp = &it_cdr->party_a.variables;
2993                         } else if (it_cdr->party_b.snapshot
2994                                 && !strcasecmp(channel_name, it_cdr->party_b.snapshot->name)) {
2995                                 headp = &it_cdr->party_b.variables;
2996                         }
2997                         if (headp) {
2998                                 set_variable(headp, name, value);
2999                         }
3000                 }
3001         }
3002         ao2_iterator_destroy(it_cdrs);
3003
3004         return 0;
3005 }
3006
3007 /*!
3008  * \brief Format a variable on a \ref cdr_object
3009  */
3010 static void cdr_object_format_var_internal(struct cdr_object *cdr, const char *name, char *value, size_t length)
3011 {
3012         struct ast_var_t *variable;
3013
3014         AST_LIST_TRAVERSE(&cdr->party_a.variables, variable, entries) {
3015                 if (!strcasecmp(name, ast_var_name(variable))) {
3016                         ast_copy_string(value, ast_var_value(variable), length);
3017                         return;
3018                 }
3019         }
3020
3021         *value = '\0';
3022 }
3023
3024 /*!
3025  * \brief Format one of the standard properties on a \ref cdr_object
3026  */
3027 static int cdr_object_format_property(struct cdr_object *cdr_obj, const char *name, char *value, size_t length)
3028 {
3029         struct ast_channel_snapshot *party_a = cdr_obj->party_a.snapshot;
3030         struct ast_channel_snapshot *party_b = cdr_obj->party_b.snapshot;
3031
3032         if (!strcasecmp(name, "clid")) {
3033                 ast_callerid_merge(value, length, party_a->caller_name, party_a->caller_number, "");
3034         } else if (!strcasecmp(name, "src")) {
3035                 ast_copy_string(value, party_a->caller_number, length);
3036         } else if (!strcasecmp(name, "dst")) {
3037                 ast_copy_string(value, party_a->exten, length);
3038         } else if (!strcasecmp(name, "dcontext")) {
3039                 ast_copy_string(value, party_a->context, length);
3040         } else if (!strcasecmp(name, "channel")) {
3041                 ast_copy_string(value, party_a->name, length);
3042         } else if (!strcasecmp(name, "dstchannel")) {
3043                 if (party_b) {
3044                         ast_copy_string(value, party_b->name, length);
3045                 } else {
3046                         ast_copy_string(value, "", length);
3047                 }
3048         } else if (!strcasecmp(name, "lastapp")) {
3049                 ast_copy_string(value, party_a->appl, length);
3050         } else if (!strcasecmp(name, "lastdata")) {
3051                 ast_copy_string(value, party_a->data, length);
3052         } else if (!strcasecmp(name, "start")) {
3053                 cdr_get_tv(cdr_obj->start, NULL, value, length);
3054         } else if (!strcasecmp(name, "answer")) {
3055                 cdr_get_tv(cdr_obj->answer, NULL, value, length);
3056         } else if (!strcasecmp(name, "end")) {
3057                 cdr_get_tv(cdr_obj->end, NULL, value, length);
3058         } else if (!strcasecmp(name, "duration")) {
3059                 snprintf(value, length, "%ld", cdr_object_get_duration(cdr_obj));
3060         } else if (!strcasecmp(name, "billsec")) {
3061                 snprintf(value, length, "%ld", cdr_object_get_billsec(cdr_obj));
3062         } else if (!strcasecmp(name, "disposition")) {
3063                 snprintf(value, length, "%u", cdr_obj->disposition);
3064         } else if (!strcasecmp(name, "amaflags")) {
3065                 snprintf(value, length, "%d", party_a->amaflags);
3066         } else if (!strcasecmp(name, "accountcode")) {
3067                 ast_copy_string(value, party_a->accountcode, length);
3068         } else if (!strcasecmp(name, "peeraccount")) {
3069                 if (party_b) {
3070                         ast_copy_string(value, party_b->accountcode, length);
3071                 } else {
3072                         ast_copy_string(value, "", length);
3073                 }
3074         } else if (!strcasecmp(name, "uniqueid")) {
3075                 ast_copy_string(value, party_a->uniqueid, length);
3076         } else if (!strcasecmp(name, "linkedid")) {
3077                 ast_copy_string(value, cdr_obj->linkedid, length);
3078         } else if (!strcasecmp(name, "userfield")) {
3079                 ast_copy_string(value, cdr_obj->party_a.userfield, length);
3080         } else if (!strcasecmp(name, "sequence")) {
3081                 snprintf(value, length, "%u", cdr_obj->sequence);
3082         } else {
3083                 return 1;
3084         }
3085
3086         return 0;
3087 }
3088
3089 /*! \internal
3090  * \brief Look up and retrieve a CDR object by channel name
3091  * \param name The name of the channel
3092  * \retval NULL on error
3093  * \retval The \ref cdr_object for the channel on success, with the reference
3094  *      count bumped by one.
3095  */
3096 static struct cdr_object *cdr_object_get_by_name(const char *name)
3097 {
3098         char *param;
3099
3100         if (ast_strlen_zero(name)) {
3101                 return NULL;
3102         }
3103
3104         param = ast_strdupa(name);
3105         return ao2_callback(active_cdrs_by_channel, 0, cdr_object_get_by_name_cb, param);
3106 }
3107
3108 int ast_cdr_getvar(const char *channel_name, const char *name, char *value, size_t length)
3109 {
3110         struct cdr_object *cdr;
3111         struct cdr_object *cdr_obj;
3112
3113         if (ast_strlen_zero(name)) {
3114                 return 1;
3115         }
3116
3117         cdr = cdr_object_get_by_name(channel_name);
3118         if (!cdr) {
3119                 ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name);
3120                 return 1;
3121         }
3122
3123         ao2_lock(cdr);
3124
3125         cdr_obj = cdr->last;
3126         if (cdr_object_format_property(cdr_obj, name, value, length)) {
3127                 /* Property failed; attempt variable */
3128                 cdr_object_format_var_internal(cdr_obj, name, value, length);
3129         }
3130
3131         ao2_unlock(cdr);
3132
3133         ao2_cleanup(cdr);
3134         return 0;
3135 }
3136
3137 int ast_cdr_serialize_variables(const char *channel_name, struct ast_str **buf, char delim, char sep)
3138 {
3139         struct cdr_object *cdr;
3140         struct cdr_object *it_cdr;
3141         struct ast_var_t *variable;
3142         const char *var;
3143         char workspace[256];
3144         int total = 0, x = 0, i;
3145
3146         cdr = cdr_object_get_by_name(channel_name);
3147         if (!cdr) {
3148                 RAII_VAR(struct module_config *, mod_cfg,
3149                          ao2_global_obj_ref(module_configs), ao2_cleanup);
3150
3151                 if (ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
3152                         ast_log(AST_LOG_ERROR, "Unable to find CDR for channel %s\n", channel_name);
3153                 }
3154
3155                 return 0;
3156         }
3157
3158         ast_str_reset(*buf);
3159
3160         ao2_lock(cdr);
3161         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3162                 if (++x > 1) {
3163                         ast_str_append(buf, 0, "\n");
3164                 }
3165
3166                 AST_LIST_TRAVERSE(&it_cdr->party_a.variables, variable, entries) {
3167                         if (!(var = ast_var_name(variable))) {
3168                                 continue;
3169                         }
3170
3171                         if (ast_str_append(buf, 0, "level %d: %s%c%s%c", x, var, delim, S_OR(ast_var_value(variable), ""), sep) < 0) {
3172                                 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
3173                                 break;
3174                         }
3175
3176                         total++;
3177                 }
3178
3179                 for (i = 0; cdr_readonly_vars[i]; i++) {
3180                         if (cdr_object_format_property(it_cdr, cdr_readonly_vars[i], workspace, sizeof(workspace))) {
3181                                 /* Unhandled read-only CDR variable. */
3182                                 ast_assert(0);
3183                                 continue;
3184                         }
3185
3186                         if (!ast_strlen_zero(workspace)
3187                                 && ast_str_append(buf, 0, "level %d: %s%c%s%c", x, cdr_readonly_vars[i], delim, workspace, sep) < 0) {
3188                                 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
3189                                 break;
3190                         }
3191                         total++;
3192                 }
3193         }
3194         ao2_unlock(cdr);
3195         ao2_cleanup(cdr);
3196         return total;
3197 }
3198
3199 void ast_cdr_free(struct ast_cdr *cdr)
3200 {
3201         while (cdr) {
3202                 struct ast_cdr *next = cdr->next;
3203
3204                 free_variables(&cdr->varshead);
3205                 ast_free(cdr);
3206                 cdr = next;
3207         }
3208 }
3209
3210 struct ast_cdr *ast_cdr_alloc(void)
3211 {
3212         struct ast_cdr *x;
3213
3214         x = ast_calloc(1, sizeof(*x));
3215         return x;
3216 }
3217
3218 const char *ast_cdr_disp2str(int disposition)
3219 {
3220         switch (disposition) {
3221         case AST_CDR_NULL:
3222                 return "NO ANSWER"; /* by default, for backward compatibility */
3223         case AST_CDR_NOANSWER:
3224                 return "NO ANSWER";
3225         case AST_CDR_FAILED:
3226                 return "FAILED";
3227         case AST_CDR_BUSY:
3228                 return "BUSY";
3229         case AST_CDR_ANSWERED:
3230                 return "ANSWERED";
3231         case AST_CDR_CONGESTION:
3232                 return "CONGESTION";
3233         }
3234         return "UNKNOWN";
3235 }
3236
3237 struct party_b_userfield_update {
3238         const char *channel_name;
3239         const char *userfield;
3240 };
3241
3242 /*! \brief Callback used to update the userfield on Party B on all CDRs */
3243 static int cdr_object_update_party_b_userfield_cb(void *obj, void *arg, int flags)
3244 {
3245         struct cdr_object *cdr = obj;
3246         struct party_b_userfield_update *info = arg;
3247         struct cdr_object *it_cdr;
3248
3249         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3250                 if (it_cdr->fn_table == &finalized_state_fn_table && it_cdr->next != NULL) {
3251                         continue;
3252                 }
3253                 if (it_cdr->party_b.snapshot
3254                         && !strcasecmp(it_cdr->party_b.snapshot->name, info->channel_name)) {
3255                         strcpy(it_cdr->party_b.userfield, info->userfield);
3256                 }
3257         }
3258         return 0;
3259 }
3260
3261 void ast_cdr_setuserfield(const char *channel_name, const char *userfield)
3262 {
3263         struct cdr_object *cdr;
3264         struct party_b_userfield_update party_b_info = {
3265                         .channel_name = channel_name,
3266                         .userfield = userfield,
3267         };
3268         struct cdr_object *it_cdr;
3269
3270         /* Handle Party A */
3271         cdr = cdr_object_get_by_name(channel_name);
3272         if (cdr) {
3273                 ao2_lock(cdr);
3274                 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3275                         if (it_cdr->fn_table == &finalized_state_fn_table && it_cdr->next != NULL) {
3276                                 continue;
3277                         }
3278                         ast_copy_string(it_cdr->party_a.userfield, userfield, AST_MAX_USER_FIELD);
3279                 }
3280                 ao2_unlock(cdr);
3281         }
3282
3283         /* Handle Party B */
3284         ao2_callback(active_cdrs_by_channel, OBJ_NODATA,
3285                         cdr_object_update_party_b_userfield_cb,
3286                         &party_b_info);
3287
3288         ao2_cleanup(cdr);
3289 }
3290
3291 static void post_cdr(struct ast_cdr *cdr)
3292 {
3293         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
3294         struct cdr_beitem *i;
3295
3296         for (; cdr ; cdr = cdr->next) {
3297                 /* For people, who don't want to see unanswered single-channel events */
3298                 if (!ast_test_flag(&mod_cfg->general->settings, CDR_UNANSWERED) &&
3299                                 cdr->disposition < AST_CDR_ANSWERED &&
3300                                 (ast_strlen_zero(cdr->channel) || ast_strlen_zero(cdr->dstchannel))) {
3301                         ast_debug(1, "Skipping CDR  for %s since we weren't answered\n", cdr->channel);
3302                         continue;
3303                 }
3304
3305                 /* Modify CDR's */
3306                 AST_RWLIST_RDLOCK(&mo_list);
3307                 AST_RWLIST_TRAVERSE(&mo_list, i, list) {
3308                         i->be(cdr);
3309                 }
3310                 AST_RWLIST_UNLOCK(&mo_list);
3311
3312                 if (ast_test_flag(cdr, AST_CDR_FLAG_DISABLE)) {
3313                         continue;
3314                 }
3315                 AST_RWLIST_RDLOCK(&be_list);
3316                 AST_RWLIST_TRAVERSE(&be_list, i, list) {
3317                         if (!i->suspended) {
3318                                 i->be(cdr);
3319                         }
3320                 }
3321                 AST_RWLIST_UNLOCK(&be_list);
3322         }
3323 }
3324
3325 int ast_cdr_set_property(const char *channel_name, enum ast_cdr_options option)
3326 {
3327         struct cdr_object *cdr;
3328         struct cdr_object *it_cdr;
3329
3330         cdr = cdr_object_get_by_name(channel_name);
3331         if (!cdr) {
3332                 return -1;
3333         }
3334
3335         ao2_lock(cdr);
3336         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3337                 if (it_cdr->fn_table == &finalized_state_fn_table) {
3338                         continue;
3339                 }
3340                 /* Note: in general, set the flags on both the CDR record as well as the
3341                  * Party A. Sometimes all we have is the Party A to look at.
3342                  */
3343                 ast_set_flag(&it_cdr->flags, option);
3344                 ast_set_flag(&it_cdr->party_a, option);
3345         }
3346         ao2_unlock(cdr);
3347
3348         ao2_cleanup(cdr);
3349         return 0;
3350 }
3351
3352 int ast_cdr_clear_property(const char *channel_name, enum ast_cdr_options option)
3353 {
3354         struct cdr_object *cdr;
3355         struct cdr_object *it_cdr;
3356
3357         cdr = cdr_object_get_by_name(channel_name);
3358         if (!cdr) {
3359                 return -1;
3360         }
3361
3362         ao2_lock(cdr);
3363         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3364                 if (it_cdr->fn_table == &finalized_state_fn_table) {
3365                         continue;
3366                 }
3367                 ast_clear_flag(&it_cdr->flags, option);
3368         }
3369         ao2_unlock(cdr);
3370
3371         ao2_cleanup(cdr);
3372         return 0;
3373 }
3374
3375 int ast_cdr_reset(const char *channel_name, int keep_variables)
3376 {
3377         struct cdr_object *cdr;
3378         struct ast_var_t *vardata;
3379         struct cdr_object *it_cdr;
3380
3381         cdr = cdr_object_get_by_name(channel_name);
3382         if (!cdr) {
3383                 return -1;
3384         }
3385
3386         ao2_lock(cdr);
3387         for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3388                 /* clear variables */
3389                 if (!keep_variables) {
3390                         while ((vardata = AST_LIST_REMOVE_HEAD(&it_cdr->party_a.variables, entries))) {
3391                                 ast_var_delete(vardata);
3392                         }
3393                         if (cdr->party_b.snapshot) {
3394                                 while ((vardata = AST_LIST_REMOVE_HEAD(&it_cdr->party_b.variables, entries))) {
3395                                         ast_var_delete(vardata);
3396                                 }
3397                         }
3398                 }
3399
3400                 /* Reset to initial state */
3401                 memset(&it_cdr->start, 0, sizeof(it_cdr->start));
3402                 memset(&it_cdr->end, 0, sizeof(it_cdr->end));
3403                 memset(&it_cdr->answer, 0, sizeof(it_cdr->answer));
3404                 it_cdr->start = ast_tvnow();
3405                 cdr_object_check_party_a_answer(it_cdr);
3406         }
3407         ao2_unlock(cdr);
3408
3409         ao2_cleanup(cdr);
3410         return 0;
3411 }
3412
3413 int ast_cdr_fork(const char *channel_name, struct ast_flags *options)
3414 {
3415         RAII_VAR(struct cdr_object *, cdr, cdr_object_get_by_name(channel_name), ao2_cleanup);
3416         struct cdr_object *new_cdr;
3417         struct cdr_object *it_cdr;
3418         struct cdr_object *cdr_obj;
3419
3420         if (!cdr) {
3421                 return -1;
3422         }
3423
3424         {
3425                 SCOPED_AO2LOCK(lock, cdr);
3426
3427                 cdr_obj = cdr->last;
3428                 if (cdr_obj->fn_table == &finalized_state_fn_table) {
3429                         /* If the last CDR in the chain is finalized, don't allow a fork -
3430                          * things are already dying at this point
3431                          */
3432                         return -1;
3433                 }
3434
3435                 /* Copy over the basic CDR information. The Party A information is
3436                  * copied over automatically as part of the append
3437                  */
3438                 ast_debug(1, "Forking CDR for channel %s\n", cdr->party_a.snapshot->name);
3439                 new_cdr = cdr_object_create_and_append(cdr);
3440                 if (!new_cdr) {
3441                         return -1;
3442                 }
3443                 new_cdr->fn_table = cdr_obj->fn_table;
3444                 ast_string_field_set(new_cdr, bridge, cdr->bridge);
3445                 ast_string_field_set(new_cdr, appl, cdr->appl);
3446                 ast_string_field_set(new_cdr, data, cdr->data);
3447                 ast_string_field_set(new_cdr, context, cdr->context);
3448                 ast_string_field_set(new_cdr, exten, cdr->exten);
3449                 new_cdr->flags = cdr->flags;
3450                 /* Explicitly clear the AST_CDR_LOCK_APP flag - we want
3451                  * the application to be changed on the new CDR if the
3452                  * dialplan demands it
3453                  */
3454                 ast_clear_flag(&new_cdr->flags, AST_CDR_LOCK_APP);
3455
3456                 /* If there's a Party B, copy it over as well */
3457                 if (cdr_obj->party_b.snapshot) {
3458                         new_cdr->party_b.snapshot = cdr_obj->party_b.snapshot;
3459                         ao2_ref(new_cdr->party_b.snapshot, +1);
3460                         strcpy(new_cdr->party_b.userfield, cdr_obj->party_b.userfield);
3461                         new_cdr->party_b.flags = cdr_obj->party_b.flags;
3462                         if (ast_test_flag(options, AST_CDR_FLAG_KEEP_VARS)) {
3463                                 copy_variables(&new_cdr->party_b.variables, &cdr_obj->party_b.variables);
3464                         }
3465                 }
3466                 new_cdr->start = cdr_obj->start;
3467                 new_cdr->answer = cdr_obj->answer;
3468
3469                 /* Modify the times based on the flags passed in */
3470                 if (ast_test_flag(options, AST_CDR_FLAG_SET_ANSWER)
3471                                 && new_cdr->party_a.snapshot->state == AST_STATE_UP) {
3472                         new_cdr->answer = ast_tvnow();
3473                 }
3474                 if (ast_test_flag(options, AST_CDR_FLAG_RESET)) {
3475                         new_cdr->answer = ast_tvnow();
3476                         new_cdr->start = ast_tvnow();
3477                 }
3478
3479                 /* Create and append, by default, copies over the variables */
3480                 if (!ast_test_flag(options, AST_CDR_FLAG_KEEP_VARS)) {
3481                         free_variables(&new_cdr->party_a.variables);
3482                 }
3483
3484                 /* Finalize any current CDRs */
3485                 if (ast_test_flag(options, AST_CDR_FLAG_FINALIZE)) {
3486                         for (it_cdr = cdr; it_cdr != new_cdr; it_cdr = it_cdr->next) {
3487                                 if (it_cdr->fn_table == &finalized_state_fn_table) {
3488                                         continue;
3489                                 }
3490                                 /* Force finalization on the CDR. This will bypass any checks for
3491                                  * end before 'h' extension.
3492                                  */
3493                                 cdr_object_finalize(it_cdr);
3494                                 cdr_object_transition_state(it_cdr, &finalized_state_fn_table);
3495                         }
3496                 }
3497         }
3498
3499         return 0;
3500 }
3501
3502 /*! \note Don't call without cdr_batch_lock */
3503 static void reset_batch(void)
3504 {
3505         batch->size = 0;
3506         batch->head = NULL;
3507         batch->tail = NULL;
3508 }
3509
3510 /*! \note Don't call without cdr_batch_lock */
3511 static int init_batch(void)
3512 {
3513         /* This is the single meta-batch used to keep track of all CDRs during the entire life of the program */
3514         if (!(batch = ast_malloc(sizeof(*batch))))
3515                 return -1;
3516
3517         reset_batch();
3518
3519         return 0;
3520 }
3521
3522 static void *do_batch_backend_process(void *data)
3523 {
3524         struct cdr_batch_item *processeditem;
3525         struct cdr_batch_item *batchitem = data;
3526
3527         /* Push each CDR into storage mechanism(s) and free all the memory */
3528         while (batchitem) {
3529                 post_cdr(batchitem->cdr);
3530                 ast_cdr_free(batchitem->cdr);
3531                 processeditem = batchitem;
3532                 batchitem = batchitem->next;
3533                 ast_free(processeditem);
3534         }
3535
3536         return NULL;
3537 }
3538
3539 static void cdr_submit_batch(int do_shutdown)
3540 {
3541         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
3542         struct cdr_batch_item *oldbatchitems = NULL;
3543         pthread_t batch_post_thread = AST_PTHREADT_NULL;
3544
3545         /* if there's no batch, or no CDRs in the batch, then there's nothing to do */
3546         if (!batch || !batch->head) {
3547                 return;
3548         }
3549
3550         /* move the old CDRs aside, and prepare a new CDR batch */
3551         ast_mutex_lock(&cdr_batch_lock);
3552         oldbatchitems = batch->head;
3553         reset_batch();
3554         ast_mutex_unlock(&cdr_batch_lock);
3555
3556         /* if configured, spawn a new thread to post these CDRs,
3557            also try to save as much as possible if we are shutting down safely */
3558         if (ast_test_flag(&mod_cfg->general->batch_settings.settings, BATCH_MODE_SCHEDULER_ONLY) || do_shutdown) {
3559                 ast_debug(1, "CDR single-threaded batch processing begins now\n");
3560                 do_batch_backend_process(oldbatchitems);
3561         } else {
3562                 if (ast_pthread_create_detached_background(&batch_post_thread, NULL, do_batch_backend_process, oldbatchitems)) {
3563                         ast_log(LOG_WARNING, "CDR processing thread could not detach, now trying in this thread\n");
3564                         do_batch_backend_process(oldbatchitems);
3565                 } else {
3566                         ast_debug(1, "CDR multi-threaded batch processing begins now\n");
3567                 }
3568         }
3569 }
3570
3571 static int submit_scheduled_batch(const void *data)
3572 {
3573         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
3574         cdr_submit_batch(0);
3575         /* manually reschedule from this point in time */
3576
3577         ast_mutex_lock(&cdr_sched_lock);
3578         cdr_sched = ast_sched_add(sched, mod_cfg->general->batch_settings.time * 1000, submit_scheduled_batch, NULL);
3579         ast_mutex_unlock(&cdr_sched_lock);
3580         /* returning zero so the scheduler does not automatically reschedule */
3581         return 0;
3582 }
3583
3584 /*! Do not hold the batch lock while calling this function */
3585 static void submit_unscheduled_batch(void)
3586 {
3587         /* Prevent two deletes from happening at the same time */
3588         ast_mutex_lock(&cdr_sched_lock);
3589         /* this is okay since we are not being called from within the scheduler */
3590         AST_SCHED_DEL(sched, cdr_sched);
3591         /* schedule the submission to occur ASAP (1 ms) */
3592         cdr_sched = ast_sched_add(sched, 1, submit_scheduled_batch, NULL);
3593         ast_mutex_unlock(&cdr_sched_lock);
3594
3595         /* signal the do_cdr thread to wakeup early and do some work (that lazy thread ;) */
3596         ast_mutex_lock(&cdr_pending_lock);
3597         ast_cond_signal(&cdr_pending_cond);
3598         ast_mutex_unlock(&cdr_pending_lock);
3599 }
3600
3601 static void cdr_detach(struct ast_cdr *cdr)
3602 {
3603         struct cdr_batch_item *newtail;
3604         int curr;
3605         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
3606         int submit_batch = 0;
3607
3608         if (!cdr) {
3609                 return;
3610         }
3611
3612         /* maybe they disabled CDR stuff completely, so just drop it */
3613         if (!ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
3614                 ast_debug(1, "Dropping CDR !\n");
3615                 ast_cdr_free(cdr);
3616                 return;
3617         }
3618
3619         /* post stuff immediately if we are not in batch mode, this is legacy behaviour */
3620         if (!ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) {
3621                 post_cdr(cdr);
3622                 ast_cdr_free(cdr);
3623                 return;
3624         }
3625
3626         /* otherwise, each CDR gets put into a batch list (at the end) */
3627         ast_debug(1, "CDR detaching from this thread\n");
3628
3629         /* we'll need a new tail for every CDR */
3630         if (!(newtail = ast_calloc(1, sizeof(*newtail)))) {
3631                 post_cdr(cdr);
3632                 ast_cdr_free(cdr);
3633                 return;
3634         }
3635
3636         /* don't traverse a whole list (just keep track of the tail) */
3637         ast_mutex_lock(&cdr_batch_lock);
3638         if (!batch)
3639                 init_batch();
3640         if (!batch->head) {
3641                 /* new batch is empty, so point the head at the new tail */
3642                 batch->head = newtail;
3643         } else {
3644                 /* already got a batch with something in it, so just append a new tail */
3645                 batch->tail->next = newtail;
3646         }
3647         newtail->cdr = cdr;
3648         batch->tail = newtail;
3649         curr = batch->size++;
3650
3651         /* if we have enough stuff to post, then do it */
3652         if (curr >= (mod_cfg->general->batch_settings.size - 1)) {
3653                 submit_batch = 1;
3654         }
3655         ast_mutex_unlock(&cdr_batch_lock);
3656
3657         /* Don't call submit_unscheduled_batch with the cdr_batch_lock held */
3658         if (submit_batch) {
3659                 submit_unscheduled_batch();
3660         }
3661 }
3662
3663 static void *do_cdr(void *data)
3664 {
3665         struct timespec timeout;
3666         int schedms;
3667         int numevents = 0;
3668
3669         for (;;) {
3670                 struct timeval now;
3671                 schedms = ast_sched_wait(sched);
3672                 /* this shouldn't happen, but provide a 1 second default just in case */
3673                 if (schedms <= 0)
3674                         schedms = 1000;
3675                 now = ast_tvadd(ast_tvnow(), ast_samp2tv(schedms, 1000));
3676                 timeout.tv_sec = now.tv_sec;
3677                 timeout.tv_nsec = now.tv_usec * 1000;
3678                 /* prevent stuff from clobbering cdr_pending_cond, then wait on signals sent to it until the timeout expires */
3679                 ast_mutex_lock(&cdr_pending_lock);
3680                 ast_cond_timedwait(&cdr_pending_cond, &cdr_pending_lock, &timeout);
3681                 numevents = ast_sched_runq(sched);
3682                 ast_mutex_unlock(&cdr_pending_lock);
3683                 ast_debug(2, "Processed %d scheduled CDR batches from the run queue\n", numevents);
3684         }
3685
3686         return NULL;
3687 }
3688
3689 static char *handle_cli_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
3690 {
3691         RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(module_configs), ao2_cleanup);
3692
3693         switch (cmd) {
3694         case CLI_INIT:
3695                 e->command = "cdr set debug [on|off]";
3696                 e->usage = "Enable or disable extra debugging in the CDR Engine. Note\n"
3697                                 "that this will dump debug information to the VERBOSE setting\n"
3698                                 "and should only be used when debugging information from the\n"
3699                                 "CDR engine is needed.\n";
3700                 return NULL;
3701         case CLI_GENERATE:
3702                 return NULL;
3703         }
3704
3705         if (a->argc != 4) {
3706                 return CLI_SHOWUSAGE;
3707         }
3708
3709         if (!strcasecmp(a->argv[3], "on")
3710                 && !ast_test_flag(&mod_cfg->general->settings, CDR_DEBUG)) {
3711                 ast_set_flag(&mod_cfg->general->settings, CDR_DEBUG);
3712                 ast_cli(a->fd, "CDR debugging enabled\n");
3713         } else if (!strcasecmp(a->argv[3], "off")
3714                 && ast_test_flag(&mod_cfg->general->settings, CDR_DEBUG)) {
3715                 ast_clear_flag(&mod_cfg->general->settings, CDR_DEBUG);
3716                 ast_cli(a->fd, "CDR debugging disabled\n");
3717         }
3718
3719         return CLI_SUCCESS;
3720 }
3721
3722 /*! \brief Complete user input for 'cdr show' */
3723 static char *cli_complete_show(struct ast_cli_args *a)
3724 {
3725         char *result = NULL;
3726         int wordlen = strlen(a->word);
3727         int which = 0;
3728         struct ao2_iterator it_cdrs;
3729         struct cdr_object *cdr;
3730
3731         it_cdrs = ao2_iterator_init(active_cdrs_by_channel, 0);
3732         while ((cdr = ao2_iterator_next(&it_cdrs))) {
3733                 if (!strncasecmp(a->word, cdr->party_a.snapshot->name, wordlen) &&
3734                         (++which > a->n)) {
3735                         result = ast_strdup(cdr->party_a.snapshot->name);
3736                         if (result) {
3737                                 ao2_ref(cdr, -1);
3738                                 break;
3739                         }
3740                 }
3741                 ao2_ref(cdr, -1);
3742         }
3743         ao2_iterator_destroy(&it_cdrs);
3744         return result;
3745 }
3746
3747 static void cli_show_channels(struct ast_cli_args *a)
3748 {
3749         struct ao2_iterator it_cdrs;
3750         struct cdr_object *cdr;
3751         char start_time_buffer[64];
3752         char answer_time_buffer[64];
3753         char end_time_buffer[64];
3754
3755 #define TITLE_STRING "%-25.25s %-25.25s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8s %-8.8s\n"
3756 #define FORMAT_STRING "%-25.25s %-25.25s %-15.15s %-8.8s %-8.8s %-8.8s %-8.8ld %-8.8ld\n"
3757
3758         ast_cli(a->fd, "\n");
3759         ast_cli(a->fd, "Channels with Call Detail Record (CDR) Information\n");
3760         ast_cli(a->fd, "--------------------------------------------------\n");
3761         ast_cli(a->fd, TITLE_STRING, "Channel", "Dst. Channel", "LastApp", "Start", "Answer", "End", "Billsec", "Duration");
3762
3763         it_cdrs = ao2_iterator_init(active_cdrs_by_channel, 0);
3764         for (; (cdr = ao2_iterator_next(&it_cdrs)); ao2_cleanup(cdr)) {
3765                 struct cdr_object *it_cdr;
3766                 struct timeval start_time = { 0, };
3767                 struct timeval answer_time = { 0, };
3768                 struct timeval end_time = { 0, };
3769
3770                 SCOPED_AO2LOCK(lock, cdr);
3771
3772                 /* Calculate the start, end, answer, billsec, and duration over the
3773                  * life of all of the CDR entries
3774                  */
3775                 for (it_cdr = cdr; it_cdr; it_cdr = it_cdr->next) {
3776                         if (snapshot_is_dialed(it_cdr->party_a.snapshot)) {
3777                                 continue;
3778                         }
3779                         if (ast_tvzero(start_time)) {
3780                                 start_time = it_cdr->start;
3781                         }
3782                         if (!ast_tvzero(it_cdr->answer) && ast_tvzero(answer_time)) {
3783                                 answer_time = it_cdr->answer;
3784                         }
3785                 }
3786
3787                 /* If there was no start time, then all CDRs were for a dialed channel; skip */
3788                 if (ast_tvzero(start_time)) {
3789                         continue;
3790                 }
3791                 it_cdr = cdr->last;
3792
3793                 end_time = ast_tvzero(it_cdr->end) ? ast_tvnow() : it_cdr->end;
3794                 cdr_get_tv(start_time, "%T", start_time_buffer, sizeof(start_time_buffer));
3795                 cdr_get_tv(answer_time, "%T", answer_time_buffer, sizeof(answer_time_buffer));
3796                 cdr_get_tv(end_time, "%T", end_time_buffer, sizeof(end_time_buffer));