How we discover our own diallable IP address and port

if there are two peers that want to exchange a file (f.e. in a file sharing service based on libp2p) there is a need to connect peers directly with each other

They won’t be able to directly connect to each other in this case, so direct P2P communication won’t be possible. However they may still talk with each other if they both connect to some other node that will relay their messages to each other – and that’s exactly what @raul explained in the OP.

  • I’ll try to detail what will happen in this particular case (nodes A and B are interchangeable in the following):
    1. Node A will connect to the network and find, based on the algorithm described in the OP, that it is behind a NAT and cannot receive any direct connections
    2. Node A will connect to some relay node C in the network and publish that relay’s address in the DHT as a way to receive connections
      • i.e.: Rather than publish /ip4/A.A.A.A/tcp/A/p2p/NODE-ID-A as its public address it will publish /ip4/C.C.C.C/tcp/c/p2p/NODE-ID-C/p2p-circuit/p2p/NODE-ID-A instead
    3. Some time later node B wants to connect to node A and finds that the published DHT entry telling it to first connect to the relay node C and ask it to relay all traffic to node A
    4. Node B does this and a two-way connection is established

It is not clear to me whether node A will try to establish a direct connection to node B if node B has a public non-relay address, but it does not matter for “just” establishing any connection between A and B; it would likely be a very useful optimization however.

2 Likes