Skip to content

Commit c3a1f9c

Browse files
committed
Fix sync coce not working
1 parent dca6f83 commit c3a1f9c

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

esp-mbedtls/src/lib.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ where
729729
///
730730
/// Note that calling it is not mandatory, because the TLS session is anyway
731731
/// negotiated during the first read or write operation.
732-
pub fn connect<'b>(&mut self) -> Result<(), TlsError> {
732+
pub fn connect(&mut self) -> Result<(), TlsError> {
733733
if matches!(self.state, SessionState::Connected) {
734734
return Ok(());
735735
} else if matches!(self.state, SessionState::Eof) {
@@ -739,7 +739,7 @@ where
739739
unsafe {
740740
mbedtls_ssl_set_bio(
741741
self.ssl_context,
742-
core::ptr::addr_of!(self) as *mut c_void,
742+
self as *mut _ as *mut c_void,
743743
Some(Self::send),
744744
Some(Self::receive),
745745
None,
@@ -753,6 +753,8 @@ where
753753
break;
754754
}
755755
if res < 0 && res != MBEDTLS_ERR_SSL_WANT_READ && res != MBEDTLS_ERR_SSL_WANT_WRITE
756+
// See https://github.com/Mbed-TLS/mbedtls/issues/8749
757+
&& res != MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET
756758
{
757759
// real error
758760
// Reference: https://os.mbed.com/teams/sandbox/code/mbedtls/docs/tip/ssl_8h.html#a4a37e497cd08c896870a42b1b618186e
@@ -789,6 +791,7 @@ where
789791
let res = self.internal_read(buf);
790792
#[allow(non_snake_case)]
791793
match res {
794+
// See https://github.com/Mbed-TLS/mbedtls/issues/8749
792795
MBEDTLS_ERR_SSL_WANT_READ | MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET => continue, // no data
793796
0_i32..=i32::MAX => return Ok(res as usize), // data
794797
i32::MIN..=-1_i32 => return Err(TlsError::MbedTlsError(res)), // error
@@ -808,8 +811,16 @@ where
808811
pub fn write(&mut self, data: &[u8]) -> Result<usize, TlsError> {
809812
self.connect()?;
810813

811-
let res = self.internal_write(data);
812-
Ok(res as usize)
814+
loop {
815+
let res = self.internal_write(data);
816+
#[allow(non_snake_case)]
817+
match res {
818+
// See https://github.com/Mbed-TLS/mbedtls/issues/8749
819+
MBEDTLS_ERR_SSL_WANT_WRITE | MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET => continue, // no data
820+
0_i32..=i32::MAX => return Ok(res as usize), // data
821+
i32::MIN..=-1_i32 => return Err(TlsError::MbedTlsError(res)), // error
822+
}
823+
}
813824
}
814825

815826
/// Flush the TLS connection

0 commit comments

Comments
 (0)