Skip to content

Commit

Permalink
Update endpoint FFI (Tencent#78)
Browse files Browse the repository at this point in the history
- quic_endpoint_new() no longer takes ownership of the resources provides by the C caller
- remove quic_transport_handler_t/quic_packet_send_handler_t from tquic.h
  • Loading branch information
iyangsj authored Nov 30, 2023
1 parent 97b90f0 commit 3e7b305
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ bytes = "1"
rustc-hash = "1.1"
slab = "0.4"
enumflags2 = "0.7.5"
ring = "0.16"
ring = "0.17"
libc = "0.2"
lazy_static = "1"
log = { version = "0.4", features = ["std"] }
Expand Down
21 changes: 9 additions & 12 deletions include/tquic.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ typedef struct quic_transport_methods_t {

typedef void *quic_transport_context_t;

typedef struct quic_transport_handler_t {
const struct quic_transport_methods_t *methods;
quic_transport_context_t context;
} quic_transport_handler_t;

/**
* Data and meta information of a outgoing packet.
*/
Expand All @@ -190,11 +185,6 @@ typedef struct quic_packet_send_methods_t {

typedef void *quic_packet_send_context_t;

typedef struct quic_packet_send_handler_t {
const struct quic_packet_send_methods_t *methods;
quic_packet_send_context_t context;
} quic_packet_send_handler_t;

/**
* Meta information of a incoming packet.
*/
Expand Down Expand Up @@ -428,13 +418,20 @@ void quic_config_set_tls_selector(struct quic_config_t *config,

/**
* Create a QUIC endpoint.
*
* The caller is responsible for the memory of the Endpoint and properly
* destroy it by calling `quic_endpoint_free`.
*
* Note: The endpoint doesn't own the underlying resources provided by the C
* caller. It is the responsibility of the caller to ensure that these
* resources outlive the endpoint and release them correctly.
*/
struct quic_endpoint_t *quic_endpoint_new(struct quic_config_t *config,
bool is_server,
struct quic_transport_handler_t *handler,
struct quic_packet_send_handler_t *sender);
const struct quic_transport_methods_t *handler_methods,
quic_transport_context_t handler_ctx,
const struct quic_packet_send_methods_t *sender_methods,
quic_packet_send_context_t sender_ctx);

/**
* Destroy a QUIC endpoint.
Expand Down
6 changes: 3 additions & 3 deletions src/connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl Connection {
local: SocketAddr,
remote: SocketAddr,
server_name: Option<&str>,
conf: &mut Config,
conf: &Config,
) -> Result<Self> {
Connection::new(scid, local, remote, server_name, None, conf, false)
}
Expand All @@ -180,7 +180,7 @@ impl Connection {
local: SocketAddr,
remote: SocketAddr,
token: Option<&AddressToken>,
conf: &mut Config,
conf: &Config,
) -> Result<Self> {
Connection::new(scid, local, remote, None, token, conf, true)
}
Expand All @@ -196,7 +196,7 @@ impl Connection {
remote: SocketAddr,
server_name: Option<&str>,
addr_token: Option<&AddressToken>,
conf: &mut Config,
conf: &Config,
is_server: bool,
) -> Result<Self> {
let trace_id = format!("{}-{}", if is_server { "SERVER" } else { "CLIENT" }, scid);
Expand Down
5 changes: 2 additions & 3 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Endpoint {

// Create a client connection.
let scid = self.cid_gen.generate();
let conn = Connection::new_client(&scid, local, remote, server_name, &mut self.config)?;
let conn = Connection::new_client(&scid, local, remote, server_name, &self.config)?;
let idx = self.conns.insert(conn);
if let Some(conn) = self.conns.get_mut(idx) {
conn.set_index(idx);
Expand Down Expand Up @@ -249,8 +249,7 @@ impl Endpoint {

// Create a server connection
let scid = self.cid_gen.generate();
let conn =
Connection::new_server(&scid, local, remote, token.as_ref(), &mut self.config)?;
let conn = Connection::new_server(&scid, local, remote, token.as_ref(), &self.config)?;
let idx = self.conns.insert(conn);
if cid_len > 0 {
self.routes.insert_with_cid(scid, idx);
Expand Down
26 changes: 21 additions & 5 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,33 @@ pub extern "C" fn quic_config_set_tls_selector(
}

/// Create a QUIC endpoint.
///
/// The caller is responsible for the memory of the Endpoint and properly
/// destroy it by calling `quic_endpoint_free`.
///
/// Note: The endpoint doesn't own the underlying resources provided by the C
/// caller. It is the responsibility of the caller to ensure that these
/// resources outlive the endpoint and release them correctly.
#[no_mangle]
pub extern "C" fn quic_endpoint_new(
config: *mut Config,
is_server: bool,
handler: *mut TransportHandler,
sender: *mut PacketSendHandler,
handler_methods: *const TransportMethods,
handler_ctx: TransportContext,
sender_methods: *const PacketSendMethods,
sender_ctx: PacketSendContext,
) -> *mut Endpoint {
let config = unsafe { Box::from_raw(config) };
let handler = unsafe { Box::from_raw(handler) };
let sender = unsafe { Rc::from_raw(sender) };
let e = Endpoint::new(config, is_server, handler, sender);
let handler = Box::new(TransportHandler {
methods: handler_methods,
context: handler_ctx,
});
let sender = Rc::new(PacketSendHandler {
methods: sender_methods,
context: sender_ctx,
});
let e = Endpoint::new(config.clone(), is_server, handler, sender);
Box::into_raw(config);
Box::into_raw(Box::new(e))
}

Expand Down Expand Up @@ -1020,6 +1034,7 @@ pub struct TransportMethods {
#[repr(transparent)]
pub struct TransportContext(*mut c_void);

/// cbindgen:no-export
#[repr(C)]
pub struct TransportHandler {
pub methods: *const TransportMethods,
Expand Down Expand Up @@ -1108,6 +1123,7 @@ pub struct PacketSendMethods {
#[repr(transparent)]
pub struct PacketSendContext(*mut c_void);

/// cbindgen:no-export
#[repr(C)]
pub struct PacketSendHandler {
pub methods: *const PacketSendMethods,
Expand Down
7 changes: 2 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ fn version_is_supported(version: u32) -> bool {
}

/// Configurations about QUIC endpoint.
#[derive(Clone)]
pub struct Config {
/// QUIC transport configuration.
local_transport_params: TransportParams,
Expand Down Expand Up @@ -604,11 +605,7 @@ impl Config {
}

/// Create new tls session.
fn new_tls_session(
&mut self,
server_name: Option<&str>,
is_server: bool,
) -> Result<TlsSession> {
fn new_tls_session(&self, server_name: Option<&str>, is_server: bool) -> Result<TlsSession> {
if self.tls_config_selector.is_none() {
return Err(Error::TlsFail("tls config selector is not set".into()));
}
Expand Down

0 comments on commit 3e7b305

Please sign in to comment.