libp2p_quic/connection/incoming.rs
1// Copyright 2017-2020 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
21use std::{
22 pin::Pin,
23 task::{Context, Poll},
24 time::Duration,
25};
26
27use futures::{
28 future::{Either, FutureExt, Select, select},
29 prelude::*,
30};
31use futures_timer::Delay;
32use libp2p_identity::PeerId;
33
34use super::{Connection, connecting::remote_peer_id};
35use crate::{ConnectionError, Error};
36
37/// Future that drives an inbound QUIC connection until its handshake completes.
38///
39/// The `Accepting` state exists solely to move the crypto-heavy first step of
40/// the handshake ClientHello processing, certificate and signature generation
41/// off the swarm task: rather than running it in `Listener::poll_next` while the
42/// listener produces the `TransportEvent::Incoming` (inside `Swarm::poll`), it
43/// runs on this future's first poll, which the connection pool drives on its own
44/// per-connection task.
45pub enum Incoming {
46 /// The connection has not been accepted yet, quinn has no connection object
47 /// until [`quinn::Incoming::accept`] runs.
48 Accepting {
49 incoming: Box<Option<quinn::Incoming>>,
50 timeout: Duration,
51 },
52 /// The connection exists; its handshake is under way.
53 Handshaking {
54 connecting: Select<quinn::Connecting, Delay>,
55 },
56}
57
58impl Incoming {
59 pub(crate) fn new(incoming: quinn::Incoming, timeout: Duration) -> Self {
60 Self::Accepting {
61 incoming: Box::new(Some(incoming)),
62 timeout,
63 }
64 }
65}
66
67impl Future for Incoming {
68 type Output = Result<(PeerId, Connection), Error>;
69
70 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
71 let this = self.get_mut();
72
73 loop {
74 match this {
75 Self::Accepting { incoming, timeout } => {
76 let accept = incoming
77 .take()
78 .ok_or(Error::InvalidState("inbound connection already accepted"))?;
79 let connection = accept.accept().map_err(ConnectionError)?;
80 *this = Self::Handshaking {
81 connecting: select(connection, Delay::new(*timeout)),
82 };
83 }
84
85 Self::Handshaking { connecting } => {
86 let connection = match futures::ready!(connecting.poll_unpin(cx)) {
87 Either::Right(_) => Err(Error::HandshakeTimedOut),
88 Either::Left((connection, _)) => {
89 connection.map_err(|e| Error::Connection(ConnectionError(e)))
90 }
91 }?;
92 let peer_id = remote_peer_id(&connection)?;
93 let muxer = Connection::new(connection);
94 return Poll::Ready(Ok((peer_id, muxer)));
95 }
96 }
97 }
98 }
99}