最近想搞UDP穿路由的程序,有个问题不太明白。

来源:百度文库 编辑:神马文学网 时间:2024/10/04 07:22:15
Ford, Srisuresh & Kegel                                        [Page 15]
Internet-Draft     P2P applications across middleboxes      October 2003
A-S 155.99.25.11:62000                        B-S 138.76.29.7:31000
|                                             |
|                                             |
Client A                                      Client B
10.0.0.1:1234                                 10.1.1.3:1234
NAT A has assigned its own UDP port 62000 to the communication
session between A and S, and NAT B has assigned its port 31000 to the
session between B and S.  By communicating through server S, A and B
learn each other‘s public IP addresses and port numbers as observed
by S.  Client A now starts sending UDP messages to port 31001 at
address 138.76.29.7 (note the port number increment), and client B
simultaneously starts sending messages to port 62001 at address
155.99.25.11.  If NATs A and B assign port numbers to new sessions
sequentially, and if not much time has passed since the A-S and B-S
sessions were initiated, then a working bi-directional communication
channel between A and B should result.  A‘s messages to B cause NAT A
to open up a new session, to which NAT A will (hopefully) assign
public port number 62001, because 62001 is next in sequence after the
port number 62000 it previously assigned to the session between A and
S.  Similarly, B‘s messages to A will cause NAT B to open a new
session, to which it will (hopefully) assign port number 31001.  If
both clients have correctly guessed the port numbers each NAT assigns
to the new sessions, then a bi-directional UDP communication channel
will have been established as shown below.
Server S
18.181.0.31:1234
|
|
+----------------------+----------------------+
|                                             |
NAT A                                         NAT B
A-S 155.99.25.11:62000                        B-S 138.76.29.7:31000
A-B 155.99.25.11:62001                        B-A 138.76.29.7:31001
|                                             |
|                                             |
Client A                                      Client B
10.0.0.1:1234                                 10.1.1.3:1234
Obviously there are many things that can cause this trick to fail.
If the predicted port number at either NAT already happens to be in
use by an unrelated session, then the NAT will skip over that port
number and the connection attempt will fail.  If either NAT sometimes
or always chooses port numbers non-sequentially, then the trick will
fail.  If a different client behind NAT A (or B respectively) opens
up a new outgoing UDP connection to any external destination after A
(B) establishes its connection with S but before sending its first