Fri Jan 18 19:58:24 2013

Asterisk developer's documentation


chan_local.c File Reference

Local Proxy Channel. More...

#include "asterisk.h"
#include <fcntl.h>
#include <sys/signal.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/acl.h"
#include "asterisk/callerid.h"
#include "asterisk/file.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/musiconhold.h"
#include "asterisk/manager.h"
#include "asterisk/stringfields.h"
#include "asterisk/devicestate.h"
#include "asterisk/astobj2.h"
Include dependency graph for chan_local.c:

Go to the source code of this file.

Data Structures

struct  local_pvt
 the local pvt structure for all channels More...

Defines

#define IS_OUTBOUND(a, b)   (a == b->chan ? 1 : 0)
#define LOCAL_ALREADY_MASQED   (1 << 0)
#define LOCAL_BRIDGE   (1 << 3)
#define LOCAL_LAUNCHED_PBX   (1 << 1)
#define LOCAL_MOH_PASSTHRU   (1 << 4)
#define LOCAL_NO_OPTIMIZATION   (1 << 2)

Functions

 AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER,"Local Proxy Channel (Note: used internally by other modules)",.load=load_module,.unload=unload_module,.load_pri=AST_MODPRI_CHANNEL_DRIVER,)
static void awesome_locking (struct local_pvt *p, struct ast_channel **outchan, struct ast_channel **outowner)
static void check_bridge (struct ast_channel *ast, struct local_pvt *p)
static int load_module (void)
 Load module into PBX, register channel.
static struct local_pvt * local_alloc (const char *data, format_t format)
 Create a call structure.
static int local_answer (struct ast_channel *ast)
static struct ast_channel * local_bridgedchannel (struct ast_channel *chan, struct ast_channel *bridge)
 Return the bridged channel of a Local channel.
static int local_call (struct ast_channel *ast, char *dest, int timeout)
 Initiate new call, part of PBX interface dest is the dial string.
static int local_devicestate (void *data)
 Adds devicestate to local channels.
static int local_digit_begin (struct ast_channel *ast, char digit)
static int local_digit_end (struct ast_channel *ast, char digit, unsigned int duration)
static int local_fixup (struct ast_channel *oldchan, struct ast_channel *newchan)
static int local_hangup (struct ast_channel *ast)
 Hangup a call through the local proxy channel.
static int local_indicate (struct ast_channel *ast, int condition, const void *data, size_t datalen)
static struct ast_channel * local_new (struct local_pvt *p, int state, const char *linkedid)
 Start new local channel.
static int local_queryoption (struct ast_channel *ast, int option, void *data, int *datalen)
static int local_queue_frame (struct local_pvt *p, int isoutbound, struct ast_frame *f, struct ast_channel *us, int us_locked)
 queue a frame on a to either the p->owner or p->chan
