coturn服务一些基本知识stun,turn,ice,candidate,Offer,Answer,SDP


STUN


STUN的全称是Session Traversal of UDP Through NAT,即UDP对NAT的简单穿越方式, STUN应用无法穿透对称NAT(Symmetric NAT)以及类似的Firewall设备。
A STUN (Session Traversal of User Datagram Protocol [UDP] Through Network Address Translators [NATs]) server allows NAT clients (i.e. IP Phones behind a firewall) to set up phone calls to a VoIP provider hosted outside of the local network.

https://info.support.huawei.com/info-finder/encyclopedia/zh/STUN.html
STUN报文交互过程


这个无法穿透的对称NAT有点像,老总给1号线给张三打电话,用2号线给李四打电话。老总就存在多个线路,这时候需要一个助手(turn服务器)帮我固定一条线路,实现老总说一句,拨进来的人都能听到,别人(张三李四王五)发的消息,都发给助理,助理发给我。这里有个严重的问题,就是我的这个助理(turn服务)压力会非常大,因为这个有多条线×多个老总的线路,毕竟助理可能会同时服务多个老总。


It's a simple client/server protocol.
It has 2 usages:
1. Finding the binding IP address and port in a NAT. 找到IP和端口地址。
2. Discovering the NAT behavior. 发现NAT行为,不好理解,如下:

In order for a server to discover NAT types (Mapping & Filtering Behavior), the server must have two public IP addresses and two source ports (usually 3478 and 3479). One set of the information is called Primary IP/Port (e.g. 1.1.1.1:3478), and the other is called Alternate IP/Port (e.g. 2.2.2.2:3479).
为了让server发现NAT类型:(映射和过滤行为),服务必须有2个公开的IP地址和两个源端口(通常是3478和3479)分别是主和备(1.1.1.1:3478 和 2.2.2.2:3479)


这里NAT types (Mapping & Filtering Behavior),NAT类型的概念不清楚。
NAT filtering: 允许一个NAT设备去过滤来自公网到私有网络的信息。
NAT mapping:  一组私有网络的主机mapping映射到一个同样的公网IP地址。
NAT filtering
is a service that allows routers to translate a set of IP addresses into a different set of IP addresses。
NAT过滤模式: 是一个服务,让路由器去把 一组IP 转变成另外一组不同的IP。
STUN 服务器

什么是Symmetric NAT

  • Full Cone NAT(完全锥型NAT)
  • 所有从同一个私网IP地址和端口(IP1:Port1)发送过来的请求都会被映射成同一个公网IP地址和端口(IP:Port)。并且,任何外部主机通过向映射的公网IP地址和端口发送报文,都可以实现和内部主机进行通信。
  • 这是一种比较宽松的策略,只要建立了私网IP地址和端口与公网IP地址和端口的映射关系,所有的Internet上的主机都可以访问该NAT之后的主机。
  • Restricted Cone NAT(限制锥型NAT)
  • 所有从同一个私网IP地址和端口(IP1:Port1)发送过来的请求都会被映射成同一个公网IP和端口号(IP:Port)。与完全锥型NAT不同的是,当且仅当内部主机之前已经向公网主机发送过报文,此时公网主机才能向私网主机发送报文
  • Port Restricted Cone NAT(端口限制锥型NAT)
  • 与限制锥型NAT很相似,只不过它包括端口号。也就是说,一台公网主机(IP2:Port2)想给私网主机发送报文,必须是这台私网主机先前已经给这个IP地址和端口发送过报文。
  • Symmetric NAT(对称NAT)
  • 所有从同一个私网IP地址和端口发送到一个特定的目的IP地址和端口的请求,都会被映射到同一个IP地址和端口。如果同一台主机使用相同的源地址和端口号发送报文,但是发往不同的目的地,NAT将会使用不同的映射。此外,只有收到数据的公网主机才可以反过来向私网主机发送报文。
  • 这和端口限制锥型NAT不同,端口限制锥型NAT是所有请求映射到相同的公网IP地址和端口,而对称NAT是不同的请求有不同的映射。
NAT的四种类型



https://support.huawei.com/hedex/hdx.do?docid=EDOC1100087046&id=ZH-CN_CONCEPT_0176373815

