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
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:
- Start with a peer ID QmPeerID.
- Lookup the QmPeerID in the DHT to resolve it to a set of addresses. For example,
/ip4/22.214.171.124/tcp/1234 (it might also be
/ip4/126.96.36.199/udp/1234/quic if the peer is using the QUIC transport).
- Connect to
/ip4/188.8.131.52/tcp/1234 (using a TCP socket).
- On this TCP connection, use multistream to negotiate the security transport.
- On this secured connection, use multistream to negotiate a stream multiplexer.
- Using the stream multiplexer, open a new stream (or multiple).
- 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):
- Both the initiator and the receiver send
<length> is the base-128 varint encoded length of
- The initiator sends
- The receiver either echos the same message back (to indicate success) or responds with