static struct ast_frame * local_read (struct ast_channel *ast)
static struct ast_channel * local_request (const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
 Part of PBX interface.
static int local_sendhtml (struct ast_channel *ast, int subclass, const char *data, int datalen)
static int local_sendtext (struct ast_channel *ast, const char *text)
static int local_setoption (struct ast_channel *chan, int option, void *data, int datalen)
static int local_write (struct ast_channel *ast, struct ast_frame *f)
static int locals_cmp_cb (void *obj, void *arg, int flags)
static char * locals_show (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI command "local show channels".
static int manager_optimize_away (struct mansession *s, const struct message *m)
static int unload_module (void)
 Unload the local proxy channel from Asterisk.

Variables

static const int BUCKET_SIZE = 1
static struct ast_cli_entry cli_local []
static struct ast_jb_conf g_jb_conf
static struct ast_channel_tech local_tech
static struct ao2_container * locals
static unsigned int name_sequence = 0
static const char tdesc [] = "Local Proxy Channel Driver"

Detailed Description

Local Proxy Channel.

Author:
Mark Spencer <markster@digium.com>

Definition in file chan_local.c.


Define Documentation

#define IS_OUTBOUND ( a,
b   )     (a == b->chan ? 1 : 0)
#define LOCAL_ALREADY_MASQED   (1 << 0)

Already masqueraded

Definition at line 158 of file chan_local.c.

Referenced by check_bridge(), and local_write().

#define LOCAL_BRIDGE   (1 << 3)

Report back the "true" channel as being bridged to

Definition at line 161 of file chan_local.c.

Referenced by local_alloc(), and local_bridgedchannel().

#define LOCAL_LAUNCHED_PBX   (1 << 1)

PBX was launched

Definition at line 159 of file chan_local.c.

Referenced by local_call(), local_devicestate(), and local_hangup().

#define LOCAL_MOH_PASSTHRU   (1 << 4)

Pass through music on hold start/stop frames

Definition at line 162 of file chan_local.c.

Referenced by local_alloc(), and local_indicate().

#define LOCAL_NO_OPTIMIZATION   (1 << 2)

Do not optimize using masquerading

Definition at line 160 of file chan_local.c.

Referenced by check_bridge(), local_alloc(), local_indicate(), and manager_optimize_away().


Function Documentation

AST_MODULE_INFO ( ASTERISK_GPL_KEY  ,
AST_MODFLAG_LOAD_ORDER  ,
"Local Proxy Channel (Note: used internally by other modules)"  ,
.  load = load_module,
.  unload = unload_module,
.  load_pri = AST_MODPRI_CHANNEL_DRIVER 
)
static void awesome_locking ( struct local_pvt *  p,
struct ast_channel **  outchan,
struct ast_channel **  outowner 
) [static]

Definition at line 173 of file chan_local.c.

References ao2_lock, ao2_unlock, ast_channel_lock, ast_channel_ref, ast_channel_trylock, ast_channel_unlock, ast_channel_unref, local_pvt::chan, CHANNEL_DEADLOCK_AVOIDANCE, and local_pvt::owner.

Referenced by local_call(), and local_hangup().

00174 {
00175    struct ast_channel *chan = NULL;
00176    struct ast_channel *owner = NULL;
00177 
00178    for (;;) {
00179       ao2_lock(p);
00180       if (p->chan) {
00181          chan = p->chan;
00182          ast_channel_ref(chan);
00183       }
00184       if (p->owner) {
00185          owner = p->owner;
00186          ast_channel_ref(owner);
00187       }
00188       ao2_unlock(p);
00189 
00190       /* if we don't have both channels, then this is very easy */
00191       if (!owner || !chan) {
00192          if (owner) {
00193             ast_channel_lock(owner);
00194          } else if(chan) {
00195             ast_channel_lock(chan);
00196          }
00197          ao2_lock(p);
00198       } else {
00199          /* lock both channels first, then get the pvt lock */
00200          ast_channel_lock(chan);
00201          while (ast_channel_trylock(owner)) {
00202             CHANNEL_DEADLOCK_AVOIDANCE(chan);
00203          }
00204          ao2_lock(p);
00205       }
00206 
00207       /* Now that we have all the locks, validate that nothing changed */
00208       if (p->owner != owner || p->chan != chan) {
00209          if (owner) {
00210             ast_channel_unlock(owner);
00211             owner = ast_channel_unref(owner);
00212          }
00213          if (chan) {
00214             ast_channel_unlock(chan);
00215             chan = ast_channel_unref(chan);
00216          }
00217          ao2_unlock(p);
00218          continue;
00219       }
00220 
00221       break;
00222    }
00223    *outowner = p->owner;
00224    *outchan = p->chan;
00225 }

static void check_bridge ( struct ast_channel *  ast,
struct local_pvt *  p 
) [static]

Definition at line 488 of file chan_local.c.

References ast_channel::_bridge, ast_party_caller::ani, ao2_lock, ao2_unlock, ast_app_group_update(), ast_bridged_channel(), ast_channel_lock, ast_channel_masquerade(), ast_channel_ref, ast_channel_trylock, ast_channel_unlock, ast_channel_unref, ast_check_hangup(), ast_debug, ast_do_masquerade(), AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frfree, AST_LIST_EMPTY, AST_LIST_FIRST, AST_LIST_REMOVE_HEAD, ast_set_flag, ast_test_flag, ast_channel::audiohooks, ast_channel::caller, local_pvt::chan, ast_channel::dialed, f, ast_frame::frametype, ast_party_redirecting::from, ast_party_caller::id, LOCAL_ALREADY_MASQED, LOCAL_NO_OPTIMIZATION, ast_channel::monitor, ast_party_id::name, ast_party_dialed::number, ast_party_id::number, local_pvt::owner, ast_channel::redirecting, ast_party_dialed::str, ast_party_dialed::subaddress, ast_party_id::subaddress, ast_party_redirecting::to, ast_party_subaddress::valid, ast_party_number::valid, and ast_party_name::valid.

Referenced by local_write().

00489 {
00490    struct ast_channel *owner;
00491    struct ast_channel *chan;
00492    struct ast_channel *bridged_chan;
00493    struct ast_frame *f;
00494 
00495    /* Do a few conditional checks early on just to see if this optimization is possible */
00496    if (ast_test_flag(p, LOCAL_NO_OPTIMIZATION | LOCAL_ALREADY_MASQED)
00497       || !p->chan || !p->owner) {
00498       return;
00499    }
00500 
00501    /* Safely get the channel bridged to p->chan */
00502    chan = ast_channel_ref(p->chan);
00503 
00504    ao2_unlock(p); /* don't call bridged channel with the pvt locked */
00505    bridged_chan = ast_bridged_channel(chan);
00506    ao2_lock(p);
00507 
00508    chan = ast_channel_unref(chan);
00509 
00510    /* since we had to unlock p to get the bridged chan, validate our
00511     * data once again and verify the bridged channel is what we expect
00512     * it to be in order to perform this optimization */
00513    if (ast_test_flag(p, LOCAL_NO_OPTIMIZATION | LOCAL_ALREADY_MASQED)
00514       || !p->chan || !p->owner
00515       || (p->chan->_bridge != bridged_chan)) {
00516       return;
00517    }
00518 
00519    /* only do the masquerade if we are being called on the outbound channel,
00520       if it has been bridged to another channel and if there are no pending
00521       frames on the owner channel (because they would be transferred to the
00522       outbound channel during the masquerade)
00523    */
00524    if (!p->chan->_bridge /* Not ast_bridged_channel!  Only go one step! */
00525       || !AST_LIST_EMPTY(&p->owner->readq)
00526       || ast != p->chan /* Sanity check (should always be false) */) {
00527       return;
00528    }
00529 
00530    /* Masquerade bridged channel into owner */
00531    /* Lock everything we need, one by one, and give up if
00532       we can't get everything.  Remember, we'll get another
00533       chance in just a little bit */
00534    if (ast_channel_trylock(p->chan->_bridge)) {
00535       return;
00536    }
00537    if (ast_check_hangup(p->chan->_bridge) || ast_channel_trylock(p->owner)) {
00538       ast_channel_unlock(p->chan->_bridge);
00539       return;
00540    }
00541 
00542    /*
00543     * At this point we have 4 locks:
00544     * p, p->chan (same as ast), p->chan->_bridge, p->owner
00545     *
00546     * Flush a voice or video frame on the outbound channel to make
00547     * the queue empty faster so we can get optimized out.
00548     */
00549    f = AST_LIST_FIRST(&p->chan->readq);
00550    if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
00551       AST_LIST_REMOVE_HEAD(&p->chan->readq, frame_list);
00552       ast_frfree(f);
00553       f = AST_LIST_FIRST(&p->chan->readq);
00554    }
00555 
00556    if (f
00557       || ast_check_hangup(p->owner)
00558       || ast_channel_masquerade(p->owner, p->chan->_bridge)) {
00559       ast_channel_unlock(p->owner);
00560       ast_channel_unlock(p->chan->_bridge);
00561       return;
00562    }
00563 
00564    /* Masquerade got setup. */
00565    ast_debug(4, "Masquerading %s <- %s\n",
00566       p->owner->name, p->chan->_bridge->name);
00567    if (p->owner->monitor && !p->chan->_bridge->monitor) {
00568       struct ast_channel_monitor *tmp;
00569 
00570       /* If a local channel is being monitored, we don't want a masquerade
00571        * to cause the monitor to go away. Since the masquerade swaps the monitors,
00572        * pre-swapping the monitors before the masquerade will ensure that the monitor
00573        * ends up where it is expected.
00574        */
00575       tmp = p->owner->monitor;
00576       p->owner->monitor = p->chan->_bridge->monitor;
00577       p->chan->_bridge->monitor = tmp;
00578    }
00579    if (p->chan->audiohooks) {
00580       struct ast_audiohook_list *audiohooks_swapper;
00581       audiohooks_swapper = p->chan->audiohooks;
00582       p->chan->audiohooks = p->owner->audiohooks;
00583       p->owner->audiohooks = audiohooks_swapper;
00584    }
00585 
00586    /* If any Caller ID was set, preserve it after masquerade like above. We must check
00587     * to see if Caller ID was set because otherwise we'll mistakingly copy info not
00588     * set from the dialplan and will overwrite the real channel Caller ID. The reason
00589     * for this whole preswapping action is because the Caller ID is set on the channel
00590     * thread (which is the to be masqueraded away local channel) before both local
00591     * channels are optimized away.
00592     */
00593    if (p->owner->caller.id.name.valid || p->owner->caller.id.number.valid
00594       || p->owner->caller.id.subaddress.valid || p->owner->caller.ani.name.valid
00595       || p->owner->caller.ani.number.valid || p->owner->caller.ani.subaddress.valid) {
00596       struct ast_party_caller tmp;
00597 
00598       tmp = p->owner->caller;
00599       p->owner->caller = p->chan->_bridge->caller;
00600       p->chan->_bridge->caller = tmp;
00601    }
00602    if (p->owner->redirecting.from.name.valid || p->owner->redirecting.from.number.valid
00603       || p->owner->redirecting.from.subaddress.valid || p->owner->redirecting.to.name.valid
00604       || p->owner->redirecting.to.number.valid || p->owner->redirecting.to.subaddress.valid) {
00605       struct ast_party_redirecting tmp;
00606 
00607       tmp = p->owner->redirecting;
00608       p->owner->redirecting = p->chan->_bridge->redirecting;
00609       p->chan->_bridge->redirecting = tmp;
00610    }
00611    if (p->owner->dialed.number.str || p->owner->dialed.subaddress.valid) {
00612       struct ast_party_dialed tmp;
00613 
00614       tmp = p->owner->dialed;
00615       p->owner->dialed = p->chan->_bridge->dialed;
00616       p->chan->_bridge->dialed = tmp;
00617    }
00618    ast_app_group_update(p->chan, p->owner);
00619    ast_set_flag(p, LOCAL_ALREADY_MASQED);
00620 
00621    ast_channel_unlock(p->owner);
00622    ast_channel_unlock(p->chan->_bridge);
00623 
00624    /* Do the masquerade now. */
00625    owner = ast_channel_ref(p->owner);
00626    ao2_unlock(p);
00627    ast_channel_unlock(ast);
00628    ast_do_masquerade(owner);
00629    ast_channel_unref(owner);
00630    ast_channel_lock(ast);
00631    ao2_lock(p);
00632 }

static int load_module ( void   )  [static]

Load module into PBX, register channel.

Definition at line 1360 of file chan_local.c.

References ao2_container_alloc, ao2_ref, ast_channel_register(), ast_cli_register_multiple(), ast_log(), ast_manager_register_xml, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, EVENT_FLAG_CALL, EVENT_FLAG_SYSTEM, locals_cmp_cb(), LOG_ERROR, and manager_optimize_away().

