Sat Mar 26 11:15:47 2011

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 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...
struct  locals

Defines

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

Functions

static void __reg_module (void)
static void __unreg_module (void)
static void check_bridge (struct local_pvt *p)
static int load_module (void)
 Load module into PBX, register channel.
static struct local_pvt * local_alloc (const char *data, int 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 struct local_pvt * local_pvt_destroy (struct local_pvt *pvt)
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)
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 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 struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Local Proxy Channel (Note: used internally by other modules)" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DRIVER, }
static struct ast_module_info * ast_module_info = &__mod_info
static struct ast_cli_entry cli_local []
static struct ast_jb_conf g_jb_conf
static struct ast_channel_tech local_tech
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 << 2)

Already masqueraded

Definition at line 148 of file chan_local.c.

Referenced by check_bridge(), and local_write().

#define LOCAL_BRIDGE   (1 << 5)

Report back the "true" channel as being bridged to

Definition at line 151 of file chan_local.c.

Referenced by local_alloc(), and local_bridgedchannel().

#define LOCAL_CANCEL_QUEUE   (1 << 1)

Cancel queue

Definition at line 147 of file chan_local.c.

Referenced by local_hangup(), and local_queue_frame().

#define LOCAL_GLARE_DETECT   (1 << 0)

Detect glare on hangup

Definition at line 146 of file chan_local.c.

Referenced by local_hangup(), and local_queue_frame().

#define LOCAL_LAUNCHED_PBX   (1 << 3)

PBX was launched

Definition at line 149 of file chan_local.c.

Referenced by local_call(), and local_hangup().

#define LOCAL_MOH_PASSTHRU   (1 << 6)

Pass through music on hold start/stop frames

Definition at line 152 of file chan_local.c.

Referenced by local_alloc(), and local_indicate().

#define LOCAL_NO_OPTIMIZATION   (1 << 4)

Do not optimize using masquerading

Definition at line 150 of file chan_local.c.

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


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 1200 of file chan_local.c.

static void __unreg_module ( void   )  [static]

Definition at line 1200 of file chan_local.c.

static void check_bridge ( struct local_pvt *  p  )  [static]

Definition at line 443 of file chan_local.c.

References ast_channel::_bridge, ast_party_caller::ani, ast_app_group_update(), ast_bridged_channel(), ast_channel_masquerade(), ast_channel_trylock, ast_channel_unlock, ast_check_hangup(), AST_LIST_EMPTY, ast_set_flag, ast_test_flag, ast_channel::audiohooks, ast_channel::caller, local_pvt::chan, ast_channel::dialed, 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::readq, 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().

00444 {
00445    struct ast_channel_monitor *tmp;
00446    if (ast_test_flag(p, LOCAL_ALREADY_MASQED) || ast_test_flag(p, LOCAL_NO_OPTIMIZATION) || !p->chan || !p->owner || (p->chan->_bridge != ast_bridged_channel(p->chan)))
00447       return;
00448 
00449    /* only do the masquerade if we are being called on the outbound channel,
00450       if it has been bridged to another channel and if there are no pending
00451       frames on the owner channel (because they would be transferred to the
00452       outbound channel during the masquerade)
00453    */
00454    if (p->chan->_bridge /* Not ast_bridged_channel!  Only go one step! */ && AST_LIST_EMPTY(&p->owner->readq)) {
00455       /* Masquerade bridged channel into owner */
00456       /* Lock everything we need, one by one, and give up if
00457          we can't get everything.  Remember, we'll get another
00458          chance in just a little bit */
00459       if (!ast_channel_trylock(p->chan->_bridge)) {
00460          if (!ast_check_hangup(p->chan->_bridge)) {
00461             if (!ast_channel_trylock(p->owner)) {
00462                if (!ast_check_hangup(p->owner)) {
00463                   if (p->owner->monitor && !p->chan->_bridge->monitor) {
00464                      /* If a local channel is being monitored, we don't want a masquerade
00465                       * to cause the monitor to go away. Since the masquerade swaps the monitors,
00466                       * pre-swapping the monitors before the masquerade will ensure that the monitor
00467                       * ends up where it is expected.
00468                       */
00469                      tmp = p->owner->monitor;
00470                      p->owner->monitor = p->chan->_bridge->monitor;
00471                      p->chan->_bridge->monitor = tmp;
00472                   }
00473                   if (p->chan->audiohooks) {
00474                      struct ast_audiohook_list *audiohooks_swapper;
00475                      audiohooks_swapper = p->chan->audiohooks;
00476                      p->chan->audiohooks = p->owner->audiohooks;
00477                      p->owner->audiohooks = audiohooks_swapper;
00478                   }
00479 
00480                   /* If any Caller ID was set, preserve it after masquerade like above. We must check
00481                    * to see if Caller ID was set because otherwise we'll mistakingly copy info not
00482                    * set from the dialplan and will overwrite the real channel Caller ID. The reason
00483                    * for this whole preswapping action is because the Caller ID is set on the channel
00484                    * thread (which is the to be masqueraded away local channel) before both local
00485                    * channels are optimized away.
00486                    */
00487                   if (p->owner->caller.id.name.valid || p->owner->caller.id.number.valid
00488                      || p->owner->caller.id.subaddress.valid || p->owner->caller.ani.name.valid
00489                      || p->owner->caller.ani.number.valid || p->owner->caller.ani.subaddress.valid) {
00490                      struct ast_party_caller tmp;
00491                      tmp = p->owner->caller;
00492                      p->owner->caller = p->chan->_bridge->caller;
00493                      p->chan->_bridge->caller = tmp;
00494                   }
00495                   if (p->owner->redirecting.from.name.valid || p->owner->redirecting.from.number.valid
00496                      || p->owner->redirecting.from.subaddress.valid || p->owner->redirecting.to.name.valid
00497                      || p->owner->redirecting.to.number.valid || p->owner->redirecting.to.subaddress.valid) {
00498                      struct ast_party_redirecting tmp;
00499                      tmp = p->owner->redirecting;
00500                      p->owner->redirecting = p->chan->_bridge->redirecting;
00501                      p->chan->_bridge->redirecting = tmp;
00502                   }
00503                   if (p->owner->dialed.number.str || p->owner->dialed.subaddress.valid) {
00504                      struct ast_party_dialed tmp;
00505                      tmp = p->owner->dialed;
00506                      p->owner->dialed = p->chan->_bridge->dialed;
00507                      p->chan->_bridge->dialed = tmp;
00508                   }
00509 
00510 
00511                   ast_app_group_update(p->chan, p->owner);
00512                   ast_channel_masquerade(p->owner, p->chan->_bridge);
00513                   ast_set_flag(p, LOCAL_ALREADY_MASQED);
00514                }
00515                ast_channel_unlock(p->owner);
00516             }
00517             ast_channel_unlock(p->chan->_bridge);
00518          }
00519       }
00520    }
00521 }

