Change the DID number format in PJSIP Trunk

I have a PJSIP trunk that is working well, the problem is all the DID numbers for the inbound calls have “>” sign at the end of the DID number, in order to solve this issue I have used a format of " _+251xxxxxxxx1." in order to match the exact DID number; this solution also works but the problem again with this format is when I send the destination of incoming call to other trunk it is sending the DID as “+251xxxxxxxx1%3e” (%3e is the hex char value of >).

What I want is to change the extensions_custom file and to manipulate the DID like this

  1. Call comes from DID Number with > sign at the end ==>
  2. Remove the > sign at the end of the DID number ==>
  3. send the call again to inbound calls (so I can set destination to the properly formatted number)

If there is other solution do not hesitate to inform me.


I assume that the Context for the problematic trunk is from-pstn-toheader, but because the provider is sending in an ususual (or perhaps malformed) format, a > is being improperly included.

Make a copy of from-pstn-toheader and add it to extensions_custom.conf, using a different name such as from-pstn-mytrunk. Modify it to deliver the number without the trailing >, then change the Context for the trunk to from-pstn-mytrunk. Your normal Inbound Routes will now see DID Number as the actual number called.

You need to write a custom context inside the extensions_custom.conf file. Something like this should do the trick (not tested):

exten =>  _.,1,Goto(from-pstn,${CUT(${PJSIP_HEADER(read,To)},>,1)},1)

This removes the character > from the number and sends it to the context from-pstn. Also don’t forget to add the newly created context from-pstn-remove-char in the pjsip settings of your trunk.

Edit: Changed From to To

Probably ‘FILTER’ not ‘CUT’

exten => s,1,Set(foo=${FILTER(+0-9,${variable})})

I tried this and it makes the DID ‘s’

When I test this custom extension the system does not receive any calls

Currently I have only one destination for all DID numbers, so I am using the following custom extension

exten => _.,1,Goto(from-pstn,12345,1)

So I create an inbound route with DID of 12345 and everything works fine. But this option is not scalable when every DID needs it own destination.

That code assumes the DID is in the From user part, which is unlikely. You haven’t told us where the DID is, so we can’t give code specific to your case and you need to make reasonable adaptations.

I didn’t understand your question, what I am currently using is whatever DID number came it convert to 12345, so in the inbound route I can assign a destination.

My problem is I want to eliminate the ‘>’ sign at the end of inbound calls DID numbers so I can set proper destination for them.
So when someone calls the DID number with ‘>’ sign => the ‘>’ sign will be removed => then the number will be sent to inbound route.

My problem is I want to eliminate the ‘>’ sign at the end of inbound calls DID numbers so I can set proper destination for them.
So when someone calls the DID number with ‘>’ sign => the ‘>’ sign will be removed => then the number will be sent to inbound route.

Yes. This is exactly what the privided code snippet does.

But as @david55 already mentioned you have to look in what part of the SIP header your trunk provider is sending the number. In the example it uses the FROM part which is wrong. I meant TO. Sorry for the confusion. For example our provider sends the DID in the E. 164 format in the P-Called-Party-ID part. In this case it would be something like this.

exten =>  _.,1,Goto(from-pstn,${CUT(${PJSIP_HEADER(read,P-Called-Party-ID)},>,1)},1)

DID number is not a concept that exists in SIP. By default, FreePBX assumes that the DID is contained in the user part of the request URI (the URI on the first line of the request). Many providers give it i the user part of the To header URI, Some provide it in other headers, and some may not provide it at all.

Generally something is broken in the provider if > appears in it.

What is being asked is from where in the request are you taking the “DID”?

What is the value of Context in your trunk settings?

What does the To header look like on an incoming INVITE (use pjsip logger or sngrep to view it)?

Stewart is asking for the value of this when you see the >, not the latest attempt at configuring it.

The context I am using is from-pstn-toheader

Here is the PJSIP log

<--- Received SIP request (1642 bytes) from UDP:10.x.x.x:5060 --->
INVITE sip:[email protected]:5060;line=vmtaiqi SIP/2.0
Via: SIP/2.0/UDP 10.x.x.x:5060;branch=z9hG4bK-*2*-4-74695498c2b4a256d186taN0
To: <tel:+251xxxxxxxxx>
From: <tel:+251xxxxxxxxx>;tag=ztesipE_mK4lmDDWW*14-3-20481*edif.14
Call-ID: 8US2XCiSY3oiChdfpY48kncu5FEr3bbcj@zteims
CSeq: 1000 INVITE
Max-Forwards: 63
Contact: <sip:10.x.x.x:5060;b_p=DIAG_2_0_020c186c_5d0;zte-did=14-3-20481-10515-12-819-6071;zte-uid=100000+251xxxxxxxxx>
Supported: histinfo,timer,100rel
P-Early-Media: supported
Session-Expires: 1800;refresher=uac
Min-SE: 90
P-Asserted-Identity: <tel:+251xxxxxxxxx>
Privacy: none
History-Info: <tel:+251xxxxxxxxx>;index=1
History-Info: <sip:[email protected];cause=302>;index=1.1
Accept: application/sdp,multipart/mixed
X-Extend-Info: srvtag=67108864
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 676

This is the from-pstn-toheader context in FreePBX:

; from-pstn-toheader
; The context is designed for providers who send the DID in the TO: SIP header
; only. The format of this header is:
; To: <sip:[email protected]>
; So the DID must be extracted between the sip: and the @, which this does
exten =>  _.,1,NoOp(Attempting to extract DID from SIP To header)
exten =>  _.,n,gotoif($["${CHANNEL(channeltype)}"="SIP"]?SIP)
exten =>  _.,n,gotoif($["${CHANNEL(channeltype)}"="PJSIP"]?PJSIP)
exten =>  _.,n,NoOp(Unable to determine SIP channel type)
exten =>  _.,n,goto(from-pstn,${EXTEN},1))
exten =>  _.,n(SIP),Goto(from-pstn,${CUT(CUT(SIP_HEADER(To),@,1),:,2)},1)
exten =>  _.,n(PJSIP),Goto(from-pstn,${CUT(CUT(PJSIP_HEADER(read,To),@,1),:,2)},1)

but the problem is that the tel: URI does not contain an @ character.

I believe that you can use the > character as the terminator instead. Since you know that your trunk is pjsip, try adding something like this to /etc/asterisk/extensions_custom.conf :

exten =>  _.,1,NoOp(Attempting to extract DID from SIP To header)
exten =>  _.,n,Goto(from-pstn,${CUT(CUT(PJSIP_HEADER(read,To),>,1),:,2)},1)

and change the Context for the trunk to from-pstn-toheader-tel .

1 Like


The custom extension you provided works as intended, now all inbound calls came with correctly formatted DID so I can now set proper destination for each.

Thanks to all of you for your help with the problem I was facing. I really appreciate it!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.