Freeswitch自定义SIP头

<``action application``=``"set" data``=``"sip_h_X-Answer=42"``/>
<``action application``=``"bridge" data``=``"sofia/mydomain.com/1000@example.com"``/>

Note that for BYE requests, you will need to use the prefix ‘sipbye_h‘ on the channel variable.



While not required, you should prefix your headers with “X-“ to avoid issues with interoperability with other SIP stacks.
All inbound SIP calls will install any X- headers into local variables.
This means you can easily bridge any X- header from one FreeSWITCH instance to another.
To access the header above on a 2nd box, use the channel variable ${sip_h_X-Answer}
It is important to note that the syntax ${sip_h_custom-header} can’t be used to retrieve any custom header not starting with X-.
It is because Sofia only reads and puts into variables custom headers starting with X-.

Adding Response Headers

There are three types of response header prefixes that can be set:

  • Response headersiprh
  • Provisional response headersipph
  • Bye response headersipbye_h

Each prefix will exclusively add headers for their given types of requests - there is no “global” response header prefix that will add a header to all response messages.
For example:

<``action application``=``"set" data``=``"sip_rh_X-Reason=Destination Number Not in Footprint"``/>
<``action application``=``"set" data``=``"sip_bye_h_X-Accounting=Some Accounting Data"``/>

Adding Custom Headers

For instance, you may need P-Charge-Info to append to your INVITE header, you may do as follows:

<``action application``=``"set"``>``<![CDATA[sip_h_P-Charge-Info=<sip:${caller_id_number}@${domain_name}>;npi=0;noa=3]]>``</``action``>

Then, you would see it in SIP message:

折叠源码

INVITE sip:19099099099@1.2.3.4 SIP/2.0
Via: SIP/2.0/UDP 5.6.7.8:5080;rport;branch=z9hG4bKyg61X9v3gUD4g
Max-Forwards: 69
From: "DJB" <``sip:2132132132``@5.6.7.8>;tag=XQKQ322vQF5gK
To: <``sip:19099099099``@1.2.3.4>
Call-ID: b6c776f6-47ed-1230-0085-000f1f659e58
CSeq: 30776798 INVITE
Contact: <``sip:mod_sofia``@5.6.7.8:5080>
User-Agent: FreeSWITCH-mod_sofia/1.2.0-rc2+git~20120713T162602Z~0afd7318bd+unclean~20120713T184029Z
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, UPDATE, INFO, REGISTER, REFER, NOTIFY
Supported: timer, precondition, path, replaces
Allow-Events: talk, hold, conference, refer
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 229
P-Charge-Info: <``sip:2132132132``@5.6.7.8>;npi=0;noa=3
X-FS-Support: update_display,send_info.
Remote-Party-ID: "DJB" <``sip:2132132132``@5.6.7.8>;party=calling;screen=yes;privacy=off

Strip Individual SIP Headers

Sometimes a SIP provider will add extra header information. Most of the time they do that for their own use (tracking calls). But that extra information can cause a lot of problems. For example: I get a call from the PSTN via a DID provider (provider1). Since im not in the office the call gets bridged to my cell phone (provider2). Provider1 add’s extra information to the sip packet like displayed below:

X-voipnow-did: 01234567890
X-voipnow-extension: 987654321
...

In some scenario, we bridge this call directly to provider2 the calls get dropped since provider2 doesnt accept the X-voipnow header, so we have to strip off those SIP headers.
To strip them off, use the application UNSET in the dialplan (the inverse of SET):

<``action application``=``"unset" data``=``"sip_h_X-voipnow-did"``/>
<``action application``=``"unset" data``=``"sip_h_X-voipnow-extension"``/>
...

Strip All custom SIP Headers

If you wish to strip all custom headers while keeping only those defined in dialplan:

<``action application``=``"set" data``=``"sip_copy_custom_headers=false"``/>
<``action application``=``"set" data``=``"sip_h_X-myCustomHeader=${sip_h_X-myCustomHeader}"``/>
...

Additional Channel variables

Additional variables may also be set to influence the way calls are handled by sofia.
For example, contacts can be filtered by setting the ‘sip_exclude_contact’ variable. Example:

Or you can perform SIP Digest authorization on outgoing calls by setting sip_auth_username and sip_auth_password variables to avoid using Gateways to authenticate. Example:

Changing the SIP Contact user FreeSWITCH normally uses mod_sofia@ip:port for the internal SIP contact. To change this to foo@ip:port, there is a variable, sip_contact_user:
{sip_contact_user=foo}sofia/my_profile/1234@192.168.0.1;transport=tcp

sip_renegotiate_codec_on_reinvite

true|false

sip_recovery_break_rfc

true|false

添加自定义SIP头

自定义头域必须使用X-前缀

diaplan中

  1. <action application="set" data="sip_h_X-My-Heder=123456"/>
  2. • 1

lua脚本中

  1. local my_header = "123456"
  2. ...
  3. session:execute("set", "sip_h_X-My-Header=" .. my_header)
  4. session:execute("export","sip_h_X-My-Header=" .. my_header)
  5. 1
  6. 2
  7. 3
  8. 4

实际效果

在sip消息后续的传递过程中,抓取sip信令可以看到在sip消息的头域里面添加了如下的一行:

X-My-Header: 123456
• 1

添加有对应信道变量的sip头

  1. 修改方式和添加自定义sip头基本一致;
  2. 可以通过将siph添加到任何通道变量前面来向出站sip添加任意标头;
  3. 自定义sip头需要以X-为前缀,添加有对应信道变量的sip头不需要。

    此段描述引用自Freeswitch官网,没有经过测试 Adding Request Header部分

获取自定义sip头

获取sip头和获取信道变量相似

diaplan中

<action application="log" data="My Header: ${sip_h_X-My-Header}"/>
• 1

lua脚本中

local my_header = session:getVariable("sip_h_X-My-Header")
freeswitch.consoleLog("info", "My Header: " .. my_header)
• 1
• 2

获取非自定义sip头

暂时没有找到可以直接获取非自定义的sip头的方法,只能通过相关的信道变量来获取sip头的信息。