| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief Realtime PBX Module | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \arg See also: \ref AstARA | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-27 19:25:16 +00:00
										 |  |  | #include <signal.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/logger.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/config.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/frame.h"
 | 
					
						
							|  |  |  | #include "asterisk/term.h"
 | 
					
						
							|  |  |  | #include "asterisk/manager.h"
 | 
					
						
							|  |  |  | #include "asterisk/cli.h"
 | 
					
						
							|  |  |  | #include "asterisk/lock.h"
 | 
					
						
							|  |  |  | #include "asterisk/md5.h"
 | 
					
						
							|  |  |  | #include "asterisk/linkedlists.h"
 | 
					
						
							|  |  |  | #include "asterisk/chanvars.h"
 | 
					
						
							|  |  |  | #include "asterisk/sched.h"
 | 
					
						
							|  |  |  | #include "asterisk/io.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							|  |  |  | #include "asterisk/astdb.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-15 21:17:07 +00:00
										 |  |  | #include "asterisk/app.h"
 | 
					
						
							| 
									
										
										
										
											2010-05-27 19:25:16 +00:00
										 |  |  | #include "asterisk/astobj2.h"
 | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | #define MODE_MATCH 		0
 | 
					
						
							|  |  |  | #define MODE_MATCHMORE 	1
 | 
					
						
							|  |  |  | #define MODE_CANMATCH 	2
 | 
					
						
							| 
									
										
										
										
											2004-11-22 14:33:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-06 17:21:44 +00:00
										 |  |  | #define EXT_DATA_SIZE 256
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-15 17:34:30 +00:00
										 |  |  | enum option_flags { | 
					
						
							| 
									
										
										
										
											2008-12-15 21:17:07 +00:00
										 |  |  | 	OPTION_PATTERNS_DISABLED = (1 << 0), | 
					
						
							| 
									
										
										
										
											2009-06-15 17:34:30 +00:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2008-12-15 21:17:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | AST_APP_OPTIONS(switch_opts, { | 
					
						
							|  |  |  | 	AST_APP_OPTION('p', OPTION_PATTERNS_DISABLED), | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-27 19:25:16 +00:00
										 |  |  | struct cache_entry { | 
					
						
							|  |  |  | 	struct timeval when; | 
					
						
							|  |  |  | 	struct ast_variable *var; | 
					
						
							|  |  |  | 	int priority; | 
					
						
							|  |  |  | 	char *context; | 
					
						
							|  |  |  | 	char exten[2]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ao2_container *cache; | 
					
						
							|  |  |  | pthread_t cleanup_thread = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int cache_hash(const void *obj, const int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const struct cache_entry *e = obj; | 
					
						
							|  |  |  | 	return ast_str_case_hash(e->exten) + e->priority; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int cache_cmp(void *obj, void *arg, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct cache_entry *e = obj, *f = arg; | 
					
						
							|  |  |  | 	return e->priority != f->priority ? 0 : | 
					
						
							|  |  |  | 		strcmp(e->exten, f->exten) ? 0 : | 
					
						
							|  |  |  | 		strcmp(e->context, f->context) ? 0 : | 
					
						
							|  |  |  | 		CMP_MATCH; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_variable *dup_vars(struct ast_variable *v) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_variable *new, *list = NULL; | 
					
						
							|  |  |  | 	for (; v; v = v->next) { | 
					
						
							|  |  |  | 		if (!(new = ast_variable_new(v->name, v->value, v->file))) { | 
					
						
							|  |  |  | 			ast_variables_destroy(list); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Reversed list in cache, but when we duplicate out of the cache,
 | 
					
						
							|  |  |  | 		 * it's back to correct order. */ | 
					
						
							|  |  |  | 		new->next = list; | 
					
						
							|  |  |  | 		list = new; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void free_entry(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct cache_entry *e = obj; | 
					
						
							|  |  |  | 	ast_variables_destroy(e->var); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int purge_old_fn(void *obj, void *arg, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct cache_entry *e = obj; | 
					
						
							|  |  |  | 	struct timeval *now = arg; | 
					
						
							|  |  |  | 	return ast_tvdiff_ms(*now, e->when) >= 1000 ? CMP_MATCH : 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void *cleanup(void *unused) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct timespec forever = { 999999999, 0 }, one_second = { 1, 0 }; | 
					
						
							|  |  |  | 	struct timeval now; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (;;) { | 
					
						
							|  |  |  | 		pthread_testcancel(); | 
					
						
							|  |  |  | 		if (ao2_container_count(cache) == 0) { | 
					
						
							|  |  |  | 			nanosleep(&forever, NULL); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		pthread_testcancel(); | 
					
						
							|  |  |  | 		now = ast_tvnow(); | 
					
						
							|  |  |  | 		ao2_callback(cache, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, purge_old_fn, &now); | 
					
						
							|  |  |  | 		pthread_testcancel(); | 
					
						
							|  |  |  | 		nanosleep(&one_second, NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-05-27 20:08:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2010-05-27 19:25:16 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | /* Realtime switch looks up extensions in the supplied realtime table.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	[context@][realtimetable][/options] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	If the realtimetable is omitted it is assumed to be "extensions".  If no context is  | 
					
						
							|  |  |  | 	specified the context is assumed to be whatever is the container. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	The realtime table should have entries for context,exten,priority,app,args | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 	The realtime table currently does not support callerid fields. | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-15 21:17:07 +00:00
										 |  |  | static struct ast_variable *realtime_switch_common(const char *table, const char *context, const char *exten, int priority, int mode, struct ast_flags flags) | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct ast_variable *var; | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 	struct ast_config *cfg; | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 	char pri[20]; | 
					
						
							| 
									
										
										
										
											2004-11-22 18:39:40 +00:00
										 |  |  | 	char *ematch; | 
					
						
							|  |  |  | 	char rexten[AST_MAX_EXTENSION + 20]=""; | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 	int match; | 
					
						
							| 
									
										
										
										
											2008-11-25 18:01:02 +00:00
										 |  |  | 	/* Optimization: since we don't support hints in realtime, it's silly to
 | 
					
						
							|  |  |  | 	 * query for a hint here, since we won't actually do anything with it. | 
					
						
							|  |  |  | 	 * This just wastes CPU time and resources. */ | 
					
						
							|  |  |  | 	if (priority < 0) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 	snprintf(pri, sizeof(pri), "%d", priority); | 
					
						
							| 
									
										
										
										
											2004-11-22 18:39:40 +00:00
										 |  |  | 	switch(mode) { | 
					
						
							|  |  |  | 	case MODE_MATCHMORE: | 
					
						
							|  |  |  | 		ematch = "exten LIKE"; | 
					
						
							|  |  |  | 		snprintf(rexten, sizeof(rexten), "%s_%%", exten); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case MODE_CANMATCH: | 
					
						
							|  |  |  | 		ematch = "exten LIKE"; | 
					
						
							|  |  |  | 		snprintf(rexten, sizeof(rexten), "%s%%", exten); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case MODE_MATCH: | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		ematch = "exten"; | 
					
						
							| 
									
										
										
										
											2006-10-25 14:44:50 +00:00
										 |  |  | 		ast_copy_string(rexten, exten, sizeof(rexten)); | 
					
						
							| 
									
										
										
										
											2004-11-22 18:39:40 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-06-19 20:48:33 +00:00
										 |  |  | 	var = ast_load_realtime(table, ematch, rexten, "context", context, "priority", pri, SENTINEL); | 
					
						
							| 
									
										
										
										
											2008-12-15 21:17:07 +00:00
										 |  |  | 	if (!var && !ast_test_flag(&flags, OPTION_PATTERNS_DISABLED)) { | 
					
						
							| 
									
										
										
										
											2008-06-19 20:48:33 +00:00
										 |  |  | 		cfg = ast_load_realtime_multientry(table, "exten LIKE", "\\_%", "context", context, "priority", pri, SENTINEL);	 | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 		if (cfg) { | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 			char *cat = ast_category_browse(cfg, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 			while(cat) { | 
					
						
							|  |  |  | 				switch(mode) { | 
					
						
							|  |  |  | 				case MODE_MATCHMORE: | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 					match = ast_extension_close(cat, exten, 1); | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				case MODE_CANMATCH: | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 					match = ast_extension_close(cat, exten, 0); | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				case MODE_MATCH: | 
					
						
							|  |  |  | 				default: | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 					match = ast_extension_match(cat, exten); | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				if (match) { | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 					var = ast_category_detach_variables(ast_category_get(cfg, cat)); | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 				cat = ast_category_browse(cfg, cat); | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 			ast_config_destroy(cfg); | 
					
						
							| 
									
										
										
										
											2004-11-24 03:07:08 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 	return var; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | static struct ast_variable *realtime_common(const char *context, const char *exten, int priority, const char *data, int mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char *ctx = NULL; | 
					
						
							|  |  |  | 	char *table; | 
					
						
							|  |  |  | 	struct ast_variable *var=NULL; | 
					
						
							| 
									
										
										
										
											2008-12-15 21:17:07 +00:00
										 |  |  | 	struct ast_flags flags = { 0, }; | 
					
						
							| 
									
										
										
										
											2010-05-27 19:25:16 +00:00
										 |  |  | 	struct cache_entry *ce; | 
					
						
							|  |  |  | 	struct { | 
					
						
							|  |  |  | 		struct cache_entry ce; | 
					
						
							|  |  |  | 		char exten[AST_MAX_EXTENSION]; | 
					
						
							|  |  |  | 	} cache_search = { { .priority = priority, .context = (char *) context }, }; | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 	char *buf = ast_strdupa(data); | 
					
						
							|  |  |  | 	if (buf) { | 
					
						
							| 
									
										
										
										
											2008-12-15 21:17:07 +00:00
										 |  |  | 		/* "Realtime" prefix is stripped off in the parent engine.  The
 | 
					
						
							|  |  |  | 		 * remaining string is: [[context@]table][/opts] */ | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 		char *opts = strchr(buf, '/'); | 
					
						
							|  |  |  | 		if (opts) | 
					
						
							|  |  |  | 			*opts++ = '\0'; | 
					
						
							|  |  |  | 		table = strchr(buf, '@'); | 
					
						
							|  |  |  | 		if (table) { | 
					
						
							|  |  |  | 			*table++ = '\0'; | 
					
						
							|  |  |  | 			ctx = buf; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ctx = S_OR(ctx, context); | 
					
						
							|  |  |  | 		table = S_OR(table, "extensions"); | 
					
						
							| 
									
										
										
										
											2008-12-15 21:17:07 +00:00
										 |  |  | 		if (!ast_strlen_zero(opts)) { | 
					
						
							|  |  |  | 			ast_app_parse_options(switch_opts, &flags, NULL, opts); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-05-27 19:25:16 +00:00
										 |  |  | 		ast_copy_string(cache_search.exten, exten, sizeof(cache_search.exten)); | 
					
						
							|  |  |  | 		if (mode == MODE_MATCH && (ce = ao2_find(cache, &cache_search, OBJ_POINTER))) { | 
					
						
							|  |  |  | 			var = dup_vars(ce->var); | 
					
						
							|  |  |  | 			ao2_ref(ce, -1); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			var = realtime_switch_common(table, ctx, exten, priority, mode, flags); | 
					
						
							|  |  |  | 			do { | 
					
						
							|  |  |  | 				struct ast_variable *new; | 
					
						
							|  |  |  | 				/* Only cache matches */ | 
					
						
							|  |  |  | 				if (mode != MODE_MATCH) { | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (!(new = dup_vars(var))) { | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (!(ce = ao2_alloc(sizeof(*ce) + strlen(exten) + strlen(context), free_entry))) { | 
					
						
							|  |  |  | 					ast_variables_destroy(new); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				ce->context = ce->exten + strlen(exten) + 1; | 
					
						
							|  |  |  | 				strcpy(ce->exten, exten); /* SAFE */ | 
					
						
							|  |  |  | 				strcpy(ce->context, context); /* SAFE */ | 
					
						
							|  |  |  | 				ce->priority = priority; | 
					
						
							|  |  |  | 				ce->var = new; | 
					
						
							|  |  |  | 				ce->when = ast_tvnow(); | 
					
						
							|  |  |  | 				ao2_link(cache, ce); | 
					
						
							|  |  |  | 				pthread_kill(cleanup_thread, SIGURG); | 
					
						
							|  |  |  | 				ao2_ref(ce, -1); | 
					
						
							|  |  |  | 			} while (0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return var; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | static int realtime_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 	struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH); | 
					
						
							|  |  |  | 	if (var) { | 
					
						
							| 
									
										
										
										
											2006-05-10 21:12:55 +00:00
										 |  |  | 		ast_variables_destroy(var); | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int realtime_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 	struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_CANMATCH); | 
					
						
							|  |  |  | 	if (var) { | 
					
						
							| 
									
										
										
										
											2006-05-10 21:12:55 +00:00
										 |  |  | 		ast_variables_destroy(var); | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-03-30 21:29:39 +00:00
										 |  |  | static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 	int res = -1; | 
					
						
							|  |  |  | 	struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 	if (var) { | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 		char *tmp=""; | 
					
						
							| 
									
										
										
										
											2007-10-12 15:41:27 +00:00
										 |  |  | 		char *app = NULL; | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 		struct ast_variable *v; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-10 21:12:55 +00:00
										 |  |  | 		for (v = var; v ; v = v->next) { | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 			if (!strcasecmp(v->name, "app")) | 
					
						
							| 
									
										
										
										
											2007-10-12 15:41:27 +00:00
										 |  |  | 				app = ast_strdupa(v->value); | 
					
						
							| 
									
										
										
										
											2008-05-22 21:42:50 +00:00
										 |  |  | 			else if (!strcasecmp(v->name, "appdata")) { | 
					
						
							| 
									
										
										
										
											2008-06-03 22:05:16 +00:00
										 |  |  | 				if (ast_compat_pbx_realtime) { | 
					
						
							| 
									
										
										
										
											2008-05-22 21:42:50 +00:00
										 |  |  | 					char *ptr; | 
					
						
							| 
									
										
										
										
											2008-05-27 13:13:17 +00:00
										 |  |  | 					int in = 0; | 
					
						
							| 
									
										
										
										
											2008-05-22 21:42:50 +00:00
										 |  |  | 					tmp = alloca(strlen(v->value) * 2 + 1); | 
					
						
							|  |  |  | 					for (ptr = tmp; *v->value; v->value++) { | 
					
						
							|  |  |  | 						if (*v->value == ',') { | 
					
						
							|  |  |  | 							*ptr++ = '\\'; | 
					
						
							|  |  |  | 							*ptr++ = ','; | 
					
						
							| 
									
										
										
										
											2008-05-27 13:13:17 +00:00
										 |  |  | 						} else if (*v->value == '|' && !in) { | 
					
						
							| 
									
										
										
										
											2008-05-22 21:42:50 +00:00
										 |  |  | 							*ptr++ = ','; | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							*ptr++ = *v->value; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2008-05-27 13:13:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						/* Don't escape '|', meaning 'or', inside expressions ($[ ]) */ | 
					
						
							|  |  |  | 						if (v->value[0] == '[' && v->value[-1] == '$') { | 
					
						
							|  |  |  | 							in++; | 
					
						
							|  |  |  | 						} else if (v->value[0] == ']' && in) { | 
					
						
							|  |  |  | 							in--; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2008-05-22 21:42:50 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2008-05-27 13:13:17 +00:00
										 |  |  | 					*ptr = '\0'; | 
					
						
							| 
									
										
										
										
											2008-05-22 21:42:50 +00:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					tmp = ast_strdupa(v->value); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-01-25 06:10:20 +00:00
										 |  |  | 		ast_variables_destroy(var); | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 		if (!ast_strlen_zero(app)) { | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 			struct ast_app *a = pbx_findapp(app); | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 			if (a) { | 
					
						
							| 
									
										
										
											
												This commits the performance mods that give the priority processing engine in the pbx, a 25-30% speed boost. The two updates used, are, first, to merge the ast_exists_extension() and the ast_spawn_extension() where they are called sequentially in a loop in the code, into a slightly upgraded version of ast_spawn_extension(), with a few extra args; and, second, I modified the substitute_variables_helper_full, so it zeroes out the byte after the evaluated string instead of demanding you pre-zero the buffer; I also went thru the code and removed the code that zeroed this buffer before every call to the substitute_variables_helper_full. The first fix provides about a 9% speedup, and the second the rest. These figures come from the 'PIPS' benchmark I describe in blogs, conf. reports, etc.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@88166 65c4cc65-6c06-0410-ace0-fbb531ad65f3
											
										 
											2007-11-01 22:26:51 +00:00
										 |  |  | 				char appdata[512]; | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 				char tmp1[80]; | 
					
						
							|  |  |  | 				char tmp2[80]; | 
					
						
							|  |  |  | 				char tmp3[EXT_DATA_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-12 18:44:36 +00:00
										 |  |  | 				appdata[0] = 0; /* just in case the substitute var func isn't called */ | 
					
						
							| 
									
										
										
										
											2004-12-06 17:21:44 +00:00
										 |  |  | 				if(!ast_strlen_zero(tmp)) | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 					pbx_substitute_variables_helper(chan, tmp, appdata, sizeof(appdata) - 1); | 
					
						
							| 
									
										
										
										
											2010-08-27 21:33:55 +00:00
										 |  |  | 				ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\")\n", | 
					
						
							|  |  |  | 						chan->exten, chan->context, chan->priority, | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 						 term_color(tmp1, app, COLOR_BRCYAN, 0, sizeof(tmp1)), | 
					
						
							|  |  |  | 						 term_color(tmp2, chan->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)), | 
					
						
							|  |  |  | 						 term_color(tmp3, S_OR(appdata, ""), COLOR_BRMAGENTA, 0, sizeof(tmp3))); | 
					
						
							| 
									
										
										
										
											2008-01-10 00:12:35 +00:00
										 |  |  | 				manager_event(EVENT_FLAG_DIALPLAN, "Newexten", | 
					
						
							| 
									
										
										
										
											2004-12-06 17:21:44 +00:00
										 |  |  | 							  "Channel: %s\r\n" | 
					
						
							|  |  |  | 							  "Context: %s\r\n" | 
					
						
							|  |  |  | 							  "Extension: %s\r\n" | 
					
						
							|  |  |  | 							  "Priority: %d\r\n" | 
					
						
							|  |  |  | 							  "Application: %s\r\n" | 
					
						
							|  |  |  | 							  "AppData: %s\r\n" | 
					
						
							| 
									
										
										
										
											2008-07-09 03:39:59 +00:00
										 |  |  | 							  "Uniqueid: %s\r\n", | 
					
						
							|  |  |  | 							  chan->name, chan->context, chan->exten, chan->priority, app, !ast_strlen_zero(appdata) ? appdata : "(NULL)", chan->uniqueid); | 
					
						
							| 
									
										
										
										
											2004-12-06 17:21:44 +00:00
										 |  |  | 				 | 
					
						
							| 
									
										
										
										
											2006-03-30 21:29:39 +00:00
										 |  |  | 				res = pbx_exec(chan, a, appdata); | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 			} else | 
					
						
							|  |  |  | 				ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context); | 
					
						
							| 
									
										
										
										
											2007-10-12 15:41:27 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "No application specified for realtime extension '%s' in context '%s'\n", exten, context); | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int realtime_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 	struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCHMORE); | 
					
						
							|  |  |  | 	if (var) { | 
					
						
							| 
									
										
										
										
											2006-03-31 09:53:05 +00:00
										 |  |  | 		ast_variables_destroy(var); | 
					
						
							| 
									
										
										
										
											2006-05-10 21:55:25 +00:00
										 |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_switch realtime_switch = | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |         name:                   "Realtime", | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  |         description:   		"Realtime Dialplan Switch", | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  |         exists:                 realtime_exists, | 
					
						
							|  |  |  |         canmatch:               realtime_canmatch, | 
					
						
							|  |  |  |         exec:                   realtime_exec, | 
					
						
							|  |  |  |         matchmore:              realtime_matchmore, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	ast_unregister_switch(&realtime_switch); | 
					
						
							| 
									
										
										
										
											2010-05-27 19:25:16 +00:00
										 |  |  | 	pthread_cancel(cleanup_thread); | 
					
						
							|  |  |  | 	pthread_kill(cleanup_thread, SIGURG); | 
					
						
							|  |  |  | 	pthread_join(cleanup_thread, NULL); | 
					
						
							|  |  |  | 	/* Destroy all remaining entries */ | 
					
						
							|  |  |  | 	ao2_ref(cache, -1); | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-27 19:25:16 +00:00
										 |  |  | 	if (!(cache = ao2_container_alloc(573, cache_hash, cache_cmp))) { | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_FAILURE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_pthread_create(&cleanup_thread, NULL, cleanup, NULL)) { | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_FAILURE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-26 20:02:27 +00:00
										 |  |  | 	if (ast_register_switch(&realtime_switch)) | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_FAILURE; | 
					
						
							|  |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							| 
									
										
										
										
											2004-11-22 03:25:27 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Switch"); |