01361 {
01362    if (!(locals = ao2_container_alloc(BUCKET_SIZE, NULL, locals_cmp_cb))) {
01363       return AST_MODULE_LOAD_FAILURE;
01364    }
01365 
01366    /* Make sure we can register our channel type */
01367    if (ast_channel_register(&local_tech)) {
01368       ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
01369       ao2_ref(locals, -1);
01370       return AST_MODULE_LOAD_FAILURE;
01371    }
01372    ast_cli_register_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
01373    ast_manager_register_xml("LocalOptimizeAway", EVENT_FLAG_SYSTEM|EVENT_FLAG_CALL, manager_optimize_away);
01374 
01375    return AST_MODULE_LOAD_SUCCESS;
01376 }

static struct local_pvt* local_alloc ( const char *  data,
format_t  format 
) [static, read]

Create a call structure.

Definition at line 1107 of file chan_local.c.

References ao2_alloc, ao2_link, ast_copy_string(), ast_exists_extension(), AST_JB_ENABLED, ast_log(), ast_set_flag, ast_test_flag, local_pvt::context, local_pvt::exten, local_pvt::jb_conf, LOCAL_BRIDGE, LOCAL_MOH_PASSTHRU, LOCAL_NO_OPTIMIZATION, LOG_ERROR, LOG_NOTICE, and local_pvt::reqformat.

Referenced by local_request().

01108 {
01109    struct local_pvt *tmp = NULL;
01110    char *c = NULL, *opts = NULL;
01111 
01112    if (!(tmp = ao2_alloc(sizeof(*tmp), NULL))) {
01113       return NULL;
01114    }
01115 
01116    /* Initialize private structure information */
01117    ast_copy_string(tmp->exten, data, sizeof(tmp->exten));
01118 
01119    memcpy(&tmp->jb_conf, &g_jb_conf, sizeof(tmp->jb_conf));
01120 
01121    /* Look for options */
01122    if ((opts = strchr(tmp->exten, '/'))) {
01123       *opts++ = '\0';
01124       if (strchr(opts, 'n'))
01125          ast_set_flag(tmp, LOCAL_NO_OPTIMIZATION);
01126       if (strchr(opts, 'j')) {
01127          if (ast_test_flag(tmp, LOCAL_NO_OPTIMIZATION))
01128             ast_set_flag(&tmp->jb_conf, AST_JB_ENABLED);
01129          else {
01130             ast_log(LOG_ERROR, "You must use the 'n' option for chan_local "
01131                "to use the 'j' option to enable the jitterbuffer\n");
01132          }
01133       }
01134       if (strchr(opts, 'b')) {
01135          ast_set_flag(tmp, LOCAL_BRIDGE);
01136       }
01137       if (strchr(opts, 'm')) {
01138          ast_set_flag(tmp, LOCAL_MOH_PASSTHRU);
01139       }
01140    }
01141 
01142    /* Look for a context */
01143    if ((c = strchr(tmp->exten, '@')))
01144       *c++ = '\0';
01145 
01146    ast_copy_string(tmp->context, c ? c : "default", sizeof(tmp->context));
01147 
01148    tmp->reqformat = format;
01149 
01150 #if 0
01151    /* We can't do this check here, because we don't know the CallerID yet, and
01152     * the CallerID could potentially affect what step is actually taken (or
01153     * even if that step exists). */
01154    if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
01155       ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
01156       tmp = local_pvt_destroy(tmp);
01157    } else {
01158 #endif
01159       /* Add to list */
01160       ao2_link(locals, tmp);
01161 #if 0
01162    }
01163 #endif
01164    return tmp; /* this is returned with a ref */
01165 }

static int local_answer ( struct ast_channel *  ast  )  [static]

Definition at line 458 of file chan_local.c.

References ao2_lock, ao2_ref, ao2_unlock, AST_CONTROL_ANSWER, AST_FRAME_CONTROL, ast_log(), IS_OUTBOUND, local_queue_frame(), LOG_WARNING, and ast_channel::tech_pvt.

00459 {
00460    struct local_pvt *p = ast->tech_pvt;
00461    int isoutbound;
00462    int res = -1;
00463 
00464    if (!p)
00465       return -1;
00466 
00467    ao2_lock(p);
00468    ao2_ref(p, 1);
00469    isoutbound = IS_OUTBOUND(ast, p);
00470    if (isoutbound) {
00471       /* Pass along answer since somebody answered us */
00472       struct ast_frame answer = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
00473       res = local_queue_frame(p, isoutbound, &answer, ast, 1);
00474    } else {
00475       ast_log(LOG_WARNING, "Huh?  Local is being asked to answer?\n");
00476    }
00477    ao2_unlock(p);
00478    ao2_ref(p, -1);
00479    return res;
00480 }

static struct ast_channel * local_bridgedchannel ( struct ast_channel *  chan,
struct ast_channel *  bridge 
) [static, read]

Return the bridged channel of a Local channel.

Definition at line 332 of file chan_local.c.

References ast_channel::_bridge, ao2_lock, ao2_unlock, ast_debug, ast_test_flag, local_pvt::chan, LOCAL_BRIDGE, local_pvt::owner, and ast_channel::tech_pvt.

00333 {
00334    struct local_pvt *p = bridge->tech_pvt;
00335    struct ast_channel *bridged = bridge;
00336 
00337    if (!p) {
00338       ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning <none>\n",
00339          chan->name, bridge->name);
00340       return NULL;
00341    }
00342 
00343    ao2_lock(p);
00344 
00345    if (ast_test_flag(p, LOCAL_BRIDGE)) {
00346       /* Find the opposite channel */
00347       bridged = (bridge == p->owner ? p->chan : p->owner);
00348       
00349       /* Now see if the opposite channel is bridged to anything */
00350       if (!bridged) {
00351          bridged = bridge;
00352       } else if (bridged->_bridge) {
00353          bridged = bridged->_bridge;
00354       }
00355    }
00356 
00357    ao2_unlock(p);
00358 
00359    return bridged;
00360 }

static int local_call ( struct ast_channel *  ast,
char *  dest,
int  timeout 
) [static]

Initiate new call, part of PBX interface dest is the dial string.

Definition at line 876 of file chan_local.c.

References accountcode, ao2_lock, ao2_ref, ao2_unlock, ast_calloc, ast_cdr_update(), ast_channel_cc_params_init(), ast_channel_datastore_inherit(), ast_channel_get_cc_config_params(), ast_channel_lock, ast_channel_unlock, ast_channel_unref, ast_connected_line_copy_from_caller(), ast_connected_line_copy_to_caller(), ast_exists_extension(), AST_FLAG_ANSWERED_ELSEWHERE, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log(), ast_party_dialed_copy(), ast_party_redirecting_copy(), ast_pbx_start(), ast_set_cc_interfaces_chanvar(), ast_set_flag, ast_strdupa, ast_string_field_set, ast_test_flag, awesome_locking(), ast_channel::caller, ast_channel::connected, ast_channel::context, context, ast_channel::dialed, ast_channel::exten, exten, ast_party_caller::id, language, len(), LOCAL_LAUNCHED_PBX, LOG_NOTICE, musicclass, ast_var_t::name, ast_party_id::number, ast_channel::redirecting, S_COR, ast_party_number::str, ast_channel::tech_pvt, ast_party_number::valid, and ast_channel::varshead.

