bridge_after.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007 - 2009, Digium, Inc.
  5. *
  6. * Richard Mudgett <rmudgett@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief After Bridge Execution API
  21. *
  22. * \author Richard Mudgett <rmudgett@digium.com>
  23. *
  24. * See Also:
  25. * \arg \ref AstCREDITS
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. #include "asterisk/logger.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/bridge_after.h"
  35. struct after_bridge_cb_node {
  36. /*! Next list node. */
  37. AST_LIST_ENTRY(after_bridge_cb_node) list;
  38. /*! Desired callback function. */
  39. ast_bridge_after_cb callback;
  40. /*! After bridge callback will not be called and destroy any resources data may contain. */
  41. ast_bridge_after_cb_failed failed;
  42. /*! Extra data to pass to the callback. */
  43. void *data;
  44. /*! Reason the after bridge callback failed. */
  45. enum ast_bridge_after_cb_reason reason;
  46. };
  47. struct after_bridge_cb_ds {
  48. /*! After bridge callbacks container. */
  49. AST_LIST_HEAD(, after_bridge_cb_node) callbacks;
  50. };
  51. /*!
  52. * \internal
  53. * \brief Indicate after bridge callback failed.
  54. * \since 12.0.0
  55. *
  56. * \param node After bridge callback node.
  57. */
  58. static void after_bridge_cb_failed(struct after_bridge_cb_node *node)
  59. {
  60. if (node->failed) {
  61. node->failed(node->reason, node->data);
  62. node->failed = NULL;
  63. }
  64. }
  65. /*!
  66. * \internal
  67. * \brief Run discarding any after bridge callbacks.
  68. * \since 12.0.0
  69. *
  70. * \param after_bridge After bridge callback container process.
  71. * \param reason Why are we doing this.
  72. */
  73. static void after_bridge_cb_run_discard(struct after_bridge_cb_ds *after_bridge, enum ast_bridge_after_cb_reason reason)
  74. {
  75. struct after_bridge_cb_node *node;
  76. for (;;) {
  77. AST_LIST_LOCK(&after_bridge->callbacks);
  78. node = AST_LIST_REMOVE_HEAD(&after_bridge->callbacks, list);
  79. AST_LIST_UNLOCK(&after_bridge->callbacks);
  80. if (!node) {
  81. break;
  82. }
  83. if (!node->reason) {
  84. node->reason = reason;
  85. }
  86. after_bridge_cb_failed(node);
  87. ast_free(node);
  88. }
  89. }
  90. /*!
  91. * \internal
  92. * \brief Destroy the after bridge callback datastore.
  93. * \since 12.0.0
  94. *
  95. * \param data After bridge callback data to destroy.
  96. */
  97. static void after_bridge_cb_destroy(void *data)
  98. {
  99. struct after_bridge_cb_ds *after_bridge = data;
  100. after_bridge_cb_run_discard(after_bridge, AST_BRIDGE_AFTER_CB_REASON_DESTROY);
  101. AST_LIST_HEAD_DESTROY(&after_bridge->callbacks);
  102. ast_free(after_bridge);
  103. }
  104. static struct after_bridge_cb_ds *after_bridge_cb_find(struct ast_channel *chan);
  105. /*!
  106. * \internal
  107. * \brief Fixup the after bridge callback datastore.
  108. * \since 12.0.0
  109. *
  110. * \param data After bridge callback data to fixup.
  111. * \param old_chan The datastore is moving from this channel.
  112. * \param new_chan The datastore is moving to this channel.
  113. */
  114. static void after_bridge_cb_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  115. {
  116. struct after_bridge_cb_ds *after_bridge;
  117. struct after_bridge_cb_node *node;
  118. after_bridge = after_bridge_cb_find(new_chan);
  119. if (!after_bridge) {
  120. return;
  121. }
  122. AST_LIST_LOCK(&after_bridge->callbacks);
  123. node = AST_LIST_LAST(&after_bridge->callbacks);
  124. if (node && !node->reason) {
  125. node->reason = AST_BRIDGE_AFTER_CB_REASON_MASQUERADE;
  126. }
  127. AST_LIST_UNLOCK(&after_bridge->callbacks);
  128. }
  129. static const struct ast_datastore_info after_bridge_cb_info = {
  130. .type = "after-bridge-cb",
  131. .destroy = after_bridge_cb_destroy,
  132. .chan_fixup = after_bridge_cb_fixup,
  133. };
  134. /*!
  135. * \internal
  136. * \brief Find an after bridge callback datastore container.
  137. * \since 12.0.0
  138. *
  139. * \param chan Channel to find the after bridge callback container on.
  140. *
  141. * \return after_bridge datastore container on success.
  142. * \retval NULL on error.
  143. */
  144. static struct after_bridge_cb_ds *after_bridge_cb_find(struct ast_channel *chan)
  145. {
  146. struct ast_datastore *datastore;
  147. SCOPED_CHANNELLOCK(lock, chan);
  148. datastore = ast_channel_datastore_find(chan, &after_bridge_cb_info, NULL);
  149. if (!datastore) {
  150. return NULL;
  151. }
  152. return datastore->data;
  153. }
  154. /*!
  155. * \internal
  156. * \brief Setup/create an after bridge callback datastore container.
  157. * \since 12.0.0
  158. *
  159. * \param chan Channel to setup/create the after bridge callback container on.
  160. *
  161. * \return after_bridge datastore container on success.
  162. * \retval NULL on error.
  163. */
  164. static struct after_bridge_cb_ds *after_bridge_cb_setup(struct ast_channel *chan)
  165. {
  166. struct ast_datastore *datastore;
  167. struct after_bridge_cb_ds *after_bridge;
  168. SCOPED_CHANNELLOCK(lock, chan);
  169. datastore = ast_channel_datastore_find(chan, &after_bridge_cb_info, NULL);
  170. if (datastore) {
  171. return datastore->data;
  172. }
  173. /* Create a new datastore. */
  174. datastore = ast_datastore_alloc(&after_bridge_cb_info, NULL);
  175. if (!datastore) {
  176. return NULL;
  177. }
  178. after_bridge = ast_calloc(1, sizeof(*after_bridge));
  179. if (!after_bridge) {
  180. ast_datastore_free(datastore);
  181. return NULL;
  182. }
  183. AST_LIST_HEAD_INIT(&after_bridge->callbacks);
  184. datastore->data = after_bridge;
  185. ast_channel_datastore_add(chan, datastore);
  186. return datastore->data;
  187. }
  188. void ast_bridge_run_after_callback(struct ast_channel *chan)
  189. {
  190. struct after_bridge_cb_ds *after_bridge;
  191. struct after_bridge_cb_node *node;
  192. after_bridge = after_bridge_cb_find(chan);
  193. if (!after_bridge) {
  194. return;
  195. }
  196. for (;;) {
  197. AST_LIST_LOCK(&after_bridge->callbacks);
  198. node = AST_LIST_REMOVE_HEAD(&after_bridge->callbacks, list);
  199. AST_LIST_UNLOCK(&after_bridge->callbacks);
  200. if (!node) {
  201. break;
  202. }
  203. if (node->reason) {
  204. after_bridge_cb_failed(node);
  205. } else {
  206. node->failed = NULL;
  207. node->callback(chan, node->data);
  208. }
  209. ast_free(node);
  210. }
  211. }
  212. void ast_bridge_discard_after_callback(struct ast_channel *chan, enum ast_bridge_after_cb_reason reason)
  213. {
  214. struct after_bridge_cb_ds *after_bridge;
  215. after_bridge = after_bridge_cb_find(chan);
  216. if (!after_bridge) {
  217. return;
  218. }
  219. after_bridge_cb_run_discard(after_bridge, reason);
  220. }
  221. int ast_bridge_set_after_callback(struct ast_channel *chan, ast_bridge_after_cb callback, ast_bridge_after_cb_failed failed, void *data)
  222. {
  223. struct after_bridge_cb_ds *after_bridge;
  224. struct after_bridge_cb_node *new_node;
  225. struct after_bridge_cb_node *last_node;
  226. /* Sanity checks. */
  227. ast_assert(chan != NULL);
  228. if (!chan || !callback) {
  229. return -1;
  230. }
  231. after_bridge = after_bridge_cb_setup(chan);
  232. if (!after_bridge) {
  233. return -1;
  234. }
  235. /* Create a new callback node. */
  236. new_node = ast_calloc(1, sizeof(*new_node));
  237. if (!new_node) {
  238. return -1;
  239. }
  240. new_node->callback = callback;
  241. new_node->failed = failed;
  242. new_node->data = data;
  243. /* Put it in the container disabling any previously active one. */
  244. AST_LIST_LOCK(&after_bridge->callbacks);
  245. last_node = AST_LIST_LAST(&after_bridge->callbacks);
  246. if (last_node && !last_node->reason) {
  247. last_node->reason = AST_BRIDGE_AFTER_CB_REASON_REPLACED;
  248. }
  249. AST_LIST_INSERT_TAIL(&after_bridge->callbacks, new_node, list);
  250. AST_LIST_UNLOCK(&after_bridge->callbacks);
  251. return 0;
  252. }
  253. const char *ast_bridge_after_cb_reason_string(enum ast_bridge_after_cb_reason reason)
  254. {
  255. switch (reason) {
  256. case AST_BRIDGE_AFTER_CB_REASON_DESTROY:
  257. return "Channel destroyed (hungup)";
  258. case AST_BRIDGE_AFTER_CB_REASON_REPLACED:
  259. return "Callback was replaced";
  260. case AST_BRIDGE_AFTER_CB_REASON_MASQUERADE:
  261. return "Channel masqueraded";
  262. case AST_BRIDGE_AFTER_CB_REASON_DEPART:
  263. return "Channel was departed from bridge";
  264. case AST_BRIDGE_AFTER_CB_REASON_REMOVED:
  265. return "Callback was removed";
  266. case AST_BRIDGE_AFTER_CB_REASON_IMPART_FAILED:
  267. return "Channel failed joining the bridge";
  268. }
  269. return "Unknown";
  270. }
  271. struct after_bridge_goto_ds {
  272. /*! Goto string that can be parsed by ast_parseable_goto(). */
  273. const char *parseable_goto;
  274. /*! Specific goto context or default context for parseable_goto. */
  275. const char *context;
  276. /*! Specific goto exten or default exten for parseable_goto. */
  277. const char *exten;
  278. /*! Specific goto priority or default priority for parseable_goto. */
  279. int priority;
  280. /*! TRUE if the peer should run the h exten. */
  281. unsigned int run_h_exten:1;
  282. /*! Specific goto location */
  283. unsigned int specific:1;
  284. };
  285. /*!
  286. * \internal
  287. * \brief Destroy the after bridge goto datastore.
  288. * \since 12.0.0
  289. *
  290. * \param data After bridge goto data to destroy.
  291. */
  292. static void after_bridge_goto_destroy(void *data)
  293. {
  294. struct after_bridge_goto_ds *after_bridge = data;
  295. ast_free((char *) after_bridge->parseable_goto);
  296. ast_free((char *) after_bridge->context);
  297. ast_free((char *) after_bridge->exten);
  298. ast_free((char *) after_bridge);
  299. }
  300. /*!
  301. * \internal
  302. * \brief Fixup the after bridge goto datastore.
  303. * \since 12.0.0
  304. *
  305. * \param data After bridge goto data to fixup.
  306. * \param old_chan The datastore is moving from this channel.
  307. * \param new_chan The datastore is moving to this channel.
  308. */
  309. static void after_bridge_goto_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  310. {
  311. /* There can be only one. Discard any already on the new channel. */
  312. ast_bridge_discard_after_goto(new_chan);
  313. }
  314. static const struct ast_datastore_info after_bridge_goto_info = {
  315. .type = "after-bridge-goto",
  316. .destroy = after_bridge_goto_destroy,
  317. .chan_fixup = after_bridge_goto_fixup,
  318. };
  319. /*!
  320. * \internal
  321. * \brief Remove channel goto location after the bridge and return it.
  322. * \since 12.0.0
  323. *
  324. * \param chan Channel to remove after bridge goto location.
  325. *
  326. * \return datastore on success.
  327. * \retval NULL on error or not found.
  328. */
  329. static struct ast_datastore *after_bridge_goto_remove(struct ast_channel *chan)
  330. {
  331. struct ast_datastore *datastore;
  332. ast_channel_lock(chan);
  333. datastore = ast_channel_datastore_find(chan, &after_bridge_goto_info, NULL);
  334. if (datastore && ast_channel_datastore_remove(chan, datastore)) {
  335. datastore = NULL;
  336. }
  337. ast_channel_unlock(chan);
  338. return datastore;
  339. }
  340. void ast_bridge_discard_after_goto(struct ast_channel *chan)
  341. {
  342. struct ast_datastore *datastore;
  343. datastore = after_bridge_goto_remove(chan);
  344. if (datastore) {
  345. ast_datastore_free(datastore);
  346. }
  347. }
  348. void ast_bridge_read_after_goto(struct ast_channel *chan, char *buffer, size_t buf_size)
  349. {
  350. struct ast_datastore *datastore;
  351. struct after_bridge_goto_ds *after_bridge;
  352. char *current_pos = buffer;
  353. size_t remaining_size = buf_size;
  354. SCOPED_CHANNELLOCK(lock, chan);
  355. datastore = ast_channel_datastore_find(chan, &after_bridge_goto_info, NULL);
  356. if (!datastore) {
  357. buffer[0] = '\0';
  358. return;
  359. }
  360. after_bridge = datastore->data;
  361. if (after_bridge->parseable_goto) {
  362. snprintf(buffer, buf_size, "%s", after_bridge->parseable_goto);
  363. return;
  364. }
  365. if (!ast_strlen_zero(after_bridge->context)) {
  366. snprintf(current_pos, remaining_size, "%s,", after_bridge->context);
  367. remaining_size = remaining_size - strlen(current_pos);
  368. current_pos += strlen(current_pos);
  369. }
  370. if (after_bridge->run_h_exten) {
  371. snprintf(current_pos, remaining_size, "h,");
  372. remaining_size = remaining_size - strlen(current_pos);
  373. current_pos += strlen(current_pos);
  374. } else if (!ast_strlen_zero(after_bridge->exten)) {
  375. snprintf(current_pos, remaining_size, "%s,", after_bridge->exten);
  376. remaining_size = remaining_size - strlen(current_pos);
  377. current_pos += strlen(current_pos);
  378. }
  379. snprintf(current_pos, remaining_size, "%d", after_bridge->priority);
  380. }
  381. int ast_bridge_setup_after_goto(struct ast_channel *chan)
  382. {
  383. struct ast_datastore *datastore;
  384. struct after_bridge_goto_ds *after_bridge;
  385. int goto_failed = -1;
  386. /* We are going to be leaving the bridging system now;
  387. * clear any pending unbridge flags
  388. */
  389. ast_channel_set_unbridged(chan, 0);
  390. /* Determine if we are going to setup a dialplan location and where. */
  391. if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
  392. /* An async goto has already setup a location. */
  393. ast_channel_clear_softhangup(chan, AST_SOFTHANGUP_ASYNCGOTO);
  394. if (!ast_check_hangup(chan)) {
  395. goto_failed = 0;
  396. }
  397. return goto_failed;
  398. }
  399. /* Get after bridge goto datastore. */
  400. datastore = after_bridge_goto_remove(chan);
  401. if (!datastore) {
  402. return goto_failed;
  403. }
  404. after_bridge = datastore->data;
  405. if (after_bridge->run_h_exten) {
  406. if (ast_exists_extension(chan, after_bridge->context, "h", 1,
  407. S_COR(ast_channel_caller(chan)->id.number.valid,
  408. ast_channel_caller(chan)->id.number.str, NULL))) {
  409. ast_debug(1, "Running after bridge goto h exten %s,h,1\n",
  410. ast_channel_context(chan));
  411. ast_pbx_h_exten_run(chan, after_bridge->context);
  412. }
  413. } else if (!ast_check_hangup(chan)) {
  414. /* Clear the outgoing flag */
  415. ast_channel_clear_flag(chan, AST_FLAG_OUTGOING);
  416. if (after_bridge->specific) {
  417. goto_failed = ast_explicit_goto(chan, after_bridge->context,
  418. after_bridge->exten, after_bridge->priority);
  419. } else if (!ast_strlen_zero(after_bridge->parseable_goto)) {
  420. char *context;
  421. char *exten;
  422. int priority;
  423. /* Option F(x) for Bridge(), Dial(), and Queue() */
  424. /* Save current dialplan location in case of failure. */
  425. context = ast_strdupa(ast_channel_context(chan));
  426. exten = ast_strdupa(ast_channel_exten(chan));
  427. priority = ast_channel_priority(chan);
  428. /* Set current dialplan position to default dialplan position */
  429. ast_explicit_goto(chan, after_bridge->context, after_bridge->exten,
  430. after_bridge->priority);
  431. /* Then perform the goto */
  432. goto_failed = ast_parseable_goto(chan, after_bridge->parseable_goto);
  433. if (goto_failed) {
  434. /* Restore original dialplan location. */
  435. ast_channel_context_set(chan, context);
  436. ast_channel_exten_set(chan, exten);
  437. ast_channel_priority_set(chan, priority);
  438. }
  439. } else {
  440. /* Option F() for Bridge(), Dial(), and Queue() */
  441. goto_failed = ast_goto_if_exists(chan, after_bridge->context,
  442. after_bridge->exten, after_bridge->priority + 1);
  443. }
  444. if (!goto_failed) {
  445. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) {
  446. ast_channel_priority_set(chan, ast_channel_priority(chan) + 1);
  447. }
  448. ast_debug(1, "Setup after bridge goto location to %s,%s,%d.\n",
  449. ast_channel_context(chan),
  450. ast_channel_exten(chan),
  451. ast_channel_priority(chan));
  452. }
  453. }
  454. /* Discard after bridge goto datastore. */
  455. ast_datastore_free(datastore);
  456. return goto_failed;
  457. }
  458. void ast_bridge_run_after_goto(struct ast_channel *chan)
  459. {
  460. int goto_failed;
  461. goto_failed = ast_bridge_setup_after_goto(chan);
  462. if (goto_failed || ast_pbx_run(chan)) {
  463. ast_hangup(chan);
  464. }
  465. }
  466. /*!
  467. * \internal
  468. * \brief Set after bridge goto location of channel.
  469. * \since 12.0.0
  470. *
  471. * \param chan Channel to setup after bridge goto location.
  472. * \param run_h_exten TRUE if the h exten should be run.
  473. * \param specific TRUE if the context/exten/priority is exactly specified.
  474. * \param context Context to goto after bridge.
  475. * \param exten Exten to goto after bridge. (Could be NULL if run_h_exten)
  476. * \param priority Priority to goto after bridge.
  477. * \param parseable_goto User specified goto string. (Could be NULL)
  478. *
  479. * \details Add a channel datastore to setup the goto location
  480. * when the channel leaves the bridge and run a PBX from there.
  481. *
  482. * If run_h_exten then execute the h exten found in the given context.
  483. * Else if specific then goto the given context/exten/priority.
  484. * Else if parseable_goto then use the given context/exten/priority
  485. * as the relative position for the parseable_goto.
  486. * Else goto the given context/exten/priority+1.
  487. */
  488. static void __after_bridge_set_goto(struct ast_channel *chan, int run_h_exten, int specific, const char *context, const char *exten, int priority, const char *parseable_goto)
  489. {
  490. struct ast_datastore *datastore;
  491. struct after_bridge_goto_ds *after_bridge;
  492. /* Sanity checks. */
  493. ast_assert(chan != NULL);
  494. if (!chan) {
  495. return;
  496. }
  497. if (run_h_exten) {
  498. ast_assert(run_h_exten && context);
  499. if (!context) {
  500. return;
  501. }
  502. } else {
  503. ast_assert(context && exten && 0 < priority);
  504. if (!context || !exten || priority < 1) {
  505. return;
  506. }
  507. }
  508. /* Create a new datastore. */
  509. datastore = ast_datastore_alloc(&after_bridge_goto_info, NULL);
  510. if (!datastore) {
  511. return;
  512. }
  513. after_bridge = ast_calloc(1, sizeof(*after_bridge));
  514. if (!after_bridge) {
  515. ast_datastore_free(datastore);
  516. return;
  517. }
  518. /* Initialize it. */
  519. after_bridge->parseable_goto = ast_strdup(parseable_goto);
  520. after_bridge->context = ast_strdup(context);
  521. after_bridge->exten = ast_strdup(exten);
  522. after_bridge->priority = priority;
  523. after_bridge->run_h_exten = run_h_exten ? 1 : 0;
  524. after_bridge->specific = specific ? 1 : 0;
  525. datastore->data = after_bridge;
  526. if ((parseable_goto && !after_bridge->parseable_goto)
  527. || (context && !after_bridge->context)
  528. || (exten && !after_bridge->exten)) {
  529. ast_datastore_free(datastore);
  530. return;
  531. }
  532. /* Put it on the channel replacing any existing one. */
  533. ast_channel_lock(chan);
  534. ast_bridge_discard_after_goto(chan);
  535. ast_channel_datastore_add(chan, datastore);
  536. ast_channel_unlock(chan);
  537. }
  538. void ast_bridge_set_after_goto(struct ast_channel *chan, const char *context, const char *exten, int priority)
  539. {
  540. __after_bridge_set_goto(chan, 0, 1, context, exten, priority, NULL);
  541. }
  542. void ast_bridge_set_after_h(struct ast_channel *chan, const char *context)
  543. {
  544. __after_bridge_set_goto(chan, 1, 0, context, NULL, 1, NULL);
  545. }
  546. void ast_bridge_set_after_go_on(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *parseable_goto)
  547. {
  548. char *p_goto;
  549. if (!ast_strlen_zero(parseable_goto)) {
  550. p_goto = ast_strdupa(parseable_goto);
  551. ast_replace_subargument_delimiter(p_goto);
  552. } else {
  553. p_goto = NULL;
  554. }
  555. __after_bridge_set_goto(chan, 0, 0, context, exten, priority, p_goto);
  556. }