static int load_module ( void   )  [static]

Load module into PBX, register channel.

Definition at line 1160 of file chan_local.c.

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

01161 {
01162    /* Make sure we can register our channel type */
01163    if (ast_channel_register(&local_tech)) {
01164       ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
01165       return AST_MODULE_LOAD_FAILURE;
01166    }
01167    ast_cli_register_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
01168    ast_manager_register_xml("LocalOptimizeAway", EVENT_FLAG_SYSTEM|EVENT_FLAG_CALL, manager_optimize_away);
01169 
01170    return AST_MODULE_LOAD_SUCCESS;
01171 }

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

Create a call structure.

Definition at line 922 of file chan_local.c.

References ast_calloc, ast_copy_string(), ast_exists_extension(), AST_JB_ENABLED, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_UNLOCK, ast_log(), ast_mutex_init, ast_set_flag, ast_test_flag, local_pvt::context, local_pvt::exten, local_pvt::jb_conf, LOCAL_BRIDGE, LOCAL_MOH_PASSTHRU, LOCAL_NO_OPTIMIZATION, local_pvt_destroy(), local_pvt::lock, LOG_ERROR, LOG_NOTICE, and local_pvt::reqformat.

Referenced by local_request().

00923 {
00924    struct local_pvt *tmp = NULL;
00925    char *c = NULL, *opts = NULL;
00926 
00927    if (!(tmp = ast_calloc(1, sizeof(*tmp))))
00928       return NULL;
00929 
00930    /* Initialize private structure information */
00931    ast_mutex_init(&tmp->lock);
00932    ast_copy_string(tmp->exten, data, sizeof(tmp->exten));
00933 
00934    memcpy(&tmp->jb_conf, &g_jb_conf, sizeof(tmp->jb_conf));
00935 
00936    /* Look for options */
00937    if ((opts = strchr(tmp->exten, '/'))) {
00938       *opts++ = '\0';
00939       if (strchr(opts, 'n'))
00940          ast_set_flag(tmp, LOCAL_NO_OPTIMIZATION);
00941       if (strchr(opts, 'j')) {
00942          if (ast_test_flag(tmp, LOCAL_NO_OPTIMIZATION))
00943             ast_set_flag(&tmp->jb_conf, AST_JB_ENABLED);
00944          else {
00945             ast_log(LOG_ERROR, "You must use the 'n' option for chan_local "
00946                "to use the 'j' option to enable the jitterbuffer\n");
00947          }
00948       }
00949       if (strchr(opts, 'b')) {
00950          ast_set_flag(tmp, LOCAL_BRIDGE);
00951       }
00952       if (strchr(opts, 'm')) {
00953          ast_set_flag(tmp, LOCAL_MOH_PASSTHRU);
00954       }
00955    }
00956 
00957    /* Look for a context */
00958    if ((c = strchr(tmp->exten, '@')))
00959       *c++ = '\0';
00960 
00961    ast_copy_string(tmp->context, c ? c : "default", sizeof(tmp->context));
00962 
00963    tmp->reqformat = format;
00964 
00965 #if 0
00966    /* We can't do this check here, because we don't know the CallerID yet, and
00967     * the CallerID could potentially affect what step is actually taken (or
00968     * even if that step exists). */
00969    if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
00970       ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
00971       tmp = local_pvt_destroy(tmp);
00972    } else {
00973 #endif
00974       /* Add to list */
00975       AST_LIST_LOCK(&locals);
00976       AST_LIST_INSERT_HEAD(&locals, tmp, list);
00977       AST_LIST_UNLOCK(&locals);
00978 #if 0
00979    }
00980 #endif
00981    
00982    return tmp;
00983 }

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

Definition at line 417 of file chan_local.c.

References AST_CONTROL_ANSWER, AST_FRAME_CONTROL, ast_log(), ast_mutex_lock, ast_mutex_unlock, IS_OUTBOUND, local_queue_frame(), local_pvt::lock, LOG_WARNING, and ast_channel::tech_pvt.

00418 {
00419    struct local_pvt *p = ast->tech_pvt;
00420    int isoutbound;
00421    int res = -1;
00422 
00423    if (!p)
00424       return -1;
00425 
00426    ast_mutex_lock(&p->lock);
00427    isoutbound = IS_OUTBOUND(ast, p);
00428    if (isoutbound) {
00429       /* Pass along answer since somebody answered us */
00430       struct ast_frame answer = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
00431       res = local_queue_frame(p, isoutbound, &answer, ast, 1);
00432    } else
00433       ast_log(LOG_WARNING, "Huh?  Local is being asked to answer?\n");
00434    if (!res)
00435       ast_mutex_unlock(&p->lock);
00436    return res;
00437 }

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 270 of file chan_local.c.

References ast_channel::_bridge, ast_debug, ast_mutex_lock, ast_mutex_unlock, ast_test_flag, local_pvt::chan, LOCAL_BRIDGE, local_pvt::lock, ast_channel::name, local_pvt::owner, and ast_channel::tech_pvt.

00271 {
00272    struct local_pvt *p = bridge->tech_pvt;
00273    struct ast_channel *bridged = bridge;
00274 
00275    if (!p) {
00276       ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning <none>\n",
00277          chan->name, bridge->name);
00278       return NULL;
00279    }
00280 
00281    ast_mutex_lock(&p->lock);
00282 
00283    if (ast_test_flag(p, LOCAL_BRIDGE)) {
00284       /* Find the opposite channel */
00285       bridged = (bridge == p->owner ? p->chan : p->owner);
00286       
00287       /* Now see if the opposite channel is bridged to anything */
00288       if (!bridged) {
00289          bridged = bridge;
00290       } else if (bridged->_bridge) {
00291          bridged = bridged->_bridge;
00292       }
00293    }
00294 
00295    ast_mutex_unlock(&p->lock);
00296 
00297    return bridged;
00298 }

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 730 of file chan_local.c.

