Skip to content

Commit

Permalink
cgosling: callbacks passing OS-specific TCP sockets handles now use g…
Browse files Browse the repository at this point in the history
…eneric GoslingTcpSocket
  • Loading branch information
Richard Pospesel committed Mar 31, 2024
1 parent 2eb69aa commit e72ce0f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 63 deletions.
10 changes: 5 additions & 5 deletions source/bindings/java/build_java_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ handlebars_helper!(inputParamsToJavaParams: |params: Vec<Param>| {
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(),
Expand All @@ -93,6 +92,7 @@ handlebars_helper!(inputParamsToJavaParams: |params: Vec<Param>| {
"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..]
Expand Down Expand Up @@ -413,10 +413,10 @@ handlebars_helper!(marshallNativeParams: |input_params: Vec<Param>| {
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<jlong>({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<jlong>({name});"),
_ => {
assert!(typename.starts_with("gosling_") || typename.starts_with("const gosling_"));
assert!(!typename.ends_with("**"));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -571,7 +571,7 @@ handlebars_helper!(marshallJNIResults: |return_type: String, input_params: Vec<P
cpp_src!("env->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("**"));
Expand Down
1 change: 1 addition & 0 deletions source/gosling/crates/cgosling/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ prefix_with_name = true

# typedefs
GoslingHandshakeHandle = "gosling_handshake_handle_t"
GoslingTcpSocket = "gosling_tcp_socket_t"

# structs

Expand Down
68 changes: 10 additions & 58 deletions source/gosling/crates/cgosling/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -2403,42 +2409,16 @@ 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,
handshake_handle: GoslingHandshakeHandle,
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,
),
>;

Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit e72ce0f

Please sign in to comment.