nattypedetection.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <HTML>
  2. <HEAD>
  3. <TITLE>Lobby2Client_PS3</TITLE>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. </HEAD>
  6. <link href="RaknetManual.css" rel="stylesheet" type="text/css">
  7. <meta name="title" content="RakNet - Advanced multiplayer game networking API">
  8. </HEAD>
  9. <BODY BGCOLOR="#ffffff" LINK="#003399" vlink="#003399" alink="#003399" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0"">
  10. <span style="background-color: rgb(255, 255, 255);"><img src="RakNet_Icon_Final-copy.jpg" alt="Oculus VR, Inc." width="150" height="150"></span><BR>
  11. <BR>
  12. <table width="100%" border="0"><tr>
  13. <td bgcolor="#2c5d92" class="RakNetWhiteHeader">
  14. <img src="spacer.gif" width="8" height="1">NAT Type detection</td>
  15. </tr></table>
  16. <TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR>
  17. <TD>
  18. <p><span class="RakNetBlueHeader">Determine in advance if you can complete NAT punchthroug</span></p>
  19. <a
  20. href="http://www.jenkinssoftware.com/raknet/manual/natpunchthrough.html">NAT
  21. punchthrough</a> success rates is based on what kind of algorithm the
  22. NAT uses.</p>
  23. <p><strong>Full cone NAT</strong>: Accepts any datagrams to a port
  24. that has been previously used. Will accept the first datagram from the
  25. remote peer.</p>
  26. <p><strong>Address-Restricted cone NAT</strong>: Accepts datagrams
  27. to a port as long as the datagram source IP address is a system we have
  28. already sent to. Will accept the first datagram if both systems send
  29. simultaneously. Otherwise, will accept the first datagram after we have
  30. sent one datagram.</p>
  31. <p><strong>Port-Restricted cone NAT</strong>: Same as
  32. address-restricted cone NAT, but we had to send to both the correct
  33. remote IP address and correct remote port. The same source address and
  34. port to a different destination uses the same mapping.</p>
  35. <p><strong>Symmetric NAT</strong>: A different port is chosen for
  36. every remote destination. The same source address and port to a
  37. different destination uses a different mapping. Since the port will be
  38. different, the first external punchthrough attempt will fail. For this
  39. to work it requires port-prediction (MAX_PREDICTIVE_PORT_RANGE&gt;1)
  40. and that the router chooses ports sequentially.</p>
  41. <center>
  42. <b>Success Graph</b>
  43. <table border="1">
  44. <tbody><tr>
  45. <td>Router Type</td>
  46. <td><i>Full cone NAT</i></td>
  47. <td><i>Address-Restricted cone NAT</i></td>
  48. <td><i>Port-Restricted cone NAT</i></td>
  49. <td><i>Symmetric NAT</i></td>
  50. </tr>
  51. <tr>
  52. <td><i>Full cone NAT</i></td>
  53. <td>YES</td>
  54. <td>YES</td>
  55. <td>YES</td>
  56. <td>YES</td>
  57. </tr>
  58. <tr>
  59. <td><i>Address-Restricted cone NAT</i></td>
  60. <td>YES</td>
  61. <td>YES</td>
  62. <td>YES</td>
  63. <td>YES</td>
  64. </tr>
  65. <tr>
  66. <td><i>Port-Restricted cone NAT</i></td>
  67. <td>YES</td>
  68. <td>YES</td>
  69. <td>YES</td>
  70. <td>NO</td>
  71. </tr>
  72. <tr>
  73. <td><i>Symmetric NAT</i></td>
  74. <td>YES</td>
  75. <td>YES</td>
  76. <td>NO</td>
  77. <td>NO</td>
  78. </tr>
  79. </tbody></table>
  80. <p>&nbsp;</p>
  81. </center>
  82. <p>The NatTypeDetection plugins allow you to determine which type of NAT
  83. you have, and therefore if NAT punchthrough is likely to complete or
  84. not. This can be determined in advance of joining a game.</p>
  85. <p><span class="RakNetBlueHeader">NAT Type Detection algorithm</span></p>
  86. <ol>
  87. <li>Client opens two ports on the same IP address. In NatTypeDetectionClient, the RakNet socket is the first port, c2 is the socket on the second port, created in NatTypeDetectionClient::DetectNATType()</li>
  88. <li>Server opens two ports on one IP address, and one port on three
  89. additional IP addresses. This is done in NatTypeDetectionServer::Startup(). The first port on the first IP address is the normal RakNet socket. The second port on the first IP address is s1p2. The other three addresses are bound to the sockets s2p3, s3p4, and s4p5.</li>
  90. <li>Client connects to the server on the first IP address normally</li>
  91. <li>Client requests NAT type detection begins</li>
  92. <li>Server attempts to send to client's second port. This was a port that was never opened previously on the client, therefore if received, then
  93. the client is not behind a NAT. This is done in NatTypeDetectionServer::Update(), defined by STATE_TESTING_NONE_1 and STATE_TESTING_NONE_2. The reason for 2 attempts is that every attempt occurs twice. The time for each attempt is the ping * 3 + 50 milliseconds. s4p5 is used for this.</li>
  94. <li>Server sends from a different IP address to client's first port, the port that RakNet is connected on.
  95. If received, then the client will accept datagrams from any source IP address to a port already in use. This is therefore full-cone NAT. s2p3 is used for this.</li>
  96. <li>Server sends from the second port on the already-connected IP
  97. address, s1p2. If received, then the client is using address-restricted cone
  98. NAT.</li>
  99. <li>Client sends to another IP address on the server, from its first
  100. (already connected) port. If the IP address and port is the same, then
  101. the client uses the same external IP address and port to all connections
  102. from the same source address. This is port-restricted NAT.</li>
  103. <li>Else symmetric NAT.</li>
  104. </ol>
  105. <p><span class="RakNetBlueHeader">Client Implementation</span></p>
  106. <ol>
  107. <li>Create an instance of the plugin: <span class="RakNetCode">NatTypeDetectionServer
  108. nayTypeDetectionClient;</span></li>
  109. <li>Attach the plugin to an instance of RakPeerInterface: <span
  110. class="RakNetCode">rakPeer-&gt;AttachPlugin(&amp;nayTypeDetectionClient);</span></li>
  111. <li>Connect to the server, and wait for
  112. ID_CONNECTION_REQUEST_ACCEPTED. Use the following line to use the free
  113. server provided by RakNet: <span class="RakNetCode">rakPeer-&gt;Connect("8.17.250.34",
  114. 60481, 0, 0);</span></li>
  115. <li>Call DetectNATType with the SystemAddress of the server.</li>
  116. <li>Wait for ID_NAT_TYPE_DETECTION_RESULT<span class="RakNetCode"></span></li>
  117. <li>Byte 1 contains the type of NAT that you have. See the
  118. enumeration NATTypeDetectionResult in NATTypeDetectionCommon.h</li>
  119. <li>Various utility functions are provided for this enumeration: <span
  120. class="RakNetCode">CanConnect(), NATTypeDetectionResultToString(),
  121. NATTypeDetectionResultToStringFriendly()</span></li>
  122. </ol>
  123. <p><span class="RakNetBlueHeader">Server Implementation</span></p>
  124. <ol>
  125. <li>Host a server somewhere, not using NAT / e.g. behind a
  126. firewall. (RakNet provides a free one at 8.17.250.34:60481, however you
  127. may wish to host your own for consistent uptime). The server must have
  128. enough external IP addresses, as described in <strong>NAT Type Detection
  129. Algorithm</strong>.</li>
  130. <li>Create an instance of the plugin: <span class="RakNetCode">NatTypeDetectionServernatTypeDetectionServer;</span></li>
  131. <li>Attach the plugin: <span class="RakNetCode">rakPeer-&gt;AttachPlugin(&amp;natTypeDetectionServer);</span></li>
  132. <li>Get a list of IP addresses on your system<br>
  133. <span class="RakNetCode">char ipList[
  134. MAXIMUM_NUMBER_OF_INTERNAL_IDS ][ 16 ];<br>
  135. unsigned int binaryAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS];<br>
  136. SocketLayer::Instance()-&gt;GetMyIP( ipList, binaryAddresses );</span></li>
  137. <li>Call <span class="RakNetCode">natTypeDetectionServer.Startup(ip2,ip3,ip4);
  138. // ip2,ip3,ip4 must be ip addresses not already in use. If you bound
  139. RakNet to ip1 in the call to RakPeer::Startup(), then use the 2nd to 4th
  140. indices in ipList.</span></li>
  141. </ol>
  142. <p><span class="RakNetBlueHeader">Sample </span><br>
  143. <br>
  144. See the sample <em>\Samples\NATCompleteClient</em></p></TD>
  145. </TR></TABLE>
  146. <table width="100%" border="0"><tr><td bgcolor="#2c5d92" class="RakNetWhiteHeader">
  147. <img src="spacer.gif" width="8" height="1">See Also</td>
  148. </tr></table>
  149. <TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR><TD><p><A HREF="index.html">Index</A><BR>
  150. <a href="nattraversalarchitecture.html">NAT Traversal architecture</a><BR>
  151. <a href="natpunchthrough.html">NAT Punchthrough</a>
  152. </p></TD></TR></TABLE>
  153. </BODY>
  154. </HTML>
粤ICP备19079148号