Score:0

Forwarding SIP headers with asterisk (PJSIP)

ru flag

I'm trying to forward a specific incoming header to the other leg of the call, but can't figure out how to pass the value of the header in the incoming leg to the pre-dial handler

[addheaders]
exten => addheader,1,Verbose("Setting header")
exten => addheader,1,Verbose(${somevar}) ; The 'somevar' variable doesn't exist anymore
exten => addheader,n,Set(PJSIP_HEADER(add,X-MyHeader)=test)
exten => addheader,n,Set(PJSIP_HEADER(add,X-MyHeader2)=${somevar})
exten => addheader,n,Return()

[incoming]
exten => _+4600.,1,Ringing
exten => _+4600.,n,Set(somevar=${PJSIP_HEADER(read,TheHeaderIWantToForward)})
exten => _+4600.,n,Verbose(${somevar}) ; Prints the correct value
exten => _+4600.,n,Dial(PJSIP/${EXTEN:1},,b(addheaders^addheader^1))
exten => _+4600.,n,Hangup

I have successfully managed to add the X-MyHeader to the outbound call leg (as the asterisk documentation shows how to), but how do I actually pass the value to the other context? I can't read the variable in "[addheaders]", and I can only read the headers in "[incoming]"

Score:1
by flag

You can use _VARIABLE: https://wiki.asterisk.org/wiki/display/AST/Variable+Inheritance

[handler]
exten => addheader,1,NoOp(Value is ${somevar})
same => n,Set(PJSIP_HEADER(add,X-myheader=${somevar})
same => n,Return()

[internal]
exten => 6010,1,NoOp(Test)
same  => n,Set(_somevar=${PJSIP_HEADER(read,X-myheader)})
same  => n,NoOp(Value is ${somevar})
same => n,Dial(PJSIP/6010,,b(handler^addheader^1))
same => n,Hangup()
Score:0
ru flag

Don't know if this was the 'correct' way of doing this, but i solved it by accepting wildcard extensions in the addheaders context:

[addheaders]
exten => _.,1,Set(PJSIP_HEADER(add,X-header-to-be-forwarded)=${EXTEN})
exten => _.,n,Return()

[incoming]
exten => _+4600.,1,Ringing
exten => _+4600.,2,Set(customheader=${PJSIP_HEADER(read,X-header-to-be-forwarded)})
exten => _+4600.,3,GotoIf($["${customheader}"=""]?7:) ; Skip adding the header if it is empty
exten => _+4600.,4,Dial(PJSIP/${EXTEN:1},,b(addheaders^${customheader}^1))
exten => _+4600.,5,GoTo(9))
exten => _+4600.,6,Dial(PJSIP/${EXTEN:1})
exten => _+4600.,7,Hangup

This has some limitations, for example "." or "," cant be used, but it solved our problem.

Score:0
in flag

You can also pass arguments to the Dial applications b option:

b([[context^]exten^]priority[(arg1[^...][^argN])]): Before initiating an
outgoing call, Gosub to the specified location using the newly created
channel.  The Gosub will be executed for each destination channel.

Like this:

exten => _+4600.,n,Dial(PJSIP/${EXTEN:1},,b(addheaders^addheader^1(${somevar})))

Then use it:

exten => addheader,n,Set(PJSIP_HEADER(add,X-MyHeader)=${ARG1})
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.