Sip header manipulation for PJSIP

I recently had to add diversion header to our sip packets and we are using PJSIP. It took a while to know how to do that since PJSIP is different than SIP and I will post it here in case someone else wants to do this.

We have several trunks and we wanted to add Diversion header for some of our outbound routes ( based on the region ). If you need it at trunk level, dont need to use Execif part , just set the header:
exten =>s,n,Set(PJSIP_HEADER(add,Diversion)=sip:[email protected])

This is how I did it :

vi /etc/asterisk/extensions_override_freepbx.conf
[func-apply-sipheaders]
include => func-apply-sipheaders-custom
exten => s,1,Noop(Applying custom SIP Headers to channel, Outbound Route= ${ROUTENAME})
;Check if call is going out over XX routes, XX_Local_LD_INT is trunk name
exten => s,n,ExecIf($[“${ROUTENAME}” = “XX_Local_LD_INT”]?Set(PJSIP_HEADER(add,Diversion)=sip:[email protected]))
;Check if it call is going out over YY routes, YY_Local_LD_INT is trunk name
exten => s,n,ExecIf($[“${ROUTENAME}” = “YY_Local_LD_INT”]?Set(PJSIP_HEADER(add,Diversion)=sip:[email protected]))

Change the caller id and sip provider ip address as you need.

And on webgui, change your trunk setting as below :
Asterisk Trunk Dial Options = TWtb(func-apply-sipheaders^s^1)

and use override instead of system.

Does REDIRECTING() not work?

There are cleaner ways of manipulating SIP headers on outgoing trunk calls, and using the override file is to be avoided whenever possible. I recommend you use the native header gosub functions with macro-dialout-trunk-predial-hook

Happy cake day @lgaetz!

2 Likes

Wow, joined in 2008. Is there anyone that’s got a longer cake day?

I can confirm that there is at least one active user with an 18 year creation date.

1 Like

Looks like forum was launched 18 years ago, so there are many that predate me by a year or two. A quick glance in the admin portal shows a quite a few still active.

1 Like

Idid not know how to use it to did not test is
:slight_smile:

So you are saying call func-set-sipheader from predial-? And do not change system asterisk trunk dialout option? I will test it thanks. And Happy Cake day…

Yes. At its simplest, this dialplan

[macro-dialout-trunk-predial-hook]
exten => s,1,NoOp(Entering user defined context [macro-dialout-trunk-predial-hook] in extensions_custom.conf)
exten => s,n,GoSub(func-set-sipheader,s,1(X-Lorne,Happy Cake Day))
exten => s,n,MacroExit()

results in this outbound INVITE

1 Like

Which wont work in FreePBX v17, FYI. Macros have gone away. This still use macros here but not here is going come back to bite people.

I believe Rob and Philippe would have close to the oldest date. I would of joined in 2007 from memory. Wow the time flies.

May 18, 2007