1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
use crate::error::PlainTextError;
use bytes::Bytes;
use futures::future::BoxFuture;
use futures::future::{self, Ready};
use futures::prelude::*;
use libp2p_core::{identity, InboundUpgrade, OutboundUpgrade, PeerId, PublicKey, UpgradeInfo};
use log::debug;
use std::{
io, iter,
pin::Pin,
task::{Context, Poll},
};
use void::Void;
mod error;
mod handshake;
#[allow(clippy::derive_partial_eq_without_eq)]
mod structs_proto {
include!(concat!(env!("OUT_DIR"), "/structs.rs"));
}
#[derive(Debug, Copy, Clone)]
pub struct PlainText1Config;
impl UpgradeInfo for PlainText1Config {
type Info = &'static [u8];
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {
iter::once(b"/plaintext/1.0.0")
}
}
impl<C> InboundUpgrade<C> for PlainText1Config {
type Output = C;
type Error = Void;
type Future = Ready<Result<C, Self::Error>>;
fn upgrade_inbound(self, i: C, _: Self::Info) -> Self::Future {
future::ready(Ok(i))
}
}
impl<C> OutboundUpgrade<C> for PlainText1Config {
type Output = C;
type Error = Void;
type Future = Ready<Result<C, Self::Error>>;
fn upgrade_outbound(self, i: C, _: Self::Info) -> Self::Future {
future::ready(Ok(i))
}
}
#[derive(Clone)]
pub struct PlainText2Config {
pub local_public_key: identity::PublicKey,
}
impl UpgradeInfo for PlainText2Config {
type Info = &'static [u8];
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {
iter::once(b"/plaintext/2.0.0")
}
}
impl<C> InboundUpgrade<C> for PlainText2Config
where
C: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type Output = (PeerId, PlainTextOutput<C>);
type Error = PlainTextError;
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future {
Box::pin(self.handshake(socket))
}
}
impl<C> OutboundUpgrade<C> for PlainText2Config
where
C: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
type Output = (PeerId, PlainTextOutput<C>);
type Error = PlainTextError;
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
Box::pin(self.handshake(socket))
}
}
impl PlainText2Config {
async fn handshake<T>(self, socket: T) -> Result<(PeerId, PlainTextOutput<T>), PlainTextError>
where
T: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
debug!("Starting plaintext handshake.");
let (socket, remote, read_buffer) = handshake::handshake(socket, self).await?;
debug!("Finished plaintext handshake.");
Ok((
remote.peer_id,
PlainTextOutput {
socket,
remote_key: remote.public_key,
read_buffer,
},
))
}
}
pub struct PlainTextOutput<S>
where
S: AsyncRead + AsyncWrite + Unpin,
{
pub socket: S,
pub remote_key: PublicKey,
read_buffer: Bytes,
}
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for PlainTextOutput<S> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, io::Error>> {
if !self.read_buffer.is_empty() {
let n = std::cmp::min(buf.len(), self.read_buffer.len());
let b = self.read_buffer.split_to(n);
buf[..n].copy_from_slice(&b[..]);
return Poll::Ready(Ok(n));
}
AsyncRead::poll_read(Pin::new(&mut self.socket), cx, buf)
}
}
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for PlainTextOutput<S> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
AsyncWrite::poll_write(Pin::new(&mut self.socket), cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
AsyncWrite::poll_flush(Pin::new(&mut self.socket), cx)
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
AsyncWrite::poll_close(Pin::new(&mut self.socket), cx)
}
}