00877 {
00878    struct local_pvt *p = ast->tech_pvt;
00879    int pvt_locked = 0;
00880 
00881    struct ast_channel *owner = NULL;
00882    struct ast_channel *chan = NULL;
00883    int res;
00884    struct ast_var_t *varptr = NULL, *new;
00885    size_t len, namelen;
00886    char *reduced_dest = ast_strdupa(dest);
00887    char *slash;
00888    const char *exten;
00889    const char *context;
00890 
00891    if (!p) {
00892       return -1;
00893    }
00894 
00895    /* since we are letting go of channel locks that were locked coming into
00896     * this function, then we need to give the tech pvt a ref */
00897    ao2_ref(p, 1);
00898    ast_channel_unlock(ast);
00899 
00900    awesome_locking(p, &chan, &owner);
00901    pvt_locked = 1;
00902 
00903    if (owner != ast) {
00904       res = -1;
00905       goto return_cleanup;
00906    }
00907 
00908    if (!owner || !chan) {
00909       res = -1;
00910       goto return_cleanup;
00911    }
00912 
00913    /*
00914     * Note that cid_num and cid_name aren't passed in the ast_channel_alloc
00915     * call, so it's done here instead.
00916     *
00917     * All these failure points just return -1. The individual strings will
00918     * be cleared when we destroy the channel.
00919     */
00920    ast_party_redirecting_copy(&chan->redirecting, &owner->redirecting);
00921 
00922    ast_party_dialed_copy(&chan->dialed, &owner->dialed);
00923 
00924    ast_connected_line_copy_to_caller(&chan->caller, &owner->connected);
00925    ast_connected_line_copy_from_caller(&chan->connected, &owner->caller);
00926 
00927    ast_string_field_set(chan, language, owner->language);
00928    ast_string_field_set(chan, accountcode, owner->accountcode);
00929    ast_string_field_set(chan, musicclass, owner->musicclass);
00930    ast_cdr_update(chan);
00931 
00932    ast_channel_cc_params_init(chan, ast_channel_get_cc_config_params(owner));
00933 
00934    /* Make sure we inherit the ANSWERED_ELSEWHERE flag if it's set on the queue/dial call request in the dialplan */
00935    if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
00936       ast_set_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE);
00937    }
00938 
00939    /* copy the channel variables from the incoming channel to the outgoing channel */
00940    /* Note that due to certain assumptions, they MUST be in the same order */
00941    AST_LIST_TRAVERSE(&owner->varshead, varptr, entries) {
00942       namelen = strlen(varptr->name);
00943       len = sizeof(struct ast_var_t) + namelen + strlen(varptr->value) + 2;
00944       if ((new = ast_calloc(1, len))) {
00945          memcpy(new, varptr, len);
00946          new->value = &(new->name[0]) + namelen + 1;
00947          AST_LIST_INSERT_TAIL(&chan->varshead, new, entries);
00948       }
00949    }
00950    ast_channel_datastore_inherit(owner, chan);
00951    /* If the local channel has /n or /b on the end of it,
00952     * we need to lop that off for our argument to setting
00953     * up the CC_INTERFACES variable
00954     */
00955    if ((slash = strrchr(reduced_dest, '/'))) {
00956       *slash = '\0';
00957    }
00958    ast_set_cc_interfaces_chanvar(chan, reduced_dest);
00959 
00960    exten = ast_strdupa(chan->exten);
00961    context = ast_strdupa(chan->context);
00962 
00963    ao2_unlock(p);
00964    pvt_locked = 0;
00965 
00966    ast_channel_unlock(chan);
00967 
00968    if (!ast_exists_extension(chan, context, exten, 1,
00969       S_COR(owner->caller.id.number.valid, owner->caller.id.number.str, NULL))) {
00970       ast_log(LOG_NOTICE, "No such extension/context %s@%s while calling Local channel\n", exten, context);
00971       res = -1;
00972       chan = ast_channel_unref(chan); /* we already unlocked it, so clear it hear so the cleanup label won't touch it. */
00973       goto return_cleanup;
00974    }
00975 
00976    /* Start switch on sub channel */
00977    if (!(res = ast_pbx_start(chan))) {
00978       ao2_lock(p);
00979       ast_set_flag(p, LOCAL_LAUNCHED_PBX);
00980       ao2_unlock(p);
00981    }
00982    chan = ast_channel_unref(chan); /* chan is already unlocked, clear it here so the cleanup lable won't touch it. */
00983 
00984 return_cleanup:
00985    if (p) {
00986       if (pvt_locked) {
00987          ao2_unlock(p);
00988       }
00989       ao2_ref(p, -1);
00990    }
00991    if (chan) {
00992       ast_channel_unlock(chan);
00993       chan = ast_channel_unref(chan);
00994    }
00995 
00996    /* owner is supposed to be == to ast,  if it
00997     * is, don't unlock it because ast must exit locked */
00998    if (owner) {
00999       if (owner != ast) {
01000          ast_channel_unlock(owner);
01001          ast_channel_lock(ast);
01002       }
01003       owner = ast_channel_unref(owner);
01004    } else {
01005       /* we have to exit with ast locked */
01006       ast_channel_lock(ast);
01007    }
01008 
01009    return res;
01010 }

static int local_devicestate ( void *  data  )  [static]

Adds devicestate to local channels.

Definition at line 283 of file chan_local.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_lock, ao2_ref, ao2_unlock, ast_debug, AST_DEVICE_INUSE, AST_DEVICE_INVALID, AST_DEVICE_NOT_INUSE, ast_exists_extension(), ast_log(), ast_strdupa, ast_test_flag, local_pvt::context, context, local_pvt::exten, exten, LOCAL_LAUNCHED_PBX, LOG_WARNING, and local_pvt::owner.

