Tue Nov 4 13:20:15 2008

Asterisk developer's documentation


chan_local.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \author Mark Spencer <markster@digium.com>
00022  *
00023  * \brief Local Proxy Channel
00024  * 
00025  * \ingroup channel_drivers
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 116038 $")
00031 
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035 #include <sys/socket.h>
00036 #include <errno.h>
00037 #include <stdlib.h>
00038 #include <fcntl.h>
00039 #include <netdb.h>
00040 #include <netinet/in.h>
00041 #include <arpa/inet.h>
00042 #include <sys/signal.h>
00043 
00044 #include "asterisk/lock.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/config.h"
00047 #include "asterisk/logger.h"
00048 #include "asterisk/module.h"
00049 #include "asterisk/pbx.h"
00050 #include "asterisk/options.h"
00051 #include "asterisk/lock.h"
00052 #include "asterisk/sched.h"
00053 #include "asterisk/io.h"
00054 #include "asterisk/rtp.h"
00055 #include "asterisk/acl.h"
00056 #include "asterisk/callerid.h"
00057 #include "asterisk/file.h"
00058 #include "asterisk/cli.h"
00059 #include "asterisk/app.h"
00060 #include "asterisk/musiconhold.h"
00061 #include "asterisk/manager.h"
00062 #include "asterisk/stringfields.h"
00063 #include "asterisk/devicestate.h"
00064 
00065 static const char tdesc[] = "Local Proxy Channel Driver";
00066 
00067 #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0)
00068 
00069 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause);
00070 static int local_digit_begin(struct ast_channel *ast, char digit);
00071 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
00072 static int local_call(struct ast_channel *ast, char *dest, int timeout);
00073 static int local_hangup(struct ast_channel *ast);
00074 static int local_answer(struct ast_channel *ast);
00075 static struct ast_frame *local_read(struct ast_channel *ast);
00076 static int local_write(struct ast_channel *ast, struct ast_frame *f);
00077 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
00078 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00079 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen);
00080 static int local_sendtext(struct ast_channel *ast, const char *text);
00081 static int local_devicestate(void *data);
00082 
00083 /* PBX interface structure for channel registration */
00084 static const struct ast_channel_tech local_tech = {
00085    .type = "Local",
00086    .description = tdesc,
00087    .capabilities = -1,
00088    .requester = local_request,
00089    .send_digit_begin = local_digit_begin,
00090    .send_digit_end = local_digit_end,
00091    .call = local_call,
00092    .hangup = local_hangup,
00093    .answer = local_answer,
00094    .read = local_read,
00095    .write = local_write,
00096    .write_video = local_write,
00097    .exception = local_read,
00098    .indicate = local_indicate,
00099    .fixup = local_fixup,
00100    .send_html = local_sendhtml,
00101    .send_text = local_sendtext,
00102    .devicestate = local_devicestate,
00103 };
00104 
00105 struct local_pvt {
00106    ast_mutex_t lock;       /* Channel private lock */
00107    unsigned int flags;                     /* Private flags */
00108    char context[AST_MAX_CONTEXT];      /* Context to call */
00109    char exten[AST_MAX_EXTENSION];      /* Extension to call */
00110    int reqformat;          /* Requested format */
00111    struct ast_channel *owner;    /* Master Channel */
00112    struct ast_channel *chan;     /* Outbound channel */
00113    struct ast_module_user *u_owner; /*! reference to keep the module loaded while in use */
00114    struct ast_module_user *u_chan;     /*! reference to keep the module loaded while in use */
00115    AST_LIST_ENTRY(local_pvt) list;     /* Next entity */
00116 };
00117 
00118 #define LOCAL_GLARE_DETECT    (1 << 0) /*!< Detect glare on hangup */
00119 #define LOCAL_CANCEL_QUEUE    (1 << 1) /*!< Cancel queue */
00120 #define LOCAL_ALREADY_MASQED  (1 << 2) /*!< Already masqueraded */
00121 #define LOCAL_LAUNCHED_PBX    (1 << 3) /*!< PBX was launched */
00122 #define LOCAL_NO_OPTIMIZATION (1 << 4) /*!< Do not optimize using masquerading */
00123 
00124 static AST_LIST_HEAD_STATIC(locals, local_pvt);
00125 
00126 /*! \brief Adds devicestate to local channels */
00127 static int local_devicestate(void *data)
00128 {
00129    char *exten = ast_strdupa(data);
00130    char *context = NULL, *opts = NULL;
00131    int res;
00132 
00133    if (!(context = strchr(exten, '@'))) {
00134       ast_log(LOG_WARNING, "Someone used Local/%s somewhere without a @context. This is bad.\n", exten);
00135       return AST_DEVICE_INVALID; 
00136    }
00137 
00138    *context++ = '\0';
00139 
00140    /* Strip options if they exist */
00141    if ((opts = strchr(context, '/')))
00142       *opts = '\0';
00143 
00144    if (option_debug > 2)
00145       ast_log(LOG_DEBUG, "Checking if extension %s@%s exists (devicestate)\n", exten, context);
00146    res = ast_exists_extension(NULL, context, exten, 1, NULL);
00147    if (!res)      
00148       return AST_DEVICE_INVALID;
00149    else
00150       return AST_DEVICE_UNKNOWN;
00151 }
00152 
00153 /*!
00154  * \note Assumes the pvt is no longer in the pvts list
00155  */
00156 static struct local_pvt *local_pvt_destroy(struct local_pvt *pvt)
00157 {
00158    ast_mutex_destroy(&pvt->lock);
00159    free(pvt);
00160    return NULL;
00161 }
00162 
00163 static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, 
00164    struct ast_channel *us, int us_locked)
00165 {
00166    struct ast_channel *other = NULL;
00167 
00168    /* Recalculate outbound channel */
00169    other = isoutbound ? p->owner : p->chan;
00170 
00171    /* Set glare detection */
00172    ast_set_flag(p, LOCAL_GLARE_DETECT);
00173    if (ast_test_flag(p, LOCAL_CANCEL_QUEUE)) {
00174       /* We had a glare on the hangup.  Forget all this business,
00175       return and destroy p.  */
00176       ast_mutex_unlock(&p->lock);
00177       p = local_pvt_destroy(p);
00178       return -1;
00179    }
00180    if (!other) {
00181       ast_clear_flag(p, LOCAL_GLARE_DETECT);
00182       return 0;
00183    }
00184 
00185    /* Ensure that we have both channels locked */
00186    while (other && ast_channel_trylock(other)) {
00187       ast_mutex_unlock(&p->lock);
00188       if (us && us_locked) {
00189          ast_channel_unlock(us);
00190       }
00191       usleep(1);
00192       if (us && us_locked) {
00193          ast_channel_lock(us);
00194       }
00195       ast_mutex_lock(&p->lock);
00196       other = isoutbound ? p->owner : p->chan;
00197    }
00198 
00199    if (other) {
00200       ast_queue_frame(other, f);
00201       ast_channel_unlock(other);
00202    }
00203 
00204    ast_clear_flag(p, LOCAL_GLARE_DETECT);
00205 
00206    return 0;
00207 }
00208 
00209 static int local_answer(struct ast_channel *ast)
00210 {
00211    struct local_pvt *p = ast->tech_pvt;
00212    int isoutbound;
00213    int res = -1;
00214 
00215    if (!p)
00216       return -1;
00217 
00218    ast_mutex_lock(&p->lock);
00219    isoutbound = IS_OUTBOUND(ast, p);
00220    if (isoutbound) {
00221       /* Pass along answer since somebody answered us */
00222       struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
00223       res = local_queue_frame(p, isoutbound, &answer, ast, 1);
00224    } else
00225       ast_log(LOG_WARNING, "Huh?  Local is being asked to answer?\n");
00226    if (!res)
00227       ast_mutex_unlock(&p->lock);
00228    return res;
00229 }
00230 
00231 static void check_bridge(struct local_pvt *p, int isoutbound)
00232 {
00233    struct ast_channel_monitor *tmp;
00234    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)))
00235       return;
00236 
00237    /* only do the masquerade if we are being called on the outbound channel,
00238       if it has been bridged to another channel and if there are no pending
00239       frames on the owner channel (because they would be transferred to the
00240       outbound channel during the masquerade)
00241    */
00242    if (isoutbound && p->chan->_bridge /* Not ast_bridged_channel!  Only go one step! */ && AST_LIST_EMPTY(&p->owner->readq)) {
00243       /* Masquerade bridged channel into owner */
00244       /* Lock everything we need, one by one, and give up if
00245          we can't get everything.  Remember, we'll get another
00246          chance in just a little bit */
00247       if (!ast_mutex_trylock(&(p->chan->_bridge)->lock)) {
00248          if (!p->chan->_bridge->_softhangup) {
00249             if (!ast_mutex_trylock(&p->owner->lock)) {
00250                if (!p->owner->_softhangup) {
00251                   if(p->owner->monitor && !p->chan->_bridge->monitor) {
00252                      /* If a local channel is being monitored, we don't want a masquerade
00253                       * to cause the monitor to go away. Since the masquerade swaps the monitors,
00254                       * pre-swapping the monitors before the masquerade will ensure that the monitor
00255                       * ends up where it is expected.
00256                       */
00257                      tmp = p->owner->monitor;
00258                      p->owner->monitor = p->chan->_bridge->monitor;
00259                      p->chan->_bridge->monitor = tmp;
00260                   }
00261                   ast_channel_masquerade(p->owner, p->chan->_bridge);
00262                   ast_set_flag(p, LOCAL_ALREADY_MASQED);
00263                }
00264                ast_mutex_unlock(&p->owner->lock);
00265             }
00266             ast_mutex_unlock(&(p->chan->_bridge)->lock);
00267          }
00268       }
00269    /* We only allow masquerading in one 'direction'... it's important to preserve the state
00270       (group variables, etc.) that live on p->chan->_bridge (and were put there by the dialplan)
00271       when the local channels go away.
00272    */
00273 #if 0
00274    } else if (!isoutbound && p->owner && p->owner->_bridge && p->chan && AST_LIST_EMPTY(&p->chan->readq)) {
00275       /* Masquerade bridged channel into chan */
00276       if (!ast_mutex_trylock(&(p->owner->_bridge)->lock)) {
00277          if (!p->owner->_bridge->_softhangup) {
00278             if (!ast_mutex_trylock(&p->chan->lock)) {
00279                if (!p->chan->_softhangup) {
00280                   ast_channel_masquerade(p->chan, p->owner->_bridge);
00281                   ast_set_flag(p, LOCAL_ALREADY_MASQED);
00282                }
00283                ast_mutex_unlock(&p->chan->lock);
00284             }
00285          }
00286          ast_mutex_unlock(&(p->owner->_bridge)->lock);
00287       }
00288 #endif
00289    }
00290 }
00291 
00292 static struct ast_frame  *local_read(struct ast_channel *ast)
00293 {
00294    return &ast_null_frame;
00295 }
00296 
00297 static int local_write(struct ast_channel *ast, struct ast_frame *f)
00298 {
00299    struct local_pvt *p = ast->tech_pvt;
00300    int res = -1;
00301    int isoutbound;
00302 
00303    if (!p)
00304       return -1;
00305 
00306    /* Just queue for delivery to the other side */
00307    ast_mutex_lock(&p->lock);
00308    isoutbound = IS_OUTBOUND(ast, p);
00309    if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO))
00310       check_bridge(p, isoutbound);
00311    if (!ast_test_flag(p, LOCAL_ALREADY_MASQED))
00312       res = local_queue_frame(p, isoutbound, f, ast, 1);
00313    else {
00314       if (option_debug)
00315          ast_log(LOG_DEBUG, "Not posting to queue since already masked on '%s'\n", ast->name);
00316       res = 0;
00317    }
00318    if (!res)
00319       ast_mutex_unlock(&p->lock);
00320    return res;
00321 }
00322 
00323 static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00324 {
00325    struct local_pvt *p = newchan->tech_pvt;
00326 
00327    if (!p)
00328       return -1;
00329 
00330    ast_mutex_lock(&p->lock);
00331 
00332    if ((p->owner != oldchan) && (p->chan != oldchan)) {
00333       ast_log(LOG_WARNING, "Old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan);
00334       ast_mutex_unlock(&p->lock);
00335       return -1;
00336    }
00337    if (p->owner == oldchan)
00338       p->owner = newchan;
00339    else
00340       p->chan = newchan;
00341    ast_mutex_unlock(&p->lock);
00342    return 0;
00343 }
00344 
00345 static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
00346 {
00347    struct local_pvt *p = ast->tech_pvt;
00348    int res = 0;
00349    struct ast_frame f = { AST_FRAME_CONTROL, };
00350    int isoutbound;
00351 
00352    if (!p)
00353       return -1;
00354 
00355    /* If this is an MOH hold or unhold, do it on the Local channel versus real channel */
00356    if (condition == AST_CONTROL_HOLD) {
00357       ast_moh_start(ast, data, NULL);
00358    } else if (condition == AST_CONTROL_UNHOLD) {
00359       ast_moh_stop(ast);
00360    } else {
00361       /* Queue up a frame representing the indication as a control frame */
00362       ast_mutex_lock(&p->lock);
00363       isoutbound = IS_OUTBOUND(ast, p);
00364       f.subclass = condition;
00365       f.data = (void*)data;
00366       f.datalen = datalen;
00367       if (!(res = local_queue_frame(p, isoutbound, &f, ast, 1)))
00368          ast_mutex_unlock(&p->lock);
00369    }
00370 
00371    return res;
00372 }
00373 
00374 static int local_digit_begin(struct ast_channel *ast, char digit)
00375 {
00376    struct local_pvt *p = ast->tech_pvt;
00377    int res = -1;
00378    struct ast_frame f = { AST_FRAME_DTMF_BEGIN, };
00379    int isoutbound;
00380 
00381    if (!p)
00382       return -1;
00383 
00384    ast_mutex_lock(&p->lock);
00385    isoutbound = IS_OUTBOUND(ast, p);
00386    f.subclass = digit;
00387    if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00388       ast_mutex_unlock(&p->lock);
00389 
00390    return res;
00391 }
00392 
00393 static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
00394 {
00395    struct local_pvt *p = ast->tech_pvt;
00396    int res = -1;
00397    struct ast_frame f = { AST_FRAME_DTMF_END, };
00398    int isoutbound;
00399 
00400    if (!p)
00401       return -1;
00402 
00403    ast_mutex_lock(&p->lock);
00404    isoutbound = IS_OUTBOUND(ast, p);
00405    f.subclass = digit;
00406    f.len = duration;
00407    if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00408       ast_mutex_unlock(&p->lock);
00409 
00410    return res;
00411 }
00412 
00413 static int local_sendtext(struct ast_channel *ast, const char *text)
00414 {
00415    struct local_pvt *p = ast->tech_pvt;
00416    int res = -1;
00417    struct ast_frame f = { AST_FRAME_TEXT, };
00418    int isoutbound;
00419 
00420    if (!p)
00421       return -1;
00422 
00423    ast_mutex_lock(&p->lock);
00424    isoutbound = IS_OUTBOUND(ast, p);
00425    f.data = (char *) text;
00426    f.datalen = strlen(text) + 1;
00427    if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00428       ast_mutex_unlock(&p->lock);
00429    return res;
00430 }
00431 
00432 static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen)
00433 {
00434    struct local_pvt *p = ast->tech_pvt;
00435    int res = -1;
00436    struct ast_frame f = { AST_FRAME_HTML, };
00437    int isoutbound;
00438 
00439    if (!p)
00440       return -1;
00441    
00442    ast_mutex_lock(&p->lock);
00443    isoutbound = IS_OUTBOUND(ast, p);
00444    f.subclass = subclass;
00445    f.data = (char *)data;
00446    f.datalen = datalen;
00447    if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0)))
00448       ast_mutex_unlock(&p->lock);
00449    return res;
00450 }
00451 
00452 /*! \brief Initiate new call, part of PBX interface 
00453  *    dest is the dial string */
00454 static int local_call(struct ast_channel *ast, char *dest, int timeout)
00455 {
00456    struct local_pvt *p = ast->tech_pvt;
00457    int res;
00458    struct ast_var_t *varptr = NULL, *new;
00459    size_t len, namelen;
00460 
00461    if (!p)
00462       return -1;
00463    
00464    ast_mutex_lock(&p->lock);
00465 
00466    /*
00467     * Note that cid_num and cid_name aren't passed in the ast_channel_alloc
00468     * call, so it's done here instead.
00469     */
00470    p->chan->cid.cid_num = ast_strdup(p->owner->cid.cid_num);
00471    p->chan->cid.cid_name = ast_strdup(p->owner->cid.cid_name);
00472    p->chan->cid.cid_rdnis = ast_strdup(p->owner->cid.cid_rdnis);
00473    p->chan->cid.cid_ani = ast_strdup(p->owner->cid.cid_ani);
00474    p->chan->cid.cid_pres = p->owner->cid.cid_pres;
00475    ast_string_field_set(p->chan, language, p->owner->language);
00476    ast_string_field_set(p->chan, accountcode, p->owner->accountcode);
00477    ast_cdr_update(p->chan);
00478    p->chan->cdrflags = p->owner->cdrflags;
00479 
00480    /* copy the channel variables from the incoming channel to the outgoing channel */
00481    /* Note that due to certain assumptions, they MUST be in the same order */
00482    AST_LIST_TRAVERSE(&p->owner->varshead, varptr, entries) {
00483       namelen = strlen(varptr->name);
00484       len = sizeof(struct ast_var_t) + namelen + strlen(varptr->value) + 2;
00485       if ((new = ast_calloc(1, len))) {
00486          memcpy(new, varptr, len);
00487          new->value = &(new->name[0]) + namelen + 1;
00488          AST_LIST_INSERT_TAIL(&p->chan->varshead, new, entries);
00489       }
00490    }
00491    ast_channel_datastore_inherit(p->owner, p->chan);
00492 
00493    /* Start switch on sub channel */
00494    if (!(res = ast_pbx_start(p->chan)))
00495       ast_set_flag(p, LOCAL_LAUNCHED_PBX);
00496 
00497    ast_mutex_unlock(&p->lock);
00498    return res;
00499 }
00500 
00501 /*! \brief Hangup a call through the local proxy channel */
00502 static int local_hangup(struct ast_channel *ast)
00503 {
00504    struct local_pvt *p = ast->tech_pvt;
00505    int isoutbound;
00506    struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
00507    struct ast_channel *ochan = NULL;
00508    int glaredetect = 0, res = 0;
00509 
00510    if (!p)
00511       return -1;
00512 
00513    ast_mutex_lock(&p->lock);
00514    isoutbound = IS_OUTBOUND(ast, p);
00515    if (isoutbound) {
00516       const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
00517       if ((status) && (p->owner)) {
00518          /* Deadlock avoidance */
00519          while (p->owner && ast_channel_trylock(p->owner)) {
00520             ast_mutex_unlock(&p->lock);
00521             if (ast) {
00522                ast_channel_unlock(ast);
00523             }
00524             usleep(1);
00525             if (ast) {
00526                ast_channel_lock(ast);
00527             }
00528             ast_mutex_lock(&p->lock);
00529          }
00530          if (p->owner) {
00531             pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status);
00532             ast_channel_unlock(p->owner);
00533          }
00534       }
00535       p->chan = NULL;
00536       ast_clear_flag(p, LOCAL_LAUNCHED_PBX);
00537       ast_module_user_remove(p->u_chan);
00538    } else {
00539       p->owner = NULL;
00540       ast_module_user_remove(p->u_owner);
00541    }
00542    
00543    ast->tech_pvt = NULL;
00544    
00545    if (!p->owner && !p->chan) {
00546       /* Okay, done with the private part now, too. */
00547       glaredetect = ast_test_flag(p, LOCAL_GLARE_DETECT);
00548       /* If we have a queue holding, don't actually destroy p yet, but
00549          let local_queue do it. */
00550       if (glaredetect)
00551          ast_set_flag(p, LOCAL_CANCEL_QUEUE);
00552       ast_mutex_unlock(&p->lock);
00553       /* Remove from list */
00554       AST_LIST_LOCK(&locals);
00555       AST_LIST_REMOVE(&locals, p, list);
00556       AST_LIST_UNLOCK(&locals);
00557       /* Grab / release lock just in case */
00558       ast_mutex_lock(&p->lock);
00559       ast_mutex_unlock(&p->lock);
00560       /* And destroy */
00561       if (!glaredetect) {
00562          p = local_pvt_destroy(p);
00563       }
00564       return 0;
00565    }
00566    if (p->chan && !ast_test_flag(p, LOCAL_LAUNCHED_PBX))
00567       /* Need to actually hangup since there is no PBX */
00568       ochan = p->chan;
00569    else
00570       res = local_queue_frame(p, isoutbound, &f, NULL, 1);
00571    if (!res)
00572       ast_mutex_unlock(&p->lock);
00573    if (ochan)
00574       ast_hangup(ochan);
00575    return 0;
00576 }
00577 
00578 /*! \brief Create a call structure */
00579 static struct local_pvt *local_alloc(const char *data, int format)
00580 {
00581    struct local_pvt *tmp = NULL;
00582    char *c = NULL, *opts = NULL;
00583 
00584    if (!(tmp = ast_calloc(1, sizeof(*tmp))))
00585       return NULL;
00586 
00587    /* Initialize private structure information */
00588    ast_mutex_init(&tmp->lock);
00589    ast_copy_string(tmp->exten, data, sizeof(tmp->exten));
00590 
00591    /* Look for options */
00592    if ((opts = strchr(tmp->exten, '/'))) {
00593       *opts++ = '\0';
00594       if (strchr(opts, 'n'))
00595          ast_set_flag(tmp, LOCAL_NO_OPTIMIZATION);
00596    }
00597 
00598    /* Look for a context */
00599    if ((c = strchr(tmp->exten, '@')))
00600       *c++ = '\0';
00601 
00602    ast_copy_string(tmp->context, c ? c : "default", sizeof(tmp->context));
00603 
00604    tmp->reqformat = format;
00605 
00606    if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
00607       ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context);
00608       tmp = local_pvt_destroy(tmp);
00609    } else {
00610       /* Add to list */
00611       AST_LIST_LOCK(&locals);
00612       AST_LIST_INSERT_HEAD(&locals, tmp, list);
00613       AST_LIST_UNLOCK(&locals);
00614    }
00615    
00616    return tmp;
00617 }
00618 
00619 /*! \brief Start new local channel */
00620 static struct ast_channel *local_new(struct local_pvt *p, int state)
00621 {
00622    struct ast_channel *tmp = NULL, *tmp2 = NULL;
00623    int randnum = ast_random() & 0xffff, fmt = 0;
00624    const char *t;
00625    int ama;
00626 
00627    /* Allocate two new Asterisk channels */
00628    /* safe accountcode */
00629    if (p->owner && p->owner->accountcode)
00630       t = p->owner->accountcode;
00631    else
00632       t = "";
00633 
00634    if (p->owner)
00635       ama = p->owner->amaflags;
00636    else
00637       ama = 0;
00638    if (!(tmp = ast_channel_alloc(1, state, 0, 0, t, p->exten, p->context, ama, "Local/%s@%s-%04x,1", p->exten, p->context, randnum)) 
00639          || !(tmp2 = ast_channel_alloc(1, AST_STATE_RING, 0, 0, t, p->exten, p->context, ama, "Local/%s@%s-%04x,2", p->exten, p->context, randnum))) {
00640       if (tmp)
00641          ast_channel_free(tmp);
00642       if (tmp2)
00643          ast_channel_free(tmp2);
00644       ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n");
00645       return NULL;
00646    } 
00647 
00648    tmp2->tech = tmp->tech = &local_tech;
00649 
00650    tmp->nativeformats = p->reqformat;
00651    tmp2->nativeformats = p->reqformat;
00652 
00653    /* Determine our read/write format and set it on each channel */
00654    fmt = ast_best_codec(p->reqformat);
00655    tmp->writeformat = fmt;
00656    tmp2->writeformat = fmt;
00657    tmp->rawwriteformat = fmt;
00658    tmp2->rawwriteformat = fmt;
00659    tmp->readformat = fmt;
00660    tmp2->readformat = fmt;
00661    tmp->rawreadformat = fmt;
00662    tmp2->rawreadformat = fmt;
00663 
00664    tmp->tech_pvt = p;
00665    tmp2->tech_pvt = p;
00666 
00667    p->owner = tmp;
00668    p->chan = tmp2;
00669    p->u_owner = ast_module_user_add(p->owner);
00670    p->u_chan = ast_module_user_add(p->chan);
00671 
00672    ast_copy_string(tmp->context, p->context, sizeof(tmp->context));
00673    ast_copy_string(tmp2->context, p->context, sizeof(tmp2->context));
00674    ast_copy_string(tmp2->exten, p->exten, sizeof(tmp->exten));
00675    tmp->priority = 1;
00676    tmp2->priority = 1;
00677 
00678    return tmp;
00679 }
00680 
00681 /*! \brief Part of PBX interface */
00682 static struct ast_channel *local_request(const char *type, int format, void *data, int *cause)
00683 {
00684    struct local_pvt *p = NULL;
00685    struct ast_channel *chan = NULL;
00686 
00687    /* Allocate a new private structure and then Asterisk channel */
00688    if ((p = local_alloc(data, format))) {
00689       if (!(chan = local_new(p, AST_STATE_DOWN))) {
00690          AST_LIST_LOCK(&locals);
00691          AST_LIST_REMOVE(&locals, p, list);
00692          AST_LIST_UNLOCK(&locals);
00693          p = local_pvt_destroy(p);
00694       }
00695    }
00696 
00697    return chan;
00698 }
00699 
00700 /*! \brief CLI command "local show channels" */
00701 static int locals_show(int fd, int argc, char **argv)
00702 {
00703    struct local_pvt *p = NULL;
00704 
00705    if (argc != 3)
00706       return RESULT_SHOWUSAGE;
00707 
00708    AST_LIST_LOCK(&locals);
00709    if (!AST_LIST_EMPTY(&locals)) {
00710       AST_LIST_TRAVERSE(&locals, p, list) {
00711          ast_mutex_lock(&p->lock);
00712          ast_cli(fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context);
00713          ast_mutex_unlock(&p->lock);
00714       }
00715    } else
00716       ast_cli(fd, "No local channels in use\n");
00717    AST_LIST_UNLOCK(&locals);
00718 
00719    return RESULT_SUCCESS;
00720 }
00721 
00722 static char show_locals_usage[] = 
00723 "Usage: local show channels\n"
00724 "       Provides summary information on active local proxy channels.\n";
00725 
00726 static struct ast_cli_entry cli_local[] = {
00727    { { "local", "show", "channels", NULL },
00728    locals_show, "List status of local channels",
00729    show_locals_usage },
00730 };
00731 
00732 /*! \brief Load module into PBX, register channel */
00733 static int load_module(void)
00734 {
00735    /* Make sure we can register our channel type */
00736    if (ast_channel_register(&local_tech)) {
00737       ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n");
00738       return -1;
00739    }
00740    ast_cli_register_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
00741    return 0;
00742 }
00743 
00744 /*! \brief Unload the local proxy channel from Asterisk */
00745 static int unload_module(void)
00746 {
00747    struct local_pvt *p = NULL;
00748 
00749    /* First, take us out of the channel loop */
00750    ast_cli_unregister_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry));
00751    ast_channel_unregister(&local_tech);
00752    if (!AST_LIST_LOCK(&locals)) {
00753       /* Hangup all interfaces if they have an owner */
00754       AST_LIST_TRAVERSE(&locals, p, list) {
00755          if (p->owner)
00756             ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
00757       }
00758       AST_LIST_UNLOCK(&locals);
00759       AST_LIST_HEAD_DESTROY(&locals);
00760    } else {
00761       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
00762       return -1;
00763    }     
00764    return 0;
00765 }
00766 
00767 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Local Proxy Channel (Note: used internally by other modules)");

Generated on Tue Nov 4 13:20:15 2008 for Asterisk - the Open Source PBX by  doxygen 1.4.7