File size: 9,839 Bytes
f0f4f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// change to quick-protobuf-codec

use std::io;
use std::io::ErrorKind;

use asynchronous_codec::{Framed, FramedRead, FramedWrite};

use futures::{AsyncRead, AsyncWrite, SinkExt, StreamExt};
use libp2p_core::Multiaddr;

use quick_protobuf_codec::Codec;
use rand::Rng;

use crate::v2::{generated::structs as proto, Nonce};

const REQUEST_MAX_SIZE: usize = 4104;
pub(super) const DATA_LEN_LOWER_BOUND: usize = 30_000u32 as usize;
pub(super) const DATA_LEN_UPPER_BOUND: usize = 100_000u32 as usize;
pub(super) const DATA_FIELD_LEN_UPPER_BOUND: usize = 4096;

fn new_io_invalid_data_err(msg: impl Into<String>) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, msg.into())
}

pub(crate) struct Coder<I> {
    inner: Framed<I, Codec<proto::Message>>,
}

impl<I> Coder<I>
where
    I: AsyncWrite + AsyncRead + Unpin,
{
    pub(crate) fn new(io: I) -> Self {
        Self {
            inner: Framed::new(io, Codec::new(REQUEST_MAX_SIZE)),
        }
    }
    pub(crate) async fn close(mut self) -> io::Result<()> {
        self.inner.close().await?;
        Ok(())
    }
}

impl<I> Coder<I>
where
    I: AsyncRead + Unpin,
{
    pub(crate) async fn next<M, E>(&mut self) -> io::Result<M>
    where
        proto::Message: TryInto<M, Error = E>,
        io::Error: From<E>,
    {
        Ok(self.next_msg().await?.try_into()?)
    }

    async fn next_msg(&mut self) -> io::Result<proto::Message> {
        self.inner
            .next()
            .await
            .ok_or(io::Error::new(
                ErrorKind::UnexpectedEof,
                "no request to read",
            ))?
            .map_err(|e| io::Error::new(ErrorKind::InvalidData, e))
    }
}