00284 {
00285    char *exten = ast_strdupa(data);
00286    char *context = NULL, *opts = NULL;
00287    int res;
00288    struct local_pvt *lp;
00289    struct ao2_iterator it;
00290 
00291    if (!(context = strchr(exten, '@'))) {
00292       ast_log(LOG_WARNING, "Someone used Local/%s somewhere without a @context. This is bad.\n", exten);
00293       return AST_DEVICE_INVALID; 
00294    }
00295 
00296    *context++ = '\0';
00297 
00298    /* Strip options if they exist */
00299    if ((opts = strchr(context, '/')))
00300       *opts = '\0';
00301 
00302    ast_debug(3, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
00303 
00304    res = ast_exists_extension(NULL, context, exten, 1, NULL);
00305    if (!res)      
00306       return AST_DEVICE_INVALID;
00307    
00308    res = AST_DEVICE_NOT_INUSE;
00309 
00310    it = ao2_iterator_init(locals, 0);
00311    for (; (lp = ao2_iterator_next(&it)); ao2_ref(lp, -1)) {
00312       int is_inuse;
00313 
00314       ao2_lock(lp);
00315       is_inuse = !strcmp(exten, lp->exten)
00316          && !strcmp(context, lp->context)
00317          && lp->owner
00318          && ast_test_flag(lp, LOCAL_LAUNCHED_PBX);
00319       ao2_unlock(lp);
00320       if (is_inuse) {
00321          res = AST_DEVICE_INUSE;
00322          ao2_ref(lp, -1);
00323          break;
00324       }
00325    }
00326    ao2_iterator_destroy(&it);
00327 
00328    return res;
00329 }

static int local_digit_begin ( struct ast_channel *  ast,
char  digit 
) [static]

Definition at line 787 of file chan_local.c.

References ao2_lock, ao2_ref, ao2_unlock, AST_FRAME_DTMF_BEGIN, ast_frame_subclass::integer, IS_OUTBOUND, local_queue_frame(), ast_frame::subclass, and ast_channel::tech_pvt.

00788 {
00789    struct local_pvt *p = ast->tech_pvt;
00790    int res = -1;
00791    struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
00792    int isoutbound;
00793 
00794    if (!p)
00795       return -1;
00796 
00797    ao2_ref(p, 1); /* ref for local_queue_frame */
00798    ao2_lock(p);
00799    isoutbound = IS_OUTBOUND(ast, p);
00800    f.subclass.integer = digit;
00801    res = local_queue_frame(p, isoutbound, &f, ast, 0);
00802    ao2_unlock(p);
00803    ao2_ref(p, -1);
00804 
00805    return res;
00806 }

static int local_digit_end ( struct ast_channel *  ast,
char  digit,
unsigned int  duration 
) [static]

Definition at line 808 of file chan_local.c.

References ao2_lock, ao2_ref, ao2_unlock, AST_FRAME_DTMF_END, ast_frame_subclass::integer, IS_OUTBOUND, ast_frame::len, local_queue_frame(), ast_frame::subclass, and ast_channel::tech_pvt.

00809 {
00810    struct local_pvt *p = ast->tech_pvt;
00811    int res = -1;
00812    struct ast_frame f = { AST_FRAME_DTMF_END, };
00813    int isoutbound;
00814 
00815    if (!p)
00816       return -1;
00817 
00818    ao2_ref(p, 1); /* ref for local_queue_frame */
00819    ao2_lock(p);
00820    isoutbound = IS_OUTBOUND(ast, p);
00821    f.subclass.integer = digit;
00822    f.len = duration;
00823    res = local_queue_frame(p, isoutbound, &f, ast, 0);
00824    ao2_unlock(p);
00825    ao2_ref(p, -1);
00826 
00827    return res;
00828 }

static int local_fixup ( struct ast_channel *  oldchan,
struct ast_channel *  newchan 
) [static]

Definition at line 672 of file chan_local.c.

References ast_channel::_bridge, ao2_lock, ao2_unlock, ast_check_hangup(), ast_log(), ast_queue_hangup(), local_pvt::chan, LOG_WARNING, local_pvt::owner, and ast_channel::tech_pvt.

00673 {
00674    struct local_pvt *p = newchan->tech_pvt;
00675 
00676    if (!p)
00677       return -1;
00678 
00679    ao2_lock(p);
00680 
00681    if ((p->owner != oldchan) && (p->chan != oldchan)) {
00682       ast_log(LOG_WARNING, "Old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
00683       ao2_unlock(p);
00684       return -1;
00685    }
00686    if (p->owner == oldchan)
00687       p->owner = newchan;
00688    else
00689       p->chan = newchan;
00690 
00691    /* Do not let a masquerade cause a Local channel to be bridged to itself! */
00692    if (!ast_check_hangup(newchan) && ((p->owner && p->owner->_bridge == p->chan) || (p->chan && p->chan->_bridge == p->owner))) {
00693       ast_log(LOG_WARNING, "You can not bridge a Local channel to itself!\n");
00694       ao2_unlock(p);
00695       ast_queue_hangup(newchan);
00696       return -1;
00697    }
00698 
00699    ao2_unlock(p);
00700    return 0;
00701 }

static int local_hangup ( struct ast_channel *  ast  )  [static]

Hangup a call through the local proxy channel.

Definition at line 1013 of file chan_local.c.

References ao2_ref, ao2_unlink, ao2_unlock, ast_channel_lock, ast_channel_unlock, ast_channel_unref, ast_clear_flag, AST_CONTROL_HANGUP, ast_debug, AST_FLAG_ANSWERED_ELSEWHERE, AST_FRAME_CONTROL, ast_hangup(), ast_module_user_remove, ast_queue_hangup(), ast_set_flag, ast_test_flag, awesome_locking(), local_pvt::chan, hangup_chan(), ast_channel::hangupcause, IS_OUTBOUND, LOCAL_LAUNCHED_PBX, local_queue_frame(), local_pvt::owner, pbx_builtin_getvar_helper(), pbx_builtin_setvar_helper(), status, ast_channel::tech_pvt, local_pvt::u_chan, and local_pvt::u_owner.

01014 {
01015    struct local_pvt *p = ast->tech_pvt;
01016    int isoutbound;
01017    int hangup_chan = 0;
01018    int res = 0;
01019    struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_HANGUP }, .data.uint32 = ast->hangupcause };
01020    struct ast_channel *owner = NULL;
01021    struct ast_channel *chan = NULL;
01022 
01023    if (!p) {
01024       return -1;
01025    }
01026 
01027    /* give the pvt a ref since we are unlocking the channel. */
01028    ao2_ref(p, 1);
01029 
01030    /* the pvt isn't going anywhere, we gave it a ref */
01031    ast_channel_unlock(ast);
01032 
01033    /* lock everything */
01034    awesome_locking(p, &chan, &owner);
01035 
01036    if (ast != chan && ast != owner) {
01037       res = -1;
01038       goto local_hangup_cleanup;
01039    }
01040 
01041    isoutbound = IS_OUTBOUND(ast, p); /* just comparing pointer of ast */
01042 
01043    if (p->chan && ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
01044       ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
01045       ast_debug(2, "This local call has the ANSWERED_ELSEWHERE flag set.\n");
01046    }
01047 
01048    if (isoutbound) {
01049       const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
01050       if ((status) && (p->owner)) {
01051          p->owner->hangupcause = p->chan->hangupcause;
01052          pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
01053       }
01054 
01055       ast_clear_flag(p, LOCAL_LAUNCHED_PBX);
01056       ast_module_user_remove(p->u_chan);
01057       p->chan = NULL;
01058    } else {
01059       ast_module_user_remove(p->u_owner);
01060       if (p->chan) {
01061          ast_queue_hangup(p->chan);
01062       }
01063       p->owner = NULL;
01064    }
01065 
01066    ast->tech_pvt = NULL; /* this is one of our locked channels, doesn't matter which */
01067 
01068    if (!p->owner && !p->chan) {
01069       ao2_unlock(p);
01070       /* Remove from list */
01071       ao2_unlink(locals, p);
01072       ao2_ref(p, -1);
01073       p = NULL;
01074       res = 0;
01075       goto local_hangup_cleanup;
01076    }
01077    if (p->chan && !ast_test_flag(p, LOCAL_LAUNCHED_PBX)) {
01078       /* Need to actually hangup since there is no PBX */
01079       hangup_chan = 1;
01080    } else {
01081       local_queue_frame(p, isoutbound, &f, NULL, 0);
01082    }
01083 
01084 local_hangup_cleanup:
01085    if (p) {
01086       ao2_unlock(p);
01087       ao2_ref(p, -1);
01088    }
01089    if (chan) {
01090       ast_channel_unlock(chan);
01091       if (hangup_chan) {
01092          ast_hangup(chan);
01093       }
01094       chan = ast_channel_unref(chan);
01095    }
01096    if (owner) {
01097       ast_channel_unlock(owner);
01098       owner = ast_channel_unref(owner);
01099    }
01100 
01101    /* leave with the same stupid channel locked that came in */
01102    ast_channel_lock(ast);
01103    return res;
01104 }

static int local_indicate ( struct ast_channel *  ast,
int  condition,
const void *  data,
size_t  datalen 
) [static]

Definition at line 703 of file chan_local.c.

References ao2_lock, ao2_ref, ao2_unlock, ast_connected_line_build_data(), ast_connected_line_copy_to_caller(), AST_CONTROL_CONNECTED_LINE, AST_CONTROL_HOLD, AST_CONTROL_REDIRECTING, AST_CONTROL_T38_PARAMETERS, AST_CONTROL_UNHOLD, ast_debug, AST_FRAME_CONTROL, ast_moh_start(), ast_moh_stop(), ast_redirecting_build_data(), AST_T38_REQUEST_PARMS, ast_test_flag, ast_channel::caller, local_pvt::chan, ast_channel::connected, ast_frame::data, ast_frame::datalen, ast_frame_subclass::integer, IS_OUTBOUND, LOCAL_MOH_PASSTHRU, LOCAL_NO_OPTIMIZATION, local_queue_frame(), local_pvt::owner, ast_frame::ptr, ast_channel::redirecting, ast_control_t38_parameters::request_response, ast_frame::subclass, and ast_channel::tech_pvt.

