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 unsigned int name_sequence = 0;
90 static struct ast_jb_conf g_jb_conf = {
93 .resync_threshold = -1,
98 static struct ast_channel *local_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause);
99 static int local_digit_begin(struct ast_channel *ast, char digit);
100 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
101 static int local_call(struct ast_channel *ast, const char *dest, int timeout);
102 static int local_hangup(struct ast_channel *ast);
103 static int local_answer(struct ast_channel *ast);
104 static struct ast_frame *local_read(struct ast_channel *ast);
105 static int local_write(struct ast_channel *ast, struct ast_frame *f);
106 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
107 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
108 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
109 static int local_sendtext(struct ast_channel *ast, const char *text);
110 static int local_devicestate(const char *data);
111 static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge);
112 static int local_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
113 static int local_setoption(struct ast_channel *chan, int option, void *data, int datalen);
115 /* PBX interface structure for channel registration */
116 static struct ast_channel_tech local_tech = {
118 .description = tdesc,
119 .requester = local_request,
120 .send_digit_begin = local_digit_begin,
121 .send_digit_end = local_digit_end,
123 .hangup = local_hangup,
124 .answer = local_answer,
126 .write = local_write,
127 .write_video = local_write,
128 .exception = local_read,
129 .indicate = local_indicate,
130 .fixup = local_fixup,
131 .send_html = local_sendhtml,
132 .send_text = local_sendtext,
133 .devicestate = local_devicestate,
134 .bridged_channel = local_bridgedchannel,
135 .queryoption = local_queryoption,
136 .setoption = local_setoption,
139 /*! \brief the local pvt structure for all channels
141 The local channel pvt has two ast_chan objects - the "owner" and the "next channel", the outbound channel
143 ast_chan owner -> local_pvt -> ast_chan chan -> yet-another-pvt-depending-on-channel-type
147 unsigned int flags; /*!< Private flags */
148 char context[AST_MAX_CONTEXT]; /*!< Context to call */
149 char exten[AST_MAX_EXTENSION]; /*!< Extension to call */
150 struct ast_format_cap *reqcap; /*!< Requested format capabilities */
151 struct ast_jb_conf jb_conf; /*!< jitterbuffer configuration for this local channel */
152 struct ast_channel *owner; /*!< Master Channel - Bridging happens here */
153 struct ast_channel *chan; /*!< Outbound channel - PBX is run here */
154 struct ast_module_user *u_owner;/*!< reference to keep the module loaded while in use */
155 struct ast_module_user *u_chan; /*!< reference to keep the module loaded while in use */
158 #define LOCAL_ALREADY_MASQED (1 << 0) /*!< Already masqueraded */
159 #define LOCAL_LAUNCHED_PBX (1 << 1) /*!< PBX was launched */
160 #define LOCAL_NO_OPTIMIZATION (1 << 2) /*!< Do not optimize using masquerading */
161 #define LOCAL_BRIDGE (1 << 3) /*!< Report back the "true" channel as being bridged to */
162 #define LOCAL_MOH_PASSTHRU (1 << 4) /*!< Pass through music on hold start/stop frames */
165 * \brief Send a pvt in with no locks held and get all locks
167 * \note NO locks should be held prior to calling this function
168 * \note The pvt must have a ref held before calling this function
169 * \note if outchan or outowner is set != NULL after calling this function
170 * those channels are locked and reffed.
173 static void awesome_locking(struct local_pvt *p, struct ast_channel **outchan, struct ast_channel **outowner)
175 struct ast_channel *chan = NULL;
176 struct ast_channel *owner = NULL;
182 ast_channel_ref(chan);
186 ast_channel_ref(owner);
190 /* if we don't have both channels, then this is very easy */
191 if (!owner || !chan) {
193 ast_channel_lock(owner);
195 ast_channel_lock(chan);
198 /* lock both channels first, then get the pvt lock */
199 ast_channel_lock_both(chan, owner);
203 /* Now that we have all the locks, validate that nothing changed */
204 if (p->owner != owner || p->chan != chan) {
206 ast_channel_unlock(owner);
207 owner = ast_channel_unref(owner);
210 ast_channel_unlock(chan);
211 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)
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 if (!strcmp(write_info->function, "CHANNEL")
242 && !strncasecmp(write_info->data, "hangup_handler_", 15)) {
243 /* Block CHANNEL(hangup_handler_xxx) writes to the other local channel. */
247 /* get the tech pvt */
248 if (!(p = ast_channel_tech_pvt(ast))) {
252 ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
254 /* get the channel we are supposed to write to */
256 otherchan = (write_info->chan == p->owner) ? p->chan : p->owner;
257 if (!otherchan || otherchan == write_info->chan) {
261 goto setoption_cleanup;
263 ast_channel_ref(otherchan);
265 /* clear the pvt lock before grabbing the channel */
268 ast_channel_lock(otherchan);
269 res = write_info->write_fn(otherchan, write_info->function, write_info->data, write_info->value);
270 ast_channel_unlock(otherchan);
275 ast_channel_unref(otherchan);
277 ast_channel_lock(ast); /* Lock back before we leave */
281 /*! \brief Adds devicestate to local channels */
282 static int local_devicestate(const char *data)
284 char *exten = ast_strdupa(data);
288 struct local_pvt *lp;
289 struct ao2_iterator it;
291 /* Strip options if they exist */
292 opts = strchr(exten, '/');
297 context = strchr(exten, '@');
300 "Someone used Local/%s somewhere without a @context. This is bad.\n", data);
301 return AST_DEVICE_INVALID;
305 ast_debug(3, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
306 res = ast_exists_extension(NULL, context, exten, 1, NULL);
308 return AST_DEVICE_INVALID;
311 res = AST_DEVICE_NOT_INUSE;
313 it = ao2_iterator_init(locals, 0);
314 for (; (lp = ao2_iterator_next(&it)); ao2_ref(lp, -1)) {
318 is_inuse = !strcmp(exten, lp->exten)
319 && !strcmp(context, lp->context)
321 && ast_test_flag(lp, LOCAL_LAUNCHED_PBX);
324 res = AST_DEVICE_INUSE;
329 ao2_iterator_destroy(&it);
334 /*! \brief Return the bridged channel of a Local channel */
335 static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge)
337 struct local_pvt *p = ast_channel_tech_pvt(bridge);
338 struct ast_channel *bridged = bridge;
341 ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning <none>\n",
342 ast_channel_name(chan), ast_channel_name(bridge));
348 if (ast_test_flag(p, LOCAL_BRIDGE)) {
349 /* Find the opposite channel */
350 bridged = (bridge == p->owner ? p->chan : p->owner);
352 /* Now see if the opposite channel is bridged to anything */
355 } else if (ast_channel_internal_bridged_channel(bridged)) {
356 bridged = ast_channel_internal_bridged_channel(bridged);
365 /* Called with ast locked */
366 static int local_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
369 struct ast_channel *bridged = NULL;
370 struct ast_channel *tmp = NULL;
373 if (option != AST_OPTION_T38_STATE) {
374 /* AST_OPTION_T38_STATE is the only supported option at this time */
378 /* for some reason the channel is not locked in channel.c when this function is called */
379 if (!(p = ast_channel_tech_pvt(ast))) {
384 if (!(tmp = IS_OUTBOUND(ast, p) ? p->owner : p->chan)) {
388 ast_channel_ref(tmp);
390 ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
392 ast_channel_lock(tmp);
393 if (!(bridged = ast_bridged_channel(tmp))) {
395 ast_channel_unlock(tmp);
398 ast_channel_ref(bridged);
399 ast_channel_unlock(tmp);
403 res = ast_channel_queryoption(bridged, option, data, datalen, 0);
404 bridged = ast_channel_unref(bridged);
407 tmp = ast_channel_unref(tmp);
409 ast_channel_lock(ast); /* Lock back before we leave */
414 /*! \brief queue a frame on a to either the p->owner or p->chan
416 * \note the local_pvt MUST have it's ref count bumped before entering this function and
417 * decremented after this function is called. This is a side effect of the deadlock
418 * avoidance that is necessary to lock 2 channels and a tech_pvt. Without a ref counted
419 * local_pvt, it is impossible to guarantee it will not be destroyed by another thread
420 * during deadlock avoidance.
422 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f,
423 struct ast_channel *us, int us_locked)
425 struct ast_channel *other = NULL;
427 /* Recalculate outbound channel */
428 other = isoutbound ? p->owner : p->chan;
434 /* do not queue frame if generator is on both local channels */
435 if (us && ast_channel_generator(us) && ast_channel_generator(other)) {
439 /* grab a ref on the channel before unlocking the pvt,
440 * other can not go away from us now regardless of locking */
441 ast_channel_ref(other);
442 if (us && us_locked) {
443 ast_channel_unlock(us);
447 if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_RINGING) {
448 ast_setstate(other, AST_STATE_RINGING);
450 ast_queue_frame(other, f);
452 other = ast_channel_unref(other);
453 if (us && us_locked) {
454 ast_channel_lock(us);
461 static int local_answer(struct ast_channel *ast)
463 struct local_pvt *p = ast_channel_tech_pvt(ast);
473 isoutbound = IS_OUTBOUND(ast, p);
475 /* Pass along answer since somebody answered us */
476 struct ast_frame answer = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
477 res = local_queue_frame(p, isoutbound, &answer, ast, 1);
479 ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n");
488 * \note This function assumes that we're only called from the "outbound" local channel side
490 * \note it is assummed p is locked and reffed before entering this function
492 static void check_bridge(struct ast_channel *ast, struct local_pvt *p)
494 struct ast_channel *owner;
495 struct ast_channel *chan;
496 struct ast_channel *bridged_chan;
499 /* Do a few conditional checks early on just to see if this optimization is possible */
500 if (ast_test_flag(p, LOCAL_NO_OPTIMIZATION | LOCAL_ALREADY_MASQED)
501 || !p->chan || !p->owner) {
505 /* Safely get the channel bridged to p->chan */
506 chan = ast_channel_ref(p->chan);
508 ao2_unlock(p); /* don't call bridged channel with the pvt locked */
509 bridged_chan = ast_bridged_channel(chan);
512 chan = ast_channel_unref(chan);
514 /* since we had to unlock p to get the bridged chan, validate our
515 * data once again and verify the bridged channel is what we expect
516 * it to be in order to perform this optimization */
517 if (ast_test_flag(p, LOCAL_NO_OPTIMIZATION | LOCAL_ALREADY_MASQED)
518 || !p->chan || !p->owner
519 || (ast_channel_internal_bridged_channel(p->chan) != bridged_chan)) {
523 /* only do the masquerade if we are being called on the outbound channel,
524 if it has been bridged to another channel and if there are no pending
525 frames on the owner channel (because they would be transferred to the
526 outbound channel during the masquerade)
528 if (!ast_channel_internal_bridged_channel(p->chan) /* Not ast_bridged_channel! Only go one step! */
529 || !AST_LIST_EMPTY(ast_channel_readq(p->owner))
530 || ast != p->chan /* Sanity check (should always be false) */) {
534 /* Masquerade bridged channel into owner */
535 /* Lock everything we need, one by one, and give up if
536 we can't get everything. Remember, we'll get another
537 chance in just a little bit */
538 if (ast_channel_trylock(ast_channel_internal_bridged_channel(p->chan))) {
541 if (ast_check_hangup(ast_channel_internal_bridged_channel(p->chan))
542 || ast_channel_trylock(p->owner)) {
543 ast_channel_unlock(ast_channel_internal_bridged_channel(p->chan));
548 * At this point we have 4 locks:
549 * p, p->chan (same as ast), p->chan->_bridge, p->owner
551 * Flush a voice or video frame on the outbound channel to make
552 * the queue empty faster so we can get optimized out.
554 f = AST_LIST_FIRST(ast_channel_readq(p->chan));
555 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
556 AST_LIST_REMOVE_HEAD(ast_channel_readq(p->chan), frame_list);
558 f = AST_LIST_FIRST(ast_channel_readq(p->chan));
562 || ast_check_hangup(p->owner)
563 || ast_channel_masquerade(p->owner, ast_channel_internal_bridged_channel(p->chan))) {
564 ast_channel_unlock(p->owner);
565 ast_channel_unlock(ast_channel_internal_bridged_channel(p->chan));
569 /* Masquerade got setup. */
570 ast_debug(4, "Masquerading %s <- %s\n",
571 ast_channel_name(p->owner),
572 ast_channel_name(ast_channel_internal_bridged_channel(p->chan)));
573 if (ast_channel_monitor(p->owner)
574 && !ast_channel_monitor(ast_channel_internal_bridged_channel(p->chan))) {
575 struct ast_channel_monitor *tmp;
577 /* If a local channel is being monitored, we don't want a masquerade
578 * to cause the monitor to go away. Since the masquerade swaps the monitors,
579 * pre-swapping the monitors before the masquerade will ensure that the monitor
580 * ends up where it is expected.
582 tmp = ast_channel_monitor(p->owner);
583 ast_channel_monitor_set(p->owner, ast_channel_monitor(ast_channel_internal_bridged_channel(p->chan)));
584 ast_channel_monitor_set(ast_channel_internal_bridged_channel(p->chan), tmp);
586 if (ast_channel_audiohooks(p->chan)) {
587 struct ast_audiohook_list *audiohooks_swapper;
589 audiohooks_swapper = ast_channel_audiohooks(p->chan);
590 ast_channel_audiohooks_set(p->chan, ast_channel_audiohooks(p->owner));
591 ast_channel_audiohooks_set(p->owner, audiohooks_swapper);
594 /* If any Caller ID was set, preserve it after masquerade like above. We must check
595 * to see if Caller ID was set because otherwise we'll mistakingly copy info not
596 * set from the dialplan and will overwrite the real channel Caller ID. The reason
597 * for this whole preswapping action is because the Caller ID is set on the channel
598 * thread (which is the to be masqueraded away local channel) before both local
599 * channels are optimized away.
601 if (ast_channel_caller(p->owner)->id.name.valid || ast_channel_caller(p->owner)->id.number.valid
602 || ast_channel_caller(p->owner)->id.subaddress.valid || ast_channel_caller(p->owner)->ani.name.valid
603 || ast_channel_caller(p->owner)->ani.number.valid || ast_channel_caller(p->owner)->ani.subaddress.valid) {
604 SWAP(*ast_channel_caller(p->owner), *ast_channel_caller(ast_channel_internal_bridged_channel(p->chan)));
606 if (ast_channel_redirecting(p->owner)->from.name.valid || ast_channel_redirecting(p->owner)->from.number.valid
607 || ast_channel_redirecting(p->owner)->from.subaddress.valid || ast_channel_redirecting(p->owner)->to.name.valid
608 || ast_channel_redirecting(p->owner)->to.number.valid || ast_channel_redirecting(p->owner)->to.subaddress.valid) {
609 SWAP(*ast_channel_redirecting(p->owner), *ast_channel_redirecting(ast_channel_internal_bridged_channel(p->chan)));
611 if (ast_channel_dialed(p->owner)->number.str || ast_channel_dialed(p->owner)->subaddress.valid) {
612 SWAP(*ast_channel_dialed(p->owner), *ast_channel_dialed(ast_channel_internal_bridged_channel(p->chan)));
614 ast_app_group_update(p->chan, p->owner);
615 ast_set_flag(p, LOCAL_ALREADY_MASQED);
617 ast_channel_unlock(p->owner);
618 ast_channel_unlock(ast_channel_internal_bridged_channel(p->chan));
620 /* Do the masquerade now. */
621 owner = ast_channel_ref(p->owner);
623 ast_channel_unlock(ast);
624 ast_do_masquerade(owner);
625 ast_channel_unref(owner);
626 ast_channel_lock(ast);
630 static struct ast_frame *local_read(struct ast_channel *ast)
632 return &ast_null_frame;
635 static int local_write(struct ast_channel *ast, struct ast_frame *f)
637 struct local_pvt *p = ast_channel_tech_pvt(ast);
645 /* Just queue for delivery to the other side */
646 ao2_ref(p, 1); /* ref for local_queue_frame */
648 isoutbound = IS_OUTBOUND(ast, p);
651 && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
652 check_bridge(ast, p);
655 if (!ast_test_flag(p, LOCAL_ALREADY_MASQED)) {
656 res = local_queue_frame(p, isoutbound, f, ast, 1);
658 ast_debug(1, "Not posting to '%s' queue since already masqueraded out\n",
659 ast_channel_name(ast));
668 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
670 struct local_pvt *p = ast_channel_tech_pvt(newchan);
678 if ((p->owner != oldchan) && (p->chan != oldchan)) {
679 ast_log(LOG_WARNING, "Old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
683 if (p->owner == oldchan) {
689 /* Do not let a masquerade cause a Local channel to be bridged to itself! */
690 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))) {
691 ast_log(LOG_WARNING, "You can not bridge a Local channel to itself!\n");
693 ast_queue_hangup(newchan);
701 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
703 struct local_pvt *p = ast_channel_tech_pvt(ast);
705 struct ast_frame f = { AST_FRAME_CONTROL, };
712 ao2_ref(p, 1); /* ref for local_queue_frame */
714 /* If this is an MOH hold or unhold, do it on the Local channel versus real channel */
715 if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_HOLD) {
716 ast_moh_start(ast, data, NULL);
717 } else if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_UNHOLD) {
719 } else if (condition == AST_CONTROL_CONNECTED_LINE || condition == AST_CONTROL_REDIRECTING) {
720 struct ast_channel *this_channel;
721 struct ast_channel *the_other_channel;
722 /* A connected line update frame may only contain a partial amount of data, such
723 * as just a source, or just a ton, and not the full amount of information. However,
724 * the collected information is all stored in the outgoing channel's connectedline
725 * structure, so when receiving a connected line update on an outgoing local channel,
726 * we need to transmit the collected connected line information instead of whatever
727 * happens to be in this control frame. The same applies for redirecting information, which
728 * is why it is handled here as well.*/
730 isoutbound = IS_OUTBOUND(ast, p);
732 this_channel = p->chan;
733 the_other_channel = p->owner;
735 this_channel = p->owner;
736 the_other_channel = p->chan;
738 if (the_other_channel) {
739 unsigned char frame_data[1024];
740 if (condition == AST_CONTROL_CONNECTED_LINE) {
742 ast_connected_line_copy_to_caller(ast_channel_caller(the_other_channel), ast_channel_connected(this_channel));
744 f.datalen = ast_connected_line_build_data(frame_data, sizeof(frame_data), ast_channel_connected(this_channel), NULL);
746 f.datalen = ast_redirecting_build_data(frame_data, sizeof(frame_data), ast_channel_redirecting(this_channel), NULL);
748 f.subclass.integer = condition;
749 f.data.ptr = frame_data;
750 res = local_queue_frame(p, isoutbound, &f, ast, 1);
754 /* Queue up a frame representing the indication as a control frame */
757 * Block -1 stop tones events if we are to be optimized out. We
758 * don't need a flurry of these events on a local channel chain
759 * when initially connected to slow the optimization process.
761 if (0 <= condition || ast_test_flag(p, LOCAL_NO_OPTIMIZATION)) {
762 isoutbound = IS_OUTBOUND(ast, p);
763 f.subclass.integer = condition;
764 f.data.ptr = (void *) data;
766 res = local_queue_frame(p, isoutbound, &f, ast, 1);
768 if (!res && (condition == AST_CONTROL_T38_PARAMETERS) &&
769 (datalen == sizeof(struct ast_control_t38_parameters))) {
770 const struct ast_control_t38_parameters *parameters = data;
772 if (parameters->request_response == AST_T38_REQUEST_PARMS) {
773 res = AST_T38_REQUEST_PARMS;
777 ast_debug(4, "Blocked indication %d\n", condition);
786 static int local_digit_begin(struct ast_channel *ast, char digit)
788 struct local_pvt *p = ast_channel_tech_pvt(ast);
790 struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
797 ao2_ref(p, 1); /* ref for local_queue_frame */
799 isoutbound = IS_OUTBOUND(ast, p);
800 f.subclass.integer = digit;
801 res = local_queue_frame(p, isoutbound, &f, ast, 0);
808 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
810 struct local_pvt *p = ast_channel_tech_pvt(ast);
812 struct ast_frame f = { AST_FRAME_DTMF_END, };
819 ao2_ref(p, 1); /* ref for local_queue_frame */
821 isoutbound = IS_OUTBOUND(ast, p);
822 f.subclass.integer = digit;
824 res = local_queue_frame(p, isoutbound, &f, ast, 0);
831 static int local_sendtext(struct ast_channel *ast, const char *text)
833 struct local_pvt *p = ast_channel_tech_pvt(ast);
835 struct ast_frame f = { AST_FRAME_TEXT, };
842 ao2_ref(p, 1); /* ref for local_queue_frame */
844 isoutbound = IS_OUTBOUND(ast, p);
845 f.data.ptr = (char *) text;
846 f.datalen = strlen(text) + 1;
847 res = local_queue_frame(p, isoutbound, &f, ast, 0);
853 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
855 struct local_pvt *p = ast_channel_tech_pvt(ast);
857 struct ast_frame f = { AST_FRAME_HTML, };
864 ao2_ref(p, 1); /* ref for local_queue_frame */
866 isoutbound = IS_OUTBOUND(ast, p);
867 f.subclass.integer = subclass;
868 f.data.ptr = (char *)data;
870 res = local_queue_frame(p, isoutbound, &f, ast, 0);
877 /*! \brief Initiate new call, part of PBX interface
878 * dest is the dial string */
879 static int local_call(struct ast_channel *ast, const char *dest, int timeout)
881 struct local_pvt *p = ast_channel_tech_pvt(ast);
884 struct ast_channel *owner = NULL;
885 struct ast_channel *chan = NULL;
887 struct ast_var_t *varptr;
888 struct ast_var_t *clone_var;
889 char *reduced_dest = ast_strdupa(dest);
898 /* since we are letting go of channel locks that were locked coming into
899 * this function, then we need to give the tech pvt a ref */
901 ast_channel_unlock(ast);
903 awesome_locking(p, &chan, &owner);
911 if (!owner || !chan) {
917 * Note that cid_num and cid_name aren't passed in the ast_channel_alloc
918 * call, so it's done here instead.
920 * All these failure points just return -1. The individual strings will
921 * be cleared when we destroy the channel.
923 ast_party_redirecting_copy(ast_channel_redirecting(chan), ast_channel_redirecting(owner));
925 ast_party_dialed_copy(ast_channel_dialed(chan), ast_channel_dialed(owner));
927 ast_connected_line_copy_to_caller(ast_channel_caller(chan), ast_channel_connected(owner));
928 ast_connected_line_copy_from_caller(ast_channel_connected(chan), ast_channel_caller(owner));
930 ast_channel_language_set(chan, ast_channel_language(owner));
931 ast_channel_accountcode_set(chan, ast_channel_accountcode(owner));
932 ast_channel_musicclass_set(chan, ast_channel_musicclass(owner));
933 ast_cdr_update(chan);
935 ast_channel_cc_params_init(chan, ast_channel_get_cc_config_params(owner));
937 /* Make sure we inherit the AST_CAUSE_ANSWERED_ELSEWHERE if it's set on the queue/dial call request in the dialplan */
938 if (ast_channel_hangupcause(ast) == AST_CAUSE_ANSWERED_ELSEWHERE) {
939 ast_channel_hangupcause_set(chan, AST_CAUSE_ANSWERED_ELSEWHERE);
942 /* copy the channel variables from the incoming channel to the outgoing channel */
943 /* Note that due to certain assumptions, they MUST be in the same order */
944 AST_LIST_TRAVERSE(ast_channel_varshead(owner), varptr, entries) {
945 clone_var = ast_var_assign(varptr->name, varptr->value);
947 AST_LIST_INSERT_TAIL(ast_channel_varshead(chan), clone_var, entries);
950 ast_channel_datastore_inherit(owner, chan);
951 /* If the local channel has /n or /b on the end of it,
952 * we need to lop that off for our argument to setting
953 * up the CC_INTERFACES variable
955 if ((slash = strrchr(reduced_dest, '/'))) {
958 ast_set_cc_interfaces_chanvar(chan, reduced_dest);
960 exten = ast_strdupa(ast_channel_exten(chan));
961 context = ast_strdupa(ast_channel_context(chan));
966 ast_channel_unlock(chan);
968 if (!ast_exists_extension(chan, context, exten, 1,
969 S_COR(ast_channel_caller(owner)->id.number.valid, ast_channel_caller(owner)->id.number.str, NULL))) {
970 ast_log(LOG_NOTICE, "No such extension/context %s@%s while calling Local channel\n", exten, context);
972 chan = ast_channel_unref(chan); /* we already unlocked it, so clear it hear so the cleanup label won't touch it. */
977 <managerEventInstance>
978 <synopsis>Raised when two halves of a Local Channel form a bridge.</synopsis>
980 <parameter name="Channel1">
981 <para>The name of the Local Channel half that bridges to another channel.</para>
983 <parameter name="Channel2">
984 <para>The name of the Local Channel half that executes the dialplan.</para>
986 <parameter name="Context">
987 <para>The context in the dialplan that Channel2 starts in.</para>
989 <parameter name="Exten">
990 <para>The extension in the dialplan that Channel2 starts in.</para>
992 <parameter name="LocalOptimization">
999 </managerEventInstance>
1001 manager_event(EVENT_FLAG_CALL, "LocalBridge",
1008 "LocalOptimization: %s\r\n",
1009 ast_channel_name(p->owner), ast_channel_name(p->chan), ast_channel_uniqueid(p->owner), ast_channel_uniqueid(p->chan),
1010 p->context, p->exten,
1011 ast_test_flag(p, LOCAL_NO_OPTIMIZATION) ? "Yes" : "No");
1014 /* Start switch on sub channel */
1015 if (!(res = ast_pbx_start(chan))) {
1017 ast_set_flag(p, LOCAL_LAUNCHED_PBX);
1020 chan = ast_channel_unref(chan); /* chan is already unlocked, clear it here so the cleanup lable won't touch it. */
1030 ast_channel_unlock(chan);
1031 chan = ast_channel_unref(chan);
1034 /* owner is supposed to be == to ast, if it
1035 * is, don't unlock it because ast must exit locked */
1038 ast_channel_unlock(owner);
1039 ast_channel_lock(ast);
1041 owner = ast_channel_unref(owner);
1043 /* we have to exit with ast locked */
1044 ast_channel_lock(ast);
1050 /*! \brief Hangup a call through the local proxy channel */
1051 static int local_hangup(struct ast_channel *ast)
1053 struct local_pvt *p = ast_channel_tech_pvt(ast);
1055 int hangup_chan = 0;
1057 struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_HANGUP }, .data.uint32 = ast_channel_hangupcause(ast) };
1058 struct ast_channel *owner = NULL;
1059 struct ast_channel *chan = NULL;
1065 /* give the pvt a ref since we are unlocking the channel. */
1068 /* the pvt isn't going anywhere, we gave it a ref */
1069 ast_channel_unlock(ast);
1071 /* lock everything */
1072 awesome_locking(p, &chan, &owner);
1074 if (ast != chan && ast != owner) {
1076 goto local_hangup_cleanup;
1079 isoutbound = IS_OUTBOUND(ast, p); /* just comparing pointer of ast */
1081 if (p->chan && ast_channel_hangupcause(ast) == AST_CAUSE_ANSWERED_ELSEWHERE) {
1082 ast_channel_hangupcause_set(p->chan, AST_CAUSE_ANSWERED_ELSEWHERE);
1083 ast_debug(2, "This local call has AST_CAUSE_ANSWERED_ELSEWHERE set.\n");
1087 const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
1088 if ((status) && (p->owner)) {
1089 ast_channel_hangupcause_set(p->owner, ast_channel_hangupcause(p->chan));
1090 pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
1093 ast_clear_flag(p, LOCAL_LAUNCHED_PBX);
1094 ast_module_user_remove(p->u_chan);
1097 ast_module_user_remove(p->u_owner);
1099 ast_queue_hangup(p->chan);
1104 ast_channel_tech_pvt_set(ast, NULL); /* this is one of our locked channels, doesn't matter which */
1106 if (!p->owner && !p->chan) {
1108 /* Remove from list */
1109 ao2_unlink(locals, p);
1113 goto local_hangup_cleanup;
1115 if (p->chan && !ast_test_flag(p, LOCAL_LAUNCHED_PBX)) {
1116 /* Need to actually hangup since there is no PBX */
1119 local_queue_frame(p, isoutbound, &f, NULL, 0);
1122 local_hangup_cleanup:
1128 ast_channel_unlock(chan);
1132 chan = ast_channel_unref(chan);
1135 ast_channel_unlock(owner);
1136 owner = ast_channel_unref(owner);
1139 /* leave with the same stupid channel locked that came in */
1140 ast_channel_lock(ast);
1144 static void local_destroy(void *obj)
1146 struct local_pvt *pvt = obj;
1147 pvt->reqcap = ast_format_cap_destroy(pvt->reqcap);
1150 /*! \brief Create a call structure */
1151 static struct local_pvt *local_alloc(const char *data, struct ast_format_cap *cap)
1153 struct local_pvt *tmp = NULL;
1158 if (!(tmp = ao2_alloc(sizeof(*tmp), local_destroy))) {
1161 if (!(tmp->reqcap = ast_format_cap_dup(cap))) {
1166 /* Initialize private structure information */
1167 parse = ast_strdupa(data);
1169 memcpy(&tmp->jb_conf, &g_jb_conf, sizeof(tmp->jb_conf));
1171 /* Look for options */
1172 if ((opts = strchr(parse, '/'))) {
1174 if (strchr(opts, 'n'))
1175 ast_set_flag(tmp, LOCAL_NO_OPTIMIZATION);
1176 if (strchr(opts, 'j')) {
1177 if (ast_test_flag(tmp, LOCAL_NO_OPTIMIZATION))
1178 ast_set_flag(&tmp->jb_conf, AST_JB_ENABLED);
1180 ast_log(LOG_ERROR, "You must use the 'n' option for chan_local "
1181 "to use the 'j' option to enable the jitterbuffer\n");
1184 if (strchr(opts, 'b')) {
1185 ast_set_flag(tmp, LOCAL_BRIDGE);
1187 if (strchr(opts, 'm')) {
1188 ast_set_flag(tmp, LOCAL_MOH_PASSTHRU);
1192 /* Look for a context */
1193 if ((c = strchr(parse, '@'))) {
1197 ast_copy_string(tmp->context, c ? c : "default", sizeof(tmp->context));
1198 ast_copy_string(tmp->exten, parse, sizeof(tmp->exten));
1201 ao2_link(locals, tmp);
1203 return tmp; /* this is returned with a ref */
1206 /*! \brief Start new local channel */
1207 static struct ast_channel *local_new(struct local_pvt *p, int state, const char *linkedid, struct ast_callid *callid)
1209 struct ast_channel *tmp = NULL, *tmp2 = NULL;
1210 struct ast_format fmt;
1211 int generated_seqno = ast_atomic_fetchadd_int((int *)&name_sequence, +1);
1215 /* Allocate two new Asterisk channels */
1216 /* safe accountcode */
1217 if (p->owner && ast_channel_accountcode(p->owner))
1218 t = ast_channel_accountcode(p->owner);
1223 ama = ast_channel_amaflags(p->owner);
1227 /* Make sure that the ;2 channel gets the same linkedid as ;1. You can't pass linkedid to both
1228 * allocations since if linkedid isn't set, then each channel will generate its own linkedid. */
1229 if (!(tmp = ast_channel_alloc(1, state, 0, 0, t, p->exten, p->context, linkedid, ama, "Local/%s@%s-%08x;1", p->exten, p->context, generated_seqno))
1230 || !(tmp2 = ast_channel_alloc(1, AST_STATE_RING, 0, 0, t, p->exten, p->context, ast_channel_linkedid(tmp), ama, "Local/%s@%s-%08x;2", p->exten, p->context, generated_seqno))) {
1232 tmp = ast_channel_release(tmp);
1234 ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n");
1239 ast_channel_callid_set(tmp, callid);
1240 ast_channel_callid_set(tmp2, callid);
1243 ast_channel_tech_set(tmp, &local_tech);
1244 ast_channel_tech_set(tmp2, &local_tech);
1246 ast_format_cap_copy(ast_channel_nativeformats(tmp), p->reqcap);
1247 ast_format_cap_copy(ast_channel_nativeformats(tmp2), p->reqcap);
1249 /* Determine our read/write format and set it on each channel */
1250 ast_best_codec(p->reqcap, &fmt);
1251 ast_format_copy(ast_channel_writeformat(tmp), &fmt);
1252 ast_format_copy(ast_channel_writeformat(tmp2), &fmt);
1253 ast_format_copy(ast_channel_rawwriteformat(tmp), &fmt);
1254 ast_format_copy(ast_channel_rawwriteformat(tmp2), &fmt);
1255 ast_format_copy(ast_channel_readformat(tmp), &fmt);
1256 ast_format_copy(ast_channel_readformat(tmp2), &fmt);
1257 ast_format_copy(ast_channel_rawreadformat(tmp), &fmt);
1258 ast_format_copy(ast_channel_rawreadformat(tmp2), &fmt);
1260 ast_channel_tech_pvt_set(tmp, p);
1261 ast_channel_tech_pvt_set(tmp2, p);
1265 p->u_owner = ast_module_user_add(p->owner);
1266 p->u_chan = ast_module_user_add(p->chan);
1268 ast_channel_context_set(tmp, p->context);
1269 ast_channel_context_set(tmp2, p->context);
1270 ast_channel_exten_set(tmp2, p->exten);
1271 ast_channel_priority_set(tmp, 1);
1272 ast_channel_priority_set(tmp2, 1);
1274 ast_jb_configure(tmp, &p->jb_conf);
1279 /*! \brief Part of PBX interface */
1280 static struct ast_channel *local_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *data, int *cause)
1282 struct local_pvt *p;
1283 struct ast_channel *chan;
1284 struct ast_callid *callid = ast_read_threadstorage_callid();
1286 /* Allocate a new private structure and then Asterisk channel */
1287 p = local_alloc(data, cap);
1290 goto local_request_end;
1292 chan = local_new(p, AST_STATE_DOWN, requestor ? ast_channel_linkedid(requestor) : NULL, callid);
1294 ao2_unlink(locals, p);
1295 } else if (ast_channel_cc_params_init(chan, requestor ? ast_channel_get_cc_config_params((struct ast_channel *)requestor) : NULL)) {
1296 ao2_unlink(locals, p);
1297 p->owner = ast_channel_release(p->owner);
1298 ast_module_user_remove(p->u_owner);
1299 p->chan = ast_channel_release(p->chan);
1300 ast_module_user_remove(p->u_chan);
1303 ao2_ref(p, -1); /* kill the ref from the alloc */
1308 ast_callid_unref(callid);
1314 /*! \brief CLI command "local show channels" */
1315 static char *locals_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1317 struct local_pvt *p = NULL;
1318 struct ao2_iterator it;
1322 e->command = "local show channels";
1324 "Usage: local show channels\n"
1325 " Provides summary information on active local proxy channels.\n";
1332 return CLI_SHOWUSAGE;
1335 if (ao2_container_count(locals) == 0) {
1336 ast_cli(a->fd, "No local channels in use\n");
1337 return RESULT_SUCCESS;
1340 it = ao2_iterator_init(locals, 0);
1341 while ((p = ao2_iterator_next(&it))) {
1343 ast_cli(a->fd, "%s -- %s@%s\n", p->owner ? ast_channel_name(p->owner) : "<unowned>", p->exten, p->context);
1347 ao2_iterator_destroy(&it);
1352 static struct ast_cli_entry cli_local[] = {
1353 AST_CLI_DEFINE(locals_show, "List status of local channels"),
1356 static int manager_optimize_away(struct mansession *s, const struct message *m)
1358 const char *channel;
1359 struct local_pvt *p, *tmp = NULL;
1360 struct ast_channel *c;
1362 struct ao2_iterator it;
1364 channel = astman_get_header(m, "Channel");
1366 if (ast_strlen_zero(channel)) {
1367 astman_send_error(s, m, "'Channel' not specified.");
1371 c = ast_channel_get_by_name(channel);
1373 astman_send_error(s, m, "Channel does not exist.");
1377 p = ast_channel_tech_pvt(c);
1378 ast_channel_unref(c);
1381 it = ao2_iterator_init(locals, 0);
1382 while ((tmp = ao2_iterator_next(&it))) {
1386 ast_clear_flag(tmp, LOCAL_NO_OPTIMIZATION);
1393 ao2_iterator_destroy(&it);
1396 astman_send_ack(s, m, "Queued channel to be optimized away");
1398 astman_send_error(s, m, "Unable to find channel");
1405 static int locals_cmp_cb(void *obj, void *arg, int flags)
1407 return (obj == arg) ? CMP_MATCH : 0;
1410 /*! \brief Load module into PBX, register channel */
1411 static int load_module(void)
1413 if (!(local_tech.capabilities = ast_format_cap_alloc())) {
1414 return AST_MODULE_LOAD_FAILURE;
1416 ast_format_cap_add_all(local_tech.capabilities);
1418 if (!(locals = ao2_container_alloc(BUCKET_SIZE, NULL, locals_cmp_cb))) {
1419 ast_format_cap_destroy(local_tech.capabilities);
1420 return AST_MODULE_LOAD_FAILURE;
1423 /* Make sure we can register our channel type */
1424 if (ast_channel_register(&local_tech)) {
1425 ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
1426 ao2_ref(locals, -1);
1427 ast_format_cap_destroy(local_tech.capabilities);
1428 return AST_MODULE_LOAD_FAILURE;
1430 ast_cli_register_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
1431 ast_manager_register_xml("LocalOptimizeAway", EVENT_FLAG_SYSTEM|EVENT_FLAG_CALL, manager_optimize_away);
1433 return AST_MODULE_LOAD_SUCCESS;
1436 /*! \brief Unload the local proxy channel from Asterisk */
1437 static int unload_module(void)
1439 struct local_pvt *p = NULL;
1440 struct ao2_iterator it;
1442 /* First, take us out of the channel loop */
1443 ast_cli_unregister_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
1444 ast_manager_unregister("LocalOptimizeAway");
1445 ast_channel_unregister(&local_tech);
1447 it = ao2_iterator_init(locals, 0);
1448 while ((p = ao2_iterator_next(&it))) {
1450 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1454 ao2_iterator_destroy(&it);
1455 ao2_ref(locals, -1);
1457 ast_format_cap_destroy(local_tech.capabilities);
1461 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Local Proxy Channel (Note: used internally by other modules)",
1462 .load = load_module,
1463 .unload = unload_module,
1464 .load_pri = AST_MODPRI_CHANNEL_DRIVER,