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