DID Number never matches

Hi all,

Since few days , I registered my trunk with my SIP provider , inbound and outbound calls were working quite well but I configured the “inbound DID” with “Any” redirected to my extension/ring group. We want add some numeros and when I try to add my number in the “Inbound DID” , inbound calls don’t work. I have a “DID not match” in the Asterisk console.

So , I called my SIP provider to be sure of the format of the num when relayed , so I tried with the international/national format and more ( 0033XXXXXXXXX, 0XXXXXXXXX, even with patterns but nothing is working I can’t figure it out )

I hope someone had the same trouble, thanks for reading and have a nice day.

The /var/log/asterisk/full log should tell you exactly what the DID number is. In fact, you might be getting a message in the log file that says something like “You should probably have an incoming route for this”.

If you’d rather, you might be able to see the information you need from the Asterisk CLI. Start that by logging into the server as ‘root’ and running the program ‘asterisk -vvvvr’.

I would like to thank you for your reply.

This is the log of a phone call that works ( but with Any DID Number for inbound routes )

– Executing [s@from-pstn:1] Set(“PJSIP/trunk PJSIP-00000003”, “__DIRECTION=INBOUND”) in new stack
– Executing [s@from-pstn:2] Gosub(“PJSIP/trunk PJSIP-00000003”, “app-blacklist-check,s,1()”) in new stack
– Executing [s@app-blacklist-check:1] GotoIf(“PJSIP/trunk PJSIP-00000003”, “0?blacklisted”) in new stack
– Executing [s@app-blacklist-check:2] Set(“PJSIP/trunk PJSIP-00000003”, “CALLED_BLACKLIST=1”) in new stack
– Executing [s@app-blacklist-check:3] Return(“PJSIP/trunk PJSIP-00000003”, “”) in new stack
– Executing [s@from-pstn:3] ExecIf(“PJSIP/trunk PJSIP-00000003”, “1?Set(__FROM_DID=s)”) in new stack
– Executing [s@from-pstn:4] Set(“PJSIP/trunk PJSIP-00000003”, “CDR(did)=s”) in new stack
– Executing [s@from-pstn:5] ExecIf(“PJSIP/trunk PJSIP-00000003”, “0 ?Set(CALLERID(name)=mypersonnalNum)”) in new stack

And the log of a inbound call that is not working ( with the DID inbound route specified with my num )

– Executing [s@from-pstn:1] NoOp(“PJSIP/trunk PJSIP-00000005”, “No DID or CID Match”) in new stack
– Executing [s@from-pstn:2] Answer(“PJSIP/trunk PJSIP-00000005”, “”) in new stack
[2016-06-22 16:50:34] WARNING[11145][C-00000005]: chan_sip.c:22377 func_header_read: This function can only be used on SIP channels.
– Executing [s@from-pstn:3] Log(“PJSIP/trunk PJSIP-00000005”, "WARNING,Friendly Scanner from ") in new stack
[2016-06-22 16:50:34] WARNING[11145][C-00000005]: Ext. s:3 @ from-pstn: Friendly Scanner from
– Executing [s@from-pstn:4] Wait(“PJSIP/trunk PJSIP-00000005”, “2”) in new stack
> 0x7f6a608a3e10 – Probation passed - setting RTP source address to X.X.X.X:37792 (trunk of my provider )
== Spawn extension (from-pstn, s, 4) exited non-zero on ‘PJSIP/trunk PJSIP-00000005’
– Executing [h@from-pstn:1] Macro(“PJSIP/trunk PJSIP-00000005”, “hangupcall,”) in new stack
– Executing [s@macro-hangupcall:1] GotoIf(“PJSIP/trunk PJSIP-00000005”, “1?theend”) in new stack

But at any moment , I can see the DID Number…

You provider is not delivering the DID in the conventional way. It’s possible that your trunk configuration is wrong or they are delivering the DID in the SIP header in which case you can set your trunk context to:


They are just delivering the DID information in the from header, change to the context that @lgaetz suggests and it will work.

Last week, we were talking about a provider that didn’t provide DID information because they only allow one DID per customer. Is it possible that they simply aren’t sending the DID information?

If that is the case (which I don’t know, try the “from-pstn-toheader” context first) and if that doesn’t work, you may end up having to just used the “any/any” route and call it good.

Hi , thank you all for your response , I think that you are right and I hope so , that’s my last solution xD

