From e72ce0fc85fbaa90d93bb92c5c2ed795644475ba Mon Sep 17 00:00:00 2001 From: Richard Pospesel Date: Sat, 30 Mar 2024 23:17:07 +0000 Subject: [PATCH] cgosling: callbacks passing OS-specific TCP sockets handles now use generic GoslingTcpSocket --- source/bindings/java/build_java_bindings.rs | 10 +-- source/gosling/crates/cgosling/cbindgen.toml | 1 + source/gosling/crates/cgosling/src/ffi.rs | 68 +++----------------- 3 files changed, 16 insertions(+), 63 deletions(-) diff --git a/source/bindings/java/build_java_bindings.rs b/source/bindings/java/build_java_bindings.rs index 0e718d0f..027121dc 100644 --- a/source/bindings/java/build_java_bindings.rs +++ b/source/bindings/java/build_java_bindings.rs @@ -73,7 +73,6 @@ handlebars_helper!(inputParamsToJavaParams: |params: Vec| { for param in params { let java_typename = match param.typename.as_ref() { "bool" => "boolean".to_string(), - "int" | "SOCKET" => "java.net.Socket".to_string(), "uint8_t" => "byte".to_string(), "uint16_t" => "int".to_string(), "uint32_t" => "long".to_string(), @@ -93,6 +92,7 @@ handlebars_helper!(inputParamsToJavaParams: |params: Vec| { "const uint8_t*" => "byte[]".to_string(), "uint8_t*" => "byte[]".to_string(), "gosling_handshake_handle_t" => "long".to_string(), + "gosling_tcp_socket_t" => "java.net.Socket".to_string(), other => { let other = if other.starts_with("const ") { &other[6..] @@ -413,10 +413,10 @@ handlebars_helper!(marshallNativeParams: |input_params: Vec| { cpp_src!("std::fill({name}_jni_buffer, {name}_jni_buffer + {name}_size, jbyte(0));"); cpp_src!("env->ReleaseByteArrayElements({name}_jni, {name}_jni_buffer, 0);"); }, - "int" | "SOCKET" => { + "gosling_handshake_handle_t" => cpp_src!("const jlong {name}_jni = static_cast({name});"), + "gosling_tcp_socket_t" => { cpp_src!("jobject {name}_jni = g_jni_glue->tcp_stream_to_java_socket(env, {name});"); }, - "gosling_handshake_handle_t" => cpp_src!("const jlong {name}_jni = static_cast({name});"), _ => { assert!(typename.starts_with("gosling_") || typename.starts_with("const gosling_")); assert!(!typename.ends_with("**")); @@ -484,7 +484,7 @@ handlebars_helper!(callJavaCallback: |name: String, return_type: String, input_p "uint32_t" | "gosling_handshake_handle_t" => "J".to_string(), "const char*" => "Ljava/lang/String;".to_string(), "const uint8_t*" | "uint8_t*" => "[B".to_string(), - "int" | "SOCKET" => "Ljava/net/Socket;".to_string(), + "gosling_tcp_socket_t" => "Ljava/net/Socket;".to_string(), "size_t" => { if name.ends_with("_size") || name.ends_with("_length") { continue; @@ -571,7 +571,7 @@ handlebars_helper!(marshallJNIResults: |return_type: String, input_params: Vec

ReleaseByteArrayElements({name}_jni, {name}_jni_buffer, JNI_ABORT);"); cpp_src!("env->DeleteLocalRef({name}_jni);"); } - "int" | "SOCKET" => cpp_src!("env->DeleteLocalRef({name}_jni);"), + "gosling_tcp_socket_t" => cpp_src!("env->DeleteLocalRef({name}_jni);"), _ => { assert!(typename.starts_with("gosling_") || typename.starts_with("const gosling_")); assert!(!typename.ends_with("**")); diff --git a/source/gosling/crates/cgosling/cbindgen.toml b/source/gosling/crates/cgosling/cbindgen.toml index 19203e85..116aa061 100644 --- a/source/gosling/crates/cgosling/cbindgen.toml +++ b/source/gosling/crates/cgosling/cbindgen.toml @@ -33,6 +33,7 @@ prefix_with_name = true # typedefs GoslingHandshakeHandle = "gosling_handshake_handle_t" +GoslingTcpSocket = "gosling_tcp_socket_t" # structs diff --git a/source/gosling/crates/cgosling/src/ffi.rs b/source/gosling/crates/cgosling/src/ffi.rs index d7c00936..2ffb5744 100644 --- a/source/gosling/crates/cgosling/src/ffi.rs +++ b/source/gosling/crates/cgosling/src/ffi.rs @@ -190,6 +190,12 @@ pub struct GoslingTorProvider; pub struct GoslingContext; /// A handle for an in-progress identity handhskae pub type GoslingHandshakeHandle = usize; +#[cfg(any(target_os = "linux", target_os = "macos"))] +/// A native TCP socket handle +pub type GoslingTcpSocket = RawFd; +#[cfg(any(target_os = "windows"))] +/// A native TCP socket handle +pub type GoslingTcpSocket = RawSocket; define_registry! {Ed25519PrivateKey} define_registry! {X25519PrivateKey} @@ -2403,9 +2409,8 @@ pub type GoslingIdentityServerHandshakeFailedCallback = Option< /// the client /// @param channel_name_length: the number of chars in channel_name not including the /// null-terminator -/// @param stream: the tcp socket file descriptor associated with the connection to the +/// @param stream: os-specific tcp socket handle associated with the connection to the /// endpoint server -#[cfg(any(target_os = "linux", target_os = "macos"))] pub type GoslingEndpointClientHandshakeCompletedCallback = Option< extern "C" fn( context: *mut GoslingContext, @@ -2413,32 +2418,7 @@ pub type GoslingEndpointClientHandshakeCompletedCallback = Option< endpoint_service_id: *const GoslingV3OnionServiceId, channel_name: *const c_char, channel_name_length: usize, - stream: RawFd, - ), ->; - -/// The function pointer type for the endpoint client channel request complete callback. -/// This callback is called when the client successfully connects to an endpoint server. -/// -/// @param context: the context associated with this event -/// @param handshake_handle: the handshake handle this callback is associated with -/// @param endpoint_service_id: the onion service id of the endpoint server the client -/// has connected to -/// @param channel_name: the null-terminated name of the channel name requested by the -/// the client -/// @param channel_name_length: the number of chars in channel_name not including the -/// null-terminator -/// @param stream: the tcp SOCKET object associated with the connection to the endpoint -/// server -#[cfg(target_os = "windows")] -pub type GoslingEndpointClientHandshakeCompletedCallback = Option< - extern "C" fn( - context: *mut GoslingContext, - handshake_handle: GoslingHandshakeHandle, - endpoint_service_id: *const GoslingV3OnionServiceId, - channel_name: *const c_char, - channel_name_length: usize, - stream: RawSocket, + stream: GoslingTcpSocket, ), >; @@ -2519,9 +2499,8 @@ pub type GoslingEndpointServerChannelSupportedCallback = Option< /// @param channel_name: the null-terminated name of the channel requested by the client /// @param channel_name_length: the number of chars in channel_name not including the /// null-terminator -/// @param stream: the tcp socket file descriptor associated with the connection to the +/// @param stream:os-specific tcp socket handle associated with the connection to the /// endpoint client -#[cfg(any(target_os = "linux", target_os = "macos"))] pub type GoslingEndpointServerHandshakeCompletedCallback = Option< extern "C" fn( context: *mut GoslingContext, @@ -2530,37 +2509,10 @@ pub type GoslingEndpointServerHandshakeCompletedCallback = Option< client_service_id: *const GoslingV3OnionServiceId, channel_name: *const c_char, channel_name_length: usize, - stream: RawFd, + stream: GoslingTcpSocket, ), >; -/// The function pointer type for the endpoint server handshake completed callback. -/// This callback is called when an endpoint server completes a handshake with an -/// endpoint client. -/// -/// @param context: the context associated with this event -/// @param handshake_handle: the handshake handle this callback is associated with -/// @param endpoint_service_id: the onion service id of the endpoint server the -/// endpoint client has connected to -/// @param client_service_id: the onion service id of the connected endpoint client -/// @param channel_name: the null-terminated name of the channel requested by the client -/// @param channel_name_length: the number of chars in channel_name not including the -/// null-terminator -/// @param stream: the tcp SOCKET object associated with the connection to the endpoint -/// client -#[cfg(target_os = "windows")] -pub type GoslingEndpointServerHandshakeCompletedCallback = Option< - extern "C" fn( - context: *mut GoslingContext, - handshake_handle: GoslingHandshakeHandle, - endpoint_service_id: *const GoslingV3OnionServiceId, - client_service_id: *const GoslingV3OnionServiceId, - channel_name: *const c_char, - channel_name_length: usize, - stream: RawSocket, - ) -> (), ->; - /// The function pointer type of the endpoint server handshake rejected callback. This /// callback is called whenever the endpoint server has rejected an endpoint client's /// handshake.