X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fonion_message%2Fmessenger.rs;fp=lightning-c-bindings%2Fsrc%2Flightning%2Fonion_message%2Fmessenger.rs;h=5ef9665afb5f275b3b1a39d7a4616c6d34d89d42;hp=4a0e6005521098892efd40527354d551600c5de9;hb=5502fccd64611f16f5ebba3759c68b1a4d69b537;hpb=1ec49fac7433e5b8693c3858a3e25998bc888eeb diff --git a/lightning-c-bindings/src/lightning/onion_message/messenger.rs b/lightning-c-bindings/src/lightning/onion_message/messenger.rs index 4a0e600..5ef9665 100644 --- a/lightning-c-bindings/src/lightning/onion_message/messenger.rs +++ b/lightning-c-bindings/src/lightning/onion_message/messenger.rs @@ -22,9 +22,21 @@ use alloc::{vec::Vec, boxed::Box}; use lightning::onion_message::messenger::OnionMessenger as 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 -/// and receiving custom onion messages is supported. +/// A sender, receiver and forwarder of [`OnionMessage`]s. +/// +/// # Handling Messages +/// +/// `OnionMessenger` implements [`OnionMessageHandler`], making it responsible for either forwarding +/// messages to peers or delegating to the appropriate handler for the message type. Currently, the +/// available handlers are: +/// * [`OffersMessageHandler`], for responding to [`InvoiceRequest`]s and paying [`Bolt12Invoice`]s +/// * [`CustomOnionMessageHandler`], for handling user-defined message types +/// +/// # Sending Messages +/// +/// [`OnionMessage`]s are sent initially using [`OnionMessenger::send_onion_message`]. When handling +/// a message, the matched handler may return a response message which `OnionMessenger` will send +/// on its behalf. /// /// # Example /// @@ -36,7 +48,7 @@ pub(crate) type nativeOnionMessenger = nativeOnionMessengerImport u64 { /// \t\t# let your_custom_message_type = 42; /// \t\tyour_custom_message_type @@ -90,8 +102,7 @@ pub(crate) type nativeOnionMessenger = nativeOnionMessengerImport -/// [`OnionMessenger`]: crate::onion_message::OnionMessenger +/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest +/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice #[must_use] #[repr(C)] pub struct OnionMessenger { @@ -159,16 +169,12 @@ impl OnionMessenger { } } /// 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. @@ -176,6 +182,7 @@ pub struct MessageRouter { } unsafe impl Send for MessageRouter {} unsafe impl Sync for MessageRouter {} +#[allow(unused)] pub(crate) fn MessageRouter_clone_fields(orig: &MessageRouter) -> MessageRouter { MessageRouter { this_arg: orig.this_arg, @@ -221,7 +228,7 @@ impl Drop for MessageRouter { use lightning::onion_message::messenger::DefaultMessageRouter as nativeDefaultMessageRouterImport; pub(crate) type nativeDefaultMessageRouter = nativeDefaultMessageRouterImport; -/// A [`MessageRouter`] that always fails. +/// A [`MessageRouter`] that can only route to a directly connected [`Destination`]. #[must_use] #[repr(C)] pub struct DefaultMessageRouter { @@ -276,10 +283,10 @@ pub extern "C" fn DefaultMessageRouter_new() -> DefaultMessageRouter { } 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 rust_obj = crate::lightning::onion_message::messenger::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(); + // We want to free rust_obj when ret gets drop()'d, not rust_obj, so forget it and set ret's free() fn + core::mem::forget(rust_obj); ret.free = Some(DefaultMessageRouter_free_void); ret } @@ -307,7 +314,7 @@ extern "C" fn DefaultMessageRouter_MessageRouter_find_path(this_arg: *const c_vo use lightning::onion_message::messenger::OnionMessagePath as nativeOnionMessagePathImport; pub(crate) type nativeOnionMessagePath = nativeOnionMessagePathImport; -/// A path for sending an [`msgs::OnionMessage`]. +/// A path for sending an [`OnionMessage`]. #[must_use] #[repr(C)] pub struct OnionMessagePath { @@ -402,7 +409,7 @@ impl Clone for 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_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 + Box::into_raw(Box::new(unsafe { (*(this_ptr as *const nativeOnionMessagePath)).clone() })) as *mut c_void } #[no_mangle] /// Creates a copy of the OnionMessagePath @@ -498,6 +505,16 @@ pub extern "C" fn Destination_free(this_ptr: Destination) { } pub extern "C" fn Destination_clone(orig: &Destination) -> Destination { orig.clone() } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn Destination_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *const Destination)).clone() })) as *mut c_void +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn Destination_free_void(this_ptr: *mut c_void) { + let _ = unsafe { Box::from_raw(this_ptr as *mut Destination) }; +} #[no_mangle] /// Utility method to constructs a new Node-variant Destination pub extern "C" fn Destination_node(a: crate::c_types::PublicKey) -> Destination { @@ -521,8 +538,8 @@ 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 [`BlindedPath`], due to having fewer than two - /// blinded hops. + /// The provided [`Destination`] was an invalid [`BlindedPath`] due to not having any blinded + /// hops. TooFewBlindedHops, /// Our next-hop peer was offline or does not support onion message forwarding. InvalidFirstHop, @@ -623,6 +640,16 @@ pub extern "C" fn SendError_free(this_ptr: SendError) { } pub extern "C" fn SendError_clone(orig: &SendError) -> SendError { orig.clone() } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn SendError_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *const SendError)).clone() })) as *mut c_void +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn SendError_free_void(this_ptr: *mut c_void) { + let _ = unsafe { Box::from_raw(this_ptr as *mut SendError) }; +} #[no_mangle] /// Utility method to constructs a new Secp256k1-variant SendError pub extern "C" fn SendError_secp256k1(a: crate::c_types::Secp256k1Error) -> SendError { @@ -678,38 +705,52 @@ pub struct CustomOnionMessageHandler { /// 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, 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, + /// + /// The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`]. + pub handle_custom_message: extern "C" fn (this_arg: *const c_void, msg: crate::lightning::onion_message::packet::OnionMessageContents) -> crate::c_types::derived::COption_OnionMessageContentsZ, /// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the /// message type is unknown. - 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, + 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_OnionMessageContentsZDecodeErrorZ, + /// Releases any [`Self::CustomMessage`]s that need to be sent. + /// + /// Typically, this is used for messages initiating a message flow rather than in response to + /// another message. The latter should use the return value of [`Self::handle_custom_message`]. + pub release_pending_custom_messages: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_C3Tuple_OnionMessageContentsDestinationBlindedPathZZ, /// 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 CustomOnionMessageHandler {} unsafe impl Sync for CustomOnionMessageHandler {} +#[allow(unused)] pub(crate) fn CustomOnionMessageHandler_clone_fields(orig: &CustomOnionMessageHandler) -> CustomOnionMessageHandler { CustomOnionMessageHandler { this_arg: orig.this_arg, handle_custom_message: Clone::clone(&orig.handle_custom_message), read_custom_message: Clone::clone(&orig.read_custom_message), + release_pending_custom_messages: Clone::clone(&orig.release_pending_custom_messages), free: Clone::clone(&orig.free), } } 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) -> Option { + type CustomMessage = crate::lightning::onion_message::packet::OnionMessageContents; + fn handle_custom_message(&self, mut msg: crate::lightning::onion_message::packet::OnionMessageContents) -> 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> { + 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() })}; local_ret } + fn release_pending_custom_messages(&self) -> Vec<(crate::lightning::onion_message::packet::OnionMessageContents, lightning::onion_message::messenger::Destination, Option)> { + let mut ret = (self.release_pending_custom_messages)(self.this_arg); + let mut local_ret = Vec::new(); for mut item in ret.into_rust().drain(..) { local_ret.push( { let (mut orig_ret_0_0, mut orig_ret_0_1, mut orig_ret_0_2) = item.to_rust(); let mut local_orig_ret_0_2 = if orig_ret_0_2.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(orig_ret_0_2.take_inner()) } }) }; let mut local_ret_0 = (orig_ret_0_0, orig_ret_0_1.into_native(), local_orig_ret_0_2); local_ret_0 }); }; + local_ret + } } // We're essentially a pointer already, or at least a set of pointers, so allow us to be used @@ -735,18 +776,174 @@ impl Drop for CustomOnionMessageHandler { } } } -/// Create an onion message with contents `message` to the destination of `path`. -/// Returns (introduction_node_id, onion_msg) +/// A processed incoming onion message, containing either a Forward (another onion message) +/// or a Receive payload with decrypted contents. +#[derive(Clone)] +#[must_use] +#[repr(C)] +pub enum PeeledOnion { + /// Forwarded onion, with the next node id and a new onion + Forward( + crate::c_types::PublicKey, + crate::lightning::ln::msgs::OnionMessage), + /// Received onion message, with decrypted contents, path_id, and reply path + Receive( + crate::lightning::onion_message::packet::ParsedOnionMessageContents, + /// + /// Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None + crate::c_types::ThirtyTwoBytes, + /// + /// Note that this (or a relevant inner pointer) may be NULL or all-0s to represent None + crate::lightning::blinded_path::BlindedPath), +} +use lightning::onion_message::messenger::PeeledOnion as PeeledOnionImport; +pub(crate) type nativePeeledOnion = PeeledOnionImport; + +impl PeeledOnion { + #[allow(unused)] + pub(crate) fn to_native(&self) -> nativePeeledOnion { + match self { + PeeledOnion::Forward (ref a, ref b, ) => { + let mut a_nonref = Clone::clone(a); + let mut b_nonref = Clone::clone(b); + nativePeeledOnion::Forward ( + a_nonref.into_rust(), + *unsafe { Box::from_raw(b_nonref.take_inner()) }, + ) + }, + PeeledOnion::Receive (ref a, ref b, ref c, ) => { + let mut a_nonref = Clone::clone(a); + let mut b_nonref = Clone::clone(b); + let mut local_b_nonref = if b_nonref.data == [0; 32] { None } else { Some( { b_nonref.data }) }; + let mut c_nonref = Clone::clone(c); + let mut local_c_nonref = if c_nonref.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(c_nonref.take_inner()) } }) }; + nativePeeledOnion::Receive ( + a_nonref.into_native(), + local_b_nonref, + local_c_nonref, + ) + }, + } + } + #[allow(unused)] + pub(crate) fn into_native(self) -> nativePeeledOnion { + match self { + PeeledOnion::Forward (mut a, mut b, ) => { + nativePeeledOnion::Forward ( + a.into_rust(), + *unsafe { Box::from_raw(b.take_inner()) }, + ) + }, + PeeledOnion::Receive (mut a, mut b, mut c, ) => { + let mut local_b = if b.data == [0; 32] { None } else { Some( { b.data }) }; + let mut local_c = if c.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(c.take_inner()) } }) }; + nativePeeledOnion::Receive ( + a.into_native(), + local_b, + local_c, + ) + }, + } + } + #[allow(unused)] + pub(crate) fn from_native(native: &nativePeeledOnion) -> Self { + match native { + nativePeeledOnion::Forward (ref a, ref b, ) => { + let mut a_nonref = Clone::clone(a); + let mut b_nonref = Clone::clone(b); + PeeledOnion::Forward ( + crate::c_types::PublicKey::from_rust(&a_nonref), + crate::lightning::ln::msgs::OnionMessage { inner: ObjOps::heap_alloc(b_nonref), is_owned: true }, + ) + }, + nativePeeledOnion::Receive (ref a, ref b, ref c, ) => { + let mut a_nonref = Clone::clone(a); + let mut b_nonref = Clone::clone(b); + let mut local_b_nonref = if b_nonref.is_none() { crate::c_types::ThirtyTwoBytes { data: [0; 32] } } else { { crate::c_types::ThirtyTwoBytes { data: (b_nonref.unwrap()) } } }; + let mut c_nonref = Clone::clone(c); + let mut local_c_nonref = crate::lightning::blinded_path::BlindedPath { inner: if c_nonref.is_none() { core::ptr::null_mut() } else { { ObjOps::heap_alloc((c_nonref.unwrap())) } }, is_owned: true }; + PeeledOnion::Receive ( + crate::lightning::onion_message::packet::ParsedOnionMessageContents::native_into(a_nonref), + local_b_nonref, + local_c_nonref, + ) + }, + } + } + #[allow(unused)] + pub(crate) fn native_into(native: nativePeeledOnion) -> Self { + match native { + nativePeeledOnion::Forward (mut a, mut b, ) => { + PeeledOnion::Forward ( + crate::c_types::PublicKey::from_rust(&a), + crate::lightning::ln::msgs::OnionMessage { inner: ObjOps::heap_alloc(b), is_owned: true }, + ) + }, + nativePeeledOnion::Receive (mut a, mut b, mut c, ) => { + let mut local_b = if b.is_none() { crate::c_types::ThirtyTwoBytes { data: [0; 32] } } else { { crate::c_types::ThirtyTwoBytes { data: (b.unwrap()) } } }; + let mut local_c = crate::lightning::blinded_path::BlindedPath { inner: if c.is_none() { core::ptr::null_mut() } else { { ObjOps::heap_alloc((c.unwrap())) } }, is_owned: true }; + PeeledOnion::Receive ( + crate::lightning::onion_message::packet::ParsedOnionMessageContents::native_into(a), + local_b, + local_c, + ) + }, + } + } +} +/// Frees any resources used by the PeeledOnion +#[no_mangle] +pub extern "C" fn PeeledOnion_free(this_ptr: PeeledOnion) { } +/// Creates a copy of the PeeledOnion +#[no_mangle] +pub extern "C" fn PeeledOnion_clone(orig: &PeeledOnion) -> PeeledOnion { + orig.clone() +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn PeeledOnion_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *const PeeledOnion)).clone() })) as *mut c_void +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn PeeledOnion_free_void(this_ptr: *mut c_void) { + let _ = unsafe { Box::from_raw(this_ptr as *mut PeeledOnion) }; +} +#[no_mangle] +/// Utility method to constructs a new Forward-variant PeeledOnion +pub extern "C" fn PeeledOnion_forward(a: crate::c_types::PublicKey,b: crate::lightning::ln::msgs::OnionMessage) -> PeeledOnion { + PeeledOnion::Forward(a, b, ) +} +#[no_mangle] +/// Utility method to constructs a new Receive-variant PeeledOnion +pub extern "C" fn PeeledOnion_receive(a: crate::lightning::onion_message::packet::ParsedOnionMessageContents,b: crate::c_types::ThirtyTwoBytes,c: crate::lightning::blinded_path::BlindedPath) -> PeeledOnion { + PeeledOnion::Receive(a, b, c, ) +} +/// Creates an [`OnionMessage`] with the given `contents` for sending to the destination of +/// `path`. +/// +/// Returns both the node id of the peer to send the message to and the message itself. /// /// Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None #[no_mangle] -pub extern "C" fn create_onion_message(entropy_source: &crate::lightning::sign::EntropySource, node_signer: &crate::lightning::sign::NodeSigner, 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_C2Tuple_PublicKeyOnionMessageZSendErrorZ { +pub extern "C" fn create_onion_message(entropy_source: &crate::lightning::sign::EntropySource, node_signer: &crate::lightning::sign::NodeSigner, mut path: crate::lightning::onion_message::messenger::OnionMessagePath, mut contents: crate::lightning::onion_message::packet::OnionMessageContents, mut reply_path: crate::lightning::blinded_path::BlindedPath) -> crate::c_types::derived::CResult_C2Tuple_PublicKeyOnionMessageZSendErrorZ { 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 = lightning::onion_message::messenger::create_onion_message::(entropy_source, node_signer, secp256k1::global::SECP256K1, *unsafe { Box::from_raw(path.take_inner()) }, message.into_native(), local_reply_path); + let mut ret = lightning::onion_message::messenger::create_onion_message::(entropy_source, node_signer, secp256k1::global::SECP256K1, *unsafe { Box::from_raw(path.take_inner()) }, contents, local_reply_path); let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let (mut orig_ret_0_0, mut orig_ret_0_1) = o; let mut local_ret_0 = (crate::c_types::PublicKey::from_rust(&orig_ret_0_0), crate::lightning::ln::msgs::OnionMessage { inner: ObjOps::heap_alloc(orig_ret_0_1), is_owned: true }).into(); local_ret_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::onion_message::messenger::SendError::native_into(e) }).into() }; local_ret } +/// Decode one layer of an incoming [`OnionMessage`]. +/// +/// Returns either the next layer of the onion for forwarding or the decrypted content for the +/// receiver. +#[no_mangle] +pub extern "C" fn peel_onion_message(msg: &crate::lightning::ln::msgs::OnionMessage, mut node_signer: crate::lightning::sign::NodeSigner, mut logger: crate::lightning::util::logger::Logger, mut custom_handler: crate::lightning::onion_message::messenger::CustomOnionMessageHandler) -> crate::c_types::derived::CResult_PeeledOnionNoneZ { + let mut ret = lightning::onion_message::messenger::peel_onion_message::(msg.get_native_ref(), secp256k1::global::SECP256K1, node_signer, logger, custom_handler); + let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::onion_message::messenger::PeeledOnion::native_into(o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { () /*e*/ }).into() }; + local_ret +} + /// Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to /// their respective handlers. #[must_use] @@ -756,26 +953,27 @@ pub extern "C" fn OnionMessenger_new(mut entropy_source: crate::lightning::sign: crate::lightning::onion_message::messenger::OnionMessenger { inner: ObjOps::heap_alloc(ret), is_owned: true } } -/// Send an onion message with contents `message` to the destination of `path`. +/// Sends an [`OnionMessage`] with the given `contents` for sending 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_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 { +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 contents: 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_onion_message(*unsafe { Box::from_raw(path.take_inner()) }, message.into_native(), local_reply_path); + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.send_onion_message(*unsafe { Box::from_raw(path.take_inner()) }, contents, 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 } impl From for crate::lightning::ln::msgs::OnionMessageHandler { fn from(obj: nativeOnionMessenger) -> Self { - let mut rust_obj = OnionMessenger { inner: ObjOps::heap_alloc(obj), is_owned: true }; + let rust_obj = crate::lightning::onion_message::messenger::OnionMessenger { inner: ObjOps::heap_alloc(obj), is_owned: true }; let mut ret = OnionMessenger_as_OnionMessageHandler(&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(); + // We want to free rust_obj when ret gets drop()'d, not rust_obj, so forget it and set ret's free() fn + core::mem::forget(rust_obj); ret.free = Some(OnionMessenger_free_void); ret } @@ -788,15 +986,11 @@ pub extern "C" fn OnionMessenger_as_OnionMessageHandler(this_arg: &OnionMessenge this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }, free: None, handle_onion_message: OnionMessenger_OnionMessageHandler_handle_onion_message, + next_onion_message_for_peer: OnionMessenger_OnionMessageHandler_next_onion_message_for_peer, peer_connected: OnionMessenger_OnionMessageHandler_peer_connected, 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::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, - }, } } @@ -804,6 +998,12 @@ 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_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 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 +} +#[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, 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() }; @@ -823,31 +1023,3 @@ 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::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); - // 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(OnionMessenger_free_void); - ret - } -} -/// 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::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, - } -} - -#[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 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 -} -