X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fonion_message%2Fmessenger.rs;h=0c989038b718922f4fd92c1ce204e4b1877ebaef;hb=7ebc93258c70a014e9129c0b464256d2ee670751;hp=66555a3d023c6d8d7c3eee77c9b666a8bebf77d7;hpb=c809fde8131b3ffdb6e5c73709e85d918a87c87d;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/onion_message/messenger.rs b/lightning-c-bindings/src/lightning/onion_message/messenger.rs index 66555a3..0c98903 100644 --- a/lightning-c-bindings/src/lightning/onion_message/messenger.rs +++ b/lightning-c-bindings/src/lightning/onion_message/messenger.rs @@ -19,7 +19,7 @@ use alloc::{vec::Vec, boxed::Box}; use lightning::onion_message::messenger::OnionMessenger as nativeOnionMessengerImport; -pub(crate) type nativeOnionMessenger = nativeOnionMessengerImport; +pub(crate) type nativeOnionMessenger = nativeOnionMessengerImport; /// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be /// used to retrieve invoices and fulfill invoice requests from [offers]. Currently, only sending @@ -31,20 +31,25 @@ pub(crate) type nativeOnionMessenger = nativeOnionMessengerImport, destination: Destination) -> Result { +/// # unimplemented!() +/// # } +/// # } /// # let seed = [42u8; 32]; /// # let time = Duration::from_secs(123456); /// # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos()); @@ -54,10 +59,15 @@ pub(crate) type nativeOnionMessenger = nativeOnionMessengerImport @@ -120,7 +138,7 @@ pub extern "C" fn OnionMessenger_free(this_obj: OnionMessenger) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn OnionMessenger_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeOnionMessenger); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeOnionMessenger) }; } #[allow(unused)] impl OnionMessenger { @@ -138,21 +156,286 @@ impl OnionMessenger { ret } } +/// A trait defining behavior for routing an [`OnionMessage`]. +/// +/// [`OnionMessage`]: msgs::OnionMessage +#[repr(C)] +pub struct MessageRouter { + /// An opaque pointer which is passed to your function implementations as an argument. + /// This has no meaning in the LDK, and can be NULL or any other value. + pub this_arg: *mut c_void, + /// Returns a route for sending an [`OnionMessage`] to the given [`Destination`]. + /// + /// [`OnionMessage`]: msgs::OnionMessage + pub find_path: extern "C" fn (this_arg: *const c_void, sender: crate::c_types::PublicKey, peers: crate::c_types::derived::CVec_PublicKeyZ, destination: crate::lightning::onion_message::messenger::Destination) -> crate::c_types::derived::CResult_OnionMessagePathNoneZ, + /// Frees any resources associated with this object given its this_arg pointer. + /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed. + pub free: Option, +} +unsafe impl Send for MessageRouter {} +unsafe impl Sync for MessageRouter {} +#[no_mangle] +pub(crate) extern "C" fn MessageRouter_clone_fields(orig: &MessageRouter) -> MessageRouter { + MessageRouter { + this_arg: orig.this_arg, + find_path: Clone::clone(&orig.find_path), + free: Clone::clone(&orig.free), + } +} + +use lightning::onion_message::messenger::MessageRouter as rustMessageRouter; +impl rustMessageRouter for MessageRouter { + fn find_path(&self, mut sender: bitcoin::secp256k1::PublicKey, mut peers: Vec, mut destination: lightning::onion_message::messenger::Destination) -> Result { + let mut local_peers = Vec::new(); for mut item in peers.drain(..) { local_peers.push( { crate::c_types::PublicKey::from_rust(&item) }); }; + let mut ret = (self.find_path)(self.this_arg, crate::c_types::PublicKey::from_rust(&sender), local_peers.into(), crate::lightning::onion_message::messenger::Destination::native_into(destination)); + let mut local_ret = match ret.result_ok { true => Ok( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).take_inner()) } }), false => Err( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) })*/ })}; + local_ret + } +} + +// We're essentially a pointer already, or at least a set of pointers, so allow us to be used +// directly as a Deref trait in higher-level structs: +impl core::ops::Deref for MessageRouter { + type Target = Self; + fn deref(&self) -> &Self { + self + } +} +/// Calls the free function if one is set +#[no_mangle] +pub extern "C" fn MessageRouter_free(this_ptr: MessageRouter) { } +impl Drop for MessageRouter { + fn drop(&mut self) { + if let Some(f) = self.free { + f(self.this_arg); + } + } +} + +use lightning::onion_message::messenger::DefaultMessageRouter as nativeDefaultMessageRouterImport; +pub(crate) type nativeDefaultMessageRouter = nativeDefaultMessageRouterImport; + +/// A [`MessageRouter`] that always fails. +#[must_use] +#[repr(C)] +pub struct DefaultMessageRouter { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeDefaultMessageRouter, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for DefaultMessageRouter { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeDefaultMessageRouter>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; + } + } +} +/// Frees any resources used by the DefaultMessageRouter, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn DefaultMessageRouter_free(this_obj: DefaultMessageRouter) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn DefaultMessageRouter_free_void(this_ptr: *mut c_void) { + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeDefaultMessageRouter) }; +} +#[allow(unused)] +impl DefaultMessageRouter { + pub(crate) fn get_native_ref(&self) -> &'static nativeDefaultMessageRouter { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeDefaultMessageRouter { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy + pub(crate) fn take_inner(mut self) -> *mut nativeDefaultMessageRouter { + assert!(self.is_owned); + let ret = ObjOps::untweak_ptr(self.inner); + self.inner = core::ptr::null_mut(); + ret + } +} +/// Constructs a new DefaultMessageRouter given each field +#[must_use] +#[no_mangle] +pub extern "C" fn DefaultMessageRouter_new() -> DefaultMessageRouter { + DefaultMessageRouter { inner: ObjOps::heap_alloc(lightning::onion_message::messenger::DefaultMessageRouter {}), is_owned: true } +} +impl From for crate::lightning::onion_message::messenger::MessageRouter { + fn from(obj: nativeDefaultMessageRouter) -> Self { + let mut rust_obj = DefaultMessageRouter { inner: ObjOps::heap_alloc(obj), is_owned: true }; + let mut ret = DefaultMessageRouter_as_MessageRouter(&rust_obj); + // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn + rust_obj.inner = core::ptr::null_mut(); + ret.free = Some(DefaultMessageRouter_free_void); + ret + } +} +/// Constructs a new MessageRouter which calls the relevant methods on this_arg. +/// This copies the `inner` pointer in this_arg and thus the returned MessageRouter must be freed before this_arg is +#[no_mangle] +pub extern "C" fn DefaultMessageRouter_as_MessageRouter(this_arg: &DefaultMessageRouter) -> crate::lightning::onion_message::messenger::MessageRouter { + crate::lightning::onion_message::messenger::MessageRouter { + this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }, + free: None, + find_path: DefaultMessageRouter_MessageRouter_find_path, + } +} + +#[must_use] +extern "C" fn DefaultMessageRouter_MessageRouter_find_path(this_arg: *const c_void, mut sender: crate::c_types::PublicKey, mut peers: crate::c_types::derived::CVec_PublicKeyZ, mut destination: crate::lightning::onion_message::messenger::Destination) -> crate::c_types::derived::CResult_OnionMessagePathNoneZ { + let mut local_peers = Vec::new(); for mut item in peers.into_rust().drain(..) { local_peers.push( { item.into_rust() }); }; + let mut ret = >::find_path(unsafe { &mut *(this_arg as *mut nativeDefaultMessageRouter) }, sender.into_rust(), local_peers, destination.into_native()); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::messenger::OnionMessagePath { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() }; + local_ret +} + + +use lightning::onion_message::messenger::OnionMessagePath as nativeOnionMessagePathImport; +pub(crate) type nativeOnionMessagePath = nativeOnionMessagePathImport; + +/// A path for sending an [`msgs::OnionMessage`]. +#[must_use] +#[repr(C)] +pub struct OnionMessagePath { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeOnionMessagePath, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for OnionMessagePath { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeOnionMessagePath>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; + } + } +} +/// Frees any resources used by the OnionMessagePath, if is_owned is set and inner is non-NULL. +#[no_mangle] +pub extern "C" fn OnionMessagePath_free(this_obj: OnionMessagePath) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn OnionMessagePath_free_void(this_ptr: *mut c_void) { + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeOnionMessagePath) }; +} +#[allow(unused)] +impl OnionMessagePath { + pub(crate) fn get_native_ref(&self) -> &'static nativeOnionMessagePath { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeOnionMessagePath { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy + pub(crate) fn take_inner(mut self) -> *mut nativeOnionMessagePath { + assert!(self.is_owned); + let ret = ObjOps::untweak_ptr(self.inner); + self.inner = core::ptr::null_mut(); + ret + } +} +/// Nodes on the path between the sender and the destination. +/// +/// Returns a copy of the field. +#[no_mangle] +pub extern "C" fn OnionMessagePath_get_intermediate_nodes(this_ptr: &OnionMessagePath) -> crate::c_types::derived::CVec_PublicKeyZ { + let mut inner_val = this_ptr.get_native_mut_ref().intermediate_nodes.clone(); + let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { crate::c_types::PublicKey::from_rust(&item) }); }; + local_inner_val.into() +} +/// Nodes on the path between the sender and the destination. +#[no_mangle] +pub extern "C" fn OnionMessagePath_set_intermediate_nodes(this_ptr: &mut OnionMessagePath, mut val: crate::c_types::derived::CVec_PublicKeyZ) { + let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item.into_rust() }); }; + unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.intermediate_nodes = local_val; +} +/// The recipient of the message. +#[no_mangle] +pub extern "C" fn OnionMessagePath_get_destination(this_ptr: &OnionMessagePath) -> crate::lightning::onion_message::messenger::Destination { + let mut inner_val = &mut this_ptr.get_native_mut_ref().destination; + crate::lightning::onion_message::messenger::Destination::from_native(inner_val) +} +/// The recipient of the message. +#[no_mangle] +pub extern "C" fn OnionMessagePath_set_destination(this_ptr: &mut OnionMessagePath, mut val: crate::lightning::onion_message::messenger::Destination) { + unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.destination = val.into_native(); +} +/// Constructs a new OnionMessagePath given each field +#[must_use] +#[no_mangle] +pub extern "C" fn OnionMessagePath_new(mut intermediate_nodes_arg: crate::c_types::derived::CVec_PublicKeyZ, mut destination_arg: crate::lightning::onion_message::messenger::Destination) -> OnionMessagePath { + let mut local_intermediate_nodes_arg = Vec::new(); for mut item in intermediate_nodes_arg.into_rust().drain(..) { local_intermediate_nodes_arg.push( { item.into_rust() }); }; + OnionMessagePath { inner: ObjOps::heap_alloc(nativeOnionMessagePath { + intermediate_nodes: local_intermediate_nodes_arg, + destination: destination_arg.into_native(), + }), is_owned: true } +} +impl Clone for OnionMessagePath { + fn clone(&self) -> Self { + Self { + inner: if <*mut nativeOnionMessagePath>::is_null(self.inner) { core::ptr::null_mut() } else { + ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) }, + is_owned: true, + } + } +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn OnionMessagePath_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeOnionMessagePath)).clone() })) as *mut c_void +} +#[no_mangle] +/// Creates a copy of the OnionMessagePath +pub extern "C" fn OnionMessagePath_clone(orig: &OnionMessagePath) -> OnionMessagePath { + orig.clone() +} /// The destination of an onion message. +#[derive(Clone)] #[must_use] #[repr(C)] pub enum Destination { /// We're sending this onion message to a node. Node( crate::c_types::PublicKey), - /// We're sending this onion message to a blinded route. - BlindedRoute( - crate::lightning::onion_message::blinded_route::BlindedRoute), + /// We're sending this onion message to a blinded path. + BlindedPath( + crate::lightning::blinded_path::BlindedPath), } use lightning::onion_message::messenger::Destination as DestinationImport; pub(crate) type nativeDestination = DestinationImport; impl Destination { + #[allow(unused)] + pub(crate) fn to_native(&self) -> nativeDestination { + match self { + Destination::Node (ref a, ) => { + let mut a_nonref = Clone::clone(a); + nativeDestination::Node ( + a_nonref.into_rust(), + ) + }, + Destination::BlindedPath (ref a, ) => { + let mut a_nonref = Clone::clone(a); + nativeDestination::BlindedPath ( + *unsafe { Box::from_raw(a_nonref.take_inner()) }, + ) + }, + } + } #[allow(unused)] pub(crate) fn into_native(self) -> nativeDestination { match self { @@ -161,14 +444,31 @@ impl Destination { a.into_rust(), ) }, - Destination::BlindedRoute (mut a, ) => { - nativeDestination::BlindedRoute ( + Destination::BlindedPath (mut a, ) => { + nativeDestination::BlindedPath ( *unsafe { Box::from_raw(a.take_inner()) }, ) }, } } #[allow(unused)] + pub(crate) fn from_native(native: &nativeDestination) -> Self { + match native { + nativeDestination::Node (ref a, ) => { + let mut a_nonref = Clone::clone(a); + Destination::Node ( + crate::c_types::PublicKey::from_rust(&a_nonref), + ) + }, + nativeDestination::BlindedPath (ref a, ) => { + let mut a_nonref = Clone::clone(a); + Destination::BlindedPath ( + crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(a_nonref), is_owned: true }, + ) + }, + } + } + #[allow(unused)] pub(crate) fn native_into(native: nativeDestination) -> Self { match native { nativeDestination::Node (mut a, ) => { @@ -176,9 +476,9 @@ impl Destination { crate::c_types::PublicKey::from_rust(&a), ) }, - nativeDestination::BlindedRoute (mut a, ) => { - Destination::BlindedRoute ( - crate::lightning::onion_message::blinded_route::BlindedRoute { inner: ObjOps::heap_alloc(a), is_owned: true }, + nativeDestination::BlindedPath (mut a, ) => { + Destination::BlindedPath ( + crate::lightning::blinded_path::BlindedPath { inner: ObjOps::heap_alloc(a), is_owned: true }, ) }, } @@ -187,19 +487,24 @@ impl Destination { /// Frees any resources used by the Destination #[no_mangle] pub extern "C" fn Destination_free(this_ptr: Destination) { } +/// Creates a copy of the Destination +#[no_mangle] +pub extern "C" fn Destination_clone(orig: &Destination) -> Destination { + orig.clone() +} #[no_mangle] /// Utility method to constructs a new Node-variant Destination pub extern "C" fn Destination_node(a: crate::c_types::PublicKey) -> Destination { Destination::Node(a, ) } #[no_mangle] -/// Utility method to constructs a new BlindedRoute-variant Destination -pub extern "C" fn Destination_blinded_route(a: crate::lightning::onion_message::blinded_route::BlindedRoute) -> Destination { - Destination::BlindedRoute(a, ) +/// Utility method to constructs a new BlindedPath-variant Destination +pub extern "C" fn Destination_blinded_path(a: crate::lightning::blinded_path::BlindedPath) -> Destination { + Destination::BlindedPath(a, ) } /// Errors that may occur when [sending an onion message]. /// -/// [sending an onion message]: OnionMessenger::send_custom_onion_message +/// [sending an onion message]: OnionMessenger::send_onion_message #[derive(Clone)] #[must_use] #[repr(C)] @@ -210,7 +515,7 @@ pub enum SendError { /// Because implementations such as Eclair will drop onion messages where the message packet /// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size. TooBigPacket, - /// The provided [`Destination`] was an invalid [`BlindedRoute`], due to having fewer than two + /// The provided [`Destination`] was an invalid [`BlindedPath`], due to having fewer than two /// blinded hops. TooFewBlindedHops, /// Our next-hop peer was offline or does not support onion message forwarding. @@ -219,6 +524,15 @@ pub enum SendError { InvalidMessage, /// Our next-hop peer's buffer was full or our total outbound buffer was full. BufferFull, + /// Failed to retrieve our node id from the provided [`NodeSigner`]. + /// + /// [`NodeSigner`]: crate::sign::NodeSigner + GetNodeIdFailed, + /// We attempted to send to a blinded path where we are the introduction node, and failed to + /// advance the blinded path to make the second hop the new introduction node. Either + /// [`NodeSigner::ecdh`] failed, we failed to tweak the current blinding point to get the + /// new blinding point, or we were attempting to send to ourselves. + BlindedPathAdvanceFailed, } use lightning::onion_message::messenger::SendError as SendErrorImport; pub(crate) type nativeSendError = SendErrorImport; @@ -228,7 +542,7 @@ impl SendError { pub(crate) fn to_native(&self) -> nativeSendError { match self { SendError::Secp256k1 (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); nativeSendError::Secp256k1 ( a_nonref.into_rust(), ) @@ -238,6 +552,8 @@ impl SendError { SendError::InvalidFirstHop => nativeSendError::InvalidFirstHop, SendError::InvalidMessage => nativeSendError::InvalidMessage, SendError::BufferFull => nativeSendError::BufferFull, + SendError::GetNodeIdFailed => nativeSendError::GetNodeIdFailed, + SendError::BlindedPathAdvanceFailed => nativeSendError::BlindedPathAdvanceFailed, } } #[allow(unused)] @@ -253,13 +569,15 @@ impl SendError { SendError::InvalidFirstHop => nativeSendError::InvalidFirstHop, SendError::InvalidMessage => nativeSendError::InvalidMessage, SendError::BufferFull => nativeSendError::BufferFull, + SendError::GetNodeIdFailed => nativeSendError::GetNodeIdFailed, + SendError::BlindedPathAdvanceFailed => nativeSendError::BlindedPathAdvanceFailed, } } #[allow(unused)] pub(crate) fn from_native(native: &nativeSendError) -> Self { match native { nativeSendError::Secp256k1 (ref a, ) => { - let mut a_nonref = (*a).clone(); + let mut a_nonref = Clone::clone(a); SendError::Secp256k1 ( crate::c_types::Secp256k1Error::from_rust(a_nonref), ) @@ -269,6 +587,8 @@ impl SendError { nativeSendError::InvalidFirstHop => SendError::InvalidFirstHop, nativeSendError::InvalidMessage => SendError::InvalidMessage, nativeSendError::BufferFull => SendError::BufferFull, + nativeSendError::GetNodeIdFailed => SendError::GetNodeIdFailed, + nativeSendError::BlindedPathAdvanceFailed => SendError::BlindedPathAdvanceFailed, } } #[allow(unused)] @@ -284,6 +604,8 @@ impl SendError { nativeSendError::InvalidFirstHop => SendError::InvalidFirstHop, nativeSendError::InvalidMessage => SendError::InvalidMessage, nativeSendError::BufferFull => SendError::BufferFull, + nativeSendError::GetNodeIdFailed => SendError::GetNodeIdFailed, + nativeSendError::BlindedPathAdvanceFailed => SendError::BlindedPathAdvanceFailed, } } } @@ -320,6 +642,14 @@ pub extern "C" fn SendError_invalid_message() -> SendError { /// Utility method to constructs a new BufferFull-variant SendError pub extern "C" fn SendError_buffer_full() -> SendError { SendError::BufferFull} +#[no_mangle] +/// Utility method to constructs a new GetNodeIdFailed-variant SendError +pub extern "C" fn SendError_get_node_id_failed() -> SendError { + SendError::GetNodeIdFailed} +#[no_mangle] +/// Utility method to constructs a new BlindedPathAdvanceFailed-variant SendError +pub extern "C" fn SendError_blinded_path_advance_failed() -> SendError { + SendError::BlindedPathAdvanceFailed} /// Checks if two SendErrors contain equal inner contents. /// This ignores pointers and is_owned flags and looks at the values in fields. #[no_mangle] @@ -341,11 +671,10 @@ pub struct CustomOnionMessageHandler { /// An opaque pointer which is passed to your function implementations as an argument. /// This has no meaning in the LDK, and can be NULL or any other value. pub this_arg: *mut c_void, - /// Called with the custom message that was received. - pub handle_custom_message: extern "C" fn (this_arg: *const c_void, msg: crate::lightning::onion_message::packet::CustomOnionMessageContents), + /// Called with the custom message that was received, returning a response to send, if any. + pub handle_custom_message: extern "C" fn (this_arg: *const c_void, msg: crate::lightning::onion_message::packet::CustomOnionMessageContents) -> crate::c_types::derived::COption_CustomOnionMessageContentsZ, /// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the /// message type is unknown. - #[must_use] pub read_custom_message: extern "C" fn (this_arg: *const c_void, message_type: u64, buffer: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_CustomOnionMessageContentsZDecodeErrorZ, /// Frees any resources associated with this object given its this_arg pointer. /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed. @@ -366,12 +695,14 @@ pub(crate) extern "C" fn CustomOnionMessageHandler_clone_fields(orig: &CustomOni use lightning::onion_message::messenger::CustomOnionMessageHandler as rustCustomOnionMessageHandler; impl rustCustomOnionMessageHandler for CustomOnionMessageHandler { type CustomMessage = crate::lightning::onion_message::packet::CustomOnionMessageContents; - fn handle_custom_message(&self, mut msg: crate::lightning::onion_message::packet::CustomOnionMessageContents) { - (self.handle_custom_message)(self.this_arg, Into::into(msg)) + fn handle_custom_message(&self, mut msg: crate::lightning::onion_message::packet::CustomOnionMessageContents) -> Option { + let mut ret = (self.handle_custom_message)(self.this_arg, Into::into(msg)); + let mut local_ret = { /*ret*/ let ret_opt = ret; if ret_opt.is_none() { None } else { Some({ { { ret_opt.take() } }})} }; + local_ret } fn read_custom_message(&self, mut message_type: u64, mut buffer: &mut R) -> Result, lightning::ln::msgs::DecodeError> { let mut ret = (self.read_custom_message)(self.this_arg, message_type, crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec(buffer))); - let mut local_ret = match ret.result_ok { true => Ok( { let mut local_ret_0 = { /* (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ let ret_0_opt = (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }); { } if ret_0_opt.is_none() { None } else { Some({ ret_0_opt.take() }) } }; local_ret_0 }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })}; + let mut local_ret = match ret.result_ok { true => Ok( { let mut local_ret_0 = { /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ let ret_0_opt = (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }); if ret_0_opt.is_none() { None } else { Some({ { { ret_0_opt.take() } }})} }; local_ret_0 }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).into_native() })}; local_ret } } @@ -398,21 +729,21 @@ impl Drop for CustomOnionMessageHandler { /// their respective handlers. #[must_use] #[no_mangle] -pub extern "C" fn OnionMessenger_new(mut keys_manager: crate::lightning::chain::keysinterface::KeysInterface, mut logger: crate::lightning::util::logger::Logger, mut custom_handler: crate::lightning::onion_message::messenger::CustomOnionMessageHandler) -> crate::lightning::onion_message::messenger::OnionMessenger { - let mut ret = lightning::onion_message::messenger::OnionMessenger::new(keys_manager, logger, custom_handler); +pub extern "C" fn OnionMessenger_new(mut entropy_source: crate::lightning::sign::EntropySource, mut node_signer: crate::lightning::sign::NodeSigner, mut logger: crate::lightning::util::logger::Logger, mut message_router: crate::lightning::onion_message::messenger::MessageRouter, mut offers_handler: crate::lightning::onion_message::offers::OffersMessageHandler, mut custom_handler: crate::lightning::onion_message::messenger::CustomOnionMessageHandler) -> crate::lightning::onion_message::messenger::OnionMessenger { + let mut ret = lightning::onion_message::messenger::OnionMessenger::new(entropy_source, node_signer, logger, message_router, offers_handler, custom_handler); crate::lightning::onion_message::messenger::OnionMessenger { inner: ObjOps::heap_alloc(ret), is_owned: true } } -/// Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`. +/// Send an onion message with contents `message` to the destination of `path`. +/// /// See [`OnionMessenger`] for example usage. /// /// Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None #[must_use] #[no_mangle] -pub extern "C" fn OnionMessenger_send_custom_onion_message(this_arg: &crate::lightning::onion_message::messenger::OnionMessenger, mut intermediate_nodes: crate::c_types::derived::CVec_PublicKeyZ, mut destination: crate::lightning::onion_message::messenger::Destination, mut msg: crate::lightning::onion_message::packet::CustomOnionMessageContents, mut reply_path: crate::lightning::onion_message::blinded_route::BlindedRoute) -> crate::c_types::derived::CResult_NoneSendErrorZ { - let mut local_intermediate_nodes = Vec::new(); for mut item in intermediate_nodes.into_rust().drain(..) { local_intermediate_nodes.push( { item.into_rust() }); }; +pub extern "C" fn OnionMessenger_send_onion_message(this_arg: &crate::lightning::onion_message::messenger::OnionMessenger, mut path: crate::lightning::onion_message::messenger::OnionMessagePath, mut message: crate::lightning::onion_message::packet::OnionMessageContents, mut reply_path: crate::lightning::blinded_path::BlindedPath) -> crate::c_types::derived::CResult_NoneSendErrorZ { let mut local_reply_path = if reply_path.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(reply_path.take_inner()) } }) }; - let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.send_custom_onion_message(&local_intermediate_nodes[..], destination.into_native(), msg, local_reply_path); + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.send_onion_message(*unsafe { Box::from_raw(path.take_inner()) }, message.into_native(), local_reply_path); let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::onion_message::messenger::SendError::native_into(e) }).into() }; local_ret } @@ -439,7 +770,7 @@ pub extern "C" fn OnionMessenger_as_OnionMessageHandler(this_arg: &OnionMessenge peer_disconnected: OnionMessenger_OnionMessageHandler_peer_disconnected, provided_node_features: OnionMessenger_OnionMessageHandler_provided_node_features, provided_init_features: OnionMessenger_OnionMessageHandler_provided_init_features, - OnionMessageProvider: crate::lightning::util::events::OnionMessageProvider { + OnionMessageProvider: crate::lightning::events::OnionMessageProvider { this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }, free: None, next_onion_message_for_peer: OnionMessenger_OnionMessageProvider_next_onion_message_for_peer, @@ -451,13 +782,13 @@ extern "C" fn OnionMessenger_OnionMessageHandler_handle_onion_message(this_arg: >::handle_onion_message(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &peer_node_id.into_rust(), msg.get_native_ref()) } #[must_use] -extern "C" fn OnionMessenger_OnionMessageHandler_peer_connected(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, init: &crate::lightning::ln::msgs::Init) -> crate::c_types::derived::CResult_NoneNoneZ { - let mut ret = >::peer_connected(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust(), init.get_native_ref()); +extern "C" fn OnionMessenger_OnionMessageHandler_peer_connected(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, init: &crate::lightning::ln::msgs::Init, mut inbound: bool) -> crate::c_types::derived::CResult_NoneNoneZ { + let mut ret = >::peer_connected(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust(), init.get_native_ref(), inbound); let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() }; local_ret } -extern "C" fn OnionMessenger_OnionMessageHandler_peer_disconnected(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, mut no_connection_possible: bool) { - >::peer_disconnected(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust(), no_connection_possible) +extern "C" fn OnionMessenger_OnionMessageHandler_peer_disconnected(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey) { + >::peer_disconnected(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust()) } #[must_use] extern "C" fn OnionMessenger_OnionMessageHandler_provided_node_features(this_arg: *const c_void) -> crate::lightning::ln::features::NodeFeatures { @@ -470,7 +801,7 @@ extern "C" fn OnionMessenger_OnionMessageHandler_provided_init_features(this_arg crate::lightning::ln::features::InitFeatures { inner: ObjOps::heap_alloc(ret), is_owned: true } } -impl From for crate::lightning::util::events::OnionMessageProvider { +impl From for crate::lightning::events::OnionMessageProvider { fn from(obj: nativeOnionMessenger) -> Self { let mut rust_obj = OnionMessenger { inner: ObjOps::heap_alloc(obj), is_owned: true }; let mut ret = OnionMessenger_as_OnionMessageProvider(&rust_obj); @@ -483,8 +814,8 @@ impl From for crate::lightning::util::events::OnionMessage /// Constructs a new OnionMessageProvider which calls the relevant methods on this_arg. /// This copies the `inner` pointer in this_arg and thus the returned OnionMessageProvider must be freed before this_arg is #[no_mangle] -pub extern "C" fn OnionMessenger_as_OnionMessageProvider(this_arg: &OnionMessenger) -> crate::lightning::util::events::OnionMessageProvider { - crate::lightning::util::events::OnionMessageProvider { +pub extern "C" fn OnionMessenger_as_OnionMessageProvider(this_arg: &OnionMessenger) -> crate::lightning::events::OnionMessageProvider { + crate::lightning::events::OnionMessageProvider { this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }, free: None, next_onion_message_for_peer: OnionMessenger_OnionMessageProvider_next_onion_message_for_peer, @@ -493,7 +824,7 @@ pub extern "C" fn OnionMessenger_as_OnionMessageProvider(this_arg: &OnionMesseng #[must_use] extern "C" fn OnionMessenger_OnionMessageProvider_next_onion_message_for_peer(this_arg: *const c_void, mut peer_node_id: crate::c_types::PublicKey) -> crate::lightning::ln::msgs::OnionMessage { - let mut ret = >::next_onion_message_for_peer(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, peer_node_id.into_rust()); + let mut ret = >::next_onion_message_for_peer(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, peer_node_id.into_rust()); let mut local_ret = crate::lightning::ln::msgs::OnionMessage { inner: if ret.is_none() { core::ptr::null_mut() } else { { ObjOps::heap_alloc((ret.unwrap())) } }, is_owned: true }; local_ret }