Debian Codegraph @

main/cel.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2007 - 2009, Digium, Inc.
5 *
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
11 *
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
15 */
16
17 /*!
18 * \file
19 *
20 * \brief Channel Event Logging API
21 *
22 * \author Steve Murphy <[email protected]>
23 * \author Russell Bryant <[email protected]>
24 */
25
26 /*! \li \ref cel.c uses the configuration file \ref cel.conf
27 * \addtogroup configuration_file Configuration Files
28 */
29
30 /*!
31 * \page cel.conf cel.conf
32 * \verbinclude cel.conf.sample
33 */
34
35 /*** MODULEINFO
36 <support_level>core</support_level>
37 ***/
38
39 #include "asterisk.h"
40
41 #include "asterisk/module.h"
42
43 #include "asterisk/channel.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/cel.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/linkedlists.h"
48 #include "asterisk/utils.h"
49 #include "asterisk/config.h"
50 #include "asterisk/config_options.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/astobj2.h"
53 #include "asterisk/stasis_message_router.h"
54 #include "asterisk/stasis_channels.h"
55 #include "asterisk/stasis_bridges.h"
56 #include "asterisk/bridge.h"
57 #include "asterisk/parking.h"
58 #include "asterisk/pickup.h"
59 #include "asterisk/core_local.h"
60 #include "asterisk/taskprocessor.h"
61
62 /*** DOCUMENTATION
63 <configInfo name="cel" language="en_US">
64 <configFile name="cel.conf">
65 <configObject name="general">
66 <since>
67 <version>12.0.0</version>
68 </since>
69 <synopsis>Options that apply globally to Channel Event Logging (CEL)</synopsis>
70 <configOption name="enable">
71 <since>
72 <version>12.0.0</version>
73 </since>
74 <synopsis>Determines whether CEL is enabled</synopsis>
75 </configOption>
76 <configOption name="dateformat">
77 <since>
78 <version>12.0.0</version>
79 </since>
80 <synopsis>The format to be used for dates when logging</synopsis>
81 </configOption>
82 <configOption name="apps">
83 <since>
84 <version>12.0.0</version>
85 </since>
86 <synopsis>List of apps for CEL to track</synopsis>
87 <description><para>A case-insensitive, comma-separated list of applications
88 to track when one or both of APP_START and APP_END events are flagged for
89 tracking</para></description>
90 </configOption>
91 <configOption name="events">
92 <since>
93 <version>12.0.0</version>
94 </since>
95 <synopsis>List of events for CEL to track</synopsis>
96 <description><para>A case-sensitive, comma-separated list of event names
97 to track. These event names do not include the leading <literal>AST_CEL</literal>.
98 </para>
99 <enumlist>
100 <enum name="ALL">
101 <para>Special value which tracks all events.</para>
102 </enum>
103 <enum name="CHAN_START"/>
104 <enum name="CHAN_END"/>
105 <enum name="ANSWER"/>
106 <enum name="HANGUP"/>
107 <enum name="APP_START"/>
108 <enum name="APP_END"/>
109 <enum name="PARK_START"/>
110 <enum name="PARK_END"/>
111 <enum name="USER_DEFINED"/>
112 <enum name="BRIDGE_ENTER"/>
113 <enum name="BRIDGE_EXIT"/>
114 <enum name="BLINDTRANSFER"/>
115 <enum name="ATTENDEDTRANSFER"/>
116 <enum name="PICKUP"/>
117 <enum name="FORWARD"/>
118 <enum name="LINKEDID_END"/>
119 <enum name="LOCAL_OPTIMIZE"/>
120 <enum name="LOCAL_OPTIMIZE_BEGIN"/>
121 <enum name="STREAM_BEGIN"/>
122 <enum name="STREAM_END"/>
123 <enum name="DTMF"/>
124 </enumlist>
125 </description>
126 </configOption>
127 </configObject>
128 </configFile>
129 </configInfo>
130 ***/
131
132 /*! Message router for state that CEL needs to know about */
133 static struct stasis_message_router *cel_state_router;
134
135 /*! Topic for CEL-specific messages */
136 static struct stasis_topic *cel_topic;
137
138 /*! Aggregation topic for all topics CEL needs to know about */
139 static struct stasis_topic *cel_aggregation_topic;
140
141 /*! Subscription for forwarding the channel caching topic */
142 static struct stasis_forward *cel_channel_forwarder;
143
144 /*! Subscription for forwarding the channel caching topic */
145 static struct stasis_forward *cel_bridge_forwarder;
146
147 /*! Subscription for forwarding the parking topic */
148 static struct stasis_forward *cel_parking_forwarder;
149
150 /*! Subscription for forwarding the CEL-specific topic */
151 static struct stasis_forward *cel_cel_forwarder;
152
153 struct stasis_message_type *cel_generic_type(void);
154 STASIS_MESSAGE_TYPE_DEFN(cel_generic_type);
155
156 /*! Container for CEL backend information */
157 static AO2_GLOBAL_OBJ_STATIC(cel_backends);
158
159 /*! The number of buckets into which backend names will be hashed */
160 #define BACKEND_BUCKETS 13
161
162 /*! Container for dial end multichannel blobs for holding on to dial statuses */
163 static AO2_GLOBAL_OBJ_STATIC(cel_dialstatus_store);
164
165 /*!
166 * \brief Maximum possible CEL event IDs
167 * \note This limit is currently imposed by the eventset definition
168 */
169 #define CEL_MAX_EVENT_IDS 64
170
171 /*!
172 * \brief Number of buckets for the appset container
173 */
174 #define NUM_APP_BUCKETS 97
175
176 /*!
177 * \brief Number of buckets for the dialstatus container
178 */
179 #define NUM_DIALSTATUS_BUCKETS 251
180
181 struct cel_linkedid {
182 /*! Number of channels with this linkedid. */
183 unsigned int count;
184 /*! Linkedid stored at end of struct. */
185 char id[0];
186 };
187
188 /*! Container of channel references to a linkedid for CEL purposes. */
189 static AO2_GLOBAL_OBJ_STATIC(cel_linkedids);
190
191 struct cel_dialstatus {
192 /*! Uniqueid of the channel */
193 char uniqueid[AST_MAX_UNIQUEID];
194 /*! The dial status */
195 char dialstatus[0];
196 };
197
198 /*! \brief Destructor for cel_config */
199 static void cel_general_config_dtor(void *obj)
200 {
201 struct ast_cel_general_config *cfg = obj;
202 ast_string_field_free_memory(cfg);
203 ao2_cleanup(cfg->apps);
204 cfg->apps = NULL;
205 }
206
207 void *ast_cel_general_config_alloc(void)
208 {
209 RAII_VAR(struct ast_cel_general_config *, cfg, NULL, ao2_cleanup);
210
211 if (!(cfg = ao2_alloc(sizeof(*cfg), cel_general_config_dtor))) {
212 return NULL;
213 }
214
215 if (ast_string_field_init(cfg, 64)) {
216 return NULL;
217 }
218
219 if (!(cfg->apps = ast_str_container_alloc(NUM_APP_BUCKETS))) {
220 return NULL;
221 }
222
223 ao2_ref(cfg, +1);
224 return cfg;
225 }
226
227 /*! \brief A container that holds all config-related information */
228 struct cel_config {
229 struct ast_cel_general_config *general;
230 };
231
232
233 static AO2_GLOBAL_OBJ_STATIC(cel_configs);
234
235 /*! \brief Destructor for cel_config */
236 static void cel_config_dtor(void *obj)
237 {
238 struct cel_config *cfg = obj;
239 ao2_cleanup(cfg->general);
240 cfg->general = NULL;
241 }
242
243 static void *cel_config_alloc(void)
244 {
245 RAII_VAR(struct cel_config *, cfg, NULL, ao2_cleanup);
246
247 if (!(cfg = ao2_alloc(sizeof(*cfg), cel_config_dtor))) {
248 return NULL;
249 }
250
251 if (!(cfg->general = ast_cel_general_config_alloc())) {
252 return NULL;
253 }
254
255 ao2_ref(cfg, +1);
256 return cfg;
257 }
258
259 /*! \brief An aco_type structure to link the "general" category to the ast_cel_general_config type */
260 static struct aco_type general_option = {
261 .type = ACO_GLOBAL,
262 .name = "general",
263 .item_offset = offsetof(struct cel_config, general),
264 .category_match = ACO_WHITELIST_EXACT,
265 .category = "general",
266 };
267
268 /*! Config sections used by existing modules. Do not add to this list. */
269 static const char *ignore_categories[] = {
270 "manager",
271 "radius",
272 NULL,
273 };
274
275 static struct aco_type ignore_option = {
276 .type = ACO_IGNORE,
277 .name = "modules",
278 .category = (const char*)ignore_categories,
279 .category_match = ACO_WHITELIST_ARRAY,
280 };
281
282 /*! \brief The config file to be processed for the module. */
283 static struct aco_file cel_conf = {
284 .filename = "cel.conf", /*!< The name of the config file */
285 .types = ACO_TYPES(&general_option, &ignore_option), /*!< The mapping object types to be processed */
286 };
287
288 static int cel_pre_apply_config(void);
289
290 CONFIG_INFO_CORE("cel", cel_cfg_info, cel_configs, cel_config_alloc,
291 .files = ACO_FILES(&cel_conf),
292 .pre_apply_config = cel_pre_apply_config,
293 );
294
295 static int cel_pre_apply_config(void)
296 {
297 struct cel_config *cfg = aco_pending_config(&cel_cfg_info);
298
299 if (!cfg->general) {
300 return -1;
301 }
302
303 if (!ao2_container_count(cfg->general->apps)) {
304 return 0;
305 }
306
307 if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_START)) {
308 return 0;
309 }
310
311 if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_END)) {
312 return 0;
313 }
314
315 ast_log(LOG_ERROR, "Applications are listed to be tracked, but APP events are not tracked\n");
316 return -1;
317 }
318
319 static struct aco_type *general_options[] = ACO_TYPES(&general_option);
320
321 /*!
322 * \brief Map of ast_cel_event_type to strings
323 */
324 static const char * const cel_event_types[CEL_MAX_EVENT_IDS] = {
325 [AST_CEL_ALL] = "ALL",
326 [AST_CEL_CHANNEL_START] = "CHAN_START",
327 [AST_CEL_CHANNEL_END] = "CHAN_END",
328 [AST_CEL_ANSWER] = "ANSWER",
329 [AST_CEL_HANGUP] = "HANGUP",
330 [AST_CEL_APP_START] = "APP_START",
331 [AST_CEL_APP_END] = "APP_END",
332 [AST_CEL_PARK_START] = "PARK_START",
333 [AST_CEL_PARK_END] = "PARK_END",
334 [AST_CEL_USER_DEFINED] = "USER_DEFINED",
335 [AST_CEL_BRIDGE_ENTER] = "BRIDGE_ENTER",
336 [AST_CEL_BRIDGE_EXIT] = "BRIDGE_EXIT",
337 [AST_CEL_BLINDTRANSFER] = "BLINDTRANSFER",
338 [AST_CEL_ATTENDEDTRANSFER] = "ATTENDEDTRANSFER",
339 [AST_CEL_PICKUP] = "PICKUP",
340 [AST_CEL_FORWARD] = "FORWARD",
341 [AST_CEL_LINKEDID_END] = "LINKEDID_END",
342 [AST_CEL_LOCAL_OPTIMIZE] = "LOCAL_OPTIMIZE",
343 [AST_CEL_LOCAL_OPTIMIZE_BEGIN] = "LOCAL_OPTIMIZE_BEGIN",
344 [AST_CEL_STREAM_BEGIN] = "STREAM_BEGIN",
345 [AST_CEL_STREAM_END] = "STREAM_END",
346 [AST_CEL_DTMF] = "DTMF",
347 };
348
349 struct cel_backend {
350 ast_cel_backend_cb callback; /*!< Callback for this backend */
351 char name[0]; /*!< Name of this backend */
352 };
353
354 /*! \brief Hashing function for cel_backend */
355 AO2_STRING_FIELD_HASH_FN(cel_backend, name)
356
357 /*! \brief Comparator function for cel_backend */
358 AO2_STRING_FIELD_CMP_FN(cel_backend, name)
359
360 /*! \brief Hashing function for dialstatus container */
361 AO2_STRING_FIELD_HASH_FN(cel_dialstatus, uniqueid)
362
363 /*! \brief Comparator function for dialstatus container */
364 AO2_STRING_FIELD_CMP_FN(cel_dialstatus, uniqueid)
365
366 unsigned int ast_cel_check_enabled(void)
367 {
368 unsigned int enabled;
369 struct cel_config *cfg = ao2_global_obj_ref(cel_configs);
370
371 enabled = (!cfg || !cfg->general) ? 0 : cfg->general->enable;
372 ao2_cleanup(cfg);
373 return enabled;
374 }
375
376 static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
377 {
378 unsigned int i;
379 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
380 RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
381 struct ao2_iterator iter;
382 char *app;
383
384 switch (cmd) {
385 case CLI_INIT:
386 e->command = "cel show status";
387 e->usage =
388 "Usage: cel show status\n"
389 " Displays the Channel Event Logging system status.\n";
390 return NULL;
391 case CLI_GENERATE:
392 return NULL;
393 case CLI_HANDLER:
394 break;
395 }
396
397 if (a->argc > 3) {
398 return CLI_SHOWUSAGE;
399 }
400
401 ast_cli(a->fd, "CEL Logging: %s\n", ast_cel_check_enabled() ? "Enabled" : "Disabled");
402
403 if (!cfg || !cfg->general || !cfg->general->enable) {
404 return CLI_SUCCESS;
405 }
406
407 for (i = 0; i < (sizeof(cfg->general->events) * 8); i++) {
408 const char *name;
409
410 if (!(cfg->general->events & ((int64_t) 1 << i))) {
411 continue;
412 }
413
414 name = ast_cel_get_type_name(i);
415 if (strcasecmp(name, "Unknown")) {
416 ast_cli(a->fd, "CEL Tracking Event: %s\n", name);
417 }
418 }
419
420 iter = ao2_iterator_init(cfg->general->apps, 0);
421 for (; (app = ao2_iterator_next(&iter)); ao2_ref(app, -1)) {
422 ast_cli(a->fd, "CEL Tracking Application: %s\n", app);
423 }
424 ao2_iterator_destroy(&iter);
425
426 if (backends) {
427 struct cel_backend *backend;
428
429 iter = ao2_iterator_init(backends, 0);
430 for (; (backend = ao2_iterator_next(&iter)); ao2_ref(backend, -1)) {
431 ast_cli(a->fd, "CEL Event Subscriber: %s\n", backend->name);
432 }
433 ao2_iterator_destroy(&iter);
434 }
435
436 return CLI_SUCCESS;
437 }
438
439 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CEL status");
440
441 enum ast_cel_event_type ast_cel_str_to_event_type(const char *name)
442 {
443 unsigned int i;
444
445 for (i = 0; i < ARRAY_LEN(cel_event_types); i++) {
446 if (cel_event_types[i] && !strcasecmp(name, cel_event_types[i])) {
447 return i;
448 }
449 }
450
451 ast_log(LOG_ERROR, "Unknown event name '%s'\n", name);
452 return AST_CEL_INVALID_VALUE;
453 }
454
455 static int ast_cel_track_event(enum ast_cel_event_type et)
456 {
457 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
458
459 if (!cfg || !cfg->general) {
460 return 0;
461 }
462
463 return (cfg->general->events & ((int64_t) 1 << et)) ? 1 : 0;
464 }
465
466 static int events_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
467 {
468 struct ast_cel_general_config *cfg = obj;
469 char *events = ast_strdupa(var->value);
470 char *cur_event;
471
472 while ((cur_event = strsep(&events, ","))) {
473 enum ast_cel_event_type event_type;
474
475 cur_event = ast_strip(cur_event);
476 if (ast_strlen_zero(cur_event)) {
477 continue;
478 }
479
480 event_type = ast_cel_str_to_event_type(cur_event);
481
482 if (event_type == AST_CEL_ALL) {
483 /* All events */
484 cfg->events = (int64_t) -1;
485 } else if (event_type == AST_CEL_INVALID_VALUE) {
486 return -1;
487 } else {
488 cfg->events |= ((int64_t) 1 << event_type);
489 }
490 }
491
492 return 0;
493 }
494
495 static int apps_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
496 {
497 struct ast_cel_general_config *cfg = obj;
498 char *apps = ast_strdupa(var->value);
499 char *cur_app;
500
501 while ((cur_app = strsep(&apps, ","))) {
502 cur_app = ast_strip(cur_app);
503 if (ast_strlen_zero(cur_app)) {
504 continue;
505 }
506
507 cur_app = ast_str_to_lower(cur_app);
508 ast_str_container_add(cfg->apps, cur_app);
509 }
510
511 return 0;
512 }
513
514 const char *ast_cel_get_type_name(enum ast_cel_event_type type)
515 {
516 return S_OR(cel_event_types[type], "Unknown");
517 }
518
519 static int cel_track_app(const char *const_app)
520 {
521 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
522 RAII_VAR(char *, app, NULL, ao2_cleanup);
523 char *app_lower;
524
525 if (!cfg || !cfg->general) {
526 return 0;
527 }
528
529 app_lower = ast_str_to_lower(ast_strdupa(const_app));
530 app = ao2_find(cfg->general->apps, app_lower, OBJ_SEARCH_KEY);
531 if (!app) {
532 return 0;
533 }
534
535 return 1;
536 }
537
538 static int cel_linkedid_ref(const char *linkedid);
539
540 struct ast_event *ast_cel_create_event(struct ast_channel_snapshot *snapshot,
541 enum ast_cel_event_type event_type, const char *userdefevname,
542 struct ast_json *extra, const char *peer)
543 {
544 struct timeval eventtime = ast_tvnow();
545
546 return ast_cel_create_event_with_time(snapshot, event_type, &eventtime,
547 userdefevname, extra, peer);
548 }
549
550 struct ast_event *ast_cel_create_event_with_time(struct ast_channel_snapshot *snapshot,
551 enum ast_cel_event_type event_type, const struct timeval *event_time,
552 const char *userdefevname, struct ast_json *extra, const char *peer)
553 {
554 RAII_VAR(char *, extra_txt, NULL, ast_json_free);
555 if (extra) {
556 extra_txt = ast_json_dump_string(extra);
557 }
558 return ast_event_new(AST_EVENT_CEL,
559 AST_EVENT_IE_CEL_EVENT_TYPE, AST_EVENT_IE_PLTYPE_UINT, event_type,
560 AST_EVENT_IE_CEL_EVENT_TIME, AST_EVENT_IE_PLTYPE_UINT, event_time->tv_sec,
561 AST_EVENT_IE_CEL_EVENT_TIME_USEC, AST_EVENT_IE_PLTYPE_UINT, event_time->tv_usec,
562 AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, S_OR(userdefevname, ""),
563 AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->caller->name,
564 AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR, snapshot->caller->number,
565 AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR, snapshot->caller->ani,
566 AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR, snapshot->caller->rdnis,
567 AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR, snapshot->caller->dnid,
568 AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, snapshot->dialplan->exten,
569 AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, snapshot->dialplan->context,
570 AST_EVENT_IE_CEL_CHANNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->base->name,
571 AST_EVENT_IE_CEL_APPNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->dialplan->appl,
572 AST_EVENT_IE_CEL_APPDATA, AST_EVENT_IE_PLTYPE_STR, snapshot->dialplan->data,
573 AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, snapshot->amaflags,
574 AST_EVENT_IE_CEL_ACCTCODE, AST_EVENT_IE_PLTYPE_STR, snapshot->base->accountcode,
575 AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, snapshot->peer->account,
576 AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, snapshot->base->uniqueid,
577 AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, snapshot->peer->linkedid,
578 AST_EVENT_IE_CEL_TENANTID, AST_EVENT_IE_PLTYPE_STR, snapshot->base->tenantid,
579 AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, snapshot->base->userfield,
580 AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, S_OR(extra_txt, ""),
581 AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, S_OR(peer, ""),
582 AST_EVENT_IE_END);
583 }
584
585 static int cel_backend_send_cb(void *obj, void *arg, int flags)
586 {
587 struct cel_backend *backend = obj;
588
589 backend->callback(arg);
590 return 0;
591 }
592
593 static int cel_report_event(struct ast_channel_snapshot *snapshot,
594 enum ast_cel_event_type event_type, const struct timeval *event_time,
595 const char *userdefevname, struct ast_json *extra,
596 const char *peer_str)
597 {
598 struct ast_event *ev;
599 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
600 RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
601
602 if (!cfg || !cfg->general || !cfg->general->enable || !backends) {
603 return 0;
604 }
605
606 /* Record the linkedid of new channels if we are tracking LINKEDID_END even if we aren't
607 * reporting on CHANNEL_START so we can track when to send LINKEDID_END */
608 if (event_type == AST_CEL_CHANNEL_START
609 && ast_cel_track_event(AST_CEL_LINKEDID_END)) {
610 if (cel_linkedid_ref(snapshot->peer->linkedid)) {
611 return -1;
612 }
613 }
614
615 if (!ast_cel_track_event(event_type)) {
616 return 0;
617 }
618
619 if ((event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END)
620 && !cel_track_app(snapshot->dialplan->appl)) {
621 return 0;
622 }
623
624 ev = ast_cel_create_event_with_time(snapshot, event_type, event_time, userdefevname, extra, peer_str);
625 if (!ev) {
626 return -1;
627 }
628
629 /* Distribute event to backends */
630 ao2_callback(backends, OBJ_MULTIPLE | OBJ_NODATA, cel_backend_send_cb, ev);
631 ast_event_destroy(ev);
632
633 return 0;
634 }
635
636 /* called whenever a channel is destroyed or a linkedid is changed to
637 * potentially emit a CEL_LINKEDID_END event */
638 static void check_retire_linkedid(struct ast_channel_snapshot *snapshot, const struct timeval *event_time)
639 {
640 RAII_VAR(struct ao2_container *, linkedids, ao2_global_obj_ref(cel_linkedids), ao2_cleanup);
641 struct cel_linkedid *lid;
642
643 if (!linkedids || ast_strlen_zero(snapshot->peer->linkedid)) {
644 /* The CEL module is shutdown. Abort. */
645 return;
646 }
647
648 ao2_lock(linkedids);
649
650 lid = ao2_find(linkedids, (void *) snapshot->peer->linkedid, OBJ_SEARCH_KEY);
651 if (!lid) {
652 ao2_unlock(linkedids);
653
654 /*
655 * The user may have done a reload to start tracking linkedids
656 * when a call was already in progress. This is an unusual kind
657 * of change to make after starting Asterisk.
658 */
659 ast_log(LOG_ERROR, "Something weird happened, couldn't find linkedid %s\n",
660 snapshot->peer->linkedid);
661 return;
662 }
663
664 if (!--lid->count) {
665 /* No channels use this linkedid anymore. */
666 ao2_unlink(linkedids, lid);
667 ao2_unlock(linkedids);
668
669 cel_report_event(snapshot, AST_CEL_LINKEDID_END, event_time, NULL, NULL, NULL);
670 } else {
671 ao2_unlock(linkedids);
672 }
673 ao2_ref(lid, -1);
674 }
675
676 static int cel_format_eventtime(struct cel_config *cfg, struct timeval eventtime, char *timebuf, size_t len)
677 {
678 if (!timebuf || len < 30) {
679 return -1;
680 }
681
682 if (ast_strlen_zero(cfg->general->date_format)) {
683 snprintf(timebuf, len, "%ld.%06ld", (long) eventtime.tv_sec,
684 (long) eventtime.tv_usec);
685 } else {
686 struct ast_tm tm;
687 ast_localtime(&eventtime, &tm, NULL);
688 ast_strftime(timebuf, len, cfg->general->date_format, &tm);
689 }
690
691 return 0;
692 }
693
694 int ast_cel_format_eventtime(struct timeval eventtime, char *timebuf, size_t len)
695 {
696 struct cel_config *cfg = ao2_global_obj_ref(cel_configs);
697 int res = 0;
698
699 if (!cfg) {
700 return -1;
701 }
702
703 res = cel_format_eventtime(cfg, eventtime, timebuf, len);
704 ao2_cleanup(cfg);
705
706 return res;
707 }
708
709 /* Note that no 'chan_fixup' function is provided for this datastore type,
710 * because the channels that will use it will never be involved in masquerades.
711 */
712 static const struct ast_datastore_info fabricated_channel_datastore = {
713 .type = "CEL fabricated channel",
714 .destroy = ast_free_ptr,
715 };
716
717 struct ast_channel *ast_cel_fabricate_channel_from_event(const struct ast_event *event)
718 {
719 struct varshead *headp;
720 struct ast_var_t *newvariable;
721 const char *mixed_name;
722 char timebuf[30];
723 struct ast_channel *tchan;
724 struct ast_cel_event_record record = {
725 .version = AST_CEL_EVENT_RECORD_VERSION,
726 };
727 struct ast_datastore *datastore;
728 char *app_data;
729 RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
730
731 if (!cfg || !cfg->general) {
732 return NULL;
733 }
734
735 /* do not call ast_channel_alloc because this is not really a real channel */
736 if (!(tchan = ast_dummy_channel_alloc())) {
737 return NULL;
738 }
739
740 headp = ast_channel_varshead(tchan);
741
742 /* first, get the variables from the event */
743 if (ast_cel_fill_record(event, &record)) {
744 ast_channel_unref(tchan);
745 return NULL;
746 }
747
748 /* next, fill the channel with their data */
749 mixed_name = (record.event_type == AST_CEL_USER_DEFINED)
750 ? record.user_defined_name : record.event_name;
751 if ((newvariable = ast_var_assign("eventtype", mixed_name))) {
752 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
753 }
754
755 cel_format_eventtime(cfg, record.event_time, timebuf, sizeof(timebuf));
756
757 if ((newvariable = ast_var_assign("eventtime", timebuf))) {
758 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
759 }
760
761 if ((newvariable = ast_var_assign("eventenum", record.event_name))) {
762 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
763 }
764 if ((newvariable = ast_var_assign("userdeftype", record.user_defined_name))) {
765 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
766 }
767 if ((newvariable = ast_var_assign("eventextra", record.extra))) {
768 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
769 }
770
771 ast_channel_caller(tchan)->id.name.valid = 1;
772 ast_channel_caller(tchan)->id.name.str = ast_strdup(record.caller_id_name);
773 ast_channel_caller(tchan)->id.number.valid = 1;
774 ast_channel_caller(tchan)->id.number.str = ast_strdup(record.caller_id_num);
775 ast_channel_caller(tchan)->ani.number.valid = 1;
776 ast_channel_caller(tchan)->ani.number.str = ast_strdup(record.caller_id_ani);
777 ast_channel_redirecting(tchan)->from.number.valid = 1;
778 ast_channel_redirecting(tchan)->from.number.str = ast_strdup(record.caller_id_rdnis);
779 ast_channel_dialed(tchan)->number.str = ast_strdup(record.caller_id_dnid);
780
781 ast_channel_exten_set(tchan, record.extension);
782 ast_channel_context_set(tchan, record.context);
783 ast_channel_name_set(tchan, record.channel_name);
784 ast_channel_internal_set_fake_ids(tchan, record.unique_id, record.linked_id);
785 ast_channel_accountcode_set(tchan, record.account_code);
786 ast_channel_peeraccount_set(tchan, record.peer_account);
787 ast_channel_userfield_set(tchan, record.user_field);
788 ast_channel_tenantid_set(tchan, record.tenant_id);
789
790 if ((newvariable = ast_var_assign("BRIDGEPEER", record.peer))) {
791 AST_LIST_INSERT_HEAD(headp, newvariable, entries);
792 }
793
794 ast_channel_amaflags_set(tchan, record.amaflag);
795
796 /* We need to store an 'application name' and 'application
797 * data' on the channel for logging purposes, but the channel
798 * structure only provides a place to store pointers, and it
799 * expects these pointers to be pointing to data that does not
800 * need to be freed. This means that the channel's destructor
801 * does not attempt to free any storage that these pointers
802 * point to. However, we can't provide data in that form directly for
803 * these structure members. In order to ensure that these data
804 * elements have a lifetime that matches the channel's
805 * lifetime, we'll put them in a datastore attached to the
806 * channel, and set's the channel's pointers to point into the
807 * datastore. The datastore will then be automatically destroyed
808 * when the channel is destroyed.
809 */
810
811 if (!(datastore = ast_datastore_alloc(&fabricated_channel_datastore, NULL))) {
812 ast_channel_unref(tchan);
813 return NULL;
814 }
815
816 if (!(app_data = ast_malloc(strlen(record.application_name) + strlen(record.application_data) + 2))) {
817 ast_datastore_free(datastore);
818 ast_channel_unref(tchan);
819 return NULL;
820 }
821
822 ast_channel_appl_set(tchan, strcpy(app_data, record.application_name));
823 ast_channel_data_set(tchan, strcpy(app_data + strlen(record.application_name) + 1,
824 record.application_data));
825
826 datastore->data = app_data;
827 ast_channel_datastore_add(tchan, datastore);
828
829 return tchan;
830 }
831
832 static int cel_linkedid_ref(const char *linkedid)
833 {
834 RAII_VAR(struct ao2_container *, linkedids, ao2_global_obj_ref(cel_linkedids), ao2_cleanup);
835 struct cel_linkedid *lid;
836
837 if (ast_strlen_zero(linkedid)) {
838 ast_log(LOG_ERROR, "The linkedid should never be empty\n");
839 return -1;
840 }
841 if (!linkedids) {
842 /* The CEL module is shutdown. Abort. */
843 return -1;
844 }
845
846 ao2_lock(linkedids);
847 lid = ao2_find(linkedids, (void *) linkedid, OBJ_SEARCH_KEY);
848 if (!lid) {
849 /*
850 * Changes to the lid->count member are protected by the
851 * container lock so the lid object does not need its own lock.
852 */
853 lid = ao2_alloc_options(sizeof(*lid) + strlen(linkedid) + 1, NULL,
854 AO2_ALLOC_OPT_LOCK_NOLOCK);
855 if (!lid) {
856 ao2_unlock(linkedids);
857 return -1;
858 }
859 strcpy(lid->id, linkedid);/* Safe */
860
861 ao2_link(linkedids, lid);
862 }
863 ++lid->count;
864 ao2_unlock(linkedids);
865 ao2_ref(lid, -1);
866
867 return 0;
868 }
869
870 int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *r)
871 {
872 if (r->version != AST_CEL_EVENT_RECORD_VERSION) {
873 ast_log(LOG_ERROR, "Module ABI mismatch for ast_cel_event_record. "
874 "Please ensure all modules were compiled for "
875 "this version of Asterisk.\n");
876 return -1;
877 }
878
879 r->event_type = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TYPE);
880
881 r->event_time.tv_sec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME);
882 r->event_time.tv_usec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME_USEC);
883
884 r->event_name = ast_cel_get_type_name(r->event_type);
885
886 r->user_defined_name= S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USEREVENT_NAME), "");
887 r->caller_id_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNAME), "");
888 r->caller_id_num = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNUM), "");
889 r->caller_id_ani = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDANI), "");
890 r->caller_id_rdnis = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDRDNIS), "");
891 r->caller_id_dnid = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDDNID), "");
892 r->extension = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTEN), "");
893 r->context = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CONTEXT), "");
894 r->channel_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CHANNAME), "");
895 r->application_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPNAME), "");
896 r->application_data = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPDATA), "");
897 r->account_code = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
898 r->peer_account = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEERACCT), "");
899 r->unique_id = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_UNIQUEID), "");
900 r->linked_id = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_LINKEDID), "");
901 r->tenant_id = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_TENANTID), "");
902 r->amaflag = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_AMAFLAGS);
903 r->user_field = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USERFIELD), "");
904 r->peer = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEER), "");
905 r->extra = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTRA), "");
906
907 return 0;
908 }
909
910 /*! \brief Typedef for callbacks that get called on channel snapshot updates */
911 typedef void (*cel_channel_snapshot_monitor)(
912 struct ast_channel_snapshot *old_snapshot,
913 struct ast_channel_snapshot *new_snapshot,
914 const struct timeval *event_time);
915
916 static struct cel_dialstatus *get_dialstatus(const char *uniqueid)
917 {
918 struct ao2_container *dial_statuses = ao2_global_obj_ref(cel_dialstatus_store);
919 struct cel_dialstatus *dialstatus = NULL;
920
921 if (dial_statuses) {
922 dialstatus = ao2_find(dial_statuses, uniqueid, OBJ_SEARCH_KEY | OBJ_UNLINK);
923 ao2_ref(dial_statuses, -1);
924 }
925 return dialstatus;
926 }
927
928 static const char *get_blob_variable(struct ast_multi_channel_blob *blob, const char *varname)
929 {
930 struct ast_json *json = ast_multi_channel_blob_get_json(blob);
931 if (!json) {
932 return NULL;
933 }
934
935 json = ast_json_object_get(json, varname);
936 if (!json) {
937 return NULL;
938 }
939
940 return ast_json_string_get(json);
941 }
942
943 /*! \brief Handle channel state changes */
944 static void cel_channel_state_change(
945 struct ast_channel_snapshot *old_snapshot,
946 struct ast_channel_snapshot *new_snapshot,
947 const struct timeval *event_time)
948 {
949 int is_hungup, was_hungup;
950
951 if (!old_snapshot) {
952 cel_report_event(new_snapshot, AST_CEL_CHANNEL_START, event_time, NULL, NULL, NULL);
953 return;
954 }
955
956 was_hungup = ast_test_flag(&old_snapshot->flags, AST_FLAG_DEAD) ? 1 : 0;
957 is_hungup = ast_test_flag(&new_snapshot->flags, AST_FLAG_DEAD) ? 1 : 0;
958
959 if (!was_hungup && is_hungup) {
960 struct ast_json *extra;
961 struct cel_dialstatus *dialstatus = get_dialstatus(new_snapshot->base->uniqueid);
962
963 extra = ast_json_pack("{s: i, s: s, s: s}",
964 "hangupcause", new_snapshot->hangup->cause,
965 "hangupsource", new_snapshot->hangup->source,
966 "dialstatus", dialstatus ? dialstatus->dialstatus : "");
967 cel_report_event(new_snapshot, AST_CEL_HANGUP, event_time, NULL, extra, NULL);
968 ast_json_unref(extra);
969 ao2_cleanup(dialstatus);
970
971 cel_report_event(new_snapshot, AST_CEL_CHANNEL_END, event_time, NULL, NULL, NULL);
972 if (ast_cel_track_event(AST_CEL_LINKEDID_END)) {
973 check_retire_linkedid(new_snapshot, event_time);
974 }
975 return;
976 }
977
978 if (old_snapshot->state != new_snapshot->state && new_snapshot->state == AST_STATE_UP) {
979 cel_report_event(new_snapshot, AST_CEL_ANSWER, event_time, NULL, NULL, NULL);
980 return;
981 }
982 }
983
984 static void cel_channel_linkedid_change(
985 struct ast_channel_snapshot *old_snapshot,
986 struct ast_channel_snapshot *new_snapshot,
987 const struct timeval *event_time)
988 {
989 if (!old_snapshot) {
990 return;
991 }
992
993 ast_assert(!ast_strlen_zero(new_snapshot->peer->linkedid));
994 ast_assert(!ast_strlen_zero(old_snapshot->peer->linkedid));
995
996 if (ast_cel_track_event(AST_CEL_LINKEDID_END)
997 && strcmp(old_snapshot->peer->linkedid, new_snapshot->peer->linkedid)) {
998 cel_linkedid_ref(new_snapshot->peer->linkedid);
999 check_retire_linkedid(old_snapshot, event_time);
1000 }
1001 }
1002
1003 static void cel_channel_app_change(
1004 struct ast_channel_snapshot *old_snapshot,
1005 struct ast_channel_snapshot *new_snapshot,
1006 const struct timeval *event_time)
1007 {
1008 if (old_snapshot && !strcmp(old_snapshot->dialplan->appl, new_snapshot->dialplan->appl)) {
1009 return;
1010 }
1011
1012 /* old snapshot has an application, end it */
1013 if (old_snapshot && !ast_strlen_zero(old_snapshot->dialplan->appl)) {
1014 cel_report_event(old_snapshot, AST_CEL_APP_END, event_time, NULL, NULL, NULL);
1015 }
1016
1017 /* new snapshot has an application, start it */
1018 if (!ast_strlen_zero(new_snapshot->dialplan->appl)) {
1019 cel_report_event(new_snapshot, AST_CEL_APP_START, event_time, NULL, NULL, NULL);
1020 }
1021 }
1022
1023 /*! \brief Handlers for channel snapshot changes.
1024 * \note Order of the handlers matters. Application changes must come before state
1025 * changes to ensure that hangup notifications occur after application changes.
1026 * Linkedid checking should always come last.
1027 */
1028 cel_channel_snapshot_monitor cel_channel_monitors[] = {
1029 cel_channel_app_change,
1030 cel_channel_state_change,
1031 cel_channel_linkedid_change,
1032 };
1033
1034 static int cel_filter_channel_snapshot(struct ast_channel_snapshot *snapshot)
1035 {
1036 if (!snapshot) {
1037 return 0;
1038 }
1039 return snapshot->base->tech_properties & AST_CHAN_TP_INTERNAL;
1040 }
1041
1042 static void cel_snapshot_update_cb(void *data, struct stasis_subscription *sub,
1043 struct stasis_message *message)
1044 {
1045 struct ast_channel_snapshot_update *update = stasis_message_data(message);
1046 size_t i;
1047
1048 if (cel_filter_channel_snapshot(update->old_snapshot) || cel_filter_channel_snapshot(update->new_snapshot)) {
1049 return;
1050 }
1051
1052 for (i = 0; i < ARRAY_LEN(cel_channel_monitors); ++i) {
1053 cel_channel_monitors[i](update->old_snapshot, update->new_snapshot, stasis_message_timestamp(message));
1054 }
1055 }
1056
1057 static struct ast_str *cel_generate_peer_str(
1058 struct ast_bridge_snapshot *bridge,
1059 struct ast_channel_snapshot *chan)
1060 {
1061 struct ast_str *peer_str = ast_str_create(32);
1062 struct ao2_iterator i;
1063 char *current_chan = NULL;
1064
1065 if (!peer_str) {
1066 return NULL;
1067 }
1068
1069 for (i = ao2_iterator_init(bridge->channels, 0);
1070 (current_chan = ao2_iterator_next(&i));
1071 ao2_cleanup(current_chan)) {
1072 struct ast_channel_snapshot *current_snapshot;
1073
1074 /* Don't add the channel for which this message is being generated */
1075 if (!strcmp(current_chan, chan->base->uniqueid)) {
1076 continue;
1077 }
1078
1079 current_snapshot = ast_channel_snapshot_get_latest(current_chan);
1080 if (!current_snapshot) {
1081 continue;
1082 }
1083
1084 ast_str_append(&peer_str, 0, "%s,", current_snapshot->base->name);
1085 ao2_cleanup(current_snapshot);
1086 }
1087 ao2_iterator_destroy(&i);
1088
1089 /* Rip off the trailing comma */
1090 ast_str_truncate(peer_str, -1);
1091
1092 return peer_str;
1093 }
1094
1095 static void cel_bridge_enter_cb(
1096 void *data, struct stasis_subscription *sub,
1097 struct stasis_message *message)
1098 {
1099 struct ast_bridge_blob *blob = stasis_message_data(message);
1100 struct ast_bridge_snapshot *snapshot = blob->bridge;
1101 struct ast_channel_snapshot *chan_snapshot = blob->channel;
1102 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1103 RAII_VAR(struct ast_str *, peer_str, NULL, ast_free);
1104
1105 if (cel_filter_channel_snapshot(chan_snapshot)) {
1106 return;
1107 }
1108
1109 extra = ast_json_pack("{s: s, s: s}",
1110 "bridge_id", snapshot->uniqueid,
1111 "bridge_technology", snapshot->technology);
1112 if (!extra) {
1113 return;
1114 }
1115
1116 peer_str = cel_generate_peer_str(snapshot, chan_snapshot);
1117 if (!peer_str) {
1118 return;
1119 }
1120
1121 cel_report_event(chan_snapshot, AST_CEL_BRIDGE_ENTER, stasis_message_timestamp(message),
1122 NULL, extra, ast_str_buffer(peer_str));
1123 }
1124
1125 static void cel_bridge_leave_cb(
1126 void *data, struct stasis_subscription *sub,
1127 struct stasis_message *message)
1128 {
1129 struct ast_bridge_blob *blob = stasis_message_data(message);
1130 struct ast_bridge_snapshot *snapshot = blob->bridge;
1131 struct ast_channel_snapshot *chan_snapshot = blob->channel;
1132 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1133 RAII_VAR(struct ast_str *, peer_str, NULL, ast_free);
1134
1135 if (cel_filter_channel_snapshot(chan_snapshot)) {
1136 return;
1137 }
1138
1139 extra = ast_json_pack("{s: s, s: s}",
1140 "bridge_id", snapshot->uniqueid,
1141 "bridge_technology", snapshot->technology);
1142 if (!extra) {
1143 return;
1144 }
1145
1146 peer_str = cel_generate_peer_str(snapshot, chan_snapshot);
1147 if (!peer_str) {
1148 return;
1149 }
1150
1151 cel_report_event(chan_snapshot, AST_CEL_BRIDGE_EXIT, stasis_message_timestamp(message),
1152 NULL, extra, ast_str_buffer(peer_str));
1153 }
1154
1155 static void cel_parking_cb(
1156 void *data, struct stasis_subscription *sub,
1157 struct stasis_message *message)
1158 {
1159 struct ast_parked_call_payload *parked_payload = stasis_message_data(message);
1160 RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
1161 const char *reason = NULL;
1162
1163 switch (parked_payload->event_type) {
1164 case PARKED_CALL:
1165 extra = ast_json_pack("{s: s, s: s}",
1166 "parker_dial_string", parked_payload->parker_dial_string,
1167 "parking_lot", parked_payload->parkinglot);
1168 if (extra) {
1169 cel_report_event(parked_payload->parkee, AST_CEL_PARK_START, stasis_message_timestamp(message),
1170 NULL, extra, NULL);
1171 }
1172 return;
1173 case PARKED_CALL_TIMEOUT:
1174 reason = "ParkedCallTimeOut";
1175 break;
1176 case PARKED_CALL_GIVEUP:
1177 reason = "ParkedCallGiveUp";
1178 break;
1179 case PARKED_CALL_UNPARKED:
1180 reason = "ParkedCallUnparked";
1181 break;
1182 case PARKED_CALL_FAILED:
1183 reason = "ParkedCallFailed";
1184 break;
1185 case PARKED_CALL_SWAP:
1186 reason = "ParkedCallSwap";
1187 break;
1188 }
1189
1190 if (parked_payload->retriever) {
1191 extra = ast_json_pack("{s: s, s: s}",
1192 "reason", reason ?: "",
1193 "retriever", parked_payload->retriever->base->name);
1194 } else {
1195 extra = ast_json_pack("{s: s}", "reason", reason ?: "");
1196 }
1197
1198 if (extra) {
1199 cel_report_event(parked_payload->parkee, AST_CEL_PARK_END, stasis_message_timestamp(message),
1200 NULL, extra, NULL);
1201 }
1202 }
1203
1204 static void save_dialstatus(struct ast_multi_channel_blob *blob, struct ast_channel_snapshot *snapshot)
1205 {
1206 struct ao2_container *dial_statuses = ao2_global_obj_ref(cel_dialstatus_store);
1207 const char *dialstatus_string = get_blob_variable(blob, "dialstatus");
1208 struct cel_dialstatus *dialstatus;
1209 size_t dialstatus_string_len;
1210
1211 if (!dial_statuses || ast_strlen_zero(dialstatus_string)) {
1212 ao2_cleanup(dial_statuses);
1213 return;
1214 }
1215
1216 dialstatus = ao2_find(dial_statuses, snapshot->base->uniqueid, OBJ_SEARCH_KEY);
1217 if (dialstatus) {
1218 if (!strcasecmp(dialstatus_string, "ANSWER") && strcasecmp(dialstatus->dialstatus, "ANSWER")) {
1219 /* In the case of an answer after we already have a dial status we give
1220 * priority to the answer since the call was, well, answered. In the case of
1221 * failure dial status results we simply let the first failure be the status.
1222 */
1223 ao2_unlink(dial_statuses, dialstatus);
1224 ao2_ref(dialstatus, -1);
1225 } else {
1226 ao2_ref(dialstatus, -1);
1227 ao2_ref(dial_statuses, -1);
1228 return;
1229 }
1230 }
1231
1232 dialstatus_string_len = strlen(dialstatus_string) + 1;
1233 dialstatus = ao2_alloc_options(sizeof(*dialstatus) + dialstatus_string_len, NULL,
1234 AO2_ALLOC_OPT_LOCK_NOLOCK);
1235 if (!dialstatus) {
1236 ao2_ref(dial_statuses, -1);
1237 return;
1238 }
1239
1240 ast_copy_string(dialstatus->uniqueid, snapshot->base->uniqueid, sizeof(dialstatus->uniqueid));
1241 ast_copy_string(dialstatus->dialstatus, dialstatus_string, dialstatus_string_len);
1242
1243 ao2_link(dial_statuses, dialstatus);
1244 ao2_ref(dialstatus, -1);
1245 ao2_ref(dial_statuses, -1);
1246 }
1247
1248 static int is_valid_dialstatus(struct ast_multi_channel_blob *blob)
1249 {
1250 const char *dialstatus = get_blob_variable(blob, "dialstatus");
1251 int res = 0;
1252
1253 if (ast_strlen_zero(dialstatus)) {
1254 res = 0;
1255 } else if (!strcasecmp(dialstatus, "CHANUNAVAIL")) {
1256 res = 1;
1257 } else if (!strcasecmp(dialstatus, "CONGESTION")) {
1258 res = 1;
1259 } else if (!strcasecmp(dialstatus, "NOANSWER")) {
1260 res = 1;
1261 } else if (!strcasecmp(dialstatus, "BUSY")) {
1262 res = 1;
1263 } else if (!strcasecmp(dialstatus, "ANSWER")) {
1264 res = 1;
1265 } else if (!strcasecmp(dialstatus, "CANCEL")) {
1266 res = 1;
1267 } else if (!strcasecmp(dialstatus, "DONTCALL")) {
1268 res = 1;
1269 } else if (!strcasecmp(dialstatus, "TORTURE")) {
1270 res = 1;
1271 } else if (!strcasecmp(dialstatus, "INVALIDARGS")) {
1272 res = 1;
1273 }
1274 return res;
1275 }
1276
1277 static void cel_dial_cb(void *data, struct stasis_subscription *sub,
1278 struct stasis_message *message)
1279 {
1280 struct ast_multi_channel_blob *blob = stasis_message_data(message);
1281 struct ast_channel_snapshot *snapshot;
1282
1283 snapshot = ast_multi_channel_blob_get_channel(blob, "caller");
1284 if (!snapshot || cel_filter_channel_snapshot(snapshot)) {
1285 return;
1286 }
1287
1288 if (!ast_strlen_zero(get_blob_variable(blob, "forward"))) {
1289 struct ast_json *extra;
1290
1291 extra = ast_json_pack("{s: s}", "forward", get_blob_variable(blob, "forward"));
1292 if (extra) {
1293 cel_report_event(snapshot, AST_CEL_FORWARD, stasis_message_timestamp(message),
1294 NULL, extra, NULL);
1295 ast_json_unref(extra);
1296 }
1297 }
1298
1299 if (is_valid_dialstatus(blob)) {
1300 save_dialstatus(blob, snapshot);
1301 }
1302 }
1303
1304 static void cel_generic_cb(
1305 void *data, struct stasis_subscription *sub,
1306 struct stasis_message *message)
1307 {
1308 struct ast_channel_blob *obj = stasis_message_data(message);
1309 int event_type = ast_json_integer_get(ast_json_object_get(obj->blob, "event_type"));
1310 struct ast_json *event_details = ast_json_object_get(obj->blob, "event_details");
1311
1312 switch (event_type) {
1313 case AST_CEL_USER_DEFINED:
1314 case AST_CEL_DTMF:
1315 case AST_CEL_STREAM_BEGIN:
1316 {
1317 const char *event = ast_json_string_get(ast_json_object_get(event_details, "event"));
1318 struct ast_json *extra = ast_json_object_get(event_details, "extra");
1319 cel_report_event(obj->snapshot, event_type, stasis_message_timestamp(message),
1320 event, extra, NULL);
1321 break;
1322 }
1323 case AST_CEL_STREAM_END:
1324 {
1325 const char *event = ast_json_string_get(ast_json_object_get(event_details, "event"));
1326 cel_report_event(obj->snapshot, event_type, stasis_message_timestamp(message),
1327 event, NULL, NULL);
1328 break;
1329 }
1330 default:
1331 ast_log(LOG_ERROR, "Unhandled %s event blob\n", ast_cel_get_type_name(event_type));
1332 break;
1333 }
1334 }
1335
1336 static void cel_blind_transfer_cb(
1337 void *data, struct stasis_subscription *sub,
1338 struct stasis_message *message)
1339 {
1340 struct ast_blind_transfer_message *transfer_msg = stasis_message_data(message);
1341 struct ast_channel_snapshot *chan_snapshot = transfer_msg->transferer;
1342 struct ast_bridge_snapshot *bridge_snapshot = transfer_msg->bridge;
1343 struct ast_json *extra;
1344
1345 if (transfer_msg->result != AST_BRIDGE_TRANSFER_SUCCESS) {
1346 return;
1347 }
1348
1349 extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}",
1350 "extension", transfer_msg->exten,
1351 "context", transfer_msg->context,
1352 "bridge_id", bridge_snapshot->uniqueid,
1353 "transferee_channel_name", transfer_msg->transferee ? transfer_msg->transferee->base->name : "N/A",
1354 "transferee_channel_uniqueid", transfer_msg->transferee ? transfer_msg->transferee->base->uniqueid : "N/A");
1355 if (extra) {
1356 cel_report_event(chan_snapshot, AST_CEL_BLINDTRANSFER, stasis_message_timestamp(message),
1357 NULL, extra, NULL);
1358 ast_json_unref(extra);
1359 }
1360 }
1361
1362 static void cel_attended_transfer_cb(
1363 void *data, struct stasis_subscription *sub,
1364 struct stasis_message *message)
1365 {
1366 struct ast_attended_transfer_message *xfer = stasis_message_data(message);
1367 struct ast_json *extra = NULL;
1368 struct ast_bridge_snapshot *bridge1, *bridge2;
1369 struct ast_channel_snapshot *channel1, *channel2;
1370
1371 /* Make sure bridge1 is always non-NULL */
1372 if (!xfer->to_transferee.bridge_snapshot) {
1373 bridge1 = xfer->to_transfer_target.bridge_snapshot;
1374 bridge2 = xfer->to_transferee.bridge_snapshot;
1375 channel1 = xfer->to_transfer_target.channel_snapshot;
1376 channel2 = xfer->to_transferee.channel_snapshot;
1377 } else {
1378 bridge1 = xfer->to_transferee.bridge_snapshot;
1379 bridge2 = xfer->to_transfer_target.bridge_snapshot;
1380 channel1 = xfer->to_transferee.channel_snapshot;
1381 channel2 = xfer->to_transfer_target.channel_snapshot;
1382 }
1383
1384 switch (xfer->dest_type) {
1385 case AST_ATTENDED_TRANSFER_DEST_FAIL:
1386 return;
1387 /* handle these three the same */
1388 case AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE:
1389 case AST_ATTENDED_TRANSFER_DEST_LINK:
1390 case AST_ATTENDED_TRANSFER_DEST_THREEWAY:
1391 extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}",
1392 "bridge1_id", bridge1->uniqueid,
1393 "channel2_name", channel2->base->name,
1394 "channel2_uniqueid", channel2->base->uniqueid,
1395 "bridge2_id", bridge2->uniqueid,
1396 "transferee_channel_name", xfer->transferee ? xfer->transferee->base->name : "N/A",
1397 "transferee_channel_uniqueid", xfer->transferee ? xfer->transferee->base->uniqueid : "N/A",
1398 "transfer_target_channel_name", xfer->target ? xfer->target->base->name : "N/A",
1399 "transfer_target_channel_uniqueid", xfer->target ? xfer->target->base->uniqueid : "N/A");
1400 if (!extra) {
1401 return;
1402 }
1403 break;
1404 case AST_ATTENDED_TRANSFER_DEST_APP:
1405 case AST_ATTENDED_TRANSFER_DEST_LOCAL_APP:
1406 extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}",
1407 "bridge1_id", bridge1->uniqueid,
1408 "channel2_name", channel2->base->name,
1409 "channel2_uniqueid", channel2->base->uniqueid,
1410 "app", xfer->dest.app,
1411 "transferee_channel_name", xfer->transferee ? xfer->transferee->base->name : "N/A",
1412 "transferee_channel_uniqueid", xfer->transferee ? xfer->transferee->base->uniqueid : "N/A",
1413 "transfer_target_channel_name", xfer->target ? xfer->target->base->name : "N/A",
1414 "transfer_target_channel_uniqueid", xfer->target ? xfer->target->base->uniqueid : "N/A");
1415 if (!extra) {
1416 return;
1417 }
1418 break;
1419 }
1420 cel_report_event(channel1, AST_CEL_ATTENDEDTRANSFER, stasis_message_timestamp(message),
1421 NULL, extra, NULL);
1422 ast_json_unref(extra);
1423 }
1424
1425 static void cel_pickup_cb(
1426 void *data, struct stasis_subscription *sub,
1427 struct stasis_message *message)
1428 {
1429 struct ast_multi_channel_blob *obj = stasis_message_data(message);
1430 struct ast_channel_snapshot *channel = ast_multi_channel_blob_get_channel(obj, "channel");
1431 struct ast_channel_snapshot *target = ast_multi_channel_blob_get_channel(obj, "target");
1432 struct ast_json *extra;
1433
1434 if (!channel || !target) {
1435 return;
1436 }
1437
1438 extra = ast_json_pack("{s: s, s: s}",
1439 "pickup_channel", channel->base->name,
1440 "pickup_channel_uniqueid", channel->base->uniqueid);
1441 if (!extra) {
1442 return;
1443 }
1444
1445 cel_report_event(target, AST_CEL_PICKUP, stasis_message_timestamp(message), NULL, extra, NULL);
1446 ast_json_unref(extra);
1447 }
1448
1449
1450 static void cel_local_optimization_cb_helper(
1451 void *data, struct stasis_subscription *sub,
1452 struct stasis_message *message,
1453 enum ast_cel_event_type event_type)
1454 {
1455 struct ast_multi_channel_blob *obj = stasis_message_data(message);
1456 struct ast_channel_snapshot *localone = ast_multi_channel_blob_get_channel(obj, "1");
1457 struct ast_channel_snapshot *localtwo = ast_multi_channel_blob_get_channel(obj, "2");
1458 struct ast_json *extra;
1459
1460 if (!localone || !localtwo) {
1461 return;
1462 }
1463
1464 extra = ast_json_pack("{s: s, s: s}",
1465 "local_two", localtwo->base->name,
1466 "local_two_uniqueid", localtwo->base->uniqueid);
1467 if (!extra) {
1468 return;
1469 }
1470
1471 cel_report_event(localone, event_type, stasis_message_timestamp(message), NULL, extra, NULL);
1472 ast_json_unref(extra);
1473 }
1474
1475 static void cel_local_optimization_end_cb(
1476 void *data, struct stasis_subscription *sub,
1477 struct stasis_message *message)
1478 {
1479 /* The AST_CEL_LOCAL_OPTIMIZE event has always been triggered by the end of optimization.
1480 This can either be used as an indication that the call was locally optimized, or as
1481 the END event in combination with the subsequently added BEGIN event. */
1482 cel_local_optimization_cb_helper(data, sub, message, AST_CEL_LOCAL_OPTIMIZE);
1483 }
1484
1485 static void cel_local_optimization_begin_cb(
1486 void *data, struct stasis_subscription *sub,
1487 struct stasis_message *message)
1488 {
1489 cel_local_optimization_cb_helper(data, sub, message, AST_CEL_LOCAL_OPTIMIZE_BEGIN);
1490 }
1491
1492 static void destroy_routes(void)
1493 {
1494 stasis_message_router_unsubscribe_and_join(cel_state_router);
1495 cel_state_router = NULL;
1496 }
1497
1498 static void destroy_subscriptions(void)
1499 {
1500 ao2_cleanup(cel_aggregation_topic);
1501 cel_aggregation_topic = NULL;
1502 ao2_cleanup(cel_topic);
1503 cel_topic = NULL;
1504
1505 cel_channel_forwarder = stasis_forward_cancel(cel_channel_forwarder);
1506 cel_bridge_forwarder = stasis_forward_cancel(cel_bridge_forwarder);
1507 cel_parking_forwarder = stasis_forward_cancel(cel_parking_forwarder);
1508 cel_cel_forwarder = stasis_forward_cancel(cel_cel_forwarder);
1509 }
1510
1511 static int unload_module(void)
1512 {
1513 destroy_routes();
1514 destroy_subscriptions();
1515 STASIS_MESSAGE_TYPE_CLEANUP(cel_generic_type);
1516
1517 ast_cli_unregister(&cli_status);
1518 aco_info_destroy(&cel_cfg_info);
1519 ao2_global_obj_release(cel_configs);
1520 ao2_global_obj_release(cel_dialstatus_store);
1521 ao2_global_obj_release(cel_linkedids);
1522 ao2_global_obj_release(cel_backends);
1523
1524 return 0;
1525 }
1526
1527 /*!
1528 * \brief Create the Stasis subscriptions for CEL
1529 */
1530 static int create_subscriptions(void)
1531 {
1532 cel_aggregation_topic = stasis_topic_create("cel:aggregator");
1533 if (!cel_aggregation_topic) {
1534 return -1;
1535 }
1536
1537 cel_topic = stasis_topic_create("cel:misc");
1538 if (!cel_topic) {
1539 return -1;
1540 }
1541
1542 cel_channel_forwarder = stasis_forward_all(
1543 ast_channel_topic_all(),
1544 cel_aggregation_topic);
1545 if (!cel_channel_forwarder) {
1546 return -1;
1547 }
1548
1549 cel_bridge_forwarder = stasis_forward_all(
1550 ast_bridge_topic_all(),
1551 cel_aggregation_topic);
1552 if (!cel_bridge_forwarder) {
1553 return -1;
1554 }
1555
1556 cel_parking_forwarder = stasis_forward_all(
1557 ast_parking_topic(),
1558 cel_aggregation_topic);
1559 if (!cel_parking_forwarder) {
1560 return -1;
1561 }
1562
1563 cel_cel_forwarder = stasis_forward_all(
1564 ast_cel_topic(),
1565 cel_aggregation_topic);
1566 if (!cel_cel_forwarder) {
1567 return -1;
1568 }
1569
1570 return 0;
1571 }
1572
1573 /*!
1574 * \brief Create the Stasis message router and routes for CEL
1575 */
1576 static int create_routes(void)
1577 {
1578 int ret = 0;
1579
1580 cel_state_router = stasis_message_router_create(cel_aggregation_topic);
1581 if (!cel_state_router) {
1582 return -1;
1583 }
1584 stasis_message_router_set_congestion_limits(cel_state_router, -1,
1585 6 * AST_TASKPROCESSOR_HIGH_WATER_LEVEL);
1586
1587 ret |= stasis_message_router_add(cel_state_router,
1588 ast_channel_snapshot_type(),
1589 cel_snapshot_update_cb,
1590 NULL);
1591
1592 ret |= stasis_message_router_add(cel_state_router,
1593 ast_channel_dial_type(),
1594 cel_dial_cb,
1595 NULL);
1596
1597 ret |= stasis_message_router_add(cel_state_router,
1598 ast_channel_entered_bridge_type(),
1599 cel_bridge_enter_cb,
1600 NULL);
1601
1602 ret |= stasis_message_router_add(cel_state_router,
1603 ast_channel_left_bridge_type(),
1604 cel_bridge_leave_cb,
1605 NULL);
1606
1607 ret |= stasis_message_router_add(cel_state_router,
1608 ast_parked_call_type(),
1609 cel_parking_cb,
1610 NULL);
1611
1612 ret |= stasis_message_router_add(cel_state_router,
1613 cel_generic_type(),
1614 cel_generic_cb,
1615 NULL);
1616
1617 ret |= stasis_message_router_add(cel_state_router,
1618 ast_blind_transfer_type(),
1619 cel_blind_transfer_cb,
1620 NULL);
1621
1622 ret |= stasis_message_router_add(cel_state_router,
1623 ast_attended_transfer_type(),
1624 cel_attended_transfer_cb,
1625 NULL);
1626
1627 ret |= stasis_message_router_add(cel_state_router,
1628 ast_call_pickup_type(),
1629 cel_pickup_cb,
1630 NULL);
1631
1632 ret |= stasis_message_router_add(cel_state_router,
1633 ast_local_optimization_end_type(),
1634 cel_local_optimization_end_cb,
1635 NULL);
1636
1637 ret |= stasis_message_router_add(cel_state_router,
1638 ast_local_optimization_begin_type(),
1639 cel_local_optimization_begin_cb,
1640 NULL);
1641
1642 if (ret) {
1643 ast_log(AST_LOG_ERROR, "Failed to register for Stasis messages\n");
1644 }
1645
1646 return ret;
1647 }
1648
1649 AO2_STRING_FIELD_HASH_FN(cel_linkedid, id)
1650 AO2_STRING_FIELD_CMP_FN(cel_linkedid, id)
1651
1652 static int load_module(void)
1653 {
1654 struct ao2_container *container;
1655
1656 container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
1657 NUM_APP_BUCKETS, cel_linkedid_hash_fn, NULL, cel_linkedid_cmp_fn);
1658 ao2_global_obj_replace_unref(cel_linkedids, container);
1659 ao2_cleanup(container);
1660 if (!container) {
1661 return AST_MODULE_LOAD_FAILURE;
1662 }
1663
1664 container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
1665 NUM_DIALSTATUS_BUCKETS, cel_dialstatus_hash_fn, NULL, cel_dialstatus_cmp_fn);
1666 ao2_global_obj_replace_unref(cel_dialstatus_store, container);
1667 ao2_cleanup(container);
1668 if (!container) {
1669 return AST_MODULE_LOAD_FAILURE;
1670 }
1671
1672 if (STASIS_MESSAGE_TYPE_INIT(cel_generic_type)) {
1673 return AST_MODULE_LOAD_FAILURE;
1674 }
1675
1676 if (ast_cli_register(&cli_status)) {
1677 return AST_MODULE_LOAD_FAILURE;
1678 }
1679
1680 container = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, BACKEND_BUCKETS,
1681 cel_backend_hash_fn, NULL, cel_backend_cmp_fn);
1682 ao2_global_obj_replace_unref(cel_backends, container);
1683 ao2_cleanup(container);
1684 if (!container) {
1685 return AST_MODULE_LOAD_FAILURE;
1686 }
1687
1688 if (aco_info_init(&cel_cfg_info)) {
1689 return AST_MODULE_LOAD_FAILURE;
1690 }
1691
1692 aco_option_register(&cel_cfg_info, "enable", ACO_EXACT, general_options, "no", OPT_BOOL_T, 1, FLDSET(struct ast_cel_general_config, enable));
1693 aco_option_register(&cel_cfg_info, "dateformat", ACO_EXACT, general_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_cel_general_config, date_format));
1694 aco_option_register_custom(&cel_cfg_info, "apps", ACO_EXACT, general_options, "", apps_handler, 0);
1695 aco_option_register_custom(&cel_cfg_info, "events", ACO_EXACT, general_options, "", events_handler, 0);
1696
1697 if (aco_process_config(&cel_cfg_info, 0)) {
1698 struct cel_config *cel_cfg = cel_config_alloc();
1699
1700 if (!cel_cfg) {
1701 return AST_MODULE_LOAD_FAILURE;
1702 }
1703
1704 /* We couldn't process the configuration so create a default config. */
1705 if (!aco_set_defaults(&general_option, "general", cel_cfg->general)) {
1706 ast_log(LOG_NOTICE, "Failed to process CEL configuration; using defaults\n");
1707 ao2_global_obj_replace_unref(cel_configs, cel_cfg);
1708 }
1709 ao2_ref(cel_cfg, -1);
1710 }
1711
1712 if (create_subscriptions()) {
1713 return AST_MODULE_LOAD_FAILURE;
1714 }
1715
1716 if (ast_cel_check_enabled() && create_routes()) {
1717 return AST_MODULE_LOAD_FAILURE;
1718 }
1719
1720 return AST_MODULE_LOAD_SUCCESS;
1721 }
1722
1723 static int reload_module(void)
1724 {
1725 unsigned int was_enabled = ast_cel_check_enabled();
1726 unsigned int is_enabled;
1727
1728 if (aco_process_config(&cel_cfg_info, 1) == ACO_PROCESS_ERROR) {
1729 return -1;
1730 }
1731
1732 is_enabled = ast_cel_check_enabled();
1733
1734 if (!was_enabled && is_enabled) {
1735 if (create_routes()) {
1736 return -1;
1737 }
1738 } else if (was_enabled && !is_enabled) {
1739 destroy_routes();
1740 }
1741
1742 ast_verb(3, "CEL logging %sabled.\n", is_enabled ? "en" : "dis");
1743
1744 return 0;
1745 }
1746
1747 void ast_cel_publish_user_event(struct ast_channel *chan,
1748 const char *event,
1749 const char *extra)
1750 {
1751 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1752
1753 blob = ast_json_pack("{s: s, s: {s: s}}",
1754 "event", event,
1755 "extra", "extra", S_OR(extra, ""));
1756 if (!blob) {
1757 return;
1758 }
1759 ast_cel_publish_event(chan, AST_CEL_USER_DEFINED, blob);
1760 }
1761
1762 void ast_cel_publish_event(struct ast_channel *chan,
1763 enum ast_cel_event_type event_type,
1764 struct ast_json *blob)
1765 {
1766 struct ast_json *cel_blob;
1767 struct stasis_message *message;
1768
1769 cel_blob = ast_json_pack("{s: i, s: o}",
1770 "event_type", event_type,
1771 "event_details", ast_json_ref(blob));
1772
1773 message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), cel_generic_type(), cel_blob);
1774 if (message) {
1775 stasis_publish(ast_cel_topic(), message);
1776 }
1777 ao2_cleanup(message);
1778 ast_json_unref(cel_blob);
1779 }
1780
1781 struct stasis_topic *ast_cel_topic(void)
1782 {
1783 return cel_topic;
1784 }
1785
1786 struct ast_cel_general_config *ast_cel_get_config(void)
1787 {
1788 RAII_VAR(struct cel_config *, mod_cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
1789
1790 if (!mod_cfg || !mod_cfg->general) {
1791 return NULL;
1792 }
1793
1794 ao2_ref(mod_cfg->general, +1);
1795 return mod_cfg->general;
1796 }
1797
1798 void ast_cel_set_config(struct ast_cel_general_config *config)
1799 {
1800 int was_enabled;
1801 int is_enabled;
1802 struct ast_cel_general_config *cleanup_config;
1803 struct cel_config *mod_cfg = ao2_global_obj_ref(cel_configs);
1804
1805 if (mod_cfg) {
1806 was_enabled = ast_cel_check_enabled();
1807
1808 cleanup_config = mod_cfg->general;
1809 ao2_bump(config);
1810 mod_cfg->general = config;
1811 ao2_cleanup(cleanup_config);
1812
1813 is_enabled = ast_cel_check_enabled();
1814 if (!was_enabled && is_enabled) {
1815 create_routes();
1816 } else if (was_enabled && !is_enabled) {
1817 destroy_routes();
1818 }
1819
1820 ao2_ref(mod_cfg, -1);
1821 }
1822 }
1823
1824 int ast_cel_backend_unregister(const char *name)
1825 {
1826 struct ao2_container *backends = ao2_global_obj_ref(cel_backends);
1827
1828 if (backends) {
1829 ao2_find(backends, name, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK);
1830 ao2_ref(backends, -1);
1831 }
1832
1833 return 0;
1834 }
1835
1836 int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
1837 {
1838 RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
1839 struct cel_backend *backend;
1840
1841 if (!backends || ast_strlen_zero(name) || !backend_callback) {
1842 return -1;
1843 }
1844
1845 /* The backend object is immutable so it doesn't need a lock of its own. */
1846 backend = ao2_alloc_options(sizeof(*backend) + 1 + strlen(name), NULL,
1847 AO2_ALLOC_OPT_LOCK_NOLOCK);
1848 if (!backend) {
1849 return -1;
1850 }
1851 strcpy(backend->name, name);/* Safe */
1852 backend->callback = backend_callback;
1853
1854 ao2_link(backends, backend);
1855 ao2_ref(backend, -1);
1856 return 0;
1857 }
1858
1859 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "CEL Engine",
1860 .support_level = AST_MODULE_SUPPORT_CORE,
1861 .load = load_module,
1862 .unload = unload_module,
1863 .reload = reload_module,
1864 .load_pri = AST_MODPRI_CORE,
1865 .requires = "extconfig",
1866 );