| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
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 or TLS 1.3) 2. secure handshake: encrypted channel + remote PeerId 3. negotiate Yamux via multistream-select or TLS ALPN 4. Yamux session init: multiplexed streams
Implements the connection upgrading pipeline.
Synopsis
- performStreamHandshake :: KeyPair -> Direction -> StreamIO -> IO (NoiseSession, HandshakeResult)
- performStreamHandshakeWithPrologue :: KeyPair -> Direction -> ByteString -> StreamIO -> IO (NoiseSession, HandshakeResult)
- noiseSessionToStreamIO :: IORef NoiseSession -> IORef NoiseSession -> IORef ByteString -> StreamIO -> StreamIO
- noiseSessionToManagedStreamIO :: MemoryAccount -> IORef NoiseSession -> IORef NoiseSession -> IORef ByteString -> StreamIO -> StreamIO
- yamuxToMuxerSession :: YamuxSession -> IO () -> IO MuxerSession
- upgradeAs :: ResourceManager -> Direction -> KeyPair -> RawConnection -> IO Connection
- upgradeAsWithConfig :: YamuxConfig -> ResourceManager -> Direction -> KeyPair -> RawConnection -> IO Connection
- upgradeWithSwitchConfig :: SwitchConfig -> ResourceManager -> Direction -> KeyPair -> RawConnection -> IO Connection
- upgradeAsWithSecurityProtocols :: ResourceManager -> [ProtocolId] -> Direction -> KeyPair -> RawConnection -> IO Connection
- upgradeOutbound :: ResourceManager -> KeyPair -> RawConnection -> IO Connection
- upgradeInbound :: ResourceManager -> KeyPair -> RawConnection -> IO Connection
- readExact :: StreamIO -> Int -> IO ByteString
- readFramedMessage :: StreamIO -> IO ByteString
- writeFramedMessage :: StreamIO -> ByteString -> IO ()
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.
performStreamHandshakeWithPrologue :: KeyPair -> Direction -> ByteString -> StreamIO -> IO (NoiseSession, HandshakeResult) Source #
Perform Noise XX with a transport-binding prologue.
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.
noiseSessionToManagedStreamIO :: MemoryAccount -> IORef NoiseSession -> IORef NoiseSession -> IORef ByteString -> StreamIO -> StreamIO Source #
Account decrypted bytes while they remain in the Noise read buffer.
Yamux → MuxerSession adapter
yamuxToMuxerSession :: YamuxSession -> IO () -> IO MuxerSession Source #
Wrap a YamuxSession as a MuxerSession. Starts sendLoop, recvLoop and keepaliveLoop as background threads. The MuxerSession provides open/accept stream operations that produce StreamIO-compatible streams.
The supplied close action closes the underlying transport; muxClose runs it after sending GoAway and stopping the session loops, so closing a connection actually releases the socket.
Full upgrade pipeline
upgradeAs :: ResourceManager -> Direction -> KeyPair -> RawConnection -> IO Connection Source #
Upgrade a raw connection, taking every role from the direction.
Byte streams negotiate Noise or TLS 1.3, then Yamux. TLS can inline Yamux negotiation through ALPN; Noise and legacy TLS peers use a second multistream-select exchange. The direction determines every protocol role.
upgradeAsWithConfig :: YamuxConfig -> ResourceManager -> Direction -> KeyPair -> RawConnection -> IO Connection Source #
Like upgradeAs, with an explicit Yamux configuration. A native
multiplexer (QUIC) does not run Yamux, so the configuration is unused there.
upgradeWithSwitchConfig :: SwitchConfig -> ResourceManager -> Direction -> KeyPair -> RawConnection -> IO Connection Source #
Upgrade with both settings carried by a SwitchConfig.
upgradeAsWithSecurityProtocols :: ResourceManager -> [ProtocolId] -> Direction -> KeyPair -> RawConnection -> IO Connection Source #
Upgrade with an explicit security preference list. Exported for conformance tests and embedders that need to require one secure transport.
upgradeOutbound :: ResourceManager -> KeyPair -> RawConnection -> IO Connection Source #
Upgrade an outbound (dialer) raw connection.
upgradeInbound :: ResourceManager -> KeyPair -> RawConnection -> IO Connection Source #
Upgrade an inbound (listener) raw connection.
Helpers (exported for testing)
readExact :: StreamIO -> Int -> IO ByteString Source #
Read exactly n bytes from a StreamIO.
Exception-style wrapper over readExactBounded for the Noise frame
reader and the yamux read callback, which expect failures as
exceptions. Defence in depth against remote memory exhaustion: the
request size is bounded by maxStreamWindowSize (callers must validate
lengths against flow control before reading).
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. Fails (instead of truncating the length prefix) if the message exceeds the 65535-byte Noise message cap.