Extension CID being sent over Trunk

I have a central PBX which is connected to a remote office via a dedIcated VPN trunk. This trunk is set to “InterOffice” which means the 3-digit Extension CID is preserved when calls are routed to the Central PBX. This allows the extension CID to be displayed on all internal calls.

However, a call placed in the remote office to a public (Outbound) number, will also be routed to the Central PBX which will then execute the outbound call via its Outbound Route dialplan and associated Outbound Trunk.

The Outbound Route is set as follows:

Route CID: 0xxx xxx xxxx (redacted numbers)
Override Extension: Yes

However, the Extension CID from the remote office is still being passed to the trunk, which then sends this to the SIP Service provider when the Outbound Trunk is set to “Allow Any CID”.

A temporary workaround is to force the Route CID on the Trunk, however this overrides all other CIDs except Emergency CIDs, including foreign CIDS from forwarded external calls.

Confusingly, the Force Trunk CID Help says:

“Intra-Company Routes will always trasmit an extension’s internal number and name.” However, this does not seem to be true when Force Trunk CID is selected, as the Trunk CID is then used.

It would seem, in fact, that the remote office Extension CID is not being treated as an extension, even though “InterOffice” is selected, which would seem to be the more logical behaviour. Instead, it is being treated the same way as foreign CIDS from forwarded external calls. I would have anticipated that a trunk designated as “InterOffice” should not be treated as an external call and should be regarded as an extension for CID purposes.

Can someone please kindly advise if there is a better or recommended way to set this up so that the Route CID is used on Outbound Calls when originating from the remote office.

I solved this in the post here: What is the best way to connect multiple FreePBX together?

The post shows config for an IAX trunk, which is great if that’s what you’re using for the intra-company trunk. If not, you’ll have to modify in order to read/write SIP headers.

Thank you Lorne.

I was using a SIP trunk for the interoffice, but no reason why I can’t change to IAX. My knowledge of these protocols is non-existent.

I’ll give it a try and let you know how it goes.

Hi Lorne,

I have placed the code into extensions_custom.conf, but the CID is not being rewritten. The calls now come in over an IAX trunk set for Intercompany = Yes. However the dump log looks like this:

3881	Dumping Info For Channel: IAX2/RaspbxIAXae-5572:	
3882	================================================================================	
3883	Info:	
3884	Name= IAX2/RaspbxIAXae-5572	
3885	Type= IAX2	
3886	UniqueID= 1616154213 .4673	
3887	LinkedID= 1616154213 .4673	
3888	CallerIDNum= 501	
3889	CallerIDName= (N/A)	
3890	ConnectedLineIDNum= (N/A)	
3891	ConnectedLineIDName=(N/A)	
3892	DNIDDigits= (N/A)	
3893	RDNIS= (N/A)	
3894	Parkinglot=	
3895	Language= en	
3896	State= Ring (4)	
3897	Rings= 0	
3898	NativeFormat= (ulaw)	
3899	WriteFormat= ulaw	
3900	ReadFormat= ulaw	
3901	RawWriteFormat= ulaw	
3902	RawReadFormat= ulaw	
3903	WriteTranscode= No	
3904	ReadTranscode= No	
3905	1stFileDescriptor= -1	
3906	Framesin= 0	
3907	Framesout= 0	
3908	TimetoHangup= 0	
3909	ElapsedTime= 0h0m0s	
3910	BridgeID= (Not bridged)	
3911	Context= macro-dialout-trunk-predial-hook	
3912	Extension= s	
3913	Priority= 2	
3914	CallGroup=	
3915	PickupGroup=	
3916	Application= DumpChan	
3917	Data= (Empty)	
3918	Blocking_in= (Not Blocking)	
3920	Variables:	
3922	ARG1=	
3924	MACRO_CONTEXT=macro-dialout-trunk	
3926	custom=SIP/Andy_voiptalk_Out	
3931	TIOHIDE=no	
3938	HOTDESCKCHAN=RaspbxIAXae-5572	
3941	DIAL_TRUNK=7	
3942	ARG4=on	
3943	ARG3=	
3945	NODEST=	
3950	MOHCLASS=default	
3952	ROUTEID=25	
3954	RECORD_ID=IAX2/RaspbxIAXae-5572	
3955	MIXMON_ID=0x7f5604040310	
3956	LOCAL_MIXMON_ID=0x7f5604040310	
3957	MIXMONITOR_FILENAME=/var/spool/asterisk/monitor/2021/03/19/out-XXXXXXXXXXX-501-20210319-114333-1616154213 .4673.wav	
3958	CALLFILENAME=out-XXXXXXXXXXX-501-20210319-114333-1616154213 .4673	
3960	RECMODE=	
3962	MON_FMT=wav	
3963	FROMEXTEN=501	
3964	TIMESTR=20210319-114333	
3965	YEAR=2021	
3966	MONTH=03	
3967	DAY=19	
3968	NOW=1616154213 	
3971	AMPUSER=	
3973	TOUCH_MONITOR=1616154213 .4673	
3974	================================================================================

A couple of things I note.

This is the inbound Intracompany Route Name

This is the outbound Route Name
(actually not the outroute intended for the number dialled, but that’s another issue)

3963 FROMEXTEN=501
This is the extension number on the Intracompany Route

This is the CID for the outbound trunk

This is the CID for the outbound route