References ast_channel::accountcode, accountcode, 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_trylock, ast_channel_unlock, 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_mutex_trylock, ast_mutex_unlock, 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, ast_channel::caller, local_pvt::chan, ast_channel::connected, ast_channel::context, ast_channel::dialed, ast_var_t::entries, ast_channel::exten, ast_party_caller::id, ast_channel::language, language, len(), LOCAL_LAUNCHED_PBX, local_pvt::lock, LOG_NOTICE, ast_channel::musicclass, musicclass, ast_var_t::name, ast_party_id::number, local_pvt::owner, ast_channel::redirecting, S_COR, ast_party_number::str, ast_channel::tech_pvt, ast_party_number::valid, and ast_channel::varshead.

00731 {
00732    struct local_pvt *p = ast->tech_pvt;
00733    int res;
00734    struct ast_var_t *varptr = NULL, *new;
00735    size_t len, namelen;
00736    char *reduced_dest = ast_strdupa(dest);
00737    char *slash;
00738 
00739    if (!p)
00740       return -1;
00741 
00742    /* If you value your sanity, please don't look at this code */
00743 start_over:
00744    while (ast_channel_trylock(p->chan)) {
00745       ast_channel_unlock(p->owner);
00746       usleep(1);
00747       ast_channel_lock(p->owner);
00748    }
00749 
00750    /* p->owner and p->chan are locked now. Let's get p locked */
00751    if (ast_mutex_trylock(&p->lock)) {
00752       /* @#$&$@ */
00753       ast_channel_unlock(p->chan);
00754       ast_channel_unlock(p->owner);
00755       usleep(1);
00756       ast_channel_lock(p->owner);
00757       goto start_over;
00758    }
00759 
00760    /*
00761     * Note that cid_num and cid_name aren't passed in the ast_channel_alloc
00762     * call, so it's done here instead.
00763     *
00764     * All these failure points just return -1. The individual strings will
00765     * be cleared when we destroy the channel.
00766     */
00767    ast_party_redirecting_copy(&p->chan->redirecting, &p->owner->redirecting);
00768 
00769    ast_party_dialed_copy(&p->chan->dialed, &p->owner->dialed);
00770 
00771    ast_connected_line_copy_to_caller(&p->chan->caller, &p->owner->connected);
00772    ast_connected_line_copy_from_caller(&p->chan->connected, &p->owner->caller);
00773 
00774    ast_string_field_set(p->chan, language, p->owner->language);
00775    ast_string_field_set(p->chan, accountcode, p->owner->accountcode);
00776    ast_string_field_set(p->chan, musicclass, p->owner->musicclass);
00777    ast_cdr_update(p->chan);
00778 
00779    ast_channel_cc_params_init(p->chan, ast_channel_get_cc_config_params(p->owner));
00780 
00781    if (!ast_exists_extension(NULL, p->chan->context, p->chan->exten, 1,
00782       S_COR(p->owner->caller.id.number.valid, p->owner->caller.id.number.str, NULL))) {
00783       ast_log(LOG_NOTICE, "No such extension/context %s@%s while calling Local channel\n", p->chan->exten, p->chan->context);
00784       ast_mutex_unlock(&p->lock);
00785       ast_channel_unlock(p->chan);
00786       return -1;
00787    }
00788 
00789    /* Make sure we inherit the ANSWERED_ELSEWHERE flag if it's set on the queue/dial call request in the dialplan */
00790    if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
00791       ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
00792    }
00793 
00794    /* copy the channel variables from the incoming channel to the outgoing channel */
00795    /* Note that due to certain assumptions, they MUST be in the same order */
00796    AST_LIST_TRAVERSE(&p->owner->varshead, varptr, entries) {
00797       namelen = strlen(varptr->name);
00798       len = sizeof(struct ast_var_t) + namelen + strlen(varptr->value) + 2;
00799       if ((new = ast_calloc(1, len))) {
00800          memcpy(new, varptr, len);
00801          new->value = &(new->name[0]) + namelen + 1;
00802          AST_LIST_INSERT_TAIL(&p->chan->varshead, new, entries);
00803       }
00804    }
00805    ast_channel_datastore_inherit(p->owner, p->chan);
00806    /* If the local channel has /n or /b on the end of it,
00807     * we need to lop that off for our argument to setting
00808     * up the CC_INTERFACES variable
00809     */
00810    if ((slash = strrchr(reduced_dest, '/'))) {
00811       *slash = '\0';
00812    }
00813    ast_set_cc_interfaces_chanvar(p->chan, reduced_dest);
00814 
00815    /* Start switch on sub channel */
00816    if (!(res = ast_pbx_start(p->chan)))
00817       ast_set_flag(p, LOCAL_LAUNCHED_PBX);
00818 
00819    ast_mutex_unlock(&p->lock);
00820    ast_channel_unlock(p->chan);
00821    return res;
00822 }

static int local_devicestate ( void *  data  )  [static]

Adds devicestate to local channels.

Definition at line 222 of file chan_local.c.

