Fix case where local is allocated but not actually called on
[asterisk/asterisk.git] / channels / chan_local.c
index 44af9fc..4c4d4e1 100755 (executable)
@@ -61,6 +61,7 @@ static struct local_pvt {
        char context[AST_MAX_EXTENSION];        /* Context to call */
        char exten[AST_MAX_EXTENSION];          /* Extension to call */
        int reqformat;                                          /* Requested format */
+       int alreadymasqed;                                      /* Already masqueraded */
        struct ast_channel *owner;                      /* Master Channel */
        struct ast_channel *chan;                       /* Outbound channel */
        struct local_pvt *next;                         /* Next entity */
@@ -98,13 +99,28 @@ static int local_answer(struct ast_channel *ast)
        if (isoutbound) {
                /* Pass along answer since somebody answered us */
                struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
-               local_queue_frame(p, isoutbound, &answer);
+               res = local_queue_frame(p, isoutbound, &answer);
        } else
                ast_log(LOG_WARNING, "Huh?  Local is being asked to answer?\n");
        ast_pthread_mutex_unlock(&p->lock);
        return res;
 }
 
+static void check_bridge(struct local_pvt *p, int isoutbound)
+{
+       if (p->alreadymasqed)
+               return;
+       if (isoutbound && p->chan && p->chan->bridge && p->owner) {
+               /* Masquerade bridged channel into owner */
+               ast_channel_masquerade(p->owner, p->chan->bridge);
+               p->alreadymasqed = 1;
+       } else if (!isoutbound && p->owner && p->owner->bridge && p->chan) {
+               /* Masquerade bridged channel into chan */
+               ast_channel_masquerade(p->chan, p->owner->bridge);
+               p->alreadymasqed = 1;
+       }
+}
+
 static struct ast_frame  *local_read(struct ast_channel *ast)
 {
        static struct ast_frame null = { AST_FRAME_NULL, };
@@ -117,9 +133,11 @@ static int local_write(struct ast_channel *ast, struct ast_frame *f)
        int res = -1;
        int isoutbound = IS_OUTBOUND(ast, p);
 
+
        /* Just queue for delivery to the other side */
        ast_pthread_mutex_lock(&p->lock);
        res = local_queue_frame(p, isoutbound, f);
+       check_bridge(p, isoutbound);
        ast_pthread_mutex_unlock(&p->lock);
        return res;
 }
@@ -171,6 +189,15 @@ static int local_digit(struct ast_channel *ast, char digit)
 static int local_call(struct ast_channel *ast, char *dest, int timeout)
 {
        struct local_pvt *p = ast->pvt->pvt;
+
+       if (p->owner->callerid)
+               p->chan->callerid = strdup(p->owner->callerid);
+       else
+               p->chan->callerid = NULL;
+       if (p->owner->ani)
+               p->chan->ani = strdup(p->owner->ani);
+       else
+               p->chan->ani = NULL;
        /* Start switch on sub channel */
        return ast_pbx_start(p->chan);
 }
@@ -202,20 +229,45 @@ static int local_hangup(struct ast_channel *ast)
        struct local_pvt *p = ast->pvt->pvt;
        int isoutbound = IS_OUTBOUND(ast, p);
        struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP };
+       struct local_pvt *cur, *prev=NULL;
+       struct ast_channel *ochan = NULL;
        ast_pthread_mutex_lock(&p->lock);
        if (isoutbound)
                p->chan = NULL;
        else
                p->owner = NULL;
        ast->pvt->pvt = NULL;
+       
        if (!p->owner && !p->chan) {
                /* Okay, done with the private part now, too. */
                ast_pthread_mutex_unlock(&p->lock);
+               /* Remove from list */
+               ast_pthread_mutex_lock(&locallock);
+               cur = locals;
+               while(cur) {
+                       if (cur == p) {
+                               if (prev)
+                                       prev->next = cur->next;
+                               else
+                                       locals = cur->next;
+                               break;
+                       }
+                       prev = cur;
+                       cur = cur->next;
+               }
+               ast_pthread_mutex_unlock(&locallock);
+               /* And destroy */
                free(p);
                return 0;
        }
-       local_queue_frame(p, isoutbound, &f);
+       if (p->chan && !p->chan->pbx)
+               /* Need to actually hangup since there is no PBX */
+               ochan = p->chan;
+       else
+               local_queue_frame(p, isoutbound, &f);
        ast_pthread_mutex_unlock(&p->lock);
+       if (ochan)
+               ast_hangup(ochan);
        return 0;
 }
 
@@ -232,14 +284,21 @@ static struct local_pvt *local_alloc(char *data, int format)
                if (c) {
                        *c = '\0';
                        c++;
-                       strncpy(tmp->context, data, sizeof(tmp->context) - 1);
-               }
+                       strncpy(tmp->context, c, sizeof(tmp->context) - 1);
+               } else
+                       strncpy(tmp->context, "default", sizeof(tmp->context) - 1);
                tmp->reqformat = format;
-               /* Add to list */
-               ast_pthread_mutex_lock(&locallock);
-               tmp->next = locals;
-               locals = tmp;
-               ast_pthread_mutex_unlock(&locallock);
+               if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) {
+                       ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->context, tmp->exten);
+                       free(tmp);
+                       tmp = NULL;
+               } else {
+                       /* Add to list */
+                       ast_pthread_mutex_lock(&locallock);
+                       tmp->next = locals;
+                       locals = tmp;
+                       ast_pthread_mutex_unlock(&locallock);
+               }
                
        }
        return tmp;
@@ -259,7 +318,7 @@ static struct ast_channel *local_new(struct local_pvt *p, int state)
        }
        if (tmp) {
                tmp->nativeformats = p->reqformat;
-               tmp->nativeformats = p->reqformat;
+               tmp2->nativeformats = p->reqformat;
                snprintf(tmp->name, sizeof(tmp->name), "Local/%s@%s-1", p->exten, p->context);
                snprintf(tmp2->name, sizeof(tmp2->name), "Local/%s@%s-2", p->exten, p->context);
                tmp->type = type;
@@ -316,7 +375,8 @@ static struct ast_channel *local_request(char *type, int format, void *data)
        struct local_pvt *p;
        struct ast_channel *chan = NULL;
        p = local_alloc(data, format);
-       chan = local_new(p, AST_STATE_DOWN);
+       if (p)
+               chan = local_new(p, AST_STATE_DOWN);
        return chan;
 }