Skip to content

Commit

Permalink
ffi: expose stream application errors
Browse files Browse the repository at this point in the history
Fixes #1699.
  • Loading branch information
bwoebi authored May 8, 2024
1 parent 70b67f9 commit 89b11b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
8 changes: 5 additions & 3 deletions quiche/examples/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ static void recv_cb(EV_P_ ev_io *w, int revents) {
(int) app_proto_len, app_proto);

const static uint8_t r[] = "GET /index.html\r\n";
if (quiche_conn_stream_send(conn_io->conn, 4, r, sizeof(r), true) < 0) {
fprintf(stderr, "failed to send HTTP request\n");
uint64_t error_code;
if (quiche_conn_stream_send(conn_io->conn, 4, r, sizeof(r), true, &error_code) < 0) {
fprintf(stderr, "failed to send HTTP request: %" PRIu64 "\n", error_code);
return;
}

Expand All @@ -179,9 +180,10 @@ static void recv_cb(EV_P_ ev_io *w, int revents) {
fprintf(stderr, "stream %" PRIu64 " is readable\n", s);

bool fin = false;
uint64_t error_code;
ssize_t recv_len = quiche_conn_stream_recv(conn_io->conn, s,
buf, sizeof(buf),
&fin);
&fin, &error_code);
if (recv_len < 0) {
break;
}
Expand Down
6 changes: 4 additions & 2 deletions quiche/examples/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,17 +378,19 @@ static void recv_cb(EV_P_ ev_io *w, int revents) {
fprintf(stderr, "stream %" PRIu64 " is readable\n", s);

bool fin = false;
uint64_t error_code;
ssize_t recv_len = quiche_conn_stream_recv(conn_io->conn, s,
buf, sizeof(buf),
&fin);
&fin, &error_code);
if (recv_len < 0) {
break;
}

if (fin) {
static const char *resp = "byez\n";
uint64_t error_code;
quiche_conn_stream_send(conn_io->conn, s, (uint8_t *) resp,
5, true);
5, true, &error_code);
}
}

Expand Down
10 changes: 8 additions & 2 deletions quiche/include/quiche.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,18 @@ size_t quiche_conn_send_quantum_on_path(const quiche_conn *conn,


// Reads contiguous data from a stream.
// out_error_code is only set when STREAM_STOPPED or STREAM_RESET are returned.
// Set to the reported error code associated with STOP_SENDING or STREAM_RESET.
ssize_t quiche_conn_stream_recv(quiche_conn *conn, uint64_t stream_id,
uint8_t *out, size_t buf_len, bool *fin);
uint8_t *out, size_t buf_len, bool *fin,
uint64_t *out_error_code);

// Writes data to a stream.
// out_error_code is only set when STREAM_STOPPED or STREAM_RESET are returned.
// Set to the reported error code associated with STOP_SENDING or STREAM_RESET.
ssize_t quiche_conn_stream_send(quiche_conn *conn, uint64_t stream_id,
const uint8_t *buf, size_t buf_len, bool fin);
const uint8_t *buf, size_t buf_len, bool fin,
uint64_t *out_error_code);

// The side of the stream to be shut down.
enum quiche_shutdown {
Expand Down
22 changes: 18 additions & 4 deletions quiche/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ pub extern fn quiche_conn_send_on_path(
#[no_mangle]
pub extern fn quiche_conn_stream_recv(
conn: &mut Connection, stream_id: u64, out: *mut u8, out_len: size_t,
fin: &mut bool,
fin: &mut bool, out_error_code: &mut u64,
) -> ssize_t {
if out_len > <ssize_t>::max_value() as usize {
panic!("The provided buffer is too large");
Expand All @@ -828,7 +828,14 @@ pub extern fn quiche_conn_stream_recv(
let (out_len, out_fin) = match conn.stream_recv(stream_id, out) {
Ok(v) => v,

Err(e) => return e.to_c(),
Err(e) => {
match e {
Error::StreamReset(error) => *out_error_code = error,
Error::StreamStopped(error) => *out_error_code = error,
_ => {},
}
return e.to_c();
},
};

*fin = out_fin;
Expand All @@ -839,7 +846,7 @@ pub extern fn quiche_conn_stream_recv(
#[no_mangle]
pub extern fn quiche_conn_stream_send(
conn: &mut Connection, stream_id: u64, buf: *const u8, buf_len: size_t,
fin: bool,
fin: bool, out_error_code: &mut u64,
) -> ssize_t {
if buf_len > <ssize_t>::max_value() as usize {
panic!("The provided buffer is too large");
Expand All @@ -850,7 +857,14 @@ pub extern fn quiche_conn_stream_send(
match conn.stream_send(stream_id, buf, fin) {
Ok(v) => v as ssize_t,

Err(e) => e.to_c(),
Err(e) => {
match e {
Error::StreamReset(error) => *out_error_code = error,
Error::StreamStopped(error) => *out_error_code = error,
_ => {},
}
e.to_c()
},
}
}

Expand Down

0 comments on commit 89b11b2

Please sign in to comment.