References ast_debug, AST_DEVICE_INUSE, AST_DEVICE_INVALID, AST_DEVICE_NOT_INUSE, ast_exists_extension(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_strdupa, local_pvt::context, local_pvt::exten, LOG_WARNING, and local_pvt::owner.

00223 {
00224    char *exten = ast_strdupa(data);
00225    char *context = NULL, *opts = NULL;
00226    int res;
00227    struct local_pvt *lp;
00228 
00229    if (!(context = strchr(exten, '@'))) {
00230       ast_log(LOG_WARNING, "Someone used Local/%s somewhere without a @context. This is bad.\n", exten);
00231       return AST_DEVICE_INVALID; 
00232    }
00233 
00234    *context++ = '\0';
00235 
00236    /* Strip options if they exist */
00237    if ((opts = strchr(context, '/')))
00238       *opts = '\0';
00239 
00240    ast_debug(3, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
00241 
00242    res = ast_exists_extension(NULL, context, exten, 1, NULL);
00243    if (!res)      
00244       return AST_DEVICE_INVALID;
00245    
00246    res = AST_DEVICE_NOT_INUSE;
00247    AST_LIST_LOCK(&locals);
00248    AST_LIST_TRAVERSE(&locals, lp, list) {
00249       if (!strcmp(exten, lp->exten) && !strcmp(context, lp->context) && lp->owner) {
00250          res = AST_DEVICE_INUSE;
00251          break;
00252       }
00253    }
00254    AST_LIST_UNLOCK(&locals);
00255 
00256    return res;
00257 }

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

Definition at line 650 of file chan_local.c.

References AST_FRAME_DTMF_BEGIN, ast_mutex_lock, ast_mutex_unlock, ast_frame_subclass::integer, IS_OUTBOUND, local_queue_frame(), local_pvt::lock, ast_frame::subclass, and ast_channel::tech_pvt.

00651 {
00652    struct local_pvt *p = ast->tech_pvt;
00653    int res = -1;
00654    struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
00655    int isoutbound;
00656 
00657    if (!p)
00658       return -1;
00659 
00660    ast_mutex_lock(&p->lock);
00661    isoutbound = IS_OUTBOUND(ast, p);
00662    f.subclass.integer = digit;
00663    if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00664       ast_mutex_unlock(&p->lock);
00665 
00666    return res;
00667 }

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

Definition at line 669 of file chan_local.c.

References AST_FRAME_DTMF_END, ast_mutex_lock, ast_mutex_unlock, ast_frame_subclass::integer, IS_OUTBOUND, ast_frame::len, local_queue_frame(), local_pvt::lock, ast_frame::subclass, and ast_channel::tech_pvt.

00670 {
00671    struct local_pvt *p = ast->tech_pvt;
00672    int res = -1;
00673    struct ast_frame f = { AST_FRAME_DTMF_END, };
00674    int isoutbound;
00675 
00676    if (!p)
00677       return -1;
00678 
00679    ast_mutex_lock(&p->lock);
00680    isoutbound = IS_OUTBOUND(ast, p);
00681    f.subclass.integer = digit;
00682    f.len = duration;
00683    if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00684       ast_mutex_unlock(&p->lock);
00685 
00686    return res;
00687 }

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

Definition at line 553 of file chan_local.c.

References ast_channel::_bridge, ast_check_hangup(), ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_queue_hangup(), local_pvt::chan, local_pvt::lock, LOG_WARNING, local_pvt::owner, and ast_channel::tech_pvt.

00554 {
00555    struct local_pvt *p = newchan->tech_pvt;
00556 
00557    if (!p)
00558       return -1;
00559 
00560    ast_mutex_lock(&p->lock);
00561 
00562    if ((p->owner != oldchan) && (p->chan != oldchan)) {
00563       ast_log(LOG_WARNING, "Old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
00564       ast_mutex_unlock(&p->lock);
00565       return -1;
00566    }
00567    if (p->owner == oldchan)
00568       p->owner = newchan;
00569    else
00570       p->chan = newchan;
00571 
00572    /* Do not let a masquerade cause a Local channel to be bridged to itself! */
00573    if (!ast_check_hangup(newchan) && (p->owner->_bridge == p->chan || p->chan->_bridge == p->owner)) {
00574       ast_log(LOG_WARNING, "You can not bridge a Local channel to itself!\n");
00575       ast_mutex_unlock(&p->lock);
00576       ast_queue_hangup(newchan);
00577       return -1;
00578    }
00579 
00580    ast_mutex_unlock(&p->lock);
00581    return 0;
00582 }

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

Hangup a call through the local proxy channel.

Definition at line 825 of file chan_local.c.

References ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_clear_flag, AST_CONTROL_HANGUP, ast_debug, AST_FLAG_ANSWERED_ELSEWHERE, AST_FRAME_CONTROL, ast_hangup(), AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, ast_module_user_remove, ast_mutex_lock, ast_mutex_unlock, ast_queue_hangup(), ast_set_flag, ast_test_flag, local_pvt::chan, ast_channel::hangupcause, IS_OUTBOUND, LOCAL_CANCEL_QUEUE, LOCAL_GLARE_DETECT, LOCAL_LAUNCHED_PBX, local_pvt_destroy(), local_queue_frame(), local_pvt::lock, 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.

00826 {
00827    struct local_pvt *p = ast->tech_pvt;
00828    int isoutbound;
00829    struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_HANGUP }, .data.uint32 = ast->hangupcause };
00830    struct ast_channel *ochan = NULL;
00831    int glaredetect = 0, res = 0;
00832 
00833    if (!p)
00834       return -1;
00835 
00836    ast_mutex_lock(&p->lock);
00837 
00838    isoutbound = IS_OUTBOUND(ast, p);
00839 
00840    if (p->chan && ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
00841       ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
00842       ast_debug(2, "This local call has the ANSWERED_ELSEWHERE flag set.\n");
00843    }
00844 
00845    if (isoutbound) {
00846       const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
00847       if ((status) && (p->owner)) {
00848          /* Deadlock avoidance */
00849          while (p->owner && ast_channel_trylock(p->owner)) {
00850             ast_mutex_unlock(&p->lock);
00851             if (p->chan) {
00852                ast_channel_unlock(p->chan);
00853             }
00854             usleep(1);
00855             if (p->chan) {
00856                ast_channel_lock(p->chan);
00857             }
00858             ast_mutex_lock(&p->lock);
00859          }
00860          if (p->owner) {
00861             pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
00862             ast_channel_unlock(p->owner);
00863          }
00864       }
00865       p->chan = NULL;
00866       ast_clear_flag(p, LOCAL_LAUNCHED_PBX);
00867       ast_module_user_remove(p->u_chan);
00868    } else {
00869       ast_module_user_remove(p->u_owner);
00870       while (p->chan && ast_channel_trylock(p->chan)) {
00871             ast_mutex_unlock(&p->lock);
00872             if (p->owner) {
00873                ast_channel_unlock(p->owner);
00874             }
00875             usleep(1);
00876             if (p->owner) {
00877                ast_channel_lock(p->owner);
00878             }
00879             ast_mutex_lock(&p->lock);
00880       }
00881 
00882       p->owner = NULL;
00883       if (p->chan) {
00884          ast_queue_hangup(p->chan);
00885          ast_channel_unlock(p->chan);
00886       }
00887    }
00888    
00889    ast->tech_pvt = NULL;
00890    
00891    if (!p->owner && !p->chan) {
00892       /* Okay, done with the private part now, too. */
00893       glaredetect = ast_test_flag(p, LOCAL_GLARE_DETECT);
00894       /* If we have a queue holding, don't actually destroy p yet, but
00895          let local_queue do it. */
00896       if (glaredetect)
00897          ast_set_flag(p, LOCAL_CANCEL_QUEUE);
00898       /* Remove from list */
00899       AST_LIST_LOCK(&locals);
00900       AST_LIST_REMOVE(&locals, p, list);
00901       AST_LIST_UNLOCK(&locals);
00902       ast_mutex_unlock(&p->lock);
00903       /* And destroy */
00904       if (!glaredetect) {
00905          p = local_pvt_destroy(p);
00906       }
00907       return 0;
00908    }
00909    if (p->chan && !ast_test_flag(p, LOCAL_LAUNCHED_PBX))
00910       /* Need to actually hangup since there is no PBX */
00911       ochan = p->chan;
00912    else
00913       res = local_queue_frame(p, isoutbound, &f, NULL, 1);
00914    if (!res)
00915       ast_mutex_unlock(&p->lock);
00916    if (ochan)
00917       ast_hangup(ochan);
00918    return 0;
00919 }

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

Definition at line 584 of file chan_local.c.

References ast_connected_line_build_data(), ast_connected_line_copy_to_caller(), AST_CONTROL_CONNECTED_LINE, AST_CONTROL_HOLD, AST_CONTROL_REDIRECTING, AST_CONTROL_UNHOLD, AST_FRAME_CONTROL, ast_moh_start(), ast_moh_stop(), ast_mutex_lock, ast_mutex_unlock, ast_redirecting_build_data(), 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_queue_frame(), local_pvt::lock, local_pvt::owner, ast_frame::ptr, ast_channel::redirecting, ast_frame::subclass, and ast_channel::tech_pvt.

00585 {
00586    struct local_pvt *p = ast->tech_pvt;
00587    int res = 0;
00588    struct ast_frame f = { AST_FRAME_CONTROL, };
00589    int isoutbound;
00590 
00591    if (!p)
00592       return -1;
00593 
00594    /* If this is an MOH hold or unhold, do it on the Local channel versus real channel */
00595    if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_HOLD) {
00596       ast_moh_start(ast, data, NULL);
00597    } else if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_UNHOLD) {
00598       ast_moh_stop(ast);
00599    } else if (condition == AST_CONTROL_CONNECTED_LINE || condition == AST_CONTROL_REDIRECTING) {
00600       struct ast_channel *this_channel;
00601       struct ast_channel *the_other_channel;
00602       /* A connected line update frame may only contain a partial amount of data, such
00603        * as just a source, or just a ton, and not the full amount of information. However,
00604        * the collected information is all stored in the outgoing channel's connectedline
00605        * structure, so when receiving a connected line update on an outgoing local channel,
00606        * we need to transmit the collected connected line information instead of whatever
00607        * happens to be in this control frame. The same applies for redirecting information, which
00608        * is why it is handled here as well.*/
00609       ast_mutex_lock(&p->lock);
00610       isoutbound = IS_OUTBOUND(ast, p);
00611       if (isoutbound) {
00612          this_channel = p->chan;
00613          the_other_channel = p->owner;
00614       } else {
00615          this_channel = p->owner;
00616          the_other_channel = p->chan;
00617       }
00618       if (the_other_channel) {
00619          unsigned char frame_data[1024];
00620          if (condition == AST_CONTROL_CONNECTED_LINE) {
00621             if (isoutbound) {
00622                ast_connected_line_copy_to_caller(&the_other_channel->caller, &this_channel->connected);
00623             }
00624             f.datalen = ast_connected_line_build_data(frame_data, sizeof(frame_data), &this_channel->connected, NULL);
00625          } else {
00626             f.datalen = ast_redirecting_build_data(frame_data, sizeof(frame_data), &this_channel->redirecting, NULL);
00627          }
00628          f.subclass.integer = condition;
00629          f.data.ptr = frame_data;
00630          if (!(res = local_queue_frame(p, isoutbound, &f, ast, 1))) {
00631             ast_mutex_unlock(&p->lock);
00632          }
00633       } else {
00634          ast_mutex_unlock(&p->lock);
00635       }
00636    } else {
00637       /* Queue up a frame representing the indication as a control frame */
00638       ast_mutex_lock(&p->lock);
00639       isoutbound = IS_OUTBOUND(ast, p);
00640       f.subclass.integer = condition;
00641       f.data.ptr = (void*)data;
00642       f.datalen = datalen;
00643       if (!(res = local_queue_frame(p, isoutbound, &f, ast, 1)))
00644          ast_mutex_unlock(&p->lock);
00645    }
00646 
00647    return res;
00648 }

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

