sip diversion header from iax2 call

asterisk_forwarding

Hello there,
i’m dealing with headache of configration when dealing with forwarded calls. I tried to put the case in the image above. From PSTN asterisk receives dial to number 1200 from number 1300 via sip, asterisk dials asterisk IVR via iax2, asterisk IVR in some cases forwards the call to number 1400 through iax2 to asterisk and asterisk then dials 1400 through sip to PSTN.

The catch here is that we need to initialize call from asterisk to number 1400 in PSTN with sip diversion header containing original caller 1200. I’ve tried to add diversion header on asterisk IVR but to no avail. When reviewing sip packets from asterisk to PSTN there is no such header. Below there are relevant logs from asterisk IVR regarding diversion header. I just can’t figure out how to transfer diversion information from asterisk IVR to asterisk, please help :slight_smile:

VERBOSE[7780][C-000088d7] pbx.c: Executing [1200@from-pstn:1] Set("IAX2/asteriskIVR_asterisk-12588", "__DIRECTION=INBOUND") in new stack
VERBOSE[7780][C-000088d7] pbx.c: Executing [1200@from-pstn:2] Gosub("IAX2/asteriskIVR_asterisk-12588", "sub-record-check,s,1(in,1300,dontcare)") in new stack
VERBOSE[7780][C-000088d7] pbx.c: Executing [s@sub-record-check:1] GotoIf("IAX2/asteriskIVR_asterisk-12588", "0?initialized") in new stack
...
VERBOSE[7780][C-000088d7] pbx.c: Executing [072@ext-queues:51] Queue("IAX2/asteriskIVR_asterisk-12588", "072,t,,custom/ivr-072-112-operator-announcement,120,,,,,") in new stack
VERBOSE[7780][C-000088d7] res_musiconhold.c: Started music on hold, class 'default', on channel 'IAX2/asteriskIVR_asterisk-12588'
VERBOSE[7852][C-000088d7] app_mixmonitor.c: Begin MixMonitor Recording IAX2/asteriskIVR_asterisk-12588
VERBOSE[7780][C-000088d7] app_queue.c: Called Local/1400@from-queue/n
VERBOSE[7853][C-000088d7] pbx.c: Executing [1400@from-queue:1] Set("Local/1400@from-queue-000051b8;2", "QAGENT=1400") in new stack
...
VERBOSE[7853][C-000088d7] pbx.c: Executing [1400@from-internal:4] GosubIf("Local/1400@from-queue-000051b8;2", "1?sub-diversion-header,s,1()") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@sub-diversion-header:1] Set("Local/1400@from-queue-000051b8;2", "DIVERSION_REASON=no-answer") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@sub-diversion-header:2] Gosub("Local/1400@from-queue-000051b8;2", "func-set-sipheader,s,1(Diversion,<tel:1200>;reason=no-answer;screen=no;privacy=off)") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@func-set-sipheader:1] NoOp("Local/1400@from-queue-000051b8;2", "Sip Add Header function called. Adding Diversion = <tel:1200>;reason=no-answer;screen=no;privacy=off") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@func-set-sipheader:2] Set("Local/1400@from-queue-000051b8;2", "HASH(__SIPHEADERS,Diversion)=<tel:1200>;reason=no-answer;screen=no;privacy=off") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@func-set-sipheader:3] Return("Local/1400@from-queue-000051b8;2", "") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@sub-diversion-header:3] Return("Local/1400@from-queue-000051b8;2", "") in new stack
...
VERBOSE[7853][C-000088d7] app_stack.c: IAX2/asteriskIVR_asterisk-24725 Internal Gosub(func-apply-sipheaders,s,1(1)) start
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:1] NoOp("IAX2/asteriskIVR_asterisk-24725", "Applying SIP Headers to channel IAX2/asteriskIVR_asterisk-24725") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:2] Set("IAX2/asteriskIVR_asterisk-24725", "TECH=IAX2") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:3] Set("IAX2/asteriskIVR_asterisk-24725", "SIPHEADERKEYS=Alert-Info,Diversion") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:4] While("IAX2/asteriskIVR_asterisk-24725", "1") in new stack
VERBOSE[7853][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:5] Set("IAX2/asteriskIVR_asterisk-24725", "sipheader=unset") in new stack
...
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@macro-dialout-trunk:30] Dial("Local/1400@from-queue-000051b9;2", "IAX2/asteriskIVR_asterisk/1400,300,b(func-apply-sipheaders^s^1,(1))U(sub-send-obroute-email^1400^1400^1^1736763769^Q3 HSP L2:1300^1300)") in new stack
VERBOSE[7859][C-000088d7] app_stack.c: IAX2/asteriskIVR_asterisk-23271 Internal Gosub(func-apply-sipheaders,s,1(1)) start
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:1] NoOp("IAX2/asteriskIVR_asterisk-23271", "Applying SIP Headers to channel IAX2/asteriskIVR_asterisk-23271") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:2] Set("IAX2/asteriskIVR_asterisk-23271", "TECH=IAX2") in new stack

You cut off the output from the apply-sip-headers GoSub so we can’t see what is happening but unless the outbound channel is chan_pjsip or chan_sip, the headers won’t be applied.

Uncut apply-sipheaders log bellow. So if i understand correctly, when routing call through iax2 → sip there is no way to preserve information about call forward in iax2 to be reconstructed as diversion header in sip?

