在網路上,當Client A 要和 Client B 連線,雙方都不知道自己的 public IP和 port。這時候就可以透過 STUN server 告訴A和B對方的public IP 跟 port 是多少。
交互式連接建立(ICE)
交互式連接建立是一種標準穿透協議,利用Stun和Turn服務器來幫助端點建立連接。市面上已有不少介紹ICE的資料,像《WebRTC權威指南(第三版)》中的“9.2 交互式連接建立”。但看了那些後,有人還是不能理解,這裡試著用一個實例來描述整個過程。 ICE協議只是製定規範,沒規定怎麼實現細節,在細節實現上這裡參考Google的WebRTC。
ICE呼叫流程 |
上圖就是《WebRTC權威指南(第三版)》中的圖9.1。呼叫要交換兩種信息,一是候選地址,二是媒體信息。候選地址用於建立網絡連接,它存儲著和網絡連接相關的參數。媒體信息(SDP)用於描述要在對等連接上傳輸的數據,包括音頻、視頻和數據。用路和車來比喻的話,候選地址用於造路,媒體信息於用指定要跑什麼車。
在圖中,雙方是串行處理媒體、候選地址,但實際中是並發的。舉個例子,主叫收到Answer後,它仍可能在收集候選地址,然後通過信令服務器發向被叫。
除了主叫必須創建Offer才開始收集候選地址、被叫必須創建Answer才開始收集候選地址外,ICE代理是相互獨立地處理媒體和候選地址。 (這結論細節參考底下的“四:選定候選地址,並啟動媒體”)。
和“9.2 交互式連接建立”一樣, 這裡也把ICE分為六個步驟。下圖是例子使用的網絡拓撲結構。
STUN server and client library from the iptel.org guys. Old but mature. License: GPL, You have to download the file via CVS.
STUN server and client library/application for Linux and Windows from the Vovida guys. Old but mature. License: Vovida Software License 1.0.
A Windows STUN client, part of the Vovida STUN server (see above). A nice application to test your NAT box.
STUN/TURN server and client library, part of the resiprocate project. Server application is provided as well, but it seems incomplete (authentication). License: 3-clause BSD license.
STUN/TURN server, supports authentication against a mysql DB. License: 3-clause BSD license.
STUN/TURN server. License: GPL3.
Open Source ICE, STUN, and TURN Library
a free STUN/TURN serve
NAT是什麼?
假設區域網路(LAN)內有5台電腦對外連線,各自有一個對內的 private IP (192.168.0.1~5,5是Client A),對外分別是 222.222.222.222 (port 99, 98, 97, 96, 95),假設有人對 222.222.222.222:95 發送 request, NAT 就會對應到192.168.0.5 轉送給Client A。
過程如下:
Before Pending Request Process for Connection
Client A propose a request to STUN server
STUN get client A ‘s public IP / port and respond to Client A
Client A told webRTC signal server that he wanted to connect Client B
Signal server informed Client B the connection request from Client A (public IP / port included)
Client B repeat step 1–2 get its own IP address information
Signal server respond information to Client A
Now they both use its own IP address information to connect each other, no signal server business included.
General overview of reflex candidates and STUN servers |
在某些情況下還是有機會無法連線,因為在某些特定的 NAT 下,STUN server的運作會沒有意義,例如:
對稱NAT(Symmetric NAT)取自 維基百科
- 每一個來自相同內部 IP 與端口,到一個特定目的地 IP 和端口的請求,都映射到一個獨特的外部 IP 和端口。同一內部 IP 與端口發到不同的目的地和端口的信息包,都使用不同的映射
- 只有曾经收到过内部主机数据的外部主机,才能够把封包发回
Distinctive IP address/ port for each connection between clients.
我們在重新審視一下剛剛透過 STUN server 交換IP information的過程,很容易可以發現,STUN server 回應給我的 IP 資訊,在Symmetric NAT的限制下只限於我與server 間的連線,若有其他Client透過這組 IP 資訊與我連線,會在 NAT 被擋下,無法順利建立連線。
That’s why we need TURN server
以下轉自 WebRTC in the real world: STUN, TURN and signaling
Session Traversal Utilities for NAT
(STUN) is a standardized protocol for such address discovery including NAT classification. Traversal Using Relays around NAT (TURN) places a third-party server to relay messages between two clients when direct media traffic between peers is not allowed by a firewall.
TURN server
TURN servers have public addresses, so they can be contacted by peers even if the peers are behind firewalls or proxies. TURN servers have a conceptually simple task — to relay a stream — but, unlike STUN servers, they inherently consume a lot of bandwidth. In other words, TURN servers need to be beefier
ICE (Interactive Connectivity Establishment)
Reference
沒有留言:
張貼留言