Start new local channel.

Definition at line 986 of file chan_local.c.

References ast_channel::accountcode, ast_channel::amaflags, ast_best_codec(), ast_channel_alloc, ast_channel_release(), ast_copy_string(), ast_jb_configure(), ast_log(), ast_module_user_add, ast_random(), AST_STATE_RING, local_pvt::chan, ast_channel::context, local_pvt::context, ast_channel::exten, local_pvt::exten, 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().

00987 {
00988    struct ast_channel *tmp = NULL, *tmp2 = NULL;
00989    int randnum = ast_random() & 0xffff, fmt = 0;
00990    const char *t;
00991    int ama;
00992 
00993    /* Allocate two new Asterisk channels */
00994    /* safe accountcode */
00995    if (p->owner && p->owner->accountcode)
00996       t = p->owner->accountcode;
00997    else
00998       t = "";
00999 
01000    if (p->owner)
01001       ama = p->owner->amaflags;
01002    else
01003       ama = 0;
01004    if (!(tmp = ast_channel_alloc(1, state, 0, 0, t, p->exten, p->context, linkedid, ama, "Local/%s@%s-%04x;1", p->exten, p->context, randnum)) 
01005       || !(tmp2 = ast_channel_alloc(1, AST_STATE_RING, 0, 0, t, p->exten, p->context, linkedid, ama, "Local/%s@%s-%04x;2", p->exten, p->context, randnum))) {
01006       if (tmp) {
01007          tmp = ast_channel_release(tmp);
01008       }
01009       ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n");
01010       return NULL;
01011    }
01012 
01013    tmp2->tech = tmp->tech = &local_tech;
01014 
01015    tmp->nativeformats = p->reqformat;
01016    tmp2->nativeformats = p->reqformat;
01017 
01018    /* Determine our read/write format and set it on each channel */
01019    fmt = ast_best_codec(p->reqformat);
01020    tmp->writeformat = fmt;
01021    tmp2->writeformat = fmt;
01022    tmp->rawwriteformat = fmt;
01023    tmp2->rawwriteformat = fmt;
01024    tmp->readformat = fmt;
01025    tmp2->readformat = fmt;
01026    tmp->rawreadformat = fmt;
01027    tmp2->rawreadformat = fmt;
01028 
01029    tmp->tech_pvt = p;
01030    tmp2->tech_pvt = p;
01031 
01032    p->owner = tmp;
01033    p->chan = tmp2;
01034    p->u_owner = ast_module_user_add(p->owner);
01035    p->u_chan = ast_module_user_add(p->chan);
01036 
01037    ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
01038    ast_copy_string(tmp2->context, p->context, sizeof(tmp2->context));
01039    ast_copy_string(tmp2->exten, p->exten, sizeof(tmp->exten));
01040    tmp->priority = 1;
01041    tmp2->priority = 1;
01042 
01043    ast_jb_configure(tmp, &p->jb_conf);
01044 
01045    return tmp;
01046 }