Indeed , in a tutorial , they tell to add in /etc/asterisk/extensions_custom.conf the following lines : ( OVH is a french provider )

exten => s,1,Goto(from-trunk,${CUT(CUT(SIP_HEADER(To),@,1),:,2)},1)

But this is my question : in which files do I have to add “from-pstn-toheader” and what parameters do I have to enter ? Sorry I’m new in the world of Asterisk/freepbx

Edit : I saw that I had in the extensions.conf :

exten => _.,1,Goto(from-pstn,${CUT(CUT(SIP_HEADER(To),@,1),:,2)},1)
How could I modify this function to get my number to the good format ? ( 033XXXXXXXXX ) france format

Edit 2 : Is it possible to retrieve the SIP_HEADER with a Chan_PJSIP ?

[2016-06-22 16:50:34] WARNING[11145][C-00000005]: chan_sip.c:22377
func_header_read: This function can only be used on SIP channels

1 Like

The underscores got dropped in the message, but the from-pstn-toheader already does what your provider says to do.

In your trunk definition, there is a “context” setting. It might be “default” or “from-pstn”. Change it to “from-pstn-toheader” and you should be able to move on to the next troubleshooting step.

Yes thanks , I’m on a good way !

In my PJSIP trunk settings , I set the the context to “from-pstn-toheader”

This is the content of my /etc/asterisk/extensions.conf :

exten => _.,1,Goto(from-pstn,${CUT(CUT(PJSIP_HEADER(To),@,1),:,2)},1) --> But do I have to go to the same context ?

include => from-pstn-custom ; create this context in extensions_custom.conf to include customizations
include => ext-did
include => ext-did-post-custom
include => from-did-direct
include => ext-did-catchall ; THIS MUST COME AFTER ext-did
include => from-pstn-toheader

And I have included from-pstn-toheader in the from-pstn context

As you said , I have moved to the next troubleshooting step :slight_smile:
Now , I have a Error and Warning :

[2016-06-24 11:23:07] ERROR[11578][C-00000000]: res_pjsip_header_funcs.c:465 func_read_header: This function requires a header name.
[2016-06-24 11:23:07] WARNING[11578][C-00000000]: chan_sip.c:22377 func_header_read: This function can only be used on SIP channels.
[2016-06-24 11:23:07] WARNING[11578][C-00000000]: Ext. s:3 @ from-pstn: Friendly Scanner from
== Spawn extension (from-pstn, s, 7) exited non-zero on ‘PJSIP/PJSIP Trunk-00000000’
== Spawn extension (macro-hangupcall, s, 4) exited non-zero on ‘PJSIP/PJSIP Trunk-00000000’ in macro ‘hangupcall’
== Spawn extension (from-pstn, h, 1) exited non-zero on ‘PJSIP/PJSIP Trunk-00000000’

You need to make these code blocks - the underscores are getting eaten by the editor and it makes it hard to verify your context is correct.

Wait, what?

You have the [from-pstn-toheader] context included in the [from-pstn] context, so you don’t have to include it again. Thoretically, if you call [from-pstn] from your inbound trunk context, you should get the [from-pstn=toheader] for free.

To say that another way, if you include [from-pstn] in the trunk definition, you should be getting the [from-pstn-toheader] context “for free”.

Your provider is telling you “if you run generic Asterisk, add this.” Since you are running FreePBX to manage your Asterisk, you get it as a benefit of the program. So, with that out there, I recommend you undo almost everything you’ve tried and simply set up your Trunk with the context “from-pstn”.

This isn’t an order or a demand - just a suggestion.

My experience with PJSIP to the outside world isn’t super positive. My providers don’t support it, so I always end up using Chan-SIP for my external SIP connections. You might consider switching your exterior connection from PJ-SIP to ChanSIP. This will simplify a couple of things, and get rid of the warnings and errors you are seeing. of course, if you’re happy with, keep going, but there are enough strange things with PJ-SIP that it might help simplify your setup.

Hi , thanks for your response.

I agry with you about registering in Chan_SIP but I can’t register with that , wheras PJSIP is working fine.

I have succeed to retrieve my DID number. The problem was in the function PJSIP_HEADER, so the line in the /etc/asterisk/extensions.conf

exten => s,1,Goto(from-trunk,${CUT(CUT(PJSIP_HEADER(To),@,1),:,2)},1)

would return my DID number like 0033XXXXXXXXX ( after inspecting packets )

