Local channels: Do not block control -1 payloads.
[asterisk/asterisk.git] / main / core_unreal.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013 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 Unreal channel derivatives framework for channel drivers like local channels.
22  *
23  * \author Richard Mudgett <rmudgett@digium.com>
24  *
25  * See Also:
26  * \arg \ref AstCREDITS
27  */
28
29 /*** MODULEINFO
30         <support_level>core</support_level>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_REGISTER_FILE()
36
37 #include "asterisk/causes.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/stasis_channels.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/musiconhold.h"
42 #include "asterisk/astobj2.h"
43 #include "asterisk/bridge.h"
44 #include "asterisk/core_unreal.h"
45
46 static unsigned int name_sequence = 0;
47
48 void ast_unreal_lock_all(struct ast_unreal_pvt *p, struct ast_channel **outchan, struct ast_channel **outowner)
49 {
50         struct ast_channel *chan = NULL;
51         struct ast_channel *owner = NULL;
52
53         ao2_lock(p);
54         for (;;) {
55                 if (p->chan) {
56                         chan = p->chan;
57                         ast_channel_ref(chan);
58                 }
59                 if (p->owner) {
60                         owner = p->owner;
61                         ast_channel_ref(owner);
62                 }
63                 ao2_unlock(p);
64
65                 /* if we don't have both channels, then this is very easy */
66                 if (!owner || !chan) {
67                         if (owner) {
68                                 ast_channel_lock(owner);
69                         } else if(chan) {
70                                 ast_channel_lock(chan);
71                         }
72                 } else {
73                         /* lock both channels first, then get the pvt lock */
74                         ast_channel_lock_both(chan, owner);
75                 }
76                 ao2_lock(p);
77
78                 /* Now that we have all the locks, validate that nothing changed */
79                 if (p->owner != owner || p->chan != chan) {
80                         if (owner) {
81                                 ast_channel_unlock(owner);
82                                 owner = ast_channel_unref(owner);
83                         }
84                         if (chan) {
85                                 ast_channel_unlock(chan);
86                                 chan = ast_channel_unref(chan);
87                         }
88                         continue;
89                 }
90
91                 break;
92         }
93         *outowner = p->owner;
94         *outchan = p->chan;
95 }
96
97 /* Called with ast locked */
98 int ast_unreal_setoption(struct ast_channel *ast, int option, void *data, int datalen)
99 {
100         int res = 0;
101         struct ast_unreal_pvt *p;
102         struct ast_channel *otherchan = NULL;
103         ast_chan_write_info_t *write_info;
104         char *info_data;
105
106         if (option != AST_OPTION_CHANNEL_WRITE) {
107                 return -1;
108         }
109
110         write_info = data;
111
112         if (write_info->version != AST_CHAN_WRITE_INFO_T_VERSION) {
113                 ast_log(LOG_ERROR, "The chan_write_info_t type has changed, and this channel hasn't been updated!\n");
114                 return -1;
115         }
116
117         info_data = write_info->data;
118         if (!strcmp(write_info->function, "CHANNEL")) {
119                 if (!strncasecmp(info_data, "hangup_handler_", 15)) {
120                         /* Block CHANNEL(hangup_handler_xxx) writes to the other unreal channel. */
121                         return 0;
122                 }
123
124                 /* Crossover the accountcode and peeraccount to cross the unreal bridge. */
125                 if (!strcasecmp(info_data, "accountcode")) {
126                         info_data = "peeraccount";
127                 } else if (!strcasecmp(info_data, "peeraccount")) {
128                         info_data = "accountcode";
129                 }
130         }
131
132         /* get the tech pvt */
133         if (!(p = ast_channel_tech_pvt(ast))) {
134                 return -1;
135         }
136         ao2_ref(p, 1);
137         ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
138
139         /* get the channel we are supposed to write to */
140         ao2_lock(p);
141         otherchan = (write_info->chan == p->owner) ? p->chan : p->owner;
142         if (!otherchan || otherchan == write_info->chan) {
143                 res = -1;
144                 otherchan = NULL;
145                 ao2_unlock(p);
146                 goto setoption_cleanup;
147         }
148         ast_channel_ref(otherchan);
149
150         /* clear the pvt lock before grabbing the channel */
151         ao2_unlock(p);
152
153         ast_channel_lock(otherchan);
154         res = write_info->write_fn(otherchan, write_info->function, info_data, write_info->value);
155         ast_channel_unlock(otherchan);
156
157 setoption_cleanup:
158         ao2_ref(p, -1);
159         if (otherchan) {
160                 ast_channel_unref(otherchan);
161         }
162         ast_channel_lock(ast); /* Lock back before we leave */
163         return res;
164 }
165
166 /* Called with ast locked */
167 int ast_unreal_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
168 {
169         struct ast_unreal_pvt *p;
170         struct ast_channel *peer;
171         struct ast_channel *other;
172         int res = 0;
173
174         if (option != AST_OPTION_T38_STATE) {
175                 /* AST_OPTION_T38_STATE is the only supported option at this time */
176                 return -1;
177         }
178
179         /* for some reason the channel is not locked in channel.c when this function is called */
180         if (!(p = ast_channel_tech_pvt(ast))) {
181                 return -1;
182         }
183
184         ao2_lock(p);
185         other = AST_UNREAL_IS_OUTBOUND(ast, p) ? p->owner : p->chan;
186         if (!other) {
187                 ao2_unlock(p);
188                 return -1;
189         }
190         ast_channel_ref(other);
191         ao2_unlock(p);
192         ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
193
194         peer = ast_channel_bridge_peer(other);
195         if (peer) {
196                 res = ast_channel_queryoption(peer, option, data, datalen, 0);
197                 ast_channel_unref(peer);
198         }
199         ast_channel_unref(other);
200         ast_channel_lock(ast); /* Lock back before we leave */
201
202         return res;
203 }
204
205 /*!
206  * \brief queue a frame onto either the p->owner or p->chan
207  *
208  * \note the ast_unreal_pvt MUST have it's ref count bumped before entering this function and
209  * decremented after this function is called.  This is a side effect of the deadlock
210  * avoidance that is necessary to lock 2 channels and a tech_pvt.  Without a ref counted
211  * ast_unreal_pvt, it is impossible to guarantee it will not be destroyed by another thread
212  * during deadlock avoidance.
213  */
214 static int unreal_queue_frame(struct ast_unreal_pvt *p, int isoutbound, struct ast_frame *f,
215         struct ast_channel *us, int us_locked)
216 {
217         struct ast_channel *other;
218
219         /* Recalculate outbound channel */
220         other = isoutbound ? p->owner : p->chan;
221         if (!other) {
222                 return 0;
223         }
224
225         /* do not queue media frames if a generator is on both unreal channels */
226         if (us
227                 && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)
228                 && ast_channel_generator(us)
229                 && ast_channel_generator(other)) {
230                 return 0;
231         }
232
233         /* grab a ref on the channel before unlocking the pvt,
234          * other can not go away from us now regardless of locking */
235         ast_channel_ref(other);
236         if (us && us_locked) {
237                 ast_channel_unlock(us);
238         }
239         ao2_unlock(p);
240
241         if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_RINGING) {
242                 ast_setstate(other, AST_STATE_RINGING);
243         }
244         ast_queue_frame(other, f);
245
246         other = ast_channel_unref(other);
247         if (us && us_locked) {
248                 ast_channel_lock(us);
249         }
250         ao2_lock(p);
251
252         return 0;
253 }
254
255 int ast_unreal_answer(struct ast_channel *ast)
256 {
257         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
258         int isoutbound;
259         int res = -1;
260
261         if (!p) {
262                 return -1;
263         }
264
265         ao2_ref(p, 1);
266         ao2_lock(p);
267         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
268         if (isoutbound) {
269                 /* Pass along answer since somebody answered us */
270                 struct ast_frame answer = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
271
272                 res = unreal_queue_frame(p, isoutbound, &answer, ast, 1);
273         } else {
274                 ast_log(LOG_WARNING, "Huh?  %s is being asked to answer?\n",
275                         ast_channel_name(ast));
276         }
277         ao2_unlock(p);
278         ao2_ref(p, -1);
279         return res;
280 }
281
282 /*!
283  * \internal
284  * \brief Check and optimize out the unreal channels between bridges.
285  * \since 12.0.0
286  *
287  * \param ast Channel writing a frame into the unreal channels.
288  * \param p Unreal channel private.
289  *
290  * \note It is assumed that ast is locked.
291  * \note It is assumed that p is locked.
292  *
293  * \retval 0 if unreal channels were not optimized out.
294  * \retval non-zero if unreal channels were optimized out.
295  */
296 static int got_optimized_out(struct ast_channel *ast, struct ast_unreal_pvt *p)
297 {
298         int res = 0;
299
300         /* Do a few conditional checks early on just to see if this optimization is possible */
301         if (ast_test_flag(p, AST_UNREAL_NO_OPTIMIZATION) || !p->chan || !p->owner) {
302                 return res;
303         }
304
305         if (ast == p->owner) {
306                 res = ast_bridge_unreal_optimize_out(p->owner, p->chan, p);
307         } else if (ast == p->chan) {
308                 res = ast_bridge_unreal_optimize_out(p->chan, p->owner, p);
309         }
310
311         return res;
312 }
313
314 struct ast_frame  *ast_unreal_read(struct ast_channel *ast)
315 {
316         return &ast_null_frame;
317 }
318
319 int ast_unreal_write(struct ast_channel *ast, struct ast_frame *f)
320 {
321         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
322         int res = -1;
323
324         if (!p) {
325                 return -1;
326         }
327
328         /* Just queue for delivery to the other side */
329         ao2_ref(p, 1);
330         ao2_lock(p);
331         switch (f->frametype) {
332         case AST_FRAME_VOICE:
333         case AST_FRAME_VIDEO:
334                 if (got_optimized_out(ast, p)) {
335                         break;
336                 }
337                 /* fall through */
338         default:
339                 res = unreal_queue_frame(p, AST_UNREAL_IS_OUTBOUND(ast, p), f, ast, 1);
340                 break;
341         }
342         ao2_unlock(p);
343         ao2_ref(p, -1);
344
345         return res;
346 }
347
348 int ast_unreal_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
349 {
350         struct ast_unreal_pvt *p = ast_channel_tech_pvt(newchan);
351         struct ast_bridge *bridge_owner;
352         struct ast_bridge *bridge_chan;
353
354         if (!p) {
355                 return -1;
356         }
357
358         ao2_lock(p);
359
360         if ((p->owner != oldchan) && (p->chan != oldchan)) {
361                 ast_log(LOG_WARNING, "Old channel %p wasn't %p or %p\n", oldchan, p->owner, p->chan);
362                 ao2_unlock(p);
363                 return -1;
364         }
365         if (p->owner == oldchan) {
366                 p->owner = newchan;
367         } else {
368                 p->chan = newchan;
369         }
370
371         if (ast_check_hangup(newchan) || !p->owner || !p->chan) {
372                 ao2_unlock(p);
373                 return 0;
374         }
375
376         /* Do not let a masquerade cause an unreal channel to be bridged to itself! */
377         bridge_owner = ast_channel_internal_bridge(p->owner);
378         bridge_chan = ast_channel_internal_bridge(p->chan);
379         if (bridge_owner && bridge_owner == bridge_chan) {
380                 ast_log(LOG_WARNING, "You can not bridge an unreal channel (%s) to itself!\n",
381                         ast_channel_name(newchan));
382                 ao2_unlock(p);
383                 ast_queue_hangup(newchan);
384                 return -1;
385         }
386
387         ao2_unlock(p);
388         return 0;
389 }
390
391 /*!
392  * \internal
393  * \brief Queue up a frame representing the indication as a control frame.
394  * \since 12.0.0
395  *
396  * \param p Unreal private structure.
397  * \param ast Channel indicating the condition.
398  * \param condition What is being indicated.
399  * \param data Extra data.
400  * \param datalen Length of extra data.
401  *
402  * \retval 0 on success.
403  * \retval AST_T38_REQUEST_PARMS if successful and condition is AST_CONTROL_T38_PARAMETERS.
404  * \retval -1 on error.
405  */
406 static int unreal_queue_indicate(struct ast_unreal_pvt *p, struct ast_channel *ast, int condition, const void *data, size_t datalen)
407 {
408         int res = 0;
409         int isoutbound;
410
411         ao2_lock(p);
412         if (ast_test_flag(p, AST_UNREAL_NO_OPTIMIZATION)) {
413                 struct ast_frame f = {
414                         .frametype = AST_FRAME_CONTROL,
415                         .subclass.integer = condition,
416                         .data.ptr = (void *) data,
417                         .datalen = datalen,
418                 };
419
420                 isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
421                 res = unreal_queue_frame(p, isoutbound, &f, ast, 1);
422                 if (!res
423                         && condition == AST_CONTROL_T38_PARAMETERS
424                         && datalen == sizeof(struct ast_control_t38_parameters)) {
425                         const struct ast_control_t38_parameters *parameters = data;
426
427                         if (parameters->request_response == AST_T38_REQUEST_PARMS) {
428                                 res = AST_T38_REQUEST_PARMS;
429                         }
430                 }
431         } else {
432                 ast_debug(4, "Blocked indication %d\n", condition);
433         }
434         ao2_unlock(p);
435
436         return res;
437 }
438
439 /*!
440  * \internal
441  * \brief Handle COLP and redirecting conditions.
442  * \since 12.0.0
443  *
444  * \param p Unreal private structure.
445  * \param ast Channel indicating the condition.
446  * \param condition What is being indicated.
447  *
448  * \retval 0 on success.
449  * \retval -1 on error.
450  */
451 static int unreal_colp_redirect_indicate(struct ast_unreal_pvt *p, struct ast_channel *ast, int condition)
452 {
453         struct ast_channel *my_chan;
454         struct ast_channel *my_owner;
455         struct ast_channel *this_channel;
456         struct ast_channel *the_other_channel;
457         int isoutbound;
458         int res = 0;
459         unsigned char frame_data[1024];
460         struct ast_frame f = {
461                 .frametype = AST_FRAME_CONTROL,
462                 .subclass.integer = condition,
463                 .data.ptr = frame_data,
464         };
465
466         /*
467          * A connected line update frame may only contain a partial
468          * amount of data, such as just a source, or just a ton, and not
469          * the full amount of information.  However, the collected
470          * information is all stored in the outgoing channel's
471          * connectedline structure, so when receiving a connected line
472          * update on an outgoing unreal channel, we need to transmit the
473          * collected connected line information instead of whatever
474          * happens to be in this control frame.  The same applies for
475          * redirecting information, which is why it is handled here as
476          * well.
477          */
478         ast_channel_unlock(ast);
479         ast_unreal_lock_all(p, &my_chan, &my_owner);
480         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
481         if (isoutbound) {
482                 this_channel = p->chan;
483                 the_other_channel = p->owner;
484         } else {
485                 this_channel = p->owner;
486                 the_other_channel = p->chan;
487         }
488         if (the_other_channel) {
489                 if (condition == AST_CONTROL_CONNECTED_LINE) {
490                         ast_connected_line_copy_to_caller(ast_channel_caller(the_other_channel),
491                                 ast_channel_connected(this_channel));
492                         f.datalen = ast_connected_line_build_data(frame_data, sizeof(frame_data),
493                                 ast_channel_connected(this_channel), NULL);
494                 } else {
495                         f.datalen = ast_redirecting_build_data(frame_data, sizeof(frame_data),
496                                 ast_channel_redirecting(this_channel), NULL);
497                 }
498         }
499         if (my_chan) {
500                 ast_channel_unlock(my_chan);
501                 ast_channel_unref(my_chan);
502         }
503         if (my_owner) {
504                 ast_channel_unlock(my_owner);
505                 ast_channel_unref(my_owner);
506         }
507         if (the_other_channel) {
508                 res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
509         }
510         ao2_unlock(p);
511         ast_channel_lock(ast);
512
513         return res;
514 }
515
516 int ast_unreal_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
517 {
518         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
519         int res = 0;
520
521         if (!p) {
522                 return -1;
523         }
524
525         ao2_ref(p, 1); /* ref for unreal_queue_frame */
526
527         switch (condition) {
528         case AST_CONTROL_MASQUERADE_NOTIFY:
529                 /*
530                  * Always block this because this is the channel being
531                  * masqueraded; not anything down the chain.
532                  */
533                 break;
534         case AST_CONTROL_CONNECTED_LINE:
535         case AST_CONTROL_REDIRECTING:
536                 res = unreal_colp_redirect_indicate(p, ast, condition);
537                 break;
538         case AST_CONTROL_HOLD:
539                 if (ast_test_flag(p, AST_UNREAL_MOH_INTERCEPT)) {
540                         ast_moh_start(ast, data, NULL);
541                         break;
542                 }
543                 res = unreal_queue_indicate(p, ast, condition, data, datalen);
544                 break;
545         case AST_CONTROL_UNHOLD:
546                 if (ast_test_flag(p, AST_UNREAL_MOH_INTERCEPT)) {
547                         ast_moh_stop(ast);
548                         break;
549                 }
550                 res = unreal_queue_indicate(p, ast, condition, data, datalen);
551                 break;
552         default:
553                 res = unreal_queue_indicate(p, ast, condition, data, datalen);
554                 break;
555         }
556
557         ao2_ref(p, -1);
558         return res;
559 }
560
561 int ast_unreal_digit_begin(struct ast_channel *ast, char digit)
562 {
563         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
564         int res = -1;
565         struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
566         int isoutbound;
567
568         if (!p) {
569                 return -1;
570         }
571
572         ao2_ref(p, 1); /* ref for unreal_queue_frame */
573         ao2_lock(p);
574         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
575         f.subclass.integer = digit;
576         res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
577         ao2_unlock(p);
578         ao2_ref(p, -1);
579
580         return res;
581 }
582
583 int ast_unreal_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
584 {
585         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
586         int res = -1;
587         struct ast_frame f = { AST_FRAME_DTMF_END, };
588         int isoutbound;
589
590         if (!p) {
591                 return -1;
592         }
593
594         ao2_ref(p, 1); /* ref for unreal_queue_frame */
595         ao2_lock(p);
596         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
597         f.subclass.integer = digit;
598         f.len = duration;
599         res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
600         ao2_unlock(p);
601         ao2_ref(p, -1);
602
603         return res;
604 }
605
606 int ast_unreal_sendtext(struct ast_channel *ast, const char *text)
607 {
608         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
609         int res = -1;
610         struct ast_frame f = { AST_FRAME_TEXT, };
611         int isoutbound;
612
613         if (!p) {
614                 return -1;
615         }
616
617         ao2_ref(p, 1); /* ref for unreal_queue_frame */
618         ao2_lock(p);
619         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
620         f.data.ptr = (char *) text;
621         f.datalen = strlen(text) + 1;
622         res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
623         ao2_unlock(p);
624         ao2_ref(p, -1);
625         return res;
626 }
627
628 int ast_unreal_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
629 {
630         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
631         int res = -1;
632         struct ast_frame f = { AST_FRAME_HTML, };
633         int isoutbound;
634
635         if (!p) {
636                 return -1;
637         }
638
639         ao2_ref(p, 1); /* ref for unreal_queue_frame */
640         ao2_lock(p);
641         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
642         f.subclass.integer = subclass;
643         f.data.ptr = (char *)data;
644         f.datalen = datalen;
645         res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
646         ao2_unlock(p);
647         ao2_ref(p, -1);
648
649         return res;
650 }
651
652 void ast_unreal_call_setup(struct ast_channel *semi1, struct ast_channel *semi2)
653 {
654         struct ast_var_t *varptr;
655         struct ast_var_t *clone_var;
656
657         ast_channel_stage_snapshot(semi2);
658
659         /*
660          * Note that cid_num and cid_name aren't passed in the
661          * ast_channel_alloc calls in ast_unreal_new_channels().  It's
662          * done here instead.
663          */
664         ast_party_redirecting_copy(ast_channel_redirecting(semi2), ast_channel_redirecting(semi1));
665
666         ast_party_dialed_copy(ast_channel_dialed(semi2), ast_channel_dialed(semi1));
667
668         /* Crossover the CallerID and conected-line to cross the unreal bridge. */
669         ast_connected_line_copy_to_caller(ast_channel_caller(semi2), ast_channel_connected(semi1));
670         ast_connected_line_copy_from_caller(ast_channel_connected(semi2), ast_channel_caller(semi1));
671
672         ast_channel_language_set(semi2, ast_channel_language(semi1));
673
674         /* Crossover the accountcode and peeraccount to cross the unreal bridge. */
675         ast_channel_accountcode_set(semi2, ast_channel_peeraccount(semi1));
676         ast_channel_peeraccount_set(semi2, ast_channel_accountcode(semi1));
677
678         ast_channel_musicclass_set(semi2, ast_channel_musicclass(semi1));
679
680         ast_channel_cc_params_init(semi2, ast_channel_get_cc_config_params(semi1));
681
682         /*
683          * Make sure we inherit the AST_CAUSE_ANSWERED_ELSEWHERE if it's
684          * set on the queue/dial call request in the dialplan.
685          */
686         if (ast_channel_hangupcause(semi1) == AST_CAUSE_ANSWERED_ELSEWHERE) {
687                 ast_channel_hangupcause_set(semi2, AST_CAUSE_ANSWERED_ELSEWHERE);
688         }
689
690         /*
691          * Copy the channel variables from the semi1 channel to the
692          * outgoing channel.
693          *
694          * Note that due to certain assumptions, they MUST be in the
695          * same order.
696          */
697         AST_LIST_TRAVERSE(ast_channel_varshead(semi1), varptr, entries) {
698                 clone_var = ast_var_assign(varptr->name, varptr->value);
699                 if (clone_var) {
700                         AST_LIST_INSERT_TAIL(ast_channel_varshead(semi2), clone_var, entries);
701                         ast_channel_publish_varset(semi2, ast_var_full_name(clone_var),
702                                 ast_var_value(clone_var));
703                 }
704         }
705         ast_channel_datastore_inherit(semi1, semi2);
706
707         ast_channel_stage_snapshot_done(semi2);
708 }
709
710 int ast_unreal_channel_push_to_bridge(struct ast_channel *ast, struct ast_bridge *bridge, unsigned int flags)
711 {
712         struct ast_bridge_features *features;
713         struct ast_channel *chan;
714         struct ast_channel *owner;
715         ast_callid bridge_callid;
716         RAII_VAR(struct ast_unreal_pvt *, p, NULL, ao2_cleanup);
717
718         ast_bridge_lock(bridge);
719         bridge_callid = bridge->callid;
720         ast_bridge_unlock(bridge);
721
722         {
723                 SCOPED_CHANNELLOCK(lock, ast);
724                 p = ast_channel_tech_pvt(ast);
725                 if (!p) {
726                         return -1;
727                 }
728                 ao2_ref(p, +1);
729         }
730
731         {
732                 SCOPED_AO2LOCK(lock, p);
733                 chan = p->chan;
734                 if (!chan) {
735                         return -1;
736                 }
737
738                 owner = p->owner;
739                 if (!owner) {
740                         return -1;
741                 }
742
743                 ast_channel_ref(chan);
744                 ast_channel_ref(owner);
745         }
746
747         if (bridge_callid) {
748                 ast_callid chan_callid;
749                 ast_callid owner_callid;
750
751                 /* chan side call ID setting */
752                 ast_channel_lock(chan);
753
754                 chan_callid = ast_channel_callid(chan);
755                 if (!chan_callid) {
756                         ast_channel_callid_set(chan, bridge_callid);
757                 }
758                 ast_channel_unlock(chan);
759
760                 /* owner side call ID setting */
761                 ast_channel_lock(owner);
762
763                 owner_callid = ast_channel_callid(owner);
764                 if (!owner_callid) {
765                         ast_channel_callid_set(owner, bridge_callid);
766                 }
767
768                 ast_channel_unlock(owner);
769         }
770
771         /* We are done with the owner now that its call ID matches the bridge */
772         ast_channel_unref(owner);
773         owner = NULL;
774
775         features = ast_bridge_features_new();
776         if (!features) {
777                 ast_channel_unref(chan);
778                 return -1;
779         }
780
781         ast_set_flag(&features->feature_flags, flags);
782
783         /* Impart the semi2 channel into the bridge */
784         if (ast_bridge_impart(bridge, chan, NULL, features,
785                 AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
786                 ast_bridge_features_destroy(features);
787                 ast_channel_unref(chan);
788                 return -1;
789         }
790
791         ao2_lock(p);
792         ast_set_flag(p, AST_UNREAL_CARETAKER_THREAD);
793         ao2_unlock(p);
794         ast_channel_unref(chan);
795
796         return 0;
797 }
798
799 int ast_unreal_hangup(struct ast_unreal_pvt *p, struct ast_channel *ast)
800 {
801         int hangup_chan = 0;
802         int res = 0;
803         int cause;
804         struct ast_channel *owner = NULL;
805         struct ast_channel *chan = NULL;
806
807         /* the pvt isn't going anywhere, it has a ref */
808         ast_channel_unlock(ast);
809
810         /* lock everything */
811         ast_unreal_lock_all(p, &chan, &owner);
812
813         if (ast != chan && ast != owner) {
814                 res = -1;
815                 goto unreal_hangup_cleanup;
816         }
817
818         cause = ast_channel_hangupcause(ast);
819
820         if (ast == p->chan) {
821                 /* Outgoing side is hanging up. */
822                 ast_clear_flag(p, AST_UNREAL_CARETAKER_THREAD);
823                 p->chan = NULL;
824                 if (p->owner) {
825                         const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
826
827                         if (status) {
828                                 ast_channel_hangupcause_set(p->owner, cause);
829                                 pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
830                         }
831                         ast_queue_hangup_with_cause(p->owner, cause);
832                 }
833         } else {
834                 /* Owner side is hanging up. */
835                 p->owner = NULL;
836                 if (p->chan) {
837                         if (cause == AST_CAUSE_ANSWERED_ELSEWHERE) {
838                                 ast_channel_hangupcause_set(p->chan, AST_CAUSE_ANSWERED_ELSEWHERE);
839                                 ast_debug(2, "%s has AST_CAUSE_ANSWERED_ELSEWHERE set.\n",
840                                         ast_channel_name(p->chan));
841                         }
842                         if (!ast_test_flag(p, AST_UNREAL_CARETAKER_THREAD)) {
843                                 /*
844                                  * Need to actually hangup p->chan since nothing else is taking
845                                  * care of it.
846                                  */
847                                 hangup_chan = 1;
848                         } else {
849                                 ast_queue_hangup_with_cause(p->chan, cause);
850                         }
851                 }
852         }
853
854         /* this is one of our locked channels, doesn't matter which */
855         ast_channel_tech_pvt_set(ast, NULL);
856         ao2_ref(p, -1);
857
858 unreal_hangup_cleanup:
859         ao2_unlock(p);
860         if (owner) {
861                 ast_channel_unlock(owner);
862                 ast_channel_unref(owner);
863         }
864         if (chan) {
865                 ast_channel_unlock(chan);
866                 if (hangup_chan) {
867                         ast_hangup(chan);
868                 }
869                 ast_channel_unref(chan);
870         }
871
872         /* leave with the channel locked that came in */
873         ast_channel_lock(ast);
874
875         return res;
876 }
877
878 void ast_unreal_destructor(void *vdoomed)
879 {
880         struct ast_unreal_pvt *doomed = vdoomed;
881
882         ao2_cleanup(doomed->reqcap);
883         doomed->reqcap = NULL;
884 }
885
886 struct ast_unreal_pvt *ast_unreal_alloc(size_t size, ao2_destructor_fn destructor, struct ast_format_cap *cap)
887 {
888         struct ast_unreal_pvt *unreal;
889
890         static const struct ast_jb_conf jb_conf = {
891                 .flags = 0,
892                 .max_size = -1,
893                 .resync_threshold = -1,
894                 .impl = "",
895                 .target_extra = -1,
896         };
897
898         unreal = ao2_alloc(size, destructor);
899         if (!unreal) {
900                 return NULL;
901         }
902
903         unreal->reqcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
904         if (!unreal->reqcap) {
905                 ao2_ref(unreal, -1);
906                 return NULL;
907         }
908         ast_format_cap_append_from_cap(unreal->reqcap, cap, AST_MEDIA_TYPE_UNKNOWN);
909
910         memcpy(&unreal->jb_conf, &jb_conf, sizeof(unreal->jb_conf));
911
912         return unreal;
913 }
914
915 struct ast_channel *ast_unreal_new_channels(struct ast_unreal_pvt *p,
916         const struct ast_channel_tech *tech, int semi1_state, int semi2_state,
917         const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
918         const struct ast_channel *requestor, ast_callid callid)
919 {
920         struct ast_channel *owner;
921         struct ast_channel *chan;
922         RAII_VAR(struct ast_format *, fmt, NULL, ao2_cleanup);
923         struct ast_assigned_ids id1 = {NULL, NULL};
924         struct ast_assigned_ids id2 = {NULL, NULL};
925         int generated_seqno = ast_atomic_fetchadd_int((int *) &name_sequence, +1);
926
927         /* set unique ids for the two channels */
928         if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
929                 id1.uniqueid = assignedids->uniqueid;
930                 id2.uniqueid = assignedids->uniqueid2;
931         }
932
933         /* if id1 given but not id2, use default of id1;2 */
934         if (id1.uniqueid && ast_strlen_zero(id2.uniqueid)) {
935                 char *uniqueid2;
936
937                 uniqueid2 = ast_alloca(strlen(id1.uniqueid) + 3);
938                 strcpy(uniqueid2, id1.uniqueid);/* Safe */
939                 strcat(uniqueid2, ";2");/* Safe */
940                 id2.uniqueid = uniqueid2;
941         }
942
943         /*
944          * Allocate two new Asterisk channels
945          *
946          * Make sure that the ;2 channel gets the same linkedid as ;1.
947          * You can't pass linkedid to both allocations since if linkedid
948          * isn't set, then each channel will generate its own linkedid.
949          */
950         owner = ast_channel_alloc(1, semi1_state, NULL, NULL, NULL,
951                 exten, context, &id1, requestor, 0,
952                 "%s/%s-%08x;1", tech->type, p->name, (unsigned)generated_seqno);
953         if (!owner) {
954                 ast_log(LOG_WARNING, "Unable to allocate owner channel structure\n");
955                 return NULL;
956         }
957
958         if (callid) {
959                 ast_channel_callid_set(owner, callid);
960         }
961
962         ast_channel_tech_set(owner, tech);
963         ao2_ref(p, +1);
964         ast_channel_tech_pvt_set(owner, p);
965
966         ast_channel_nativeformats_set(owner, p->reqcap);
967
968         /* Determine our read/write format and set it on each channel */
969         fmt = ast_format_cap_get_format(p->reqcap, 0);
970         if (!fmt) {
971                 ast_channel_tech_pvt_set(owner, NULL);
972                 ao2_ref(p, -1);
973                 ast_channel_unlock(owner);
974                 ast_channel_release(owner);
975                 return NULL;
976         }
977
978         ast_channel_set_writeformat(owner, fmt);
979         ast_channel_set_rawwriteformat(owner, fmt);
980         ast_channel_set_readformat(owner, fmt);
981         ast_channel_set_rawreadformat(owner, fmt);
982
983         ast_set_flag(ast_channel_flags(owner), AST_FLAG_DISABLE_DEVSTATE_CACHE);
984
985         ast_jb_configure(owner, &p->jb_conf);
986
987         if (ast_channel_cc_params_init(owner, requestor
988                 ? ast_channel_get_cc_config_params((struct ast_channel *) requestor) : NULL)) {
989                 ast_channel_tech_pvt_set(owner, NULL);
990                 ao2_ref(p, -1);
991                 ast_channel_tech_pvt_set(owner, NULL);
992                 ast_channel_unlock(owner);
993                 ast_channel_release(owner);
994                 return NULL;
995         }
996
997         p->owner = owner;
998         ast_channel_unlock(owner);
999
1000         chan = ast_channel_alloc(1, semi2_state, NULL, NULL, NULL,
1001                 exten, context, &id2, owner, 0,
1002                 "%s/%s-%08x;2", tech->type, p->name, (unsigned)generated_seqno);
1003         if (!chan) {
1004                 ast_log(LOG_WARNING, "Unable to allocate chan channel structure\n");
1005                 ast_channel_tech_pvt_set(owner, NULL);
1006                 ao2_ref(p, -1);
1007                 ast_channel_tech_pvt_set(owner, NULL);
1008                 ast_channel_release(owner);
1009                 return NULL;
1010         }
1011
1012         if (callid) {
1013                 ast_channel_callid_set(chan, callid);
1014         }
1015
1016         ast_channel_tech_set(chan, tech);
1017         ao2_ref(p, +1);
1018         ast_channel_tech_pvt_set(chan, p);
1019
1020         ast_channel_nativeformats_set(chan, p->reqcap);
1021
1022         /* Format was already determined when setting up owner */
1023         ast_channel_set_writeformat(chan, fmt);
1024         ast_channel_set_rawwriteformat(chan, fmt);
1025         ast_channel_set_readformat(chan, fmt);
1026         ast_channel_set_rawreadformat(chan, fmt);
1027
1028         ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
1029
1030         p->chan = chan;
1031         ast_channel_unlock(chan);
1032
1033         return owner;
1034 }