static struct local_pvt* local_pvt_destroy ( struct local_pvt *  pvt  )  [static, read]

Note:
Assumes the pvt is no longer in the pvts list

Definition at line 262 of file chan_local.c.

References ast_free, ast_mutex_destroy, and local_pvt::lock.

Referenced by local_alloc(), local_hangup(), local_queue_frame(), and local_request().

00263 {
00264    ast_mutex_destroy(&pvt->lock);
00265    ast_free(pvt);
00266    return NULL;
00267 }

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

Definition at line 300 of file chan_local.c.

References ast_bridged_channel(), ast_channel_queryoption(), ast_channel_trylock, ast_channel_unlock, ast_mutex_lock, ast_mutex_unlock, AST_OPTION_T38_STATE, local_pvt::chan, DEADLOCK_AVOIDANCE, IS_OUTBOUND, local_pvt::lock, local_pvt::owner, and ast_channel::tech_pvt.

00301 {
00302    struct local_pvt *p = ast->tech_pvt;
00303    struct ast_channel *chan, *bridged;
00304    int res;
00305 
00306    if (!p) {
00307       return -1;
00308    }
00309 
00310    if (option != AST_OPTION_T38_STATE) {
00311       /* AST_OPTION_T38_STATE is the only supported option at this time */
00312       return -1;
00313    }
00314 
00315    ast_mutex_lock(&p->lock);
00316    chan = IS_OUTBOUND(ast, p) ? p->owner : p->chan;
00317 
00318 try_again:
00319    if (!chan) {
00320       ast_mutex_unlock(&p->lock);
00321       return -1;
00322    }
00323 
00324    if (ast_channel_trylock(chan)) {
00325       DEADLOCK_AVOIDANCE(&p->lock);
00326       chan = IS_OUTBOUND(ast, p) ? p->owner : p->chan;
00327       goto try_again;
00328    }
00329 
00330    bridged = ast_bridged_channel(chan);
00331    if (!bridged) {
00332       /* can't query channel unless we are bridged */
00333       ast_mutex_unlock(&p->lock);
00334       ast_channel_unlock(chan);
00335       return -1;
00336    }
00337 
00338    if (ast_channel_trylock(bridged)) {
00339       ast_channel_unlock(chan);
00340       DEADLOCK_AVOIDANCE(&p->lock);
00341       chan = IS_OUTBOUND(ast, p) ? p->owner : p->chan;
00342       goto try_again;
00343    }
00344 
00345    res = ast_channel_queryoption(bridged, option, data, datalen, 0);
00346    ast_mutex_unlock(&p->lock);
00347    ast_channel_unlock(chan);
00348    ast_channel_unlock(bridged);
00349    return res;
00350 }

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

Definition at line 352 of file chan_local.c.

References ast_channel_trylock, ast_channel_unlock, ast_clear_flag, AST_CONTROL_RINGING, AST_FRAME_CONTROL, ast_log(), ast_mutex_lock, ast_mutex_trylock, ast_mutex_unlock, ast_queue_frame(), ast_set_flag, ast_setstate(), AST_STATE_RINGING, ast_test_flag, local_pvt::chan, CHANNEL_DEADLOCK_AVOIDANCE, ast_frame::frametype, ast_channel::generator, ast_frame_subclass::integer, LOCAL_CANCEL_QUEUE, LOCAL_GLARE_DETECT, local_pvt_destroy(), local_pvt::lock, LOG_ERROR, 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().

00354 {
00355    struct ast_channel *other = NULL;
00356 
00357    /* Recalculate outbound channel */
00358    other = isoutbound ? p->owner : p->chan;
00359 
00360    if (!other) {
00361       return 0;
00362    }
00363 
00364    /* do not queue frame if generator is on both local channels */
00365    if (us && us->generator && other->generator) {
00366       return 0;
00367    }
00368 
00369    /* Set glare detection */
00370    ast_set_flag(p, LOCAL_GLARE_DETECT);
00371 
00372    /* Ensure that we have both channels locked */
00373    while (other && ast_channel_trylock(other)) {
00374       int res;
00375       if ((res = ast_mutex_unlock(&p->lock))) {
00376          ast_log(LOG_ERROR, "chan_local bug! '&p->lock' was not locked when entering local_queue_frame! (%s)\n", strerror(res));
00377          return -1;
00378       }
00379       if (us && us_locked) {
00380          do {
00381             CHANNEL_DEADLOCK_AVOIDANCE(us);
00382          } while (ast_mutex_trylock(&p->lock));
00383       } else {
00384          usleep(1);
00385          ast_mutex_lock(&p->lock);
00386       }
00387       other = isoutbound ? p->owner : p->chan;
00388    }
00389 
00390    /* Since glare detection only occurs within this function, and because
00391     * a pvt flag cannot be set without having the pvt lock, this is the only
00392     * location where we could detect a cancelling of the queue. */
00393    if (ast_test_flag(p, LOCAL_CANCEL_QUEUE)) {
00394       /* We had a glare on the hangup.  Forget all this business,
00395       return and destroy p.  */
00396       ast_mutex_unlock(&p->lock);
00397       p = local_pvt_destroy(p);
00398       if (other) {
00399          ast_channel_unlock(other);
00400       }
00401       return -1;
00402    }
00403 
00404    if (other) {
00405       if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_RINGING) {
00406          ast_setstate(other, AST_STATE_RINGING);
00407       }
00408       ast_queue_frame(other, f);
00409       ast_channel_unlock(other);
00410    }
00411 
00412    ast_clear_flag(p, LOCAL_GLARE_DETECT);
00413 
00414    return 0;
00415 }

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