3958 CALLFILENAME=out-XXXXXXXXXXX-501-20210319-114333-1616154213 .4673
This is the number dialled (anonymised) followed by the Extension Number

your code first decides:
; determine if call is intracompany or not
exten => s,n,GotoIf($["${INTRACOMPANYROUTE}"=“YES”]?intra:pstn)

However there is no variable called INTRACOMPANYROUTE listed in the dump. Therefore the call is passed to the pstn section and the CID is not rewritten with the DB value for outboundcid.

Any thoughts please?

Kind regards,

Andy Woolford

Not sure what’s going on, if you want to share a link(s) to the entire call flows on each leg, I will take a look. I just tested, and any calls that use an Outbound Route marked intracompany will get the channel variable INTRACOMPANYROUTE set to YES. Share traces via pastebin:

Perhaps the use case is different…

I have a trunk in our central PBX which is set only to listen for calls originating from a VPN (Intracompany) PBX. So any calls coming from that PBX will either go to an internal number extension or to an outbound PSTN number according to the central PBX dialplan. The remote PBX has an intracompany outbound route that ensures only the extension number is sent to the Central PBX. On this basis we will only ever get a 3 digit exension CID coming in through the intracompany trunk.

The call is then routed using the standard dialplans in the central PBX. Calls that are for a PSTN should have the CID for the outbound route. Calls that go to an internal exension should have the CID for the calling extension.

The current behaviour is that all outbound calls are ignoring the CID in the Outbound route and only sending the CID for the 3 digit extension which is calling via the Intracompany Trunk.PBX%20Layout

Sorry for the delay… here is the pastebin link. Thanks for the offer to look through it.


The expectation with the recommendation above is that you’re using stock FreePBX contexts, but you’re not:


You will have to adapt the concept to your unique situation.

The internal remote context only restricts the outbound routes using the Custom Context module.
Also, I note your code “dialout-trunk-predial-hook” is called from the [macro-dialout-trunk] context which is still in the flow after the [from-internal-remote] context has exited.

For testing I could remove this. Which context would you recommend? [from-trunk] ?

You’re going to be on your own here. The big picture call flow is as follows:

  • If outbound call is using intracompany route use the predial hook to preserve the PSTN callerid
  • If the outbound call is not using an intracompany trunk, use the predial hook to look for preserved CID and use it for the call.

The same context works for both cases.

OK Many thanks. I’ll see if I can hack it :slight_smile:

Always appreciate your help. :+1:

Hi Lorne,

Just to close this issue, the solution for me was to set the variable INTRACOMPANYROUTE in the inbound custom context so that the incoming call from a remote office could be caught reliably by the [macro-dialout-trunk-predial-hook]. However, as my application is based only on the central server - not the remote servers, the modification to the CID is done by setting CALLERID(all) rather than changing the outbound IAX CID in the remote office which seemed to be your intention.

I have two remote offices which enter FreePBX at:

  1. [from-internal-remote] and
  2. [from-internal-remote-uk]

These custom contexts include [from-internal-remote-custom] and [from-internal-remote-uk-custom] into which I have included a new context [from-internal-remote-common].
This new context will set the variable INTRACOMPANYROUTE=YES for any calls originating from a remote office.

The caveat is that the [macro-dialout-trunk-predial-hook] is being called after the [outbound-callerid] macro in the outbound trunk flow, which normally ensures that the EMERGENCY or forced TRUNK CID will be used if selected. For this reason, I have called this macro again to ensure the EMERGENCY and TRUNK CID is not overwritten with apparently no ill effects.

This code gives the desired behaviour for me, that calls originating from a remote office will have the extension CID overwritten by the TRUNKOVERRIDECID if defined in the route. The switch “Override Extension” is ignored in this case. If the route CID exists, then it will be used. The switch will of course operate normally for all other calls entering the PBX from extensions or non-intracompany trunks.

Thanks for your help which pointed me in the right direction.

include => from-internal-remote-common

include => from-internal-remote-common

; set INTRACOMPANYROUTE=YES for calls entering the [from-internal-remote-common] context
exten => _X!,1,Noop(Entering user defined context [from-internal-remote-common] in extensions_custom.conf)

exten => s,1,Noop(Entering user defined context [macro-dialout-trunk-predial-hook] in extensions_custom.conf)
exten => s,n,DumpChan ; uncomment for debug
exten => s,n,Noop(OUTNUM: ${OUTNUM})

; determine if call is intracompany or not
exten => s,n,GotoIf($["${INTRACOMPANYROUTE}"=“YES”]?intra:end)

; calls from intracompany routes come here
exten => s,n(intra),Noop(This call uses an Intra-Company route)

exten => s,n,ExecIf($["${TRUNKCIDOVERRIDE}"!=""]?set(CALLERID(all)=${TRUNKCIDOVERRIDE}))
exten => s,n,Noop(If TRUNKCIDOVERRIDE exists: Set outbound cid: ${TRUNKCIDOVERRIDE})
exten => s,n,goto(end)

;call Macro [outbound-callerid] again to ensure EMERGENCY or forced TRUNK CID is not overwritten
exten => s,n(end),Macro(outbound-callerid,${DIAL_TRUNK})

exten => s,n,MacroExit

<Edit: Correction due to syntax error at Line 24. Enclose variable in quotes and evaluate as text>

1 Like

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