How to search threw multiple contexts for match

I have a new install (and I am new to Asterisk) of Asterisk 10.6.1 / DAHDI 2.6.1 on a Centos 6.2 box. I cannot get something to work and either I am not understanding it or I cannot seem to figure out how to do this. My system has a PRI and SIP trunks and it connects to three other phone systems via PRI or SIP. There are no phones directly connected to it. Basically right now this is what I have (with the extra stuff removed) and it works:

[from-pstn] ;from the pri
exten => _X.,1,Goto(to-did,${EXTEN},1)

[from-sip] ;from the SIP trunks
exten => _X.,1,Goto(to-did,${SIP_HEADER(TO):5:10},1)

[to-did]
include => no-did-match
exten => 8005551212,1,Goto(to-toshiba,${EXTEN},1)
exten => 8005551213,1,Goto(to-esi,${EXTEN},1)
;..... more numbers

[no-did-match]
exten => _X.,1,Noop(Catch-All DID Match - not found ${EXTEN} - Sending to Default)
exten => _X.,n,Goto(to-zultys,${EXTEN},1)

[to-toshiba]
exten => _X.,1,Dial(${TRUNK_TOSHIBA}/${EXTEN},32,e)
exten => _X.,n,Goto(bad-number,${EXTEN},1)

[to-esi]
exten => _X.,1,Dial(${TRUNK_ESI_PRI}/${EXTEN},32,e)
exten => _X.,n,Goto(bad-number,${EXTEN},1)

[to-zultys]
exten => _X.,1,Dial(${TRUNK_ZULTYS_PRI}/${EXTEN},32,e)
exten => _X.,n,Goto(bad-number,${EXTEN},1)

[from-internal] ; from the other systems for outbound calls
exten => _X.,1,Goto(dial-number,${EXTEN},1)

[dial-number]
exten => _NXXNXXXXXX,1,Goto(dial-TDS,${EXTEN}, 1)
;... more outbound rules and destinations

What I want to do is include the to-did context in the dial-number context so if one of the systems dials a number that exists in another system, it just routes the call internally. Here is how I tried to do it but it does not work:

[from-pstn] ;from the pri
exten => _X.,1,Goto(to-did,${EXTEN},1)
exten => _X.,n,Goto(no-did-match,${EXTEN},1)

[from-sip] ;from the SIP trunks
exten => _X.,1,Goto(to-did,${SIP_HEADER(TO):5:10},1)
exten => _X.,n,Goto(no-did-match,${SIP_HEADER(TO):5:10},1)

[to-did]
exten => 8005551212,1,Goto(to-toshiba,${EXTEN},1)
exten => 8005551213,1,Goto(to-esi,${EXTEN},1)
;..... more numbers

[no-did-match]
exten => _X.,1,Noop(Catch-All DID Match - not found ${EXTEN} - Sending to Default)
exten => _X.,n,Goto(to-zultys,${EXTEN},1)

;....

[from-internal] ; from the other systems for outbound calls
exten => _X.,1,Goto(to-did,${EXTEN},1)
exten => _X.,n,Goto(dial-number,${EXTEN},1)

;....

With that example if the number is not in to-did, it rejects the call in error instead of processing to the next line. I have also tried gosub with the same results. So I read to control sort order use include statements, but it does not work either -

[from-pstn] ;from the pri
include => to-did
include => no-did-match

;.....

[to-did]
exten => 8005551212,1,Goto(to-toshiba,${EXTEN},1)
exten => 8005551213,1,Goto(to-esi,${EXTEN},1)
;..... more numbers

[no-did-match]
exten => _X.,1,Noop(Catch-All DID Match - not found ${EXTEN} - Sending to Default)
exten => _X.,n,Goto(to-zultys,${EXTEN},1)

;.....

[from-internal] ; from the other systems for outbound calls
include => to-did
exten => _X.,1,Goto(dial-number,${EXTEN},1)

;.....

All the calls now just follow the no-did-match destination. If I comment that out and dial one of the listed did numbers it errors out and does not match. I do not understand why, it matches just fine when using a Goto, but by using the include I am not sure how to change the ${EXTEN} for the sip calls to get the number from the SIP header, but right now my priority is getting the pri calls to work. I have tried various combinations is goto, gosub with a gotoif and includes and I cannot get this to work. Anyone have a idea why? Is there a way if you do a goto, and there is not a match, it just continues instead of erroring out and rejecting the call?

Goto, by definition, doesn’t return!

My feeling is that you have a deep misunderstanding of how the dialplan works, but I can’t put my finger on what it is.

[quote=“david55”]Goto, by definition, doesn’t return!

My feeling is that you have a deep misunderstanding of how the dialplan works, but I can’t put my finger on what it is.[/quote]

Would not doubt it. I realized goto does not return after looking at the definition, but gosub should from what I read. I tried using gosub with a return but that would not work either. I am familiar with about 6 different programming languages, 8 different phone systems, but this is different. From what I have read, include should work, but it does not, and I am not sure what I am doing wrong. All I want to do is scan two contexts in order for a match. It works if I include no-did-match in to-did but I do not want that if I sent it the call from the from-internal context. So on a inbound call from either from-pstn or from-sip I want numbers to match the to-did and if there is none follow no-did-match, and on a call from-internal to see if there is a match in to-did then route the call out the system if there is no match without having to duplicate all the information in to-did. I am dealing with 80+ numbers. - Jeremy

You are right, include should work. Can you provide more details, like call logs, configs or at least output from:

dialplan show 8005551212@from-internal ?

[quote=“thor”][quote]include should work, but it does not, and I am not sure what I am doing wrong.

You are right, include should work. Can you provide more details, like call logs, configs or at least output from:[/quote][/quote]

I wish I could, unfortunately this is a live system and I had to get it working so I went a totally different direction. I have it working now the way I want but I an using a odbc-mysql query of the dialed number (much easier to manage a sql database than the conf file directly) that I have a column of numbers, what it terminates to and the trunk string. That works very well because I can test no rows received with a Gotoif and process the call from there. I minimized the amount of contexts that I was using and put most of it in-line so I did not include or goto around much. I had to duplicate most of the routines but 6 lines is better than 80 numbers/lines. From the experiments I noticed when I included a context that used the wildcard ( exten => _X., ) the include would work, but if it had an actual number ( exten => 8005551212, ) it would never match. I could not get it to match using _8005551212 or _XXXXXXXXXX either. I should of saved the file before I made the changes so I could troubleshoot it. - Jeremy