66555a3d023c6d8d7c3eee77c9b666a8bebf77d7
[ldk-c-bindings] / lightning-c-bindings / src / lightning / onion_message / messenger.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
8
9 //! LDK sends, receives, and forwards onion messages via the [`OnionMessenger`]. See its docs for
10 //! more information.
11
12 use alloc::str::FromStr;
13 use core::ffi::c_void;
14 use core::convert::Infallible;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17 #[cfg(feature="no-std")]
18 use alloc::{vec::Vec, boxed::Box};
19
20
21 use lightning::onion_message::messenger::OnionMessenger as nativeOnionMessengerImport;
22 pub(crate) type nativeOnionMessenger = nativeOnionMessengerImport<crate::lightning::chain::keysinterface::Sign, crate::lightning::chain::keysinterface::KeysInterface, crate::lightning::util::logger::Logger, crate::lightning::onion_message::messenger::CustomOnionMessageHandler>;
23
24 /// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
25 /// used to retrieve invoices and fulfill invoice requests from [offers]. Currently, only sending
26 /// and receiving custom onion messages is supported.
27 ///
28 /// # Example
29 ///
30 /// ```
31 /// # extern crate bitcoin;
32 /// # use bitcoin::hashes::_export::_core::time::Duration;
33 /// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
34 /// # use lightning::chain::keysinterface::{InMemorySigner, KeysManager, KeysInterface};
35 /// # use lightning::ln::msgs::DecodeError;
36 /// # use lightning::ln::peer_handler::IgnoringMessageHandler;
37 /// # use lightning::onion_message::messenger::{Destination, OnionMessenger};
38 /// # use lightning::onion_message::packet::CustomOnionMessageContents;
39 /// # use lightning::onion_message::blinded_route::BlindedRoute;
40 /// # use lightning::util::logger::{Logger, Record};
41 /// # use lightning::util::ser::{Writeable, Writer};
42 /// # use lightning::io;
43 /// # use std::sync::Arc;
44 /// # struct FakeLogger {};
45 /// # impl Logger for FakeLogger {
46 /// #     fn log(&self, record: &Record) { unimplemented!() }
47 /// # }
48 /// # let seed = [42u8; 32];
49 /// # let time = Duration::from_secs(123456);
50 /// # let keys_manager = KeysManager::new(&seed, time.as_secs(), time.subsec_nanos());
51 /// # let logger = Arc::new(FakeLogger {});
52 /// # let node_secret = SecretKey::from_slice(&hex::decode(\"0101010101010101010101010101010101010101010101010101010101010101\").unwrap()[..]).unwrap();
53 /// # let secp_ctx = Secp256k1::new();
54 /// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
55 /// # let (hop_node_id2, hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1, hop_node_id1);
56 /// # let destination_node_id = hop_node_id1;
57 /// # let your_custom_message_handler = IgnoringMessageHandler {};
58 /// // Create the onion messenger. This must use the same `keys_manager` as is passed to your
59 /// // ChannelManager.
60 /// let onion_messenger = OnionMessenger::new(&keys_manager, logger, your_custom_message_handler);
61 ///
62 /// # struct YourCustomMessage {}
63 /// impl Writeable for YourCustomMessage {
64 /// \tfn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
65 /// \t\t# Ok(())
66 /// \t\t// Write your custom onion message to `w`
67 /// \t}
68 /// }
69 /// impl CustomOnionMessageContents for YourCustomMessage {
70 /// \tfn tlv_type(&self) -> u64 {
71 /// \t\t# let your_custom_message_type = 42;
72 /// \t\tyour_custom_message_type
73 /// \t}
74 /// }
75 /// // Send a custom onion message to a node id.
76 /// let intermediate_hops = [hop_node_id1, hop_node_id2];
77 /// let reply_path = None;
78 /// # let your_custom_message = YourCustomMessage {};
79 /// onion_messenger.send_custom_onion_message(&intermediate_hops, Destination::Node(destination_node_id), your_custom_message, reply_path);
80 ///
81 /// // Create a blinded route to yourself, for someone to send an onion message to.
82 /// # let your_node_id = hop_node_id1;
83 /// let hops = [hop_node_id3, hop_node_id4, your_node_id];
84 /// let blinded_route = BlindedRoute::new(&hops, &keys_manager, &secp_ctx).unwrap();
85 ///
86 /// // Send a custom onion message to a blinded route.
87 /// # let intermediate_hops = [hop_node_id1, hop_node_id2];
88 /// let reply_path = None;
89 /// # let your_custom_message = YourCustomMessage {};
90 /// onion_messenger.send_custom_onion_message(&intermediate_hops, Destination::BlindedRoute(blinded_route), your_custom_message, reply_path);
91 /// ```
92 ///
93 /// [offers]: <https://github.com/lightning/bolts/pull/798>
94 /// [`OnionMessenger`]: crate::onion_message::OnionMessenger
95 #[must_use]
96 #[repr(C)]
97 pub struct OnionMessenger {
98         /// A pointer to the opaque Rust object.
99
100         /// Nearly everywhere, inner must be non-null, however in places where
101         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
102         pub inner: *mut nativeOnionMessenger,
103         /// Indicates that this is the only struct which contains the same pointer.
104
105         /// Rust functions which take ownership of an object provided via an argument require
106         /// this to be true and invalidate the object pointed to by inner.
107         pub is_owned: bool,
108 }
109
110 impl Drop for OnionMessenger {
111         fn drop(&mut self) {
112                 if self.is_owned && !<*mut nativeOnionMessenger>::is_null(self.inner) {
113                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
114                 }
115         }
116 }
117 /// Frees any resources used by the OnionMessenger, if is_owned is set and inner is non-NULL.
118 #[no_mangle]
119 pub extern "C" fn OnionMessenger_free(this_obj: OnionMessenger) { }
120 #[allow(unused)]
121 /// Used only if an object of this type is returned as a trait impl by a method
122 pub(crate) extern "C" fn OnionMessenger_free_void(this_ptr: *mut c_void) {
123         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeOnionMessenger); }
124 }
125 #[allow(unused)]
126 impl OnionMessenger {
127         pub(crate) fn get_native_ref(&self) -> &'static nativeOnionMessenger {
128                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
129         }
130         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeOnionMessenger {
131                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
132         }
133         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
134         pub(crate) fn take_inner(mut self) -> *mut nativeOnionMessenger {
135                 assert!(self.is_owned);
136                 let ret = ObjOps::untweak_ptr(self.inner);
137                 self.inner = core::ptr::null_mut();
138                 ret
139         }
140 }
141 /// The destination of an onion message.
142 #[must_use]
143 #[repr(C)]
144 pub enum Destination {
145         /// We're sending this onion message to a node.
146         Node(
147                 crate::c_types::PublicKey),
148         /// We're sending this onion message to a blinded route.
149         BlindedRoute(
150                 crate::lightning::onion_message::blinded_route::BlindedRoute),
151 }
152 use lightning::onion_message::messenger::Destination as DestinationImport;
153 pub(crate) type nativeDestination = DestinationImport;
154
155 impl Destination {
156         #[allow(unused)]
157         pub(crate) fn into_native(self) -> nativeDestination {
158                 match self {
159                         Destination::Node (mut a, ) => {
160                                 nativeDestination::Node (
161                                         a.into_rust(),
162                                 )
163                         },
164                         Destination::BlindedRoute (mut a, ) => {
165                                 nativeDestination::BlindedRoute (
166                                         *unsafe { Box::from_raw(a.take_inner()) },
167                                 )
168                         },
169                 }
170         }
171         #[allow(unused)]
172         pub(crate) fn native_into(native: nativeDestination) -> Self {
173                 match native {
174                         nativeDestination::Node (mut a, ) => {
175                                 Destination::Node (
176                                         crate::c_types::PublicKey::from_rust(&a),
177                                 )
178                         },
179                         nativeDestination::BlindedRoute (mut a, ) => {
180                                 Destination::BlindedRoute (
181                                         crate::lightning::onion_message::blinded_route::BlindedRoute { inner: ObjOps::heap_alloc(a), is_owned: true },
182                                 )
183                         },
184                 }
185         }
186 }
187 /// Frees any resources used by the Destination
188 #[no_mangle]
189 pub extern "C" fn Destination_free(this_ptr: Destination) { }
190 #[no_mangle]
191 /// Utility method to constructs a new Node-variant Destination
192 pub extern "C" fn Destination_node(a: crate::c_types::PublicKey) -> Destination {
193         Destination::Node(a, )
194 }
195 #[no_mangle]
196 /// Utility method to constructs a new BlindedRoute-variant Destination
197 pub extern "C" fn Destination_blinded_route(a: crate::lightning::onion_message::blinded_route::BlindedRoute) -> Destination {
198         Destination::BlindedRoute(a, )
199 }
200 /// Errors that may occur when [sending an onion message].
201 ///
202 /// [sending an onion message]: OnionMessenger::send_custom_onion_message
203 #[derive(Clone)]
204 #[must_use]
205 #[repr(C)]
206 pub enum SendError {
207         /// Errored computing onion message packet keys.
208         Secp256k1(
209                 crate::c_types::Secp256k1Error),
210         /// Because implementations such as Eclair will drop onion messages where the message packet
211         /// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
212         TooBigPacket,
213         /// The provided [`Destination`] was an invalid [`BlindedRoute`], due to having fewer than two
214         /// blinded hops.
215         TooFewBlindedHops,
216         /// Our next-hop peer was offline or does not support onion message forwarding.
217         InvalidFirstHop,
218         /// Onion message contents must have a TLV type >= 64.
219         InvalidMessage,
220         /// Our next-hop peer's buffer was full or our total outbound buffer was full.
221         BufferFull,
222 }
223 use lightning::onion_message::messenger::SendError as SendErrorImport;
224 pub(crate) type nativeSendError = SendErrorImport;
225
226 impl SendError {
227         #[allow(unused)]
228         pub(crate) fn to_native(&self) -> nativeSendError {
229                 match self {
230                         SendError::Secp256k1 (ref a, ) => {
231                                 let mut a_nonref = (*a).clone();
232                                 nativeSendError::Secp256k1 (
233                                         a_nonref.into_rust(),
234                                 )
235                         },
236                         SendError::TooBigPacket => nativeSendError::TooBigPacket,
237                         SendError::TooFewBlindedHops => nativeSendError::TooFewBlindedHops,
238                         SendError::InvalidFirstHop => nativeSendError::InvalidFirstHop,
239                         SendError::InvalidMessage => nativeSendError::InvalidMessage,
240                         SendError::BufferFull => nativeSendError::BufferFull,
241                 }
242         }
243         #[allow(unused)]
244         pub(crate) fn into_native(self) -> nativeSendError {
245                 match self {
246                         SendError::Secp256k1 (mut a, ) => {
247                                 nativeSendError::Secp256k1 (
248                                         a.into_rust(),
249                                 )
250                         },
251                         SendError::TooBigPacket => nativeSendError::TooBigPacket,
252                         SendError::TooFewBlindedHops => nativeSendError::TooFewBlindedHops,
253                         SendError::InvalidFirstHop => nativeSendError::InvalidFirstHop,
254                         SendError::InvalidMessage => nativeSendError::InvalidMessage,
255                         SendError::BufferFull => nativeSendError::BufferFull,
256                 }
257         }
258         #[allow(unused)]
259         pub(crate) fn from_native(native: &nativeSendError) -> Self {
260                 match native {
261                         nativeSendError::Secp256k1 (ref a, ) => {
262                                 let mut a_nonref = (*a).clone();
263                                 SendError::Secp256k1 (
264                                         crate::c_types::Secp256k1Error::from_rust(a_nonref),
265                                 )
266                         },
267                         nativeSendError::TooBigPacket => SendError::TooBigPacket,
268                         nativeSendError::TooFewBlindedHops => SendError::TooFewBlindedHops,
269                         nativeSendError::InvalidFirstHop => SendError::InvalidFirstHop,
270                         nativeSendError::InvalidMessage => SendError::InvalidMessage,
271                         nativeSendError::BufferFull => SendError::BufferFull,
272                 }
273         }
274         #[allow(unused)]
275         pub(crate) fn native_into(native: nativeSendError) -> Self {
276                 match native {
277                         nativeSendError::Secp256k1 (mut a, ) => {
278                                 SendError::Secp256k1 (
279                                         crate::c_types::Secp256k1Error::from_rust(a),
280                                 )
281                         },
282                         nativeSendError::TooBigPacket => SendError::TooBigPacket,
283                         nativeSendError::TooFewBlindedHops => SendError::TooFewBlindedHops,
284                         nativeSendError::InvalidFirstHop => SendError::InvalidFirstHop,
285                         nativeSendError::InvalidMessage => SendError::InvalidMessage,
286                         nativeSendError::BufferFull => SendError::BufferFull,
287                 }
288         }
289 }
290 /// Frees any resources used by the SendError
291 #[no_mangle]
292 pub extern "C" fn SendError_free(this_ptr: SendError) { }
293 /// Creates a copy of the SendError
294 #[no_mangle]
295 pub extern "C" fn SendError_clone(orig: &SendError) -> SendError {
296         orig.clone()
297 }
298 #[no_mangle]
299 /// Utility method to constructs a new Secp256k1-variant SendError
300 pub extern "C" fn SendError_secp256k1(a: crate::c_types::Secp256k1Error) -> SendError {
301         SendError::Secp256k1(a, )
302 }
303 #[no_mangle]
304 /// Utility method to constructs a new TooBigPacket-variant SendError
305 pub extern "C" fn SendError_too_big_packet() -> SendError {
306         SendError::TooBigPacket}
307 #[no_mangle]
308 /// Utility method to constructs a new TooFewBlindedHops-variant SendError
309 pub extern "C" fn SendError_too_few_blinded_hops() -> SendError {
310         SendError::TooFewBlindedHops}
311 #[no_mangle]
312 /// Utility method to constructs a new InvalidFirstHop-variant SendError
313 pub extern "C" fn SendError_invalid_first_hop() -> SendError {
314         SendError::InvalidFirstHop}
315 #[no_mangle]
316 /// Utility method to constructs a new InvalidMessage-variant SendError
317 pub extern "C" fn SendError_invalid_message() -> SendError {
318         SendError::InvalidMessage}
319 #[no_mangle]
320 /// Utility method to constructs a new BufferFull-variant SendError
321 pub extern "C" fn SendError_buffer_full() -> SendError {
322         SendError::BufferFull}
323 /// Checks if two SendErrors contain equal inner contents.
324 /// This ignores pointers and is_owned flags and looks at the values in fields.
325 #[no_mangle]
326 pub extern "C" fn SendError_eq(a: &SendError, b: &SendError) -> bool {
327         if &a.to_native() == &b.to_native() { true } else { false }
328 }
329 /// Handler for custom onion messages. If you are using [`SimpleArcOnionMessenger`],
330 /// [`SimpleRefOnionMessenger`], or prefer to ignore inbound custom onion messages,
331 /// [`IgnoringMessageHandler`] must be provided to [`OnionMessenger::new`]. Otherwise, a custom
332 /// implementation of this trait must be provided, with [`CustomMessage`] specifying the supported
333 /// message types.
334 ///
335 /// See [`OnionMessenger`] for example usage.
336 ///
337 /// [`IgnoringMessageHandler`]: crate::ln::peer_handler::IgnoringMessageHandler
338 /// [`CustomMessage`]: Self::CustomMessage
339 #[repr(C)]
340 pub struct CustomOnionMessageHandler {
341         /// An opaque pointer which is passed to your function implementations as an argument.
342         /// This has no meaning in the LDK, and can be NULL or any other value.
343         pub this_arg: *mut c_void,
344         /// Called with the custom message that was received.
345         pub handle_custom_message: extern "C" fn (this_arg: *const c_void, msg: crate::lightning::onion_message::packet::CustomOnionMessageContents),
346         /// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
347         /// message type is unknown.
348         #[must_use]
349         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,
350         /// Frees any resources associated with this object given its this_arg pointer.
351         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
352         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
353 }
354 unsafe impl Send for CustomOnionMessageHandler {}
355 unsafe impl Sync for CustomOnionMessageHandler {}
356 #[no_mangle]
357 pub(crate) extern "C" fn CustomOnionMessageHandler_clone_fields(orig: &CustomOnionMessageHandler) -> CustomOnionMessageHandler {
358         CustomOnionMessageHandler {
359                 this_arg: orig.this_arg,
360                 handle_custom_message: Clone::clone(&orig.handle_custom_message),
361                 read_custom_message: Clone::clone(&orig.read_custom_message),
362                 free: Clone::clone(&orig.free),
363         }
364 }
365
366 use lightning::onion_message::messenger::CustomOnionMessageHandler as rustCustomOnionMessageHandler;
367 impl rustCustomOnionMessageHandler for CustomOnionMessageHandler {
368         type CustomMessage = crate::lightning::onion_message::packet::CustomOnionMessageContents;
369         fn handle_custom_message(&self, mut msg: crate::lightning::onion_message::packet::CustomOnionMessageContents) {
370                 (self.handle_custom_message)(self.this_arg, Into::into(msg))
371         }
372         fn read_custom_message<R:crate::c_types::io::Read>(&self, mut message_type: u64, mut buffer: &mut R) -> Result<Option<crate::lightning::onion_message::packet::CustomOnionMessageContents>, lightning::ln::msgs::DecodeError> {
373                 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)));
374                 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() })};
375                 local_ret
376         }
377 }
378
379 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
380 // directly as a Deref trait in higher-level structs:
381 impl core::ops::Deref for CustomOnionMessageHandler {
382         type Target = Self;
383         fn deref(&self) -> &Self {
384                 self
385         }
386 }
387 /// Calls the free function if one is set
388 #[no_mangle]
389 pub extern "C" fn CustomOnionMessageHandler_free(this_ptr: CustomOnionMessageHandler) { }
390 impl Drop for CustomOnionMessageHandler {
391         fn drop(&mut self) {
392                 if let Some(f) = self.free {
393                         f(self.this_arg);
394                 }
395         }
396 }
397 /// Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
398 /// their respective handlers.
399 #[must_use]
400 #[no_mangle]
401 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 {
402         let mut ret = lightning::onion_message::messenger::OnionMessenger::new(keys_manager, logger, custom_handler);
403         crate::lightning::onion_message::messenger::OnionMessenger { inner: ObjOps::heap_alloc(ret), is_owned: true }
404 }
405
406 /// Send an onion message with contents `message` to `destination`, routing it through `intermediate_nodes`.
407 /// See [`OnionMessenger`] for example usage.
408 ///
409 /// Note that reply_path (or a relevant inner pointer) may be NULL or all-0s to represent None
410 #[must_use]
411 #[no_mangle]
412 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 {
413         let mut local_intermediate_nodes = Vec::new(); for mut item in intermediate_nodes.into_rust().drain(..) { local_intermediate_nodes.push( { item.into_rust() }); };
414         let mut local_reply_path = if reply_path.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(reply_path.take_inner()) } }) };
415         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.send_custom_onion_message(&local_intermediate_nodes[..], destination.into_native(), msg, local_reply_path);
416         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() };
417         local_ret
418 }
419
420 impl From<nativeOnionMessenger> for crate::lightning::ln::msgs::OnionMessageHandler {
421         fn from(obj: nativeOnionMessenger) -> Self {
422                 let mut rust_obj = OnionMessenger { inner: ObjOps::heap_alloc(obj), is_owned: true };
423                 let mut ret = OnionMessenger_as_OnionMessageHandler(&rust_obj);
424                 // 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
425                 rust_obj.inner = core::ptr::null_mut();
426                 ret.free = Some(OnionMessenger_free_void);
427                 ret
428         }
429 }
430 /// Constructs a new OnionMessageHandler which calls the relevant methods on this_arg.
431 /// This copies the `inner` pointer in this_arg and thus the returned OnionMessageHandler must be freed before this_arg is
432 #[no_mangle]
433 pub extern "C" fn OnionMessenger_as_OnionMessageHandler(this_arg: &OnionMessenger) -> crate::lightning::ln::msgs::OnionMessageHandler {
434         crate::lightning::ln::msgs::OnionMessageHandler {
435                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
436                 free: None,
437                 handle_onion_message: OnionMessenger_OnionMessageHandler_handle_onion_message,
438                 peer_connected: OnionMessenger_OnionMessageHandler_peer_connected,
439                 peer_disconnected: OnionMessenger_OnionMessageHandler_peer_disconnected,
440                 provided_node_features: OnionMessenger_OnionMessageHandler_provided_node_features,
441                 provided_init_features: OnionMessenger_OnionMessageHandler_provided_init_features,
442                 OnionMessageProvider: crate::lightning::util::events::OnionMessageProvider {
443                         this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
444                         free: None,
445                         next_onion_message_for_peer: OnionMessenger_OnionMessageProvider_next_onion_message_for_peer,
446                 },
447         }
448 }
449
450 extern "C" fn OnionMessenger_OnionMessageHandler_handle_onion_message(this_arg: *const c_void, mut peer_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::OnionMessage) {
451         <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::handle_onion_message(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &peer_node_id.into_rust(), msg.get_native_ref())
452 }
453 #[must_use]
454 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 {
455         let mut ret = <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::peer_connected(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust(), init.get_native_ref());
456         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() };
457         local_ret
458 }
459 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) {
460         <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::peer_disconnected(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust(), no_connection_possible)
461 }
462 #[must_use]
463 extern "C" fn OnionMessenger_OnionMessageHandler_provided_node_features(this_arg: *const c_void) -> crate::lightning::ln::features::NodeFeatures {
464         let mut ret = <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::provided_node_features(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, );
465         crate::lightning::ln::features::NodeFeatures { inner: ObjOps::heap_alloc(ret), is_owned: true }
466 }
467 #[must_use]
468 extern "C" fn OnionMessenger_OnionMessageHandler_provided_init_features(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey) -> crate::lightning::ln::features::InitFeatures {
469         let mut ret = <nativeOnionMessenger as lightning::ln::msgs::OnionMessageHandler<>>::provided_init_features(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, &their_node_id.into_rust());
470         crate::lightning::ln::features::InitFeatures { inner: ObjOps::heap_alloc(ret), is_owned: true }
471 }
472
473 impl From<nativeOnionMessenger> for crate::lightning::util::events::OnionMessageProvider {
474         fn from(obj: nativeOnionMessenger) -> Self {
475                 let mut rust_obj = OnionMessenger { inner: ObjOps::heap_alloc(obj), is_owned: true };
476                 let mut ret = OnionMessenger_as_OnionMessageProvider(&rust_obj);
477                 // 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
478                 rust_obj.inner = core::ptr::null_mut();
479                 ret.free = Some(OnionMessenger_free_void);
480                 ret
481         }
482 }
483 /// Constructs a new OnionMessageProvider which calls the relevant methods on this_arg.
484 /// This copies the `inner` pointer in this_arg and thus the returned OnionMessageProvider must be freed before this_arg is
485 #[no_mangle]
486 pub extern "C" fn OnionMessenger_as_OnionMessageProvider(this_arg: &OnionMessenger) -> crate::lightning::util::events::OnionMessageProvider {
487         crate::lightning::util::events::OnionMessageProvider {
488                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
489                 free: None,
490                 next_onion_message_for_peer: OnionMessenger_OnionMessageProvider_next_onion_message_for_peer,
491         }
492 }
493
494 #[must_use]
495 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 {
496         let mut ret = <nativeOnionMessenger as lightning::util::events::OnionMessageProvider<>>::next_onion_message_for_peer(unsafe { &mut *(this_arg as *mut nativeOnionMessenger) }, peer_node_id.into_rust());
497         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 };
498         local_ret
499 }
500