# libp2p/rust-libp2p architecture

> Architecture diagram of the GitHub repository libp2p/rust-libp2p, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/libp2p/rust-libp2p
- Repository: https://github.com/libp2p/rust-libp2p

## Overview

##### Main Components Structure

a) Core Layer (Bottom)
- Transport API
- StreamMuxer API
- Core protocols and types
- Identity management

b) Transport Layer
- TCP/UDP implementations
- WebSocket/WebRTC implementations
- TLS/Noise security protocols
- DNS resolution

c) Muxing Layer
- Yamux multiplexer
- Mplex multiplexer
- Stream management

d) Swarm Layer
- Network behavior management
- Connection handling
- Protocol negotiation

e) Protocol Layer (Top)
- Application protocols (Ping, Identify, Kad, etc.)
- Custom protocol implementations

##### Component Relationships

a) Data Flow
- How data moves from transport → muxing → swarm → protocols

b) Dependencies
- Core dependencies that multiple components share

##### Specific Elements

a) Transport Section:
**┌─ Transport Layer ─────────────────┐**
- ┌─────────┐ ┌────────┐ ┌────────┐ │
- TCP   │ │  WebRTC│ │  QUIC  │ │
- ┘ └────────┘ └────────┘ │
- ┌─────────┐ ┌────────┐           │
- TLS   │ │ Noise  │           │
- ┘ └────────┘           │
- ┘

b) Protocol Section:
**┌─ Protocol Layer ──────────────────┐**
- ┌────────┐ ┌────────┐ ┌─────────┐ │
- Kad   │ │  Ping  │ │Identify │ │
- ┘ └────────┘ └─────────┘ │
- ┌────────┐ ┌────────┐            │
- Gossipsub│ │AutoNAT│            │
- ┘ └────────┘            │
- ┘

##### Important Details to Capture

a) Security Layer:
- How identity and encryption are handled
- Where TLS/Noise protocols integrate

b) Network Layer:
- NAT traversal components
- Relay capabilities
- Multiaddress handling

c) Protocol Negotiation:
- How protocols are negotiated
- Multistream-select mechanism

##### Implementation Notes

- Use standardized symbols for network components
- Version compatibility where relevant
- Optional vs. required components
- Experimental features differently

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Style Definitions
    classDef core fill:#2374ab
    classDef transport fill:#45b7d1
    classDef security fill:#ff6b6b
    classDef protocol fill:#845ec2
    classDef swarm fill:#ff9671
    classDef muxing fill:#4da167
    
    %% Legend
    subgraph Legend
        L1[Core]:::core
        L2[Transport]:::transport
        L3[Security]:::security
        L4[Protocol]:::protocol
        L5[Swarm]:::swarm
        L6[Muxing]:::muxing
    end

    %% Core Layer
    subgraph CoreLayer
        CoreTypes["Core Types"]
        TransportAPI["Transport API"]
        StreamMuxerAPI["StreamMuxer API"]
        Identity["Identity Management"]
    end
    
    %% Transport Layer
    subgraph TransportLayer
        TCP["TCP Transport"]
        WebSocket["WebSocket"]
        WebRTC["WebRTC"]
        QUIC["QUIC"]
        DNS["DNS"]
        
        subgraph SecurityProtocols
            TLS["TLS"]
            Noise["Noise Protocol"]
        end
    end
    
    %% Muxing Layer
    subgraph MuxingLayer
        Yamux["Yamux Multiplexer"]
        Mplex["Mplex Multiplexer"]
    end
    
    %% Swarm Layer
    subgraph SwarmLayer
        SwarmCore["Swarm Core"]
        BehaviourMgmt["Behaviour Management"]
        ConnectionHandler["Connection Handler"]
    end
    
    %% Protocol Layer
    subgraph ProtocolLayer
        Kad["Kademlia"]
        Ping["Ping"]
        Identify["Identify"]
        Gossipsub["Gossipsub"]
        AutoNAT["AutoNAT"]
        Relay["Relay"]
        ReqRes["Request-Response"]
        Stream["Stream"]
    end

    %% Relationships
    CoreTypes --> TransportAPI
    CoreTypes --> StreamMuxerAPI
    CoreTypes --> Identity
    
    TransportAPI --> TCP & WebSocket & WebRTC & QUIC & DNS
    TCP & WebSocket & WebRTC --> SecurityProtocols
    
    StreamMuxerAPI --> Yamux
    StreamMuxerAPI --> Mplex
    
    Yamux & Mplex --> SwarmCore
    SwarmCore --> BehaviourMgmt
    SwarmCore --> ConnectionHandler
    
    SwarmCore --> Kad & Ping & Identify & Gossipsub & AutoNAT & Relay & ReqRes & Stream

    %% Styling
    class CoreTypes,TransportAPI,StreamMuxerAPI,Identity core
    class TCP,WebSocket,WebRTC,QUIC,DNS transport
    class TLS,Noise security
    class Kad,Ping,Identify,Gossipsub,AutoNAT,Relay,ReqRes,Stream protocol
    class SwarmCore,BehaviourMgmt,ConnectionHandler swarm
    class Yamux,Mplex muxing

    %% Click Events
    click CoreTypes "https://github.com/libp2p/rust-libp2p/blob/master/core/src/lib.rs"
    click TransportAPI "https://github.com/libp2p/rust-libp2p/tree/master/core/src/transport/"
    click StreamMuxerAPI "https://github.com/libp2p/rust-libp2p/tree/master/core/src/muxing/"
    click Identity "https://github.com/libp2p/rust-libp2p/tree/master/identity/src/"
    click TCP "https://github.com/libp2p/rust-libp2p/tree/master/transports/tcp/"
    click WebSocket "https://github.com/libp2p/rust-libp2p/tree/master/transports/websocket/"
    click WebRTC "https://github.com/libp2p/rust-libp2p/tree/master/transports/webrtc/"
    click TLS "https://github.com/libp2p/rust-libp2p/tree/master/transports/tls/"
    click Noise "https://github.com/libp2p/rust-libp2p/tree/master/transports/noise/"
    click DNS "https://github.com/libp2p/rust-libp2p/tree/master/transports/dns/"
    click Yamux "https://github.com/libp2p/rust-libp2p/tree/master/muxers/yamux/"
    click Mplex "https://github.com/libp2p/rust-libp2p/tree/master/muxers/mplex/"
    click Ping "https://github.com/libp2p/rust-libp2p/tree/master/protocols/ping/"
    click Identify "https://github.com/libp2p/rust-libp2p/tree/master/protocols/identify/"
    click Kad "https://github.com/libp2p/rust-libp2p/tree/master/protocols/kad/"
    click Gossipsub "https://github.com/libp2p/rust-libp2p/tree/master/protocols/gossipsub/"
    click AutoNAT "https://github.com/libp2p/rust-libp2p/tree/master/protocols/autonat/"
    click Relay "https://github.com/libp2p/rust-libp2p/tree/master/protocols/relay/"
    click ReqRes "https://github.com/libp2p/rust-libp2p/tree/master/protocols/request-response/"
    click Stream "https://github.com/libp2p/rust-libp2p/tree/master/protocols/stream/"
    click SwarmCore "https://github.com/libp2p/rust-libp2p/tree/master/swarm/src/"
    click BehaviourMgmt "https://github.com/libp2p/rust-libp2p/tree/master/swarm/src/behaviour/"
    click ConnectionHandler "https://github.com/libp2p/rust-libp2p/tree/master/swarm/src/connection/"
```