00704 {
00705    struct local_pvt *p = ast->tech_pvt;
00706    int res = 0;
00707    struct ast_frame f = { AST_FRAME_CONTROL, };
00708    int isoutbound;
00709 
00710    if (!p)
00711       return -1;
00712 
00713    ao2_ref(p, 1); /* ref for local_queue_frame */
00714 
00715    /* If this is an MOH hold or unhold, do it on the Local channel versus real channel */
00716    if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_HOLD) {
00717       ast_moh_start(ast, data, NULL);
00718    } else if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_UNHOLD) {
00719       ast_moh_stop(ast);
00720    } else if (condition == AST_CONTROL_CONNECTED_LINE || condition == AST_CONTROL_REDIRECTING) {
00721       struct ast_channel *this_channel;
00722       struct ast_channel *the_other_channel;
00723       /* A connected line update frame may only contain a partial amount of data, such
00724        * as just a source, or just a ton, and not the full amount of information. However,
00725        * the collected information is all stored in the outgoing channel's connectedline
00726        * structure, so when receiving a connected line update on an outgoing local channel,
00727        * we need to transmit the collected connected line information instead of whatever
00728        * happens to be in this control frame. The same applies for redirecting information, which
00729        * is why it is handled here as well.*/
00730       ao2_lock(p);
00731       isoutbound = IS_OUTBOUND(ast, p);
00732       if (isoutbound) {
00733          this_channel = p->chan;
00734          the_other_channel = p->owner;
00735       } else {
00736          this_channel = p->owner;
00737          the_other_channel = p->chan;
00738       }
00739       if (the_other_channel) {
00740          unsigned char frame_data[1024];
00741          if (condition == AST_CONTROL_CONNECTED_LINE) {
00742             if (isoutbound) {
00743                ast_connected_line_copy_to_caller(&the_other_channel->caller, &this_channel->connected);
00744             }
00745             f.datalen = ast_connected_line_build_data(frame_data, sizeof(frame_data), &this_channel->connected, NULL);
00746          } else {
00747             f.datalen = ast_redirecting_build_data(frame_data, sizeof(frame_data), &this_channel->redirecting, NULL);
00748          }
00749          f.subclass.integer = condition;
00750          f.data.ptr = frame_data;
00751          res = local_queue_frame(p, isoutbound, &f, ast, 1);
00752       }
00753       ao2_unlock(p);
00754    } else {
00755       /* Queue up a frame representing the indication as a control frame */
00756       ao2_lock(p);
00757       /*
00758        * Block -1 stop tones events if we are to be optimized out.  We
00759        * don't need a flurry of these events on a local channel chain
00760        * when initially connected to slow the optimization process.
00761        */
00762       if (0 <= condition || ast_test_flag(p, LOCAL_NO_OPTIMIZATION)) {
00763          isoutbound = IS_OUTBOUND(ast, p);
00764          f.subclass.integer = condition;
00765          f.data.ptr = (void *) data;
00766          f.datalen = datalen;
00767          res = local_queue_frame(p, isoutbound, &f, ast, 1);
00768 
00769          if (!res && (condition == AST_CONTROL_T38_PARAMETERS) &&
00770              (datalen == sizeof(struct ast_control_t38_parameters))) {
00771             const struct ast_control_t38_parameters *parameters = data;
00772             
00773             if (parameters->request_response == AST_T38_REQUEST_PARMS) {
00774                res = AST_T38_REQUEST_PARMS;
00775             }
00776          }
00777       } else {
00778          ast_debug(4, "Blocked indication %d\n", condition);
00779       }
00780       ao2_unlock(p);
00781    }
00782 
00783    ao2_ref(p, -1);
00784    return res;
00785 }

static struct ast_channel* local_new ( struct local_pvt *  p,
int  state,
const char *  linkedid 
) [static, read]

Start new local channel.

Definition at line 1168 of file chan_local.c.

References ast_channel::amaflags, ast_atomic_fetchadd_int(), ast_best_codec(), ast_channel_alloc, ast_channel_release(), ast_copy_string(), AST_FLAG_DISABLE_DEVSTATE_CACHE, ast_jb_configure(), ast_log(), ast_module_user_add, AST_STATE_RING, local_pvt::chan, ast_channel::context, local_pvt::context, ast_channel::exten, local_pvt::exten, ast_channel::flags, local_pvt::jb_conf, LOG_WARNING, ast_channel::nativeformats, local_pvt::owner, ast_channel::priority, ast_channel::rawreadformat, ast_channel::rawwriteformat, ast_channel::readformat, local_pvt::reqformat, ast_channel::tech, ast_channel::tech_pvt, local_pvt::u_chan, local_pvt::u_owner, and ast_channel::writeformat.

Referenced by local_request().

01169 {
01170    struct ast_channel *tmp = NULL, *tmp2 = NULL;
01171    int fmt = 0;
01172    int generated_seqno = ast_atomic_fetchadd_int((int *)&name_sequence, +1);
01173    const char *t;
01174    int ama;
01175 
01176    /* Allocate two new Asterisk channels */
01177    /* safe accountcode */
01178    if (p->owner && p->owner->accountcode)
01179       t = p->owner->accountcode;
01180    else
01181       t = "";
01182 
01183    if (p->owner)
01184       ama = p->owner->amaflags;
01185    else
01186       ama = 0;
01187 
01188    /* Make sure that the ;2 channel gets the same linkedid as ;1. You can't pass linkedid to both
01189     * allocations since if linkedid isn't set, then each channel will generate its own linkedid. */
01190    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))
01191       || !(tmp2 = ast_channel_alloc(1, AST_STATE_RING, 0, 0, t, p->exten, p->context, tmp->linkedid, ama, "Local/%s@%s-%08x;2", p->exten, p->context, generated_seqno))) {
01192       if (tmp) {
01193          tmp = ast_channel_release(tmp);
01194       }
01195       ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n");
01196       return NULL;
01197    }
01198 
01199    tmp2->tech = tmp->tech = &local_tech;
01200 
01201    tmp->nativeformats = p->reqformat;
01202    tmp2->nativeformats = p->reqformat;
01203 
01204    /* Determine our read/write format and set it on each channel */
01205    fmt = ast_best_codec(p->reqformat);
01206    tmp->writeformat = fmt;
01207    tmp2->writeformat = fmt;
01208    tmp->rawwriteformat = fmt;
01209    tmp2->rawwriteformat = fmt;
01210    tmp->readformat = fmt;
01211    tmp2->readformat = fmt;
01212    tmp->rawreadformat = fmt;
01213    tmp2->rawreadformat = fmt;
01214 
01215    tmp->tech_pvt = p;
01216    tmp2->tech_pvt = p;
01217 
01218    tmp->flags |= AST_FLAG_DISABLE_DEVSTATE_CACHE;
01219    tmp2->flags |= AST_FLAG_DISABLE_DEVSTATE_CACHE;
01220 
01221    p->owner = tmp;
01222    p->chan = tmp2;
01223    p->u_owner = ast_module_user_add(p->owner);
01224    p->u_chan = ast_module_user_add(p->chan);
01225 
01226    ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
01227    ast_copy_string(tmp2->context, p->context, sizeof(tmp2->context));
01228    ast_copy_string(tmp2->exten, p->exten, sizeof(tmp->exten));
01229    tmp->priority = 1;
01230    tmp2->priority = 1;
01231 
01232    ast_jb_configure(tmp, &p->jb_conf);
01233 
01234    return tmp;
01235 }

static int local_queryoption ( struct ast_channel *  ast,
int  option,
void *  data,
int *  datalen 
) [static]

Definition at line 363 of file chan_local.c.

References ao2_lock, ao2_unlock, ast_bridged_channel(), ast_channel_lock, ast_channel_queryoption(), ast_channel_ref, ast_channel_unlock, ast_channel_unref, AST_OPTION_T38_STATE, local_pvt::chan, IS_OUTBOUND, local_pvt::owner, and ast_channel::tech_pvt.

00364 {
00365    struct local_pvt *p;
00366    struct ast_channel *bridged = NULL;
00367    struct ast_channel *tmp = NULL;
00368    int res = 0;
00369 
00370    if (option != AST_OPTION_T38_STATE) {
00371       /* AST_OPTION_T38_STATE is the only supported option at this time */
00372       return -1;
00373    }
00374 
00375    /* for some reason the channel is not locked in channel.c when this function is called */
00376    if (!(p = ast->tech_pvt)) {
00377       return -1;
00378    }
00379 
00380    ao2_lock(p);
00381    if (!(tmp = IS_OUTBOUND(ast, p) ? p->owner : p->chan)) {
00382       ao2_unlock(p);
00383       return -1;
00384    }
00385    ast_channel_ref(tmp);
00386    ao2_unlock(p);
00387    ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
00388 
00389    ast_channel_lock(tmp);
00390    if (!(bridged = ast_bridged_channel(tmp))) {
00391       res = -1;
00392       ast_channel_unlock(tmp);
00393       goto query_cleanup;
00394    }
00395    ast_channel_ref(bridged);
00396    ast_channel_unlock(tmp);
00397 
00398 query_cleanup:
00399    if (bridged) {
00400       res = ast_channel_queryoption(bridged, option, data, datalen, 0);
00401       bridged = ast_channel_unref(bridged);
00402    }
00403    if (tmp) {
00404       tmp = ast_channel_unref(tmp);
00405    }
00406    ast_channel_lock(ast); /* Lock back before we leave */
00407 
00408    return res;
00409 }