So I replaced the function by something in the "hard way " , I have put my number like :

exten => s,1,Goto(from-trunk,0033XXXXXXXXX,1)

And it is working ! But ( there is always a but …) I have 5 numeros redirecting to my principal trunk line with the presentation of the numero.
SO I thought , “Ok , let’s add my numbers !” My file looks like this :

exten => s,1,Goto(from-trunk,0033XXXXXXXX1,1)
exten => s,1,Goto(from-trunk,0033XXXXXXXX2,1)
exten => s,1,Goto(from-trunk,0033XXXXXXXX3,1)

And when I call 0033XXXXXXXX2 or 0033XXXXXXXX3 ( they are redirected to different ring groups/extensions) , it is always the ring group or extension of the first num 0033XXXXXXXX1 , what do I miss , this is making me out of my mind lol

Thanks for reading and sorry for the long post ( again ) ^^

exten => s,1,Goto(from-trunk,0033XXXXXXXX1,1)
exten => s,n,Goto(from-trunk,0033XXXXXXXX2,1)
exten => s,n,Goto(from-trunk,0033XXXXXXXX3,1)

Hi cynjut , I did the modification in the conf file :

exten => s,1,Goto(from-trunk,00334XXXXXXX0,1)
exten => s,n,Goto(from-trunk,00334XXXXXXX1,1)
exten => s,n,Goto(from-trunk,00334XXXXXXX2,1)
exten => s,n,Goto(from-trunk,00334XXXXXXX3,1)

But when I try to call all the numbers except the first one , the first one keeps ringing… I’ll try to learn the syntax of the file to make thinks work but a little help would be so nice ! :slight_smile:
Thanks , have a nice day

Edit : Priority letter nPriority numbers can also be simplified by using the letter n in place of the priority numbers greater than one. The letter n stands for next, and when Asterisk sees priority n it replaces it in memory with the previous priority number plus one. Note that you must still explicitly declare priority number one.

So , I’m forced to retrieve the DID Number in a dynamic way right ? But there is my question , if the function PJSIP_HEADER requires a name as it is told in the error , what I have to put ?
Or do I have to do a PJSIPAddHeader first ?

Edit 2 :
I know some stuff in programming but I have to
say that I don’t really understand what this line really return ( this
is provide by my sip provider for chan_sip :
exten => s,1,Goto(from-trunk,${CUT(CUT(SIP_HEADER(To),@,1),:,2)},1) --> provide by sip provider
exten => s,1,Goto(from-trunk,${CUT(CUT(PJSIP_HEADER(To),@,1),:,2)},1) --> to match my case

Because when I take my SIP packet :

Retransmitting #3 (NAT) to X.X.X.X:5060:
REGISTER sip:mytrunk SIP/2.0
Via: SIP/2.0/UDP X.X.X.X:5060;branch=z9hG4bK51c03678;rport
Max-Forwards: 70
From: ;tag=as61398322
**To: sip:00334XXXXXXXX@mytrunk **
Call-ID: 6681469103d0b6fc6484e92a5048406c@[::1]
Supported: replaces, timer
User-Agent: FPBX-13.0.119(13.7.1)
Expires: 3600
Content-Length: 0

-I understand that in that way : Take the “To” line
cut with the “@” as the seperate char and take the first column so you
get sip:00334XXXXXXXX , then recut with the “:” as the seperate char and
take the 2nd colum so you retrieve the DID number, but the function in
Chan_SIP and Chan_PJSIP don’t have the same comportement because
Chan_PJSIP needs a name ?

I saw the syntax PJSIP_HEADER(action,name[,number]) and the source code of the error :

if (ast_strlen_zero(args.header_name)) {
ast_log(AST_LOG_ERROR, “This function requires a header name.\n”);
return -1;
But I can’t see what I’m missing ( a header name , but what ? …)

I think that the problem is all in the syntax of the extensions.conf
line but I don’t still have the knowledge to correct myself.

Edit 3 : I finally did it ! I can retrieve my DID with :

exten => s,1,Goto(from-trunk,${CUT(CUT(PJSIP_HEADER(read,To),@,1),:,2)},1)

Thank you all for your support :smiley:


Thank you that worked for me.

Got the DID in the TO header from a Broadsoft gateway:

To: "58770XXXX 58770XXXX"sip:[email protected].

changed the context “from-pstn” in the trunk to “from-pstn-toheader”

And problem solved.