VERBOSE[7859][C-000088d7] pbx.c: Executing [s@macro-dialout-trunk:30] Dial("Local/1400@from-queue-000051b9;2", "IAX2/asteriskIVR_asterisk/1400,300,b(func-apply-sipheaders^s^1,(1))U(sub-send-obroute-email^1400^1400^1^1736763769^Q3 HSP L2:1300^1300)") in new stack
VERBOSE[7859][C-000088d7] app_stack.c: IAX2/asteriskIVR_asterisk-23271 Internal Gosub(func-apply-sipheaders,s,1(1)) start
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:1] NoOp("IAX2/asteriskIVR_asterisk-23271", "Applying SIP Headers to channel IAX2/asteriskIVR_asterisk-23271") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:2] Set("IAX2/asteriskIVR_asterisk-23271", "TECH=IAX2") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:3] Set("IAX2/asteriskIVR_asterisk-23271", "SIPHEADERKEYS=Alert-Info,Diversion") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:4] While("IAX2/asteriskIVR_asterisk-23271", "1") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:5] Set("IAX2/asteriskIVR_asterisk-23271", "sipheader=unset") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:6] ExecIf("IAX2/asteriskIVR_asterisk-23271", "0?Set(PJSIP_HEADER(remove,Alert-Info)=)") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:7] ExecIf("IAX2/asteriskIVR_asterisk-23271", "0?Set(sipheader=<http://127.0.0.1>;info=unset)") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:8] ExecIf("IAX2/asteriskIVR_asterisk-23271", "0?Set(sipheader=<http://127.0.0.1>unset)") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:9] ExecIf("IAX2/asteriskIVR_asterisk-23271", "0?Set(PJSIP_HEADER(add,Alert-Info)=unset)") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:10] EndWhile("IAX2/asteriskIVR_asterisk-23271", "") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:4] While("IAX2/asteriskIVR_asterisk-23271", "1") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:5] Set("IAX2/asteriskIVR_asterisk-23271", "sipheader=<tel:1200>;reason=no-answer;screen=no;privacy=off") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:6] ExecIf("IAX2/asteriskIVR_asterisk-23271", "0?Set(PJSIP_HEADER(remove,Diversion)=)") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:7] ExecIf("IAX2/asteriskIVR_asterisk-23271", "0?Set(sipheader=<http://127.0.0.1>;info=<tel:1200>;reason=no-answer;screen=no;privacy=off)") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:8] ExecIf("IAX2/asteriskIVR_asterisk-23271", "0?Set(sipheader=<http://127.0.0.1><tel:1200>;reason=no-answer;screen=no;privacy=off)") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:9] ExecIf("IAX2/asteriskIVR_asterisk-23271", "0?Set(PJSIP_HEADER(add,Diversion)=<tel:1200>;reason=no-answer;screen=no;privacy=off)") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:10] EndWhile("IAX2/asteriskIVR_asterisk-23271", "") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:4] While("IAX2/asteriskIVR_asterisk-23271", "0") in new stack
VERBOSE[7859][C-000088d7] pbx.c: Executing [s@func-apply-sipheaders:11] Return("IAX2/asteriskIVR_asterisk-23271", "") in new stack
VERBOSE[7859][C-000088d7] app_stack.c: Spawn extension (, 1400, 1) exited non-zero on 'IAX2/asteriskIVR_asterisk-23271'
VERBOSE[7859][C-000088d7] app_stack.c: IAX2/asteriskIVR_asterisk-23271 Internal Gosub(func-apply-sipheaders,s,1(1)) complete GOSUB_RETVAL=

If it can be done, it will have to be done through the built-in code for Diversion headers, not through the explicit SIP header hack. Look into the REDIRECTING function.

I should’ve expressed myself better in op. The “headache of configration when dealing with forwarded calls” is done through freepbx IVR where there is queue with agent that have number 1400 configured as static agent.

So if i understand the high level of IVR correctly; IVR answers the call, goes through routes / conditions / destinations that will ultimately end in queue, dial agent in new channel and bridge incoming and outgoing channels.

In Configuration → Advanced configuration i have toggled “Generate Diversion Headers” from No to Yes but apparently it is not enough.

The ultimate goal is to call agent, display original caller as source number and send correct diversion header in sip invite from asterisk to PSTN. As the freepbx IVR is set through web gui, i’m lost where should i specify redirecting function instead of dial.

It’s about 8 years since I played with this, once, on plain Asterisk, so whilst I know that REDIRECTING is the way that Asterisk is designed to handle the generation of Diversion, I don’t know the details, and I don’t know how much FreePBX gets in the way.

REDIRECTING is a “function” (not in the mathematical sense), which sets, or modifies, the information that Asterisk, itself, will put in Diversion headers. Dial is an application that initiates and supervises a call between two parties. They are very different things.

The overview documentation on manipulating this sort of thing is Manipulating Party ID Information - Asterisk Documentation

I switched from iax2 to sip trunks between asterisk IVR and asterisk and diversion headers are now handled correctly. Haven’t found anything that could resemble diversion header indication in iax2 protocol (albeit the research was shallow).

Now to find out how to copy diversion header from between incoming and outgoing call. But that’s other topic, this topic can be closed (i don’t know how to mark as solver or closed :see_no_evil:).