Skip to content

Commit d137291

Browse files
committed
Handle stream EOF
1 parent 013098c commit d137291

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

esp-mbedtls/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,12 @@ impl<'d> Tls<'d> {
578578
///
579579
/// Note that there could be only one active `Tls` instance at any point in time,
580580
/// and the function will return an error if there is already an active instance.
581-
#[cfg(all(not(any(
581+
#[cfg(not(any(
582582
feature = "esp32",
583583
feature = "esp32c3",
584584
feature = "esp32s2",
585585
feature = "esp32s3"
586-
))))]
586+
)))]
587587
pub fn new() -> Result<Self, TlsError> {
588588
Self::create()
589589
}
@@ -1074,6 +1074,10 @@ pub mod asynch {
10741074
log::debug!("Establishing SSL connection");
10751075

10761076
self.io(|ssl| unsafe { mbedtls_ssl_handshake(ssl) }).await?;
1077+
if matches!(self.state, SessionState::Eof) {
1078+
return Err(TlsError::Eof);
1079+
}
1080+
10771081
self.state = SessionState::Connected;
10781082

10791083
Ok(())
@@ -1182,6 +1186,10 @@ pub mod asynch {
11821186
PollOutcome::Retry => continue,
11831187
PollOutcome::WantRead => self.wait_read().await?,
11841188
PollOutcome::WantWrite => self.flush_write().await?,
1189+
PollOutcome::Eof => {
1190+
self.state = SessionState::Eof;
1191+
break Ok(0);
1192+
}
11851193
}
11861194
}
11871195
}
@@ -1271,6 +1279,8 @@ pub mod asynch {
12711279
WantWrite,
12721280
/// Operation needs to be retried
12731281
Retry,
1282+
/// End of stream reached
1283+
Eof,
12741284
}
12751285

12761286
/// A context for using the async `Read` and `Write` traits from within the synchronous MbedTLS "mbio" callbacks
@@ -1372,6 +1382,7 @@ pub mod asynch {
13721382
MBEDTLS_ERR_SSL_WANT_WRITE => Ok(PollOutcome::WantWrite),
13731383
// See https://github.com/Mbed-TLS/mbedtls/issues/8749
13741384
MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET => Ok(PollOutcome::Retry),
1385+
MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY => Ok(PollOutcome::Eof),
13751386
res if res < 0 => {
13761387
::log::warn!("MbedTLS error: {res} / {res:x}");
13771388
Err(TlsError::MbedTlsError(res))

0 commit comments

Comments
 (0)