Skip to content

Commit 5434bfc

Browse files
Add feature for early testing of feature(read_buf) (#248)
Also update to rustls 0.20.4.
1 parent 39a5596 commit 5434bfc

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ links = "rustls_ffi"
1818
# interfering with copies of the global logger brought in by other Rust
1919
# libraries.
2020
no_log_capture = []
21+
read_buf = ["rustls/read_buf"]
2122

2223
[dependencies]
2324
# Keep in sync with RUSTLS_CRATE_VERSION in build.rs
24-
rustls = { version = "=0.20.2", features = [ "dangerous_configuration" ] }
25+
rustls = { version = "=0.20.4", features = [ "dangerous_configuration" ] }
2526
webpki = "0.22"
2627
libc = "0.2"
2728
sct = "0.7"

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::Write;
33
use std::{env, fs, path::PathBuf};
44

55
// Keep in sync with Cargo.toml.
6-
const RUSTLS_CRATE_VERSION: &str = "0.20.2";
6+
const RUSTLS_CRATE_VERSION: &str = "0.20.4";
77

88
fn main() {
99
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

cbindgen.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ rename_variants = "ScreamingSnakeCase"
88

99
[export]
1010
include = ["rustls_tls_version"]
11+
12+
[defines]
13+
"feature = read_buf" = "DEFINE_READ_BUF"

src/connection.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,45 @@ impl rustls_connection {
471471
}
472472
}
473473

474+
/// Read up to `count` plaintext bytes from the `rustls_connection` into `buf`.
475+
/// On success, store the number of bytes read in *out_n (this may be less
476+
/// than `count`). A success with *out_n set to 0 means "all bytes currently
477+
/// available have been read, but more bytes may become available after
478+
/// subsequent calls to rustls_connection_read_tls and
479+
/// rustls_connection_process_new_packets."
480+
///
481+
/// This experimental API is only available when using a nightly Rust compiler
482+
/// and enabling the `read_buf` Cargo feature. It will be deprecated and later
483+
/// removed in future versions.
484+
///
485+
/// Unlike with `rustls_connection_read`, this function may be called with `buf`
486+
/// pointing to an uninitialized memory buffer.
487+
#[cfg(feature = "read_buf")]
488+
#[no_mangle]
489+
pub extern "C" fn rustls_connection_read_2(
490+
conn: *mut rustls_connection,
491+
buf: *mut std::mem::MaybeUninit<u8>,
492+
count: size_t,
493+
out_n: *mut size_t,
494+
) -> rustls_result {
495+
ffi_panic_boundary! {
496+
let conn: &mut Connection = try_mut_from_ptr!(conn);
497+
let read_buf: &mut [std::mem::MaybeUninit<u8>] = try_mut_slice!(buf, count);
498+
let out_n: &mut size_t = try_mut_from_ptr!(out_n);
499+
500+
let mut read_buf = std::io::ReadBuf::uninit(read_buf);
501+
502+
let n_read: usize = match conn.reader().read_buf(&mut read_buf) {
503+
Ok(()) => read_buf.filled_len(),
504+
Err(e) if e.kind() == ErrorKind::UnexpectedEof => return rustls_result::UnexpectedEof,
505+
Err(e) if e.kind() == ErrorKind::WouldBlock => return rustls_result::PlaintextEmpty,
506+
Err(_) => return rustls_result::Io,
507+
};
508+
*out_n = n_read;
509+
rustls_result::Ok
510+
}
511+
}
512+
474513
/// Free a rustls_connection. Calling with NULL is fine.
475514
/// Must not be called twice with the same value.
476515
#[no_mangle]

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![crate_type = "staticlib"]
22
#![allow(non_camel_case_types)]
33
#![allow(clippy::not_unsafe_ptr_arg_deref)]
4+
#![cfg_attr(feature = "read_buf", feature(read_buf))]
45

56
//! This package contains bindings for using rustls via a C API. If
67
//! you're looking at this on docs.rs, [you may want the rustls docs

src/rustls.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,28 @@ rustls_result rustls_connection_read(struct rustls_connection *conn,
10071007
size_t count,
10081008
size_t *out_n);
10091009

1010+
#if defined(DEFINE_READ_BUF)
1011+
/**
1012+
* Read up to `count` plaintext bytes from the `rustls_connection` into `buf`.
1013+
* On success, store the number of bytes read in *out_n (this may be less
1014+
* than `count`). A success with *out_n set to 0 means "all bytes currently
1015+
* available have been read, but more bytes may become available after
1016+
* subsequent calls to rustls_connection_read_tls and
1017+
* rustls_connection_process_new_packets."
1018+
*
1019+
* This experimental API is only available when using a nightly Rust compiler
1020+
* and enabling the `read_buf` Cargo feature. It will be deprecated and later
1021+
* removed in future versions.
1022+
*
1023+
* Unlike with `rustls_connection_read`, this function may be called with `buf`
1024+
* pointing to an uninitialized memory buffer.
1025+
*/
1026+
rustls_result rustls_connection_read_2(struct rustls_connection *conn,
1027+
uint8_t *buf,
1028+
size_t count,
1029+
size_t *out_n);
1030+
#endif
1031+
10101032
/**
10111033
* Free a rustls_connection. Calling with NULL is fine.
10121034
* Must not be called twice with the same value.

0 commit comments

Comments
 (0)