libp2p-hs-0.1.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

Network.LibP2P.Switch.Upgrade

Description

Connection upgrade pipeline for the Switch.

Transforms a raw transport connection into a fully upgraded (secure + multiplexed) Connection by executing a 4-step pipeline: 1. multistream-select: negotiate security protocol ("/noise") 2. Noise XX handshake: encrypted channel + remote PeerId 3. multistream-select: negotiate muxer ("yamux1.0.0") 4. Yamux session init: multiplexed streams

See docs/08-switch.md §Connection Upgrading Pipeline.

Synopsis

Streaming handshake

performStreamHandshake :: KeyPair -> Direction -> StreamIO -> IO (NoiseSession, HandshakeResult) Source #

Perform a Noise XX handshake over a StreamIO using framed messages. Returns (NoiseSession, HandshakeResult) with the remote PeerId.

Encrypted StreamIO

noiseSessionToStreamIO Source #

Arguments

:: IORef NoiseSession

Send session state

-> IORef NoiseSession

Recv session state

-> IORef ByteString

Read buffer (decrypted but unconsumed bytes)

-> StreamIO

Raw (unencrypted) StreamIO

-> StreamIO 

Create an encrypted StreamIO from a NoiseSession and raw StreamIO.

Uses separate IORefs for send/recv session state (each direction's CipherState is independent in Noise). A read buffer (IORef ByteString) bridges Noise's message-boundary decryption with StreamIO's byte-level reads.

Yamux → MuxerSession adapter

yamuxToMuxerSession :: YamuxSession -> IO MuxerSession Source #

Wrap a YamuxSession as a MuxerSession. Starts sendLoop and recvLoop as background threads. The MuxerSession provides open/accept stream operations that produce StreamIO-compatible streams.

Full upgrade pipeline

upgradeOutbound :: KeyPair -> RawConnection -> IO Connection Source #

Upgrade an outbound (dialer) raw connection. Pipeline: mss(noise) → Noise XX → mss(yamux/1.0.0) → Yamux client

upgradeInbound :: KeyPair -> RawConnection -> IO Connection Source #

Upgrade an inbound (listener) raw connection. Pipeline: mss(noise) → Noise XX → mss(yamux/1.0.0) → Yamux server

Helpers (exported for testing)

readExact :: StreamIO -> Int -> IO ByteString Source #

Read exactly n bytes from a StreamIO.

readFramedMessage :: StreamIO -> IO ByteString Source #

Read a 2-byte-BE-length-prefixed Noise frame from a StreamIO.

writeFramedMessage :: StreamIO -> ByteString -> IO () Source #

Write a 2-byte-BE-length-prefixed Noise frame to a StreamIO.