Understanding how the multistream protocol is to be implemented

Hi, I have been going over the multistream protocol spec on github for about 2 days, but I don’t think I completely understand how it is inteded to be implemented :slight_smile:

Does a dialer connect to an IP and then send a protocol over TCP? Or is it something else? I understood that the dialer and listener agree on the protocol or the listener can reply with na if the listener does not support the protocol asked for by the dialer.

I’m trying to implement the same in python, but I am a bit stuck because I don’t fuly understand how the spec works and would immensely appreciate it if someone could help me out.


So, multistream expects a stream to already exist. Connecting to a remote node usually goes like this:

  1. Start with a peer ID QmPeerID.
  2. Lookup the QmPeerID in the DHT to resolve it to a set of addresses. For example, /ip4/ (it might also be /ip4/ if the peer is using the QUIC transport).
  3. Connect to /ip4/ (using a TCP socket).
  4. On this TCP connection, use multistream to negotiate the security transport.
  5. On this secured connection, use multistream to negotiate a stream multiplexer.
  6. Using the stream multiplexer, open a new stream (or multiple).
  7. On each new stream, again use multistream to negotiate the protocol you’d like to speak (e.g., bitswap, dht, ping, etc.).

As for the actual protocol, it’s absurdly simple (a bit too simple, actually; we’re working on improving it):

  1. Both the initiator and the receiver send <length>/multistream/1.0.0\n where <length> is the base-128 varint encoded length of /multistream/1.0.0\n.
  2. The initiator sends <length>/some/protocol/name\n.
  3. The receiver either echos the same message back (to indicate success) or responds with <length>na\n.