https://slidetodoc.com/nat-traversal-for-vo-ip-aichun-pang-graduate/

TURN


TURN的全称为Traversal Using RelayNAT,即通过Relay方式穿越NAT


TURN如何工作


ICE跟STUN和TURN不一样,ICE不是一种协议,而是一个framework,它整合了STUN和TURN。

ICE是交互式连通性建立(Interactive Connectivity Establishment)的缩写

有很多开源的TURN服务器(比如Coturnrestund)。这些服务器维护良好,非常适合你的服务。或者你可以使用多个TURN平台即服务(PaaS)提供商中的一个,而不是缩放你自己的服务器(特别是在媒体流量较低时)。



第一步
,先使用nc命令检测要使用的端口是否已经开放,比如检测3478端口

以服务器IP:129.204.197.213 为例
简单的检测原理:服务器使用nc -l 3478监听3478端口,然后找一台linux机器使用nc 命令去连接,具体操作
比如:测试3478的TCP是否开放
服务器:
sudo nc -l 3478
客户端:
nc -v 129.204.197.213 3478
连接成功时提示
Connection to 129.204.197.213 3478 port [tcp/*] succeeded!

如果没有开放那就要去设置云服务器的安全组了。

3478:udp/tcp
443:tcp
8088:tcp
8089:tcp
8090:tcp
40000-60000:udp


测试时候一些词语解释:
gather candidate

host candidate:本机物理网卡或逻辑网卡地址和端口
srflx candidate:全称Sever Reflexive Candidate,端发送Binding请求到STUN/TURN Server经过NAT时,NAT上分配的地址和端口.
prflx candidate:全称Peer Reflexive Candidate,端发送Binding请求到对等端经过NAT时,NAT上分配的地址和端口.
relay candidate:全称Relayed Candidate,端发送Allocate请求到TURN Server,由TURN server用于中继的地址和端口.

TimeTypeFoundationProtocolAddressPortPriorityURL (if present)relayProtocol (if present)
0.011 | host | 1479725378 | udp | f14c2362-c773-4795-8a44-bf834f6458e6.local | 64535 | 126 | 30 | 255 |  |
0.031 | srflx | 4015559533 | udp | 11.11.11.11 | 3976 | 100 | 30 | 255 | stun:mi.com:3478 |
0.106 | relay | 980838732 | udp | 22.22.22.22 | 63986 | 2 | 31 | 255 | turn:mi.com:3478?transport=udp | udp
0.119 | Done


ICE candidate
ICE候选

ICE candidates. As well as exchanging information about the media (discussed above in Offer/Answer and SDP), peers must exchange information about the network connection. This is known as an ICE candidate and details the available methods the peer is able to communicate (directly or through a TURN server)
peers必须交换网络链接的信息  就是 ICE候选。是不是类似: 一群男女相亲对象互换联系方式。这种情况下全部女生都是某个男生的候选,全部男生也是某个女生的候选。那就要去和某些有意向的候选人互换信息。

Offer/Answer and SDP
提供/回答和SDP
The Session Description Protocol's (SDP) Offer/Answer Model is used by two entities to reach agreement on session description, such as which media streams are in the session, the codecs, etc. The offerer indicates the desired session description in the offer.
SDP: Session Description Protocal 会话描述协议
SDP's Offer/Answer 模型,会话描述协议里面的提供/回答模型,是描述 2个人如何展开会话。比如使用哪些media streams流媒体和编码器等。
有点类似古代欧洲两个人决斗,其中一个人选择格斗武器,另外一个人同意或者不同意。 offer/answer。这个offer区别于ask询问,区别于tcp的三次握手,三次握手太麻烦。因为这个offer/answer, 直接由A方选择好后,提供给B方。一般情况下就同意了。

ICE :Interactive Connectivity Establishment
交互式连接建立
The RTCIceCandidate interface—part of the WebRTC API—represents a candidate Interactive Connectivity Establishment (ICE) configuration which may be used to establish an RTCPeerConnection . An ICE candidate describes the protocols and routing needed for WebRTC to be able to communicate with a remote device.
代表一个候选 
The ICE protocol is used to generate media traversal candidates which can be used in WebRTC applications, and which can be successfully sent and received ...
交换连接建立协议用来生成media遍历候选,

阅读量: 332
发布于:
修改于: