It is possible by setting
<param name="parse-all-invite-headers" value="true"/>
in the Sofia SIP Profile. Then all headers from the Invite are set as sip_i_Header-Name channel variables.
Reacting to specific Headears in any other SIP-Messages:
If you want to react to specific headers on other messages, you can do this via setting the variable sip_watch_headers (needs to be exported and prefixed with nolocal if you want it for B-leg only)
If the header is detected, you will get a CUSTOM event of the Subclass "sofia::notify_watched_header".
example to detect Reason header on B-Leg:
<action application="export" data="_nolocal_sip_watch_headers=Reason"/>
Here is an example of the Event looking for a Reason header on the B-Leg, watching the Header "Reason":
"Event-Name": "CUSTOM",
...
"Event-Calling-File": "sofia.c",
"Event-Calling-Function": "notify_watched_header",
"Event-Calling-Line-Number": "1443",
"Event-Sequence": "98672",
"Event-Subclass": "sofia::notify_watched_header",
"SIP-Message": "SIP/2.0 183 Session Progress",
"Header-Name": "Reason",
"Header-Value": "Q.850;cause=16",
"Channel-State": "CS_CONSUME_MEDIA",
"Channel-Call-State": "DOWN",
"Channel-State-Number": "7",
...
"Call-Direction": "outbound",
This event can be reacted either with Lua by setting a hook script in the Lua configuration or via AMQP / EventSocket.
How to do react to these events with Lua
https://freeswitch.org/confluence/display/FREESWITCH/mod_lua#Event_Hooks
Example: autload_configs/lua.conf.xml:
<configuration name="lua.conf" description="LUA Configuration">
<settings>
<param name="module-directory" value="/etc/freeswitch/scripts/?.so"/>
<param name="script-directory" value="/etc/freeswitch/scripts/?.lua"/>
<!--<param name="startup-script" value="startup_script_1.lua"/>--> <!-- started at fs startup and maybe lives forever -->
<hook event="CHANNEL_DESTROY" script="/etc/freeswitch/scripts/on_channel_destroy.lua"/>
<hook event="CUSTOM" subclass="sofia::notify_watched_header" script="/etc/freeswitch/scripts/on_reason_header.lua"/>
</settings>
</configuration>
Example Lua script
local uuid = event:getHeader("Unique-ID")
local shallHangup = event:getHeader("variable_HangupOnReasonInEarly")
local answerState = event:getHeader("Answer-State")
if (shallHangup ~= nil and shallHangup == "true" and answerState == "ringing") then
local value = event:getHeader("Header-Value")
local code = value:match(";cause=(%d*)")
--local data = event:serialize("json")
freeswitch.consoleLog("INFO","REASON DETECTED: for: " .. uuid .. "\n")
api = freeswitch.API()
api:executeString("uuid_kill " .. uuid .. " " .. code)
end
To enable it in dialplan:
<action application="export" data="_nolocal_sip_watch_headers=Reason"/>
<action application="export" data="_nolocal_HangupOnReasonInEarly=true"/>
<action application="bridge" data="..."/>