Debian Codegraph @

main/features.c

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