From FreeSWITCH Wiki
说明
FreeSWITCH的自动穿透机制使用NAT-PMP或者UPnP发现外部IP地址(externalIPaddress),不再需要搜寻STUN服务。实现NAT功能的设备(如路由器等)必须支持UPnP或NAT-PMP,以便FreeSWITCH的自动穿透机制正常使用。在自动穿透机制正常运转后,要做的只是简单的配置一下sipprofile文件。
注意:
1、仍推荐你为你的sip服务器提供商准备第二份配置文件。默认的conf/sip_profiles/external.xml文件是特别为(sip服务)提供商而准备的。
2、nat自动穿透机制只应用NAT-PMP与UPnP。如果应用失败,则不会再回滚应用nat机制。
如需使用STUN,需要设置外部rtp地址(externalrtp)和外部sip地址(externalsip)。
3、从SVN版本15916开始,在/usr/local/freeswitch/conf/sip_profile/internal.xml和 external.xml中使用新的设置项:autonat:IP,如下所示:
<span style="font-size:14px;"> <param name="ext-rtp-ip" value="autonat:xxx.yyy.zzz.aaa"/> <param name="ext-sip-ip" value="autonat:xxx.yyy.zzz.aaa"/></span>
这对于NAT-PMP和UPnP机制总是出问题的路由器来说非常有用。
安装步骤
从一个默认配置文件开始:
-
如果你的配置文件版本比较老(在r13618之前),在conf/sip_profiles/internal.xml找到下面两行:
<!-- <param name="ext-rtp-ip" value="$${external_rtp_ip}"/> -->
<!-- <param name="ext-sip-ip" value="$${external_sip_ip}"/> -->
-
取消下面两行的注释,并修改如下:
<span style="font-size:14px;"> <param name="ext-rtp-ip" value="auto-nat"/></span> <span style="font-size:14px;"> <param name="ext-sip-ip" value="auto-nat"/></span>
-
重启FreeSWITCH。输入命令“sofiastatus”,得到的输出如下所示:
API CALL [sofia(status)] output:
Name Type Data State
=================================================================================================
internal profile sip:mod_sofia@192.168.1.103:5060 RUNNING (1)
192.168.1.103 alias internal ALIASED
default alias internal ALIASED
internal-ipv6 profile sip:mod_sofia@[::1]:5060 RUNNING (0)
=================================================================================================
2 profiles 2 aliases
-
输入命令“sofiastatus profile internal”,输出如下所示:
API CALL [sofia(status profile internal)] output:
=================================================================================================
Name internal
Domain Name N/A
DBName sofia_reg_internal
Pres Hosts
Dialplan XML
Context public
Challenge Realm auto_from
RTP-IP 192.168.1.103
Ext-RTP-IP 67.182.63.90
SIP-IP 192.168.1.103
Ext-SIP-IP 67.182.63.90
URL sip:mod_sofia@192.168.1.103:5060
BIND-URL sip:mod_sofia@192.168.1.103:5060
HOLD-MUSIC local_stream://moh
OUTBOUND-PROXY N/A
CODECS G7221@32000h,G7221@16000h,G722,PCMU,PCMA,GSM
TEL-EVENT 101
DTMF-MODE rfc2833
CNG 13
SESSION-TO 0
MAX-DIALOG 0
NOMEDIA false
LATE-NEG false
PROXY-MEDIA false
AGGRESSIVENAT false
STUN-ENABLED true
STUN-AUTO-DISABLE false
CALLS-IN 0
FAILED-CALLS-IN 0
CALLS-OUT 1
FAILED-CALLS-OUT
Registrations:
=================================================================================================
=================================================================================================
-
注意命令输出中含有“Ext-RTP-IP”和“Ext-SIP-IP”的两行,如下所示:
<span style="font-size:14px;">Ext-RTP-IP 67.182.63.90</span> <span style="font-size:14px;">Ext-SIP-IP 67.182.63.90</span>
-
如果上面的输出中含有公共IP地址(即路由器对外公布的地址),则代表自动穿透正在运行。通过检查nat设置可以看出通过UPnP/NAT-PMP获取的公共IP地址就是
路由器等网络设备与外部交互的地址。
输入命令“nat_mapstatus",来判断nat_map是否在工作。输入如下所示:
<span style="font-size:12px;">Nat Type: UPNP, ExtIP: 76.237.11.11 port,proto,proto_num,sticky 5060,udp,0,0 5060,tcp,1,0 5080,udp,0,0 5080,tcp,1,0 4 total.</span>
通过查看启动过程中的输出的一条调试信息来确定外部IP地址有没有被检测到:
<span style="font-size:12px;">2009-09-11 09:42:55.962276 [INFO] switch_nat.c:406 NAT detected type: upnp, ExtIP: '83.59.46.46' </span>
NOTE :不管上面怎么配置都会启动NAT检测。如需关闭,使用"-nonat"选项启动freeswitch即可。
拨打电话测试
在这个测试中我使用的是portaudio,当然你也可以注册sip电话来测试。
我们将会使用FreeSWITCH公共会议回声测试,来验证两个方向的语音。如果拨打的电话能听到自己的回声,证明一切正常。下面是一个样例:
<span style="font-size:14px;">freeswitch> pa call sip:9196@conference.freeswitch.or</span> <span style="font-size:14px;">2009-06-04 13:24:57 [NOTICE] switch_channel.c:602 switch_channel_set_name() New Channel portaudio/sip:9196@conference.freeswitch.org [77d54b4c-d395-af49-8950-2fe99635a0be]</span> <span style="font-size:14px;">Pa_StartStream: waveInStart returned = 0x0.</span> <span style="font-size:14px;">2009-06-04 13:24:58 [NOTICE] mod_PortAudio.c:1773 place_call() Channel [portaudio/sip:9196@conference.freeswitch.org] has been answered</span> <span style="font-size:14px;">API CALL [pa(call sip:9196@conference.freeswitch.org)] output:</span> <span style="font-size:14px;">SUCCESS:4:77d54b4c-d395-af49-8950-2fe99635a0be</span> <span style="font-size:14px;">2009-06-04 13:24:58 [INFO] mod_dialplan_xml.c:252 dialplan_hunt() Processing FreeSWITCH->sip:9196@conference.freeswitch.org in context default</span> <span style="font-size:14px;">2009-06-04 13:24:58 [NOTICE] switch_channel.c:602 switch_channel_set_name() New Channel sofia/internal/9196@conference.freeswitch.org [b34bd3c1-11ba-c54c-a518-4ca11f756726]</span> <span style="font-size:14px;">2009-06-04 13:24:58 [NOTICE] sofia.c:3605 sofia_handle_sip_i_state() Channel [sofia/internal/9196@conference.freeswitch.org] has been answered</span>
在此时你应该可以听见自己的回声。如果没有听到则需要检查网络配置。
下一步挂断电话:
<span style="font-size:14px;">freeswitch> pa hangup</span> <span style="font-size:14px;">2009-06-04 13:25:07 [NOTICE] mod_PortAudio.c:1549 hangup_call() Hangup portaudio/sip:9196@conference.freeswitch.org [CS_EXECUTE] [NORMAL_CLEARING]</span> <span style="font-size:14px;">API CALL [pa(hangup)] output:</span> <span style="font-size:14px;">OK</span> <span style="font-size:14px;">2009-06-04 13:25:07 [NOTICE] switch_ivr_bridge.c:505 audio_bridge_on_exchange_media() Hangup sofia/internal/9196@conference.freeswitch.org [CS_EXCHANGE_MEDIA] [</span> <span style="font-size:14px;">NORMAL_CLEARING]</span> <span style="font-size:14px;">2009-06-04 13:25:07 [NOTICE] switch_core_session.c:1085 switch_core_session_thread() Session 7 (sofia/internal/9196@conference.freeswitch.org) Ended</span> <span style="font-size:14px;">2009-06-04 13:25:07 [NOTICE] switch_core_session.c:1087 switch_core_session_thread() Close Channel sofia/internal/9196@conference.freeswitch.org [CS_DESTROY]</span> <span style="font-size:14px;">WinMME StopStream: waiting for background thread.</span> <span style="font-size:14px;">2009-06-04 13:25:08 [NOTICE] switch_core_session.c:1085 switch_core_session_thread() Session 6 (portaudio/sip:9196@conference.freeswitch.org) Ended</span> <span style="font-size:14px;">2009-06-04 13:25:08 [NOTICE] switch_core_session.c:1087 switch_core_session_thread() Close Channel portaudio/sip:9196@conference.freeswitch.org [CS_DESTROY]</span>
恭喜!如果你看到上面的输出信息,证明你已经摆脱了其他人正在忍受的NAT噩梦。
Making A Test Call and bridging to a SIP phone
下面的测试将会呼叫会议,并拨打位于默认域中用户105的sip电话,命令如下:
<span style="font-size:14px;">originate sofia/internal/9996@conference.freeswitch.org &bridge(user/105@default)</span>
双NAT测试
1)双NAT场景主要表现在两个电话endpoints都位于它们各自的NAT设备之后。两个FreeSWITCH服务器各自都位于UPnP/NAT-PMP之后,如下所示:
<span style="font-size:14px;">FS1 <> NAT <> Internet <> NAT <> FS2</span>
在这种情况下FreeSWITCH的自动穿透机制就派上了用场。像上面一样配置FreeSWITCH,然后分别将FreeSWITCH服务器注册到另一个服务器上。
2)使用eyebeam/snom的双nat网络,如下:
<span style="font-size:14px;">Eyebeam/snom1 <> NAT <> Internet <> NAT <> FS</span>
和
<span style="font-size:14px;">Eyebeam/snom2 <> Internet <> NAT <> FS</span>
两个客户端使用两个方式进行语音通话。snom1可以拨打snom2,反之亦然。发现这种一种现象:软电话收到“BYE”消息后挂断,FS给的原因是“ACKtimeout”(确认超时),尽管此时FS已经接收到“ACK”消息(此时仍有语音流,所以不应该是RTP超时)。不确定是否因为安装步骤错误导致的这个问题。一旦确定是该问题将立即升级。Eyebeam同时也开启了stun服务。FS的stun不可用,autonat可用。路由器是netgearFVS318无线路由器。Snom320也存在同样问题。
Troubleshooting
<stub>
Upnp 部分:
-
带有autonat功能的FS在启动时会开启SIP端口,同时也会动态的开启RTP端口。
-
路由器必须支持upnp。有一些路由器,如FVS318无线路由器,会被类似于FS的upnp设备在路由器上创建一张upnp端口映射表。通过该方式可以检验端口是否被正常的开启。
-
对于FS端,可以在FS的命令行中输入"nat_mapadd 1234 udp",检查路由器上的端口是否被开启。如果路由器有显示upnp端口映射表的功能最好,否则可以使用命令“nat_mapadd 1234 tcp",然后从外网telnet1234端口,检查端口是否开启。
(在测试完上面的命令后,可以使用命令"delete"代替“add”,手工的删除端口。如nat_mapdelete 1234 tcp ).
-
对于FS日志,可以检查debug模式下的日志。当通话正在进行中时,查看是否有switch_nat_add_mapping_upnp()用于增加端口,switch_nat_del_mapping_upnp()用于删除端口。
-
使用源代码中的测试程序miniupnpc。进入libs/miniupnpc目录,输入make进行编译,将会产生一个名为"upnpc-static"的文件。使用"-l"选项列出现存的端口映射。同样,你也可以在网关设备上新建或删除映射的端口条目。
-
从download.com网站上下载工具"UPnPportworks"。使用这个工具可以查看端口列表,新增、删除端口。使用该工具检查路由器上的UPnP是否可用。(有时无法正常使用,但是只要先运行BaUPnP,再来使用的话则一切正常)。该工具甚至可以用于转发sip端口,不过需要注意的是最终的目的是要FS可以正常的增删端口,不是端口转发。并且不能用来转发RTP端口,转发后也听不到声音。
-
使用tcpdump和wireshark抓取UPnP或NAT-PMP的数据包。UPnP的过滤字符串是“host239.255.255.250”。NAT-PMP的过滤字符串是“host224.0.0.1”。
总之:
-
在开始点对点通话前,先进行回声测试。测试失败了,就先解决回声的问题。
Issues with FS UPnP
-
I have found that "UPnP port works"s/w was able to add / remove ports to the FS ip, but the nat_mapcommand in the console did not. Please post similar experiences hereand report to have them fixed.
支持FSUPNP设备列表
-
Linksys WRTxxxwith dd-wrt or tomato firmware. dd-wrt version v24sp1 worksfunctionally, but the UPNP UI page is always empty.
-
LinksysWRT54GL with dd-wrt v24-sp2 10/10/09 std -or- v24 preSP2[Beta] Build13064 (recommended) drops 5060 and 5080 tcp/upd mappings. Theconnections will be up for a minute or so, and then gone.(10/7/2010)
-
above is alsotrue for DD-WRT v24-sp2 (08/07/10) std. Tried adjusting timeouts inAdministration->Management->IP Filter Settings (adjust thesefor P2P) with no joy.
-
-
Tomato Version1.28 on a wrt54gl works well (working for several weeks so far...).
-
LinksysWRT54g2 with linksys firmware works with FS, but unreliable. FScan't open the ports sometimes. Please add your own experience.
-
AT&TUverse Residential gateway (by 2Wire) supports NAT-PMP. I am unableto find a status page that shows current mappings.
-
Netgear FVS318wireless supports upnp and shows a table of portmappings. Workedwith FS autonat.
-
Dlink DIR 655 wireless supports upnp, its"status"->"Internet sessions" shows"Local-NAT-Internet" port mappings. Please disable its"SIP ALG" feature under "Advanced"->"FirewallSettings"->"Application Level Gateway (ALG)Configuration", otherwise the SIP gateway registrations are notstable.
Note firmware 1.2.1 has UPNP bug, itsUPNP function working for a while until after a couple of hoursstopping work properly. new firmware 1.3.2NA seems to have correctedthe UPNP bug per its claim, still keeping watching it on my DIR655.
DLINK DIR 655's UPNP is not stable even withfirmware 1.3.2NA, not recommended for production use.
-
Blanc WirelessG router BW54R11 Firmware 1.4.1 UPNP feature worked with FSauto-nat.
-
Netgear wgt624is a really bad router. Please stay away from it !
-
Mikrotikrouters: PC-based and RouterBoards with RouterOS v.3.25. Previousversions not tested (maybe working all 3.xx branch)
-
Digicom wave 64 with firmware version:2.11.62.2(RE0.C2D)3.10.16.0
支持自动穿透的NAT-PMP设备列表
-
Apple Airport Extreme with Firmware 7.4.2
Please update this wiki with list of routersas you test them
什么时候不使用穿透
FreeSwitch搭建在公网,并且启动模式为“nonat”。就我所知,fs仍针对那些位于nat设备后面的ip电话使用nat。(不是百分之百确定)。