litep2p/multistream_select/mod.rs
1// Copyright 2017 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21#![allow(unused)]
22#![allow(clippy::derivable_impls)]
23
24//! # Multistream-select Protocol Negotiation
25//!
26//! This crate implements the `multistream-select` protocol, which is the protocol
27//! used by libp2p to negotiate which application-layer protocol to use with the
28//! remote on a connection or substream.
29//!
30//! > **Note**: This crate is used primarily by core components of *libp2p* and it
31//! > is usually not used directly on its own.
32//!
33//! ## Roles
34//!
35//! Two peers using the multistream-select negotiation protocol on an I/O stream
36//! are distinguished by their role as a _dialer_ (or _initiator_) or as a _listener_
37//! (or _responder_). Thereby the dialer plays the active part, driving the protocol,
38//! whereas the listener reacts to the messages received.
39//!
40//! The dialer has two options: it can either pick a protocol from the complete list
41//! of protocols that the listener supports, or it can directly suggest a protocol.
42//! Either way, a selected protocol is sent to the listener who can either accept (by
43//! echoing the same protocol) or reject (by responding with a message stating
44//! "not available"). If a suggested protocol is not available, the dialer may
45//! suggest another protocol. This process continues until a protocol is agreed upon,
46//! yielding a [`Negotiated`] stream, or the dialer has run out of
47//! alternatives.
48//!
49//! See [`dialer_select_proto`] and [`listener_select_proto`].
50//!
51//! ## [`Negotiated`]
52//!
53//! A `Negotiated` represents an I/O stream that has settled on a protocol
54//! to use. By default, with [`Version::V1`], protocol negotiation is always
55//! at least one dedicated round-trip message exchange, before application
56//! data for the negotiated protocol can be sent by the dialer. There is
57//! a variant [`Version::V1Lazy`] that permits 0-RTT negotiation if the
58//! dialer only supports a single protocol. In that case, when a dialer
59//! settles on a protocol to use, the [`DialerSelectFuture`] yields a
60//! [`Negotiated`] I/O stream before the negotiation
61//! data has been flushed. It is then expecting confirmation for that protocol
62//! as the first messages read from the stream. This behaviour allows the dialer
63//! to immediately send data relating to the negotiated protocol together with the
64//! remaining negotiation message(s). Note, however, that a dialer that performs
65//! multiple 0-RTT negotiations in sequence for different protocols layered on
66//! top of each other may trigger undesirable behaviour for a listener not
67//! supporting one of the intermediate protocols. See
68//! [`dialer_select_proto`] and the documentation of [`Version::V1Lazy`] for further details.
69
70#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
71
72mod dialer_select;
73mod length_delimited;
74mod listener_select;
75mod negotiated;
76mod protocol;
77
78pub use crate::multistream_select::{
79 dialer_select::{dialer_select_proto, DialerSelectFuture, DialerState, HandshakeResult},
80 listener_select::{
81 listener_negotiate, listener_select_proto, ListenerSelectFuture, ListenerSelectResult,
82 },
83 negotiated::{Negotiated, NegotiatedComplete, NegotiationError},
84 protocol::{HeaderLine, Message, Protocol, ProtocolError},
85};
86
87/// Supported multistream-select versions.
88#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89pub enum Version {
90 /// Version 1 of the multistream-select protocol. See [1] and [2].
91 ///
92 /// [1]: https://github.com/libp2p/specs/blob/master/connections/README.md#protocol-negotiation
93 /// [2]: https://github.com/multiformats/multistream-select
94 V1,
95 /// A "lazy" variant of version 1 that is identical on the wire but whereby
96 /// the dialer delays flushing protocol negotiation data in order to combine
97 /// it with initial application data, thus performing 0-RTT negotiation.
98 ///
99 /// This strategy is only applicable for the node with the role of "dialer"
100 /// in the negotiation and only if the dialer supports just a single
101 /// application protocol. In that case the dialer immedidately "settles"
102 /// on that protocol, buffering the negotiation messages to be sent
103 /// with the first round of application protocol data (or an attempt
104 /// is made to read from the `Negotiated` I/O stream).
105 ///
106 /// A listener will behave identically to `V1`. This ensures interoperability with `V1`.
107 /// Notably, it will immediately send the multistream header as well as the protocol
108 /// confirmation, resulting in multiple frames being sent on the underlying transport.
109 /// Nevertheless, if the listener supports the protocol that the dialer optimistically
110 /// settled on, it can be a 0-RTT negotiation.
111 ///
112 /// > **Note**: `V1Lazy` is specific to `rust-libp2p`. The wire protocol is identical to `V1`
113 /// > and generally interoperable with peers only supporting `V1`. Nevertheless, there is a
114 /// > pitfall that is rarely encountered: When nesting multiple protocol negotiations, the
115 /// > listener should either be known to support all of the dialer's optimistically chosen
116 /// > protocols or there is must be no intermediate protocol without a payload and none of
117 /// > the protocol payloads must have the potential for being mistaken for a multistream-select
118 /// > protocol message. This avoids rare edge-cases whereby the listener may not recognize
119 /// > upgrade boundaries and erroneously process a request despite not supporting one of
120 /// > the intermediate protocols that the dialer committed to. See [1] and [2].
121 ///
122 /// [1]: https://github.com/multiformats/go-multistream/issues/20
123 /// [2]: https://github.com/libp2p/rust-libp2p/pull/1212
124 V1Lazy,
125 // Draft: https://github.com/libp2p/specs/pull/95
126 // V2,
127}
128
129impl Default for Version {
130 fn default() -> Self {
131 Version::V1
132 }
133}