Architecture
The networking system manages all device-to-device communication through a single service that handles connections, protocols, and state management.Core Components
NetworkingService coordinates all networking operations. It manages the Iroh endpoint, tracks device states, and routes messages to protocol handlers.Network Transport
Iroh provides the underlying transport using QUIC, which offers:- Built-in encryption using TLS 1.3
- Multiplexed streams over a single connection
- Reliable delivery with automatic retransmission
- NAT traversal with 90%+ success rate
- Relay fallback when direct connections fail
Protocol System
The networking module uses ALPN (Application-Layer Protocol Negotiation) to route connections to specific protocol handlers.Device Discovery
Devices find each other through multiple mechanisms:Local Network Discovery
Iroh automatically discovers devices on your local network using mDNS. When a device starts, it broadcasts its presence and listens for others.Manual Connection
You can connect to devices using their NodeAddr, which includes their NodeId and network addresses.Direct addresses work on local networks. The relay URL enables connections across the internet when direct connections fail.
Device Pairing
Pairing establishes trust between devices using cryptographic signatures and user-friendly codes.Pairing Flow
The initiator generates a pairing code that the joiner enters to establish trust.1
Generate Pairing Code
The initiator creates a BIP39 mnemonic code:
2
Exchange Device Info
Both devices exchange their information and public keys:
3
Challenge-Response
The initiator challenges the joiner to prove they have the code:
4
Establish Session
Both devices derive session keys for future communication:
Pairing Security
The pairing protocol prevents several attacks:- Man-in-the-middle: Public key exchange with out-of-band verification
- Replay attacks: Fresh challenges for each pairing attempt
- Brute force: Rate limiting on pairing attempts
- Eavesdropping: All communication encrypted after initial handshake
Message Protocol
Paired devices communicate using an encrypted messaging protocol.Message Types
Message Flow
Messages are serialized as JSON and encrypted using session keys:Reliability
QUIC provides reliable delivery, but the application layer adds:- Message acknowledgments for critical operations
- Automatic retries with exponential backoff
- Connection health monitoring with periodic pings
- Graceful reconnection after network changes
File Transfer
The file transfer protocol enables secure, resumable file sharing between devices.Transfer Process
1
Request File
Device A requests a file by its entry ID:
2
Stream Chunks
Device B streams the file in encrypted chunks:
3
Verify Transfer
Both devices verify the transfer using checksums:
Transfer Features
- Resumable transfers: Continue from where you left off
- Progress tracking: Real-time updates on transfer status
- Bandwidth throttling: Respect network limits
- Parallel transfers: Multiple files simultaneously
- Compression: Optional gzip compression for text files
Connection Management
The event loop handles all incoming connections and routes them appropriately.Event Loop
Connection States
Connections transition through several states:- Connecting: Initial QUIC handshake
- Connected: Active connection, can send/receive
- Idle: No recent activity, may be closed
- Closing: Graceful shutdown in progress
- Closed: Connection terminated
Keep-Alive
Connections are kept alive using periodic pings:NAT Traversal
Iroh handles NAT traversal automatically using several techniques:Direct Connection
First, Iroh attempts a direct connection using known addresses:STUN
If direct connection fails, Iroh uses STUN to discover public addresses:Relay Fallback
When both devices are behind symmetric NATs, Iroh falls back to relay servers:Relay servers don’t decrypt your data. They only forward encrypted packets between devices.
Security
Encryption Layers
The networking stack provides multiple encryption layers:- Transport encryption: QUIC’s built-in TLS 1.3
- Application encryption: Additional encryption using session keys
- File encryption: Per-file encryption keys for transfers
Key Management
Trust Model
- Device identity: Ed25519 signatures prove device authenticity
- Pairing verification: Out-of-band code exchange prevents MITM
- Forward secrecy: New keys for each session and transfer
- No central authority: Direct device-to-device trust
API Usage
Initialize Networking
Pair Devices
Send Messages
Transfer Files
Performance
Benchmarks
Typical performance on local network:- Connection setup: 10-50ms
- Message latency: 1-5ms
- File transfer: 100MB/s+ (gigabit network)
- Memory usage: ~10MB per connection
Optimization Strategies
- Connection pooling: Reuse connections for multiple operations
- Stream multiplexing: Multiple logical streams over one connection
- Adaptive chunking: Adjust chunk size based on network conditions
- Compression: Enable for text-heavy workloads
Troubleshooting
Connection Issues
If devices can’t connect:Common Problems
Problem: “Connection refused”- Check firewall allows UDP port 11204
- Verify both devices are running
- Ensure correct NodeId
- Check network allows UDP traffic
- Try relay connection instead of direct
- Verify NAT type using STUN
- Ensure pairing code is entered correctly
- Check code hasn’t expired (5 minute timeout)
- Verify clocks are roughly synchronized
Debug Commands
Implementation Details
Protocol Registration
New protocols are registered during initialization:Error Handling
The networking module uses a typed error system:State Persistence
Device relationships and session keys are encrypted and persisted via the KeyManager:Future Development
Planned Features
Enhanced Discovery- DHT-based global discovery
- Bluetooth device discovery
- QR code pairing
- Video streaming protocol
- Real-time collaboration
- Distributed compute
- Custom relay servers
- Relay server selection
- Bandwidth quotas
- Protocol buffer serialization
- Native stream handling
- Zero-copy transfers
