Skip to content

Commit

Permalink
implement bail_invalid_handle() macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Pospesel committed Jun 30, 2024
1 parent 9920047 commit 11fe3d6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 45 deletions.
3 changes: 2 additions & 1 deletion source/gosling/crates/cgosling/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use cgosling_proc_macros::*;
use crate::context::*;
use crate::crypto::*;
use crate::error::*;
use crate::macros::*;

#[derive(Default, Clone)]
pub(crate) struct EventCallbacks {
Expand Down Expand Up @@ -514,7 +515,7 @@ macro_rules! impl_callback_setter {
let context = match context_tuple_registry.get_mut($context as usize) {
Some(context) => context,
None => {
bail!("context is invalid");
bail_invalid_handle!(context);
}
};
context.1.[<$callback_type>] = $callback;
Expand Down
44 changes: 22 additions & 22 deletions source/gosling/crates/cgosling/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ pub unsafe extern "C" fn gosling_context_init(
// get our tor provider
let tor_provider = match get_tor_provider_registry().remove(in_tor_provider as usize) {
Some(tor_provider) => tor_provider,
None => bail!("tor_provider is invalid"),
None => bail_invalid_handle!(tor_provider),
};

// get our identity key
let ed25519_private_key_registry = get_ed25519_private_key_registry();
let identity_private_key =
match ed25519_private_key_registry.get(identity_private_key as usize) {
Some(identity_private_key) => identity_private_key,
None => bail!("identity_private_key is invalid"),
None => bail_invalid_handle!(identity_private_key),
};

// construct context
Expand Down Expand Up @@ -137,7 +137,7 @@ pub extern "C" fn gosling_context_bootstrap_tor(
let mut context_tuple_registry = get_context_tuple_registry();
let context = match context_tuple_registry.get_mut(context as usize) {
Some(context) => context,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};
Ok(context.0.bootstrap()?)
});
Expand All @@ -159,7 +159,7 @@ pub extern "C" fn gosling_context_start_identity_server(
let mut context_tuple_registry = get_context_tuple_registry();
let context = match context_tuple_registry.get_mut(context as usize) {
Some(context) => context,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};
Ok(context.0.identity_server_start()?)
});
Expand All @@ -181,7 +181,7 @@ pub extern "C" fn gosling_context_stop_identity_server(
let mut context_tuple_registry = get_context_tuple_registry();
let context = match context_tuple_registry.get_mut(context as usize) {
Some(context) => context,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};
Ok(context.0.identity_server_stop()?)
});
Expand Down Expand Up @@ -221,7 +221,7 @@ pub extern "C" fn gosling_context_start_endpoint_server(
let mut context_tuple_registry = get_context_tuple_registry();
let context = match context_tuple_registry.get_mut(context as usize) {
Some(context) => context,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};

let endpoint_name =
Expand All @@ -235,20 +235,20 @@ pub extern "C" fn gosling_context_start_endpoint_server(
let endpoint_private_key =
match ed25519_private_key_registry.get(endpoint_private_key as usize) {
Some(ed25519_private_key) => ed25519_private_key,
None => bail!("endpoint_private_key is invalid"),
None => bail_invalid_handle!(endpoint_private_key),
};

let v3_onion_service_id_registry = get_v3_onion_service_id_registry();
let client_identity = match v3_onion_service_id_registry.get(client_identity as usize) {
Some(v3_onion_service_id) => v3_onion_service_id,
None => bail!("client_identity is invalid"),
None => bail_invalid_handle!(client_identity),
};

let x25519_public_key_registry = get_x25519_public_key_registry();
let client_auth_public_key =
match x25519_public_key_registry.get(client_auth_public_key as usize) {
Some(x25519_public_key) => x25519_public_key,
None => bail!("client_auth_public_key is invalid"),
None => bail_invalid_handle!(client_auth_public_key),
};

Ok(context.0.endpoint_server_start(
Expand Down Expand Up @@ -279,14 +279,14 @@ pub extern "C" fn gosling_context_stop_endpoint_server(
let mut context_tuple_registry = get_context_tuple_registry();
let context = match context_tuple_registry.get_mut(context as usize) {
Some(context) => context,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};

let ed25519_private_key_registry = get_ed25519_private_key_registry();
let endpoint_private_key =
match ed25519_private_key_registry.get(endpoint_private_key as usize) {
Some(ed25519_private_key) => ed25519_private_key,
None => bail!("endpoint_private_key is invalid"),
None => bail_invalid_handle!(endpoint_private_key),
};

let endpoint_identity = V3OnionServiceId::from_private_key(endpoint_private_key);
Expand Down Expand Up @@ -325,14 +325,14 @@ pub extern "C" fn gosling_context_begin_identity_handshake(
let mut context_tuple_registry = get_context_tuple_registry();
let context = match context_tuple_registry.get_mut(context as usize) {
Some(context) => context,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};

let v3_onion_service_id_registry = get_v3_onion_service_id_registry();
let identity_service_id =
match v3_onion_service_id_registry.get(identity_service_id as usize) {
Some(v3_onion_service_id) => v3_onion_service_id,
None => bail!("identity_service_id is invalid"),
None => bail_invalid_handle!(identity_service_id),
};

let endpoint_name = unsafe {
Expand Down Expand Up @@ -368,7 +368,7 @@ pub extern "C" fn gosling_context_abort_identity_client_handshake(
let mut context_tuple_registry = get_context_tuple_registry();
let context = match context_tuple_registry.get_mut(context as usize) {
Some(context) => context,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};

Ok(context
Expand Down Expand Up @@ -410,21 +410,21 @@ pub extern "C" fn gosling_context_begin_endpoint_handshake(
let mut context_tuple_registry = get_context_tuple_registry();
let context = match context_tuple_registry.get_mut(context as usize) {
Some(context) => context,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};

let v3_onion_service_id_registry = get_v3_onion_service_id_registry();
let endpoint_service_id =
match v3_onion_service_id_registry.get(endpoint_service_id as usize) {
Some(v3_onion_service_id) => v3_onion_service_id,
None => bail!("endpoint_service_id is invalid"),
None => bail_invalid_handle!(endpoint_service_id),
};

let x25519_private_key_registry = get_x25519_private_key_registry();
let client_auth_private_key =
match x25519_private_key_registry.get(client_auth_private_key as usize) {
Some(x25519_private_key) => x25519_private_key,
None => bail!("client_auth_private_key is invalid"),
None => bail_invalid_handle!(client_auth_private_key),
};

let channel_name = unsafe {
Expand Down Expand Up @@ -462,7 +462,7 @@ pub extern "C" fn gosling_context_abort_endpoint_client_handshake(
let mut context_tuple_registry = get_context_tuple_registry();
let context = match context_tuple_registry.get_mut(context as usize) {
Some(context) => context,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};

Ok(context
Expand Down Expand Up @@ -569,7 +569,7 @@ fn handle_context_event(
Some(context) => context
.0
.identity_client_handle_challenge_received(handle, challenge_response)?,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};
}
ContextEvent::IdentityClientHandshakeCompleted {
Expand Down Expand Up @@ -707,7 +707,7 @@ fn handle_context_event(
endpoint_supported,
endpoint_challenge,
)?,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};
}
ContextEvent::IdentityServerChallengeResponseReceived {
Expand Down Expand Up @@ -742,7 +742,7 @@ fn handle_context_event(
handle,
challenge_response_valid,
)?,
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};
}
ContextEvent::IdentityServerHandshakeCompleted {
Expand Down Expand Up @@ -1035,7 +1035,7 @@ pub extern "C" fn gosling_context_poll_events(
};
(context_events, callbacks)
}
None => bail!("context is invalid"),
None => bail_invalid_handle!(context),
};

// consume the events and trigger any callbacks
Expand Down
18 changes: 9 additions & 9 deletions source/gosling/crates/cgosling/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub unsafe extern "C" fn gosling_ed25519_private_key_clone(

let private_key = match get_ed25519_private_key_registry().get(private_key as usize) {
Some(private_key) => private_key.clone(),
None => bail!("private key is invalid"),
None => bail_invalid_handle!(private_key),
};
let handle = get_ed25519_private_key_registry().insert(private_key);
*out_private_key = handle as *mut GoslingEd25519PrivateKey;
Expand All @@ -118,7 +118,7 @@ pub unsafe extern "C" fn gosling_x25519_public_key_clone(

let public_key = match get_x25519_public_key_registry().get(public_key as usize) {
Some(public_key) => public_key.clone(),
None => bail!("public key is invalid"),
None => bail_invalid_handle!(public_key),
};
let handle = get_x25519_public_key_registry().insert(public_key);
*out_public_key = handle as *mut GoslingX25519PublicKey;
Expand Down Expand Up @@ -146,7 +146,7 @@ pub unsafe extern "C" fn gosling_x25519_private_key_clone(

let private_key = match get_x25519_private_key_registry().get(private_key as usize) {
Some(private_key) => private_key.clone(),
None => bail!("private key is invalid"),
None => bail_invalid_handle!(private_key),
};
let handle = get_x25519_private_key_registry().insert(private_key);
*out_private_key = handle as *mut GoslingX25519PrivateKey;
Expand All @@ -173,7 +173,7 @@ pub unsafe extern "C" fn gosling_v3_onion_service_id_clone(

let service_id = match get_v3_onion_service_id_registry().get(service_id as usize) {
Some(service_id) => service_id.clone(),
None => bail!("service_id is invalid"),
None => bail_invalid_handle!(service_id),
};
let handle = get_v3_onion_service_id_registry().insert(service_id);
*out_service_id = handle as *mut GoslingV3OnionServiceId;
Expand Down Expand Up @@ -289,7 +289,7 @@ pub extern "C" fn gosling_ed25519_private_key_to_keyblob(
};
}
None => {
bail!("private_key is invalid");
bail_invalid_handle!(private_key);
}
};

Expand Down Expand Up @@ -381,7 +381,7 @@ pub extern "C" fn gosling_x25519_private_key_to_base64(
};
}
None => {
bail!("private_key is invalid");
bail_invalid_handle!(private_key);
}
};

Expand Down Expand Up @@ -477,7 +477,7 @@ pub extern "C" fn gosling_x25519_public_key_to_base32(
};
}
None => {
bail!("public_key is invalid");
bail_invalid_handle!(public_key);
}
};

Expand Down Expand Up @@ -547,7 +547,7 @@ pub unsafe extern "C" fn gosling_v3_onion_service_id_from_ed25519_private_key(
let ed25519_private_key =
match ed25519_private_key_registry.get(ed25519_private_key as usize) {
Some(ed25519_private_key) => ed25519_private_key,
None => bail!("ed25519_private_key is invalid"),
None => bail_invalid_handle!(ed25519_private_key),
};
V3OnionServiceId::from_private_key(ed25519_private_key)
};
Expand Down Expand Up @@ -607,7 +607,7 @@ pub extern "C" fn gosling_v3_onion_service_id_to_string(
};
}
None => {
bail!("service_id is invalid");
bail_invalid_handle!(service_id);
}
};

Expand Down
2 changes: 1 addition & 1 deletion source/gosling/crates/cgosling/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub unsafe extern "C" fn gosling_error_clone(

let orig_error = match get_error_registry().get(orig_error as usize) {
Some(orig_error) => orig_error.clone(),
None => bail!("error is invalid"),
None => bail_invalid_handle!(orig_error),
};
let handle = get_error_registry().insert(orig_error);
*out_error = handle as *mut GoslingError;
Expand Down
10 changes: 10 additions & 0 deletions source/gosling/crates/cgosling/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,13 @@ macro_rules! ensure_not_null {
}
}
pub(crate) use ensure_not_null;

// bail because handle invalid
macro_rules! bail_invalid_handle {
($handle:ident) => {
paste::paste! {
bail!(stringify!([<$handle>] is invalid))
}
}
}
pub(crate) use bail_invalid_handle;
6 changes: 3 additions & 3 deletions source/gosling/crates/cgosling/src/tor_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ pub unsafe extern "C" fn gosling_tor_provider_config_new_system_legacy_client_co
// constructor tor_socks_addr
let tor_socks_host = match get_ip_addr_registry().get(tor_socks_host as usize) {
Some(tor_socks_host) => tor_socks_host.clone(),
None => bail!("tor_socks_host is invalid"),
None => bail_invalid_handle!(tor_socks_host),
};
let tor_socks_addr = std::net::SocketAddr::new(tor_socks_host, tor_socks_port);

// construct tor_control_addr
let tor_control_host = match get_ip_addr_registry().get(tor_control_host as usize) {
Some(tor_control_host) => tor_control_host.clone(),
None => bail!("tor_control_host is invalid"),
None => bail_invalid_handle!(tor_control_host),
};
let tor_control_addr = std::net::SocketAddr::new(tor_control_host, tor_control_port);

Expand Down Expand Up @@ -242,7 +242,7 @@ pub unsafe extern "C" fn gosling_tor_provider_from_tor_provider_config(
Box::new(tor_provider)
},
},
None => bail!("tor_provider_config is invalid"),
None => bail_invalid_handle!(tor_provider_config),
};

let handle = get_tor_provider_registry().insert(tor_provider);
Expand Down
Loading

0 comments on commit 11fe3d6

Please sign in to comment.