P2P CheatSheet
What is P2P?
It's a general term to describe two machine can communicate directly with each other without an intermediate server. Each machine acts as the server and receiver.
How does P2P communication work?
- There is a central server (Rendezvous Server) used to store all available machines. The central server saves information about each machine available for future P2P connections.
- When two machines knows the information about each other, they try to build the direct communication channel.
- If they're not behind any NAT, this is easy, each acts as the server and client with their unique IPs.
- If they're behind NAT, then they need to tell the router how to set a port-mapping rule so that traffics coming from a particular port can be redirected to them. They achieve this by following protocols that the router may or may not follow, so it is possible a P2P connection is blocked completely.
- Some examples of such protocols include: UPnP, NAT_PMP, STUN, TURN, Hole Punching, ICE.
An example:
Initiating Peer's Perspective (Let's say Alice wants to connect to Bob):
Alice needs to know Bob's public IP address and the relevant public port. This information is often obtained through a Rendezvous Server or other peer discovery mechanisms. This public IP and port represent the "address" of Bob's router as seen from the internet.
Alice initiates a connection attempt to Bob's public IP address and the target public port.
Bob's Router's Role:
When Alice's connection request arrives at Bob's router (at its public IP address and the target public port), the router needs to decide where to send this traffic on its local network.
This is where port mapping (port forwarding) comes into play. For Bob's computer to receive Alice's connection, there must be a rule in Bob's router that says: "Any traffic arriving at my public IP address on port X (the target port Alice is using) should be forwarded to the private IP address of Bob's computer (e.g., 192.168.0.15) on port Y (the port Bob's P2P application is listening on)."
How Does Bob's Device Tell the Router?
This is the key part you touched on:
Manual Configuration: The most straightforward way is for Bob (the user) to manually log into his router's configuration interface and set up this port forwarding rule. He needs to know the public port his P2P application expects connections on and the private IP address and port of his computer.
Automatic Configuration via Protocols (UPnP, NAT-PMP, PCP): As we discussed earlier, Bob's P2P application can attempt to request the router to create this port mapping automatically using protocols like UPnP, NAT-PMP, or PCP. If Bob's router supports and allows this, the application can dynamically tell the router how to redirect traffic for its specific ports to its local IP and port.
Therefore, to summarize:
Alice needs to know Bob's router's public IP and the specific public port associated with Bob's P2P service.
It is indeed up to Bob's device (via manual configuration or automatic port mapping protocols) to ensure that his router knows to forward incoming traffic on that specific public port to the correct private IP address and port of his P2P application.