Bridge system: Fix memory leaks and double frees on impart failure.
[asterisk/asterisk.git] / res / stasis / stasis_bridge.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Richard Mudgett <rmudgett@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  * \brief Stasis bridge subclass.
22  *
23  * \author Richard Mudgett <rmudgett@digium.com>
24  *
25  * See Also:
26  * \arg \ref AstCREDITS
27  */
28
29
30 #include "asterisk.h"
31
32 ASTERISK_REGISTER_FILE()
33
34 #include "asterisk/bridge.h"
35 #include "asterisk/bridge_after.h"
36 #include "asterisk/bridge_internal.h"
37 #include "asterisk/bridge_features.h"
38 #include "asterisk/stasis_app.h"
39 #include "asterisk/stasis_channels.h"
40 #include "stasis_bridge.h"
41 #include "control.h"
42 #include "command.h"
43 #include "app.h"
44 #include "asterisk/stasis_app.h"
45 #include "asterisk/pbx.h"
46
47 /* ------------------------------------------------------------------- */
48
49 static struct ast_bridge_methods bridge_stasis_v_table;
50
51 static void bridge_stasis_run_cb(struct ast_channel *chan, void *data)
52 {
53         RAII_VAR(char *, app_name, NULL, ast_free);
54         struct ast_app *app_stasis;
55
56         /* Take ownership of the swap_app memory from the datastore */
57         app_name = app_get_replace_channel_app(chan);
58         if (!app_name) {
59                 ast_log(LOG_ERROR, "Failed to get app name for %s (%p)\n", ast_channel_name(chan), chan);
60                 return;
61         }
62
63         /* find Stasis() */
64         app_stasis = pbx_findapp("Stasis");
65         if (!app_stasis) {
66                 ast_log(LOG_WARNING, "Could not find application (Stasis)\n");
67                 return;
68         }
69
70         if (ast_check_hangup_locked(chan)) {
71                 /* channel hungup, don't run Stasis() */
72                 return;
73         }
74
75         /* run Stasis() */
76         pbx_exec(chan, app_stasis, app_name);
77 }
78
79 static int add_channel_to_bridge(
80         struct stasis_app_control *control,
81         struct ast_channel *chan, void *obj)
82 {
83         struct ast_bridge *bridge = obj;
84         int res;
85
86         res = control_add_channel_to_bridge(control,
87                 chan, bridge);
88         return res;
89 }
90
91 static void bridge_stasis_queue_join_action(struct ast_bridge *self,
92         struct ast_bridge_channel *bridge_channel)
93 {
94         ast_channel_lock(bridge_channel->chan);
95         command_prestart_queue_command(bridge_channel->chan, add_channel_to_bridge,
96                 ao2_bump(self), __ao2_cleanup);
97         ast_channel_unlock(bridge_channel->chan);
98 }
99
100 /*!
101  * \internal
102  * \brief Peek at channel before it is pushed into bridge
103  * \since 13.2.0
104  *
105  * \param self Bridge to operate upon.
106  * \param bridge_channel Bridge channel to push.
107  * \param swap Bridge channel to swap places with if not NULL.
108  *
109  * \note On entry, self is already locked.
110  *
111  * \retval 0 on success.
112  * \retval -1 on failure.  The channel should not be pushed.
113  */
114 static int bridge_stasis_push_peek(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
115 {
116         struct stasis_app_control *swap_control;
117         struct ast_channel_snapshot *to_be_replaced;
118
119         if (!swap) {
120                 goto done;
121         }
122
123         swap_control = stasis_app_control_find_by_channel(swap->chan);
124         if (!swap_control) {
125                 ast_log(LOG_ERROR,"Failed to find stasis app control for swapped channel %s\n", ast_channel_name(swap->chan));
126                 return -1;
127         }
128         to_be_replaced = ast_channel_snapshot_get_latest(ast_channel_uniqueid(swap->chan));
129
130         ast_debug(3, "Copying stasis app name %s from %s to %s\n", app_name(control_app(swap_control)),
131                 ast_channel_name(swap->chan), ast_channel_name(bridge_channel->chan));
132
133         ast_channel_lock(bridge_channel->chan);
134
135         /* copy the app name from the swap channel */
136         app_set_replace_channel_app(bridge_channel->chan, app_name(control_app(swap_control)));
137
138         /* set the replace channel snapshot */
139         app_set_replace_channel_snapshot(bridge_channel->chan, to_be_replaced);
140
141         ast_channel_unlock(bridge_channel->chan);
142
143         ao2_ref(swap_control, -1);
144         ao2_cleanup(to_be_replaced);
145
146 done:
147         return ast_bridge_base_v_table.push_peek(self, bridge_channel, swap);
148 }
149
150 /*!
151  * \internal
152  * \brief Push this channel into the Stasis bridge.
153  * \since 12.5.0
154  *
155  * \param self Bridge to operate upon.
156  * \param bridge_channel Bridge channel to push.
157  * \param swap Bridge channel to swap places with if not NULL.
158  *
159  * \note On entry, self is already locked.
160  *
161  * \retval 0 on success.
162  * \retval -1 on failure.  The channel did not get pushed.
163  */
164 static int bridge_stasis_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
165 {
166         struct stasis_app_control *control = stasis_app_control_find_by_channel(bridge_channel->chan);
167
168         if (!control && !stasis_app_channel_is_internal(bridge_channel->chan)) {
169                 /* channel not in Stasis(), get it there */
170                 ast_debug(1, "Bridge %s: pushing non-stasis %p(%s) setup to come back in under stasis\n",
171                         self->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
172
173                 /* Attach after-bridge callback and pass ownership of swap_app to it */
174                 if (ast_bridge_set_after_callback(bridge_channel->chan,
175                         bridge_stasis_run_cb, NULL, NULL)) {
176                         ast_log(LOG_ERROR,
177                                 "Failed to set after bridge callback for bridge %s non-stasis push of %s\n",
178                                 self->uniqueid, ast_channel_name(bridge_channel->chan));
179                         return -1;
180                 }
181
182                 bridge_stasis_queue_join_action(self, bridge_channel);
183                 if (swap) {
184                         /* nudge the swap channel out of the bridge */
185                         ast_bridge_channel_leave_bridge(swap, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
186                 }
187
188                 /* Return -1 so the push fails and the after-bridge callback gets called
189                  * This keeps the bridging framework from putting the channel into the bridge
190                  * until the Stasis thread gets started, and then the channel is put into the bridge.
191                  */
192                 return -1;
193         }
194
195         /*
196          * If going into a holding bridge, default the role to participant, if
197          * it has no compatible role currently
198          */
199         if ((self->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING)
200             && !ast_channel_has_role(bridge_channel->chan, "announcer")
201             && !ast_channel_has_role(bridge_channel->chan, "holding_participant")) {
202                 if (ast_channel_add_bridge_role(bridge_channel->chan, "holding_participant")) {
203                         ast_log(LOG_ERROR, "Failed to set holding participant on %s\n", ast_channel_name(bridge_channel->chan));
204                         return -1;
205                 }
206
207                 if (ast_channel_set_bridge_role_option(bridge_channel->chan, "holding_participant", "idle_mode", "none")) {
208                         ast_log(LOG_ERROR, "Failed to set holding participant mode on %s\n", ast_channel_name(bridge_channel->chan));
209                         return -1;
210                 }
211         }
212
213         ao2_cleanup(control);
214         if (self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES) {
215                 ast_bridge_channel_update_linkedids(bridge_channel, swap);
216                 if (ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
217                         ast_bridge_channel_update_accountcodes(bridge_channel, swap);
218                 }
219         }
220
221         return ast_bridge_base_v_table.push(self, bridge_channel, swap);
222 }
223
224 static int bridge_stasis_moving(struct ast_bridge_channel *bridge_channel, void *hook_pvt,
225                 struct ast_bridge *src, struct ast_bridge *dst)
226 {
227         if (src->v_table == &bridge_stasis_v_table &&
228                         dst->v_table != &bridge_stasis_v_table) {
229                 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
230                 struct ast_channel *chan;
231
232                 chan = bridge_channel->chan;
233                 ast_assert(chan != NULL);
234
235                 control = stasis_app_control_find_by_channel(chan);
236                 if (!control) {
237                         return -1;
238                 }
239
240                 stasis_app_channel_set_stasis_end_published(chan);
241                 app_send_end_msg(control_app(control), chan);
242         }
243
244         return -1;
245 }
246
247 /*!
248  * \internal
249  * \brief Pull this channel from the Stasis bridge.
250  * \since 12.5.0
251  *
252  * \param self Bridge to operate upon.
253  * \param bridge_channel Bridge channel to pull.
254  *
255  * \note On entry, self is already locked.
256  *
257  * \return Nothing
258  */
259 static void bridge_stasis_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
260 {
261         if ((self->allowed_capabilities & STASIS_BRIDGE_MIXING_CAPABILITIES)
262                 && ast_test_flag(&self->feature_flags, AST_BRIDGE_FLAG_SMART)) {
263                 ast_bridge_channel_update_accountcodes(NULL, bridge_channel);
264         }
265
266         if (self->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
267                 ast_channel_clear_bridge_roles(bridge_channel->chan);
268         }
269
270         ast_bridge_move_hook(bridge_channel->features, bridge_stasis_moving, NULL, NULL, 0);
271
272         ast_bridge_base_v_table.pull(self, bridge_channel);
273 }
274
275 struct ast_bridge *bridge_stasis_new(uint32_t capabilities, unsigned int flags, const char *name, const char *id)
276 {
277         void *bridge;
278
279         bridge = bridge_alloc(sizeof(struct ast_bridge), &bridge_stasis_v_table);
280         bridge = bridge_base_init(bridge, capabilities, flags, "Stasis", name, id);
281         bridge = bridge_register(bridge);
282
283         return bridge;
284 }
285
286 void bridge_stasis_init(void)
287 {
288         /* Setup the Stasis bridge subclass v_table. */
289         bridge_stasis_v_table = ast_bridge_base_v_table;
290         bridge_stasis_v_table.name = "stasis";
291         bridge_stasis_v_table.push = bridge_stasis_push;
292         bridge_stasis_v_table.pull = bridge_stasis_pull;
293         bridge_stasis_v_table.push_peek = bridge_stasis_push_peek;
294 }