Skip to content
Snippets Groups Projects
cel.c 52.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Asterisk -- An open source telephony toolkit.
     *
     * Copyright (C) 2007 - 2009, Digium, Inc.
     *
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
     * This program is free software, distributed under the terms of
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    /*!
     * \file
     *
     * \brief Channel Event Logging API
     *
     * \author Steve Murphy <murf@digium.com>
     * \author Russell Bryant <russell@digium.com>
     */
    
    
    /*! \li \ref cel.c uses the configuration file \ref cel.conf
     * \addtogroup configuration_file Configuration Files
     */
    
    /*!
     * \page cel.conf cel.conf
     * \verbinclude cel.conf.sample
     */
    
    
    /*** MODULEINFO
    	<support_level>core</support_level>
     ***/
    
    
    ASTERISK_REGISTER_FILE()
    
    
    #include "asterisk/_private.h"
    
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    #include "asterisk/cel.h"
    #include "asterisk/logger.h"
    #include "asterisk/linkedlists.h"
    #include "asterisk/utils.h"
    #include "asterisk/config.h"
    
    #include "asterisk/config_options.h"
    
    #include "asterisk/cli.h"
    #include "asterisk/astobj2.h"
    
    #include "asterisk/stasis_message_router.h"
    #include "asterisk/stasis_channels.h"
    
    #include "asterisk/stasis_bridges.h"
    #include "asterisk/bridge.h"
    
    #include "asterisk/parking.h"
    
    #include "asterisk/core_local.h"
    
    
    /*** DOCUMENTATION
    	<configInfo name="cel" language="en_US">
    		<configFile name="cel.conf">
    			<configObject name="general">
    				<synopsis>Options that apply globally to Channel Event Logging (CEL)</synopsis>
    				<configOption name="enable">
    					<synopsis>Determines whether CEL is enabled</synopsis>
    				</configOption>
    				<configOption name="dateformat">
    					<synopsis>The format to be used for dates when logging</synopsis>
    				</configOption>
    				<configOption name="apps">
    					<synopsis>List of apps for CEL to track</synopsis>
    					<description><para>A case-insensitive, comma-separated list of applications
    					to track when one or both of APP_START and APP_END events are flagged for
    					tracking</para></description>
    				</configOption>
    				<configOption name="events">
    					<synopsis>List of events for CEL to track</synopsis>
    					<description><para>A case-sensitive, comma-separated list of event names
    					to track. These event names do not include the leading <literal>AST_CEL</literal>.
    					</para>
    					<enumlist>
    						<enum name="ALL">
    							<para>Special value which tracks all events.</para>
    						</enum>
    						<enum name="CHAN_START"/>
    						<enum name="CHAN_END"/>
    						<enum name="ANSWER"/>
    						<enum name="HANGUP"/>
    						<enum name="APP_START"/>
    						<enum name="APP_END"/>
    						<enum name="PARK_START"/>
    						<enum name="PARK_END"/>
    						<enum name="USER_DEFINED"/>
    
    						<enum name="BRIDGE_ENTER"/>
    						<enum name="BRIDGE_EXIT"/>
    
    						<enum name="BLINDTRANSFER"/>
    						<enum name="ATTENDEDTRANSFER"/>
    						<enum name="PICKUP"/>
    						<enum name="FORWARD"/>
    						<enum name="LINKEDID_END"/>
    
    						<enum name="LOCAL_OPTIMIZE"/>
    
    					</enumlist>
    					</description>
    				</configOption>
    			</configObject>
    		</configFile>
    	</configInfo>
     ***/
    
    /*! Message router for state that CEL needs to know about */
    static struct stasis_message_router *cel_state_router;
    
    Kinsey Moore's avatar
    Kinsey Moore committed
    /*! Topic for CEL-specific messages */
    static struct stasis_topic *cel_topic;
    
    
    /*! Aggregation topic for all topics CEL needs to know about */
    
    Kinsey Moore's avatar
    Kinsey Moore committed
    static struct stasis_topic *cel_aggregation_topic;
    
    /*! Subscription for forwarding the channel caching topic */
    
    static struct stasis_forward *cel_channel_forwarder;
    
    
    /*! Subscription for forwarding the channel caching topic */
    
    static struct stasis_forward *cel_bridge_forwarder;
    
    /*! Subscription for forwarding the parking topic */
    
    static struct stasis_forward *cel_parking_forwarder;
    
    Kinsey Moore's avatar
    Kinsey Moore committed
    /*! Subscription for forwarding the CEL-specific topic */
    
    static struct stasis_forward *cel_cel_forwarder;
    
    Kinsey Moore's avatar
    Kinsey Moore committed
    
    struct stasis_message_type *cel_generic_type(void);
    STASIS_MESSAGE_TYPE_DEFN(cel_generic_type);
    
    
    /*! Container for CEL backend information */
    
    static AO2_GLOBAL_OBJ_STATIC(cel_backends);
    
    
    /*! The number of buckets into which backend names will be hashed */
    #define BACKEND_BUCKETS 13
    
    
    /*! Container for dial end multichannel blobs for holding on to dial statuses */
    
    static AO2_GLOBAL_OBJ_STATIC(cel_dialstatus_store);
    
    
    /*!
     * \brief Maximum possible CEL event IDs
     * \note This limit is currently imposed by the eventset definition
     */
    #define CEL_MAX_EVENT_IDS 64
    
    
     * \brief Number of buckets for the appset container
    
    #define NUM_APP_BUCKETS		97
    
     * \brief Number of buckets for the dialstatus container
    
    #define NUM_DIALSTATUS_BUCKETS	251
    
    struct cel_linkedid {
    	/*! Number of channels with this linkedid. */
    	unsigned int count;
    	/*! Linkedid stored at end of struct. */
    	char id[0];
    };
    
    /*! Container of channel references to a linkedid for CEL purposes. */
    static AO2_GLOBAL_OBJ_STATIC(cel_linkedids);
    
    /*! \brief Destructor for cel_config */
    static void cel_general_config_dtor(void *obj)
    {
    
    	struct ast_cel_general_config *cfg = obj;
    
    	ast_string_field_free_memory(cfg);
    	ao2_cleanup(cfg->apps);
    	cfg->apps = NULL;
    }
    
    
    void *ast_cel_general_config_alloc(void)
    
    	RAII_VAR(struct ast_cel_general_config *, cfg, NULL, ao2_cleanup);
    
    
    	if (!(cfg = ao2_alloc(sizeof(*cfg), cel_general_config_dtor))) {
    		return NULL;
    	}
    
    	if (ast_string_field_init(cfg, 64)) {
    		return NULL;
    	}
    
    	if (!(cfg->apps = ast_str_container_alloc(NUM_APP_BUCKETS))) {
    		return NULL;
    	}
    
    	ao2_ref(cfg, +1);
    	return cfg;
    }
    
    /*! \brief A container that holds all config-related information */
    struct cel_config {
    
    	struct ast_cel_general_config *general;
    
    };
    
    
    static AO2_GLOBAL_OBJ_STATIC(cel_configs);
    
    /*! \brief Destructor for cel_config */
    static void cel_config_dtor(void *obj)
    {
    	struct cel_config *cfg = obj;
    	ao2_cleanup(cfg->general);
    	cfg->general = NULL;
    }
    
    static void *cel_config_alloc(void)
    {
    	RAII_VAR(struct cel_config *, cfg, NULL, ao2_cleanup);
    
    	if (!(cfg = ao2_alloc(sizeof(*cfg), cel_config_dtor))) {
    		return NULL;
    	}
    
    
    	if (!(cfg->general = ast_cel_general_config_alloc())) {
    
    		return NULL;
    	}
    
    	ao2_ref(cfg, +1);
    	return cfg;
    }
    
    
    /*! \brief An aco_type structure to link the "general" category to the ast_cel_general_config type */
    
    static struct aco_type general_option = {
    	.type = ACO_GLOBAL,
    	.name = "general",
    	.item_offset = offsetof(struct cel_config, general),
    	.category_match = ACO_WHITELIST,
    	.category = "^general$",
    };
    
    /*! \brief The config file to be processed for the module. */
    static struct aco_file cel_conf = {
    	.filename = "cel.conf",                  /*!< The name of the config file */
    	.types = ACO_TYPES(&general_option),     /*!< The mapping object types to be processed */
    	.skip_category = "(^manager$|^radius$)", /*!< Config sections used by existing modules. Do not add to this list. */
    };
    
    static int cel_pre_apply_config(void);
    
    CONFIG_INFO_CORE("cel", cel_cfg_info, cel_configs, cel_config_alloc,
    	.files = ACO_FILES(&cel_conf),
    	.pre_apply_config = cel_pre_apply_config,
    );
    
    static int cel_pre_apply_config(void)
    {
    	struct cel_config *cfg = aco_pending_config(&cel_cfg_info);
    
    	if (!cfg->general) {
    		return -1;
    	}
    
    	if (!ao2_container_count(cfg->general->apps)) {
    		return 0;
    	}
    
    	if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_START)) {
    		return 0;
    	}
    
    	if (cfg->general->events & ((int64_t) 1 << AST_CEL_APP_END)) {
    		return 0;
    	}
    
    	ast_log(LOG_ERROR, "Applications are listed to be tracked, but APP events are not tracked\n");
    	return -1;
    }
    
    static struct aco_type *general_options[] = ACO_TYPES(&general_option);
    
    
    /*!
     * \brief Map of ast_cel_event_type to strings
     */
    
    static const char * const cel_event_types[CEL_MAX_EVENT_IDS] = {
    
    	[AST_CEL_CHANNEL_START]    = "CHAN_START",
    	[AST_CEL_CHANNEL_END]      = "CHAN_END",
    	[AST_CEL_ANSWER]           = "ANSWER",
    	[AST_CEL_HANGUP]           = "HANGUP",
    	[AST_CEL_APP_START]        = "APP_START",
    	[AST_CEL_APP_END]          = "APP_END",
    	[AST_CEL_PARK_START]       = "PARK_START",
    	[AST_CEL_PARK_END]         = "PARK_END",
    	[AST_CEL_USER_DEFINED]     = "USER_DEFINED",
    
    	[AST_CEL_BRIDGE_ENTER]     = "BRIDGE_ENTER",
    	[AST_CEL_BRIDGE_EXIT]      = "BRIDGE_EXIT",
    
    	[AST_CEL_BLINDTRANSFER]    = "BLINDTRANSFER",
    	[AST_CEL_ATTENDEDTRANSFER] = "ATTENDEDTRANSFER",
    	[AST_CEL_PICKUP]           = "PICKUP",
    	[AST_CEL_FORWARD]          = "FORWARD",
    	[AST_CEL_LINKEDID_END]     = "LINKEDID_END",
    
    	[AST_CEL_LOCAL_OPTIMIZE]   = "LOCAL_OPTIMIZE",
    
    struct cel_backend {
    	ast_cel_backend_cb callback; /*!< Callback for this backend */
    	char name[0];                /*!< Name of this backend */
    };
    
    /*! \brief Hashing function for cel_backend */
    static int cel_backend_hash(const void *obj, int flags)
    {
    	const struct cel_backend *backend;
    	const char *name;
    
    
    	switch (flags & OBJ_SEARCH_MASK) {
    	case OBJ_SEARCH_OBJECT:
    
    		backend = obj;
    		name = backend->name;
    		break;
    
    		name = obj;
    		break;
    	default:
    		/* Hash can only work on something with a full key. */
    		ast_assert(0);
    		return 0;
    	}
    
    	return ast_str_hash(name);
    }
    
    /*! \brief Comparator function for cel_backend */
    static int cel_backend_cmp(void *obj, void *arg, int flags)
    {
    
    	const struct cel_backend *object_left = obj;
    	const struct cel_backend *object_right = arg;
    	const char *right_key = arg;
    	int cmp;
    
    	switch (flags & OBJ_SEARCH_MASK) {
    	case OBJ_SEARCH_OBJECT:
    		right_key = object_right->name;
    		/* Fall through */
    	case OBJ_SEARCH_KEY:
    		cmp = strcmp(object_left->name, right_key);
    
    	case OBJ_SEARCH_PARTIAL_KEY:
    		/*
    		 * We could also use a partial key struct containing a length
    		 * so strlen() does not get called for every comparison instead.
    		 */
    		cmp = strncmp(object_left->name, right_key, strlen(right_key));
    
    		/*
    		 * What arg points to is specific to this traversal callback
    		 * and has no special meaning to astobj2.
    		 */
    		cmp = 0;
    		break;
    	}
    	if (cmp) {
    
    	/*
    	 * At this point the traversal callback is identical to a sorted
    	 * container.
    	 */
    	return CMP_MATCH;
    
    static const char *get_caller_uniqueid(struct ast_multi_channel_blob *blob)
    {
    	struct ast_channel_snapshot *caller = ast_multi_channel_blob_get_channel(blob, "caller");
    	if (!caller) {
    		return NULL;
    	}
    
    	return caller->uniqueid;
    }
    
    /*! \brief Hashing function for dialstatus container */
    static int dialstatus_hash(const void *obj, int flags)
    {
    
    	struct ast_multi_channel_blob *blob;
    	const char *key;
    
    	switch (flags & OBJ_SEARCH_MASK) {
    	case OBJ_SEARCH_KEY:
    		key = obj;
    		break;
    	case OBJ_SEARCH_OBJECT:
    		blob = (void *) obj;
    		key = get_caller_uniqueid(blob);
    		break;
    	default:
    		/* Hash can only work on something with a full key. */
    		ast_assert(0);
    		return 0;
    	}
    	return ast_str_hash(key);
    
    }
    
    /*! \brief Comparator function for dialstatus container */
    static int dialstatus_cmp(void *obj, void *arg, int flags)
    {
    
    	struct ast_multi_channel_blob *object_left = obj;
    	struct ast_multi_channel_blob *object_right = arg;
    	const char *right_key = arg;
    	int cmp;
    
    	switch (flags & OBJ_SEARCH_MASK) {
    	case OBJ_SEARCH_OBJECT:
    		right_key = get_caller_uniqueid(object_right);
    		/* Fall through */
    	case OBJ_SEARCH_KEY:
    		cmp = strcmp(get_caller_uniqueid(object_left), right_key);
    		break;
    	case OBJ_SEARCH_PARTIAL_KEY:
    		/*
    		 * We could also use a partial key struct containing a length
    		 * so strlen() does not get called for every comparison instead.
    		 */
    		cmp = strncmp(get_caller_uniqueid(object_left), right_key, strlen(right_key));
    		break;
    	default:
    		/*
    		 * What arg points to is specific to this traversal callback
    		 * and has no special meaning to astobj2.
    		 */
    		cmp = 0;
    		break;
    
    	/*
    	 * At this point the traversal callback is identical to a sorted
    	 * container.
    	 */
    	return CMP_MATCH;
    
    unsigned int ast_cel_check_enabled(void)
    
    	unsigned int enabled;
    	struct cel_config *cfg = ao2_global_obj_ref(cel_configs);
    
    	enabled = (!cfg || !cfg->general) ? 0 : cfg->general->enable;
    	ao2_cleanup(cfg);
    	return enabled;
    
    }
    
    static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
    {
    	unsigned int i;
    
    	RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
    
    	RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
    	struct ao2_iterator iter;
    	char *app;
    
    	switch (cmd) {
    	case CLI_INIT:
    		e->command = "cel show status";
    		e->usage =
    			"Usage: cel show status\n"
    			"       Displays the Channel Event Logging system status.\n";
    		return NULL;
    	case CLI_GENERATE:
    		return NULL;
    	case CLI_HANDLER:
    		break;
    	}
    
    	if (a->argc > 3) {
    		return CLI_SHOWUSAGE;
    	}
    
    
    	ast_cli(a->fd, "CEL Logging: %s\n", ast_cel_check_enabled() ? "Enabled" : "Disabled");
    
    	if (!cfg || !cfg->general || !cfg->general->enable) {
    
    	for (i = 0; i < (sizeof(cfg->general->events) * 8); i++) {
    
    		if (!(cfg->general->events & ((int64_t) 1 << i))) {
    
    			continue;
    		}
    
    		name = ast_cel_get_type_name(i);
    		if (strcasecmp(name, "Unknown")) {
    			ast_cli(a->fd, "CEL Tracking Event: %s\n", name);
    		}
    	}
    
    
    	iter = ao2_iterator_init(cfg->general->apps, 0);
    	for (; (app = ao2_iterator_next(&iter)); ao2_ref(app, -1)) {
    		ast_cli(a->fd, "CEL Tracking Application: %s\n", app);
    	}
    	ao2_iterator_destroy(&iter);
    
    	if (backends) {
    		struct cel_backend *backend;
    
    		iter = ao2_iterator_init(backends, 0);
    		for (; (backend = ao2_iterator_next(&iter)); ao2_ref(backend, -1)) {
    			ast_cli(a->fd, "CEL Event Subscriber: %s\n", backend->name);
    		}
    		ao2_iterator_destroy(&iter);
    	}
    
    
    	return CLI_SUCCESS;
    }
    
    static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the CEL status");
    
    enum ast_cel_event_type ast_cel_str_to_event_type(const char *name)
    {
    	unsigned int i;
    
    	for (i = 0; i < ARRAY_LEN(cel_event_types); i++) {
    
    		if (cel_event_types[i] && !strcasecmp(name, cel_event_types[i])) {
    
    	ast_log(LOG_ERROR, "Unknown event name '%s'\n", name);
    	return AST_CEL_INVALID_VALUE;
    
    }
    
    static int ast_cel_track_event(enum ast_cel_event_type et)
    {
    
    	RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
    
    	if (!cfg || !cfg->general) {
    		return 0;
    	}
    
    	return (cfg->general->events & ((int64_t) 1 << et));
    
    static int events_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
    
    	struct ast_cel_general_config *cfg = obj;
    
    	char *events = ast_strdupa(var->value);
    
    	char *cur_event;
    
    	while ((cur_event = strsep(&events, ","))) {
    		enum ast_cel_event_type event_type;
    
    		cur_event = ast_strip(cur_event);
    		if (ast_strlen_zero(cur_event)) {
    			continue;
    		}
    
    		event_type = ast_cel_str_to_event_type(cur_event);
    
    
    		if (event_type == AST_CEL_ALL) {
    
    			cfg->events = (int64_t) -1;
    
    		} else if (event_type == AST_CEL_INVALID_VALUE) {
    
    			cfg->events |= ((int64_t) 1 << event_type);
    
    static int apps_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
    
    	struct ast_cel_general_config *cfg = obj;
    
    	char *apps = ast_strdupa(var->value);
    
    	char *cur_app;
    
    	while ((cur_app = strsep(&apps, ","))) {
    		cur_app = ast_strip(cur_app);
    		if (ast_strlen_zero(cur_app)) {
    			continue;
    		}
    
    
    		cur_app = ast_str_to_lower(cur_app);
    		ast_str_container_add(cfg->apps, cur_app);
    
    const char *ast_cel_get_type_name(enum ast_cel_event_type type)
    {
    	return S_OR(cel_event_types[type], "Unknown");
    }
    
    static int cel_track_app(const char *const_app)
    {
    	RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
    	RAII_VAR(char *, app, NULL, ao2_cleanup);
    	char *app_lower;
    
    	if (!cfg || !cfg->general) {
    		return 0;
    
    	app_lower = ast_str_to_lower(ast_strdupa(const_app));
    
    	app = ao2_find(cfg->general->apps, app_lower, OBJ_SEARCH_KEY);
    
    static int cel_linkedid_ref(const char *linkedid);
    
    struct ast_event *ast_cel_create_event(struct ast_channel_snapshot *snapshot,
    		enum ast_cel_event_type event_type, const char *userdefevname,
    
    		struct ast_json *extra, const char *peer)
    
    {
    	struct timeval eventtime = ast_tvnow();
    
    	RAII_VAR(char *, extra_txt, NULL, ast_json_free);
    
    	if (extra) {
    		extra_txt = ast_json_dump_string(extra);
    	}
    
    	return ast_event_new(AST_EVENT_CEL,
    		AST_EVENT_IE_CEL_EVENT_TYPE, AST_EVENT_IE_PLTYPE_UINT, event_type,
    		AST_EVENT_IE_CEL_EVENT_TIME, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_sec,
    		AST_EVENT_IE_CEL_EVENT_TIME_USEC, AST_EVENT_IE_PLTYPE_UINT, eventtime.tv_usec,
    		AST_EVENT_IE_CEL_USEREVENT_NAME, AST_EVENT_IE_PLTYPE_STR, S_OR(userdefevname, ""),
    		AST_EVENT_IE_CEL_CIDNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_name,
    		AST_EVENT_IE_CEL_CIDNUM, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_number,
    		AST_EVENT_IE_CEL_CIDANI, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_ani,
    		AST_EVENT_IE_CEL_CIDRDNIS, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_rdnis,
    		AST_EVENT_IE_CEL_CIDDNID, AST_EVENT_IE_PLTYPE_STR, snapshot->caller_dnid,
    		AST_EVENT_IE_CEL_EXTEN, AST_EVENT_IE_PLTYPE_STR, snapshot->exten,
    		AST_EVENT_IE_CEL_CONTEXT, AST_EVENT_IE_PLTYPE_STR, snapshot->context,
    		AST_EVENT_IE_CEL_CHANNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->name,
    		AST_EVENT_IE_CEL_APPNAME, AST_EVENT_IE_PLTYPE_STR, snapshot->appl,
    		AST_EVENT_IE_CEL_APPDATA, AST_EVENT_IE_PLTYPE_STR, snapshot->data,
    		AST_EVENT_IE_CEL_AMAFLAGS, AST_EVENT_IE_PLTYPE_UINT, snapshot->amaflags,
    		AST_EVENT_IE_CEL_ACCTCODE, AST_EVENT_IE_PLTYPE_STR, snapshot->accountcode,
    		AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, snapshot->peeraccount,
    		AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, snapshot->uniqueid,
    		AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, snapshot->linkedid,
    		AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, snapshot->userfield,
    
    		AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, S_OR(extra_txt, ""),
    
    		AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, S_OR(peer, ""),
    
    		AST_EVENT_IE_END);
    }
    
    
    static int cel_backend_send_cb(void *obj, void *arg, int flags)
    {
    	struct cel_backend *backend = obj;
    
    	backend->callback(arg);
    	return 0;
    }
    
    
    static int cel_report_event(struct ast_channel_snapshot *snapshot,
    
    		enum ast_cel_event_type event_type, const char *userdefevname,
    
    		struct ast_json *extra, const char *peer_str)
    
    {
    	struct ast_event *ev;
    	RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
    
    	RAII_VAR(struct ao2_container *, backends, ao2_global_obj_ref(cel_backends), ao2_cleanup);
    
    	if (!cfg || !cfg->general || !cfg->general->enable || !backends) {
    
    	/* Record the linkedid of new channels if we are tracking LINKEDID_END even if we aren't
    	 * reporting on CHANNEL_START so we can track when to send LINKEDID_END */
    
    	if (event_type == AST_CEL_CHANNEL_START
    		&& ast_cel_track_event(AST_CEL_LINKEDID_END)) {
    		if (cel_linkedid_ref(snapshot->linkedid)) {
    
    	if (!ast_cel_track_event(event_type)) {
    		return 0;
    	}
    
    	if ((event_type == AST_CEL_APP_START || event_type == AST_CEL_APP_END)
    		&& !cel_track_app(snapshot->appl)) {
    		return 0;
    
    	ev = ast_cel_create_event(snapshot, event_type, userdefevname, extra, peer_str);
    
    	/* Distribute event to backends */
    
    	ao2_callback(backends, OBJ_MULTIPLE | OBJ_NODATA, cel_backend_send_cb, ev);
    
    }
    
    /* called whenever a channel is destroyed or a linkedid is changed to
     * potentially emit a CEL_LINKEDID_END event */
    
    static void check_retire_linkedid(struct ast_channel_snapshot *snapshot)
    
    	RAII_VAR(struct ao2_container *, linkedids, ao2_global_obj_ref(cel_linkedids), ao2_cleanup);
    	struct cel_linkedid *lid;
    
    	if (!linkedids || ast_strlen_zero(snapshot->linkedid)) {
    		/* The CEL module is shutdown.  Abort. */
    
    	ao2_lock(linkedids);
    
    	lid = ao2_find(linkedids, (void *) snapshot->linkedid, OBJ_SEARCH_KEY);
    	if (!lid) {
    		ao2_unlock(linkedids);
    
    		/*
    		 * The user may have done a reload to start tracking linkedids
    		 * when a call was already in progress.  This is an unusual kind
    		 * of change to make after starting Asterisk.
    		 */
    		ast_log(LOG_ERROR, "Something weird happened, couldn't find linkedid %s\n",
    			snapshot->linkedid);
    
    	if (!--lid->count) {
    		/* No channels use this linkedid anymore. */
    		ao2_unlink(linkedids, lid);
    		ao2_unlock(linkedids);
    
    
    		cel_report_event(snapshot, AST_CEL_LINKEDID_END, NULL, NULL, NULL);
    
    	} else {
    		ao2_unlock(linkedids);
    
    	ao2_ref(lid, -1);
    
    /* Note that no 'chan_fixup' function is provided for this datastore type,
     * because the channels that will use it will never be involved in masquerades.
     */
    static const struct ast_datastore_info fabricated_channel_datastore = {
    	.type = "CEL fabricated channel",
    
    	.destroy = ast_free_ptr,
    
    struct ast_channel *ast_cel_fabricate_channel_from_event(const struct ast_event *event)
    {
    	struct varshead *headp;
    	struct ast_var_t *newvariable;
    
    	char timebuf[30];
    	struct ast_channel *tchan;
    	struct ast_cel_event_record record = {
    		.version = AST_CEL_EVENT_RECORD_VERSION,
    	};
    
    	struct ast_datastore *datastore;
    	char *app_data;
    
    	RAII_VAR(struct cel_config *, cfg, ao2_global_obj_ref(cel_configs), ao2_cleanup);
    
    	if (!cfg || !cfg->general) {
    		return NULL;
    	}
    
    
    	/* do not call ast_channel_alloc because this is not really a real channel */
    	if (!(tchan = ast_dummy_channel_alloc())) {
    		return NULL;
    	}
    
    
    	headp = ast_channel_varshead(tchan);
    
    
    	/* first, get the variables from the event */
    	if (ast_cel_fill_record(event, &record)) {
    
    		ast_channel_unref(tchan);
    
    		return NULL;
    	}
    
    	/* next, fill the channel with their data */
    
    	mixed_name = (record.event_type == AST_CEL_USER_DEFINED)
    		? record.user_defined_name : record.event_name;
    	if ((newvariable = ast_var_assign("eventtype", mixed_name))) {
    
    		AST_LIST_INSERT_HEAD(headp, newvariable, entries);
    	}
    
    
    	if (ast_strlen_zero(cfg->general->date_format)) {
    
    		snprintf(timebuf, sizeof(timebuf), "%ld.%06ld", (long) record.event_time.tv_sec,
    
    				(long) record.event_time.tv_usec);
    
    	} else {
    		struct ast_tm tm;
    		ast_localtime(&record.event_time, &tm, NULL);
    
    		ast_strftime(timebuf, sizeof(timebuf), cfg->general->date_format, &tm);
    
    	}
    
    	if ((newvariable = ast_var_assign("eventtime", timebuf))) {
    		AST_LIST_INSERT_HEAD(headp, newvariable, entries);
    	}
    
    
    	if ((newvariable = ast_var_assign("eventenum", record.event_name))) {
    		AST_LIST_INSERT_HEAD(headp, newvariable, entries);
    	}
    
    	if ((newvariable = ast_var_assign("userdeftype", record.user_defined_name))) {
    		AST_LIST_INSERT_HEAD(headp, newvariable, entries);
    	}
    
    	if ((newvariable = ast_var_assign("eventextra", record.extra))) {
    		AST_LIST_INSERT_HEAD(headp, newvariable, entries);
    	}
    
    
    	ast_channel_caller(tchan)->id.name.valid = 1;
    	ast_channel_caller(tchan)->id.name.str = ast_strdup(record.caller_id_name);
    	ast_channel_caller(tchan)->id.number.valid = 1;
    	ast_channel_caller(tchan)->id.number.str = ast_strdup(record.caller_id_num);
    	ast_channel_caller(tchan)->ani.number.valid = 1;
    	ast_channel_caller(tchan)->ani.number.str = ast_strdup(record.caller_id_ani);
    	ast_channel_redirecting(tchan)->from.number.valid = 1;
    	ast_channel_redirecting(tchan)->from.number.str = ast_strdup(record.caller_id_rdnis);
    	ast_channel_dialed(tchan)->number.str = ast_strdup(record.caller_id_dnid);
    
    	ast_channel_exten_set(tchan, record.extension);
    	ast_channel_context_set(tchan, record.context);
    
    	ast_channel_name_set(tchan, record.channel_name);
    
    	ast_channel_internal_set_fake_ids(tchan, record.unique_id, record.linked_id);
    
    	ast_channel_accountcode_set(tchan, record.account_code);
    	ast_channel_peeraccount_set(tchan, record.peer_account);
    	ast_channel_userfield_set(tchan, record.user_field);
    
    	if ((newvariable = ast_var_assign("BRIDGEPEER", record.peer))) {
    		AST_LIST_INSERT_HEAD(headp, newvariable, entries);
    	}
    
    	ast_channel_amaflags_set(tchan, record.amaflag);
    
    	/* We need to store an 'application name' and 'application
    	 * data' on the channel for logging purposes, but the channel
    	 * structure only provides a place to store pointers, and it
    	 * expects these pointers to be pointing to data that does not
    	 * need to be freed. This means that the channel's destructor
    	 * does not attempt to free any storage that these pointers
    	 * point to. However, we can't provide data in that form directly for
    	 * these structure members. In order to ensure that these data
    	 * elements have a lifetime that matches the channel's
    	 * lifetime, we'll put them in a datastore attached to the
    	 * channel, and set's the channel's pointers to point into the
    	 * datastore.  The datastore will then be automatically destroyed
    	 * when the channel is destroyed.
    	 */
    
    	if (!(datastore = ast_datastore_alloc(&fabricated_channel_datastore, NULL))) {
    		ast_channel_unref(tchan);
    		return NULL;
    	}
    
    	if (!(app_data = ast_malloc(strlen(record.application_name) + strlen(record.application_data) + 2))) {
    		ast_datastore_free(datastore);
    		ast_channel_unref(tchan);
    		return NULL;
    	}
    
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	ast_channel_appl_set(tchan, strcpy(app_data, record.application_name));
    	ast_channel_data_set(tchan, strcpy(app_data + strlen(record.application_name) + 1,
    		record.application_data));
    
    
    	datastore->data = app_data;
    	ast_channel_datastore_add(tchan, datastore);
    
    
    static int cel_linkedid_ref(const char *linkedid)
    
    	RAII_VAR(struct ao2_container *, linkedids, ao2_global_obj_ref(cel_linkedids), ao2_cleanup);
    	struct cel_linkedid *lid;
    
    
    	if (ast_strlen_zero(linkedid)) {
    		ast_log(LOG_ERROR, "The linkedid should never be empty\n");
    		return -1;
    	}
    
    	if (!linkedids) {
    		/* The CEL module is shutdown.  Abort. */
    		return -1;
    	}
    
    	ao2_lock(linkedids);
    	lid = ao2_find(linkedids, (void *) linkedid, OBJ_SEARCH_KEY);
    	if (!lid) {
    		/*
    		 * Changes to the lid->count member are protected by the
    		 * container lock so the lid object does not need its own lock.
    		 */
    		lid = ao2_alloc_options(sizeof(*lid) + strlen(linkedid) + 1, NULL,
    			AO2_ALLOC_OPT_LOCK_NOLOCK);
    		if (!lid) {
    			ao2_unlock(linkedids);
    
    		strcpy(lid->id, linkedid);/* Safe */
    
    		ao2_link(linkedids, lid);
    
    	++lid->count;
    	ao2_unlock(linkedids);
    	ao2_ref(lid, -1);
    
    
    int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *r)
    {
    	if (r->version != AST_CEL_EVENT_RECORD_VERSION) {
    		ast_log(LOG_ERROR, "Module ABI mismatch for ast_cel_event_record.  "
    				"Please ensure all modules were compiled for "
    				"this version of Asterisk.\n");
    		return -1;
    	}
    
    	r->event_type = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TYPE);
    
    	r->event_time.tv_sec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME);
    	r->event_time.tv_usec = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_EVENT_TIME_USEC);
    
    
    	r->event_name = ast_cel_get_type_name(r->event_type);
    
    	if (r->event_type == AST_CEL_USER_DEFINED) {
    		r->user_defined_name = ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USEREVENT_NAME);
    	} else {
    
    	}
    
    	r->caller_id_name   = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNAME), "");
    	r->caller_id_num    = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDNUM), "");
    	r->caller_id_ani    = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDANI), "");
    	r->caller_id_rdnis  = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDRDNIS), "");
    	r->caller_id_dnid   = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CIDDNID), "");
    	r->extension        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTEN), "");
    	r->context          = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CONTEXT), "");
    	r->channel_name     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_CHANNAME), "");
    	r->application_name = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPNAME), "");
    	r->application_data = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_APPDATA), "");
    	r->account_code     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_ACCTCODE), "");
    
    	r->peer_account     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEERACCT), "");
    
    	r->unique_id        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_UNIQUEID), "");
    	r->linked_id        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_LINKEDID), "");
    	r->amaflag          = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_AMAFLAGS);
    	r->user_field       = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USERFIELD), "");
    	r->peer             = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEER), "");
    	r->extra            = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_EXTRA), "");
    
    	return 0;
    }
    
    
    /*! \brief Typedef for callbacks that get called on channel snapshot updates */
    typedef void (*cel_channel_snapshot_monitor)(
    	struct ast_channel_snapshot *old_snapshot,
    	struct ast_channel_snapshot *new_snapshot);
    
    static struct ast_multi_channel_blob *get_dialstatus_blob(const char *uniqueid)
    
    	struct ao2_container *dial_statuses = ao2_global_obj_ref(cel_dialstatus_store);
    	struct ast_multi_channel_blob *blob = NULL;
    
    	if (dial_statuses) {
    		blob = ao2_find(dial_statuses, uniqueid, OBJ_SEARCH_KEY | OBJ_UNLINK);
    		ao2_ref(dial_statuses, -1);
    	}
    	return blob;
    
    Kinsey Moore's avatar
    Kinsey Moore committed
    static const char *get_blob_variable(struct ast_multi_channel_blob *blob, const char *varname)
    
    	struct ast_json *json = ast_multi_channel_blob_get_json(blob);
    	if (!json) {
    		return NULL;
    	}
    
    Kinsey Moore's avatar
    Kinsey Moore committed
    	json = ast_json_object_get(json, varname);
    
    	if (!json) {
    		return NULL;
    	}
    
    	return ast_json_string_get(json);
    
    /*! \brief Handle channel state changes */
    static void cel_channel_state_change(
    	struct ast_channel_snapshot *old_snapshot,
    	struct ast_channel_snapshot *new_snapshot)
    {
    	int is_hungup, was_hungup;
    
    	if (!new_snapshot) {
    
    		cel_report_event(old_snapshot, AST_CEL_CHANNEL_END, NULL, NULL, NULL);
    
    		if (ast_cel_track_event(AST_CEL_LINKEDID_END)) {
    			check_retire_linkedid(old_snapshot);
    		}