Peer-to-Peer Networking

Peer-to-peer (P2P) networking is a decentralised architecture where nodes (peers) communicate directly without relying on centralised servers. Each peer acts as both client and server, sharing resources and services with other peers.

P2P Characteristics

  • Decentralisation: No single point of failure or control.
  • Scalability: Adding peers increases capacity, unlike client-server models.
  • Resilience: Network continues functioning despite node failures.
  • Resource sharing: Peers share bandwidth, storage, and processing power.

P2P Architectures

Pure P2P

Every peer is equal. There are no dedicated servers. Examples: early BitTorrent (without trackers), IPFS.

Hybrid P2P

A central server assists in discovery but data flows peer-to-peer. Examples: BitTorrent with trackers, Napster (central index, P2P transfer).

Structured P2P

Peers are organised in a deterministic topology (DHT, distributed hash table). Lookups are efficient (O(log n)). Examples: Kademlia (used by BitTorrent, Ethereum), Chord, Pastry.

Unstructured P2P

Peers connect randomly. Discovery uses flooding or random walks. Examples: Gnutella, early KaZaA.

P2P Technologies on Linux

BitTorrent

BitTorrent is the most widely used P2P protocol for file distribution. It breaks files into pieces and distributes them across peers.

Linux clients:

Create a torrent:

transmission-create --private --tracker udp://tracker.example.com:6969/announce myfile.iso -o myfile.torrent

IPFS (InterPlanetary File System)

IPFS addresses content by its hash, not its location. Content-addressed storage enables permanent, decentralised web hosting.

ipfs add file.txt              # Add file to IPFS
ipfs cat QmHash                # Retrieve file by hash
ipfs daemon                    # Run IPFS node

NAT Traversal

P2P applications must handle NAT traversal to communicate across different networks:

  • UPnP/NAT-PMP: Automatic port forwarding on home routers.
  • STUN/TURN/ICE: Used by WebRTC for NAT traversal.
  • Hole punching: Simultaneous outgoing connections that "punch through" NAT.

P2P Use Cases

  • File sharing: BitTorrent, IPFS.
  • Distributed storage: IPFS, Storj, Filecoin.
  • Blockchain: Bitcoin, Ethereum nodes propagate transactions P2P.
  • Real-time communication: WebRTC, VoIP.
  • CDN alternatives: P2P content delivery (e.g., WebTorrent in browsers).

Security Considerations

  • Trust: P2P networks may contain malicious nodes distributing malware.
  • Bandwidth: P2P applications consume upload bandwidth.
  • Legal: Copyrighted material distribution may violate laws.
  • Firewall traversal: P2P traffic may be blocked or throttled.