f26642429b1f98259648a710fd66df6d669b8713
[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_FILE_VERSION(__FILE__, "$Revision$")
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         /*
413          * Block -1 stop tones events if we are to be optimized out.  We
414          * don't need a flurry of these events on an unreal channel chain
415          * when initially connected to slow the optimization process.
416          */
417         if (0 <= condition || ast_test_flag(p, AST_UNREAL_NO_OPTIMIZATION)) {
418                 struct ast_frame f = {
419                         .frametype = AST_FRAME_CONTROL,
420                         .subclass.integer = condition,
421                         .data.ptr = (void *) data,
422                         .datalen = datalen,
423                 };
424
425                 isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
426                 res = unreal_queue_frame(p, isoutbound, &f, ast, 1);
427                 if (!res
428                         && condition == AST_CONTROL_T38_PARAMETERS
429                         && datalen == sizeof(struct ast_control_t38_parameters)) {
430                         const struct ast_control_t38_parameters *parameters = data;
431
432                         if (parameters->request_response == AST_T38_REQUEST_PARMS) {
433                                 res = AST_T38_REQUEST_PARMS;
434                         }
435                 }
436         } else {
437                 ast_debug(4, "Blocked indication %d\n", condition);
438         }
439         ao2_unlock(p);
440
441         return res;
442 }
443
444 /*!
445  * \internal
446  * \brief Handle COLP and redirecting conditions.
447  * \since 12.0.0
448  *
449  * \param p Unreal private structure.
450  * \param ast Channel indicating the condition.
451  * \param condition What is being indicated.
452  *
453  * \retval 0 on success.
454  * \retval -1 on error.
455  */
456 static int unreal_colp_redirect_indicate(struct ast_unreal_pvt *p, struct ast_channel *ast, int condition)
457 {
458         struct ast_channel *my_chan;
459         struct ast_channel *my_owner;
460         struct ast_channel *this_channel;
461         struct ast_channel *the_other_channel;
462         int isoutbound;
463         int res = 0;
464         unsigned char frame_data[1024];
465         struct ast_frame f = {
466                 .frametype = AST_FRAME_CONTROL,
467                 .subclass.integer = condition,
468                 .data.ptr = frame_data,
469         };
470
471         /*
472          * A connected line update frame may only contain a partial
473          * amount of data, such as just a source, or just a ton, and not
474          * the full amount of information.  However, the collected
475          * information is all stored in the outgoing channel's
476          * connectedline structure, so when receiving a connected line
477          * update on an outgoing unreal channel, we need to transmit the
478          * collected connected line information instead of whatever
479          * happens to be in this control frame.  The same applies for
480          * redirecting information, which is why it is handled here as
481          * well.
482          */
483         ast_channel_unlock(ast);
484         ast_unreal_lock_all(p, &my_chan, &my_owner);
485         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
486         if (isoutbound) {
487                 this_channel = p->chan;
488                 the_other_channel = p->owner;
489         } else {
490                 this_channel = p->owner;
491                 the_other_channel = p->chan;
492         }
493         if (the_other_channel) {
494                 if (condition == AST_CONTROL_CONNECTED_LINE) {
495                         ast_connected_line_copy_to_caller(ast_channel_caller(the_other_channel),
496                                 ast_channel_connected(this_channel));
497                         f.datalen = ast_connected_line_build_data(frame_data, sizeof(frame_data),
498                                 ast_channel_connected(this_channel), NULL);
499                 } else {
500                         f.datalen = ast_redirecting_build_data(frame_data, sizeof(frame_data),
501                                 ast_channel_redirecting(this_channel), NULL);
502                 }
503         }
504         if (my_chan) {
505                 ast_channel_unlock(my_chan);
506                 ast_channel_unref(my_chan);
507         }
508         if (my_owner) {
509                 ast_channel_unlock(my_owner);
510                 ast_channel_unref(my_owner);
511         }
512         if (the_other_channel) {
513                 res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
514         }
515         ao2_unlock(p);
516         ast_channel_lock(ast);
517
518         return res;
519 }
520
521 int ast_unreal_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
522 {
523         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
524         int res = 0;
525
526         if (!p) {
527                 return -1;
528         }
529
530         ao2_ref(p, 1); /* ref for unreal_queue_frame */
531
532         switch (condition) {
533         case AST_CONTROL_MASQUERADE_NOTIFY:
534                 /*
535                  * Always block this because this is the channel being
536                  * masqueraded; not anything down the chain.
537                  */
538                 break;
539         case AST_CONTROL_CONNECTED_LINE:
540         case AST_CONTROL_REDIRECTING:
541                 res = unreal_colp_redirect_indicate(p, ast, condition);
542                 break;
543         case AST_CONTROL_HOLD:
544                 if (ast_test_flag(p, AST_UNREAL_MOH_INTERCEPT)) {
545                         ast_moh_start(ast, data, NULL);
546                         break;
547                 }
548                 res = unreal_queue_indicate(p, ast, condition, data, datalen);
549                 break;
550         case AST_CONTROL_UNHOLD:
551                 if (ast_test_flag(p, AST_UNREAL_MOH_INTERCEPT)) {
552                         ast_moh_stop(ast);
553                         break;
554                 }
555                 res = unreal_queue_indicate(p, ast, condition, data, datalen);
556                 break;
557         default:
558                 res = unreal_queue_indicate(p, ast, condition, data, datalen);
559                 break;
560         }
561
562         ao2_ref(p, -1);
563         return res;
564 }
565
566 int ast_unreal_digit_begin(struct ast_channel *ast, char digit)
567 {
568         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
569         int res = -1;
570         struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
571         int isoutbound;
572
573         if (!p) {
574                 return -1;
575         }
576
577         ao2_ref(p, 1); /* ref for unreal_queue_frame */
578         ao2_lock(p);
579         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
580         f.subclass.integer = digit;
581         res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
582         ao2_unlock(p);
583         ao2_ref(p, -1);
584
585         return res;
586 }
587
588 int ast_unreal_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
589 {
590         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
591         int res = -1;
592         struct ast_frame f = { AST_FRAME_DTMF_END, };
593         int isoutbound;
594
595         if (!p) {
596                 return -1;
597         }
598
599         ao2_ref(p, 1); /* ref for unreal_queue_frame */
600         ao2_lock(p);
601         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
602         f.subclass.integer = digit;
603         f.len = duration;
604         res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
605         ao2_unlock(p);
606         ao2_ref(p, -1);
607
608         return res;
609 }
610
611 int ast_unreal_sendtext(struct ast_channel *ast, const char *text)
612 {
613         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
614         int res = -1;
615         struct ast_frame f = { AST_FRAME_TEXT, };
616         int isoutbound;
617
618         if (!p) {
619                 return -1;
620         }
621
622         ao2_ref(p, 1); /* ref for unreal_queue_frame */
623         ao2_lock(p);
624         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
625         f.data.ptr = (char *) text;
626         f.datalen = strlen(text) + 1;
627         res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
628         ao2_unlock(p);
629         ao2_ref(p, -1);
630         return res;
631 }
632
633 int ast_unreal_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
634 {
635         struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
636         int res = -1;
637         struct ast_frame f = { AST_FRAME_HTML, };
638         int isoutbound;
639
640         if (!p) {
641                 return -1;
642         }
643
644         ao2_ref(p, 1); /* ref for unreal_queue_frame */
645         ao2_lock(p);
646         isoutbound = AST_UNREAL_IS_OUTBOUND(ast, p);
647         f.subclass.integer = subclass;
648         f.data.ptr = (char *)data;
649         f.datalen = datalen;
650         res = unreal_queue_frame(p, isoutbound, &f, ast, 0);
651         ao2_unlock(p);
652         ao2_ref(p, -1);
653
654         return res;
655 }
656
657 void ast_unreal_call_setup(struct ast_channel *semi1, struct ast_channel *semi2)
658 {
659         struct ast_var_t *varptr;
660         struct ast_var_t *clone_var;
661
662         ast_channel_stage_snapshot(semi2);
663
664         /*
665          * Note that cid_num and cid_name aren't passed in the
666          * ast_channel_alloc calls in ast_unreal_new_channels().  It's
667          * done here instead.
668          */
669         ast_party_redirecting_copy(ast_channel_redirecting(semi2), ast_channel_redirecting(semi1));
670
671         ast_party_dialed_copy(ast_channel_dialed(semi2), ast_channel_dialed(semi1));
672
673         /* Crossover the CallerID and conected-line to cross the unreal bridge. */
674         ast_connected_line_copy_to_caller(ast_channel_caller(semi2), ast_channel_connected(semi1));
675         ast_connected_line_copy_from_caller(ast_channel_connected(semi2), ast_channel_caller(semi1));
676
677         ast_channel_language_set(semi2, ast_channel_language(semi1));
678
679         /* Crossover the accountcode and peeraccount to cross the unreal bridge. */
680         ast_channel_accountcode_set(semi2, ast_channel_peeraccount(semi1));
681         ast_channel_peeraccount_set(semi2, ast_channel_accountcode(semi1));
682
683         ast_channel_musicclass_set(semi2, ast_channel_musicclass(semi1));
684
685         ast_channel_cc_params_init(semi2, ast_channel_get_cc_config_params(semi1));
686
687         /*
688          * Make sure we inherit the AST_CAUSE_ANSWERED_ELSEWHERE if it's
689          * set on the queue/dial call request in the dialplan.
690          */
691         if (ast_channel_hangupcause(semi1) == AST_CAUSE_ANSWERED_ELSEWHERE) {
692                 ast_channel_hangupcause_set(semi2, AST_CAUSE_ANSWERED_ELSEWHERE);
693         }
694
695         /*
696          * Copy the channel variables from the semi1 channel to the
697          * outgoing channel.
698          *
699          * Note that due to certain assumptions, they MUST be in the
700          * same order.
701          */
702         AST_LIST_TRAVERSE(ast_channel_varshead(semi1), varptr, entries) {
703                 clone_var = ast_var_assign(varptr->name, varptr->value);
704                 if (clone_var) {
705                         AST_LIST_INSERT_TAIL(ast_channel_varshead(semi2), clone_var, entries);
706                         ast_channel_publish_varset(semi2, ast_var_full_name(clone_var),
707                                 ast_var_value(clone_var));
708                 }
709         }
710         ast_channel_datastore_inherit(semi1, semi2);
711
712         ast_channel_stage_snapshot_done(semi2);
713 }
714
715 int ast_unreal_channel_push_to_bridge(struct ast_channel *ast, struct ast_bridge *bridge, unsigned int flags)
716 {
717         struct ast_bridge_features *features;
718         struct ast_channel *chan;
719         struct ast_channel *owner;
720         ast_callid bridge_callid;
721         RAII_VAR(struct ast_unreal_pvt *, p, NULL, ao2_cleanup);
722
723         ast_bridge_lock(bridge);
724         bridge_callid = bridge->callid;
725         ast_bridge_unlock(bridge);
726
727         {
728                 SCOPED_CHANNELLOCK(lock, ast);
729                 p = ast_channel_tech_pvt(ast);
730                 if (!p) {
731                         return -1;
732                 }
733                 ao2_ref(p, +1);
734         }
735
736         {
737                 SCOPED_AO2LOCK(lock, p);
738                 chan = p->chan;
739                 if (!chan) {
740                         return -1;
741                 }
742
743                 owner = p->owner;
744                 if (!owner) {
745                         return -1;
746                 }
747
748                 ast_channel_ref(chan);
749                 ast_channel_ref(owner);
750         }
751
752         if (bridge_callid) {
753                 ast_callid chan_callid;
754                 ast_callid owner_callid;
755
756                 /* chan side call ID setting */
757                 ast_channel_lock(chan);
758
759                 chan_callid = ast_channel_callid(chan);
760                 if (!chan_callid) {
761                         ast_channel_callid_set(chan, bridge_callid);
762                 }
763                 ast_channel_unlock(chan);
764
765                 /* owner side call ID setting */
766                 ast_channel_lock(owner);
767
768                 owner_callid = ast_channel_callid(owner);
769                 if (!owner_callid) {
770                         ast_channel_callid_set(owner, bridge_callid);
771                 }
772
773                 ast_channel_unlock(owner);
774         }
775
776         /* We are done with the owner now that its call ID matches the bridge */
777         ast_channel_unref(owner);
778         owner = NULL;
779
780         features = ast_bridge_features_new();
781         if (!features) {
782                 ast_channel_unref(chan);
783                 return -1;
784         }
785
786         ast_set_flag(&features->feature_flags, flags);
787
788         /* Impart the semi2 channel into the bridge */
789         if (ast_bridge_impart(bridge, chan, NULL, features,
790                 AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
791                 ast_bridge_features_destroy(features);
792                 ast_channel_unref(chan);
793                 return -1;
794         }
795
796         ao2_lock(p);
797         ast_set_flag(p, AST_UNREAL_CARETAKER_THREAD);
798         ao2_unlock(p);
799         ast_channel_unref(chan);
800
801         return 0;
802 }
803
804 int ast_unreal_hangup(struct ast_unreal_pvt *p, struct ast_channel *ast)
805 {
806         int hangup_chan = 0;
807         int res = 0;
808         int cause;
809         struct ast_channel *owner = NULL;
810         struct ast_channel *chan = NULL;
811
812         /* the pvt isn't going anywhere, it has a ref */
813         ast_channel_unlock(ast);
814
815         /* lock everything */
816         ast_unreal_lock_all(p, &chan, &owner);
817
818         if (ast != chan && ast != owner) {
819                 res = -1;
820                 goto unreal_hangup_cleanup;
821         }
822
823         cause = ast_channel_hangupcause(ast);
824
825         if (ast == p->chan) {
826                 /* Outgoing side is hanging up. */
827                 ast_clear_flag(p, AST_UNREAL_CARETAKER_THREAD);
828                 p->chan = NULL;
829                 if (p->owner) {
830                         const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
831
832                         if (status) {
833                                 ast_channel_hangupcause_set(p->owner, cause);
834                                 pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
835                         }
836                         ast_queue_hangup_with_cause(p->owner, cause);
837                 }
838         } else {
839                 /* Owner side is hanging up. */
840                 p->owner = NULL;
841                 if (p->chan) {
842                         if (cause == AST_CAUSE_ANSWERED_ELSEWHERE) {
843                                 ast_channel_hangupcause_set(p->chan, AST_CAUSE_ANSWERED_ELSEWHERE);
844                                 ast_debug(2, "%s has AST_CAUSE_ANSWERED_ELSEWHERE set.\n",
845                                         ast_channel_name(p->chan));
846                         }
847                         if (!ast_test_flag(p, AST_UNREAL_CARETAKER_THREAD)) {
848                                 /*
849                                  * Need to actually hangup p->chan since nothing else is taking
850                                  * care of it.
851                                  */
852                                 hangup_chan = 1;
853                         } else {
854                                 ast_queue_hangup_with_cause(p->chan, cause);
855                         }
856                 }
857         }
858
859         /* this is one of our locked channels, doesn't matter which */
860         ast_channel_tech_pvt_set(ast, NULL);
861         ao2_ref(p, -1);
862
863 unreal_hangup_cleanup:
864         ao2_unlock(p);
865         if (owner) {
866                 ast_channel_unlock(owner);
867                 ast_channel_unref(owner);
868         }
869         if (chan) {
870                 ast_channel_unlock(chan);
871                 if (hangup_chan) {
872                         ast_hangup(chan);
873                 }
874                 ast_channel_unref(chan);
875         }
876
877         /* leave with the channel locked that came in */
878         ast_channel_lock(ast);
879
880         return res;
881 }
882
883 void ast_unreal_destructor(void *vdoomed)
884 {
885         struct ast_unreal_pvt *doomed = vdoomed;
886
887         ao2_cleanup(doomed->reqcap);
888         doomed->reqcap = NULL;
889 }
890
891 struct ast_unreal_pvt *ast_unreal_alloc(size_t size, ao2_destructor_fn destructor, struct ast_format_cap *cap)
892 {
893         struct ast_unreal_pvt *unreal;
894
895         static const struct ast_jb_conf jb_conf = {
896                 .flags = 0,
897                 .max_size = -1,
898                 .resync_threshold = -1,
899                 .impl = "",
900                 .target_extra = -1,
901         };
902
903         unreal = ao2_alloc(size, destructor);
904         if (!unreal) {
905                 return NULL;
906         }
907
908         unreal->reqcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
909         if (!unreal->reqcap) {
910                 ao2_ref(unreal, -1);
911                 return NULL;
912         }
913         ast_format_cap_append_from_cap(unreal->reqcap, cap, AST_MEDIA_TYPE_UNKNOWN);
914
915         memcpy(&unreal->jb_conf, &jb_conf, sizeof(unreal->jb_conf));
916
917         return unreal;
918 }
919
920 struct ast_channel *ast_unreal_new_channels(struct ast_unreal_pvt *p,
921         const struct ast_channel_tech *tech, int semi1_state, int semi2_state,
922         const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
923         const struct ast_channel *requestor, ast_callid callid)
924 {
925         struct ast_channel *owner;
926         struct ast_channel *chan;
927         RAII_VAR(struct ast_format *, fmt, NULL, ao2_cleanup);
928         struct ast_assigned_ids id1 = {NULL, NULL};
929         struct ast_assigned_ids id2 = {NULL, NULL};
930         int generated_seqno = ast_atomic_fetchadd_int((int *) &name_sequence, +1);
931
932         /* set unique ids for the two channels */
933         if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
934                 id1.uniqueid = assignedids->uniqueid;
935                 id2.uniqueid = assignedids->uniqueid2;
936         }
937
938         /* if id1 given but not id2, use default of id1;2 */
939         if (id1.uniqueid && ast_strlen_zero(id2.uniqueid)) {
940                 char *uniqueid2;
941
942                 uniqueid2 = ast_alloca(strlen(id1.uniqueid) + 3);
943                 strcpy(uniqueid2, id1.uniqueid);/* Safe */
944                 strcat(uniqueid2, ";2");/* Safe */
945                 id2.uniqueid = uniqueid2;
946         }
947
948         /*
949          * Allocate two new Asterisk channels
950          *
951          * Make sure that the ;2 channel gets the same linkedid as ;1.
952          * You can't pass linkedid to both allocations since if linkedid
953          * isn't set, then each channel will generate its own linkedid.
954          */
955         owner = ast_channel_alloc(1, semi1_state, NULL, NULL, NULL,
956                 exten, context, &id1, requestor, 0,
957                 "%s/%s-%08x;1", tech->type, p->name, (unsigned)generated_seqno);
958         if (!owner) {
959                 ast_log(LOG_WARNING, "Unable to allocate owner channel structure\n");
960                 return NULL;
961         }
962
963         if (callid) {
964                 ast_channel_callid_set(owner, callid);
965         }
966
967         ast_channel_tech_set(owner, tech);
968         ao2_ref(p, +1);
969         ast_channel_tech_pvt_set(owner, p);
970
971         ast_channel_nativeformats_set(owner, p->reqcap);
972
973         /* Determine our read/write format and set it on each channel */
974         fmt = ast_format_cap_get_format(p->reqcap, 0);
975         if (!fmt) {
976                 ast_channel_tech_pvt_set(owner, NULL);
977                 ao2_ref(p, -1);
978                 ast_channel_unlock(owner);
979                 ast_channel_release(owner);
980                 return NULL;
981         }
982
983         ast_channel_set_writeformat(owner, fmt);
984         ast_channel_set_rawwriteformat(owner, fmt);
985         ast_channel_set_readformat(owner, fmt);
986         ast_channel_set_rawreadformat(owner, fmt);
987
988         ast_set_flag(ast_channel_flags(owner), AST_FLAG_DISABLE_DEVSTATE_CACHE);
989
990         ast_jb_configure(owner, &p->jb_conf);
991
992         if (ast_channel_cc_params_init(owner, requestor
993                 ? ast_channel_get_cc_config_params((struct ast_channel *) requestor) : NULL)) {
994                 ast_channel_tech_pvt_set(owner, NULL);
995                 ao2_ref(p, -1);
996                 ast_channel_tech_pvt_set(owner, NULL);
997                 ast_channel_unlock(owner);
998                 ast_channel_release(owner);
999                 return NULL;
1000         }
1001
1002         p->owner = owner;
1003         ast_channel_unlock(owner);
1004
1005         chan = ast_channel_alloc(1, semi2_state, NULL, NULL, NULL,
1006                 exten, context, &id2, owner, 0,
1007                 "%s/%s-%08x;2", tech->type, p->name, (unsigned)generated_seqno);
1008         if (!chan) {
1009                 ast_log(LOG_WARNING, "Unable to allocate chan channel structure\n");
1010                 ast_channel_tech_pvt_set(owner, NULL);
1011                 ao2_ref(p, -1);
1012                 ast_channel_tech_pvt_set(owner, NULL);
1013                 ast_channel_release(owner);
1014                 return NULL;
1015         }
1016
1017         if (callid) {
1018                 ast_channel_callid_set(chan, callid);
1019         }
1020
1021         ast_channel_tech_set(chan, tech);
1022         ao2_ref(p, +1);
1023         ast_channel_tech_pvt_set(chan, p);
1024
1025         ast_channel_nativeformats_set(chan, p->reqcap);
1026
1027         /* Format was already determined when setting up owner */
1028         ast_channel_set_writeformat(chan, fmt);
1029         ast_channel_set_rawwriteformat(chan, fmt);
1030         ast_channel_set_readformat(chan, fmt);
1031         ast_channel_set_rawreadformat(chan, fmt);
1032
1033         ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE);
1034
1035         p->chan = chan;
1036         ast_channel_unlock(chan);
1037
1038         return owner;
1039 }