import type { Metadata } from 'next'; import UDPHolePunchingDiagram from '@/components/diagrams/UDPHolePunchingDiagram'; import NATTraversalDiagram from '@/components/diagrams/NATTraversalDiagram'; import PeerDiscoveryDiagram from '@/components/diagrams/PeerDiscoveryDiagram'; export const metadata: Metadata = { title: 'How P2P Works - Understanding Peer-to-Peer Networking', description: 'Learn about peer-to-peer networking, UDP hole-punching, NAT traversal, and how P2NS uses these technologies.', keywords: ['P2P networking', 'peer-to-peer', 'UDP hole-punching', 'NAT traversal', 'Hyperswarm', 'DHT', 'Corestore', 'P2P explained'], authors: [{ name: 'Raven Scott' }], openGraph: { title: 'How P2P Works - Understanding Peer-to-Peer Networking', description: 'Learn about peer-to-peer networking, UDP hole-punching, NAT traversal, and how P2NS uses these technologies.', url: 'https://p2ns.space/learn/p2p', siteName: 'P2NS', type: 'website', }, twitter: { card: 'summary_large_image', title: 'How P2P Works - Understanding Peer-to-Peer Networking', description: 'Learn about peer-to-peer networking, UDP hole-punching, NAT traversal, and how P2NS uses these technologies.', }, alternates: { canonical: 'https://p2ns.space/learn/p2p', }, }; export default function P2PPage() { return (
{/* Introduction */}

Understanding Peer-to-Peer Networking

Peer-to-peer (P2P) networking is a distributed architecture where participants (peers) share resources and communicate directly without relying on a central server. P2NS leverages P2P technology to create a resilient, decentralized DNS system.

Key Concept: In a P2P network, each node can act as both a client and a server, enabling direct communication and resource sharing between peers.

{/* UDP Hole Punching */}

UDP Hole Punching

UDP hole-punching lets two peers behind NAT establish a direct path. In the Holepunch stack (used by Hyperswarm and Holesail), peers coordinate through HyperDHT—a distributed hash table—not a single central rendezvous server. When both sides use predictable NAT ports, most sessions connect directly; symmetric NAT or blocked UDP may fall back to encrypted relays.

How It Works

  1. Announce on the DHT: Each peer registers its public endpoints with HyperDHT under its cryptographic identity.
  2. Signaling through the DHT: When Alice wants Bob, the DHT routes a connect intent so both sides learn each other's public IP and port.
  3. Simultaneous UDP probes: Both peers send UDP packets to each other's public endpoints at the same time (often via libudx).
  4. NAT creates a "hole": Outgoing packets open a temporary mapping so return traffic from that peer can pass through the firewall.
  5. Direct path or relay: On success, peers talk directly; if hole-punching fails (e.g. symmetric NAT on both sides), Hyperswarm can route through blind relays that only see encrypted Noise traffic (~85% of connections succeed with hole punching in typical conditions).
{/* NAT Traversal */}

NAT Traversal

Network Address Translation (NAT) is used by routers to share a single public IP address among multiple devices. NAT traversal techniques allow P2P connections to work through these devices.

Types of NAT

  • Open NAT: Predictable mapping—hole punching usually works.
  • Consistent NAT: Same external port per internal port—still punchable with coordination.
  • Random / symmetric NAT: Unpredictable external ports—often needs a relay fallback.

CGNAT Challenges

Carrier-Grade NAT (CGNAT) adds an additional layer of NAT, making traversal even more complex. P2NS uses Holesail, which is specifically designed to handle these challenging scenarios.

Holesail implements advanced techniques including:

  • Multiple simultaneous connection attempts
  • Protocol-specific optimizations
{/* Peer Discovery */}

Peer Discovery with Hyperswarm

Hyperswarm is a high-level API on top of HyperDHT (Kademlia-style DHT). Peers announce and discover each other by 32-byte topics without a central directory server.

How Hyperswarm Works

Topic-based discovery: P2NS hashes TOPIC_SEED (default p2ns-dns) with SHA-256 to form a 32-byte topic. Every node interested in the network joins that topic.

DHT lookup: HyperDHT returns peers that have announced on the topic. DHT results are candidates only—each connection still completes a Noise XX handshake so the remote party must prove key ownership.

Connection establishment: Hyperswarm coordinates UDP hole punching (and relay fallback when needed), then runs encrypted streams for Core RPC and plugin traffic.

Automatic Reconnection: Hyperswarm automatically handles peer churn, reconnecting when peers go offline and come back online.

{/* Holepunch stack */}

The Holepunch Connection Stack

P2NS uses the Holepunch stack in layers: raw UDP paths, then encryption, then application protocols.

  1. libudx — reliable UDP between peers after hole punching
  2. Secret Stream (Noise XX) — mutual authentication and XChaCha20-Poly1305 encryption
  3. Protomux / protomux-rpc — multiplexed channels for core invites, consensus, and plugins
  4. Hypercore / Corestore — append-only, Merkle-verified logs for DNS state

Domain tunnels use Holesail (separate hashes on the DHT) for reaching services behind NAT; the P2NS swarm topic is only for DNS peer sync.

{/* Corestore & Autopass */}

Decentralized Storage & Security

Corestore

Corestore manages Hypercore append-only logs. Each block is Merkle-linked and signed with Ed25519, so peers verify data by proof—not by trusting who delivered it. P2NS stores domain claims and votes in this replicated log.

  • Immutable append-only history
  • Sparse replication of blocks
  • Automatic sync across peers
  • Domain conflicts resolved by peer voting (not Autobase)

Autopass

Autopass manages secure writer additions to the Corestore. It ensures only authorized peers can write to the ledger, preventing spam and attacks.

  • Invitation-based access control
  • Genesis master creates the ledger; masters and writers issue invites
  • One networkId per network (manifest)
  • Cryptographic verification
{/* Glossary */}

Glossary

{[ { term: 'NAT (Network Address Translation)', definition: 'A method of remapping IP addresses, allowing multiple devices to share a single public IP address.' }, { term: 'CGNAT (Carrier-Grade NAT)', definition: 'A large-scale NAT implementation used by ISPs to conserve IPv4 addresses, adding complexity to P2P connections.' }, { term: 'UDP Hole Punching', definition: 'A technique that allows two peers behind NAT devices to establish a direct connection by coordinating simultaneous UDP probes, usually signaled over HyperDHT.' }, { term: 'HyperDHT', definition: 'The Kademlia DHT used by Hyperswarm for peer announcements, lookups, and hole-punch coordination—distributed, not a single rendezvous server.' }, { term: 'DHT (Distributed Hash Table)', definition: 'A decentralized key-value store that allows peers to find each other without a central directory.' }, { term: 'Peer Discovery', definition: 'The process of finding other peers in a P2P network, typically using DHT topics (as P2NS does on Hyperswarm).' }, { term: 'Consensus', definition: 'In P2NS, agreement on domain ownership through voting on claims in the shared Corestore log (distinct from Autobase-style multi-writer ordering).' }, ].map((item) => (
{item.term}
{item.definition}
))}
); }