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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
// Copyright 2020 Netwarps 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. use std::io; use std::io::ErrorKind; use async_trait::async_trait; use futures::prelude::*; use futures::{AsyncReadExt, AsyncWriteExt}; /// Read Trait for async/await /// #[async_trait] pub trait ReadEx: Send { /// Reads some bytes from the byte stream. /// /// On success, returns the total number of bytes read. /// /// If the return value is `Ok(n)`, then it must be guaranteed that /// `0 <= n <= buf.len()`. A nonzero `n` value indicates that the buffer has been /// filled with `n` bytes of data. If `n` is `0`, then it can indicate one of two /// scenarios: /// /// 1. This reader has reached its "end of file" and will likely no longer be able to /// produce bytes. Note that this does not mean that the reader will always no /// longer be able to produce bytes. /// 2. The buffer specified was 0 bytes in length. /// /// Attempt to read bytes from underlying stream object. /// /// On success, returns `Ok(n)`. /// Otherwise, returns `Err(io:Error)` async fn read2(&mut self, buf: &mut [u8]) -> Result<usize, io::Error>; /// Reads the exact number of bytes requested. /// /// On success, returns `Ok(())`. /// Otherwise, returns `Err(io:Error)`. async fn read_exact2<'a>(&'a mut self, buf: &'a mut [u8]) -> Result<(), io::Error> { let mut buf_piece = buf; while !buf_piece.is_empty() { let n = self.read2(buf_piece).await?; if n == 0 { return Err(ErrorKind::UnexpectedEof.into()); } let (_, rest) = buf_piece.split_at_mut(n); buf_piece = rest; } Ok(()) } /// Reads a fixed-length integer from the underlying IO. /// /// On success, return `Ok(n)`. /// Otherwise, returns `Err(io:Error)`. async fn read_fixed_u32(&mut self) -> Result<usize, io::Error> { let mut len = [0; 4]; self.read_exact2(&mut len).await?; let n = u32::from_be_bytes(len) as usize; Ok(n) } /// Reads a variable-length integer from the underlying IO. /// /// As a special exception, if the `IO` is empty and EOFs right at the beginning, then we /// return `Ok(0)`. /// /// On success, return `Ok(n)`. /// Otherwise, returns `Err(io:Error)`. /// /// > **Note**: This function reads bytes one by one from the underlying IO. It is therefore encouraged /// > to use some sort of buffering mechanism. async fn read_varint(&mut self) -> Result<usize, io::Error> { let mut buffer = unsigned_varint::encode::usize_buffer(); let mut buffer_len = 0; loop { match self.read2(&mut buffer[buffer_len..=buffer_len]).await? { 0 => { // Reaching EOF before finishing to read the length is an error, unless the EOF is // at the very beginning of the substream, in which case we assume that the data is // empty. if buffer_len == 0 { return Ok(0); } else { return Err(io::ErrorKind::UnexpectedEof.into()); } } n => debug_assert_eq!(n, 1), } buffer_len += 1; match unsigned_varint::decode::usize(&buffer[..buffer_len]) { Ok((len, _)) => return Ok(len), Err(unsigned_varint::decode::Error::Overflow) => { return Err(io::Error::new(io::ErrorKind::InvalidData, "overflow in variable-length integer")); } // TODO: why do we have a `__Nonexhaustive` variant in the error? I don't know how to process it // Err(unsigned_varint::decode::Error::Insufficient) => {} Err(_) => {} } } } /// Reads a fixed length-prefixed message from the underlying IO. /// /// The `max_size` parameter is the maximum size in bytes of the message that we accept. This is /// necessary in order to avoid DoS attacks where the remote sends us a message of several /// gigabytes. /// /// > **Note**: Assumes that a fixed-length prefix indicates the length of the message. This is /// > compatible with what `write_one_fixed` does. async fn read_one_fixed(&mut self, max_size: usize) -> Result<Vec<u8>, io::Error> { let len = self.read_fixed_u32().await?; if len > max_size { return Err(io::Error::new( io::ErrorKind::InvalidData, format!("Received data size over maximum frame length: {}>{}", len, max_size), )); } let mut buf = vec![0; len]; self.read_exact2(&mut buf).await?; Ok(buf) } /// Reads a variable length-prefixed message from the underlying IO. /// /// The `max_size` parameter is the maximum size in bytes of the message that we accept. This is /// necessary in order to avoid DoS attacks where the remote sends us a message of several /// gigabytes. /// /// On success, returns `Ok(Vec<u8>)`. /// Otherwise, returns `Err(io:Error)`. /// /// > **Note**: Assumes that a variable-length prefix indicates the length of the message. This is /// > compatible with what `write_one` does. async fn read_one(&mut self, max_size: usize) -> Result<Vec<u8>, io::Error> { let len = self.read_varint().await?; if len > max_size { return Err(io::Error::new( io::ErrorKind::InvalidData, format!("Received data size over maximum frame length: {}>{}", len, max_size), )); } let mut buf = vec![0; len]; self.read_exact2(&mut buf).await?; Ok(buf) } } /// Write Trait for async/await /// #[async_trait] pub trait WriteEx: Send { /// Attempt to write bytes from `buf` into the object. /// /// On success, returns `Ok(num_bytes_written)`. /// Otherwise, returns `Err(io:Error)` async fn write2(&mut self, buf: &[u8]) -> Result<usize, io::Error>; /// Attempt to write the entire contents of data into object. /// /// The operation will not complete until all the data has been written. /// /// On success, returns `Ok(())`. /// Otherwise, returns `Err(io:Error)` async fn write_all2(&mut self, buf: &[u8]) -> Result<(), io::Error> { let mut buf_piece = buf; while !buf_piece.is_empty() { let n = self.write2(buf_piece).await?; if n == 0 { return Err(io::ErrorKind::WriteZero.into()); } let (_, rest) = buf_piece.split_at(n); buf_piece = rest; } Ok(()) } /// Writes a variable-length integer to the underlying IO. /// /// On success, returns `Ok(())`. /// Otherwise, returns `Err(io:Error)` /// /// > **Note**: Does **NOT** flush the IO. async fn write_varint(&mut self, len: usize) -> Result<(), io::Error> { let mut len_data = unsigned_varint::encode::usize_buffer(); let encoded_len = unsigned_varint::encode::usize(len, &mut len_data).len(); self.write_all2(&len_data[..encoded_len]).await?; Ok(()) } /// Writes a fixed-length integer to the underlying IO. /// /// On success, returns `Ok(())`. /// Otherwise, returns `Err(io:Error)` /// /// > **Note**: Does **NOT** flush the IO. async fn write_fixed_u32(&mut self, len: usize) -> Result<(), io::Error> { self.write_all2((len as u32).to_be_bytes().as_ref()).await?; Ok(()) } /// Send a fixed length message to the underlying IO, then flushes the writing side. /// /// > **Note**: Prepends a fixed-length prefix indicate the length of the message. This is /// > compatible with what `read_one_fixed` expects. async fn write_one_fixed(&mut self, buf: &[u8]) -> Result<(), io::Error> { self.write_fixed_u32(buf.len()).await?; self.write_all2(buf).await?; self.flush2().await?; Ok(()) } /// Send a variable length message to the underlying IO, then flushes the writing side. /// /// On success, returns `Ok(())`. /// Otherwise, returns `Err(io:Error)` /// /// > **Note**: Prepends a variable-length prefix indicate the length of the message. This is /// > compatible with what `read_one` expects. async fn write_one(&mut self, buf: &[u8]) -> Result<(), io::Error> { self.write_varint(buf.len()).await?; self.write_all2(buf).await?; self.flush2().await?; Ok(()) } /// Attempt to flush the object, ensuring that any buffered data reach /// their destination. /// /// On success, returns `Ok(())`. /// Otherwise, returns `Err(io:Error)` async fn flush2(&mut self) -> Result<(), io::Error>; /// Attempt to close the object. /// /// On success, returns `Ok(())`. /// Otherwise, returns `Err(io:Error)` async fn close2(&mut self) -> Result<(), io::Error>; } #[async_trait] impl<T: AsyncRead + Unpin + Send> ReadEx for T { async fn read2(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { let n = AsyncReadExt::read(self, buf).await?; Ok(n) } } #[async_trait] impl<T: AsyncWrite + Unpin + Send> WriteEx for T { async fn write2(&mut self, buf: &[u8]) -> Result<usize, io::Error> { AsyncWriteExt::write(self, buf).await } async fn flush2(&mut self) -> Result<(), io::Error> { AsyncWriteExt::flush(self).await } async fn close2(&mut self) -> Result<(), io::Error> { AsyncWriteExt::close(self).await } } #[cfg(test)] mod tests { use super::*; use futures::io::{self, AsyncReadExt, Cursor}; use libp2prs_runtime::task; struct Test(Cursor<Vec<u8>>); #[async_trait] impl ReadEx for Test { async fn read2(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { self.0.read(buf).await } } #[async_trait] impl WriteEx for Test { async fn write2(&mut self, buf: &[u8]) -> Result<usize, io::Error> { self.0.write(buf).await } async fn flush2(&mut self) -> Result<(), io::Error> { self.0.flush().await } async fn close2(&mut self) -> Result<(), io::Error> { self.0.close().await } } /// Read Vec<u8> #[test] fn test_read() { task::block_on(async { let mut reader = Test(Cursor::new(vec![1, 2, 3, 4])); let mut output = [0u8; 3]; let bytes = reader.read2(&mut output[..]).await.unwrap(); assert_eq!(bytes, 3); assert_eq!(output, [1, 2, 3]); }); } // Read string #[test] fn test_read_string() { task::block_on(async { let mut reader = Test(Cursor::new(b"hello world".to_vec())); let mut output = [0u8; 3]; let bytes = reader.read2(&mut output[..]).await.unwrap(); assert_eq!(bytes, 3); assert_eq!(output, [104, 101, 108]); }); } #[test] fn test_read_exact() { task::block_on(async { let mut reader = Test(Cursor::new(vec![1, 2, 3, 4])); let mut output = [0u8; 3]; let _bytes = reader.read_exact2(&mut output[..]).await; assert_eq!(output, [1, 2, 3]); }); } #[test] fn test_read_fixed_u32() { task::block_on(async { let mut reader = Test(Cursor::new(b"hello world".to_vec())); let size = reader.read_fixed_u32().await.unwrap(); assert_eq!(size, 1751477356); }); } #[test] fn test_read_varint() { task::block_on(async { let mut reader = Test(Cursor::new(vec![1, 2, 3, 4, 5, 6])); let size = reader.read_varint().await.unwrap(); assert_eq!(size, 1); }); } #[test] fn test_read_one() { task::block_on(async { let mut reader = Test(Cursor::new(vec![11, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])); let output = match reader.read_one(11).await { Ok(v) => v, _ => Vec::new(), }; assert_eq!(output, b"hello world"); }); } #[test] fn test_write() { task::block_on(async { let mut writer = Test(Cursor::new(vec![0u8; 5])); let size = writer.write2(&[1, 2, 3, 4]).await.unwrap(); assert_eq!(size, 4); assert_eq!(writer.0.get_mut(), &[1, 2, 3, 4, 0]) }); } #[test] fn test_write_all2() { task::block_on(async { let mut writer = Test(Cursor::new(vec![0u8; 4])); let output = vec![1, 2, 3, 4, 5]; let _bytes = writer.write_all2(&output[..]).await.unwrap(); assert_eq!(writer.0.get_mut(), &[1, 2, 3, 4, 5]); }); } #[test] fn test_write_fixed_u32() { task::block_on(async { let mut writer = Test(Cursor::new(b"hello world".to_vec())); let _result = writer.write_fixed_u32(1751477356).await.unwrap(); // Binary value of `hell` is 17751477356, if write successfully, current // pointer ought to stay on 4 assert_eq!(writer.0.position(), 4); }); } #[test] fn test_write_varint() { task::block_on(async { let mut writer = Test(Cursor::new(vec![2, 2, 3, 4, 5, 6])); let _result = writer.write_varint(1).await.unwrap(); assert_eq!(writer.0.position(), 1); }); } #[test] fn test_write_one() { task::block_on(async { let mut writer = Test(Cursor::new(vec![0u8; 0])); let _result = writer.write_one("hello world".as_ref()).await; assert_eq!(writer.0.get_mut(), &[11, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]); }); } }