Skip to content

Commit

Permalink
Merge pull request #1473 from NerLOR/master
Browse files Browse the repository at this point in the history
Add SSL_peek support
  • Loading branch information
sfackler authored May 23, 2021
2 parents 6f395bc + e33047d commit 78caec6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions openssl-sys/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ extern "C" {
pub fn SSL_stateless(s: *mut SSL) -> c_int;
pub fn SSL_connect(ssl: *mut SSL) -> c_int;
pub fn SSL_read(ssl: *mut SSL, buf: *mut c_void, num: c_int) -> c_int;
pub fn SSL_peek(ssl: *mut SSL, buf: *mut c_void, num: c_int) -> c_int;
#[cfg(ossl111)]
pub fn SSL_read_early_data(
s: *mut ::SSL,
Expand Down
24 changes: 24 additions & 0 deletions openssl/src/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2460,6 +2460,11 @@ impl SslRef {
unsafe { ffi::SSL_read(self.as_ptr(), buf.as_ptr() as *mut c_void, len) }
}

fn peek(&mut self, buf: &mut [u8]) -> c_int {
let len = cmp::min(c_int::max_value() as usize, buf.len()) as c_int;
unsafe { ffi::SSL_peek(self.as_ptr(), buf.as_ptr() as *mut c_void, len) }
}

fn write(&mut self, buf: &[u8]) -> c_int {
let len = cmp::min(c_int::max_value() as usize, buf.len()) as c_int;
unsafe { ffi::SSL_write(self.as_ptr(), buf.as_ptr() as *const c_void, len) }
Expand Down Expand Up @@ -3704,6 +3709,25 @@ impl<S: Read + Write> SslStream<S> {
}
}

/// Reads data from the stream, without removing it from the queue.
///
/// This corresponds to [`SSL_peek`].
///
/// [`SSL_peek`]: https://www.openssl.org/docs/manmaster/man3/SSL_peek.html
pub fn ssl_peek(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
// See above for why we short-circuit on zero-length buffers
if buf.is_empty() {
return Ok(0);
}

let ret = self.ssl.peek(buf);
if ret > 0 {
Ok(ret as usize)
} else {
Err(self.make_error(ret))
}
}

/// Shuts down the session.
///
/// The shutdown process consists of two steps. The first step sends a close notify message to
Expand Down

0 comments on commit 78caec6

Please sign in to comment.