static int local_queue_frame ( struct local_pvt *  p,
int  isoutbound,
struct ast_frame *  f,
struct ast_channel *  us,
int  us_locked 
) [static]

queue a frame on a to either the p->owner or p->chan

Note:
the local_pvt MUST have it's ref count bumped before entering this function and decremented after this function is called. This is a side effect of the deadlock avoidance that is necessary to lock 2 channels and a tech_pvt. Without a ref counted local_pvt, it is impossible to guarantee it will not be destroyed by another thread during deadlock avoidance.

Definition at line 419 of file chan_local.c.

References ao2_lock, ao2_unlock, ast_channel_lock, ast_channel_ref, ast_channel_unlock, ast_channel_unref, AST_CONTROL_RINGING, AST_FRAME_CONTROL, ast_queue_frame(), ast_setstate(), AST_STATE_RINGING, local_pvt::chan, ast_frame::frametype, ast_channel::generator, ast_frame_subclass::integer, local_pvt::owner, and ast_frame::subclass.

Referenced by local_answer(), local_digit_begin(), local_digit_end(), local_hangup(), local_indicate(), local_sendhtml(), local_sendtext(), and local_write().

00421 {
00422    struct ast_channel *other = NULL;
00423 
00424    /* Recalculate outbound channel */
00425    other = isoutbound ? p->owner : p->chan;
00426 
00427    if (!other) {
00428       return 0;
00429    }
00430 
00431    /* do not queue frame if generator is on both local channels */
00432    if (us && us->generator && other->generator) {
00433       return 0;
00434    }
00435 
00436    /* grab a ref on the channel before unlocking the pvt,
00437     * other can not go away from us now regardless of locking */
00438    ast_channel_ref(other);
00439    if (us && us_locked) {
00440       ast_channel_unlock(us);
00441    }
00442    ao2_unlock(p);
00443 
00444    if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_RINGING) {
00445       ast_setstate(other, AST_STATE_RINGING);
00446    }
00447    ast_queue_frame(other, f);
00448 
00449    other = ast_channel_unref(other);
00450    if (us && us_locked) {
00451       ast_channel_lock(us);
00452    }
00453    ao2_lock(p);
00454 
00455    return 0;
00456 }

static struct ast_frame * local_read ( struct ast_channel *  ast  )  [static, read]

Definition at line 634 of file chan_local.c.

References ast_null_frame.

00635 {
00636    return &ast_null_frame;
00637 }

static struct ast_channel * local_request ( const char *  type,
format_t  format,
const struct ast_channel *  requestor,
void *  data,
int *  cause 
) [static, read]

Part of PBX interface.

Definition at line 1238 of file chan_local.c.

References ao2_ref, ao2_unlink, ast_channel_cc_params_init(), ast_channel_get_cc_config_params(), ast_channel_release(), ast_module_user_remove, AST_STATE_DOWN, local_pvt::chan, local_alloc(), local_new(), local_pvt::owner, local_pvt::u_chan, and local_pvt::u_owner.

01239 {
01240    struct local_pvt *p;
01241    struct ast_channel *chan;
01242 
01243    /* Allocate a new private structure and then Asterisk channels */
01244    p = local_alloc(data, format);
01245    if (!p) {
01246       return NULL;
01247    }
01248    chan = local_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
01249    if (!chan) {
01250       ao2_unlink(locals, p);
01251    } else if (ast_channel_cc_params_init(chan, requestor ? ast_channel_get_cc_config_params((struct ast_channel *)requestor) : NULL)) {
01252       ao2_unlink(locals, p);
01253       p->owner = ast_channel_release(p->owner);
01254       ast_module_user_remove(p->u_owner);
01255       p->chan = ast_channel_release(p->chan);
01256       ast_module_user_remove(p->u_chan);
01257       chan = NULL;
01258    }
01259    ao2_ref(p, -1); /* kill the ref from the alloc */
01260 
01261    return chan;
01262 }

static int local_sendhtml ( struct ast_channel *  ast,
int  subclass,
const char *  data,
int  datalen 
) [static]

Definition at line 851 of file chan_local.c.

References ao2_lock, ao2_ref, ao2_unlock, AST_FRAME_HTML, ast_frame::data, ast_frame::datalen, ast_frame_subclass::integer, IS_OUTBOUND, local_queue_frame(), ast_frame::ptr, ast_frame::subclass, and ast_channel::tech_pvt.

00852 {
00853    struct local_pvt *p = ast->tech_pvt;
00854    int res = -1;
00855    struct ast_frame f = { AST_FRAME_HTML, };
00856    int isoutbound;
00857 
00858    if (!p)
00859       return -1;
00860 
00861    ao2_lock(p);
00862    ao2_ref(p, 1); /* ref for local_queue_frame */
00863    isoutbound = IS_OUTBOUND(ast, p);
00864    f.subclass.integer = subclass;
00865    f.data.ptr = (char *)data;
00866    f.datalen = datalen;
00867    res = local_queue_frame(p, isoutbound, &f, ast, 0);
00868    ao2_unlock(p);
00869    ao2_ref(p, -1);
00870 
00871    return res;
00872 }

static int local_sendtext ( struct ast_channel *  ast,
const char *  text 
) [static]

Definition at line 830 of file chan_local.c.

References ao2_lock, ao2_ref, ao2_unlock, AST_FRAME_TEXT, ast_frame::data, ast_frame::datalen, IS_OUTBOUND, local_queue_frame(), ast_frame::ptr, and ast_channel::tech_pvt.

00831 {
00832    struct local_pvt *p = ast->tech_pvt;
00833    int res = -1;
00834    struct ast_frame f = { AST_FRAME_TEXT, };
00835    int isoutbound;
00836 
00837    if (!p)
00838       return -1;
00839 
00840    ao2_lock(p);
00841    ao2_ref(p, 1); /* ref for local_queue_frame */
00842    isoutbound = IS_OUTBOUND(ast, p);
00843    f.data.ptr = (char *) text;
00844    f.datalen = strlen(text) + 1;
00845    res = local_queue_frame(p, isoutbound, &f, ast, 0);
00846    ao2_unlock(p);
00847    ao2_ref(p, -1);
00848    return res;
00849 }

static int local_setoption ( struct ast_channel *  chan,
int  option,
void *  data,
int  datalen 
) [static]

Definition at line 228 of file chan_local.c.

References ao2_lock, ao2_ref, ao2_unlock, AST_CHAN_WRITE_INFO_T_VERSION, ast_channel_lock, ast_channel_ref, ast_channel_unlock, ast_channel_unref, ast_log(), AST_OPTION_CHANNEL_WRITE, local_pvt::chan, ast_chan_write_info_t::chan, ast_chan_write_info_t::data, ast_chan_write_info_t::function, LOG_ERROR, local_pvt::owner, ast_channel::tech_pvt, ast_chan_write_info_t::value, ast_chan_write_info_t::version, and ast_chan_write_info_t::write_fn.

