Skip to content

Instance Creation Process

Mikhail Savochkin edited this page Dec 5, 2016 · 2 revisions

p2p runs RPC listeners, which waits for commands from p2p client. When daemon received 'start' command (p2p start ...) instance creation process started.

First of all, p2p checks if hash and TAP interface IP provided as arguments is unique. Then, p2p runs a goroutine for a new instance and returns a response to a p2p client.

This new goroutine is a p2p instance and all p2p internals is happening inside, separated from daemon (which is only listening to commands) and from other instances. This means, each internal procedure will be repeated for each instance (e.g. connection to a DHT server).

During instance creation, p2p connects to a DHT server, sends to it provided hash and waits for unique ID for this instance. ID is unique across all p2p clients that is connection to this DHT server family. It also may request IP address for instance if -ip dhcp argument was provided.

After receiving ID, p2p's DHT client requests list of participants of a current swarm (identified by hash) and waits for other peers IDs. When it receives the list of IDs it checks whether this peer is known or not. If it's unknown peer, DHT client requests IP information from DHT server. When it receives list of IPs it starts to establish connection to a peer.

First, it checks if any of peer's IPs correlates with it's own LAN IP addresses and belong to a same subnet. If it find any overlap it tries to handshake with a client. If it can't receive handshake response, p2p fallbacks to an Internet IP of a peer and tries to handshake over it. Again, if it doesn't receives handshake response, then it will switch to proxy.

DHT client requests list of single proxy server from list of proxies. When DHT server responses with proxy IP, p2p client will handshake it and received unique Tunnel ID for this peer. Meanwhile, DHT server will notify second peer about failed attempts to handshake with it and ask it to handshake with the same proxy too.

When both clients successfully handshake with proxy they start to communicate with each other, using received Tunnel ID (Proxy uses Tunnel ID to route packets to a destination peer).

Handshake procedure between two peers is a simple encoded message, which contains ID, TAP IP address and it's MAC address. This information is going to be stored in a peer table and used to route packet received by TUN/TAP device to it's destination.

Instance Flow