Add custom SIP header to PJSIP trunk

EDIT: See Maple’s post below for a much better and cleaner way to do this.

I realize this is an old topic, but I found it by searching for something very close to the title of the thread, so I imagine others are finding it too. The reason this isn’t working now is the line I quoted above. It probably worked when this thread was started, but in current releases of FreePBX 14 it doesn’t. That’s because by default, FreePBX sends these options in a trunk Dial string:

TWtb(func-apply-sipheaders^s^1)

If you override the default options to Ttb(custom-privacy-header^s^1) you would think that’s what FreePBX would use, but noooo… what it really sends as part of the Dial options is

Ttb(custom-privacy-header^s^1)b(func-apply-sipheaders^s^1)

Yes, that’s two b options, and it appears that Asterisk will only accept one and that it takes the last one (which you will see if you watch the CLI). The problem is that FreePBX is going to this context:

[func-apply-sipheaders]
include => func-apply-sipheaders-custom
exten => s,1,Noop(Applying SIP Headers to channel)
exten => s,n,Set(SIPHEADERKEYS=${HASHKEYS(SIPHEADERS)})
exten => s,n,ExecIf($["${HASH(SIPHEADERS,Alert-Info)}" = "unset"]?Set(Rheader=1))
exten => s,n,While($["${SET(sipkey=${SHIFT(SIPHEADERKEYS)})}" != ""])
exten => s,n,Set(sipheader=${HASH(SIPHEADERS,${sipkey})})
exten => s,n,SIPAddHeader(${sipkey}: ${sipheader})
exten => s,n,Set(PJSIP_HEADER(add,${sipkey})=${sipheader})
exten => s,n,EndWhile
exten => s,n,ExecIf($["${Rheader}" = "1"]?SIPRemoveHeader(Alert-Info:))
exten => s,n,ExecIf($["${Rheader}" = "1"]?Set(PJSIP_HEADER(remove,Alert-Info)=))
exten => s,n,Return()

;–== end of [func-apply-sipheaders] ==–;`

Since it has the include => func-apply-sipheaders-custom I tried adding that context in extensions_custom.conf but for some reason FreePBX cheerfully ignores it (I will never understand why those non-working includes are there). So the only solution I could figure out is to copy that entire context to extensions_override_freepbx.conf and make modifications there. So at the top of that file you might change it to look like this:

[func-apply-sipheaders]
include => func-apply-sipheaders-custom
exten => s,1,Noop(Applying SIP Headers to channel)
exten => s,n,Set(PJSIP_HEADER(add,Privacy)=id)
.....

Or if you want to only use it on a specific trunk, this may work (replace trunkname with the real trunk name)…

exten => s,n,ExecIf($["${CUT(DIALEDPEERNUMBER,@,2)}" = "trunkname-PJSIP"]?Set(PJSIP_HEADER(add,Privacy)=id))

I really have no idea what that [func-apply-sipheaders] context is doing, or if there is a better way to do this. I would be very happy if there were a better or easier way, because I really don’t like using extensions_override_freepbx.conf because you have no idea when the the code you have modified might be changed in a new release of FreePBX. But I don’t see any other hooks to insert this into the outgoing channel.

1 Like