pbx_realtime.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Realtime PBX Module
  21. *
  22. * \arg See also: \ref AstARA
  23. */
  24. /*** MODULEINFO
  25. <support_level>extended</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <signal.h>
  30. #include "asterisk/file.h"
  31. #include "asterisk/logger.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/config.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/frame.h"
  37. #include "asterisk/term.h"
  38. #include "asterisk/manager.h"
  39. #include "asterisk/cli.h"
  40. #include "asterisk/lock.h"
  41. #include "asterisk/linkedlists.h"
  42. #include "asterisk/chanvars.h"
  43. #include "asterisk/sched.h"
  44. #include "asterisk/io.h"
  45. #include "asterisk/utils.h"
  46. #include "asterisk/astdb.h"
  47. #include "asterisk/app.h"
  48. #include "asterisk/astobj2.h"
  49. #include "asterisk/stasis_channels.h"
  50. #define MODE_MATCH 0
  51. #define MODE_MATCHMORE 1
  52. #define MODE_CANMATCH 2
  53. #define EXT_DATA_SIZE 256
  54. enum option_flags {
  55. OPTION_PATTERNS_DISABLED = (1 << 0),
  56. };
  57. AST_APP_OPTIONS(switch_opts, {
  58. AST_APP_OPTION('p', OPTION_PATTERNS_DISABLED),
  59. });
  60. struct cache_entry {
  61. struct timeval when;
  62. struct ast_variable *var;
  63. int priority;
  64. char *context;
  65. char exten[2];
  66. };
  67. struct ao2_container *cache;
  68. pthread_t cleanup_thread = 0;
  69. static int cache_hash(const void *obj, const int flags)
  70. {
  71. const struct cache_entry *e = obj;
  72. return ast_str_case_hash(e->exten) + e->priority;
  73. }
  74. static int cache_cmp(void *obj, void *arg, int flags)
  75. {
  76. struct cache_entry *e = obj, *f = arg;
  77. return e->priority != f->priority ? 0 :
  78. strcmp(e->exten, f->exten) ? 0 :
  79. strcmp(e->context, f->context) ? 0 :
  80. CMP_MATCH;
  81. }
  82. static struct ast_variable *dup_vars(struct ast_variable *v)
  83. {
  84. struct ast_variable *new, *list = NULL;
  85. for (; v; v = v->next) {
  86. if (!(new = ast_variable_new(v->name, v->value, v->file))) {
  87. ast_variables_destroy(list);
  88. return NULL;
  89. }
  90. /* Reversed list in cache, but when we duplicate out of the cache,
  91. * it's back to correct order. */
  92. new->next = list;
  93. list = new;
  94. }
  95. return list;
  96. }
  97. static void free_entry(void *obj)
  98. {
  99. struct cache_entry *e = obj;
  100. ast_variables_destroy(e->var);
  101. }
  102. static int purge_old_fn(void *obj, void *arg, int flags)
  103. {
  104. struct cache_entry *e = obj;
  105. struct timeval *now = arg;
  106. return ast_tvdiff_ms(*now, e->when) >= 1000 ? CMP_MATCH : 0;
  107. }
  108. static void *cleanup(void *unused)
  109. {
  110. struct timespec forever = { 999999999, 0 }, one_second = { 1, 0 };
  111. struct timeval now;
  112. for (;;) {
  113. pthread_testcancel();
  114. if (ao2_container_count(cache) == 0) {
  115. nanosleep(&forever, NULL);
  116. }
  117. pthread_testcancel();
  118. now = ast_tvnow();
  119. ao2_callback(cache, OBJ_MULTIPLE | OBJ_UNLINK | OBJ_NODATA, purge_old_fn, &now);
  120. pthread_testcancel();
  121. nanosleep(&one_second, NULL);
  122. }
  123. return NULL;
  124. }
  125. static int extension_length_comparator(struct ast_category *p, struct ast_category *q)
  126. {
  127. const char *extenp = S_OR(ast_variable_find(p, "exten"), "");
  128. const char *extenq = S_OR(ast_variable_find(q, "exten"), "");
  129. return strlen(extenp) - strlen(extenq);
  130. }
  131. /* Realtime switch looks up extensions in the supplied realtime table.
  132. [context@][realtimetable][/options]
  133. If the realtimetable is omitted it is assumed to be "extensions". If no context is
  134. specified the context is assumed to be whatever is the container.
  135. The realtime table should have entries for context,exten,priority,app,args
  136. The realtime table currently does not support callerid fields.
  137. */
  138. static struct ast_variable *realtime_switch_common(const char *table, const char *context, const char *exten, int priority, int mode, struct ast_flags flags)
  139. {
  140. struct ast_variable *var;
  141. struct ast_config *cfg;
  142. char pri[20];
  143. char *ematch;
  144. char rexten[AST_MAX_EXTENSION + 20]="";
  145. int match;
  146. /* Optimization: since we don't support hints in realtime, it's silly to
  147. * query for a hint here, since we won't actually do anything with it.
  148. * This just wastes CPU time and resources. */
  149. if (priority < 0) {
  150. return NULL;
  151. }
  152. snprintf(pri, sizeof(pri), "%d", priority);
  153. switch(mode) {
  154. case MODE_MATCHMORE:
  155. ematch = "exten LIKE";
  156. snprintf(rexten, sizeof(rexten), "%s_%%", exten);
  157. break;
  158. case MODE_CANMATCH:
  159. ematch = "exten LIKE";
  160. snprintf(rexten, sizeof(rexten), "%s%%", exten);
  161. break;
  162. case MODE_MATCH:
  163. default:
  164. ematch = "exten";
  165. ast_copy_string(rexten, exten, sizeof(rexten));
  166. }
  167. var = ast_load_realtime(table, ematch, rexten, "context", context, "priority", pri, SENTINEL);
  168. if (!var && !ast_test_flag(&flags, OPTION_PATTERNS_DISABLED)) {
  169. cfg = ast_load_realtime_multientry(table, "exten LIKE", "\\_%", "context", context, "priority", pri, SENTINEL);
  170. if (cfg) {
  171. char *cat = NULL;
  172. /* Sort so that longer patterns are checked first */
  173. ast_config_sort_categories(cfg, 1, extension_length_comparator);
  174. while ((cat = ast_category_browse(cfg, cat))) {
  175. const char *realtime_exten = ast_variable_retrieve(cfg, cat, "exten");
  176. switch(mode) {
  177. case MODE_MATCHMORE:
  178. match = ast_extension_close(realtime_exten, exten, 1);
  179. break;
  180. case MODE_CANMATCH:
  181. match = ast_extension_close(realtime_exten, exten, 0);
  182. break;
  183. case MODE_MATCH:
  184. default:
  185. match = ast_extension_match(realtime_exten, exten);
  186. }
  187. if (match) {
  188. var = ast_category_detach_variables(ast_category_get(cfg, cat, NULL));
  189. break;
  190. }
  191. }
  192. ast_config_destroy(cfg);
  193. }
  194. }
  195. return var;
  196. }
  197. static struct ast_variable *realtime_common(const char *context, const char *exten, int priority, const char *data, int mode)
  198. {
  199. const char *ctx = NULL;
  200. char *table;
  201. struct ast_variable *var=NULL;
  202. struct ast_flags flags = { 0, };
  203. struct cache_entry *ce;
  204. struct {
  205. struct cache_entry ce;
  206. char exten[AST_MAX_EXTENSION];
  207. } cache_search = { { .priority = priority, .context = (char *) context }, };
  208. char *buf = ast_strdupa(data);
  209. /* "Realtime" prefix is stripped off in the parent engine. The
  210. * remaining string is: [[context@]table][/opts] */
  211. char *opts = strchr(buf, '/');
  212. if (opts)
  213. *opts++ = '\0';
  214. table = strchr(buf, '@');
  215. if (table) {
  216. *table++ = '\0';
  217. ctx = buf;
  218. }
  219. ctx = S_OR(ctx, context);
  220. table = S_OR(table, "extensions");
  221. if (!ast_strlen_zero(opts)) {
  222. ast_app_parse_options(switch_opts, &flags, NULL, opts);
  223. }
  224. ast_copy_string(cache_search.exten, exten, sizeof(cache_search.exten));
  225. if (mode == MODE_MATCH && (ce = ao2_find(cache, &cache_search, OBJ_POINTER))) {
  226. var = dup_vars(ce->var);
  227. ao2_ref(ce, -1);
  228. } else {
  229. var = realtime_switch_common(table, ctx, exten, priority, mode, flags);
  230. do {
  231. struct ast_variable *new;
  232. /* Only cache matches */
  233. if (mode != MODE_MATCH) {
  234. break;
  235. }
  236. if (!(new = dup_vars(var))) {
  237. break;
  238. }
  239. if (!(ce = ao2_alloc(sizeof(*ce) + strlen(exten) + strlen(context), free_entry))) {
  240. ast_variables_destroy(new);
  241. break;
  242. }
  243. ce->context = ce->exten + strlen(exten) + 1;
  244. strcpy(ce->exten, exten); /* SAFE */
  245. strcpy(ce->context, context); /* SAFE */
  246. ce->priority = priority;
  247. ce->var = new;
  248. ce->when = ast_tvnow();
  249. ao2_link(cache, ce);
  250. pthread_kill(cleanup_thread, SIGURG);
  251. ao2_ref(ce, -1);
  252. } while (0);
  253. }
  254. return var;
  255. }
  256. static int realtime_exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  257. {
  258. struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);
  259. if (var) {
  260. ast_variables_destroy(var);
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. static int realtime_canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  266. {
  267. struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_CANMATCH);
  268. if (var) {
  269. ast_variables_destroy(var);
  270. return 1;
  271. }
  272. return 0;
  273. }
  274. static int realtime_exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  275. {
  276. int res = -1;
  277. struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCH);
  278. if (var) {
  279. char *appdata_tmp = "";
  280. char *app = NULL;
  281. struct ast_variable *v;
  282. for (v = var; v ; v = v->next) {
  283. if (!strcasecmp(v->name, "app"))
  284. app = ast_strdupa(v->value);
  285. else if (!strcasecmp(v->name, "appdata")) {
  286. appdata_tmp = ast_strdupa(v->value);
  287. }
  288. }
  289. ast_variables_destroy(var);
  290. if (!ast_strlen_zero(app)) {
  291. struct ast_app *a = pbx_findapp(app);
  292. if (a) {
  293. char appdata[512];
  294. char tmp1[80];
  295. char tmp2[80];
  296. char tmp3[EXT_DATA_SIZE];
  297. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  298. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  299. appdata[0] = 0; /* just in case the substitute var func isn't called */
  300. if(!ast_strlen_zero(appdata_tmp))
  301. pbx_substitute_variables_helper(chan, appdata_tmp, appdata, sizeof(appdata) - 1);
  302. ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\")\n",
  303. ast_channel_exten(chan), ast_channel_context(chan), ast_channel_priority(chan),
  304. term_color(tmp1, app, COLOR_BRCYAN, 0, sizeof(tmp1)),
  305. term_color(tmp2, ast_channel_name(chan), COLOR_BRMAGENTA, 0, sizeof(tmp2)),
  306. term_color(tmp3, S_OR(appdata, ""), COLOR_BRMAGENTA, 0, sizeof(tmp3)));
  307. if (ast_channel_snapshot_type()) {
  308. ast_channel_lock(chan);
  309. snapshot = ast_channel_snapshot_create(chan);
  310. ast_channel_unlock(chan);
  311. }
  312. if (snapshot) {
  313. /* pbx_exec sets application name and data, but we don't want to log
  314. * every exec. Just update the snapshot here instead.
  315. */
  316. ast_string_field_set(snapshot, appl, app);
  317. ast_string_field_set(snapshot, data, !ast_strlen_zero(appdata) ? appdata : "(NULL)");
  318. msg = stasis_message_create(ast_channel_snapshot_type(), snapshot);
  319. if (msg) {
  320. stasis_publish(ast_channel_topic(chan), msg);
  321. }
  322. }
  323. res = pbx_exec(chan, a, appdata);
  324. } else
  325. ast_log(LOG_NOTICE, "No such application '%s' for extension '%s' in context '%s'\n", app, exten, context);
  326. } else {
  327. ast_log(LOG_WARNING, "No application specified for realtime extension '%s' in context '%s'\n", exten, context);
  328. }
  329. }
  330. return res;
  331. }
  332. static int realtime_matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  333. {
  334. struct ast_variable *var = realtime_common(context, exten, priority, data, MODE_MATCHMORE);
  335. if (var) {
  336. ast_variables_destroy(var);
  337. return 1;
  338. }
  339. return 0;
  340. }
  341. static struct ast_switch realtime_switch =
  342. {
  343. .name = "Realtime",
  344. .description = "Realtime Dialplan Switch",
  345. .exists = realtime_exists,
  346. .canmatch = realtime_canmatch,
  347. .exec = realtime_exec,
  348. .matchmore = realtime_matchmore,
  349. };
  350. static int unload_module(void)
  351. {
  352. ast_unregister_switch(&realtime_switch);
  353. pthread_cancel(cleanup_thread);
  354. pthread_kill(cleanup_thread, SIGURG);
  355. pthread_join(cleanup_thread, NULL);
  356. /* Destroy all remaining entries */
  357. ao2_ref(cache, -1);
  358. return 0;
  359. }
  360. static int load_module(void)
  361. {
  362. cache = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 573,
  363. cache_hash, NULL, cache_cmp);
  364. if (!cache) {
  365. return AST_MODULE_LOAD_FAILURE;
  366. }
  367. if (ast_pthread_create(&cleanup_thread, NULL, cleanup, NULL)) {
  368. return AST_MODULE_LOAD_FAILURE;
  369. }
  370. if (ast_register_switch(&realtime_switch))
  371. return AST_MODULE_LOAD_FAILURE;
  372. return AST_MODULE_LOAD_SUCCESS;
  373. }
  374. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Realtime Switch");