00229 {
00230    int res = 0;
00231    struct local_pvt *p = NULL;
00232    struct ast_channel *otherchan = NULL;
00233    ast_chan_write_info_t *write_info;
00234 
00235    if (option != AST_OPTION_CHANNEL_WRITE) {
00236       return -1;
00237    }
00238 
00239    write_info = data;
00240 
00241    if (write_info->version != AST_CHAN_WRITE_INFO_T_VERSION) {
00242       ast_log(LOG_ERROR, "The chan_write_info_t type has changed, and this channel hasn't been updated!\n");
00243       return -1;
00244    }
00245 
00246    /* get the tech pvt */
00247    if (!(p = ast->tech_pvt)) {
00248       return -1;
00249    }
00250    ao2_ref(p, 1);
00251    ast_channel_unlock(ast); /* Held when called, unlock before locking another channel */
00252 
00253    /* get the channel we are supposed to write to */
00254    ao2_lock(p);
00255    otherchan = (write_info->chan == p->owner) ? p->chan : p->owner;
00256    if (!otherchan || otherchan == write_info->chan) {
00257       res = -1;
00258       otherchan = NULL;
00259       ao2_unlock(p);
00260       goto setoption_cleanup;
00261    }
00262    ast_channel_ref(otherchan);
00263 
00264    /* clear the pvt lock before grabbing the channel */
00265    ao2_unlock(p);
00266 
00267    ast_channel_lock(otherchan);
00268    res = write_info->write_fn(otherchan, write_info->function, write_info->data, write_info->value);
00269    ast_channel_unlock(otherchan);
00270 
00271 setoption_cleanup:
00272    if (p) {
00273       ao2_ref(p, -1);
00274    }
00275    if (otherchan) {
00276       ast_channel_unref(otherchan);
00277    }
00278    ast_channel_lock(ast); /* Lock back before we leave */
00279    return res;
00280 }

static int local_write ( struct ast_channel *  ast,
struct ast_frame *  f 
) [static]

Definition at line 639 of file chan_local.c.

References ao2_lock, ao2_ref, ao2_unlock, ast_debug, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_test_flag, check_bridge(), ast_frame::frametype, IS_OUTBOUND, LOCAL_ALREADY_MASQED, local_queue_frame(), and ast_channel::tech_pvt.

00640 {
00641    struct local_pvt *p = ast->tech_pvt;
00642    int res = -1;
00643    int isoutbound;
00644 
00645    if (!p) {
00646       return -1;
00647    }
00648 
00649    /* Just queue for delivery to the other side */
00650    ao2_ref(p, 1); /* ref for local_queue_frame */
00651    ao2_lock(p);
00652    isoutbound = IS_OUTBOUND(ast, p);
00653 
00654    if (isoutbound
00655       && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
00656       check_bridge(ast, p);
00657    }
00658 
00659    if (!ast_test_flag(p, LOCAL_ALREADY_MASQED)) {
00660       res = local_queue_frame(p, isoutbound, f, ast, 1);
00661    } else {
00662       ast_debug(1, "Not posting to '%s' queue since already masqueraded out\n",
00663          ast->name);
00664       res = 0;
00665    }
00666    ao2_unlock(p);
00667    ao2_ref(p, -1);
00668 
00669    return res;
00670 }

static int locals_cmp_cb ( void *  obj,
void *  arg,
int  flags 
) [static]

Definition at line 1354 of file chan_local.c.

References CMP_MATCH.

Referenced by load_module().

01355 {
01356    return (obj == arg) ? CMP_MATCH : 0;
01357 }

static char* locals_show ( struct ast_cli_entry *  e,
int  cmd,
struct ast_cli_args *  a 
) [static]

CLI command "local show channels".

Definition at line 1265 of file chan_local.c.

References ao2_container_count(), ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_lock, ao2_ref, ao2_unlock, ast_cli_args::argc, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, local_pvt::context, local_pvt::exten, ast_cli_args::fd, local_pvt::owner, RESULT_SUCCESS, and ast_cli_entry::usage.

01266 {
01267    struct local_pvt *p = NULL;
01268    struct ao2_iterator it;
01269 
01270    switch (cmd) {
01271    case CLI_INIT:
01272       e->command = "local show channels";
01273       e->usage =
01274          "Usage: local show channels\n"
01275          "       Provides summary information on active local proxy channels.\n";
01276       return NULL;
01277    case CLI_GENERATE:
01278       return NULL;
01279    }
01280 
01281    if (a->argc != 3)
01282       return CLI_SHOWUSAGE;
01283 
01284    if (ao2_container_count(locals) == 0) {
01285       ast_cli(a->fd, "No local channels in use\n");
01286       return RESULT_SUCCESS;
01287    }
01288 
01289    it = ao2_iterator_init(locals, 0);
01290    while ((p = ao2_iterator_next(&it))) {
01291       ao2_lock(p);
01292       ast_cli(a->fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
01293       ao2_unlock(p);
01294       ao2_ref(p, -1);
01295    }
01296    ao2_iterator_destroy(&it);
01297 
01298    return CLI_SUCCESS;
01299 }

static int manager_optimize_away ( struct mansession *  s,
const struct message *  m 
) [static]

Definition at line 1305 of file chan_local.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_lock, ao2_ref, ao2_unlock, ast_channel_get_by_name(), ast_channel_unref, ast_clear_flag, ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), LOCAL_NO_OPTIMIZATION, and ast_channel::tech_pvt.

Referenced by load_module().

01306 {
01307    const char *channel;
01308    struct local_pvt *p, *tmp = NULL;
01309    struct ast_channel *c;
01310    int found = 0;
01311    struct ao2_iterator it;
01312 
01313    channel = astman_get_header(m, "Channel");
01314 
01315    if (ast_strlen_zero(channel)) {
01316       astman_send_error(s, m, "'Channel' not specified.");
01317       return 0;
01318    }
01319 
01320    c = ast_channel_get_by_name(channel);
01321    if (!c) {
01322       astman_send_error(s, m, "Channel does not exist.");
01323       return 0;
01324    }
01325 
01326    p = c->tech_pvt;
01327    ast_channel_unref(c);
01328    c = NULL;
01329 
01330    it = ao2_iterator_init(locals, 0);
01331    while ((tmp = ao2_iterator_next(&it))) {
01332       if (tmp == p) {
01333          ao2_lock(tmp);
01334          found = 1;
01335          ast_clear_flag(tmp, LOCAL_NO_OPTIMIZATION);
01336          ao2_unlock(tmp);
01337          ao2_ref(tmp, -1);
01338          break;
01339       }
01340       ao2_ref(tmp, -1);
01341    }
01342    ao2_iterator_destroy(&it);
01343 
01344    if (found) {
01345       astman_send_ack(s, m, "Queued channel to be optimized away");
01346    } else {
01347       astman_send_error(s, m, "Unable to find channel");
01348    }
01349 
01350    return 0;
01351 }

static int unload_module ( void   )  [static]

Unload the local proxy channel from Asterisk.

Definition at line 1379 of file chan_local.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_channel_unregister(), ast_cli_unregister_multiple(), ast_manager_unregister(), ast_softhangup(), AST_SOFTHANGUP_APPUNLOAD, and local_pvt::owner.

01380 {
01381    struct local_pvt *p = NULL;
01382    struct ao2_iterator it;
01383 
01384    /* First, take us out of the channel loop */
01385    ast_cli_unregister_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
01386    ast_manager_unregister("LocalOptimizeAway");
01387    ast_channel_unregister(&local_tech);
01388 
01389    it = ao2_iterator_init(locals, 0);
01390    while ((p = ao2_iterator_next(&it))) {
01391       if (p->owner) {
01392          ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
01393       }
01394       ao2_ref(p, -1);
01395    }
01396    ao2_iterator_destroy(&it);
01397    ao2_ref(locals, -1);
01398 
01399    return 0;
01400 }


Variable Documentation

const int BUCKET_SIZE = 1 [static]

Definition at line 83 of file chan_local.c.

struct ast_cli_entry cli_local[] [static]
Initial value:
 {
   AST_CLI_DEFINE(locals_show, "List status of local channels"),
}

Definition at line 1301 of file chan_local.c.

struct ast_jb_conf g_jb_conf [static]

Definition at line 89 of file chan_local.c.

struct ast_channel_tech local_tech [static]

Definition at line 115 of file chan_local.c.

struct ao2_container* locals [static]

Definition at line 85 of file chan_local.c.

unsigned int name_sequence = 0 [static]

Definition at line 87 of file chan_local.c.

const char tdesc[] = "Local Proxy Channel Driver" [static]

Definition at line 76 of file chan_local.c.


Generated on 18 Jan 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1