I was recently trying to model a simple peer to peer network. In my concept, peers try to both open a port and establish a connection with as many peers as possible using a third party "bootstrap" peer source to find a peer. I quickly noticed a problem:
- Peer A opens a port 6013 and announces his existence
- Peer B opens a port 31235 and announces his existence
- Peer A receives the announcement of B and connects to peer B, using 3111 as its source port
- Peer B receives the announcement of A and connects to peer B, using 7777 as its source port
At this moment we have both A connecting to the server socket of B and B connecting to the server socket of A without a way to be sure that these are actually the same hosts talking to each other - the source ports of client connections do not suggest which server port they have open. Moreover, I can't identify peers by their IP addresses because they might be behind some kind of a NAT.
What can I do to avoid such situation? Doubling the connections shouldn't really cause de-synchronization problems in my case, but this redundancy would probably lead to overhead I don't want.