Passing a SIP response code to Hangup()

I want to revisit a topic started by @Bucky101 in October of 2021. I am dealing with the same issue described here: Map SIP Hangup Codes and reason text 1:1

Basically, I have Asterisk working as a back-to-back user agent. When one endpoint sends a SIP response code, I want to pass along that exact same SIP response code to the other endpoint. Asterisk allows us to pass a causecode argument to the Hangup() function. Unfortunately, this parameter is the ISDN causecode and not the SIP response code which I’m needing.

I’ve found that it is possible to get the actual SIP response code sent by the endpoint where the call failed using HANGUPCAUSE_KEYS after the Dial application returns like this:

same => n,Dial()
same => n,GotoIf($[${LEN(${HANGUPCAUSEKEYS})}=0]?logError)
same => n,Set(ARRAY(item)=${HANGUPCAUSEKEYS})
same => n,Set(SIPCAUSE=${HANGUPCAUSE(${item},tech)})
same => n,Set(SIPCAUSECODE=${CUT(SIPCAUSE, ,2)})

Now, SIPCAUSECODE is the integer SIP response code received from the endpoint. It would be perfect if I could just pass this code directly to the Hangup function like:

same => n,Hangup(${SIPCAUSECODE})

The bad news is that Asterisk is expecting an ISDN causecode, and if it receives a value it doesn’t recognize, it ignores it.

The good news is that changing this behavior requires changing only one line of source in the chan_pjsip module. There is a hangup_cause2sip function in chan_pjsip.c that converts the ISDN causecode to the corresponding SIP response code. It’s just a big switch statement, and the default case (if it sees a code it doesn’t recognize) is to return 0. All we have to do is return the passed-in argument instead of 0.

I’ve already made this change on our system, and it does exactly what we need. However, it is admittedly something of a hack. Is it acceptable to the wider asterisk community to allow the PJSIP implementation of the Hangup function to accept an ISDN cause code OR a SIP response code (relying on the accident that domains of the two codes don’t overlap)? Or would we need to create a separate Hangup function with a different name and behavior?

Also, do we need to maintain the behavior of returning 0 when hangup_cause2sip is passed an unrecognized value, or is it acceptable to simply return the passed-in argument? The hangup_cause2sip function is used in only this one place in Asterisk, so this change seems unlikely to break other things.

It would need to be something else. The Hangup functionality is specifically documented everywhere and throughout the core as being ISDN cause code. This also generates events and sets the value elsewhere, which are also documented as ISDN cause code. I don’t know about the behavior of hangup_cause2sip, it would need to be audited.

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