Better protect bridge_channel state from other threads.
[asterisk/asterisk.git] / bridges / bridge_builtin_features.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@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  *
21  * \brief Built in bridging features
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  *
25  * \ingroup bridges
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41
42 #include "asterisk/module.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/bridging.h"
45 #include "asterisk/bridging_technology.h"
46 #include "asterisk/frame.h"
47 #include "asterisk/file.h"
48 #include "asterisk/app.h"
49 #include "asterisk/astobj2.h"
50
51 /*! \brief Helper function that presents dialtone and grabs extension */
52 static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len, const char *context)
53 {
54         int res;
55
56         /* Play the simple "transfer" prompt out and wait */
57         res = ast_stream_and_wait(chan, "pbx-transfer", AST_DIGIT_ANY);
58         ast_stopstream(chan);
59
60         /* If the person hit a DTMF digit while the above played back stick it into the buffer */
61         if (res) {
62                 exten[0] = (char)res;
63         }
64
65         /* Drop to dialtone so they can enter the extension they want to transfer to */
66         res = ast_app_dtget(chan, context, exten, exten_len, 100, 1000);
67
68         return res;
69 }
70
71 /*! \brief Helper function that creates an outgoing channel and returns it immediately */
72 static struct ast_channel *dial_transfer(struct ast_channel *caller, const char *exten, const char *context)
73 {
74         char destination[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 1];
75         struct ast_channel *chan;
76         int cause;
77
78         /* Fill the variable with the extension and context we want to call */
79         snprintf(destination, sizeof(destination), "%s@%s", exten, context);
80
81         /* Now we request that chan_local prepare to call the destination */
82         if (!(chan = ast_request("Local", ast_channel_nativeformats(caller), caller, destination, &cause))) {
83                 return NULL;
84         }
85
86         /* Before we actually dial out let's inherit appropriate information. */
87         ast_channel_lock_both(caller, chan);
88         ast_connected_line_copy_from_caller(ast_channel_connected(chan), ast_channel_caller(caller));
89         ast_channel_inherit_variables(caller, chan);
90         ast_channel_datastore_inherit(caller, chan);
91         ast_channel_unlock(chan);
92         ast_channel_unlock(caller);
93
94         /* Since the above worked fine now we actually call it and return the channel */
95         if (ast_call(chan, destination, 0)) {
96                 ast_hangup(chan);
97                 return NULL;
98         }
99
100         return chan;
101 }
102
103 /*! \brief Internal built in feature for blind transfers */
104 static int feature_blind_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
105 {
106         char exten[AST_MAX_EXTENSION] = "";
107         struct ast_channel *chan = NULL;
108         struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
109         const char *context = (blind_transfer && !ast_strlen_zero(blind_transfer->context) ? blind_transfer->context : ast_channel_context(bridge_channel->chan));
110
111         /* Grab the extension to transfer to */
112         if (!grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
113                 ast_stream_and_wait(bridge_channel->chan, "pbx-invalid", AST_DIGIT_ANY);
114                 return 0;
115         }
116
117         /* Get a channel that is the destination we wish to call */
118         if (!(chan = dial_transfer(bridge_channel->chan, exten, context))) {
119                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
120                 return 0;
121         }
122
123         /* This is sort of the fun part. We impart the above channel onto the bridge, and have it take our place. */
124         ast_bridge_impart(bridge, chan, bridge_channel->chan, NULL, 1);
125
126         return 0;
127 }
128
129 /*! \brief Attended transfer feature to turn it into a threeway call */
130 static int attended_threeway_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
131 {
132         /*
133          * This is sort of abusing the depart state but in this instance
134          * it is only going to be handled by feature_attended_transfer()
135          * so it is okay.
136          */
137         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_DEPART);
138         return 0;
139 }
140
141 /*! \brief Attended transfer abort feature */
142 static int attended_abort_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
143 {
144         struct ast_bridge_channel *called_bridge_channel = NULL;
145
146         /* It is possible (albeit unlikely) that the bridge channels list may change, so we have to ensure we do all of our magic while locked */
147         ao2_lock(bridge);
148
149         if (AST_LIST_FIRST(&bridge->channels) != bridge_channel) {
150                 called_bridge_channel = AST_LIST_FIRST(&bridge->channels);
151         } else {
152                 called_bridge_channel = AST_LIST_LAST(&bridge->channels);
153         }
154
155         /* Now we basically eject the other channel from the bridge. This will cause their thread to hang them up, and our own code to consider the transfer failed. */
156         if (called_bridge_channel) {
157                 ast_bridge_change_state(called_bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
158         }
159
160         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
161
162         ao2_unlock(bridge);
163
164         return 0;
165 }
166
167 /*! \brief Internal built in feature for attended transfers */
168 static int feature_attended_transfer(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
169 {
170         char exten[AST_MAX_EXTENSION] = "";
171         struct ast_channel *chan = NULL;
172         struct ast_bridge *attended_bridge = NULL;
173         struct ast_bridge_features caller_features, called_features;
174         enum ast_bridge_channel_state attended_bridge_result;
175         struct ast_bridge_features_attended_transfer *attended_transfer = hook_pvt;
176         const char *context = (attended_transfer && !ast_strlen_zero(attended_transfer->context) ? attended_transfer->context : ast_channel_context(bridge_channel->chan));
177
178         /* Grab the extension to transfer to */
179         if (!grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
180                 ast_stream_and_wait(bridge_channel->chan, "pbx-invalid", AST_DIGIT_ANY);
181                 return 0;
182         }
183
184         /* Get a channel that is the destination we wish to call */
185         if (!(chan = dial_transfer(bridge_channel->chan, exten, context))) {
186                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
187                 return 0;
188         }
189
190         /* Create a bridge to use to talk to the person we are calling */
191         if (!(attended_bridge = ast_bridge_new(AST_BRIDGE_CAPABILITY_1TO1MIX, 0))) {
192                 ast_hangup(chan);
193                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
194                 return 0;
195         }
196
197         /* Setup our called features structure so that if they hang up we immediately get thrown out of the bridge */
198         ast_bridge_features_init(&called_features);
199         ast_bridge_features_set_flag(&called_features, AST_BRIDGE_FLAG_DISSOLVE);
200
201         /* This is how this is going down, we are imparting the channel we called above into this bridge first */
202         ast_bridge_impart(attended_bridge, chan, NULL, &called_features, 1);
203
204         /* Before we join setup a features structure with the hangup option, just in case they want to use DTMF */
205         ast_bridge_features_init(&caller_features);
206         ast_bridge_features_enable(&caller_features, AST_BRIDGE_BUILTIN_HANGUP,
207                                    (attended_transfer && !ast_strlen_zero(attended_transfer->complete) ? attended_transfer->complete : "*1"), NULL);
208         ast_bridge_features_hook(&caller_features, (attended_transfer && !ast_strlen_zero(attended_transfer->threeway) ? attended_transfer->threeway : "*2"),
209                                  attended_threeway_transfer, NULL, NULL);
210         ast_bridge_features_hook(&caller_features, (attended_transfer && !ast_strlen_zero(attended_transfer->abort) ? attended_transfer->abort : "*3"),
211                                  attended_abort_transfer, NULL, NULL);
212
213         /* But for the caller we want to join the bridge in a blocking fashion so we don't spin around in this function doing nothing while waiting */
214         attended_bridge_result = ast_bridge_join(attended_bridge, bridge_channel->chan, NULL, &caller_features, NULL);
215
216         /* Since the above returned the caller features structure is of no more use */
217         ast_bridge_features_cleanup(&caller_features);
218
219         /* Drop the channel we are transferring to out of the above bridge since it has ended */
220         if ((attended_bridge_result != AST_BRIDGE_CHANNEL_STATE_HANGUP) && !ast_bridge_depart(attended_bridge, chan)) {
221                 /* If the user wants to turn this into a threeway transfer then do so, otherwise they take our place */
222                 if (attended_bridge_result == AST_BRIDGE_CHANNEL_STATE_DEPART) {
223                         /* We want to impart them upon the bridge and just have us return to it as normal */
224                         ast_bridge_impart(bridge, chan, NULL, NULL, 1);
225                 } else {
226                         ast_bridge_impart(bridge, chan, bridge_channel->chan, NULL, 1);
227                 }
228         } else {
229                 ast_stream_and_wait(bridge_channel->chan, "beeperr", AST_DIGIT_ANY);
230         }
231
232         /* Now that all channels are out of it we can destroy the bridge and the called features structure */
233         ast_bridge_features_cleanup(&called_features);
234         ast_bridge_destroy(attended_bridge);
235
236         return 0;
237 }
238
239 /*! \brief Internal built in feature for hangup */
240 static int feature_hangup(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
241 {
242         /*
243          * This is very simple, we simply change the state on the
244          * bridge_channel to force the channel out of the bridge and the
245          * core takes care of the rest.
246          */
247         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
248         return 0;
249 }
250
251 static int unload_module(void)
252 {
253         return 0;
254 }
255
256 static int load_module(void)
257 {
258         ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
259         ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
260         ast_bridge_features_register(AST_BRIDGE_BUILTIN_HANGUP, feature_hangup, NULL);
261
262         /* Bump up our reference count so we can't be unloaded */
263         ast_module_ref(ast_module_info->self);
264
265         return AST_MODULE_LOAD_SUCCESS;
266 }
267
268 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Built in bridging features");