features.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2012, Digium, Inc.
  5. * Copyright (C) 2012, Russell Bryant
  6. *
  7. * Mark Spencer <markster@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Routines implementing call features as call pickup, parking and transfer
  22. *
  23. * \author Mark Spencer <markster@digium.com>
  24. */
  25. /*! \li \ref features.c uses the configuration file \ref features.conf
  26. * \addtogroup configuration_file Configuration Files
  27. */
  28. /*!
  29. * \page features.conf features.conf
  30. * \verbinclude features.conf.sample
  31. */
  32. /*** MODULEINFO
  33. <support_level>core</support_level>
  34. ***/
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include "asterisk/_private.h"
  38. #include <pthread.h>
  39. #include <signal.h>
  40. #include <sys/time.h>
  41. #include <sys/signal.h>
  42. #include <netinet/in.h>
  43. #include "asterisk/lock.h"
  44. #include "asterisk/file.h"
  45. #include "asterisk/channel.h"
  46. #include "asterisk/pbx.h"
  47. #include "asterisk/causes.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/translate.h"
  50. #include "asterisk/app.h"
  51. #include "asterisk/say.h"
  52. #include "asterisk/features.h"
  53. #include "asterisk/musiconhold.h"
  54. #include "asterisk/config.h"
  55. #include "asterisk/cli.h"
  56. #include "asterisk/manager.h"
  57. #include "asterisk/utils.h"
  58. #include "asterisk/adsi.h"
  59. #include "asterisk/devicestate.h"
  60. #include "asterisk/monitor.h"
  61. #include "asterisk/audiohook.h"
  62. #include "asterisk/global_datastores.h"
  63. #include "asterisk/astobj2.h"
  64. #include "asterisk/test.h"
  65. #include "asterisk/bridge.h"
  66. #include "asterisk/bridge_features.h"
  67. #include "asterisk/bridge_basic.h"
  68. #include "asterisk/bridge_after.h"
  69. #include "asterisk/stasis.h"
  70. #include "asterisk/stasis_channels.h"
  71. #include "asterisk/features_config.h"
  72. /*** DOCUMENTATION
  73. <application name="Bridge" language="en_US">
  74. <synopsis>
  75. Bridge two channels.
  76. </synopsis>
  77. <syntax>
  78. <parameter name="channel" required="true">
  79. <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
  80. </parameter>
  81. <parameter name="options">
  82. <optionlist>
  83. <option name="p">
  84. <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
  85. </option>
  86. <option name="F" argsep="^">
  87. <argument name="context" required="false" />
  88. <argument name="exten" required="false" />
  89. <argument name="priority" required="true" />
  90. <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
  91. to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
  92. <note>
  93. <para>Any channel variables you want the called channel to inherit from the caller channel must be
  94. prefixed with one or two underbars ('_').</para>
  95. </note>
  96. <note>
  97. <para>This option will override the 'x' option</para>
  98. </note>
  99. </option>
  100. <option name="F">
  101. <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
  102. to the next priority of the current extension and <emphasis>start</emphasis> execution
  103. at that location.</para>
  104. <note>
  105. <para>Any channel variables you want the called channel to inherit from the caller channel must be
  106. prefixed with one or two underbars ('_').</para>
  107. </note>
  108. <note>
  109. <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
  110. </note>
  111. <note>
  112. <para>This option will override the 'x' option</para>
  113. </note>
  114. </option>
  115. <option name="h">
  116. <para>Allow the called party to hang up by sending the
  117. <replaceable>*</replaceable> DTMF digit.</para>
  118. </option>
  119. <option name="H">
  120. <para>Allow the calling party to hang up by pressing the
  121. <replaceable>*</replaceable> DTMF digit.</para>
  122. </option>
  123. <option name="k">
  124. <para>Allow the called party to enable parking of the call by sending
  125. the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  126. </option>
  127. <option name="K">
  128. <para>Allow the calling party to enable parking of the call by sending
  129. the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  130. </option>
  131. <option name="L(x[:y][:z])">
  132. <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
  133. when <replaceable>y</replaceable> ms are left. Repeat the warning every
  134. <replaceable>z</replaceable> ms. The following special variables can be
  135. used with this option:</para>
  136. <variablelist>
  137. <variable name="LIMIT_PLAYAUDIO_CALLER">
  138. <para>Play sounds to the caller. yes|no (default yes)</para>
  139. </variable>
  140. <variable name="LIMIT_PLAYAUDIO_CALLEE">
  141. <para>Play sounds to the callee. yes|no</para>
  142. </variable>
  143. <variable name="LIMIT_TIMEOUT_FILE">
  144. <para>File to play when time is up.</para>
  145. </variable>
  146. <variable name="LIMIT_CONNECT_FILE">
  147. <para>File to play when call begins.</para>
  148. </variable>
  149. <variable name="LIMIT_WARNING_FILE">
  150. <para>File to play as warning if <replaceable>y</replaceable> is
  151. defined. The default is to say the time remaining.</para>
  152. </variable>
  153. </variablelist>
  154. </option>
  155. <option name="S(x)">
  156. <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
  157. </option>
  158. <option name="t">
  159. <para>Allow the called party to transfer the calling party by sending the
  160. DTMF sequence defined in <filename>features.conf</filename>.</para>
  161. </option>
  162. <option name="T">
  163. <para>Allow the calling party to transfer the called party by sending the
  164. DTMF sequence defined in <filename>features.conf</filename>.</para>
  165. </option>
  166. <option name="w">
  167. <para>Allow the called party to enable recording of the call by sending
  168. the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
  169. </option>
  170. <option name="W">
  171. <para>Allow the calling party to enable recording of the call by sending
  172. the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
  173. </option>
  174. <option name="x">
  175. <para>Cause the called party to be hung up after the bridge, instead of being
  176. restarted in the dialplan.</para>
  177. </option>
  178. </optionlist>
  179. </parameter>
  180. </syntax>
  181. <description>
  182. <para>Allows the ability to bridge two channels via the dialplan.</para>
  183. <para>This application sets the following channel variable upon completion:</para>
  184. <variablelist>
  185. <variable name="BRIDGERESULT">
  186. <para>The result of the bridge attempt as a text string.</para>
  187. <value name="SUCCESS" />
  188. <value name="FAILURE" />
  189. <value name="LOOP" />
  190. <value name="NONEXISTENT" />
  191. <value name="INCOMPATIBLE" />
  192. </variable>
  193. </variablelist>
  194. </description>
  195. </application>
  196. <manager name="Bridge" language="en_US">
  197. <synopsis>
  198. Bridge two channels already in the PBX.
  199. </synopsis>
  200. <syntax>
  201. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  202. <parameter name="Channel1" required="true">
  203. <para>Channel to Bridge to Channel2.</para>
  204. </parameter>
  205. <parameter name="Channel2" required="true">
  206. <para>Channel to Bridge to Channel1.</para>
  207. </parameter>
  208. <parameter name="Tone">
  209. <para>Play courtesy tone to Channel 2.</para>
  210. <enumlist>
  211. <enum name="no" />
  212. <enum name="Channel1" />
  213. <enum name="Channel2" />
  214. <enum name="Both" />
  215. </enumlist>
  216. </parameter>
  217. </syntax>
  218. <description>
  219. <para>Bridge together two channels already in the PBX.</para>
  220. </description>
  221. </manager>
  222. ***/
  223. typedef enum {
  224. FEATURE_INTERPRET_DETECT, /* Used by ast_feature_detect */
  225. FEATURE_INTERPRET_DO, /* Used by feature_interpret */
  226. FEATURE_INTERPRET_CHECK, /* Used by feature_check */
  227. } feature_interpret_op;
  228. struct ast_dial_features {
  229. /*! Channel's feature flags. */
  230. struct ast_flags my_features;
  231. /*! Bridge peer's feature flags. */
  232. struct ast_flags peer_features;
  233. };
  234. static void *dial_features_duplicate(void *data)
  235. {
  236. struct ast_dial_features *df = data, *df_copy;
  237. if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
  238. return NULL;
  239. }
  240. memcpy(df_copy, df, sizeof(*df));
  241. return df_copy;
  242. }
  243. static const struct ast_datastore_info dial_features_info = {
  244. .type = "dial-features",
  245. .destroy = ast_free_ptr,
  246. .duplicate = dial_features_duplicate,
  247. };
  248. /*!
  249. * \internal
  250. * \brief Set the features datastore if it doesn't exist.
  251. *
  252. * \param chan Channel to add features datastore
  253. * \param my_features The channel's feature flags
  254. * \param peer_features The channel's bridge peer feature flags
  255. *
  256. * \retval TRUE if features datastore already existed.
  257. */
  258. static int add_features_datastore(struct ast_channel *chan, const struct ast_flags *my_features, const struct ast_flags *peer_features)
  259. {
  260. struct ast_datastore *datastore;
  261. struct ast_dial_features *dialfeatures;
  262. ast_channel_lock(chan);
  263. datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL);
  264. ast_channel_unlock(chan);
  265. if (datastore) {
  266. /* Already exists. */
  267. return 1;
  268. }
  269. /* Create a new datastore with specified feature flags. */
  270. datastore = ast_datastore_alloc(&dial_features_info, NULL);
  271. if (!datastore) {
  272. ast_log(LOG_WARNING, "Unable to create channel features datastore.\n");
  273. return 0;
  274. }
  275. dialfeatures = ast_calloc(1, sizeof(*dialfeatures));
  276. if (!dialfeatures) {
  277. ast_log(LOG_WARNING, "Unable to allocate memory for feature flags.\n");
  278. ast_datastore_free(datastore);
  279. return 0;
  280. }
  281. ast_copy_flags(&dialfeatures->my_features, my_features, AST_FLAGS_ALL);
  282. ast_copy_flags(&dialfeatures->peer_features, peer_features, AST_FLAGS_ALL);
  283. datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  284. datastore->data = dialfeatures;
  285. ast_channel_lock(chan);
  286. ast_channel_datastore_add(chan, datastore);
  287. ast_channel_unlock(chan);
  288. return 0;
  289. }
  290. struct ast_bridge_thread_obj
  291. {
  292. struct ast_bridge_config bconfig;
  293. struct ast_channel *chan;
  294. struct ast_channel *peer;
  295. struct ast_callid *callid; /*<! callid pointer (Only used to bind thread) */
  296. unsigned int return_to_pbx:1;
  297. };
  298. static void set_config_flags(struct ast_channel *chan, struct ast_bridge_config *config)
  299. {
  300. ast_clear_flag(config, AST_FLAGS_ALL);
  301. if (ast_test_flag(&config->features_caller, AST_FEATURE_DTMF_MASK)) {
  302. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
  303. }
  304. if (ast_test_flag(&config->features_callee, AST_FEATURE_DTMF_MASK)) {
  305. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
  306. }
  307. if (!(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
  308. RAII_VAR(struct ao2_container *, applicationmap, NULL, ao2_cleanup);
  309. ast_channel_lock(chan);
  310. applicationmap = ast_get_chan_applicationmap(chan);
  311. ast_channel_unlock(chan);
  312. if (!applicationmap) {
  313. return;
  314. }
  315. /* If an applicationmap exists for this channel at all, then the channel needs the DTMF flag set */
  316. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
  317. }
  318. }
  319. void ast_channel_log(char *title, struct ast_channel *chan);
  320. void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */
  321. {
  322. ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long) chan);
  323. ast_log(LOG_NOTICE, "CHAN: name: %s; appl: %s; data: %s; contxt: %s; exten: %s; pri: %d;\n",
  324. ast_channel_name(chan), ast_channel_appl(chan), ast_channel_data(chan),
  325. ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
  326. ast_log(LOG_NOTICE, "CHAN: acctcode: %s; dialcontext: %s; amaflags: %x; maccontxt: %s; macexten: %s; macpri: %d;\n",
  327. ast_channel_accountcode(chan), ast_channel_dialcontext(chan), ast_channel_amaflags(chan),
  328. ast_channel_macrocontext(chan), ast_channel_macroexten(chan), ast_channel_macropriority(chan));
  329. ast_log(LOG_NOTICE, "CHAN: masq: %p; masqr: %p; uniqueID: %s; linkedID:%s\n",
  330. ast_channel_masq(chan), ast_channel_masqr(chan),
  331. ast_channel_uniqueid(chan), ast_channel_linkedid(chan));
  332. if (ast_channel_masqr(chan)) {
  333. ast_log(LOG_NOTICE, "CHAN: masquerading as: %s; cdr: %p;\n",
  334. ast_channel_name(ast_channel_masqr(chan)), ast_channel_cdr(ast_channel_masqr(chan)));
  335. }
  336. ast_log(LOG_NOTICE, "===== done ====\n");
  337. }
  338. static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
  339. {
  340. const char *feature;
  341. if (ast_strlen_zero(features)) {
  342. return;
  343. }
  344. for (feature = features; *feature; feature++) {
  345. struct ast_flags *party;
  346. if (isupper(*feature)) {
  347. party = &config->features_caller;
  348. } else {
  349. party = &config->features_callee;
  350. }
  351. switch (tolower(*feature)) {
  352. case 't' :
  353. ast_set_flag(party, AST_FEATURE_REDIRECT);
  354. break;
  355. case 'k' :
  356. ast_set_flag(party, AST_FEATURE_PARKCALL);
  357. break;
  358. case 'h' :
  359. ast_set_flag(party, AST_FEATURE_DISCONNECT);
  360. break;
  361. case 'w' :
  362. ast_set_flag(party, AST_FEATURE_AUTOMON);
  363. break;
  364. case 'x' :
  365. ast_set_flag(party, AST_FEATURE_AUTOMIXMON);
  366. break;
  367. default :
  368. ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
  369. break;
  370. }
  371. }
  372. }
  373. static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
  374. {
  375. if (add_features_datastore(caller, &config->features_caller, &config->features_callee)) {
  376. /*
  377. * If we don't return here, then when we do a builtin_atxfer we
  378. * will copy the disconnect flags over from the atxfer to the
  379. * callee (Party C).
  380. */
  381. return;
  382. }
  383. add_features_datastore(callee, &config->features_callee, &config->features_caller);
  384. }
  385. static void clear_dialed_interfaces(struct ast_channel *chan)
  386. {
  387. struct ast_datastore *di_datastore;
  388. ast_channel_lock(chan);
  389. if ((di_datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL))) {
  390. if (option_debug) {
  391. ast_log(LOG_DEBUG, "Removing dialed interfaces datastore on %s since we're bridging\n", ast_channel_name(chan));
  392. }
  393. if (!ast_channel_datastore_remove(chan, di_datastore)) {
  394. ast_datastore_free(di_datastore);
  395. }
  396. }
  397. ast_channel_unlock(chan);
  398. }
  399. static void bridge_config_set_limits_warning_values(struct ast_bridge_config *config, struct ast_bridge_features_limits *limits)
  400. {
  401. if (config->end_sound) {
  402. ast_string_field_set(limits, duration_sound, config->end_sound);
  403. }
  404. if (config->warning_sound) {
  405. ast_string_field_set(limits, warning_sound, config->warning_sound);
  406. }
  407. if (config->start_sound) {
  408. ast_string_field_set(limits, connect_sound, config->start_sound);
  409. }
  410. limits->frequency = config->warning_freq;
  411. limits->warning = config->play_warning;
  412. }
  413. /*!
  414. * \internal brief Setup limit hook structures on calls that need limits
  415. *
  416. * \param config ast_bridge_config which provides the limit data
  417. * \param caller_limits pointer to an ast_bridge_features_limits struct which will store the caller side limits
  418. * \param callee_limits pointer to an ast_bridge_features_limits struct which will store the callee side limits
  419. */
  420. static void bridge_config_set_limits(struct ast_bridge_config *config, struct ast_bridge_features_limits *caller_limits, struct ast_bridge_features_limits *callee_limits)
  421. {
  422. if (ast_test_flag(&config->features_caller, AST_FEATURE_PLAY_WARNING)) {
  423. bridge_config_set_limits_warning_values(config, caller_limits);
  424. }
  425. if (ast_test_flag(&config->features_callee, AST_FEATURE_PLAY_WARNING)) {
  426. bridge_config_set_limits_warning_values(config, callee_limits);
  427. }
  428. caller_limits->duration = config->timelimit;
  429. callee_limits->duration = config->timelimit;
  430. }
  431. /*!
  432. * \internal
  433. * \brief Check if Monitor needs to be started on a channel.
  434. * \since 12.0.0
  435. *
  436. * \param chan The bridge considers this channel the caller.
  437. * \param peer The bridge considers this channel the callee.
  438. *
  439. * \return Nothing
  440. */
  441. static void bridge_check_monitor(struct ast_channel *chan, struct ast_channel *peer)
  442. {
  443. const char *value;
  444. const char *monitor_args = NULL;
  445. struct ast_channel *monitor_chan = NULL;
  446. ast_channel_lock(chan);
  447. value = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR");
  448. if (!ast_strlen_zero(value)) {
  449. monitor_args = ast_strdupa(value);
  450. monitor_chan = chan;
  451. }
  452. ast_channel_unlock(chan);
  453. if (!monitor_chan) {
  454. ast_channel_lock(peer);
  455. value = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR");
  456. if (!ast_strlen_zero(value)) {
  457. monitor_args = ast_strdupa(value);
  458. monitor_chan = peer;
  459. }
  460. ast_channel_unlock(peer);
  461. }
  462. if (monitor_chan) {
  463. struct ast_app *monitor_app;
  464. monitor_app = pbx_findapp("Monitor");
  465. if (monitor_app) {
  466. pbx_exec(monitor_chan, monitor_app, monitor_args);
  467. }
  468. }
  469. }
  470. /*!
  471. * \internal
  472. * \brief Send the peer channel on its way on bridge start failure.
  473. * \since 12.0.0
  474. *
  475. * \param chan Chan to put into autoservice.
  476. * \param peer Chan to send to after bridge goto or run hangup handlers and hangup.
  477. *
  478. * \return Nothing
  479. */
  480. static void bridge_failed_peer_goto(struct ast_channel *chan, struct ast_channel *peer)
  481. {
  482. if (ast_bridge_setup_after_goto(peer)
  483. || ast_pbx_start(peer)) {
  484. ast_autoservice_chan_hangup_peer(chan, peer);
  485. }
  486. }
  487. static int pre_bridge_setup(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config,
  488. struct ast_bridge_features *chan_features, struct ast_bridge_features *peer_features)
  489. {
  490. int res;
  491. set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
  492. add_features_datastores(chan, peer, config);
  493. /*
  494. * This is an interesting case. One example is if a ringing
  495. * channel gets redirected to an extension that picks up a
  496. * parked call. This will make sure that the call taken out of
  497. * parking gets told that the channel it just got bridged to is
  498. * still ringing.
  499. */
  500. if (ast_channel_state(chan) == AST_STATE_RINGING
  501. && ast_channel_visible_indication(peer) != AST_CONTROL_RINGING) {
  502. ast_indicate(peer, AST_CONTROL_RINGING);
  503. }
  504. bridge_check_monitor(chan, peer);
  505. set_config_flags(chan, config);
  506. /* Answer if need be */
  507. if (ast_channel_state(chan) != AST_STATE_UP) {
  508. if (ast_raw_answer(chan)) {
  509. return -1;
  510. }
  511. }
  512. #ifdef FOR_DEBUG
  513. /* show the two channels and cdrs involved in the bridge for debug & devel purposes */
  514. ast_channel_log("Pre-bridge CHAN Channel info", chan);
  515. ast_channel_log("Pre-bridge PEER Channel info", peer);
  516. #endif
  517. /*
  518. * If we are bridging a call, stop worrying about forwarding
  519. * loops. We presume that if a call is being bridged, that the
  520. * humans in charge know what they're doing. If they don't,
  521. * well, what can we do about that?
  522. */
  523. clear_dialed_interfaces(chan);
  524. clear_dialed_interfaces(peer);
  525. res = 0;
  526. ast_channel_lock(chan);
  527. res |= ast_bridge_features_ds_append(chan, &config->features_caller);
  528. ast_channel_unlock(chan);
  529. ast_channel_lock(peer);
  530. res |= ast_bridge_features_ds_append(peer, &config->features_callee);
  531. ast_channel_unlock(peer);
  532. if (res) {
  533. return -1;
  534. }
  535. if (config->timelimit) {
  536. struct ast_bridge_features_limits call_duration_limits_chan;
  537. struct ast_bridge_features_limits call_duration_limits_peer;
  538. int abandon_call = 0; /* TRUE if set limits fails so we can abandon the call. */
  539. if (ast_bridge_features_limits_construct(&call_duration_limits_chan)) {
  540. ast_log(LOG_ERROR, "Could not construct caller duration limits. Bridge canceled.\n");
  541. return -1;
  542. }
  543. if (ast_bridge_features_limits_construct(&call_duration_limits_peer)) {
  544. ast_log(LOG_ERROR, "Could not construct callee duration limits. Bridge canceled.\n");
  545. ast_bridge_features_limits_destroy(&call_duration_limits_chan);
  546. return -1;
  547. }
  548. bridge_config_set_limits(config, &call_duration_limits_chan, &call_duration_limits_peer);
  549. if (ast_bridge_features_set_limits(chan_features, &call_duration_limits_chan, 0)) {
  550. abandon_call = 1;
  551. }
  552. if (ast_bridge_features_set_limits(peer_features, &call_duration_limits_peer, 0)) {
  553. abandon_call = 1;
  554. }
  555. /* At this point we are done with the limits structs since they have been copied to the individual feature sets. */
  556. ast_bridge_features_limits_destroy(&call_duration_limits_chan);
  557. ast_bridge_features_limits_destroy(&call_duration_limits_peer);
  558. if (abandon_call) {
  559. ast_log(LOG_ERROR, "Could not set duration limits on one or more sides of the call. Bridge canceled.\n");
  560. return -1;
  561. }
  562. }
  563. return 0;
  564. }
  565. int ast_bridge_call_with_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, unsigned int flags)
  566. {
  567. int res;
  568. struct ast_bridge *bridge;
  569. struct ast_bridge_features chan_features;
  570. struct ast_bridge_features *peer_features;
  571. /* Setup features. */
  572. res = ast_bridge_features_init(&chan_features);
  573. peer_features = ast_bridge_features_new();
  574. if (res || !peer_features) {
  575. ast_bridge_features_destroy(peer_features);
  576. ast_bridge_features_cleanup(&chan_features);
  577. bridge_failed_peer_goto(chan, peer);
  578. return -1;
  579. }
  580. if (pre_bridge_setup(chan, peer, config, &chan_features, peer_features)) {
  581. ast_bridge_features_destroy(peer_features);
  582. ast_bridge_features_cleanup(&chan_features);
  583. bridge_failed_peer_goto(chan, peer);
  584. return -1;
  585. }
  586. /* Create bridge */
  587. bridge = ast_bridge_basic_new();
  588. if (!bridge) {
  589. ast_bridge_features_destroy(peer_features);
  590. ast_bridge_features_cleanup(&chan_features);
  591. bridge_failed_peer_goto(chan, peer);
  592. return -1;
  593. }
  594. ast_bridge_basic_set_flags(bridge, flags);
  595. /* Put peer into the bridge */
  596. if (ast_bridge_impart(bridge, peer, NULL, peer_features,
  597. AST_BRIDGE_IMPART_CHAN_INDEPENDENT | AST_BRIDGE_IMPART_INHIBIT_JOIN_COLP)) {
  598. ast_bridge_destroy(bridge, 0);
  599. ast_bridge_features_cleanup(&chan_features);
  600. bridge_failed_peer_goto(chan, peer);
  601. return -1;
  602. }
  603. /* Join bridge */
  604. ast_bridge_join(bridge, chan, NULL, &chan_features, NULL,
  605. AST_BRIDGE_JOIN_PASS_REFERENCE | AST_BRIDGE_JOIN_INHIBIT_JOIN_COLP);
  606. /*
  607. * If the bridge was broken for a hangup that isn't real, then
  608. * don't run the h extension, because the channel isn't really
  609. * hung up. This should really only happen with
  610. * AST_SOFTHANGUP_ASYNCGOTO.
  611. */
  612. res = -1;
  613. ast_channel_lock(chan);
  614. if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
  615. res = 0;
  616. }
  617. ast_channel_unlock(chan);
  618. ast_bridge_features_cleanup(&chan_features);
  619. if (res && config->end_bridge_callback) {
  620. config->end_bridge_callback(config->end_bridge_callback_data);
  621. }
  622. return res;
  623. }
  624. /*!
  625. * \brief bridge the call and set CDR
  626. *
  627. * \param chan The bridge considers this channel the caller.
  628. * \param peer The bridge considers this channel the callee.
  629. * \param config Configuration for this bridge.
  630. *
  631. * Set start time, check for two channels,check if monitor on
  632. * check for feature activation, create new CDR
  633. * \retval res on success.
  634. * \retval -1 on failure to bridge.
  635. */
  636. int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
  637. {
  638. return ast_bridge_call_with_flags(chan, peer, config, 0);
  639. }
  640. enum play_tone_action {
  641. PLAYTONE_NONE = 0,
  642. PLAYTONE_CHANNEL1 = (1 << 0),
  643. PLAYTONE_CHANNEL2 = (1 << 1),
  644. PLAYTONE_BOTH = PLAYTONE_CHANNEL1 | PLAYTONE_CHANNEL2,
  645. };
  646. static enum play_tone_action parse_playtone(const char *playtone_val)
  647. {
  648. if (ast_strlen_zero(playtone_val) || ast_false(playtone_val)) {
  649. return PLAYTONE_NONE;
  650. } if (!strcasecmp(playtone_val, "channel1")) {
  651. return PLAYTONE_CHANNEL1;
  652. } else if (!strcasecmp(playtone_val, "channel2") || ast_true(playtone_val)) {
  653. return PLAYTONE_CHANNEL2;
  654. } else if (!strcasecmp(playtone_val, "both")) {
  655. return PLAYTONE_BOTH;
  656. } else {
  657. /* Invalid input. Assume none */
  658. return PLAYTONE_NONE;
  659. }
  660. }
  661. /*!
  662. * \brief Bridge channels together
  663. * \param s
  664. * \param m
  665. *
  666. * Make sure valid channels were specified,
  667. * send errors if any of the channels could not be found/locked, answer channels if needed,
  668. * create the placeholder channels and grab the other channels
  669. * make the channels compatible, send error if we fail doing so
  670. * setup the bridge thread object and start the bridge.
  671. *
  672. * \retval 0
  673. */
  674. static int action_bridge(struct mansession *s, const struct message *m)
  675. {
  676. const char *channela = astman_get_header(m, "Channel1");
  677. const char *channelb = astman_get_header(m, "Channel2");
  678. enum play_tone_action playtone = parse_playtone(astman_get_header(m, "Tone"));
  679. RAII_VAR(struct ast_channel *, chana, NULL, ao2_cleanup);
  680. RAII_VAR(struct ast_channel *, chanb, NULL, ao2_cleanup);
  681. const char *chana_exten;
  682. const char *chana_context;
  683. int chana_priority;
  684. const char *chanb_exten;
  685. const char *chanb_context;
  686. int chanb_priority;
  687. struct ast_bridge *bridge;
  688. char buf[256];
  689. RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_a, NULL, ao2_cleanup);
  690. RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_b, NULL, ao2_cleanup);
  691. /* make sure valid channels were specified */
  692. if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
  693. astman_send_error(s, m, "Missing channel parameter in request");
  694. return 0;
  695. }
  696. ast_debug(1, "Performing Bridge action on %s and %s\n", channela, channelb);
  697. /* Start with chana */
  698. chana = ast_channel_get_by_name_prefix(channela, strlen(channela));
  699. if (!chana) {
  700. snprintf(buf, sizeof(buf), "Channel1 does not exist: %s", channela);
  701. astman_send_error(s, m, buf);
  702. return 0;
  703. }
  704. xfer_cfg_a = ast_get_chan_features_xfer_config(chana);
  705. ast_channel_lock(chana);
  706. chana_exten = ast_strdupa(ast_channel_exten(chana));
  707. chana_context = ast_strdupa(ast_channel_context(chana));
  708. chana_priority = ast_channel_priority(chana);
  709. if (!ast_test_flag(ast_channel_flags(chana), AST_FLAG_IN_AUTOLOOP)) {
  710. chana_priority++;
  711. }
  712. ast_channel_unlock(chana);
  713. chanb = ast_channel_get_by_name_prefix(channelb, strlen(channelb));
  714. if (!chanb) {
  715. snprintf(buf, sizeof(buf), "Channel2 does not exist: %s", channelb);
  716. astman_send_error(s, m, buf);
  717. return 0;
  718. }
  719. xfer_cfg_b = ast_get_chan_features_xfer_config(chanb);
  720. ast_channel_lock(chanb);
  721. chanb_exten = ast_strdupa(ast_channel_exten(chanb));
  722. chanb_context = ast_strdupa(ast_channel_context(chanb));
  723. chanb_priority = ast_channel_priority(chanb);
  724. if (!ast_test_flag(ast_channel_flags(chanb), AST_FLAG_IN_AUTOLOOP)) {
  725. chanb_priority++;
  726. }
  727. ast_channel_unlock(chanb);
  728. bridge = ast_bridge_basic_new();
  729. if (!bridge) {
  730. astman_send_error(s, m, "Unable to create bridge\n");
  731. return 0;
  732. }
  733. ast_bridge_set_after_go_on(chana, chana_context, chana_exten, chana_priority, NULL);
  734. if (ast_bridge_add_channel(bridge, chana, NULL, playtone & PLAYTONE_CHANNEL1, xfer_cfg_a ? xfer_cfg_a->xfersound : NULL)) {
  735. snprintf(buf, sizeof(buf), "Unable to add Channel1 to bridge: %s", ast_channel_name(chana));
  736. astman_send_error(s, m, buf);
  737. ast_bridge_destroy(bridge, 0);
  738. return 0;
  739. }
  740. ast_bridge_set_after_go_on(chanb, chanb_context, chanb_exten, chanb_priority, NULL);
  741. if (ast_bridge_add_channel(bridge, chanb, NULL, playtone & PLAYTONE_CHANNEL2, xfer_cfg_b ? xfer_cfg_b->xfersound : NULL)) {
  742. snprintf(buf, sizeof(buf), "Unable to add Channel2 to bridge: %s", ast_channel_name(chanb));
  743. astman_send_error(s, m, buf);
  744. ast_bridge_destroy(bridge, 0);
  745. return 0;
  746. }
  747. astman_send_ack(s, m, "Channels have been bridged");
  748. ao2_cleanup(bridge);
  749. return 0;
  750. }
  751. static char *app_bridge = "Bridge";
  752. enum {
  753. BRIDGE_OPT_PLAYTONE = (1 << 0),
  754. OPT_CALLEE_HANGUP = (1 << 1),
  755. OPT_CALLER_HANGUP = (1 << 2),
  756. OPT_DURATION_LIMIT = (1 << 3),
  757. OPT_DURATION_STOP = (1 << 4),
  758. OPT_CALLEE_TRANSFER = (1 << 5),
  759. OPT_CALLER_TRANSFER = (1 << 6),
  760. OPT_CALLEE_MONITOR = (1 << 7),
  761. OPT_CALLER_MONITOR = (1 << 8),
  762. OPT_CALLEE_PARK = (1 << 9),
  763. OPT_CALLER_PARK = (1 << 10),
  764. OPT_CALLEE_KILL = (1 << 11),
  765. OPT_CALLEE_GO_ON = (1 << 12),
  766. };
  767. enum {
  768. OPT_ARG_DURATION_LIMIT = 0,
  769. OPT_ARG_DURATION_STOP,
  770. OPT_ARG_CALLEE_GO_ON,
  771. /* note: this entry _MUST_ be the last one in the enum */
  772. OPT_ARG_ARRAY_SIZE,
  773. };
  774. AST_APP_OPTIONS(bridge_exec_options, BEGIN_OPTIONS
  775. AST_APP_OPTION('p', BRIDGE_OPT_PLAYTONE),
  776. AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
  777. AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
  778. AST_APP_OPTION('H', OPT_CALLER_HANGUP),
  779. AST_APP_OPTION('k', OPT_CALLEE_PARK),
  780. AST_APP_OPTION('K', OPT_CALLER_PARK),
  781. AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
  782. AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
  783. AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
  784. AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
  785. AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
  786. AST_APP_OPTION('W', OPT_CALLER_MONITOR),
  787. AST_APP_OPTION('x', OPT_CALLEE_KILL),
  788. END_OPTIONS );
  789. int ast_bridge_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
  790. char *parse, struct timeval *calldurationlimit)
  791. {
  792. char *stringp = ast_strdupa(parse);
  793. char *limit_str, *warning_str, *warnfreq_str;
  794. const char *var;
  795. int play_to_caller = 0, play_to_callee = 0;
  796. int delta;
  797. limit_str = strsep(&stringp, ":");
  798. warning_str = strsep(&stringp, ":");
  799. warnfreq_str = strsep(&stringp, ":");
  800. config->timelimit = atol(limit_str);
  801. if (warning_str)
  802. config->play_warning = atol(warning_str);
  803. if (warnfreq_str)
  804. config->warning_freq = atol(warnfreq_str);
  805. if (!config->timelimit) {
  806. ast_log(LOG_WARNING, "Bridge does not accept L(%s), hanging up.\n", limit_str);
  807. config->timelimit = config->play_warning = config->warning_freq = 0;
  808. config->warning_sound = NULL;
  809. return -1; /* error */
  810. } else if ( (delta = config->play_warning - config->timelimit) > 0) {
  811. int w = config->warning_freq;
  812. /*
  813. * If the first warning is requested _after_ the entire call
  814. * would end, and no warning frequency is requested, then turn
  815. * off the warning. If a warning frequency is requested, reduce
  816. * the 'first warning' time by that frequency until it falls
  817. * within the call's total time limit.
  818. *
  819. * Graphically:
  820. * timelim->| delta |<-playwarning
  821. * 0__________________|_________________|
  822. * | w | | | |
  823. *
  824. * so the number of intervals to cut is 1+(delta-1)/w
  825. */
  826. if (w == 0) {
  827. config->play_warning = 0;
  828. } else {
  829. config->play_warning -= w * ( 1 + (delta-1)/w );
  830. if (config->play_warning < 1)
  831. config->play_warning = config->warning_freq = 0;
  832. }
  833. }
  834. ast_channel_lock(chan);
  835. var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
  836. play_to_caller = var ? ast_true(var) : 1;
  837. var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
  838. play_to_callee = var ? ast_true(var) : 0;
  839. if (!play_to_caller && !play_to_callee)
  840. play_to_caller = 1;
  841. var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
  842. config->warning_sound = !ast_strlen_zero(var) ? ast_strdup(var) : ast_strdup("timeleft");
  843. /* The code looking at config wants a NULL, not just "", to decide
  844. * that the message should not be played, so we replace "" with NULL.
  845. * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
  846. * not found.
  847. */
  848. var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
  849. config->end_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
  850. var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
  851. config->start_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
  852. ast_channel_unlock(chan);
  853. /* undo effect of S(x) in case they are both used */
  854. calldurationlimit->tv_sec = 0;
  855. calldurationlimit->tv_usec = 0;
  856. /* more efficient to do it like S(x) does since no advanced opts */
  857. if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
  858. calldurationlimit->tv_sec = config->timelimit / 1000;
  859. calldurationlimit->tv_usec = (config->timelimit % 1000) * 1000;
  860. ast_verb(3, "Setting call duration limit to %.3lf seconds.\n",
  861. calldurationlimit->tv_sec + calldurationlimit->tv_usec / 1000000.0);
  862. play_to_caller = 0;
  863. play_to_callee = 0;
  864. config->timelimit = 0;
  865. config->play_warning = 0;
  866. config->warning_freq = 0;
  867. } else {
  868. ast_verb(4, "Limit Data for this call:\n");
  869. ast_verb(4, "timelimit = %ld ms (%.3lf s)\n", config->timelimit, config->timelimit / 1000.0);
  870. ast_verb(4, "play_warning = %ld ms (%.3lf s)\n", config->play_warning, config->play_warning / 1000.0);
  871. ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
  872. ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
  873. ast_verb(4, "warning_freq = %ld ms (%.3lf s)\n", config->warning_freq, config->warning_freq / 1000.0);
  874. ast_verb(4, "start_sound = %s\n", S_OR(config->start_sound, ""));
  875. ast_verb(4, "warning_sound = %s\n", config->warning_sound);
  876. ast_verb(4, "end_sound = %s\n", S_OR(config->end_sound, ""));
  877. }
  878. if (play_to_caller)
  879. ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
  880. if (play_to_callee)
  881. ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
  882. return 0;
  883. }
  884. /*!
  885. * \brief Bridge channels
  886. * \param chan
  887. * \param data channel to bridge with.
  888. *
  889. * Split data, check we aren't bridging with ourself, check valid channel,
  890. * answer call if not already, check compatible channels, setup bridge config
  891. * now bridge call, if transferred party hangs up return to PBX extension.
  892. */
  893. static int bridge_exec(struct ast_channel *chan, const char *data)
  894. {
  895. struct ast_channel *current_dest_chan;
  896. char *tmp_data = NULL;
  897. struct ast_flags opts = { 0, };
  898. struct ast_bridge_config bconfig = { { 0, }, };
  899. char *opt_args[OPT_ARG_ARRAY_SIZE];
  900. struct timeval calldurationlimit = { 0, };
  901. const char *context;
  902. const char *extension;
  903. int priority;
  904. int bridge_add_failed;
  905. struct ast_bridge_features chan_features;
  906. struct ast_bridge_features *peer_features;
  907. struct ast_bridge *bridge;
  908. struct ast_features_xfer_config *xfer_cfg;
  909. AST_DECLARE_APP_ARGS(args,
  910. AST_APP_ARG(dest_chan);
  911. AST_APP_ARG(options);
  912. );
  913. if (ast_strlen_zero(data)) {
  914. ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
  915. return -1;
  916. }
  917. tmp_data = ast_strdupa(data);
  918. AST_STANDARD_APP_ARGS(args, tmp_data);
  919. if (!ast_strlen_zero(args.options))
  920. ast_app_parse_options(bridge_exec_options, &opts, opt_args, args.options);
  921. /* make sure we have a valid end point */
  922. current_dest_chan = ast_channel_get_by_name_prefix(args.dest_chan,
  923. strlen(args.dest_chan));
  924. if (!current_dest_chan) {
  925. ast_log(LOG_WARNING, "Bridge failed because channel %s does not exist\n",
  926. args.dest_chan);
  927. return 0;
  928. }
  929. /* avoid bridge with ourselves */
  930. if (chan == current_dest_chan) {
  931. ast_channel_unref(current_dest_chan);
  932. ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", ast_channel_name(chan));
  933. return 0;
  934. }
  935. if (ast_test_flag(&opts, OPT_DURATION_LIMIT)
  936. && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])
  937. && ast_bridge_timelimit(chan, &bconfig, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit)) {
  938. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
  939. goto done;
  940. }
  941. if (ast_test_flag(&opts, OPT_CALLEE_TRANSFER))
  942. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_REDIRECT);
  943. if (ast_test_flag(&opts, OPT_CALLER_TRANSFER))
  944. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_REDIRECT);
  945. if (ast_test_flag(&opts, OPT_CALLEE_HANGUP))
  946. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
  947. if (ast_test_flag(&opts, OPT_CALLER_HANGUP))
  948. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
  949. if (ast_test_flag(&opts, OPT_CALLEE_MONITOR))
  950. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_AUTOMON);
  951. if (ast_test_flag(&opts, OPT_CALLER_MONITOR))
  952. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_AUTOMON);
  953. if (ast_test_flag(&opts, OPT_CALLEE_PARK))
  954. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_PARKCALL);
  955. if (ast_test_flag(&opts, OPT_CALLER_PARK))
  956. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_PARKCALL);
  957. /* Setup after bridge goto location. */
  958. if (ast_test_flag(&opts, OPT_CALLEE_GO_ON)) {
  959. ast_channel_lock(chan);
  960. context = ast_strdupa(ast_channel_context(chan));
  961. extension = ast_strdupa(ast_channel_exten(chan));
  962. priority = ast_channel_priority(chan);
  963. ast_channel_unlock(chan);
  964. ast_bridge_set_after_go_on(current_dest_chan, context, extension, priority,
  965. opt_args[OPT_ARG_CALLEE_GO_ON]);
  966. } else if (!ast_test_flag(&opts, OPT_CALLEE_KILL)) {
  967. ast_channel_lock(current_dest_chan);
  968. context = ast_strdupa(ast_channel_context(current_dest_chan));
  969. extension = ast_strdupa(ast_channel_exten(current_dest_chan));
  970. priority = ast_channel_priority(current_dest_chan);
  971. ast_channel_unlock(current_dest_chan);
  972. ast_bridge_set_after_go_on(current_dest_chan, context, extension, priority, NULL);
  973. }
  974. if (ast_bridge_features_init(&chan_features)) {
  975. ast_bridge_features_cleanup(&chan_features);
  976. goto done;
  977. }
  978. peer_features = ast_bridge_features_new();
  979. if (!peer_features) {
  980. ast_bridge_features_cleanup(&chan_features);
  981. goto done;
  982. }
  983. if (pre_bridge_setup(chan, current_dest_chan, &bconfig, &chan_features, peer_features)) {
  984. ast_bridge_features_destroy(peer_features);
  985. ast_bridge_features_cleanup(&chan_features);
  986. goto done;
  987. }
  988. bridge = ast_bridge_basic_new();
  989. if (!bridge) {
  990. ast_bridge_features_destroy(peer_features);
  991. ast_bridge_features_cleanup(&chan_features);
  992. goto done;
  993. }
  994. xfer_cfg = ast_get_chan_features_xfer_config(current_dest_chan);
  995. bridge_add_failed = ast_bridge_add_channel(bridge, current_dest_chan, peer_features,
  996. ast_test_flag(&opts, BRIDGE_OPT_PLAYTONE),
  997. xfer_cfg ? xfer_cfg->xfersound : NULL);
  998. ao2_cleanup(xfer_cfg);
  999. if (bridge_add_failed) {
  1000. ast_bridge_features_destroy(peer_features);
  1001. ast_bridge_features_cleanup(&chan_features);
  1002. ast_bridge_destroy(bridge, 0);
  1003. goto done;
  1004. }
  1005. /* Don't keep the channel ref in case it was not already in a bridge. */
  1006. current_dest_chan = ast_channel_unref(current_dest_chan);
  1007. ast_bridge_join(bridge, chan, NULL, &chan_features, NULL,
  1008. AST_BRIDGE_JOIN_PASS_REFERENCE);
  1009. ast_bridge_features_cleanup(&chan_features);
  1010. /* The bridge has ended, set BRIDGERESULT to SUCCESS. */
  1011. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
  1012. done:
  1013. ast_free((char *) bconfig.warning_sound);
  1014. ast_free((char *) bconfig.end_sound);
  1015. ast_free((char *) bconfig.start_sound);
  1016. ast_channel_cleanup(current_dest_chan);
  1017. return 0;
  1018. }
  1019. /*!
  1020. * \internal
  1021. * \brief Clean up resources on Asterisk shutdown
  1022. */
  1023. static void features_shutdown(void)
  1024. {
  1025. ast_features_config_shutdown();
  1026. ast_manager_unregister("Bridge");
  1027. ast_unregister_application(app_bridge);
  1028. }
  1029. int ast_features_init(void)
  1030. {
  1031. int res;
  1032. res = ast_features_config_init();
  1033. if (res) {
  1034. return res;
  1035. }
  1036. res |= ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
  1037. res |= ast_manager_register_xml_core("Bridge", EVENT_FLAG_CALL, action_bridge);
  1038. if (res) {
  1039. features_shutdown();
  1040. } else {
  1041. ast_register_cleanup(features_shutdown);
  1042. }
  1043. return res;
  1044. }