File size: 15,401 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 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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
// Copyright 2022 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.
use bytes::Bytes;
use std::io;
use crate::proto::Flag;
#[derive(Debug, Copy, Clone)]
pub(crate) enum State {
Open,
ReadClosed,
WriteClosed,
ClosingRead {
/// Whether the write side of our channel was already closed.
write_closed: bool,
inner: Closing,
},
ClosingWrite {
/// Whether the write side of our channel was already closed.
read_closed: bool,
inner: Closing,
},
BothClosed {
reset: bool,
},
}
/// Represents the state of closing one half (either read or write) of the connection.
///
/// Gracefully closing the read or write requires sending the `STOP_SENDING` or `FIN` flag respectively
/// and flushing the underlying connection.
#[derive(Debug, Copy, Clone)]
pub(crate) enum Closing {
Requested,
MessageSent,
}
impl State {
/// Performs a state transition for a flag contained in an inbound message.
pub(crate) fn handle_inbound_flag(&mut self, flag: Flag, buffer: &mut Bytes) {
let current = *self;
match (current, flag) {
(Self::Open, Flag::FIN) => {
*self = Self::ReadClosed;
}
(Self::WriteClosed, Flag::FIN) => {
*self = Self::BothClosed { reset: false };
}
(Self::Open, Flag::STOP_SENDING) => {
*self = Self::WriteClosed;
}
(Self::ReadClosed, Flag::STOP_SENDING) => {
*self = Self::BothClosed { reset: false };
}
(_, Flag::RESET) => {
buffer.clear();
*self = Self::BothClosed { reset: true };
}
_ => {}
}
}
pub(crate) fn write_closed(&mut self) {
match self {
State::ClosingWrite {
read_closed: true,
inner,
} => {
debug_assert!(matches!(inner, Closing::MessageSent));
*self = State::BothClosed { reset: false };
}
State::ClosingWrite {
read_closed: false,
inner,
} => {
debug_assert!(matches!(inner, Closing::MessageSent));
*self = State::WriteClosed;
}
State::Open
| State::ReadClosed
| State::WriteClosed
| State::ClosingRead { .. }
| State::BothClosed { .. } => {
unreachable!("bad state machine impl")
}
}
}
pub(crate) fn close_write_message_sent(&mut self) {
match self {
State::ClosingWrite { inner, read_closed } => {
debug_assert!(matches!(inner, Closing::Requested));
*self = State::ClosingWrite {
read_closed: *read_closed,
inner: Closing::MessageSent,
};
}
State::Open
| State::ReadClosed
| State::WriteClosed
| State::ClosingRead { .. }
| State::BothClosed { .. } => {
unreachable!("bad state machine impl")
}
}
}
pub(crate) fn read_closed(&mut self) {
match self {
State::ClosingRead {
write_closed: true,
inner,
} => {
debug_assert!(matches!(inner, Closing::MessageSent));
*self = State::BothClosed { reset: false };
}
State::ClosingRead {
write_closed: false,
inner,
} => {
debug_assert!(matches!(inner, Closing::MessageSent));
*self = State::ReadClosed;
}
State::Open
| State::ReadClosed
| State::WriteClosed
| State::ClosingWrite { .. }
| State::BothClosed { .. } => {
unreachable!("bad state machine impl")
}
}
}
pub(crate) fn close_read_message_sent(&mut self) {
match self {
State::ClosingRead {
inner,
write_closed,
} => {
debug_assert!(matches!(inner, Closing::Requested));
*self = State::ClosingRead {
write_closed: *write_closed,
inner: Closing::MessageSent,
};
}
State::Open
| State::ReadClosed
| State::WriteClosed
| State::ClosingWrite { .. }
| State::BothClosed { .. } => {
unreachable!("bad state machine impl")
}
}
}
/// Whether we should read from the stream in the [`futures::AsyncWrite`] implementation.
///
/// This is necessary for read-closed streams because we would otherwise not read any more flags from
/// the socket.
pub(crate) fn read_flags_in_async_write(&self) -> bool {
matches!(self, Self::ReadClosed)
}
/// Acts as a "barrier" for [`futures::AsyncRead::poll_read`].
pub(crate) fn read_barrier(&self) -> io::Result<()> {
use State::*;
let kind = match self {
Open
| WriteClosed
| ClosingWrite {
read_closed: false, ..
} => return Ok(()),
ClosingWrite {
read_closed: true, ..
}
| ReadClosed
| ClosingRead { .. }
| BothClosed { reset: false } => io::ErrorKind::BrokenPipe,
BothClosed { reset: true } => io::ErrorKind::ConnectionReset,
};
Err(kind.into())
}
/// Acts as a "barrier" for [`futures::AsyncWrite::poll_write`].
pub(crate) fn write_barrier(&self) -> io::Result<()> {
use State::*;
let kind = match self {
Open
| ReadClosed
| ClosingRead {
write_closed: false,
..
} => return Ok(()),
ClosingRead {
write_closed: true, ..
}
| WriteClosed
| ClosingWrite { .. }
| BothClosed { reset: false } => io::ErrorKind::BrokenPipe,
BothClosed { reset: true } => io::ErrorKind::ConnectionReset,
};
Err(kind.into())
}
/// Acts as a "barrier" for [`futures::AsyncWrite::poll_close`].
pub(crate) fn close_write_barrier(&mut self) -> io::Result<Option<Closing>> {
loop {
match &self {
State::WriteClosed => return Ok(None),
State::ClosingWrite { inner, .. } => return Ok(Some(*inner)),
State::Open => {
*self = Self::ClosingWrite {
read_closed: false,
inner: Closing::Requested,
};
}
State::ReadClosed => {
*self = Self::ClosingWrite {
read_closed: true,
inner: Closing::Requested,
};
}
State::ClosingRead {
write_closed: true, ..
}
| State::BothClosed { reset: false } => {
return Err(io::ErrorKind::BrokenPipe.into())
}
State::ClosingRead {
write_closed: false,
..
} => {
return Err(io::Error::new(
io::ErrorKind::Other,
"cannot close read half while closing write half",
))
}
State::BothClosed { reset: true } => {
return Err(io::ErrorKind::ConnectionReset.into())
}
}
}
}
/// Acts as a "barrier" for [`Stream::poll_close_read`](super::Stream::poll_close_read).
pub(crate) fn close_read_barrier(&mut self) -> io::Result<Option<Closing>> {
loop {
match self {
State::ReadClosed => return Ok(None),
State::ClosingRead { inner, .. } => return Ok(Some(*inner)),
State::Open => {
*self = Self::ClosingRead {
write_closed: false,
inner: Closing::Requested,
};
}
State::WriteClosed => {
*self = Self::ClosingRead {
write_closed: true,
inner: Closing::Requested,
};
}
State::ClosingWrite {
read_closed: true, ..
}
| State::BothClosed { reset: false } => {
return Err(io::ErrorKind::BrokenPipe.into())
}
State::ClosingWrite {
read_closed: false, ..
} => {
return Err(io::Error::new(
io::ErrorKind::Other,
"cannot close write half while closing read half",
))
}
State::BothClosed { reset: true } => {
return Err(io::ErrorKind::ConnectionReset.into())
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::ErrorKind;
#[test]
fn cannot_read_after_receiving_fin() {
let mut open = State::Open;
open.handle_inbound_flag(Flag::FIN, &mut Bytes::default());
let error = open.read_barrier().unwrap_err();
assert_eq!(error.kind(), ErrorKind::BrokenPipe)
}
#[test]
fn cannot_read_after_closing_read() {
let mut open = State::Open;
open.close_read_barrier().unwrap();
open.close_read_message_sent();
open.read_closed();
let error = open.read_barrier().unwrap_err();
assert_eq!(error.kind(), ErrorKind::BrokenPipe)
}
#[test]
fn cannot_write_after_receiving_stop_sending() {
let mut open = State::Open;
open.handle_inbound_flag(Flag::STOP_SENDING, &mut Bytes::default());
let error = open.write_barrier().unwrap_err();
assert_eq!(error.kind(), ErrorKind::BrokenPipe)
}
#[test]
fn cannot_write_after_closing_write() {
let mut open = State::Open;
open.close_write_barrier().unwrap();
open.close_write_message_sent();
open.write_closed();
let error = open.write_barrier().unwrap_err();
assert_eq!(error.kind(), ErrorKind::BrokenPipe)
}
#[test]
fn everything_broken_after_receiving_reset() {
let mut open = State::Open;
open.handle_inbound_flag(Flag::RESET, &mut Bytes::default());
let error1 = open.read_barrier().unwrap_err();
let error2 = open.write_barrier().unwrap_err();
let error3 = open.close_write_barrier().unwrap_err();
let error4 = open.close_read_barrier().unwrap_err();
assert_eq!(error1.kind(), ErrorKind::ConnectionReset);
assert_eq!(error2.kind(), ErrorKind::ConnectionReset);
assert_eq!(error3.kind(), ErrorKind::ConnectionReset);
assert_eq!(error4.kind(), ErrorKind::ConnectionReset);
}
#[test]
fn should_read_flags_in_async_write_after_read_closed() {
let mut open = State::Open;
open.handle_inbound_flag(Flag::FIN, &mut Bytes::default());
assert!(open.read_flags_in_async_write())
}
#[test]
fn cannot_read_or_write_after_receiving_fin_and_stop_sending() {
let mut open = State::Open;
open.handle_inbound_flag(Flag::FIN, &mut Bytes::default());
open.handle_inbound_flag(Flag::STOP_SENDING, &mut Bytes::default());
let error1 = open.read_barrier().unwrap_err();
let error2 = open.write_barrier().unwrap_err();
assert_eq!(error1.kind(), ErrorKind::BrokenPipe);
assert_eq!(error2.kind(), ErrorKind::BrokenPipe);
}
#[test]
fn can_read_after_closing_write() {
let mut open = State::Open;
open.close_write_barrier().unwrap();
open.close_write_message_sent();
open.write_closed();
open.read_barrier().unwrap();
}
#[test]
fn can_write_after_closing_read() {
let mut open = State::Open;
open.close_read_barrier().unwrap();
open.close_read_message_sent();
open.read_closed();
open.write_barrier().unwrap();
}
#[test]
fn cannot_write_after_starting_close() {
let mut open = State::Open;
open.close_write_barrier().expect("to close in open");
let error = open.write_barrier().unwrap_err();
assert_eq!(error.kind(), ErrorKind::BrokenPipe);
}
#[test]
fn cannot_read_after_starting_close() {
let mut open = State::Open;
open.close_read_barrier().expect("to close in open");
let error = open.read_barrier().unwrap_err();
assert_eq!(error.kind(), ErrorKind::BrokenPipe);
}
#[test]
fn can_read_in_open() {
let open = State::Open;
let result = open.read_barrier();
result.unwrap();
}
#[test]
fn can_write_in_open() {
let open = State::Open;
let result = open.write_barrier();
result.unwrap();
}
#[test]
fn write_close_barrier_returns_ok_when_closed() {
let mut open = State::Open;
open.close_write_barrier().unwrap();
open.close_write_message_sent();
open.write_closed();
let maybe = open.close_write_barrier().unwrap();
assert!(maybe.is_none())
}
#[test]
fn read_close_barrier_returns_ok_when_closed() {
let mut open = State::Open;
open.close_read_barrier().unwrap();
open.close_read_message_sent();
open.read_closed();
let maybe = open.close_read_barrier().unwrap();
assert!(maybe.is_none())
}
#[test]
fn reset_flag_clears_buffer() {
let mut open = State::Open;
let mut buffer = Bytes::copy_from_slice(b"foobar");
open.handle_inbound_flag(Flag::RESET, &mut buffer);
assert!(buffer.is_empty());
}
}
|