Skip to content

Commit

Permalink
return error also in the closing state
Browse files Browse the repository at this point in the history
If smoltcp receives a FIN message, the socket changes the state
to the `closing`, but the socket is still active. To close the
socket, the kernel returns a error if the socket is in the
closing state.

The PR should solve issue #880
  • Loading branch information
stlankes committed Sep 3, 2023
1 parent a987ec4 commit b0b009c
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions src/fd/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,28 @@ impl<T> Socket<T> {
#[allow(clippy::needless_pass_by_ref_mut)]
async fn async_read(&self, buffer: &mut [u8]) -> Result<isize, i32> {
future::poll_fn(|cx| {
self.with(|socket| {
if !socket.is_active() {
Poll::Ready(Err(-crate::errno::EIO))
} else if socket.can_recv() {
Poll::Ready(
socket
.recv(|data| {
let len = core::cmp::min(buffer.len(), data.len());
buffer[..len].copy_from_slice(&data[..len]);
(len, isize::try_from(len).unwrap())
})
.map_err(|_| -crate::errno::EIO),
)
} else {
socket.register_recv_waker(cx.waker());
Poll::Pending
self.with(|socket| match socket.state() {
tcp::State::FinWait1
| tcp::State::FinWait2
| tcp::State::Closed
| tcp::State::Closing
| tcp::State::CloseWait
| tcp::State::TimeWait => Poll::Ready(Err(-crate::errno::EIO)),
_ => {
if socket.can_recv() {
Poll::Ready(
socket
.recv(|data| {
let len = core::cmp::min(buffer.len(), data.len());
buffer[..len].copy_from_slice(&data[..len]);
(len, isize::try_from(len).unwrap())
})
.map_err(|_| -crate::errno::EIO),
)
} else {
socket.register_recv_waker(cx.waker());
Poll::Pending
}
}
})
})
Expand All @@ -99,21 +105,29 @@ impl<T> Socket<T> {
while pos < buffer.len() {
let n = future::poll_fn(|cx| {
self.with(|socket| {
if !socket.is_active() {
Poll::Ready(Err(-crate::errno::EIO))
} else if socket.can_send() {
Poll::Ready(
socket
.send_slice(&buffer[pos..])
.map_err(|_| -crate::errno::EIO),
)
} else if pos > 0 {
// we already send some data => return 0 as signal to stop the
// async write
Poll::Ready(Ok(0))
} else {
socket.register_send_waker(cx.waker());
Poll::Pending
match socket.state() {
tcp::State::FinWait1
| tcp::State::FinWait2
| tcp::State::Closed
| tcp::State::Closing
| tcp::State::CloseWait
| tcp::State::TimeWait => Poll::Ready(Err(-crate::errno::EIO)),
_ => {
if socket.can_send() {
Poll::Ready(
socket
.send_slice(&buffer[pos..])
.map_err(|_| -crate::errno::EIO),
)
} else if pos > 0 {
// we already send some data => return 0 as signal to stop the
// async write
Poll::Ready(Ok(0))
} else {
socket.register_send_waker(cx.waker());
Poll::Pending
}
}
}
})
})
Expand Down

0 comments on commit b0b009c

Please sign in to comment.