impl<I> Coder<I>
where
    I: AsyncWrite + Unpin,
{
    pub(crate) async fn send<M>(&mut self, msg: M) -> io::Result<()>
    where
        M: Into<proto::Message>,
    {
        self.inner.send(msg.into()).await?;
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) enum Request {
    Dial(DialRequest),
    Data(DialDataResponse),
}

impl From<DialRequest> for proto::Message {
    fn from(val: DialRequest) -> Self {
        let addrs = val.addrs.iter().map(|e| e.to_vec()).collect();
        let nonce = val.nonce;

        proto::Message {
            msg: proto::mod_Message::OneOfmsg::dialRequest(proto::DialRequest { addrs, nonce }),
        }
    }
}

impl From<DialDataResponse> for proto::Message {
    fn from(val: DialDataResponse) -> Self {
        debug_assert!(
            val.data_count <= DATA_FIELD_LEN_UPPER_BOUND,
            "data_count too large"
        );
        proto::Message {
            msg: proto::mod_Message::OneOfmsg::dialDataResponse(proto::DialDataResponse {
                data: vec![0; val.data_count], // One could use Cow::Borrowed here, but it will require a modification of the generated code and that will fail the CI
            }),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct DialRequest {
    pub(crate) addrs: Vec<Multiaddr>,
    pub(crate) nonce: u64,
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct DialDataResponse {
    data_count: usize,
}

impl DialDataResponse {
    pub(crate) fn new(data_count: usize) -> Option<Self> {
        if data_count <= DATA_FIELD_LEN_UPPER_BOUND {
            Some(Self { data_count })
        } else {
            None
        }
    }

    pub(crate) fn get_data_count(&self) -> usize {
        self.data_count
    }
}

impl TryFrom<proto::Message> for Request {
    type Error = io::Error;

    fn try_from(msg: proto::Message) -> Result<Self, Self::Error> {
        match msg.msg {
            proto::mod_Message::OneOfmsg::dialRequest(proto::DialRequest { addrs, nonce }) => {
                let addrs = addrs
                    .into_iter()
                    .map(|e| e.to_vec())
                    .map(|e| {
                        Multiaddr::try_from(e).map_err(|err| {
                            new_io_invalid_data_err(format!("invalid multiaddr: {}", err))
                        })
                    })
                    .collect::<Result<Vec<_>, io::Error>>()?;
                Ok(Self::Dial(DialRequest { addrs, nonce }))
            }
            proto::mod_Message::OneOfmsg::dialDataResponse(proto::DialDataResponse { data }) => {
                let data_count = data.len();
                Ok(Self::Data(DialDataResponse { data_count }))
            }
            _ => Err(new_io_invalid_data_err(
                "expected dialResponse or dialDataRequest",
            )),
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) enum Response {
    Dial(DialResponse),
    Data(DialDataRequest),
}

#[derive(Debug, Clone)]
pub(crate) struct DialDataRequest {
    pub(crate) addr_idx: usize,
    pub(crate) num_bytes: usize,
}

#[derive(Debug, Clone)]
pub(crate) struct DialResponse {
    pub(crate) status: proto::mod_DialResponse::ResponseStatus,
    pub(crate) addr_idx: usize,
    pub(crate) dial_status: proto::DialStatus,
}

impl TryFrom<proto::Message> for Response {
    type Error = io::Error;

    fn try_from(msg: proto::Message) -> Result<Self, Self::Error> {
        match msg.msg {
            proto::mod_Message::OneOfmsg::dialResponse(proto::DialResponse {
                status,
                addrIdx,
                dialStatus,
            }) => Ok(Response::Dial(DialResponse {
                status,
                addr_idx: addrIdx as usize,
                dial_status: dialStatus,
            })),
            proto::mod_Message::OneOfmsg::dialDataRequest(proto::DialDataRequest {
                addrIdx,
                numBytes,
            }) => Ok(Self::Data(DialDataRequest {
                addr_idx: addrIdx as usize,
                num_bytes: numBytes as usize,
            })),
            _ => Err(new_io_invalid_data_err(
                "invalid message type, expected dialResponse or dialDataRequest",
            )),
        }
    }
}

impl From<Response> for proto::Message {
    fn from(val: Response) -> Self {
        match val {
            Response::Dial(DialResponse {
                status,
                addr_idx,
                dial_status,
            }) => proto::Message {
                msg: proto::mod_Message::OneOfmsg::dialResponse(proto::DialResponse {
                    status,
                    addrIdx: addr_idx as u32,
                    dialStatus: dial_status,
                }),
            },
            Response::Data(DialDataRequest {
                addr_idx,
                num_bytes,
            }) => proto::Message {
                msg: proto::mod_Message::OneOfmsg::dialDataRequest(proto::DialDataRequest {
                    addrIdx: addr_idx as u32,
                    numBytes: num_bytes as u64,
                }),
            },
        }
    }
}

impl DialDataRequest {
    pub(crate) fn from_rng<R: rand_core::RngCore>(addr_idx: usize, mut rng: R) -> Self {
        let num_bytes = rng.gen_range(DATA_LEN_LOWER_BOUND..=DATA_LEN_UPPER_BOUND);
        Self {
            addr_idx,
            num_bytes,
        }
    }
}

const DIAL_BACK_MAX_SIZE: usize = 10;

pub(crate) async fn dial_back(stream: impl AsyncWrite + Unpin, nonce: Nonce) -> io::Result<()> {
    let msg = proto::DialBack { nonce };
    let mut framed = FramedWrite::new(stream, Codec::<proto::DialBack>::new(DIAL_BACK_MAX_SIZE));

    framed
        .send(msg)
        .await
        .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;

    Ok(())
}

pub(crate) async fn recv_dial_back(stream: impl AsyncRead + Unpin) -> io::Result<Nonce> {
    let framed = &mut FramedRead::new(stream, Codec::<proto::DialBack>::new(DIAL_BACK_MAX_SIZE));
    let proto::DialBack { nonce } = framed
        .next()
        .await
        .ok_or(io::Error::from(io::ErrorKind::UnexpectedEof))??;
    Ok(nonce)
}

pub(crate) async fn dial_back_response(stream: impl AsyncWrite + Unpin) -> io::Result<()> {
    let msg = proto::DialBackResponse {
        status: proto::mod_DialBackResponse::DialBackStatus::OK,
    };
    let mut framed = FramedWrite::new(
        stream,
        Codec::<proto::DialBackResponse>::new(DIAL_BACK_MAX_SIZE),
    );
    framed
        .send(msg)
        .await
        .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;

    Ok(())
}

pub(crate) async fn recv_dial_back_response(
    stream: impl AsyncRead + AsyncWrite + Unpin,
) -> io::Result<()> {
    let framed = &mut FramedRead::new(
        stream,
        Codec::<proto::DialBackResponse>::new(DIAL_BACK_MAX_SIZE),
    );
    let proto::DialBackResponse { status } = framed
        .next()
        .await
        .ok_or(io::Error::from(io::ErrorKind::UnexpectedEof))??;

    if proto::mod_DialBackResponse::DialBackStatus::OK == status {
        Ok(())
    } else {
        Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid dial back response",
        ))
    }
}

#[cfg(test)]
mod tests {
    use crate::v2::generated::structs::{
        mod_Message::OneOfmsg, DialDataResponse as GenDialDataResponse, Message,
    };

    #[test]
    fn message_correct_max_size() {
        let message_bytes = quick_protobuf::serialize_into_vec(&Message {
            msg: OneOfmsg::dialDataResponse(GenDialDataResponse {
                data: vec![0; 4096],
            }),
        })
        .unwrap();
        assert_eq!(message_bytes.len(), super::REQUEST_MAX_SIZE);
    }

    #[test]
    fn dial_back_correct_size() {
        let dial_back = super::proto::DialBack { nonce: 0 };
        let buf = quick_protobuf::serialize_into_vec(&dial_back).unwrap();
        assert!(buf.len() <= super::DIAL_BACK_MAX_SIZE);

        let dial_back_max_nonce = super::proto::DialBack { nonce: u64::MAX };
        let buf = quick_protobuf::serialize_into_vec(&dial_back_max_nonce).unwrap();
        assert!(buf.len() <= super::DIAL_BACK_MAX_SIZE);
    }
}