Adding a header for correlating calls

Hi,
when doing a call from my extension to an external number fpbx / asterisk creates to call-ids:
call-id a: extension to asterisk
call-id b: asterisk to provider / carrier
X-CID (containg call-id a) in call b, to let 3rd party product be able to correlate the calls.

afterwards i need the same for the other direction:
call-id a: asterisk to provider / carrier
call-id b: extension to asterisk
X-CID (containg call-id a) in call b, to let 3rd party product be able to correlate the calls.

Now I need a correlation between both calls. For this I want to add a custom header. I am using PJSIP.
problem:
I don’t know how to do this. I tried playing around with extension_custom.conf:

[macro-dialout-trunk-predial-hook]
exten => b,1,Set(pjsipCallId=CHANNEL(pjsip,call-id)
exten => b,n,Set(PJSIP_HEADER(add,X-CID)=${pjsipCallId})
exten => b,n,Return

…, but at least not successull

Can someone tell me where I can find the internal routing logic containing the contexts etc.? was searching for a small chart.

regards,
andre

edit - the following technique will no longer work for adding custom headers

To add a custom header to a PJSIP channel:

For pjsip, the task is complicated, you have to be aware of which channel (the callee or the caller) that you are applying the sip header, which means the custom header must be done as part of the dial pre-dial handler. First add a block of custom code to extensions_custom.conf

; example for outbound calls on specific trunk (requied for pjsip, but also works with chan_sip, chan_iax)
[astrakid-custom-header]
exten => s,1,noop(Entering user defined context [astrakid-custom-header] in extensions_custom.conf)
exten => s,n,Set(PJSIP_HEADER(add,X-Tag)=CDITC)    ; for custom header
exten => s,n,Set(PJSIP_HEADER(add,Privacy)=id)     ; use this header to force privacy for some providers
exten => s,n,Return

Then in the trunk settings, you need to override the default dial options and add a custom dial option with a predial handler (using the dial option lower case b) referencing the custom code block:

Ttb(astrakid-custom-header^s^1)

great, that brought me a step ahead. with that i was able to add the header, second problem was getting call-id from internal call-leg, but that was possible by reading the call-id from that call-leg before and put it into an inherited variable!
thanks a lot.
this direction is now working.
can you give me a final hint for the external call hitting my asterisk? how can i force fpbx to call that custom context when dialing extensions?

Example to execute a block of dialplan on every inbound call. Calls arrive in FreePBX in the context defined in the trunk parameters with an extension set to the DID or if the DID is unknown the extension will be ‘s’. There is also the context ‘from-pstn-custom’ SHOULD NOT BE USED EVER, because each line you add this this context will overwrite one of the dialplan lines generated by FreePBX.

Set your inbound trunks to your own context, in this case from-pstn-dumpchan In the file, extensions_custom.conf add the following:

[from-pstn-dumpchan]
exten => _.,1,Noop(Entering user defined context [from-pstn-dumpchan] in extensions_custom.conf)
exten => _.,n,DumpChan
exten => _.,n,Goto(from-pstn,${EXTEN},1)

thanks, but that is not what i need, maybe i wasn’t clearly enough.
every call that is hitting my asterisk is already hitting my extensions_custom.conf, and i will read the pjsip-caller-id and put it into a variable which is inherited and so it is reusable for adding a custom header in the following call-leg.
but i find no way to edit this new call-leg. as far as i see the [macro-dial] is called, but [macro-dial-custom] seems not to work.

incoming external call (call leg a):
– Executing [+123123123@from-pstn:4] Set(“PJSIP/123123123-00000065”, “pjsipCallId=p46t1507050425m379978c605531s2”) in new stack
– Executing [+123123123@from-pstn:5] Set(“PJSIP/123123123-00000065”, “PJSIP_HEADER(add,X-CID)=p46t1507050425m379978c605531s2”) in new stack
– Executing [+123123123@from-pstn:6] ExecIf(“PJSIP/123123123-00000065”, “0 ?Set(CALLERID(name)=+456456456456)”) in new stack

=> that is fine.
but where can i hook into the call to the extension to add the custom header?

made it work by editing extensions_additional.conf:
[func-apply-sipheaders]
include => func-apply-sipheaders-custom
exten => s,1,Noop(Applying SIP Headers to channel)
;;; added line - pjsipCallId is set before in extensions_custom.conf ;;;
exten => s,n,set(PJSIP_HEADER(add,X-CID)=${pjsipCallId})
[…]

but this line in any custom-parts in extensions_custom.conf doesn’t work. tried
[macro-dial-custom]
[func-apply-sipheaders-custom]
[macro-user-callerid-custom]

if i could get away from changing extensions_additional.conf i would be very happy!