Helper: Try to read incoming data from socket/BIO Returns true if peer closed connection (with or without close_notify)
(
&self,
conn: &mut TlsConnection,
vm: &VirtualMachine,
)
| 4297 | // Helper: Try to read incoming data from socket/BIO |
| 4298 | // Returns true if peer closed connection (with or without close_notify) |
| 4299 | fn try_read_close_notify( |
| 4300 | &self, |
| 4301 | conn: &mut TlsConnection, |
| 4302 | vm: &VirtualMachine, |
| 4303 | ) -> PyResult<bool> { |
| 4304 | // In socket mode, peek first to avoid consuming post-TLS cleartext |
| 4305 | // data. During STARTTLS, after close_notify exchange, the socket |
| 4306 | // transitions to cleartext. Without peeking, sock_recv may consume |
| 4307 | // cleartext data meant for the application after unwrap(). |
| 4308 | if self.incoming_bio.is_none() { |
| 4309 | return self.try_read_close_notify_socket(conn, vm); |
| 4310 | } |
| 4311 | |
| 4312 | // BIO mode: read from incoming BIO |
| 4313 | match self.sock_recv(SSL3_RT_MAX_PLAIN_LENGTH, vm) { |
| 4314 | Ok(bytes_obj) => { |
| 4315 | let bytes = ArgBytesLike::try_from_object(vm, bytes_obj)?; |
| 4316 | let data = bytes.borrow_buf(); |
| 4317 | |
| 4318 | if data.is_empty() { |
| 4319 | if let Some(ref bio) = self.incoming_bio { |
| 4320 | // BIO mode: check if EOF was signaled via write_eof() |
| 4321 | let bio_obj: PyObjectRef = bio.clone().into(); |
| 4322 | let eof_attr = bio_obj.get_attr("eof", vm)?; |
| 4323 | let is_eof = eof_attr.try_to_bool(vm)?; |
| 4324 | if !is_eof { |
| 4325 | return Ok(false); |
| 4326 | } |
| 4327 | } |
| 4328 | return Ok(true); |
| 4329 | } |
| 4330 | |
| 4331 | let data_slice: &[u8] = data.as_ref(); |
| 4332 | let mut cursor = std::io::Cursor::new(data_slice); |
| 4333 | let _ = conn.read_tls(&mut cursor); |
| 4334 | let _ = conn.process_new_packets(); |
| 4335 | Ok(false) |
| 4336 | } |
| 4337 | Err(e) => { |
| 4338 | if is_blocking_io_error(&e, vm) { |
| 4339 | return Ok(false); |
| 4340 | } |
| 4341 | Ok(true) |
| 4342 | } |
| 4343 | } |
| 4344 | } |
| 4345 | |
| 4346 | /// Socket-mode close_notify reader that respects TLS record boundaries. |
| 4347 | /// Uses MSG_PEEK to inspect data before consuming, preventing accidental |
no test coverage detected