Libp2p.MultistreamSelect (libp2p_elixir v0.9.0)

Multistream-select 1.0 negotiation helpers.

Used to negotiate:

  • connection-level protocols (security, muxer) and
  • stream-level protocols (app protocols, e.g. eth2 req/resp).

Summary

Functions

Decode a single message. Returns {msg, rest} or :more.

Encode a multistream-select message (uvarint length prefix + utf8 + \n).

Feed inbound bytes. Returns {events, out_bytes, st2}.

Produce bytes that should be written immediately when starting negotiation.

Types

role()

@type role() :: :initiator | :responder

state()

@type state() :: %{
  role: role(),
  buf: binary(),
  proposals: [binary()],
  sent_first_proposal?: boolean(),
  eager_propose?: boolean(),
  selected: nil | binary(),
  sent_mss?: boolean(),
  got_mss?: boolean()
}

Functions

decode_message(bin)

@spec decode_message(binary()) :: {binary(), binary()} | :more

Decode a single message. Returns {msg, rest} or :more.

Returned msg excludes the trailing \n.

encode_message(msg)

@spec encode_message(binary()) :: binary()

Encode a multistream-select message (uvarint length prefix + utf8 + \n).

feed(st, bytes, supported_protocols)

@spec feed(state(), binary(), MapSet.t(binary())) :: {[term()], binary(), state()}

Feed inbound bytes. Returns {events, out_bytes, st2}.

Events:

  • {:selected, protocol_id} when negotiation completes successfully.
  • {:error, reason} for protocol violations.

multistream_id()

@spec multistream_id() :: binary()

new_initiator(proposals)

@spec new_initiator([binary()]) :: state()

new_initiator(proposals, opts)

@spec new_initiator(
  [binary()],
  keyword()
) :: state()

new_responder()

@spec new_responder() :: state()

start(st)

@spec start(state()) :: {binary(), state()}

Produce bytes that should be written immediately when starting negotiation.