2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
21 * \author Mark Spencer <markster@digium.com>
23 * \brief Local Proxy Channel
25 * \ingroup channel_drivers
29 <support_level>core</support_level>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <sys/signal.h>
39 #include "asterisk/lock.h"
40 #include "asterisk/causes.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/config.h"
43 #include "asterisk/module.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/sched.h"
46 #include "asterisk/io.h"
47 #include "asterisk/acl.h"
48 #include "asterisk/callerid.h"
49 #include "asterisk/file.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/app.h"
52 #include "asterisk/musiconhold.h"
53 #include "asterisk/manager.h"
54 #include "asterisk/stringfields.h"
55 #include "asterisk/devicestate.h"
56 #include "asterisk/astobj2.h"
59 <manager name="LocalOptimizeAway" language="en_US">
61 Optimize away a local channel when possible.
64 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
65 <parameter name="Channel" required="true">
66 <para>The channel name to optimize away.</para>
70 <para>A local channel created with "/n" will not automatically optimize away.
71 Calling this command on the local channel will clear that flag and allow
72 it to optimize away if it's bridged or when it becomes bridged.</para>
77 static const char tdesc[] = "Local Proxy Channel Driver";
79 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
81 /* right now we are treating the locals astobj2 container as a
82 * list. If there is ever a reason to make this more efficient
83 * increasing the bucket size would help. */
84 static const int BUCKET_SIZE = 1;
86 static struct ao2_container *locals;
88 static struct ast_jb_conf g_jb_conf = {
91 .resync_threshold = -1,
96 static struct ast_channel *local_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
97 static int local_digit_begin(struct ast_channel *ast, char digit);
98 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
99 static int local_call(struct ast_channel *ast, const char *dest, int timeout);
100 static int local_hangup(struct ast_channel *ast);
101 static int local_answer(struct ast_channel *ast);
102 static struct ast_frame *local_read(struct ast_channel *ast);
103 static int local_write(struct ast_channel *ast, struct ast_frame *f);
104 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
105 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
106 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
107 static int local_sendtext(struct ast_channel *ast, const char *text);
108 static int local_devicestate(const char *data);
109 static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
110 static int local_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
111 static int local_setoption(struct ast_channel *chan, int option, void *data, int datalen);
113 /* PBX interface structure for channel registration */
114 static struct ast_channel_tech local_tech = {
116 .description = tdesc,
117 .requester = local_request,
118 .send_digit_begin = local_digit_begin,
119 .send_digit_end = local_digit_end,
121 .hangup = local_hangup,
122 .answer = local_answer,
124 .write = local_write,
125 .write_video = local_write,
126 .exception = local_read,
127 .indicate = local_indicate,
128 .fixup = local_fixup,
129 .send_html = local_sendhtml,
130 .send_text = local_sendtext,
131 .devicestate = local_devicestate,
132 .bridged_channel = local_bridgedchannel,
133 .queryoption = local_queryoption,
134 .setoption = local_setoption,
137 /*! \brief the local pvt structure for all channels
139 The local channel pvt has two ast_chan objects - the "owner" and the "next channel", the outbound channel
141 ast_chan owner -> local_pvt -> ast_chan chan -> yet-another-pvt-depending-on-channel-type
145 unsigned int flags; /*!< Private flags */
146 char context[AST_MAX_CONTEXT]; /*!< Context to call */
147 char exten[AST_MAX_EXTENSION]; /*!< Extension to call */
148 struct ast_format_cap *reqcap; /*!< Requested format capabilities */
149 struct ast_jb_conf jb_conf; /*!< jitterbuffer configuration for this local channel */
150 struct ast_channel *owner; /*!< Master Channel - Bridging happens here */
151 struct ast_channel *chan; /*!< Outbound channel - PBX is run here */
152 struct ast_module_user *u_owner;/*!< reference to keep the module loaded while in use */
153 struct ast_module_user *u_chan; /*!< reference to keep the module loaded while in use */
156 #define LOCAL_ALREADY_MASQED (1 << 0) /*!< Already masqueraded */
157 #define LOCAL_LAUNCHED_PBX (1 << 1) /*!< PBX was launched */
158 #define LOCAL_NO_OPTIMIZATION (1 << 2) /*!< Do not optimize using masquerading */
159 #define LOCAL_BRIDGE (1 << 3) /*!< Report back the "true" channel as being bridged to */
160 #define LOCAL_MOH_PASSTHRU (1 << 4) /*!< Pass through music on hold start/stop frames */
163 * \brief Send a pvt in with no locks held and get all locks
165 * \note NO locks should be held prior to calling this function
166 * \note The pvt must have a ref held before calling this function
167 * \note if outchan or outowner is set != NULL after calling this function
168 * those channels are locked and reffed.
171 static void awesome_locking(struct local_pvt *p, struct ast_channel **outchan, struct ast_channel **outowner)
173 struct ast_channel *chan = NULL;
174 struct ast_channel *owner = NULL;
180 ast_channel_ref(chan);
184 ast_channel_ref(owner);
188 /* if we don't have both channels, then this is very easy */
189 if (!owner || !chan) {
191 ast_channel_lock(owner);
193 ast_channel_lock(chan);
197 /* lock both channels first, then get the pvt lock */
198 ast_channel_lock_both(chan, owner);
202 /* Now that we have all the locks, validate that nothing changed */
203 if (p->owner != owner || p->chan != chan) {
205 ast_channel_unlock(owner);
206 owner = ast_channel_unref(owner);
209 ast_channel_unlock(chan);
210 chan = ast_channel_unref(chan);
218 *outowner = p->owner;
222 /* Called with ast locked */
223 static int local_setoption(struct ast_channel *ast, int option, void * data, int datalen)
226 struct local_pvt *p = NULL;
227 struct ast_channel *otherchan = NULL;
228 ast_chan_write_info_t *write_info;
230 if (option != AST_OPTION_CHANNEL_WRITE) {
236 if (write_info->version != AST_CHAN_WRITE_INFO_T_VERSION) {
237 ast_log(LOG_ERROR, "The chan_write_info_t type has changed, and this channel hasn't been updated!\n");
241 /* get the tech pvt */
242 if (!(p = ast_channel_tech_pvt(ast))) {
246 ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
248 /* get the channel we are supposed to write to */
250 otherchan = (write_info->chan == p->owner) ? p->chan : p->owner;
251 if (!otherchan || otherchan == write_info->chan) {
255 goto setoption_cleanup;
257 ast_channel_ref(otherchan);
259 /* clear the pvt lock before grabbing the channel */
262 ast_channel_lock(otherchan);
263 res = write_info->write_fn(otherchan, write_info->function, write_info->data, write_info->value);
264 ast_channel_unlock(otherchan);
271 ast_channel_unref(otherchan);
273 ast_channel_lock(ast); /* Lock back before we leave */
277 /*! \brief Adds devicestate to local channels */
278 static int local_devicestate(const char *data)
280 char *exten = ast_strdupa(data);
281 char *context = NULL, *opts = NULL;
283 struct local_pvt *lp;
284 struct ao2_iterator it;
286 if (!(context = strchr(exten, '@'))) {
287 ast_log(LOG_WARNING, "Someone used Local/%s somewhere without a @context. This is bad.\n", exten);
288 return AST_DEVICE_INVALID;
293 /* Strip options if they exist */
294 if ((opts = strchr(context, '/')))
297 ast_debug(3, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
299 res = ast_exists_extension(NULL, context, exten, 1, NULL);
301 return AST_DEVICE_INVALID;
303 res = AST_DEVICE_NOT_INUSE;
305 it = ao2_iterator_init(locals, 0);
306 while ((lp = ao2_iterator_next(&it))) {
307 if (!strcmp(exten, lp->exten) && !strcmp(context, lp->context) && lp->owner) {
308 res = AST_DEVICE_INUSE;
314 ao2_iterator_destroy(&it);
319 /*! \brief Return the bridged channel of a Local channel */
320 static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
322 struct local_pvt *p = ast_channel_tech_pvt(bridge);
323 struct ast_channel *bridged = bridge;
326 ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning <none>\n",
327 ast_channel_name(chan), ast_channel_name(bridge));
333 if (ast_test_flag(p, LOCAL_BRIDGE)) {
334 /* Find the opposite channel */
335 bridged = (bridge == p->owner ? p->chan : p->owner);
337 /* Now see if the opposite channel is bridged to anything */
340 } else if (ast_channel_internal_bridged_channel(bridged)) {
341 bridged = ast_channel_internal_bridged_channel(bridged);
350 /* Called with ast locked */
351 static int local_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
354 struct ast_channel *bridged = NULL;
355 struct ast_channel *tmp = NULL;
358 if (option != AST_OPTION_T38_STATE) {
359 /* AST_OPTION_T38_STATE is the only supported option at this time */
363 /* for some reason the channel is not locked in channel.c when this function is called */
364 if (!(p = ast_channel_tech_pvt(ast))) {
369 if (!(tmp = IS_OUTBOUND(ast, p) ? p->owner : p->chan)) {
373 ast_channel_ref(tmp);
375 ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
377 ast_channel_lock(tmp);
378 if (!(bridged = ast_bridged_channel(tmp))) {
380 ast_channel_unlock(tmp);
383 ast_channel_ref(bridged);
384 ast_channel_unlock(tmp);
388 res = ast_channel_queryoption(bridged, option, data, datalen, 0);
389 bridged = ast_channel_unref(bridged);
392 tmp = ast_channel_unref(tmp);
394 ast_channel_lock(ast); /* Lock back before we leave */
399 /*! \brief queue a frame on a to either the p->owner or p->chan
401 * \note the local_pvt MUST have it's ref count bumped before entering this function and
402 * decremented after this function is called. This is a side effect of the deadlock
403 * avoidance that is necessary to lock 2 channels and a tech_pvt. Without a ref counted
404 * local_pvt, it is impossible to guarantee it will not be destroyed by another thread
405 * during deadlock avoidance.
407 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f,
408 struct ast_channel *us, int us_locked)
410 struct ast_channel *other = NULL;
412 /* Recalculate outbound channel */
413 other = isoutbound ? p->owner : p->chan;
419 /* do not queue frame if generator is on both local channels */
420 if (us && ast_channel_generator(us) && ast_channel_generator(other)) {
424 /* grab a ref on the channel before unlocking the pvt,
425 * other can not go away from us now regardless of locking */
426 ast_channel_ref(other);
427 if (us && us_locked) {
428 ast_channel_unlock(us);
432 if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_RINGING) {
433 ast_setstate(other, AST_STATE_RINGING);
435 ast_queue_frame(other, f);
437 other = ast_channel_unref(other);
438 if (us && us_locked) {
439 ast_channel_lock(us);
446 static int local_answer(struct ast_channel *ast)
448 struct local_pvt *p = ast_channel_tech_pvt(ast);
458 isoutbound = IS_OUTBOUND(ast, p);
460 /* Pass along answer since somebody answered us */
461 struct ast_frame answer = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
462 res = local_queue_frame(p, isoutbound, &answer, ast, 1);
464 ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
473 * \note This function assumes that we're only called from the "outbound" local channel side
475 * \note it is assummed p is locked and reffed before entering this function
477 static void check_bridge(struct ast_channel *ast, struct local_pvt *p)
479 struct ast_channel *owner;
480 struct ast_channel *chan;
481 struct ast_channel *bridged_chan;
484 /* Do a few conditional checks early on just to see if this optimization is possible */
485 if (ast_test_flag(p, LOCAL_NO_OPTIMIZATION | LOCAL_ALREADY_MASQED)
486 || !p->chan || !p->owner) {
490 /* Safely get the channel bridged to p->chan */
491 chan = ast_channel_ref(p->chan);
493 ao2_unlock(p); /* don't call bridged channel with the pvt locked */
494 bridged_chan = ast_bridged_channel(chan);
497 chan = ast_channel_unref(chan);
499 /* since we had to unlock p to get the bridged chan, validate our
500 * data once again and verify the bridged channel is what we expect
501 * it to be in order to perform this optimization */
502 if (ast_test_flag(p, LOCAL_NO_OPTIMIZATION | LOCAL_ALREADY_MASQED)
503 || !p->chan || !p->owner
504 || (ast_channel_internal_bridged_channel(p->chan) != bridged_chan)) {
508 /* only do the masquerade if we are being called on the outbound channel,
509 if it has been bridged to another channel and if there are no pending
510 frames on the owner channel (because they would be transferred to the
511 outbound channel during the masquerade)
513 if (!ast_channel_internal_bridged_channel(p->chan) /* Not ast_bridged_channel! Only go one step! */
514 || !AST_LIST_EMPTY(ast_channel_readq(p->owner))
515 || ast != p->chan /* Sanity check (should always be false) */) {
519 /* Masquerade bridged channel into owner */
520 /* Lock everything we need, one by one, and give up if
521 we can't get everything. Remember, we'll get another
522 chance in just a little bit */
523 if (ast_channel_trylock(ast_channel_internal_bridged_channel(p->chan))) {
526 if (ast_check_hangup(ast_channel_internal_bridged_channel(p->chan))
527 || ast_channel_trylock(p->owner)) {
528 ast_channel_unlock(ast_channel_internal_bridged_channel(p->chan));
533 * At this point we have 4 locks:
534 * p, p->chan (same as ast), p->chan->_bridge, p->owner
536 * Flush a voice or video frame on the outbound channel to make
537 * the queue empty faster so we can get optimized out.
539 f = AST_LIST_FIRST(ast_channel_readq(p->chan));
540 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
541 AST_LIST_REMOVE_HEAD(ast_channel_readq(p->chan), frame_list);
543 f = AST_LIST_FIRST(ast_channel_readq(p->chan));
547 || ast_check_hangup(p->owner)
548 || ast_channel_masquerade(p->owner, ast_channel_internal_bridged_channel(p->chan))) {
549 ast_channel_unlock(p->owner);
550 ast_channel_unlock(ast_channel_internal_bridged_channel(p->chan));
554 /* Masquerade got setup. */
555 ast_debug(4, "Masquerading %s <- %s\n",
556 ast_channel_name(p->owner),
557 ast_channel_name(ast_channel_internal_bridged_channel(p->chan)));
558 if (ast_channel_monitor(p->owner)
559 && !ast_channel_monitor(ast_channel_internal_bridged_channel(p->chan))) {
560 struct ast_channel_monitor *tmp;
562 /* If a local channel is being monitored, we don't want a masquerade
563 * to cause the monitor to go away. Since the masquerade swaps the monitors,
564 * pre-swapping the monitors before the masquerade will ensure that the monitor
565 * ends up where it is expected.
567 tmp = ast_channel_monitor(p->owner);
568 ast_channel_monitor_set(p->owner, ast_channel_monitor(ast_channel_internal_bridged_channel(p->chan)));
569 ast_channel_monitor_set(ast_channel_internal_bridged_channel(p->chan), tmp);
571 if (ast_channel_audiohooks(p->chan)) {
572 struct ast_audiohook_list *audiohooks_swapper;
574 audiohooks_swapper = ast_channel_audiohooks(p->chan);
575 ast_channel_audiohooks_set(p->chan, ast_channel_audiohooks(p->owner));
576 ast_channel_audiohooks_set(p->owner, audiohooks_swapper);
579 /* If any Caller ID was set, preserve it after masquerade like above. We must check
580 * to see if Caller ID was set because otherwise we'll mistakingly copy info not
581 * set from the dialplan and will overwrite the real channel Caller ID. The reason
582 * for this whole preswapping action is because the Caller ID is set on the channel
583 * thread (which is the to be masqueraded away local channel) before both local
584 * channels are optimized away.
586 if (ast_channel_caller(p->owner)->id.name.valid || ast_channel_caller(p->owner)->id.number.valid
587 || ast_channel_caller(p->owner)->id.subaddress.valid || ast_channel_caller(p->owner)->ani.name.valid
588 || ast_channel_caller(p->owner)->ani.number.valid || ast_channel_caller(p->owner)->ani.subaddress.valid) {
589 SWAP(*ast_channel_caller(p->owner), *ast_channel_caller(ast_channel_internal_bridged_channel(p->chan)));
591 if (ast_channel_redirecting(p->owner)->from.name.valid || ast_channel_redirecting(p->owner)->from.number.valid
592 || ast_channel_redirecting(p->owner)->from.subaddress.valid || ast_channel_redirecting(p->owner)->to.name.valid
593 || ast_channel_redirecting(p->owner)->to.number.valid || ast_channel_redirecting(p->owner)->to.subaddress.valid) {
594 SWAP(*ast_channel_redirecting(p->owner), *ast_channel_redirecting(ast_channel_internal_bridged_channel(p->chan)));
596 if (ast_channel_dialed(p->owner)->number.str || ast_channel_dialed(p->owner)->subaddress.valid) {
597 SWAP(*ast_channel_dialed(p->owner), *ast_channel_dialed(ast_channel_internal_bridged_channel(p->chan)));
599 ast_app_group_update(p->chan, p->owner);
600 ast_set_flag(p, LOCAL_ALREADY_MASQED);
602 ast_channel_unlock(p->owner);
603 ast_channel_unlock(ast_channel_internal_bridged_channel(p->chan));
605 /* Do the masquerade now. */
606 owner = ast_channel_ref(p->owner);
608 ast_channel_unlock(ast);
609 ast_do_masquerade(owner);
610 ast_channel_unref(owner);
611 ast_channel_lock(ast);
615 static struct ast_frame *local_read(struct ast_channel *ast)
617 return &ast_null_frame;
620 static int local_write(struct ast_channel *ast, struct ast_frame *f)
622 struct local_pvt *p = ast_channel_tech_pvt(ast);
630 /* Just queue for delivery to the other side */
631 ao2_ref(p, 1); /* ref for local_queue_frame */
633 isoutbound = IS_OUTBOUND(ast, p);
636 && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
637 check_bridge(ast, p);
640 if (!ast_test_flag(p, LOCAL_ALREADY_MASQED)) {
641 res = local_queue_frame(p, isoutbound, f, ast, 1);
643 ast_debug(1, "Not posting to '%s' queue since already masqueraded out\n",
644 ast_channel_name(ast));
653 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
655 struct local_pvt *p = ast_channel_tech_pvt(newchan);
663 if ((p->owner != oldchan) && (p->chan != oldchan)) {
664 ast_log(LOG_WARNING, "Old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
668 if (p->owner == oldchan) {
674 /* Do not let a masquerade cause a Local channel to be bridged to itself! */
675 if (!ast_check_hangup(newchan) && ((p->owner && ast_channel_internal_bridged_channel(p->owner) == p->chan) || (p->chan && ast_channel_internal_bridged_channel(p->chan) == p->owner))) {
676 ast_log(LOG_WARNING, "You can not bridge a Local channel to itself!\n");
678 ast_queue_hangup(newchan);
686 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
688 struct local_pvt *p = ast_channel_tech_pvt(ast);
690 struct ast_frame f = { AST_FRAME_CONTROL, };
697 ao2_ref(p, 1); /* ref for local_queue_frame */
699 /* If this is an MOH hold or unhold, do it on the Local channel versus real channel */
700 if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_HOLD) {
701 ast_moh_start(ast, data, NULL);
702 } else if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_UNHOLD) {
704 } else if (condition == AST_CONTROL_CONNECTED_LINE || condition == AST_CONTROL_REDIRECTING) {
705 struct ast_channel *this_channel;
706 struct ast_channel *the_other_channel;
707 /* A connected line update frame may only contain a partial amount of data, such
708 * as just a source, or just a ton, and not the full amount of information. However,
709 * the collected information is all stored in the outgoing channel's connectedline
710 * structure, so when receiving a connected line update on an outgoing local channel,
711 * we need to transmit the collected connected line information instead of whatever
712 * happens to be in this control frame. The same applies for redirecting information, which
713 * is why it is handled here as well.*/
715 isoutbound = IS_OUTBOUND(ast, p);
717 this_channel = p->chan;
718 the_other_channel = p->owner;
720 this_channel = p->owner;
721 the_other_channel = p->chan;
723 if (the_other_channel) {
724 unsigned char frame_data[1024];
725 if (condition == AST_CONTROL_CONNECTED_LINE) {
727 ast_connected_line_copy_to_caller(ast_channel_caller(the_other_channel), ast_channel_connected(this_channel));
729 f.datalen = ast_connected_line_build_data(frame_data, sizeof(frame_data), ast_channel_connected(this_channel), NULL);
731 f.datalen = ast_redirecting_build_data(frame_data, sizeof(frame_data), ast_channel_redirecting(this_channel), NULL);
733 f.subclass.integer = condition;
734 f.data.ptr = frame_data;
735 res = local_queue_frame(p, isoutbound, &f, ast, 1);
739 /* Queue up a frame representing the indication as a control frame */
742 * Block -1 stop tones events if we are to be optimized out. We
743 * don't need a flurry of these events on a local channel chain
744 * when initially connected to slow the optimization process.
746 if (0 <= condition || ast_test_flag(p, LOCAL_NO_OPTIMIZATION)) {
747 isoutbound = IS_OUTBOUND(ast, p);
748 f.subclass.integer = condition;
749 f.data.ptr = (void *) data;
751 res = local_queue_frame(p, isoutbound, &f, ast, 1);
753 ast_debug(4, "Blocked indication %d\n", condition);
762 static int local_digit_begin(struct ast_channel *ast, char digit)
764 struct local_pvt *p = ast_channel_tech_pvt(ast);
766 struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
773 ao2_ref(p, 1); /* ref for local_queue_frame */
775 isoutbound = IS_OUTBOUND(ast, p);
776 f.subclass.integer = digit;
777 res = local_queue_frame(p, isoutbound, &f, ast, 0);
784 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
786 struct local_pvt *p = ast_channel_tech_pvt(ast);
788 struct ast_frame f = { AST_FRAME_DTMF_END, };
795 ao2_ref(p, 1); /* ref for local_queue_frame */
797 isoutbound = IS_OUTBOUND(ast, p);
798 f.subclass.integer = digit;
800 res = local_queue_frame(p, isoutbound, &f, ast, 0);
807 static int local_sendtext(struct ast_channel *ast, const char *text)
809 struct local_pvt *p = ast_channel_tech_pvt(ast);
811 struct ast_frame f = { AST_FRAME_TEXT, };
819 ao2_ref(p, 1); /* ref for local_queue_frame */
820 isoutbound = IS_OUTBOUND(ast, p);
821 f.data.ptr = (char *) text;
822 f.datalen = strlen(text) + 1;
823 res = local_queue_frame(p, isoutbound, &f, ast, 0);
829 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
831 struct local_pvt *p = ast_channel_tech_pvt(ast);
833 struct ast_frame f = { AST_FRAME_HTML, };
841 ao2_ref(p, 1); /* ref for local_queue_frame */
842 isoutbound = IS_OUTBOUND(ast, p);
843 f.subclass.integer = subclass;
844 f.data.ptr = (char *)data;
846 res = local_queue_frame(p, isoutbound, &f, ast, 0);
853 /*! \brief Initiate new call, part of PBX interface
854 * dest is the dial string */
855 static int local_call(struct ast_channel *ast, const char *dest, int timeout)
857 struct local_pvt *p = ast_channel_tech_pvt(ast);
860 struct ast_channel *owner = NULL;
861 struct ast_channel *chan = NULL;
863 struct ast_var_t *varptr;
864 struct ast_var_t *clone_var;
865 char *reduced_dest = ast_strdupa(dest);
874 /* since we are letting go of channel locks that were locked coming into
875 * this function, then we need to give the tech pvt a ref */
877 ast_channel_unlock(ast);
879 awesome_locking(p, &chan, &owner);
887 if (!owner || !chan) {
893 * Note that cid_num and cid_name aren't passed in the ast_channel_alloc
894 * call, so it's done here instead.
896 * All these failure points just return -1. The individual strings will
897 * be cleared when we destroy the channel.
899 ast_party_redirecting_copy(ast_channel_redirecting(chan), ast_channel_redirecting(owner));
901 ast_party_dialed_copy(ast_channel_dialed(chan), ast_channel_dialed(owner));
903 ast_connected_line_copy_to_caller(ast_channel_caller(chan), ast_channel_connected(owner));
904 ast_connected_line_copy_from_caller(ast_channel_connected(chan), ast_channel_caller(owner));
906 ast_channel_language_set(chan, ast_channel_language(owner));
907 ast_channel_accountcode_set(chan, ast_channel_accountcode(owner));
908 ast_channel_musicclass_set(chan, ast_channel_musicclass(owner));
909 ast_cdr_update(chan);
911 ast_channel_cc_params_init(chan, ast_channel_get_cc_config_params(owner));
913 /* Make sure we inherit the AST_CAUSE_ANSWERED_ELSEWHERE if it's set on the queue/dial call request in the dialplan */
914 if (ast_channel_hangupcause(ast) == AST_CAUSE_ANSWERED_ELSEWHERE) {
915 ast_channel_hangupcause_set(chan, AST_CAUSE_ANSWERED_ELSEWHERE);
918 /* copy the channel variables from the incoming channel to the outgoing channel */
919 /* Note that due to certain assumptions, they MUST be in the same order */
920 AST_LIST_TRAVERSE(ast_channel_varshead(owner), varptr, entries) {
921 clone_var = ast_var_assign(varptr->name, varptr->value);
923 AST_LIST_INSERT_TAIL(ast_channel_varshead(chan), clone_var, entries);
926 ast_channel_datastore_inherit(owner, chan);
927 /* If the local channel has /n or /b on the end of it,
928 * we need to lop that off for our argument to setting
929 * up the CC_INTERFACES variable
931 if ((slash = strrchr(reduced_dest, '/'))) {
934 ast_set_cc_interfaces_chanvar(chan, reduced_dest);
936 exten = ast_strdupa(ast_channel_exten(chan));
937 context = ast_strdupa(ast_channel_context(chan));
942 ast_channel_unlock(chan);
944 if (!ast_exists_extension(chan, context, exten, 1,
945 S_COR(ast_channel_caller(owner)->id.number.valid, ast_channel_caller(owner)->id.number.str, NULL))) {
946 ast_log(LOG_NOTICE, "No such extension/context %s@%s while calling Local channel\n", exten, context);
948 chan = ast_channel_unref(chan); /* we already unlocked it, so clear it hear so the cleanup label won't touch it. */
952 manager_event(EVENT_FLAG_CALL, "LocalBridge",
959 "LocalOptimization: %s\r\n",
960 ast_channel_name(p->owner), ast_channel_name(p->chan), ast_channel_uniqueid(p->owner), ast_channel_uniqueid(p->chan),
961 p->context, p->exten,
962 ast_test_flag(p, LOCAL_NO_OPTIMIZATION) ? "Yes" : "No");
965 /* Start switch on sub channel */
966 if (!(res = ast_pbx_start(chan))) {
968 ast_set_flag(p, LOCAL_LAUNCHED_PBX);
971 chan = ast_channel_unref(chan); /* chan is already unlocked, clear it here so the cleanup lable won't touch it. */
981 ast_channel_unlock(chan);
982 chan = ast_channel_unref(chan);
985 /* owner is supposed to be == to ast, if it
986 * is, don't unlock it because ast must exit locked */
989 ast_channel_unlock(owner);
990 ast_channel_lock(ast);
992 owner = ast_channel_unref(owner);
994 /* we have to exit with ast locked */
995 ast_channel_lock(ast);
1001 /*! \brief Hangup a call through the local proxy channel */
1002 static int local_hangup(struct ast_channel *ast)
1004 struct local_pvt *p = ast_channel_tech_pvt(ast);
1006 int hangup_chan = 0;
1008 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_HANGUP }, .data.uint32 = ast_channel_hangupcause(ast) };
1009 struct ast_channel *owner = NULL;
1010 struct ast_channel *chan = NULL;
1016 /* give the pvt a ref since we are unlocking the channel. */
1019 /* the pvt isn't going anywhere, we gave it a ref */
1020 ast_channel_unlock(ast);
1022 /* lock everything */
1023 awesome_locking(p, &chan, &owner);
1025 if (ast != chan && ast != owner) {
1027 goto local_hangup_cleanup;
1030 isoutbound = IS_OUTBOUND(ast, p); /* just comparing pointer of ast */
1032 if (p->chan && ast_channel_hangupcause(ast) == AST_CAUSE_ANSWERED_ELSEWHERE) {
1033 ast_channel_hangupcause_set(p->chan, AST_CAUSE_ANSWERED_ELSEWHERE);
1034 ast_debug(2, "This local call has AST_CAUSE_ANSWERED_ELSEWHERE set.\n");
1038 const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
1039 if ((status) && (p->owner)) {
1040 ast_channel_hangupcause_set(p->owner, ast_channel_hangupcause(p->chan));
1041 pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
1044 ast_clear_flag(p, LOCAL_LAUNCHED_PBX);
1045 ast_module_user_remove(p->u_chan);
1048 ast_module_user_remove(p->u_owner);
1050 ast_queue_hangup(p->chan);
1055 ast_channel_tech_pvt_set(ast, NULL); /* this is one of our locked channels, doesn't matter which */
1057 if (!p->owner && !p->chan) {
1059 /* Remove from list */
1060 ao2_unlink(locals, p);
1064 goto local_hangup_cleanup;
1066 if (p->chan && !ast_test_flag(p, LOCAL_LAUNCHED_PBX)) {
1067 /* Need to actually hangup since there is no PBX */
1070 local_queue_frame(p, isoutbound, &f, NULL, 0);
1073 local_hangup_cleanup:
1079 ast_channel_unlock(chan);
1083 chan = ast_channel_unref(chan);
1086 ast_channel_unlock(owner);
1087 owner = ast_channel_unref(owner);
1090 /* leave with the same stupid channel locked that came in */
1091 ast_channel_lock(ast);
1095 static void local_destroy(void *obj)
1097 struct local_pvt *pvt = obj;
1098 pvt->reqcap = ast_format_cap_destroy(pvt->reqcap);
1101 /*! \brief Create a call structure */
1102 static struct local_pvt *local_alloc(const char *data, struct ast_format_cap *cap)
1104 struct local_pvt *tmp = NULL;
1105 char *c = NULL, *opts = NULL;
1107 if (!(tmp = ao2_alloc(sizeof(*tmp), local_destroy))) {
1110 if (!(tmp->reqcap = ast_format_cap_dup(cap))) {
1115 /* Initialize private structure information */
1116 ast_copy_string(tmp->exten, data, sizeof(tmp->exten));
1118 memcpy(&tmp->jb_conf, &g_jb_conf, sizeof(tmp->jb_conf));
1120 /* Look for options */
1121 if ((opts = strchr(tmp->exten, '/'))) {
1123 if (strchr(opts, 'n'))
1124 ast_set_flag(tmp, LOCAL_NO_OPTIMIZATION);
1125 if (strchr(opts, 'j')) {
1126 if (ast_test_flag(tmp, LOCAL_NO_OPTIMIZATION))
1127 ast_set_flag(&tmp->jb_conf, AST_JB_ENABLED);
1129 ast_log(LOG_ERROR, "You must use the 'n' option for chan_local "
1130 "to use the 'j' option to enable the jitterbuffer\n");
1133 if (strchr(opts, 'b')) {
1134 ast_set_flag(tmp, LOCAL_BRIDGE);
1136 if (strchr(opts, 'm')) {
1137 ast_set_flag(tmp, LOCAL_MOH_PASSTHRU);
1141 /* Look for a context */
1142 if ((c = strchr(tmp->exten, '@')))
1145 ast_copy_string(tmp->context, c ? c : "default", sizeof(tmp->context));
1147 /* We can't do this check here, because we don't know the CallerID yet, and
1148 * the CallerID could potentially affect what step is actually taken (or
1149 * even if that step exists). */
1150 if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
1151 ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
1152 tmp = local_pvt_destroy(tmp);
1156 ao2_link(locals, tmp);
1160 return tmp; /* this is returned with a ref */
1163 /*! \brief Start new local channel */
1164 static struct ast_channel *local_new(struct local_pvt *p, int state, const char *linkedid)
1166 struct ast_channel *tmp = NULL, *tmp2 = NULL;
1167 int randnum = ast_random() & 0xffff;
1168 struct ast_format fmt;
1172 /* Allocate two new Asterisk channels */
1173 /* safe accountcode */
1174 if (p->owner && ast_channel_accountcode(p->owner))
1175 t = ast_channel_accountcode(p->owner);
1180 ama = ast_channel_amaflags(p->owner);
1184 /* Make sure that the ;2 channel gets the same linkedid as ;1. You can't pass linkedid to both
1185 * allocations since if linkedid isn't set, then each channel will generate its own linkedid. */
1186 if (!(tmp = ast_channel_alloc(1, state, 0, 0, t, p->exten, p->context, linkedid, ama, "Local/%s@%s-%04x;1", p->exten, p->context, randnum))
1187 || !(tmp2 = ast_channel_alloc(1, AST_STATE_RING, 0, 0, t, p->exten, p->context, ast_channel_linkedid(tmp), ama, "Local/%s@%s-%04x;2", p->exten, p->context, randnum))) {
1189 tmp = ast_channel_release(tmp);
1191 ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n");
1195 ast_channel_tech_set(tmp, &local_tech);
1196 ast_channel_tech_set(tmp2, &local_tech);
1198 ast_format_cap_copy(ast_channel_nativeformats(tmp), p->reqcap);
1199 ast_format_cap_copy(ast_channel_nativeformats(tmp2), p->reqcap);
1201 /* Determine our read/write format and set it on each channel */
1202 ast_best_codec(p->reqcap, &fmt);
1203 ast_format_copy(ast_channel_writeformat(tmp), &fmt);
1204 ast_format_copy(ast_channel_writeformat(tmp2), &fmt);
1205 ast_format_copy(ast_channel_rawwriteformat(tmp), &fmt);
1206 ast_format_copy(ast_channel_rawwriteformat(tmp2), &fmt);
1207 ast_format_copy(ast_channel_readformat(tmp), &fmt);
1208 ast_format_copy(ast_channel_readformat(tmp2), &fmt);
1209 ast_format_copy(ast_channel_rawreadformat(tmp), &fmt);
1210 ast_format_copy(ast_channel_rawreadformat(tmp2), &fmt);
1212 ast_channel_tech_pvt_set(tmp, p);
1213 ast_channel_tech_pvt_set(tmp2, p);
1217 p->u_owner = ast_module_user_add(p->owner);
1218 p->u_chan = ast_module_user_add(p->chan);
1220 ast_channel_context_set(tmp, p->context);
1221 ast_channel_context_set(tmp2, p->context);
1222 ast_channel_exten_set(tmp2, p->exten);
1223 ast_channel_priority_set(tmp, 1);
1224 ast_channel_priority_set(tmp2, 1);
1226 ast_jb_configure(tmp, &p->jb_conf);
1231 /*! \brief Part of PBX interface */
1232 static struct ast_channel *local_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause)
1234 struct local_pvt *p;
1235 struct ast_channel *chan;
1237 /* Allocate a new private structure and then Asterisk channel */
1238 p = local_alloc(data, cap);
1242 chan = local_new(p, AST_STATE_DOWN, requestor ? ast_channel_linkedid(requestor) : NULL);
1244 ao2_unlink(locals, p);
1245 } else if (ast_channel_cc_params_init(chan, requestor ? ast_channel_get_cc_config_params((struct ast_channel *)requestor) : NULL)) {
1246 ao2_unlink(locals, p);
1247 p->owner = ast_channel_release(p->owner);
1248 ast_module_user_remove(p->u_owner);
1249 p->chan = ast_channel_release(p->chan);
1250 ast_module_user_remove(p->u_chan);
1253 ao2_ref(p, -1); /* kill the ref from the alloc */
1258 /*! \brief CLI command "local show channels" */
1259 static char *locals_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1261 struct local_pvt *p = NULL;
1262 struct ao2_iterator it;
1266 e->command = "local show channels";
1268 "Usage: local show channels\n"
1269 " Provides summary information on active local proxy channels.\n";
1276 return CLI_SHOWUSAGE;
1279 if (ao2_container_count(locals) == 0) {
1280 ast_cli(a->fd, "No local channels in use\n");
1281 return RESULT_SUCCESS;
1284 it = ao2_iterator_init(locals, 0);
1285 while ((p = ao2_iterator_next(&it))) {
1287 ast_cli(a->fd, "%s -- %s@%s\n", p->owner ? ast_channel_name(p->owner) : "<unowned>", p->exten, p->context);
1291 ao2_iterator_destroy(&it);
1296 static struct ast_cli_entry cli_local[] = {
1297 AST_CLI_DEFINE(locals_show, "List status of local channels"),
1300 static int manager_optimize_away(struct mansession *s, const struct message *m)
1302 const char *channel;
1303 struct local_pvt *p, *tmp = NULL;
1304 struct ast_channel *c;
1306 struct ao2_iterator it;
1308 channel = astman_get_header(m, "Channel");
1310 if (ast_strlen_zero(channel)) {
1311 astman_send_error(s, m, "'Channel' not specified.");
1315 c = ast_channel_get_by_name(channel);
1317 astman_send_error(s, m, "Channel does not exist.");
1321 p = ast_channel_tech_pvt(c);
1322 ast_channel_unref(c);
1325 it = ao2_iterator_init(locals, 0);
1326 while ((tmp = ao2_iterator_next(&it))) {
1330 ast_clear_flag(tmp, LOCAL_NO_OPTIMIZATION);
1337 ao2_iterator_destroy(&it);
1340 astman_send_ack(s, m, "Queued channel to be optimized away");
1342 astman_send_error(s, m, "Unable to find channel");
1349 static int locals_cmp_cb(void *obj, void *arg, int flags)
1351 return (obj == arg) ? CMP_MATCH : 0;
1354 /*! \brief Load module into PBX, register channel */
1355 static int load_module(void)
1357 if (!(local_tech.capabilities = ast_format_cap_alloc())) {
1358 return AST_MODULE_LOAD_FAILURE;
1360 ast_format_cap_add_all(local_tech.capabilities);
1362 if (!(locals = ao2_container_alloc(BUCKET_SIZE, NULL, locals_cmp_cb))) {
1363 ast_format_cap_destroy(local_tech.capabilities);
1364 return AST_MODULE_LOAD_FAILURE;
1367 /* Make sure we can register our channel type */
1368 if (ast_channel_register(&local_tech)) {
1369 ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
1370 ao2_ref(locals, -1);
1371 ast_format_cap_destroy(local_tech.capabilities);
1372 return AST_MODULE_LOAD_FAILURE;
1374 ast_cli_register_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
1375 ast_manager_register_xml("LocalOptimizeAway", EVENT_FLAG_SYSTEM|EVENT_FLAG_CALL, manager_optimize_away);
1377 return AST_MODULE_LOAD_SUCCESS;
1380 /*! \brief Unload the local proxy channel from Asterisk */
1381 static int unload_module(void)
1383 struct local_pvt *p = NULL;
1384 struct ao2_iterator it;
1386 /* First, take us out of the channel loop */
1387 ast_cli_unregister_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
1388 ast_manager_unregister("LocalOptimizeAway");
1389 ast_channel_unregister(&local_tech);
1391 it = ao2_iterator_init(locals, 0);
1392 while ((p = ao2_iterator_next(&it))) {
1394 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1398 ao2_iterator_destroy(&it);
1399 ao2_ref(locals, -1);
1401 ast_format_cap_destroy(local_tech.capabilities);
1405 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Local Proxy Channel (Note: used internally by other modules)",
1406 .load = load_module,
1407 .unload = unload_module,
1408 .load_pri = AST_MODPRI_CHANNEL_DRIVER,