Definition at line 523 of file chan_local.c.

References ast_null_frame.

00524 {
00525    return &ast_null_frame;
00526 }

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 1049 of file chan_local.c.

References ast_channel_cc_params_init(), ast_channel_get_cc_config_params(), ast_channel_release(), AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, AST_STATE_DOWN, local_pvt::chan, ast_channel::linkedid, local_alloc(), local_new(), and local_pvt_destroy().

01050 {
01051    struct local_pvt *p = NULL;
01052    struct ast_channel *chan = NULL;
01053 
01054    /* Allocate a new private structure and then Asterisk channel */
01055    if ((p = local_alloc(data, format))) {
01056       if (!(chan = local_new(p, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL))) {
01057          AST_LIST_LOCK(&locals);
01058          AST_LIST_REMOVE(&locals, p, list);
01059          AST_LIST_UNLOCK(&locals);
01060          p = local_pvt_destroy(p);
01061       }
01062       if (chan && ast_channel_cc_params_init(chan, requestor ? ast_channel_get_cc_config_params((struct ast_channel *)requestor) : NULL)) {
01063          chan = ast_channel_release(chan);
01064          p = local_pvt_destroy(p);
01065       }
01066    }
01067 
01068    return chan;
01069 }

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

Definition at line 708 of file chan_local.c.

References AST_FRAME_HTML, ast_mutex_lock, ast_mutex_unlock, ast_frame::data, ast_frame::datalen, ast_frame_subclass::integer, IS_OUTBOUND, local_queue_frame(), local_pvt::lock, ast_frame::ptr, ast_frame::subclass, and ast_channel::tech_pvt.

00709 {
00710    struct local_pvt *p = ast->tech_pvt;
00711    int res = -1;
00712    struct ast_frame f = { AST_FRAME_HTML, };
00713    int isoutbound;
00714 
00715    if (!p)
00716       return -1;
00717    
00718    ast_mutex_lock(&p->lock);
00719    isoutbound = IS_OUTBOUND(ast, p);
00720    f.subclass.integer = subclass;
00721    f.data.ptr = (char *)data;
00722    f.datalen = datalen;
00723    if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00724       ast_mutex_unlock(&p->lock);
00725    return res;
00726 }

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

Definition at line 689 of file chan_local.c.

References AST_FRAME_TEXT, ast_mutex_lock, ast_mutex_unlock, ast_frame::data, ast_frame::datalen, IS_OUTBOUND, local_queue_frame(), local_pvt::lock, ast_frame::ptr, and ast_channel::tech_pvt.

00690 {
00691    struct local_pvt *p = ast->tech_pvt;
00692    int res = -1;
00693    struct ast_frame f = { AST_FRAME_TEXT, };
00694    int isoutbound;
00695 
00696    if (!p)
00697       return -1;
00698 
00699    ast_mutex_lock(&p->lock);
00700    isoutbound = IS_OUTBOUND(ast, p);
00701    f.data.ptr = (char *) text;
00702    f.datalen = strlen(text) + 1;
00703    if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00704       ast_mutex_unlock(&p->lock);
00705    return res;
00706 }

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

Definition at line 156 of file chan_local.c.

References AST_CHAN_WRITE_INFO_T_VERSION, ast_channel_lock, ast_channel_trylock, ast_channel_unlock, ast_log(), ast_mutex_trylock, ast_mutex_unlock, 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, local_pvt::lock, LOG_ERROR, LOG_WARNING, ast_channel::name, 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.

00157 {
00158    int res;
00159    struct local_pvt *p;
00160    struct ast_channel *otherchan;
00161    ast_chan_write_info_t *write_info;
00162 
00163    if (option != AST_OPTION_CHANNEL_WRITE) {
00164       return -1;
00165    }
00166 
00167    write_info = data;
00168 
00169    if (write_info->version != AST_CHAN_WRITE_INFO_T_VERSION) {
00170       ast_log(LOG_ERROR, "The chan_write_info_t type has changed, and this channel hasn't been updated!\n");
00171       return -1;
00172    }
00173 
00174 
00175 startover:
00176    ast_channel_lock(chan);
00177 
00178    p = chan->tech_pvt;
00179    if (!p) {
00180       ast_channel_unlock(chan);
00181       ast_log(LOG_WARNING, "Could not update other side of %s, local_pvt went away.\n", chan->name);
00182       return -1;
00183    }
00184 
00185    while (ast_mutex_trylock(&p->lock)) {
00186       ast_channel_unlock(chan);
00187       sched_yield();
00188       ast_channel_lock(chan);
00189       p = chan->tech_pvt;
00190       if (!p) {
00191          ast_channel_unlock(chan);
00192          ast_log(LOG_WARNING, "Could not update other side of %s, local_pvt went away.\n", chan->name);
00193          return -1;
00194       }
00195    }
00196 
00197    otherchan = (write_info->chan == p->owner) ? p->chan : p->owner;
00198 
00199    if (!otherchan || otherchan == write_info->chan) {
00200       ast_mutex_unlock(&p->lock);
00201       ast_channel_unlock(chan);
00202       ast_log(LOG_WARNING, "Could not update other side of %s, other side went away.\n", chan->name);
00203       return 0;
00204    }
00205 
00206    if (ast_channel_trylock(otherchan)) {
00207       ast_mutex_unlock(&p->lock);
00208       ast_channel_unlock(chan);
00209       goto startover;
00210    }
00211 
00212    res = write_info->write_fn(otherchan, write_info->function, write_info->data, write_info->value);
00213 
00214    ast_channel_unlock(otherchan);
00215    ast_mutex_unlock(&p->lock);
00216    ast_channel_unlock(chan);
00217 
00218    return res;
00219 }

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

