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.
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
22 use lightning::ln::peer_handler::IgnoringMessageHandler as nativeIgnoringMessageHandlerImport;
23 type nativeIgnoringMessageHandler = nativeIgnoringMessageHandlerImport;
25 /// A dummy struct which implements `RoutingMessageHandler` without storing any routing information
26 /// or doing any processing. You can provide one of these as the route_handler in a MessageHandler.
29 pub struct IgnoringMessageHandler {
30 /// A pointer to the opaque Rust object.
32 /// Nearly everywhere, inner must be non-null, however in places where
33 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
34 pub inner: *mut nativeIgnoringMessageHandler,
35 /// Indicates that this is the only struct which contains the same pointer.
37 /// Rust functions which take ownership of an object provided via an argument require
38 /// this to be true and invalidate the object pointed to by inner.
42 impl Drop for IgnoringMessageHandler {
44 if self.is_owned && !<*mut nativeIgnoringMessageHandler>::is_null(self.inner) {
45 let _ = unsafe { Box::from_raw(self.inner) };
49 /// Frees any resources used by the IgnoringMessageHandler, if is_owned is set and inner is non-NULL.
51 pub extern "C" fn IgnoringMessageHandler_free(this_obj: IgnoringMessageHandler) { }
53 /// Used only if an object of this type is returned as a trait impl by a method
54 extern "C" fn IgnoringMessageHandler_free_void(this_ptr: *mut c_void) {
55 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeIgnoringMessageHandler); }
58 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
59 impl IgnoringMessageHandler {
60 pub(crate) fn take_inner(mut self) -> *mut nativeIgnoringMessageHandler {
61 assert!(self.is_owned);
63 self.inner = std::ptr::null_mut();
67 /// Constructs a new IgnoringMessageHandler given each field
70 pub extern "C" fn IgnoringMessageHandler_new() -> IgnoringMessageHandler {
71 IgnoringMessageHandler { inner: Box::into_raw(Box::new(nativeIgnoringMessageHandler {
74 impl From<nativeIgnoringMessageHandler> for crate::util::events::MessageSendEventsProvider {
75 fn from(obj: nativeIgnoringMessageHandler) -> Self {
76 let mut rust_obj = IgnoringMessageHandler { inner: Box::into_raw(Box::new(obj)), is_owned: true };
77 let mut ret = IgnoringMessageHandler_as_MessageSendEventsProvider(&rust_obj);
78 // 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
79 rust_obj.inner = std::ptr::null_mut();
80 ret.free = Some(IgnoringMessageHandler_free_void);
84 /// Constructs a new MessageSendEventsProvider which calls the relevant methods on this_arg.
85 /// This copies the `inner` pointer in this_arg and thus the returned MessageSendEventsProvider must be freed before this_arg is
87 pub extern "C" fn IgnoringMessageHandler_as_MessageSendEventsProvider(this_arg: &IgnoringMessageHandler) -> crate::util::events::MessageSendEventsProvider {
88 crate::util::events::MessageSendEventsProvider {
89 this_arg: unsafe { (*this_arg).inner as *mut c_void },
91 get_and_clear_pending_msg_events: IgnoringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
96 extern "C" fn IgnoringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MessageSendEventZ {
97 let mut ret = <nativeIgnoringMessageHandler as lightning::util::events::MessageSendEventsProvider<>>::get_and_clear_pending_msg_events(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, );
98 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::util::events::MessageSendEvent::native_into(item) }); };
102 impl From<nativeIgnoringMessageHandler> for crate::ln::msgs::RoutingMessageHandler {
103 fn from(obj: nativeIgnoringMessageHandler) -> Self {
104 let mut rust_obj = IgnoringMessageHandler { inner: Box::into_raw(Box::new(obj)), is_owned: true };
105 let mut ret = IgnoringMessageHandler_as_RoutingMessageHandler(&rust_obj);
106 // 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
107 rust_obj.inner = std::ptr::null_mut();
108 ret.free = Some(IgnoringMessageHandler_free_void);
112 /// Constructs a new RoutingMessageHandler which calls the relevant methods on this_arg.
113 /// This copies the `inner` pointer in this_arg and thus the returned RoutingMessageHandler must be freed before this_arg is
115 pub extern "C" fn IgnoringMessageHandler_as_RoutingMessageHandler(this_arg: &IgnoringMessageHandler) -> crate::ln::msgs::RoutingMessageHandler {
116 crate::ln::msgs::RoutingMessageHandler {
117 this_arg: unsafe { (*this_arg).inner as *mut c_void },
119 handle_node_announcement: IgnoringMessageHandler_RoutingMessageHandler_handle_node_announcement,
120 handle_channel_announcement: IgnoringMessageHandler_RoutingMessageHandler_handle_channel_announcement,
121 handle_channel_update: IgnoringMessageHandler_RoutingMessageHandler_handle_channel_update,
122 handle_htlc_fail_channel_update: IgnoringMessageHandler_RoutingMessageHandler_handle_htlc_fail_channel_update,
123 get_next_channel_announcements: IgnoringMessageHandler_RoutingMessageHandler_get_next_channel_announcements,
124 get_next_node_announcements: IgnoringMessageHandler_RoutingMessageHandler_get_next_node_announcements,
125 sync_routing_table: IgnoringMessageHandler_RoutingMessageHandler_sync_routing_table,
126 handle_reply_channel_range: IgnoringMessageHandler_RoutingMessageHandler_handle_reply_channel_range,
127 handle_reply_short_channel_ids_end: IgnoringMessageHandler_RoutingMessageHandler_handle_reply_short_channel_ids_end,
128 handle_query_channel_range: IgnoringMessageHandler_RoutingMessageHandler_handle_query_channel_range,
129 handle_query_short_channel_ids: IgnoringMessageHandler_RoutingMessageHandler_handle_query_short_channel_ids,
130 MessageSendEventsProvider: crate::util::events::MessageSendEventsProvider {
131 this_arg: unsafe { (*this_arg).inner as *mut c_void },
133 get_and_clear_pending_msg_events: IgnoringMessageHandler_RoutingMessageHandler_get_and_clear_pending_msg_events,
139 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_node_announcement(this_arg: *const c_void, _msg: &crate::ln::msgs::NodeAnnouncement) -> crate::c_types::derived::CResult_boolLightningErrorZ {
140 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_node_announcement(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, unsafe { &*_msg.inner });
141 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::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
145 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_channel_announcement(this_arg: *const c_void, _msg: &crate::ln::msgs::ChannelAnnouncement) -> crate::c_types::derived::CResult_boolLightningErrorZ {
146 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_channel_announcement(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, unsafe { &*_msg.inner });
147 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::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
151 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_channel_update(this_arg: *const c_void, _msg: &crate::ln::msgs::ChannelUpdate) -> crate::c_types::derived::CResult_boolLightningErrorZ {
152 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_channel_update(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, unsafe { &*_msg.inner });
153 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::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
156 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_htlc_fail_channel_update(this_arg: *const c_void, _update: &crate::ln::msgs::HTLCFailChannelUpdate) {
157 <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_htlc_fail_channel_update(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, &_update.to_native())
160 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 {
161 let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::get_next_channel_announcements(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, _starting_point, _batch_amount);
162 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::ln::msgs::ChannelUpdate { inner: if orig_ret_0_1.is_none() { std::ptr::null_mut() } else { { Box::into_raw(Box::new((orig_ret_0_1.unwrap()))) } }, is_owned: true }; let mut local_orig_ret_0_2 = crate::ln::msgs::ChannelUpdate { inner: if orig_ret_0_2.is_none() { std::ptr::null_mut() } else { { Box::into_raw(Box::new((orig_ret_0_2.unwrap()))) } }, is_owned: true }; let mut local_ret_0 = (crate::ln::msgs::ChannelAnnouncement { inner: Box::into_raw(Box::new(orig_ret_0_0)), is_owned: true }, local_orig_ret_0_1, local_orig_ret_0_2).into(); local_ret_0 }); };
166 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 {
167 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();
168 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);
169 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::ln::msgs::NodeAnnouncement { inner: Box::into_raw(Box::new(item)), is_owned: true } }); };
172 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_sync_routing_table(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, _init: &crate::ln::msgs::Init) {
173 <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::sync_routing_table(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, &_their_node_id.into_rust(), unsafe { &*_init.inner })
176 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::ln::msgs::ReplyChannelRange) -> crate::c_types::derived::CResult_NoneLightningErrorZ {
177 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()) });
178 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
182 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::ln::msgs::ReplyShortChannelIdsEnd) -> crate::c_types::derived::CResult_NoneLightningErrorZ {
183 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()) });
184 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
188 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::ln::msgs::QueryChannelRange) -> crate::c_types::derived::CResult_NoneLightningErrorZ {
189 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()) });
190 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
194 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::ln::msgs::QueryShortChannelIds) -> crate::c_types::derived::CResult_NoneLightningErrorZ {
195 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()) });
196 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
200 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_get_and_clear_pending_msg_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MessageSendEventZ {
201 let mut ret = <nativeIgnoringMessageHandler as lightning::util::events::MessageSendEventsProvider<>>::get_and_clear_pending_msg_events(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, );
202 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::util::events::MessageSendEvent::native_into(item) }); };
207 use lightning::ln::peer_handler::ErroringMessageHandler as nativeErroringMessageHandlerImport;
208 type nativeErroringMessageHandler = nativeErroringMessageHandlerImport;
210 /// A dummy struct which implements `ChannelMessageHandler` without having any channels.
211 /// You can provide one of these as the route_handler in a MessageHandler.
214 pub struct ErroringMessageHandler {
215 /// A pointer to the opaque Rust object.
217 /// Nearly everywhere, inner must be non-null, however in places where
218 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
219 pub inner: *mut nativeErroringMessageHandler,
220 /// Indicates that this is the only struct which contains the same pointer.
222 /// Rust functions which take ownership of an object provided via an argument require
223 /// this to be true and invalidate the object pointed to by inner.
227 impl Drop for ErroringMessageHandler {
229 if self.is_owned && !<*mut nativeErroringMessageHandler>::is_null(self.inner) {
230 let _ = unsafe { Box::from_raw(self.inner) };
234 /// Frees any resources used by the ErroringMessageHandler, if is_owned is set and inner is non-NULL.
236 pub extern "C" fn ErroringMessageHandler_free(this_obj: ErroringMessageHandler) { }
238 /// Used only if an object of this type is returned as a trait impl by a method
239 extern "C" fn ErroringMessageHandler_free_void(this_ptr: *mut c_void) {
240 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeErroringMessageHandler); }
243 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
244 impl ErroringMessageHandler {
245 pub(crate) fn take_inner(mut self) -> *mut nativeErroringMessageHandler {
246 assert!(self.is_owned);
247 let ret = self.inner;
248 self.inner = std::ptr::null_mut();
252 /// Constructs a new ErroringMessageHandler
255 pub extern "C" fn ErroringMessageHandler_new() -> ErroringMessageHandler {
256 let mut ret = lightning::ln::peer_handler::ErroringMessageHandler::new();
257 ErroringMessageHandler { inner: Box::into_raw(Box::new(ret)), is_owned: true }
260 impl From<nativeErroringMessageHandler> for crate::util::events::MessageSendEventsProvider {
261 fn from(obj: nativeErroringMessageHandler) -> Self {
262 let mut rust_obj = ErroringMessageHandler { inner: Box::into_raw(Box::new(obj)), is_owned: true };
263 let mut ret = ErroringMessageHandler_as_MessageSendEventsProvider(&rust_obj);
264 // 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
265 rust_obj.inner = std::ptr::null_mut();
266 ret.free = Some(ErroringMessageHandler_free_void);
270 /// Constructs a new MessageSendEventsProvider which calls the relevant methods on this_arg.
271 /// This copies the `inner` pointer in this_arg and thus the returned MessageSendEventsProvider must be freed before this_arg is
273 pub extern "C" fn ErroringMessageHandler_as_MessageSendEventsProvider(this_arg: &ErroringMessageHandler) -> crate::util::events::MessageSendEventsProvider {
274 crate::util::events::MessageSendEventsProvider {
275 this_arg: unsafe { (*this_arg).inner as *mut c_void },
277 get_and_clear_pending_msg_events: ErroringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
282 extern "C" fn ErroringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MessageSendEventZ {
283 let mut ret = <nativeErroringMessageHandler as lightning::util::events::MessageSendEventsProvider<>>::get_and_clear_pending_msg_events(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, );
284 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::util::events::MessageSendEvent::native_into(item) }); };
288 impl From<nativeErroringMessageHandler> for crate::ln::msgs::ChannelMessageHandler {
289 fn from(obj: nativeErroringMessageHandler) -> Self {
290 let mut rust_obj = ErroringMessageHandler { inner: Box::into_raw(Box::new(obj)), is_owned: true };
291 let mut ret = ErroringMessageHandler_as_ChannelMessageHandler(&rust_obj);
292 // 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
293 rust_obj.inner = std::ptr::null_mut();
294 ret.free = Some(ErroringMessageHandler_free_void);
298 /// Constructs a new ChannelMessageHandler which calls the relevant methods on this_arg.
299 /// This copies the `inner` pointer in this_arg and thus the returned ChannelMessageHandler must be freed before this_arg is
301 pub extern "C" fn ErroringMessageHandler_as_ChannelMessageHandler(this_arg: &ErroringMessageHandler) -> crate::ln::msgs::ChannelMessageHandler {
302 crate::ln::msgs::ChannelMessageHandler {
303 this_arg: unsafe { (*this_arg).inner as *mut c_void },
305 handle_open_channel: ErroringMessageHandler_ChannelMessageHandler_handle_open_channel,
306 handle_accept_channel: ErroringMessageHandler_ChannelMessageHandler_handle_accept_channel,
307 handle_funding_created: ErroringMessageHandler_ChannelMessageHandler_handle_funding_created,
308 handle_funding_signed: ErroringMessageHandler_ChannelMessageHandler_handle_funding_signed,
309 handle_funding_locked: ErroringMessageHandler_ChannelMessageHandler_handle_funding_locked,
310 handle_shutdown: ErroringMessageHandler_ChannelMessageHandler_handle_shutdown,
311 handle_closing_signed: ErroringMessageHandler_ChannelMessageHandler_handle_closing_signed,
312 handle_update_add_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_add_htlc,
313 handle_update_fulfill_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_fulfill_htlc,
314 handle_update_fail_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_fail_htlc,
315 handle_update_fail_malformed_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_fail_malformed_htlc,
316 handle_commitment_signed: ErroringMessageHandler_ChannelMessageHandler_handle_commitment_signed,
317 handle_revoke_and_ack: ErroringMessageHandler_ChannelMessageHandler_handle_revoke_and_ack,
318 handle_update_fee: ErroringMessageHandler_ChannelMessageHandler_handle_update_fee,
319 handle_announcement_signatures: ErroringMessageHandler_ChannelMessageHandler_handle_announcement_signatures,
320 peer_disconnected: ErroringMessageHandler_ChannelMessageHandler_peer_disconnected,
321 peer_connected: ErroringMessageHandler_ChannelMessageHandler_peer_connected,
322 handle_channel_reestablish: ErroringMessageHandler_ChannelMessageHandler_handle_channel_reestablish,
323 handle_channel_update: ErroringMessageHandler_ChannelMessageHandler_handle_channel_update,
324 handle_error: ErroringMessageHandler_ChannelMessageHandler_handle_error,
325 MessageSendEventsProvider: crate::util::events::MessageSendEventsProvider {
326 this_arg: unsafe { (*this_arg).inner as *mut c_void },
328 get_and_clear_pending_msg_events: ErroringMessageHandler_ChannelMessageHandler_get_and_clear_pending_msg_events,
333 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::ln::features::InitFeatures, msg: &crate::ln::msgs::OpenChannel) {
334 <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()) }, unsafe { &*msg.inner })
336 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::ln::features::InitFeatures, msg: &crate::ln::msgs::AcceptChannel) {
337 <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()) }, unsafe { &*msg.inner })
339 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_funding_created(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::FundingCreated) {
340 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_funding_created(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
342 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_funding_signed(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::FundingSigned) {
343 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_funding_signed(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
345 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_funding_locked(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::FundingLocked) {
346 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_funding_locked(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
348 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_shutdown(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, _their_features: &crate::ln::features::InitFeatures, msg: &crate::ln::msgs::Shutdown) {
349 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_shutdown(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*_their_features.inner }, unsafe { &*msg.inner })
351 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_closing_signed(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::ClosingSigned) {
352 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_closing_signed(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
354 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_update_add_htlc(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::UpdateAddHTLC) {
355 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_add_htlc(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
357 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_update_fulfill_htlc(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::UpdateFulfillHTLC) {
358 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_fulfill_htlc(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
360 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_update_fail_htlc(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::UpdateFailHTLC) {
361 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_fail_htlc(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
363 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::ln::msgs::UpdateFailMalformedHTLC) {
364 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_fail_malformed_htlc(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
366 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_commitment_signed(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::CommitmentSigned) {
367 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_commitment_signed(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
369 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_revoke_and_ack(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::RevokeAndACK) {
370 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_revoke_and_ack(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
372 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_update_fee(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::UpdateFee) {
373 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_fee(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
375 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_announcement_signatures(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::AnnouncementSignatures) {
376 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_announcement_signatures(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
378 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_channel_reestablish(this_arg: *const c_void, mut their_node_id: crate::c_types::PublicKey, msg: &crate::ln::msgs::ChannelReestablish) {
379 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_channel_reestablish(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
381 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_channel_update(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, _msg: &crate::ln::msgs::ChannelUpdate) {
382 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_channel_update(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), unsafe { &*_msg.inner })
384 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) {
385 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::peer_disconnected(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), _no_connection_possible)
387 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_peer_connected(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, _msg: &crate::ln::msgs::Init) {
388 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::peer_connected(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), unsafe { &*_msg.inner })
390 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_handle_error(this_arg: *const c_void, mut _their_node_id: crate::c_types::PublicKey, _msg: &crate::ln::msgs::ErrorMessage) {
391 <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_error(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), unsafe { &*_msg.inner })
394 extern "C" fn ErroringMessageHandler_ChannelMessageHandler_get_and_clear_pending_msg_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MessageSendEventZ {
395 let mut ret = <nativeErroringMessageHandler as lightning::util::events::MessageSendEventsProvider<>>::get_and_clear_pending_msg_events(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, );
396 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::util::events::MessageSendEvent::native_into(item) }); };
401 use lightning::ln::peer_handler::MessageHandler as nativeMessageHandlerImport;
402 type nativeMessageHandler = nativeMessageHandlerImport<crate::ln::msgs::ChannelMessageHandler, crate::ln::msgs::RoutingMessageHandler>;
404 /// Provides references to trait impls which handle different types of messages.
407 pub struct MessageHandler {
408 /// A pointer to the opaque Rust object.
410 /// Nearly everywhere, inner must be non-null, however in places where
411 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
412 pub inner: *mut nativeMessageHandler,
413 /// Indicates that this is the only struct which contains the same pointer.
415 /// Rust functions which take ownership of an object provided via an argument require
416 /// this to be true and invalidate the object pointed to by inner.
420 impl Drop for MessageHandler {
422 if self.is_owned && !<*mut nativeMessageHandler>::is_null(self.inner) {
423 let _ = unsafe { Box::from_raw(self.inner) };
427 /// Frees any resources used by the MessageHandler, if is_owned is set and inner is non-NULL.
429 pub extern "C" fn MessageHandler_free(this_obj: MessageHandler) { }
431 /// Used only if an object of this type is returned as a trait impl by a method
432 extern "C" fn MessageHandler_free_void(this_ptr: *mut c_void) {
433 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMessageHandler); }
436 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
437 impl MessageHandler {
438 pub(crate) fn take_inner(mut self) -> *mut nativeMessageHandler {
439 assert!(self.is_owned);
440 let ret = self.inner;
441 self.inner = std::ptr::null_mut();
445 /// A message handler which handles messages specific to channels. Usually this is just a
446 /// ChannelManager object or a ErroringMessageHandler.
448 pub extern "C" fn MessageHandler_get_chan_handler(this_ptr: &MessageHandler) -> *const crate::ln::msgs::ChannelMessageHandler {
449 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.chan_handler;
452 /// A message handler which handles messages specific to channels. Usually this is just a
453 /// ChannelManager object or a ErroringMessageHandler.
455 pub extern "C" fn MessageHandler_set_chan_handler(this_ptr: &mut MessageHandler, mut val: crate::ln::msgs::ChannelMessageHandler) {
456 unsafe { &mut *this_ptr.inner }.chan_handler = val;
458 /// A message handler which handles messages updating our knowledge of the network channel
459 /// graph. Usually this is just a NetGraphMsgHandlerMonitor object or an IgnoringMessageHandler.
461 pub extern "C" fn MessageHandler_get_route_handler(this_ptr: &MessageHandler) -> *const crate::ln::msgs::RoutingMessageHandler {
462 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.route_handler;
465 /// A message handler which handles messages updating our knowledge of the network channel
466 /// graph. Usually this is just a NetGraphMsgHandlerMonitor object or an IgnoringMessageHandler.
468 pub extern "C" fn MessageHandler_set_route_handler(this_ptr: &mut MessageHandler, mut val: crate::ln::msgs::RoutingMessageHandler) {
469 unsafe { &mut *this_ptr.inner }.route_handler = val;
471 /// Constructs a new MessageHandler given each field
474 pub extern "C" fn MessageHandler_new(mut chan_handler_arg: crate::ln::msgs::ChannelMessageHandler, mut route_handler_arg: crate::ln::msgs::RoutingMessageHandler) -> MessageHandler {
475 MessageHandler { inner: Box::into_raw(Box::new(nativeMessageHandler {
476 chan_handler: chan_handler_arg,
477 route_handler: route_handler_arg,
478 })), is_owned: true }
480 /// Provides an object which can be used to send data to and which uniquely identifies a connection
481 /// to a remote host. You will need to be able to generate multiple of these which meet Eq and
482 /// implement Hash to meet the PeerManager API.
484 /// For efficiency, Clone should be relatively cheap for this type.
486 /// You probably want to just extend an int and put a file descriptor in a struct and implement
487 /// send_data. Note that if you are using a higher-level net library that may call close() itself,
488 /// be careful to ensure you don't have races whereby you might register a new connection with an
489 /// fd which is the same as a previous one which has yet to be removed via
490 /// PeerManager::socket_disconnected().
492 pub struct SocketDescriptor {
493 /// An opaque pointer which is passed to your function implementations as an argument.
494 /// This has no meaning in the LDK, and can be NULL or any other value.
495 pub this_arg: *mut c_void,
496 /// Attempts to send some data from the given slice to the peer.
498 /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
499 /// Note that in the disconnected case, socket_disconnected must still fire and further write
500 /// attempts may occur until that time.
502 /// If the returned size is smaller than data.len(), a write_available event must
503 /// trigger the next time more data can be written. Additionally, until the a send_data event
504 /// completes fully, no further read_events should trigger on the same peer!
506 /// If a read_event on this descriptor had previously returned true (indicating that read
507 /// events should be paused to prevent DoS in the send buffer), resume_read may be set
508 /// indicating that read events on this descriptor should resume. A resume_read of false does
509 /// *not* imply that further read events should be paused.
511 pub send_data: extern "C" fn (this_arg: *mut c_void, data: crate::c_types::u8slice, resume_read: bool) -> usize,
512 /// Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
513 /// more calls to write_buffer_space_avail, read_event or socket_disconnected may be made with
514 /// this descriptor. No socket_disconnected call should be generated as a result of this call,
515 /// though races may occur whereby disconnect_socket is called after a call to
516 /// socket_disconnected but prior to socket_disconnected returning.
517 pub disconnect_socket: extern "C" fn (this_arg: *mut c_void),
518 /// Checks if two objects are equal given this object's this_arg pointer and another object.
519 pub eq: extern "C" fn (this_arg: *const c_void, other_arg: &SocketDescriptor) -> bool,
520 /// Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
521 /// This is used, for example, for inclusion of this object in a hash map.
522 pub hash: extern "C" fn (this_arg: *const c_void) -> u64,
523 /// Creates a copy of the object pointed to by this_arg, for a copy of this SocketDescriptor.
524 /// Note that the ultimate copy of the SocketDescriptor will have all function pointers the same as the original.
525 /// May be NULL if no action needs to be taken, the this_arg pointer will be copied into the new SocketDescriptor.
526 pub clone: Option<extern "C" fn (this_arg: *const c_void) -> *mut c_void>,
527 /// Frees any resources associated with this object given its this_arg pointer.
528 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
529 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
531 impl std::cmp::Eq for SocketDescriptor {}
532 impl std::cmp::PartialEq for SocketDescriptor {
533 fn eq(&self, o: &Self) -> bool { (self.eq)(self.this_arg, o) }
535 impl std::hash::Hash for SocketDescriptor {
536 fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64((self.hash)(self.this_arg)) }
539 /// Creates a copy of a SocketDescriptor
540 pub extern "C" fn SocketDescriptor_clone(orig: &SocketDescriptor) -> SocketDescriptor {
542 this_arg: if let Some(f) = orig.clone { (f)(orig.this_arg) } else { orig.this_arg },
543 send_data: orig.send_data.clone(),
544 disconnect_socket: orig.disconnect_socket.clone(),
546 hash: orig.hash.clone(),
547 clone: orig.clone.clone(),
548 free: orig.free.clone(),
551 impl Clone for SocketDescriptor {
552 fn clone(&self) -> Self {
553 SocketDescriptor_clone(self)
557 use lightning::ln::peer_handler::SocketDescriptor as rustSocketDescriptor;
558 impl rustSocketDescriptor for SocketDescriptor {
559 fn send_data(&mut self, data: &[u8], resume_read: bool) -> usize {
560 let mut local_data = crate::c_types::u8slice::from_slice(data);
561 let mut ret = (self.send_data)(self.this_arg, local_data, resume_read);
564 fn disconnect_socket(&mut self) {
565 (self.disconnect_socket)(self.this_arg)
569 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
570 // directly as a Deref trait in higher-level structs:
571 impl std::ops::Deref for SocketDescriptor {
573 fn deref(&self) -> &Self {
577 /// Calls the free function if one is set
579 pub extern "C" fn SocketDescriptor_free(this_ptr: SocketDescriptor) { }
580 impl Drop for SocketDescriptor {
582 if let Some(f) = self.free {
588 use lightning::ln::peer_handler::PeerHandleError as nativePeerHandleErrorImport;
589 type nativePeerHandleError = nativePeerHandleErrorImport;
591 /// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
592 /// generate no further read_event/write_buffer_space_avail/socket_disconnected calls for the
596 pub struct PeerHandleError {
597 /// A pointer to the opaque Rust object.
599 /// Nearly everywhere, inner must be non-null, however in places where
600 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
601 pub inner: *mut nativePeerHandleError,
602 /// Indicates that this is the only struct which contains the same pointer.
604 /// Rust functions which take ownership of an object provided via an argument require
605 /// this to be true and invalidate the object pointed to by inner.
609 impl Drop for PeerHandleError {
611 if self.is_owned && !<*mut nativePeerHandleError>::is_null(self.inner) {
612 let _ = unsafe { Box::from_raw(self.inner) };
616 /// Frees any resources used by the PeerHandleError, if is_owned is set and inner is non-NULL.
618 pub extern "C" fn PeerHandleError_free(this_obj: PeerHandleError) { }
620 /// Used only if an object of this type is returned as a trait impl by a method
621 extern "C" fn PeerHandleError_free_void(this_ptr: *mut c_void) {
622 unsafe { let _ = Box::from_raw(this_ptr as *mut nativePeerHandleError); }
625 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
626 impl PeerHandleError {
627 pub(crate) fn take_inner(mut self) -> *mut nativePeerHandleError {
628 assert!(self.is_owned);
629 let ret = self.inner;
630 self.inner = std::ptr::null_mut();
634 /// Used to indicate that we probably can't make any future connections to this peer, implying
635 /// we should go ahead and force-close any channels we have with it.
637 pub extern "C" fn PeerHandleError_get_no_connection_possible(this_ptr: &PeerHandleError) -> bool {
638 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.no_connection_possible;
641 /// Used to indicate that we probably can't make any future connections to this peer, implying
642 /// we should go ahead and force-close any channels we have with it.
644 pub extern "C" fn PeerHandleError_set_no_connection_possible(this_ptr: &mut PeerHandleError, mut val: bool) {
645 unsafe { &mut *this_ptr.inner }.no_connection_possible = val;
647 /// Constructs a new PeerHandleError given each field
650 pub extern "C" fn PeerHandleError_new(mut no_connection_possible_arg: bool) -> PeerHandleError {
651 PeerHandleError { inner: Box::into_raw(Box::new(nativePeerHandleError {
652 no_connection_possible: no_connection_possible_arg,
653 })), is_owned: true }
655 impl Clone for PeerHandleError {
656 fn clone(&self) -> Self {
658 inner: if <*mut nativePeerHandleError>::is_null(self.inner) { std::ptr::null_mut() } else {
659 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
665 /// Used only if an object of this type is returned as a trait impl by a method
666 pub(crate) extern "C" fn PeerHandleError_clone_void(this_ptr: *const c_void) -> *mut c_void {
667 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativePeerHandleError)).clone() })) as *mut c_void
670 /// Creates a copy of the PeerHandleError
671 pub extern "C" fn PeerHandleError_clone(orig: &PeerHandleError) -> PeerHandleError {
675 use lightning::ln::peer_handler::PeerManager as nativePeerManagerImport;
676 type nativePeerManager = nativePeerManagerImport<crate::ln::peer_handler::SocketDescriptor, crate::ln::msgs::ChannelMessageHandler, crate::ln::msgs::RoutingMessageHandler, crate::util::logger::Logger>;
678 /// A PeerManager manages a set of peers, described by their SocketDescriptor and marshalls socket
679 /// events into messages which it passes on to its MessageHandlers.
681 /// Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
682 /// a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
683 /// essentially you should default to using a SimpleRefPeerManager, and use a
684 /// SimpleArcPeerManager when you require a PeerManager with a static lifetime, such as when
685 /// you're using lightning-net-tokio.
688 pub struct PeerManager {
689 /// A pointer to the opaque Rust object.
691 /// Nearly everywhere, inner must be non-null, however in places where
692 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
693 pub inner: *mut nativePeerManager,
694 /// Indicates that this is the only struct which contains the same pointer.
696 /// Rust functions which take ownership of an object provided via an argument require
697 /// this to be true and invalidate the object pointed to by inner.
701 impl Drop for PeerManager {
703 if self.is_owned && !<*mut nativePeerManager>::is_null(self.inner) {
704 let _ = unsafe { Box::from_raw(self.inner) };
708 /// Frees any resources used by the PeerManager, if is_owned is set and inner is non-NULL.
710 pub extern "C" fn PeerManager_free(this_obj: PeerManager) { }
712 /// Used only if an object of this type is returned as a trait impl by a method
713 extern "C" fn PeerManager_free_void(this_ptr: *mut c_void) {
714 unsafe { let _ = Box::from_raw(this_ptr as *mut nativePeerManager); }
717 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
719 pub(crate) fn take_inner(mut self) -> *mut nativePeerManager {
720 assert!(self.is_owned);
721 let ret = self.inner;
722 self.inner = std::ptr::null_mut();
726 /// Constructs a new PeerManager with the given message handlers and node_id secret key
727 /// ephemeral_random_data is used to derive per-connection ephemeral keys and must be
728 /// cryptographically secure random bytes.
731 pub extern "C" fn PeerManager_new(mut message_handler: crate::ln::peer_handler::MessageHandler, mut our_node_secret: crate::c_types::SecretKey, ephemeral_random_data: *const [u8; 32], mut logger: crate::util::logger::Logger) -> PeerManager {
732 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);
733 PeerManager { inner: Box::into_raw(Box::new(ret)), is_owned: true }
736 /// Get the list of node ids for peers which have completed the initial handshake.
738 /// For outbound connections, this will be the same as the their_node_id parameter passed in to
739 /// new_outbound_connection, however entries will only appear once the initial handshake has
740 /// completed and we are sure the remote peer has the private key for the given node_id.
743 pub extern "C" fn PeerManager_get_peer_node_ids(this_arg: &PeerManager) -> crate::c_types::derived::CVec_PublicKeyZ {
744 let mut ret = unsafe { &*this_arg.inner }.get_peer_node_ids();
745 let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::c_types::PublicKey::from_rust(&item) }); };
749 /// Indicates a new outbound connection has been established to a node with the given node_id.
750 /// Note that if an Err is returned here you MUST NOT call socket_disconnected for the new
751 /// descriptor but must disconnect the connection immediately.
753 /// Returns a small number of bytes to send to the remote node (currently always 50).
755 /// Panics if descriptor is duplicative with some other descriptor which has not yet had a
756 /// socket_disconnected().
759 pub extern "C" fn PeerManager_new_outbound_connection(this_arg: &PeerManager, mut their_node_id: crate::c_types::PublicKey, mut descriptor: crate::ln::peer_handler::SocketDescriptor) -> crate::c_types::derived::CResult_CVec_u8ZPeerHandleErrorZ {
760 let mut ret = unsafe { &*this_arg.inner }.new_outbound_connection(their_node_id.into_rust(), descriptor);
761 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::ln::peer_handler::PeerHandleError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
765 /// Indicates a new inbound connection has been established.
767 /// May refuse the connection by returning an Err, but will never write bytes to the remote end
768 /// (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
769 /// call socket_disconnected for the new descriptor but must disconnect the connection
772 /// Panics if descriptor is duplicative with some other descriptor which has not yet had
773 /// socket_disconnected called.
776 pub extern "C" fn PeerManager_new_inbound_connection(this_arg: &PeerManager, mut descriptor: crate::ln::peer_handler::SocketDescriptor) -> crate::c_types::derived::CResult_NonePeerHandleErrorZ {
777 let mut ret = unsafe { &*this_arg.inner }.new_inbound_connection(descriptor);
778 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::peer_handler::PeerHandleError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
782 /// Indicates that there is room to write data to the given socket descriptor.
784 /// May return an Err to indicate that the connection should be closed.
786 /// Will most likely call send_data on the descriptor passed in (or the descriptor handed into
787 /// new_*\\_connection) before returning. Thus, be very careful with reentrancy issues! The
788 /// invariants around calling write_buffer_space_avail in case a write did not fully complete
789 /// must still hold - be ready to call write_buffer_space_avail again if a write call generated
790 /// here isn't sufficient! Panics if the descriptor was not previously registered in a
791 /// new_\\*_connection event.
794 pub extern "C" fn PeerManager_write_buffer_space_avail(this_arg: &PeerManager, descriptor: &mut crate::ln::peer_handler::SocketDescriptor) -> crate::c_types::derived::CResult_NonePeerHandleErrorZ {
795 let mut ret = unsafe { &*this_arg.inner }.write_buffer_space_avail(descriptor);
796 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { 0u8 /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::peer_handler::PeerHandleError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
800 /// Indicates that data was read from the given socket descriptor.
802 /// May return an Err to indicate that the connection should be closed.
804 /// Will *not* call back into send_data on any descriptors to avoid reentrancy complexity.
805 /// Thus, however, you almost certainly want to call process_events() after any read_event to
806 /// generate send_data calls to handle responses.
808 /// If Ok(true) is returned, further read_events should not be triggered until a send_data call
809 /// on this file descriptor has resume_read set (preventing DoS issues in the send buffer).
811 /// Panics if the descriptor was not previously registered in a new_*_connection event.
814 pub extern "C" fn PeerManager_read_event(this_arg: &PeerManager, peer_descriptor: &mut crate::ln::peer_handler::SocketDescriptor, mut data: crate::c_types::u8slice) -> crate::c_types::derived::CResult_boolPeerHandleErrorZ {
815 let mut ret = unsafe { &*this_arg.inner }.read_event(peer_descriptor, data.to_slice());
816 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::ln::peer_handler::PeerHandleError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
820 /// Checks for any events generated by our handlers and processes them. Includes sending most
821 /// response messages as well as messages generated by calls to handler functions directly (eg
822 /// functions like ChannelManager::process_pending_htlc_forward or send_payment).
824 pub extern "C" fn PeerManager_process_events(this_arg: &PeerManager) {
825 unsafe { &*this_arg.inner }.process_events()
828 /// Indicates that the given socket descriptor's connection is now closed.
830 /// This must only be called if the socket has been disconnected by the peer or your own
831 /// decision to disconnect it and must NOT be called in any case where other parts of this
832 /// library (eg PeerHandleError, explicit disconnect_socket calls) instruct you to disconnect
835 /// Panics if the descriptor was not previously registered in a successful new_*_connection event.
837 pub extern "C" fn PeerManager_socket_disconnected(this_arg: &PeerManager, descriptor: &crate::ln::peer_handler::SocketDescriptor) {
838 unsafe { &*this_arg.inner }.socket_disconnected(descriptor)
841 /// Disconnect a peer given its node id.
843 /// Set no_connection_possible to true to prevent any further connection with this peer,
844 /// force-closing any channels we have with it.
846 /// If a peer is connected, this will call `disconnect_socket` on the descriptor for the peer,
847 /// so be careful about reentrancy issues.
849 pub extern "C" fn PeerManager_disconnect_by_node_id(this_arg: &PeerManager, mut node_id: crate::c_types::PublicKey, mut no_connection_possible: bool) {
850 unsafe { &*this_arg.inner }.disconnect_by_node_id(node_id.into_rust(), no_connection_possible)
853 /// This function should be called roughly once every 30 seconds.
854 /// It will send pings to each peer and disconnect those which did not respond to the last round of pings.
855 /// Will most likely call send_data on all of the registered descriptors, thus, be very careful with reentrancy issues!
857 pub extern "C" fn PeerManager_timer_tick_occurred(this_arg: &PeerManager) {
858 unsafe { &*this_arg.inner }.timer_tick_occurred()