Skip to content

Commit

Permalink
Switch to only using poll_recv
Browse files Browse the repository at this point in the history
  • Loading branch information
lrstewart committed Sep 23, 2024
1 parent dbc3f79 commit 24456fb
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 448 deletions.
20 changes: 6 additions & 14 deletions bindings/rust/s2n-tls/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,12 @@ impl Connection {
}

/// Attempts to flush any data previously buffered by a call to [send](`Self::poll_send`).
///
/// This method ONLY flushes any data already encrypted and queued for writing
/// to the underlying stream. It does not automatically retry a previous call
/// to `poll_send`, which might require encrypting and sending more records.
/// The only way to reliably complete a call to `poll_send` is to call `poll_send`
/// again with the same inputs until it returns `Ready`.
pub fn poll_flush(&mut self) -> Poll<Result<&mut Self, Error>> {
self.poll_send(&[0; 0]).map_ok(|_| self)
}
Expand Down Expand Up @@ -921,20 +927,6 @@ impl Connection {
}
}

#[allow(dead_code)]
pub(crate) fn message_type(&self) -> Result<&str, Error> {
let message = unsafe {
s2n_connection_get_last_message_name(self.connection.as_ptr()).into_result()?
};
unsafe {
// SAFETY: Constructed strings have a null byte appended to them.
// SAFETY: The data has a 'static lifetime, because it resides in a
// static char array, and is never modified after its initial
// creation.
const_str!(message)
}
}

pub fn cipher_suite(&self) -> Result<&str, Error> {
let cipher = unsafe { s2n_connection_get_cipher(self.connection.as_ptr()).into_result()? };
unsafe {
Expand Down
28 changes: 24 additions & 4 deletions bindings/rust/s2n-tls/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use s2n_tls_sys::*;
use std::{convert::TryFrom, ffi::CStr};

#[non_exhaustive]
#[derive(Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ErrorType {
UnknownErrorType,
NoError,
Expand Down Expand Up @@ -49,6 +49,7 @@ impl From<libc::c_int> for ErrorType {
enum Context {
InvalidInput,
MissingWaker,
Definition(ErrorType, &'static str, &'static str),
Code(s2n_status_code::Type, Errno),
Application(Box<dyn std::error::Error + Send + Sync + 'static>),
}
Expand Down Expand Up @@ -167,6 +168,14 @@ impl Error {
Self(Context::Application(error))
}

/// A one-off error occured while running bindings code.
///
/// Can be emitted rather than creating an entirely new error type.
#[allow(dead_code)]
pub(crate) fn from(kind: ErrorType, name: &'static str, message: &'static str) -> Self {
Self(Context::Definition(kind, name, message))
}

fn capture() -> Self {
unsafe {
let s2n_errno = s2n_errno_location();
Expand All @@ -186,6 +195,7 @@ impl Error {
match self.0 {
Context::InvalidInput => "InvalidInput",
Context::MissingWaker => "MissingWaker",
Context::Definition(_, name, _) => name,
Context::Application(_) => "ApplicationError",
Context::Code(code, _) => unsafe {
// Safety: we assume the string has a valid encoding coming from s2n
Expand All @@ -200,6 +210,7 @@ impl Error {
Context::MissingWaker => {
"Tried to perform an asynchronous operation without a configured waker"
}
Context::Definition(_, _, msg) => msg,
Context::Application(_) => "An error occurred while executing application code",
Context::Code(code, _) => unsafe {
// Safety: we assume the string has a valid encoding coming from s2n
Expand All @@ -210,7 +221,10 @@ impl Error {

pub fn debug(&self) -> Option<&'static str> {
match self.0 {
Context::InvalidInput | Context::MissingWaker | Context::Application(_) => None,
Context::InvalidInput
| Context::MissingWaker
| Context::Definition(_, _, _)
| Context::Application(_) => None,
Context::Code(code, _) => unsafe {
let debug_info = s2n_strerror_debug(code, core::ptr::null());

Expand All @@ -231,14 +245,17 @@ impl Error {
pub fn kind(&self) -> ErrorType {
match self.0 {
Context::InvalidInput | Context::MissingWaker => ErrorType::UsageError,
Context::Definition(error_type, _, _) => error_type,
Context::Application(_) => ErrorType::Application,
Context::Code(code, _) => unsafe { ErrorType::from(s2n_error_get_type(code)) },
}
}

pub fn source(&self) -> ErrorSource {
match self.0 {
Context::InvalidInput | Context::MissingWaker => ErrorSource::Bindings,
Context::InvalidInput | Context::MissingWaker | Context::Definition(_, _, _) => {
ErrorSource::Bindings
}
Context::Application(_) => ErrorSource::Application,
Context::Code(_, _) => ErrorSource::Library,
}
Expand Down Expand Up @@ -270,7 +287,10 @@ impl Error {
/// This API is currently incomplete and should not be relied upon.
pub fn alert(&self) -> Option<u8> {
match self.0 {
Context::InvalidInput | Context::MissingWaker | Context::Application(_) => None,
Context::InvalidInput
| Context::MissingWaker
| Context::Definition(_, _, _)
| Context::Application(_) => None,
Context::Code(code, _) => {
let mut alert = 0;
let r = unsafe { s2n_error_get_alert(code, &mut alert) };
Expand Down
Loading

0 comments on commit 24456fb

Please sign in to comment.