Definition at line 528 of file chan_local.c.

References ast_debug, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_mutex_lock, ast_mutex_unlock, ast_test_flag, check_bridge(), ast_frame::frametype, IS_OUTBOUND, LOCAL_ALREADY_MASQED, local_queue_frame(), local_pvt::lock, ast_channel::name, and ast_channel::tech_pvt.

00529 {
00530    struct local_pvt *p = ast->tech_pvt;
00531    int res = -1;
00532    int isoutbound;
00533 
00534    if (!p)
00535       return -1;
00536 
00537    /* Just queue for delivery to the other side */
00538    ast_mutex_lock(&p->lock);
00539    isoutbound = IS_OUTBOUND(ast, p);
00540    if (isoutbound && f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO))
00541       check_bridge(p);
00542    if (!ast_test_flag(p, LOCAL_ALREADY_MASQED))
00543       res = local_queue_frame(p, isoutbound, f, ast, 1);
00544    else {
00545       ast_debug(1, "Not posting to queue since already masked on '%s'\n", ast->name);
00546       res = 0;
00547    }
00548    if (!res)
00549       ast_mutex_unlock(&p->lock);
00550    return res;
00551 }

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 1072 of file chan_local.c.

References ast_cli_args::argc, ast_cli(), AST_LIST_EMPTY, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, local_pvt::context, local_pvt::exten, ast_cli_args::fd, local_pvt::lock, ast_channel::name, local_pvt::owner, and ast_cli_entry::usage.

01073 {
01074    struct local_pvt *p = NULL;
01075 
01076    switch (cmd) {
01077    case CLI_INIT:
01078       e->command = "local show channels";
01079       e->usage =
01080          "Usage: local show channels\n"
01081          "       Provides summary information on active local proxy channels.\n";
01082       return NULL;
01083    case CLI_GENERATE:
01084       return NULL;
01085    }
01086 
01087    if (a->argc != 3)
01088       return CLI_SHOWUSAGE;
01089 
01090    AST_LIST_LOCK(&locals);
01091    if (!AST_LIST_EMPTY(&locals)) {
01092       AST_LIST_TRAVERSE(&locals, p, list) {
01093          ast_mutex_lock(&p->lock);
01094          ast_cli(a->fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
01095          ast_mutex_unlock(&p->lock);
01096       }
01097    } else
01098       ast_cli(a->fd, "No local channels in use\n");
01099    AST_LIST_UNLOCK(&locals);
01100 
01101    return CLI_SUCCESS;
01102 }

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

Definition at line 1108 of file chan_local.c.

References ast_channel_get_by_name(), ast_channel_unref, ast_clear_flag, AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), LOCAL_NO_OPTIMIZATION, local_pvt::lock, and ast_channel::tech_pvt.

Referenced by load_module().

01109 {
01110    const char *channel;
01111    struct local_pvt *p, *tmp = NULL;
01112    struct ast_channel *c;
01113    int found = 0;
01114 
01115    channel = astman_get_header(m, "Channel");
01116 
01117    if (ast_strlen_zero(channel)) {
01118       astman_send_error(s, m, "'Channel' not specified.");
01119       return 0;
01120    }
01121 
01122    c = ast_channel_get_by_name(channel);
01123    if (!c) {
01124       astman_send_error(s, m, "Channel does not exist.");
01125       return 0;
01126    }
01127 
01128    p = c->tech_pvt;
01129    ast_channel_unref(c);
01130    c = NULL;
01131 
01132    if (AST_LIST_LOCK(&locals)) {
01133       astman_send_error(s, m, "Unable to lock the monitor");
01134       return 0;
01135    }
01136 
01137 
01138    AST_LIST_TRAVERSE(&locals, tmp, list) {
01139       if (tmp == p) {
01140          ast_mutex_lock(&tmp->lock);
01141          found = 1;
01142          ast_clear_flag(tmp, LOCAL_NO_OPTIMIZATION);
01143          ast_mutex_unlock(&tmp->lock);
01144          break;
01145       }
01146    }
01147    AST_LIST_UNLOCK(&locals);
01148 
01149    if (found) {
01150       astman_send_ack(s, m, "Queued channel to be optimized away");
01151    } else {
01152       astman_send_error(s, m, "Unable to find channel");
01153    }
01154 
01155    return 0;
01156 }

static int unload_module ( void   )  [static]

Unload the local proxy channel from Asterisk.

Definition at line 1174 of file chan_local.c.

References ast_channel_unregister(), ast_cli_unregister_multiple(), AST_LIST_LOCK, AST_LIST_TRAVERSE, AST_LIST_UNLOCK, ast_log(), ast_manager_unregister(), ast_softhangup(), AST_SOFTHANGUP_APPUNLOAD, cli_local, LOG_WARNING, and local_pvt::owner.

01175 {
01176    struct local_pvt *p = NULL;
01177 
01178    /* First, take us out of the channel loop */
01179    ast_cli_unregister_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
01180    ast_manager_unregister("LocalOptimizeAway");
01181    ast_channel_unregister(&local_tech);
01182    if (!AST_LIST_LOCK(&locals)) {
01183       /* Hangup all interfaces if they have an owner */
01184       AST_LIST_TRAVERSE(&locals, p, list) {
01185          if (p->owner)
01186             ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
01187       }
01188       AST_LIST_UNLOCK(&locals);
01189    } else {
01190       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
01191       return -1;
01192    }     
01193    return 0;
01194 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Local Proxy Channel (Note: used internally by other modules)" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DRIVER, } [static]

Definition at line 1200 of file chan_local.c.

Definition at line 1200 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 1104 of file chan_local.c.

Referenced by load_module(), and unload_module().

struct ast_jb_conf g_jb_conf [static]

Definition at line 75 of file chan_local.c.

struct ast_channel_tech local_tech [static]

Definition at line 101 of file chan_local.c.

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

Definition at line 71 of file chan_local.c.


Generated on Sat Mar 26 11:15:51 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.9