Fix case where local is allocated but not actually called on
[asterisk/asterisk.git] / channels / chan_local.c
index feabb36..4c4d4e1 100755 (executable)
@@ -189,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);
 }
@@ -220,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;
 }