NAT 類型及檢測方法

來源:互聯網
上載者:User

NAT 類型及檢測方法

STUN協議是一個客戶機/伺服器協議,在公網上存在著大量的STUN伺服器,使用者可以通過在自己主機上運行STUN用戶端遠端連線STUN伺服器來確認自身的網路狀況.

用戶端主機所在網路可以分為以下類型:

1, Opened: 即主機擁有公網IP,並且沒有防火牆,可自由與外部通訊.

2, Full Cone NAT: 主機前有NAT裝置,NAT規則如下:從主機UDP連接埠A發出的資料包都會對應到NAT裝置出口IP的連接埠B,並且從任意外部地址發送到該NAT裝置UDP連接埠B的包都會被轉到主機連接埠A.

3, Restricted cone NAT: 主機前有NAT裝置,NAT規則如下:從主機UDP連接埠A發出的資料包都會對應到NAT裝置出口IP的連接埠B,但只有從之前該主機發出包的目的IP發出到該NAT裝置UDP連接埠B的包才會被轉到主機連接埠A.

4, Port Restricted cone NAT: 主機前有NAT裝置,NAT規則如下:從主機UDP連接埠A發出的資料包都會對應到NAT裝置出口IP的連接埠B,但只有從之前該主機發出包的目的IP/PORT發出到該NAT裝置UDP連接埠B的包才會被轉到主機連接埠A.

5, Symmetric UDP Firewall: 主機出口處沒有NAT裝置,但有防火牆,且防火牆規則如下:從主機UDP連接埠A發出的資料包保持源地址,但只有從之前該主機發出包的目的IP/PORT發出到該主機連接埠A的包才能通過防火牆.

6, Symmetric NAT: 主機前有NAT裝置,NAT規則如下:即使資料包都從主機UDP端A發出,但只要目的地址不同,NAT裝置就會為之分配不同的出連接埠B.

7, Blocked: 防火牆限制UDP通訊.

 

 1  /// <summary>
 2     /// Specifies UDP network type.
 3     /// </summary>
 4     public enum STUN_NetType
 5     {
 6         /// <summary>
 7         /// UDP is always blocked.
 8         /// </summary>
 9         UdpBlocked,
10 
11         /// <summary>
12         /// No NAT, public IP, no firewall.
13         /// </summary>
14         OpenInternet,
15 
16         /// <summary>
17         /// No NAT, public IP, but symmetric UDP firewall.
18         /// </summary>
19         SymmetricUdpFirewall,
20 
21         /// <summary>
22         /// A full cone NAT is one where all requests from the same internal IP address and port are 
23         /// mapped to the same external IP address and port. Furthermore, any external host can send 
24         /// a packet to the internal host, by sending a packet to the mapped external address.
25         /// </summary>
26         FullCone,
27 
28         /// <summary>
29         /// A restricted cone NAT is one where all requests from the same internal IP address and 
30         /// port are mapped to the same external IP address and port. Unlike a full cone NAT, an external
31         /// host (with IP address X) can send a packet to the internal host only if the internal host 
32         /// had previously sent a packet to IP address X.
33         /// </summary>
34         RestrictedCone,
35 
36         /// <summary>
37         /// A port restricted cone NAT is like a restricted cone NAT, but the restriction 
38         /// includes port numbers. Specifically, an external host can send a packet, with source IP
39         /// address X and source port P, to the internal host only if the internal host had previously 
40         /// sent a packet to IP address X and port P.
41         /// </summary>
42         PortRestrictedCone,
43 
44         /// <summary>
45         /// A symmetric NAT is one where all requests from the same internal IP address and port, 
46         /// to a specific destination IP address and port, are mapped to the same external IP address and
47         /// port.  If the same host sends a packet with the same source address and port, but to 
48         /// a different destination, a different mapping is used. Furthermore, only the external host that
49         /// receives a packet can send a UDP packet back to the internal host.
50         /// </summary>
51         Symmetric
52     }

 

測試過程

STUN伺服器運行在UDP協議之上,它具有兩個固定公網地址,能完成以下幾個功能:

1. 告訴STUN用戶端經NAT裝置映射後的公網地址.

2. 根據STUN用戶端的要求,從伺服器的其他不同IP或連接埠向用戶端回送包.

如何根據STUN伺服器提供的功能來確認網路類型呢? rfc3489給出了如過程:

這個過程可概括如下:

1, STUN用戶端向STUN伺服器發送請求,要求得到自身經NAT映射後的地址:

a,收不到伺服器回複,則認為UDP被防火牆阻斷,不能通訊,網路類型:Blocked.

b,收到伺服器回複,對比本地地址,如果相同,則認為無NAT裝置,進入第2步,否則認為有NAT裝置,進入3步.

2, (已確認無NAT裝置)STUN用戶端向STUN伺服器發送請求,要求伺服器從其他IP和PORT向用戶端回複包:

a,收不到伺服器從其他IP地址的回複,認為包被前置防火牆阻斷,網路類型:Symmetric UDP Firewall.

b,收到則認為用戶端處在一個開放的網路上,網路類型:Opened.

3, (已確認存在NAT裝置)STUN用戶端向STUN伺服器發送請求,要求伺服器從其他IP和PORT向用戶端回複包:

a,收不到伺服器從其他IP地址的回複,認為包被前置NAT裝置阻斷,進入第4步.

b,收到則認為NAT裝置類型為Full Cone,即網路類型:Full Cone NAT.

4, STUN用戶端向STUN伺服器的另外一個IP地址發送請求,要求得到自身經NAT映射後的地址,並對比之:

a,地址不相同,則網路類型:Symmetric NAT.

b,相同則認為是Restricted NAT,進入第5步,進一步確認類型.

5, (已確認Restricted NAT裝置)STUN用戶端向STUN伺服器發送請求,要求伺服器從相同IP的其他PORT向用戶端回複包:

a,收不到伺服器從其他PORT地址的回複,認為包被前置NAT裝置阻斷,網路類型:Port Restricted cone NAT.

b,收到則認為網路類型: Restricted cone NAT.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.