1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Top level peer message handling and socket handling logic lives here.
11 //! Instead of actually servicing sockets ourselves we require that you implement the
12 //! SocketDescriptor interface and use that to receive actions which you should perform on the
13 //! socket, and call into PeerManager with bytes read from the socket. The PeerManager will then
14 //! call into the provided message handlers (probably a ChannelManager and NetGraphmsgHandler) with messages
15 //! they should handle, and encoding/sending response messages.
17 use std::str::FromStr;
19 use core::convert::Infallible;
20 use bitcoin::hashes::Hash;
21 use crate::c_types::*;
23 /// Handler for BOLT1-compliant messages.
25 pub struct CustomMessageHandler {
26 /// An opaque pointer which is passed to your function implementations as an argument.
27 /// This has no meaning in the LDK, and can be NULL or any other value.
28 pub this_arg: *mut c_void,
29 /// Called with the message type that was received and the buffer to be read.
30 /// Can return a `MessageHandlingError` if the message could not be handled.
32 pub handle_custom_message: extern "C" fn (this_arg: *const c_void, msg: crate::lightning::ln::wire::Type, sender_node_id: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_NoneLightningErrorZ,
33 /// Gets the list of pending messages which were generated by the custom message
34 /// handler, clearing the list in the process. The first tuple element must
35 /// correspond to the intended recipients node ids. If no connection to one of the
36 /// specified node does not exist, the message is simply not sent to it.
38 pub get_and_clear_pending_msg: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_C2Tuple_PublicKeyTypeZZ,
39 /// Implementation of CustomMessageReader for this object.
40 pub CustomMessageReader: crate::lightning::ln::wire::CustomMessageReader,
41 /// Frees any resources associated with this object given its this_arg pointer.
42 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
43 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
45 unsafe impl Send for CustomMessageHandler {}
46 unsafe impl Sync for CustomMessageHandler {}
48 pub(crate) extern "C" fn CustomMessageHandler_clone_fields(orig: &CustomMessageHandler) -> CustomMessageHandler {
49 CustomMessageHandler {
50 this_arg: orig.this_arg,
51 handle_custom_message: Clone::clone(&orig.handle_custom_message),
52 get_and_clear_pending_msg: Clone::clone(&orig.get_and_clear_pending_msg),
53 CustomMessageReader: crate::lightning::ln::wire::CustomMessageReader_clone_fields(&orig.CustomMessageReader),
54 free: Clone::clone(&orig.free),
57 impl lightning::ln::wire::CustomMessageReader for CustomMessageHandler {
58 type CustomMessage = crate::lightning::ln::wire::Type;
59 fn read<R:std::io::Read>(&self, mut message_type: u16, mut buffer: &mut R) -> Result<Option<crate::lightning::ln::wire::Type>, lightning::ln::msgs::DecodeError> {
60 let mut ret = (self.CustomMessageReader.read)(self.CustomMessageReader.this_arg, message_type, crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec(buffer)));
61 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((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).take_inner()) } })};
66 use lightning::ln::peer_handler::CustomMessageHandler as rustCustomMessageHandler;
67 impl rustCustomMessageHandler for CustomMessageHandler {
68 fn handle_custom_message(&self, mut msg: crate::lightning::ln::wire::Type, mut sender_node_id: &bitcoin::secp256k1::key::PublicKey) -> Result<(), lightning::ln::msgs::LightningError> {
69 let mut ret = (self.handle_custom_message)(self.this_arg, Into::into(msg), crate::c_types::PublicKey::from_rust(&sender_node_id));
70 let mut local_ret = match ret.result_ok { true => Ok( { () /*(*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) })*/ }), false => Err( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).take_inner()) } })};
73 fn get_and_clear_pending_msg(&self) -> Vec<(bitcoin::secp256k1::key::PublicKey, crate::lightning::ln::wire::Type)> {
74 let mut ret = (self.get_and_clear_pending_msg)(self.this_arg);
75 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) = item.to_rust(); let mut local_ret_0 = (orig_ret_0_0.into_rust(), orig_ret_0_1); local_ret_0 }); };
80 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
81 // directly as a Deref trait in higher-level structs:
82 impl std::ops::Deref for CustomMessageHandler {
84 fn deref(&self) -> &Self {
88 /// Calls the free function if one is set
90 pub extern "C" fn CustomMessageHandler_free(this_ptr: CustomMessageHandler) { }
91 impl Drop for CustomMessageHandler {
93 if let Some(f) = self.free {
99 use lightning::ln::peer_handler::IgnoringMessageHandler as nativeIgnoringMessageHandlerImport;
100 pub(crate) type nativeIgnoringMessageHandler = nativeIgnoringMessageHandlerImport;
102 /// A dummy struct which implements `RoutingMessageHandler` without storing any routing information
103 /// or doing any processing. You can provide one of these as the route_handler in a MessageHandler.
106 pub struct IgnoringMessageHandler {
107 /// A pointer to the opaque Rust object.
109 /// Nearly everywhere, inner must be non-null, however in places where
110 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
111 pub inner: *mut nativeIgnoringMessageHandler,
112 /// Indicates that this is the only struct which contains the same pointer.
114 /// Rust functions which take ownership of an object provided via an argument require
115 /// this to be true and invalidate the object pointed to by inner.
119 impl Drop for IgnoringMessageHandler {
121 if self.is_owned && !<*mut nativeIgnoringMessageHandler>::is_null(self.inner) {
122 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
126 /// Frees any resources used by the IgnoringMessageHandler, if is_owned is set and inner is non-NULL.
128 pub extern "C" fn IgnoringMessageHandler_free(this_obj: IgnoringMessageHandler) { }
130 /// Used only if an object of this type is returned as a trait impl by a method
131 pub(crate) extern "C" fn IgnoringMessageHandler_free_void(this_ptr: *mut c_void) {
132 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeIgnoringMessageHandler); }
135 impl IgnoringMessageHandler {
136 pub(crate) fn get_native_ref(&self) -> &'static nativeIgnoringMessageHandler {
137 unsafe { &*ObjOps::untweak_ptr(self.inner) }
139 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeIgnoringMessageHandler {
140 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
142 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
143 pub(crate) fn take_inner(mut self) -> *mut nativeIgnoringMessageHandler {
144 assert!(self.is_owned);
145 let ret = ObjOps::untweak_ptr(self.inner);
146 self.inner = std::ptr::null_mut();
150 /// Constructs a new IgnoringMessageHandler given each field
153 pub extern "C" fn IgnoringMessageHandler_new() -> IgnoringMessageHandler {
154 IgnoringMessageHandler { inner: ObjOps::heap_alloc(nativeIgnoringMessageHandler {
157 impl From<nativeIgnoringMessageHandler> for crate::lightning::util::events::MessageSendEventsProvider {
158 fn from(obj: nativeIgnoringMessageHandler) -> Self {
159 let mut rust_obj = IgnoringMessageHandler { inner: ObjOps::heap_alloc(obj), is_owned: true };
160 let mut ret = IgnoringMessageHandler_as_MessageSendEventsProvider(&rust_obj);
161 // 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
162 rust_obj.inner = std::ptr::null_mut();
163 ret.free = Some(IgnoringMessageHandler_free_void);
167 /// Constructs a new MessageSendEventsProvider which calls the relevant methods on this_arg.
168 /// This copies the `inner` pointer in this_arg and thus the returned MessageSendEventsProvider must be freed before this_arg is
170 pub extern "C" fn IgnoringMessageHandler_as_MessageSendEventsProvider(this_arg: &IgnoringMessageHandler) -> crate::lightning::util::events::MessageSendEventsProvider {
171 crate::lightning::util::events::MessageSendEventsProvider {
172 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
174 get_and_clear_pending_msg_events: IgnoringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
179 extern "C" fn IgnoringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MessageSendEventZ {
180 let mut ret = <nativeIgnoringMessageHandler as lightning::util::events::MessageSendEventsProvider<>>::get_and_clear_pending_msg_events(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, );
181 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::util::events::MessageSendEvent::native_into(item) }); };
185 impl From<nativeIgnoringMessageHandler> for crate::lightning::ln::msgs::RoutingMessageHandler {
186 fn from(obj: nativeIgnoringMessageHandler) -> Self {
187 let mut rust_obj = IgnoringMessageHandler { inner: ObjOps::heap_alloc(obj), is_owned: true };
188 let mut ret = IgnoringMessageHandler_as_RoutingMessageHandler(&rust_obj);
189 // 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
190 rust_obj.inner = std::ptr::null_mut();
191 ret.free = Some(IgnoringMessageHandler_free_void);
195 /// Constructs a new RoutingMessageHandler which calls the relevant methods on this_arg.
196 /// This copies the `inner` pointer in this_arg and thus the returned RoutingMessageHandler must be freed before this_arg is
198 pub extern "C" fn IgnoringMessageHandler_as_RoutingMessageHandler(this_arg: &IgnoringMessageHandler) -> crate::lightning::ln::msgs::RoutingMessageHandler {
199 crate::lightning::ln::msgs::RoutingMessageHandler {
200 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
202 handle_node_announcement: IgnoringMessageHandler_RoutingMessageHandler_handle_node_announcement,
203 handle_channel_announcement: IgnoringMessageHandler_RoutingMessageHandler_handle_channel_announcement,
204 handle_channel_update: IgnoringMessageHandler_RoutingMessageHandler_handle_channel_update,
205 get_next_channel_announcements: IgnoringMessageHandler_RoutingMessageHandler_get_next_channel_announcements,
206 get_next_node_announcements: IgnoringMessageHandler_RoutingMessageHandler_get_next_node_announcements,
207 sync_routing_table: IgnoringMessageHandler_RoutingMessageHandler_sync_routing_table,
208 handle_reply_channel_range: IgnoringMessageHandler_RoutingMessageHandler_handle_reply_channel_range,
209 handle_reply_short_channel_ids_end: IgnoringMessageHandler_RoutingMessageHandler_handle_reply_short_channel_ids_end,
210 handle_query_channel_range: IgnoringMessageHandler_RoutingMessageHandler_handle_query_channel_range,
211 handle_query_short_channel_ids: IgnoringMessageHandler_RoutingMessageHandler_handle_query_short_channel_ids,
212 MessageSendEventsProvider: crate::lightning::util::events::MessageSendEventsProvider {
213 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
215 get_and_clear_pending_msg_events: IgnoringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
221 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_node_announcement(this_arg: *const c_void, _msg: &crate::lightning::ln::msgs::NodeAnnouncement) -> crate::c_types::derived::CResult_boolLightningErrorZ {
222 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_node_announcement(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, _msg.get_native_ref());
223 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::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
227 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_channel_announcement(this_arg: *const c_void, _msg: &crate::lightning::ln::msgs::ChannelAnnouncement) -> crate::c_types::derived::CResult_boolLightningErrorZ {
228 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_channel_announcement(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, _msg.get_native_ref());
229 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::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
233 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_channel_update(this_arg: *const c_void, _msg: &crate::lightning::ln::msgs::ChannelUpdate) -> crate::c_types::derived::CResult_boolLightningErrorZ {
234 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_channel_update(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, _msg.get_native_ref());
235 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::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
239 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_get_next_channel_announcements(this_arg: *const c_void, mut _starting_point: u64, mut _batch_amount: u8) -> crate::c_types::derived::CVec_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ {
240 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::get_next_channel_announcements(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, _starting_point, _batch_amount);
241 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { let (mut orig_ret_0_0, mut orig_ret_0_1, mut orig_ret_0_2) = item; let mut local_orig_ret_0_1 = crate::lightning::ln::msgs::ChannelUpdate { inner: if orig_ret_0_1.is_none() { std::ptr::null_mut() } else { { ObjOps::heap_alloc((orig_ret_0_1.unwrap())) } }, is_owned: true }; let mut local_orig_ret_0_2 = crate::lightning::ln::msgs::ChannelUpdate { inner: if orig_ret_0_2.is_none() { std::ptr::null_mut() } else { { ObjOps::heap_alloc((orig_ret_0_2.unwrap())) } }, is_owned: true }; let mut local_ret_0 = (crate::lightning::ln::msgs::ChannelAnnouncement { inner: ObjOps::heap_alloc(orig_ret_0_0), is_owned: true }, local_orig_ret_0_1, local_orig_ret_0_2).into(); local_ret_0 }); };
245 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_get_next_node_announcements(this_arg: *const c_void, mut _starting_point: crate::c_types::PublicKey, mut _batch_amount: u8) -> crate::c_types::derived::CVec_NodeAnnouncementZ {
246 let mut local__starting_point_base = if _starting_point.is_null() { None } else { Some( { _starting_point.into_rust() }) }; let mut local__starting_point = local__starting_point_base.as_ref();
247 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::get_next_node_announcements(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, local__starting_point, _batch_amount);
248 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::ln::msgs::NodeAnnouncement { inner: ObjOps::heap_alloc(item), is_owned: true } }); };
251 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_sync_routing_table(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, _init: &crate::lightning::ln::msgs::Init) {
252 <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::sync_routing_table(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, &_their_node_id.into_rust(), _init.get_native_ref())
255 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_reply_channel_range(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, mut _msg: crate::lightning::ln::msgs::ReplyChannelRange) -> crate::c_types::derived::CResult_NoneLightningErrorZ {
256 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_reply_channel_range(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, &_their_node_id.into_rust(), *unsafe { Box::from_raw(_msg.take_inner()) });
257 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::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
261 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_reply_short_channel_ids_end(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, mut _msg: crate::lightning::ln::msgs::ReplyShortChannelIdsEnd) -> crate::c_types::derived::CResult_NoneLightningErrorZ {
262 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_reply_short_channel_ids_end(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, &_their_node_id.into_rust(), *unsafe { Box::from_raw(_msg.take_inner()) });
263 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::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
267 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_query_channel_range(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, mut _msg: crate::lightning::ln::msgs::QueryChannelRange) -> crate::c_types::derived::CResult_NoneLightningErrorZ {
268 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_query_channel_range(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, &_their_node_id.into_rust(), *unsafe { Box::from_raw(_msg.take_inner()) });
269 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::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
273 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_query_short_channel_ids(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, mut _msg: crate::lightning::ln::msgs::QueryShortChannelIds) -> crate::c_types::derived::CResult_NoneLightningErrorZ {
274 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_query_short_channel_ids(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, &_their_node_id.into_rust(), *unsafe { Box::from_raw(_msg.take_inner()) });
275 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::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
279 use core::convert::Infallible as nativeInfallible;
280 impl From<nativeInfallible> for crate::lightning::ln::wire::Type {
281 fn from(obj: nativeInfallible) -> Self {
285 impl From<nativeIgnoringMessageHandler> for crate::lightning::ln::wire::CustomMessageReader {
286 fn from(obj: nativeIgnoringMessageHandler) -> Self {
287 let mut rust_obj = IgnoringMessageHandler { inner: ObjOps::heap_alloc(obj), is_owned: true };
288 let mut ret = IgnoringMessageHandler_as_CustomMessageReader(&rust_obj);
289 // 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
290 rust_obj.inner = std::ptr::null_mut();
291 ret.free = Some(IgnoringMessageHandler_free_void);
295 /// Constructs a new CustomMessageReader which calls the relevant methods on this_arg.
296 /// This copies the `inner` pointer in this_arg and thus the returned CustomMessageReader must be freed before this_arg is
298 pub extern "C" fn IgnoringMessageHandler_as_CustomMessageReader(this_arg: &IgnoringMessageHandler) -> crate::lightning::ln::wire::CustomMessageReader {
299 crate::lightning::ln::wire::CustomMessageReader {
300 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
302 read: IgnoringMessageHandler_CustomMessageReader_read,
307 extern "C" fn IgnoringMessageHandler_CustomMessageReader_read(this_arg: *const c_void, mut _message_type: u16, mut _buffer: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_TypeZDecodeErrorZ {
308 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::wire::CustomMessageReader<>>::read(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, _message_type, &mut _buffer.to_reader());
309 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = if o.is_none() { crate::c_types::derived::COption_TypeZ::None } else { crate::c_types::derived::COption_TypeZ::Some( { Into::into(o.unwrap()) }) }; local_ret_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
313 impl From<nativeIgnoringMessageHandler> for crate::lightning::ln::peer_handler::CustomMessageHandler {
314 fn from(obj: nativeIgnoringMessageHandler) -> Self {
315 let mut rust_obj = IgnoringMessageHandler { inner: ObjOps::heap_alloc(obj), is_owned: true };
316 let mut ret = IgnoringMessageHandler_as_CustomMessageHandler(&rust_obj);
317 // 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
318 rust_obj.inner = std::ptr::null_mut();
319 ret.free = Some(IgnoringMessageHandler_free_void);
323 /// Constructs a new CustomMessageHandler which calls the relevant methods on this_arg.
324 /// This copies the `inner` pointer in this_arg and thus the returned CustomMessageHandler must be freed before this_arg is
326 pub extern "C" fn IgnoringMessageHandler_as_CustomMessageHandler(this_arg: &IgnoringMessageHandler) -> crate::lightning::ln::peer_handler::CustomMessageHandler {
327 crate::lightning::ln::peer_handler::CustomMessageHandler {
328 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
330 handle_custom_message: IgnoringMessageHandler_CustomMessageHandler_handle_custom_message,
331 get_and_clear_pending_msg: IgnoringMessageHandler_CustomMessageHandler_get_and_clear_pending_msg,
332 CustomMessageReader: crate::lightning::ln::wire::CustomMessageReader {
333 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
335 read: IgnoringMessageHandler_CustomMessageReader_read,
341 extern "C" fn IgnoringMessageHandler_CustomMessageHandler_handle_custom_message(this_arg: *const c_void, mut msg: crate::lightning::ln::wire::Type, mut sender_node_id: crate::c_types::PublicKey) -> crate::c_types::derived::CResult_NoneLightningErrorZ {
345 extern "C" fn IgnoringMessageHandler_CustomMessageHandler_get_and_clear_pending_msg(this_arg: *const c_void) -> crate::c_types::derived::CVec_C2Tuple_PublicKeyTypeZZ {
346 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::peer_handler::CustomMessageHandler<>>::get_and_clear_pending_msg(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, );
347 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { let (mut orig_ret_0_0, mut orig_ret_0_1) = item; let mut local_ret_0 = (crate::c_types::PublicKey::from_rust(&orig_ret_0_0), Into::into(orig_ret_0_1)).into(); local_ret_0 }); };
352 use lightning::ln::peer_handler::ErroringMessageHandler as nativeErroringMessageHandlerImport;
353 pub(crate) type nativeErroringMessageHandler = nativeErroringMessageHandlerImport;
355 /// A dummy struct which implements `ChannelMessageHandler` without having any channels.
356 /// You can provide one of these as the route_handler in a MessageHandler.
359 pub struct ErroringMessageHandler {
360 /// A pointer to the opaque Rust object.
362 /// Nearly everywhere, inner must be non-null, however in places where
363 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
364 pub inner: *mut nativeErroringMessageHandler,
365 /// Indicates that this is the only struct which contains the same pointer.
367 /// Rust functions which take ownership of an object provided via an argument require
368 /// this to be true and invalidate the object pointed to by inner.
372 impl Drop for ErroringMessageHandler {
374 if self.is_owned && !<*mut nativeErroringMessageHandler>::is_null(self.inner) {
375 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
379 /// Frees any resources used by the ErroringMessageHandler, if is_owned is set and inner is non-NULL.
381 pub extern "C" fn ErroringMessageHandler_free(this_obj: ErroringMessageHandler) { }
383 /// Used only if an object of this type is returned as a trait impl by a method
384 pub(crate) extern "C" fn ErroringMessageHandler_free_void(this_ptr: *mut c_void) {
385 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeErroringMessageHandler); }
388 impl ErroringMessageHandler {
389 pub(crate) fn get_native_ref(&self) -> &'static nativeErroringMessageHandler {
390 unsafe { &*ObjOps::untweak_ptr(self.inner) }
392 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeErroringMessageHandler {
393 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
395 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
396 pub(crate) fn take_inner(mut self) -> *mut nativeErroringMessageHandler {
397 assert!(self.is_owned);
398 let ret = ObjOps::untweak_ptr(self.inner);
399 self.inner = std::ptr::null_mut();
403 /// Constructs a new ErroringMessageHandler
406 pub extern "C" fn ErroringMessageHandler_new() -> ErroringMessageHandler {
407 let mut ret = lightning::ln::peer_handler::ErroringMessageHandler::new();
408 ErroringMessageHandler { inner: ObjOps::heap_alloc(ret), is_owned: true }
411 impl From<nativeErroringMessageHandler> for crate::lightning::util::events::MessageSendEventsProvider {
412 fn from(obj: nativeErroringMessageHandler) -> Self {
413 let mut rust_obj = ErroringMessageHandler { inner: ObjOps::heap_alloc(obj), is_owned: true };
414 let mut ret = ErroringMessageHandler_as_MessageSendEventsProvider(&rust_obj);
415 // 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
416 rust_obj.inner = std::ptr::null_mut();
417 ret.free = Some(ErroringMessageHandler_free_void);
421 /// Constructs a new MessageSendEventsProvider which calls the relevant methods on this_arg.
422 /// This copies the `inner` pointer in this_arg and thus the returned MessageSendEventsProvider must be freed before this_arg is
424 pub extern "C" fn ErroringMessageHandler_as_MessageSendEventsProvider(this_arg: &ErroringMessageHandler) -> crate::lightning::util::events::MessageSendEventsProvider {
425 crate::lightning::util::events::MessageSendEventsProvider {
426 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
428 get_and_clear_pending_msg_events: ErroringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
433 extern "C" fn ErroringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MessageSendEventZ {
434 let mut ret = <nativeErroringMessageHandler as lightning::util::events::MessageSendEventsProvider<>>::get_and_clear_pending_msg_events(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, );
435 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::util::events::MessageSendEvent::native_into(item) }); };
439 impl From<nativeErroringMessageHandler> for crate::lightning::ln::msgs::ChannelMessageHandler {
440 fn from(obj: nativeErroringMessageHandler) -> Self {
441 let mut rust_obj = ErroringMessageHandler { inner: ObjOps::heap_alloc(obj), is_owned: true };
442 let mut ret = ErroringMessageHandler_as_ChannelMessageHandler(&rust_obj);
443 // 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
444 rust_obj.inner = std::ptr::null_mut();
445 ret.free = Some(ErroringMessageHandler_free_void);
449 /// Constructs a new ChannelMessageHandler which calls the relevant methods on this_arg.
450 /// This copies the `inner` pointer in this_arg and thus the returned ChannelMessageHandler must be freed before this_arg is
452 pub extern "C" fn ErroringMessageHandler_as_ChannelMessageHandler(this_arg: &ErroringMessageHandler) -> crate::lightning::ln::msgs::ChannelMessageHandler {
453 crate::lightning::ln::msgs::ChannelMessageHandler {
454 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
456 handle_open_channel: ErroringMessageHandler_ChannelMessageHandler_handle_open_channel,
457 handle_accept_channel: ErroringMessageHandler_ChannelMessageHandler_handle_accept_channel,
458 handle_funding_created: ErroringMessageHandler_ChannelMessageHandler_handle_funding_created,
459 handle_funding_signed: ErroringMessageHandler_ChannelMessageHandler_handle_funding_signed,
460 handle_funding_locked: ErroringMessageHandler_ChannelMessageHandler_handle_funding_locked,
461 handle_shutdown: ErroringMessageHandler_ChannelMessageHandler_handle_shutdown,
462 handle_closing_signed: ErroringMessageHandler_ChannelMessageHandler_handle_closing_signed,
463 handle_update_add_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_add_htlc,
464 handle_update_fulfill_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_fulfill_htlc,
465 handle_update_fail_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_fail_htlc,
466 handle_update_fail_malformed_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_fail_malformed_htlc,
467 handle_commitment_signed: ErroringMessageHandler_ChannelMessageHandler_handle_commitment_signed,
468 handle_revoke_and_ack: ErroringMessageHandler_ChannelMessageHandler_handle_revoke_and_ack,
469 handle_update_fee: ErroringMessageHandler_ChannelMessageHandler_handle_update_fee,
470 handle_announcement_signatures: ErroringMessageHandler_ChannelMessageHandler_handle_announcement_signatures,
471 peer_disconnected: ErroringMessageHandler_ChannelMessageHandler_peer_disconnected,
472 peer_connected: ErroringMessageHandler_ChannelMessageHandler_peer_connected,
473 handle_channel_reestablish: ErroringMessageHandler_ChannelMessageHandler_handle_channel_reestablish,
474 handle_channel_update: ErroringMessageHandler_ChannelMessageHandler_handle_channel_update,
475 handle_error: ErroringMessageHandler_ChannelMessageHandler_handle_error,
476 MessageSendEventsProvider: crate::lightning::util::events::MessageSendEventsProvider {
477 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
479 get_and_clear_pending_msg_events: ErroringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
484 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_open_channel(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, mut _their_features: crate::lightning::ln::features::InitFeatures, msg: &crate::lightning::ln::msgs::OpenChannel) {
485 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_open_channel(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), *unsafe { Box::from_raw(_their_features.take_inner()) }, msg.get_native_ref())
487 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_accept_channel(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, mut _their_features: crate::lightning::ln::features::InitFeatures, msg: &crate::lightning::ln::msgs::AcceptChannel) {
488 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_accept_channel(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), *unsafe { Box::from_raw(_their_features.take_inner()) }, msg.get_native_ref())
490 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_funding_created(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::FundingCreated) {
491 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_funding_created(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
493 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_funding_signed(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::FundingSigned) {
494 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_funding_signed(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
496 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_funding_locked(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::FundingLocked) {
497 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_funding_locked(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
499 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_shutdown(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, _their_features: &crate::lightning::ln::features::InitFeatures, msg: &crate::lightning::ln::msgs::Shutdown) {
500 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_shutdown(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), _their_features.get_native_ref(), msg.get_native_ref())
502 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_closing_signed(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::ClosingSigned) {
503 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_closing_signed(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
505 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_update_add_htlc(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::UpdateAddHTLC) {
506 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_add_htlc(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
508 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_update_fulfill_htlc(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::UpdateFulfillHTLC) {
509 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_fulfill_htlc(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
511 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_update_fail_htlc(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::UpdateFailHTLC) {
512 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_fail_htlc(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
514 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_update_fail_malformed_htlc(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::UpdateFailMalformedHTLC) {
515 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_fail_malformed_htlc(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
517 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_commitment_signed(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::CommitmentSigned) {
518 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_commitment_signed(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
520 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_revoke_and_ack(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::RevokeAndACK) {
521 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_revoke_and_ack(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
523 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_update_fee(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::UpdateFee) {
524 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_fee(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
526 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_announcement_signatures(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::AnnouncementSignatures) {
527 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_announcement_signatures(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
529 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_channel_reestablish(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::lightning::ln::msgs::ChannelReestablish) {
530 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_channel_reestablish(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), msg.get_native_ref())
532 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_channel_update(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, _msg: &crate::lightning::ln::msgs::ChannelUpdate) {
533 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_channel_update(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), _msg.get_native_ref())
535 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_peer_disconnected(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, mut _no_connection_possible: bool) {
536 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::peer_disconnected(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), _no_connection_possible)
538 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_peer_connected(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, _msg: &crate::lightning::ln::msgs::Init) {
539 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::peer_connected(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), _msg.get_native_ref())
541 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_error(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, _msg: &crate::lightning::ln::msgs::ErrorMessage) {
542 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_error(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), _msg.get_native_ref())
546 use lightning::ln::peer_handler::MessageHandler as nativeMessageHandlerImport;
547 pub(crate) type nativeMessageHandler = nativeMessageHandlerImport<crate::lightning::ln::msgs::ChannelMessageHandler, crate::lightning::ln::msgs::RoutingMessageHandler>;
549 /// Provides references to trait impls which handle different types of messages.
552 pub struct MessageHandler {
553 /// A pointer to the opaque Rust object.
555 /// Nearly everywhere, inner must be non-null, however in places where
556 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
557 pub inner: *mut nativeMessageHandler,
558 /// Indicates that this is the only struct which contains the same pointer.
560 /// Rust functions which take ownership of an object provided via an argument require
561 /// this to be true and invalidate the object pointed to by inner.
565 impl Drop for MessageHandler {
567 if self.is_owned && !<*mut nativeMessageHandler>::is_null(self.inner) {
568 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
572 /// Frees any resources used by the MessageHandler, if is_owned is set and inner is non-NULL.
574 pub extern "C" fn MessageHandler_free(this_obj: MessageHandler) { }
576 /// Used only if an object of this type is returned as a trait impl by a method
577 pub(crate) extern "C" fn MessageHandler_free_void(this_ptr: *mut c_void) {
578 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMessageHandler); }
581 impl MessageHandler {
582 pub(crate) fn get_native_ref(&self) -> &'static nativeMessageHandler {
583 unsafe { &*ObjOps::untweak_ptr(self.inner) }
585 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMessageHandler {
586 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
588 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
589 pub(crate) fn take_inner(mut self) -> *mut nativeMessageHandler {
590 assert!(self.is_owned);
591 let ret = ObjOps::untweak_ptr(self.inner);
592 self.inner = std::ptr::null_mut();
596 /// A message handler which handles messages specific to channels. Usually this is just a
597 /// [`ChannelManager`] object or an [`ErroringMessageHandler`].
599 /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
601 pub extern "C" fn MessageHandler_get_chan_handler(this_ptr: &MessageHandler) -> *const crate::lightning::ln::msgs::ChannelMessageHandler {
602 let mut inner_val = &mut this_ptr.get_native_mut_ref().chan_handler;
605 /// A message handler which handles messages specific to channels. Usually this is just a
606 /// [`ChannelManager`] object or an [`ErroringMessageHandler`].
608 /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
610 pub extern "C" fn MessageHandler_set_chan_handler(this_ptr: &mut MessageHandler, mut val: crate::lightning::ln::msgs::ChannelMessageHandler) {
611 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.chan_handler = val;
613 /// A message handler which handles messages updating our knowledge of the network channel
614 /// graph. Usually this is just a [`NetGraphMsgHandler`] object or an
615 /// [`IgnoringMessageHandler`].
617 /// [`NetGraphMsgHandler`]: crate::routing::network_graph::NetGraphMsgHandler
619 pub extern "C" fn MessageHandler_get_route_handler(this_ptr: &MessageHandler) -> *const crate::lightning::ln::msgs::RoutingMessageHandler {
620 let mut inner_val = &mut this_ptr.get_native_mut_ref().route_handler;
623 /// A message handler which handles messages updating our knowledge of the network channel
624 /// graph. Usually this is just a [`NetGraphMsgHandler`] object or an
625 /// [`IgnoringMessageHandler`].
627 /// [`NetGraphMsgHandler`]: crate::routing::network_graph::NetGraphMsgHandler
629 pub extern "C" fn MessageHandler_set_route_handler(this_ptr: &mut MessageHandler, mut val: crate::lightning::ln::msgs::RoutingMessageHandler) {
630 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.route_handler = val;
632 /// Constructs a new MessageHandler given each field
635 pub extern "C" fn MessageHandler_new(mut chan_handler_arg: crate::lightning::ln::msgs::ChannelMessageHandler, mut route_handler_arg: crate::lightning::ln::msgs::RoutingMessageHandler) -> MessageHandler {
636 MessageHandler { inner: ObjOps::heap_alloc(nativeMessageHandler {
637 chan_handler: chan_handler_arg,
638 route_handler: route_handler_arg,
641 /// Provides an object which can be used to send data to and which uniquely identifies a connection
642 /// to a remote host. You will need to be able to generate multiple of these which meet Eq and
643 /// implement Hash to meet the PeerManager API.
645 /// For efficiency, Clone should be relatively cheap for this type.
647 /// Two descriptors may compare equal (by [`cmp::Eq`] and [`hash::Hash`]) as long as the original
648 /// has been disconnected, the [`PeerManager`] has been informed of the disconnection (either by it
649 /// having triggered the disconnection or a call to [`PeerManager::socket_disconnected`]), and no
650 /// further calls to the [`PeerManager`] related to the original socket occur. This allows you to
651 /// use a file descriptor for your SocketDescriptor directly, however for simplicity you may wish
652 /// to simply use another value which is guaranteed to be globally unique instead.
654 pub struct SocketDescriptor {
655 /// An opaque pointer which is passed to your function implementations as an argument.
656 /// This has no meaning in the LDK, and can be NULL or any other value.
657 pub this_arg: *mut c_void,
658 /// Attempts to send some data from the given slice to the peer.
660 /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
661 /// Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
662 /// called and further write attempts may occur until that time.
664 /// If the returned size is smaller than `data.len()`, a
665 /// [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
666 /// written. Additionally, until a `send_data` event completes fully, no further
667 /// [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
668 /// prevent denial-of-service issues, you should not read or buffer any data from the socket
671 /// If a [`PeerManager::read_event`] call on this descriptor had previously returned true
672 /// (indicating that read events should be paused to prevent DoS in the send buffer),
673 /// `resume_read` may be set indicating that read events on this descriptor should resume. A
674 /// `resume_read` of false carries no meaning, and should not cause any action.
676 pub send_data: extern "C" fn (this_arg: *mut c_void, data: crate::c_types::u8slice, resume_read: bool) -> usize,
677 /// Disconnect the socket pointed to by this SocketDescriptor.
679 /// You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
680 /// call (doing so is a noop).
681 pub disconnect_socket: extern "C" fn (this_arg: *mut c_void),
682 /// Checks if two objects are equal given this object's this_arg pointer and another object.
683 pub eq: extern "C" fn (this_arg: *const c_void, other_arg: &SocketDescriptor) -> bool,
684 /// Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
685 /// This is used, for example, for inclusion of this object in a hash map.
686 pub hash: extern "C" fn (this_arg: *const c_void) -> u64,
687 /// Called, if set, after this SocketDescriptor has been cloned into a duplicate object.
688 /// The new SocketDescriptor is provided, and should be mutated as needed to perform a
689 /// deep copy of the object pointed to by this_arg or avoid any double-freeing.
690 pub cloned: Option<extern "C" fn (new_SocketDescriptor: &mut SocketDescriptor)>,
691 /// Frees any resources associated with this object given its this_arg pointer.
692 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
693 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
695 unsafe impl Send for SocketDescriptor {}
696 unsafe impl Sync for SocketDescriptor {}
698 pub(crate) extern "C" fn SocketDescriptor_clone_fields(orig: &SocketDescriptor) -> SocketDescriptor {
700 this_arg: orig.this_arg,
701 send_data: Clone::clone(&orig.send_data),
702 disconnect_socket: Clone::clone(&orig.disconnect_socket),
703 eq: Clone::clone(&orig.eq),
704 hash: Clone::clone(&orig.hash),
705 cloned: Clone::clone(&orig.cloned),
706 free: Clone::clone(&orig.free),
709 impl std::cmp::Eq for SocketDescriptor {}
710 impl std::cmp::PartialEq for SocketDescriptor {
711 fn eq(&self, o: &Self) -> bool { (self.eq)(self.this_arg, o) }
713 impl std::hash::Hash for SocketDescriptor {
714 fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64((self.hash)(self.this_arg)) }
717 /// Creates a copy of a SocketDescriptor
718 pub extern "C" fn SocketDescriptor_clone(orig: &SocketDescriptor) -> SocketDescriptor {
719 let mut res = SocketDescriptor_clone_fields(orig);
720 if let Some(f) = orig.cloned { (f)(&mut res) };
723 impl Clone for SocketDescriptor {
724 fn clone(&self) -> Self {
725 SocketDescriptor_clone(self)
729 use lightning::ln::peer_handler::SocketDescriptor as rustSocketDescriptor;
730 impl rustSocketDescriptor for SocketDescriptor {
731 fn send_data(&mut self, mut data: &[u8], mut resume_read: bool) -> usize {
732 let mut local_data = crate::c_types::u8slice::from_slice(data);
733 let mut ret = (self.send_data)(self.this_arg, local_data, resume_read);
736 fn disconnect_socket(&mut self) {
737 (self.disconnect_socket)(self.this_arg)
741 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
742 // directly as a Deref trait in higher-level structs:
743 impl std::ops::Deref for SocketDescriptor {
745 fn deref(&self) -> &Self {
749 /// Calls the free function if one is set
751 pub extern "C" fn SocketDescriptor_free(this_ptr: SocketDescriptor) { }
752 impl Drop for SocketDescriptor {
754 if let Some(f) = self.free {
760 use lightning::ln::peer_handler::PeerHandleError as nativePeerHandleErrorImport;
761 pub(crate) type nativePeerHandleError = nativePeerHandleErrorImport;
763 /// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
764 /// generate no further read_event/write_buffer_space_avail/socket_disconnected calls for the
768 pub struct PeerHandleError {
769 /// A pointer to the opaque Rust object.
771 /// Nearly everywhere, inner must be non-null, however in places where
772 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
773 pub inner: *mut nativePeerHandleError,
774 /// Indicates that this is the only struct which contains the same pointer.
776 /// Rust functions which take ownership of an object provided via an argument require
777 /// this to be true and invalidate the object pointed to by inner.
781 impl Drop for PeerHandleError {
783 if self.is_owned && !<*mut nativePeerHandleError>::is_null(self.inner) {
784 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
788 /// Frees any resources used by the PeerHandleError, if is_owned is set and inner is non-NULL.
790 pub extern "C" fn PeerHandleError_free(this_obj: PeerHandleError) { }
792 /// Used only if an object of this type is returned as a trait impl by a method
793 pub(crate) extern "C" fn PeerHandleError_free_void(this_ptr: *mut c_void) {
794 unsafe { let _ = Box::from_raw(this_ptr as *mut nativePeerHandleError); }
797 impl PeerHandleError {
798 pub(crate) fn get_native_ref(&self) -> &'static nativePeerHandleError {
799 unsafe { &*ObjOps::untweak_ptr(self.inner) }
801 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePeerHandleError {
802 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
804 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
805 pub(crate) fn take_inner(mut self) -> *mut nativePeerHandleError {
806 assert!(self.is_owned);
807 let ret = ObjOps::untweak_ptr(self.inner);
808 self.inner = std::ptr::null_mut();
812 /// Used to indicate that we probably can't make any future connections to this peer, implying
813 /// we should go ahead and force-close any channels we have with it.
815 pub extern "C" fn PeerHandleError_get_no_connection_possible(this_ptr: &PeerHandleError) -> bool {
816 let mut inner_val = &mut this_ptr.get_native_mut_ref().no_connection_possible;
819 /// Used to indicate that we probably can't make any future connections to this peer, implying
820 /// we should go ahead and force-close any channels we have with it.
822 pub extern "C" fn PeerHandleError_set_no_connection_possible(this_ptr: &mut PeerHandleError, mut val: bool) {
823 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.no_connection_possible = val;
825 /// Constructs a new PeerHandleError given each field
828 pub extern "C" fn PeerHandleError_new(mut no_connection_possible_arg: bool) -> PeerHandleError {
829 PeerHandleError { inner: ObjOps::heap_alloc(nativePeerHandleError {
830 no_connection_possible: no_connection_possible_arg,
833 impl Clone for PeerHandleError {
834 fn clone(&self) -> Self {
836 inner: if <*mut nativePeerHandleError>::is_null(self.inner) { std::ptr::null_mut() } else {
837 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
843 /// Used only if an object of this type is returned as a trait impl by a method
844 pub(crate) extern "C" fn PeerHandleError_clone_void(this_ptr: *const c_void) -> *mut c_void {
845 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativePeerHandleError)).clone() })) as *mut c_void
848 /// Creates a copy of the PeerHandleError
849 pub extern "C" fn PeerHandleError_clone(orig: &PeerHandleError) -> PeerHandleError {
853 use lightning::ln::peer_handler::PeerManager as nativePeerManagerImport;
854 pub(crate) type nativePeerManager = nativePeerManagerImport<crate::lightning::ln::peer_handler::SocketDescriptor, crate::lightning::ln::msgs::ChannelMessageHandler, crate::lightning::ln::msgs::RoutingMessageHandler, crate::lightning::util::logger::Logger, crate::lightning::ln::peer_handler::CustomMessageHandler>;
856 /// A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
857 /// socket events into messages which it passes on to its [`MessageHandler`].
859 /// Locks are taken internally, so you must never assume that reentrancy from a
860 /// [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
862 /// Calls to [`read_event`] will decode relevant messages and pass them to the
863 /// [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of
864 /// parallelism in Rust-Lightning is in calls to [`read_event`]. Note, however, that calls to any
865 /// [`PeerManager`] functions related to the same connection must occur only in serial, making new
866 /// calls only after previous ones have returned.
868 /// Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
869 /// a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
870 /// essentially you should default to using a SimpleRefPeerManager, and use a
871 /// SimpleArcPeerManager when you require a PeerManager with a static lifetime, such as when
872 /// you're using lightning-net-tokio.
874 /// [`read_event`]: PeerManager::read_event
877 pub struct PeerManager {
878 /// A pointer to the opaque Rust object.
880 /// Nearly everywhere, inner must be non-null, however in places where
881 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
882 pub inner: *mut nativePeerManager,
883 /// Indicates that this is the only struct which contains the same pointer.
885 /// Rust functions which take ownership of an object provided via an argument require
886 /// this to be true and invalidate the object pointed to by inner.
890 impl Drop for PeerManager {
892 if self.is_owned && !<*mut nativePeerManager>::is_null(self.inner) {
893 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
897 /// Frees any resources used by the PeerManager, if is_owned is set and inner is non-NULL.
899 pub extern "C" fn PeerManager_free(this_obj: PeerManager) { }
901 /// Used only if an object of this type is returned as a trait impl by a method
902 pub(crate) extern "C" fn PeerManager_free_void(this_ptr: *mut c_void) {
903 unsafe { let _ = Box::from_raw(this_ptr as *mut nativePeerManager); }
907 pub(crate) fn get_native_ref(&self) -> &'static nativePeerManager {
908 unsafe { &*ObjOps::untweak_ptr(self.inner) }
910 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePeerManager {
911 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
913 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
914 pub(crate) fn take_inner(mut self) -> *mut nativePeerManager {
915 assert!(self.is_owned);
916 let ret = ObjOps::untweak_ptr(self.inner);
917 self.inner = std::ptr::null_mut();
921 /// Constructs a new PeerManager with the given message handlers and node_id secret key
922 /// ephemeral_random_data is used to derive per-connection ephemeral keys and must be
923 /// cryptographically secure random bytes.
926 pub extern "C" fn PeerManager_new(mut message_handler: crate::lightning::ln::peer_handler::MessageHandler, mut our_node_secret: crate::c_types::SecretKey, ephemeral_random_data: *const [u8; 32], mut logger: crate::lightning::util::logger::Logger, mut custom_message_handler: crate::lightning::ln::peer_handler::CustomMessageHandler) -> PeerManager {
927 let mut ret = lightning::ln::peer_handler::PeerManager::new(*unsafe { Box::from_raw(message_handler.take_inner()) }, our_node_secret.into_rust(), unsafe { &*ephemeral_random_data}, logger, custom_message_handler);
928 PeerManager { inner: ObjOps::heap_alloc(ret), is_owned: true }
931 /// Get the list of node ids for peers which have completed the initial handshake.
933 /// For outbound connections, this will be the same as the their_node_id parameter passed in to
934 /// new_outbound_connection, however entries will only appear once the initial handshake has
935 /// completed and we are sure the remote peer has the private key for the given node_id.
938 pub extern "C" fn PeerManager_get_peer_node_ids(this_arg: &PeerManager) -> crate::c_types::derived::CVec_PublicKeyZ {
939 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_peer_node_ids();
940 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::c_types::PublicKey::from_rust(&item) }); };
944 /// Indicates a new outbound connection has been established to a node with the given node_id.
945 /// Note that if an Err is returned here you MUST NOT call socket_disconnected for the new
946 /// descriptor but must disconnect the connection immediately.
948 /// Returns a small number of bytes to send to the remote node (currently always 50).
950 /// Panics if descriptor is duplicative with some other descriptor which has not yet been
951 /// [`socket_disconnected()`].
953 /// [`socket_disconnected()`]: PeerManager::socket_disconnected
956 pub extern "C" fn PeerManager_new_outbound_connection(this_arg: &PeerManager, mut their_node_id: crate::c_types::PublicKey, mut descriptor: crate::lightning::ln::peer_handler::SocketDescriptor) -> crate::c_types::derived::CResult_CVec_u8ZPeerHandleErrorZ {
957 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.new_outbound_connection(their_node_id.into_rust(), descriptor);
958 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { item }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::peer_handler::PeerHandleError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
962 /// Indicates a new inbound connection has been established.
964 /// May refuse the connection by returning an Err, but will never write bytes to the remote end
965 /// (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
966 /// call socket_disconnected for the new descriptor but must disconnect the connection
969 /// Panics if descriptor is duplicative with some other descriptor which has not yet been
970 /// [`socket_disconnected()`].
972 /// [`socket_disconnected()`]: PeerManager::socket_disconnected
975 pub extern "C" fn PeerManager_new_inbound_connection(this_arg: &PeerManager, mut descriptor: crate::lightning::ln::peer_handler::SocketDescriptor) -> crate::c_types::derived::CResult_NonePeerHandleErrorZ {
976 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.new_inbound_connection(descriptor);
977 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::ln::peer_handler::PeerHandleError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
981 /// Indicates that there is room to write data to the given socket descriptor.
983 /// May return an Err to indicate that the connection should be closed.
985 /// May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
986 /// returning. Thus, be very careful with reentrancy issues! The invariants around calling
987 /// [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
988 /// ready to call `[write_buffer_space_avail`] again if a write call generated here isn't
991 /// [`send_data`]: SocketDescriptor::send_data
992 /// [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
995 pub extern "C" fn PeerManager_write_buffer_space_avail(this_arg: &PeerManager, descriptor: &mut crate::lightning::ln::peer_handler::SocketDescriptor) -> crate::c_types::derived::CResult_NonePeerHandleErrorZ {
996 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.write_buffer_space_avail(descriptor);
997 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::ln::peer_handler::PeerHandleError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
1001 /// Indicates that data was read from the given socket descriptor.
1003 /// May return an Err to indicate that the connection should be closed.
1005 /// Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity.
1006 /// Thus, however, you should call [`process_events`] after any `read_event` to generate
1007 /// [`send_data`] calls to handle responses.
1009 /// If `Ok(true)` is returned, further read_events should not be triggered until a
1010 /// [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
1013 /// [`send_data`]: SocketDescriptor::send_data
1014 /// [`process_events`]: PeerManager::process_events
1017 pub extern "C" fn PeerManager_read_event(this_arg: &PeerManager, peer_descriptor: &mut crate::lightning::ln::peer_handler::SocketDescriptor, mut data: crate::c_types::u8slice) -> crate::c_types::derived::CResult_boolPeerHandleErrorZ {
1018 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.read_event(peer_descriptor, data.to_slice());
1019 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::ln::peer_handler::PeerHandleError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
1023 /// Checks for any events generated by our handlers and processes them. Includes sending most
1024 /// response messages as well as messages generated by calls to handler functions directly (eg
1025 /// functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
1027 /// May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
1030 /// You don't have to call this function explicitly if you are using [`lightning-net-tokio`]
1031 /// or one of the other clients provided in our language bindings.
1033 /// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
1034 /// [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
1035 /// [`send_data`]: SocketDescriptor::send_data
1037 pub extern "C" fn PeerManager_process_events(this_arg: &PeerManager) {
1038 unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.process_events()
1041 /// Indicates that the given socket descriptor's connection is now closed.
1043 pub extern "C" fn PeerManager_socket_disconnected(this_arg: &PeerManager, descriptor: &crate::lightning::ln::peer_handler::SocketDescriptor) {
1044 unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.socket_disconnected(descriptor)
1047 /// Disconnect a peer given its node id.
1049 /// Set `no_connection_possible` to true to prevent any further connection with this peer,
1050 /// force-closing any channels we have with it.
1052 /// If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
1053 /// peer. Thus, be very careful about reentrancy issues.
1055 /// [`disconnect_socket`]: SocketDescriptor::disconnect_socket
1057 pub extern "C" fn PeerManager_disconnect_by_node_id(this_arg: &PeerManager, mut node_id: crate::c_types::PublicKey, mut no_connection_possible: bool) {
1058 unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.disconnect_by_node_id(node_id.into_rust(), no_connection_possible)
1061 /// Disconnects all currently-connected peers. This is useful on platforms where there may be
1062 /// an indication that TCP sockets have stalled even if we weren't around to time them out
1063 /// using regular ping/pongs.
1065 pub extern "C" fn PeerManager_disconnect_all_peers(this_arg: &PeerManager) {
1066 unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.disconnect_all_peers()
1069 /// Send pings to each peer and disconnect those which did not respond to the last round of
1072 /// This may be called on any timescale you want, however, roughly once every five to ten
1073 /// seconds is preferred. The call rate determines both how often we send a ping to our peers
1074 /// and how much time they have to respond before we disconnect them.
1076 /// May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
1079 /// [`send_data`]: SocketDescriptor::send_data
1081 pub extern "C" fn PeerManager_timer_tick_occurred(this_arg: &PeerManager) {
1082 unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.timer_tick_occurred()