// Copyright 2018 Parity Technologies (UK) Ltd. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. //! Implements the Yamux multiplexing protocol for libp2p, see also the //! [specification](https://github.com/hashicorp/yamux/blob/master/spec.md). use futures::{future, prelude::*, ready, stream::{BoxStream, LocalBoxStream}}; use libp2p_core::muxing::StreamMuxerEvent; use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}; use parking_lot::Mutex; use std::{fmt, io, iter, ops::{Deref, DerefMut}, pin::Pin, task::Context}; use thiserror::Error; pub use yamux::WindowUpdateMode; /// A Yamux connection. /// /// This implementation isn't capable of detecting when the underlying socket changes its address, /// and no [`StreamMuxerEvent::AddressChange`] event is ever emitted. pub struct Yamux(Mutex>); impl fmt::Debug for Yamux { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("Yamux") } } struct Inner { /// The [`futures::stream::Stream`] of incoming substreams. incoming: S, /// Handle to control the connection. control: yamux::Control, } /// A token to poll for an outbound substream. #[derive(Debug)] pub struct OpenSubstreamToken(()); impl Yamux> where C: AsyncRead + AsyncWrite + Send + Unpin + 'static { /// Create a new Yamux connection. pub fn new(io: C, mut cfg: yamux::Config, mode: yamux::Mode) -> Self { cfg.set_read_after_close(false); let conn = yamux::Connection::new(io, cfg, mode); let ctrl = conn.control(); let inner = Inner { incoming: Incoming { stream: yamux::into_stream(conn).err_into().boxed(), _marker: std::marker::PhantomData }, control: ctrl, }; Yamux(Mutex::new(inner)) } } impl Yamux> where C: AsyncRead + AsyncWrite + Unpin + 'static { /// Create a new Yamux connection (which is ![`Send`]). pub fn local(io: C, mut cfg: yamux::Config, mode: yamux::Mode) -> Self { cfg.set_read_after_close(false); let conn = yamux::Connection::new(io, cfg, mode); let ctrl = conn.control(); let inner = Inner { incoming: LocalIncoming { stream: yamux::into_stream(conn).err_into().boxed_local(), _marker: std::marker::PhantomData }, control: ctrl, }; Yamux(Mutex::new(inner)) } } type Poll = std::task::Poll>; impl libp2p_core::StreamMuxer for Yamux where S: Stream> + Unpin { type Substream = yamux::Stream; type OutboundSubstream = OpenSubstreamToken; type Error = YamuxError; fn poll_event(&self, c: &mut Context<'_>) -> Poll> { let mut inner = self.0.lock(); match ready!(inner.incoming.poll_next_unpin(c)) { Some(Ok(s)) => Poll::Ready(Ok(StreamMuxerEvent::InboundSubstream(s))), Some(Err(e)) => Poll::Ready(Err(e)), None => Poll::Ready(Err(yamux::ConnectionError::Closed.into())) } } fn open_outbound(&self) -> Self::OutboundSubstream { OpenSubstreamToken(()) } fn poll_outbound(&self, c: &mut Context<'_>, _: &mut OpenSubstreamToken) -> Poll { let mut inner = self.0.lock(); Pin::new(&mut inner.control).poll_open_stream(c).map_err(YamuxError) } fn destroy_outbound(&self, _: Self::OutboundSubstream) { self.0.lock().control.abort_open_stream() } fn read_substream(&self, c: &mut Context<'_>, s: &mut Self::Substream, b: &mut [u8]) -> Poll { Pin::new(s).poll_read(c, b).map_err(|e| YamuxError(e.into())) } fn write_substream(&self, c: &mut Context<'_>, s: &mut Self::Substream, b: &[u8]) -> Poll { Pin::new(s).poll_write(c, b).map_err(|e| YamuxError(e.into())) } fn flush_substream(&self, c: &mut Context<'_>, s: &mut Self::Substream) -> Poll<()> { Pin::new(s).poll_flush(c).map_err(|e| YamuxError(e.into())) } fn shutdown_substream(&self, c: &mut Context<'_>, s: &mut Self::Substream) -> Poll<()> { Pin::new(s).poll_close(c).map_err(|e| YamuxError(e.into())) } fn destroy_substream(&self, _: Self::Substream) { } fn close(&self, c: &mut Context<'_>) -> Poll<()> { let mut inner = self.0.lock(); if let std::task::Poll::Ready(x) = Pin::new(&mut inner.control).poll_close(c) { return Poll::Ready(x.map_err(YamuxError)) } while let std::task::Poll::Ready(x) = inner.incoming.poll_next_unpin(c) { match x { Some(Ok(_)) => {} // drop inbound stream Some(Err(e)) => return Poll::Ready(Err(e)), None => return Poll::Ready(Ok(())) } } Poll::Pending } fn flush_all(&self, _: &mut Context<'_>) -> Poll<()> { Poll::Ready(Ok(())) } } /// The yamux configuration. #[derive(Clone)] pub struct Config(yamux::Config); /// The yamux configuration for upgrading I/O resources which are ![`Send`]. #[derive(Clone)] pub struct LocalConfig(Config); impl Config { pub fn new(cfg: yamux::Config) -> Self { Config(cfg) } /// Turn this into a [`LocalConfig`] for use with upgrades of ![`Send`] resources. pub fn local(self) -> LocalConfig { LocalConfig(self) } } impl Default for Config { fn default() -> Self { Config(yamux::Config::default()) } } impl Deref for Config { type Target = yamux::Config; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for Config { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl UpgradeInfo for Config { type Info = &'static [u8]; type InfoIter = iter::Once; fn protocol_info(&self) -> Self::InfoIter { iter::once(b"/yamux/1.0.0") } } impl UpgradeInfo for LocalConfig { type Info = &'static [u8]; type InfoIter = iter::Once; fn protocol_info(&self) -> Self::InfoIter { iter::once(b"/yamux/1.0.0") } } impl InboundUpgrade for Config where C: AsyncRead + AsyncWrite + Send + Unpin + 'static { type Output = Yamux>; type Error = io::Error; type Future = future::Ready>; fn upgrade_inbound(self, io: C, _: Self::Info) -> Self::Future { future::ready(Ok(Yamux::new(io, self.0, yamux::Mode::Server))) } } impl InboundUpgrade for LocalConfig where C: AsyncRead + AsyncWrite + Unpin + 'static { type Output = Yamux>; type Error = io::Error; type Future = future::Ready>; fn upgrade_inbound(self, io: C, _: Self::Info) -> Self::Future { future::ready(Ok(Yamux::local(io, (self.0).0, yamux::Mode::Server))) } } impl OutboundUpgrade for Config where C: AsyncRead + AsyncWrite + Send + Unpin + 'static { type Output = Yamux>; type Error = io::Error; type Future = future::Ready>; fn upgrade_outbound(self, io: C, _: Self::Info) -> Self::Future { future::ready(Ok(Yamux::new(io, self.0, yamux::Mode::Client))) } } impl OutboundUpgrade for LocalConfig where C: AsyncRead + AsyncWrite + Unpin + 'static { type Output = Yamux>; type Error = io::Error; type Future = future::Ready>; fn upgrade_outbound(self, io: C, _: Self::Info) -> Self::Future { future::ready(Ok(Yamux::local(io, (self.0).0, yamux::Mode::Client))) } } /// The Yamux [`libp2p_core::StreamMuxer`] error type. #[derive(Debug, Error)] #[error("yamux error: {0}")] pub struct YamuxError(#[from] pub yamux::ConnectionError); impl Into for YamuxError { fn into(self: YamuxError) -> io::Error { io::Error::new(io::ErrorKind::Other, self.to_string()) } } /// The [`futures::stream::Stream`] of incoming substreams. pub struct Incoming { stream: BoxStream<'static, Result>, _marker: std::marker::PhantomData } impl fmt::Debug for Incoming { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("Incoming") } } /// The [`futures::stream::Stream`] of incoming substreams (`!Send`). pub struct LocalIncoming { stream: LocalBoxStream<'static, Result>, _marker: std::marker::PhantomData } impl fmt::Debug for LocalIncoming { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("LocalIncoming") } } impl Stream for Incoming { type Item = Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> std::task::Poll> { self.stream.as_mut().poll_next_unpin(cx) } fn size_hint(&self) -> (usize, Option) { self.stream.size_hint() } } impl Unpin for Incoming { } impl Stream for LocalIncoming { type Item = Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> std::task::Poll> { self.stream.as_mut().poll_next_unpin(cx) } fn size_hint(&self) -> (usize, Option) { self.stream.size_hint() } } impl Unpin for LocalIncoming { }