Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / ln / peer_handler.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
8
9 //! Top level peer message handling and socket handling logic lives here.
10 //!
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.
16
17 use std::str::FromStr;
18 use std::ffi::c_void;
19 use bitcoin::hashes::Hash;
20 use crate::c_types::*;
21
22
23 use lightning::ln::peer_handler::IgnoringMessageHandler as nativeIgnoringMessageHandlerImport;
24 type nativeIgnoringMessageHandler = nativeIgnoringMessageHandlerImport;
25
26 /// A dummy struct which implements `RoutingMessageHandler` without storing any routing information
27 /// or doing any processing. You can provide one of these as the route_handler in a MessageHandler.
28 #[must_use]
29 #[repr(C)]
30 pub struct IgnoringMessageHandler {
31         /// A pointer to the opaque Rust object.
32
33         /// Nearly everywhere, inner must be non-null, however in places where
34         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
35         pub inner: *mut nativeIgnoringMessageHandler,
36         /// Indicates that this is the only struct which contains the same pointer.
37
38         /// Rust functions which take ownership of an object provided via an argument require
39         /// this to be true and invalidate the object pointed to by inner.
40         pub is_owned: bool,
41 }
42
43 impl Drop for IgnoringMessageHandler {
44         fn drop(&mut self) {
45                 if self.is_owned && !<*mut nativeIgnoringMessageHandler>::is_null(self.inner) {
46                         let _ = unsafe { Box::from_raw(self.inner) };
47                 }
48         }
49 }
50 /// Frees any resources used by the IgnoringMessageHandler, if is_owned is set and inner is non-NULL.
51 #[no_mangle]
52 pub extern "C" fn IgnoringMessageHandler_free(this_obj: IgnoringMessageHandler) { }
53 #[allow(unused)]
54 /// Used only if an object of this type is returned as a trait impl by a method
55 extern "C" fn IgnoringMessageHandler_free_void(this_ptr: *mut c_void) {
56         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeIgnoringMessageHandler); }
57 }
58 #[allow(unused)]
59 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
60 impl IgnoringMessageHandler {
61         pub(crate) fn take_inner(mut self) -> *mut nativeIgnoringMessageHandler {
62                 assert!(self.is_owned);
63                 let ret = self.inner;
64                 self.inner = std::ptr::null_mut();
65                 ret
66         }
67 }
68 /// Constructs a new IgnoringMessageHandler given each field
69 #[must_use]
70 #[no_mangle]
71 pub extern "C" fn IgnoringMessageHandler_new() -> IgnoringMessageHandler {
72         IgnoringMessageHandler { inner: Box::into_raw(Box::new(nativeIgnoringMessageHandler {
73         })), is_owned: true }
74 }
75 impl From<nativeIgnoringMessageHandler> for crate::lightning::util::events::MessageSendEventsProvider {
76         fn from(obj: nativeIgnoringMessageHandler) -> Self {
77                 let mut rust_obj = IgnoringMessageHandler { inner: Box::into_raw(Box::new(obj)), is_owned: true };
78                 let mut ret = IgnoringMessageHandler_as_MessageSendEventsProvider(&rust_obj);
79                 // 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
80                 rust_obj.inner = std::ptr::null_mut();
81                 ret.free = Some(IgnoringMessageHandler_free_void);
82                 ret
83         }
84 }
85 /// Constructs a new MessageSendEventsProvider which calls the relevant methods on this_arg.
86 /// This copies the `inner` pointer in this_arg and thus the returned MessageSendEventsProvider must be freed before this_arg is
87 #[no_mangle]
88 pub extern "C" fn IgnoringMessageHandler_as_MessageSendEventsProvider(this_arg: &IgnoringMessageHandler) -> crate::lightning::util::events::MessageSendEventsProvider {
89         crate::lightning::util::events::MessageSendEventsProvider {
90                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
91                 free: None,
92                 get_and_clear_pending_msg_events: IgnoringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
93         }
94 }
95
96 #[must_use]
97 extern "C" fn IgnoringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MessageSendEventZ {
98         let mut ret = <nativeIgnoringMessageHandler as lightning::util::events::MessageSendEventsProvider<>>::get_and_clear_pending_msg_events(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, );
99         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::util::events::MessageSendEvent::native_into(item) }); };
100         local_ret.into()
101 }
102
103 impl From<nativeIgnoringMessageHandler> for crate::lightning::ln::msgs::RoutingMessageHandler {
104         fn from(obj: nativeIgnoringMessageHandler) -> Self {
105                 let mut rust_obj = IgnoringMessageHandler { inner: Box::into_raw(Box::new(obj)), is_owned: true };
106                 let mut ret = IgnoringMessageHandler_as_RoutingMessageHandler(&rust_obj);
107                 // 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
108                 rust_obj.inner = std::ptr::null_mut();
109                 ret.free = Some(IgnoringMessageHandler_free_void);
110                 ret
111         }
112 }
113 /// Constructs a new RoutingMessageHandler which calls the relevant methods on this_arg.
114 /// This copies the `inner` pointer in this_arg and thus the returned RoutingMessageHandler must be freed before this_arg is
115 #[no_mangle]
116 pub extern "C" fn IgnoringMessageHandler_as_RoutingMessageHandler(this_arg: &IgnoringMessageHandler) -> crate::lightning::ln::msgs::RoutingMessageHandler {
117         crate::lightning::ln::msgs::RoutingMessageHandler {
118                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
119                 free: None,
120                 handle_node_announcement: IgnoringMessageHandler_RoutingMessageHandler_handle_node_announcement,
121                 handle_channel_announcement: IgnoringMessageHandler_RoutingMessageHandler_handle_channel_announcement,
122                 handle_channel_update: IgnoringMessageHandler_RoutingMessageHandler_handle_channel_update,
123                 handle_htlc_fail_channel_update: IgnoringMessageHandler_RoutingMessageHandler_handle_htlc_fail_channel_update,
124                 get_next_channel_announcements: IgnoringMessageHandler_RoutingMessageHandler_get_next_channel_announcements,
125                 get_next_node_announcements: IgnoringMessageHandler_RoutingMessageHandler_get_next_node_announcements,
126                 sync_routing_table: IgnoringMessageHandler_RoutingMessageHandler_sync_routing_table,
127                 handle_reply_channel_range: IgnoringMessageHandler_RoutingMessageHandler_handle_reply_channel_range,
128                 handle_reply_short_channel_ids_end: IgnoringMessageHandler_RoutingMessageHandler_handle_reply_short_channel_ids_end,
129                 handle_query_channel_range: IgnoringMessageHandler_RoutingMessageHandler_handle_query_channel_range,
130                 handle_query_short_channel_ids: IgnoringMessageHandler_RoutingMessageHandler_handle_query_short_channel_ids,
131                 MessageSendEventsProvider: crate::lightning::util::events::MessageSendEventsProvider {
132                         this_arg: unsafe { (*this_arg).inner as *mut c_void },
133                         free: None,
134                         get_and_clear_pending_msg_events: IgnoringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
135                 },
136         }
137 }
138
139 #[must_use]
140 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 {
141         let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_node_announcement(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, unsafe { &*_msg.inner });
142         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
143         local_ret
144 }
145 #[must_use]
146 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 {
147         let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_channel_announcement(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, unsafe { &*_msg.inner });
148         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
149         local_ret
150 }
151 #[must_use]
152 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 {
153         let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_channel_update(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, unsafe { &*_msg.inner });
154         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
155         local_ret
156 }
157 extern "C" fn IgnoringMessageHandler_RoutingMessageHandler_handle_htlc_fail_channel_update(this_arg: *const c_void, _update: &crate::lightning::ln::msgs::HTLCFailChannelUpdate) {
158         <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::handle_htlc_fail_channel_update(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, &_update.to_native())
159 }
160 #[must_use]
161 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 {
162         let mut ret = <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::get_next_channel_announcements(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, _starting_point, _batch_amount);
163         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 {  { Box::into_raw(Box::new((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 {  { Box::into_raw(Box::new((orig_ret_0_2.unwrap()))) } }, is_owned: true }; let mut local_ret_0 = (crate::lightning::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 }); };
164         local_ret.into()
165 }
166 #[must_use]
167 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 {
168         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();
169         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);
170         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::ln::msgs::NodeAnnouncement { inner: Box::into_raw(Box::new(item)), is_owned: true } }); };
171         local_ret.into()
172 }
173 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) {
174         <nativeIgnoringMessageHandler as lightning::ln::msgs::RoutingMessageHandler<>>::sync_routing_table(unsafe { &mut *(this_arg as *mut nativeIgnoringMessageHandler) }, &_their_node_id.into_rust(), unsafe { &*_init.inner })
175 }
176 #[must_use]
177 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 {
178         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()) });
179         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
180         local_ret
181 }
182 #[must_use]
183 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 {
184         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()) });
185         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
186         local_ret
187 }
188 #[must_use]
189 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 {
190         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()) });
191         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
192         local_ret
193 }
194 #[must_use]
195 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 {
196         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()) });
197         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
198         local_ret
199 }
200
201
202 use lightning::ln::peer_handler::ErroringMessageHandler as nativeErroringMessageHandlerImport;
203 type nativeErroringMessageHandler = nativeErroringMessageHandlerImport;
204
205 /// A dummy struct which implements `ChannelMessageHandler` without having any channels.
206 /// You can provide one of these as the route_handler in a MessageHandler.
207 #[must_use]
208 #[repr(C)]
209 pub struct ErroringMessageHandler {
210         /// A pointer to the opaque Rust object.
211
212         /// Nearly everywhere, inner must be non-null, however in places where
213         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
214         pub inner: *mut nativeErroringMessageHandler,
215         /// Indicates that this is the only struct which contains the same pointer.
216
217         /// Rust functions which take ownership of an object provided via an argument require
218         /// this to be true and invalidate the object pointed to by inner.
219         pub is_owned: bool,
220 }
221
222 impl Drop for ErroringMessageHandler {
223         fn drop(&mut self) {
224                 if self.is_owned && !<*mut nativeErroringMessageHandler>::is_null(self.inner) {
225                         let _ = unsafe { Box::from_raw(self.inner) };
226                 }
227         }
228 }
229 /// Frees any resources used by the ErroringMessageHandler, if is_owned is set and inner is non-NULL.
230 #[no_mangle]
231 pub extern "C" fn ErroringMessageHandler_free(this_obj: ErroringMessageHandler) { }
232 #[allow(unused)]
233 /// Used only if an object of this type is returned as a trait impl by a method
234 extern "C" fn ErroringMessageHandler_free_void(this_ptr: *mut c_void) {
235         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeErroringMessageHandler); }
236 }
237 #[allow(unused)]
238 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
239 impl ErroringMessageHandler {
240         pub(crate) fn take_inner(mut self) -> *mut nativeErroringMessageHandler {
241                 assert!(self.is_owned);
242                 let ret = self.inner;
243                 self.inner = std::ptr::null_mut();
244                 ret
245         }
246 }
247 /// Constructs a new ErroringMessageHandler
248 #[must_use]
249 #[no_mangle]
250 pub extern "C" fn ErroringMessageHandler_new() -> ErroringMessageHandler {
251         let mut ret = lightning::ln::peer_handler::ErroringMessageHandler::new();
252         ErroringMessageHandler { inner: Box::into_raw(Box::new(ret)), is_owned: true }
253 }
254
255 impl From<nativeErroringMessageHandler> for crate::lightning::util::events::MessageSendEventsProvider {
256         fn from(obj: nativeErroringMessageHandler) -> Self {
257                 let mut rust_obj = ErroringMessageHandler { inner: Box::into_raw(Box::new(obj)), is_owned: true };
258                 let mut ret = ErroringMessageHandler_as_MessageSendEventsProvider(&rust_obj);
259                 // 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
260                 rust_obj.inner = std::ptr::null_mut();
261                 ret.free = Some(ErroringMessageHandler_free_void);
262                 ret
263         }
264 }
265 /// Constructs a new MessageSendEventsProvider which calls the relevant methods on this_arg.
266 /// This copies the `inner` pointer in this_arg and thus the returned MessageSendEventsProvider must be freed before this_arg is
267 #[no_mangle]
268 pub extern "C" fn ErroringMessageHandler_as_MessageSendEventsProvider(this_arg: &ErroringMessageHandler) -> crate::lightning::util::events::MessageSendEventsProvider {
269         crate::lightning::util::events::MessageSendEventsProvider {
270                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
271                 free: None,
272                 get_and_clear_pending_msg_events: ErroringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
273         }
274 }
275
276 #[must_use]
277 extern "C" fn ErroringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events(this_arg: *const c_void) -> crate::c_types::derived::CVec_MessageSendEventZ {
278         let mut ret = <nativeErroringMessageHandler as lightning::util::events::MessageSendEventsProvider<>>::get_and_clear_pending_msg_events(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, );
279         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::lightning::util::events::MessageSendEvent::native_into(item) }); };
280         local_ret.into()
281 }
282
283 impl From<nativeErroringMessageHandler> for crate::lightning::ln::msgs::ChannelMessageHandler {
284         fn from(obj: nativeErroringMessageHandler) -> Self {
285                 let mut rust_obj = ErroringMessageHandler { inner: Box::into_raw(Box::new(obj)), is_owned: true };
286                 let mut ret = ErroringMessageHandler_as_ChannelMessageHandler(&rust_obj);
287                 // 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
288                 rust_obj.inner = std::ptr::null_mut();
289                 ret.free = Some(ErroringMessageHandler_free_void);
290                 ret
291         }
292 }
293 /// Constructs a new ChannelMessageHandler which calls the relevant methods on this_arg.
294 /// This copies the `inner` pointer in this_arg and thus the returned ChannelMessageHandler must be freed before this_arg is
295 #[no_mangle]
296 pub extern "C" fn ErroringMessageHandler_as_ChannelMessageHandler(this_arg: &ErroringMessageHandler) -> crate::lightning::ln::msgs::ChannelMessageHandler {
297         crate::lightning::ln::msgs::ChannelMessageHandler {
298                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
299                 free: None,
300                 handle_open_channel: ErroringMessageHandler_ChannelMessageHandler_handle_open_channel,
301                 handle_accept_channel: ErroringMessageHandler_ChannelMessageHandler_handle_accept_channel,
302                 handle_funding_created: ErroringMessageHandler_ChannelMessageHandler_handle_funding_created,
303                 handle_funding_signed: ErroringMessageHandler_ChannelMessageHandler_handle_funding_signed,
304                 handle_funding_locked: ErroringMessageHandler_ChannelMessageHandler_handle_funding_locked,
305                 handle_shutdown: ErroringMessageHandler_ChannelMessageHandler_handle_shutdown,
306                 handle_closing_signed: ErroringMessageHandler_ChannelMessageHandler_handle_closing_signed,
307                 handle_update_add_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_add_htlc,
308                 handle_update_fulfill_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_fulfill_htlc,
309                 handle_update_fail_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_fail_htlc,
310                 handle_update_fail_malformed_htlc: ErroringMessageHandler_ChannelMessageHandler_handle_update_fail_malformed_htlc,
311                 handle_commitment_signed: ErroringMessageHandler_ChannelMessageHandler_handle_commitment_signed,
312                 handle_revoke_and_ack: ErroringMessageHandler_ChannelMessageHandler_handle_revoke_and_ack,
313                 handle_update_fee: ErroringMessageHandler_ChannelMessageHandler_handle_update_fee,
314                 handle_announcement_signatures: ErroringMessageHandler_ChannelMessageHandler_handle_announcement_signatures,
315                 peer_disconnected: ErroringMessageHandler_ChannelMessageHandler_peer_disconnected,
316                 peer_connected: ErroringMessageHandler_ChannelMessageHandler_peer_connected,
317                 handle_channel_reestablish: ErroringMessageHandler_ChannelMessageHandler_handle_channel_reestablish,
318                 handle_channel_update: ErroringMessageHandler_ChannelMessageHandler_handle_channel_update,
319                 handle_error: ErroringMessageHandler_ChannelMessageHandler_handle_error,
320                 MessageSendEventsProvider: crate::lightning::util::events::MessageSendEventsProvider {
321                         this_arg: unsafe { (*this_arg).inner as *mut c_void },
322                         free: None,
323                         get_and_clear_pending_msg_events: ErroringMessageHandler_MessageSendEventsProvider_get_and_clear_pending_msg_events,
324                 },
325         }
326 }
327
328 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) {
329         <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 })
330 }
331 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) {
332         <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 })
333 }
334 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) {
335         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_funding_created(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
336 }
337 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) {
338         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_funding_signed(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
339 }
340 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) {
341         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_funding_locked(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
342 }
343 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) {
344         <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 })
345 }
346 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) {
347         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_closing_signed(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
348 }
349 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) {
350         <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 })
351 }
352 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) {
353         <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 })
354 }
355 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) {
356         <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 })
357 }
358 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) {
359         <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 })
360 }
361 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) {
362         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_commitment_signed(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
363 }
364 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) {
365         <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 })
366 }
367 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) {
368         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_update_fee(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
369 }
370 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) {
371         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_announcement_signatures(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
372 }
373 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) {
374         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_channel_reestablish(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &their_node_id.into_rust(), unsafe { &*msg.inner })
375 }
376 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) {
377         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_channel_update(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), unsafe { &*_msg.inner })
378 }
379 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) {
380         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::peer_disconnected(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), _no_connection_possible)
381 }
382 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) {
383         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::peer_connected(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), unsafe { &*_msg.inner })
384 }
385 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) {
386         <nativeErroringMessageHandler as lightning::ln::msgs::ChannelMessageHandler<>>::handle_error(unsafe { &mut *(this_arg as *mut nativeErroringMessageHandler) }, &_their_node_id.into_rust(), unsafe { &*_msg.inner })
387 }
388
389
390 use lightning::ln::peer_handler::MessageHandler as nativeMessageHandlerImport;
391 type nativeMessageHandler = nativeMessageHandlerImport<crate::lightning::ln::msgs::ChannelMessageHandler, crate::lightning::ln::msgs::RoutingMessageHandler>;
392
393 /// Provides references to trait impls which handle different types of messages.
394 #[must_use]
395 #[repr(C)]
396 pub struct MessageHandler {
397         /// A pointer to the opaque Rust object.
398
399         /// Nearly everywhere, inner must be non-null, however in places where
400         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
401         pub inner: *mut nativeMessageHandler,
402         /// Indicates that this is the only struct which contains the same pointer.
403
404         /// Rust functions which take ownership of an object provided via an argument require
405         /// this to be true and invalidate the object pointed to by inner.
406         pub is_owned: bool,
407 }
408
409 impl Drop for MessageHandler {
410         fn drop(&mut self) {
411                 if self.is_owned && !<*mut nativeMessageHandler>::is_null(self.inner) {
412                         let _ = unsafe { Box::from_raw(self.inner) };
413                 }
414         }
415 }
416 /// Frees any resources used by the MessageHandler, if is_owned is set and inner is non-NULL.
417 #[no_mangle]
418 pub extern "C" fn MessageHandler_free(this_obj: MessageHandler) { }
419 #[allow(unused)]
420 /// Used only if an object of this type is returned as a trait impl by a method
421 extern "C" fn MessageHandler_free_void(this_ptr: *mut c_void) {
422         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMessageHandler); }
423 }
424 #[allow(unused)]
425 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
426 impl MessageHandler {
427         pub(crate) fn take_inner(mut self) -> *mut nativeMessageHandler {
428                 assert!(self.is_owned);
429                 let ret = self.inner;
430                 self.inner = std::ptr::null_mut();
431                 ret
432         }
433 }
434 /// A message handler which handles messages specific to channels. Usually this is just a
435 /// [`ChannelManager`] object or an [`ErroringMessageHandler`].
436 ///
437 /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
438 #[no_mangle]
439 pub extern "C" fn MessageHandler_get_chan_handler(this_ptr: &MessageHandler) -> *const crate::lightning::ln::msgs::ChannelMessageHandler {
440         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.chan_handler;
441         inner_val
442 }
443 /// A message handler which handles messages specific to channels. Usually this is just a
444 /// [`ChannelManager`] object or an [`ErroringMessageHandler`].
445 ///
446 /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
447 #[no_mangle]
448 pub extern "C" fn MessageHandler_set_chan_handler(this_ptr: &mut MessageHandler, mut val: crate::lightning::ln::msgs::ChannelMessageHandler) {
449         unsafe { &mut *this_ptr.inner }.chan_handler = val;
450 }
451 /// A message handler which handles messages updating our knowledge of the network channel
452 /// graph. Usually this is just a [`NetGraphMsgHandler`] object or an
453 /// [`IgnoringMessageHandler`].
454 ///
455 /// [`NetGraphMsgHandler`]: crate::routing::network_graph::NetGraphMsgHandler
456 #[no_mangle]
457 pub extern "C" fn MessageHandler_get_route_handler(this_ptr: &MessageHandler) -> *const crate::lightning::ln::msgs::RoutingMessageHandler {
458         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.route_handler;
459         inner_val
460 }
461 /// A message handler which handles messages updating our knowledge of the network channel
462 /// graph. Usually this is just a [`NetGraphMsgHandler`] object or an
463 /// [`IgnoringMessageHandler`].
464 ///
465 /// [`NetGraphMsgHandler`]: crate::routing::network_graph::NetGraphMsgHandler
466 #[no_mangle]
467 pub extern "C" fn MessageHandler_set_route_handler(this_ptr: &mut MessageHandler, mut val: crate::lightning::ln::msgs::RoutingMessageHandler) {
468         unsafe { &mut *this_ptr.inner }.route_handler = val;
469 }
470 /// Constructs a new MessageHandler given each field
471 #[must_use]
472 #[no_mangle]
473 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 {
474         MessageHandler { inner: Box::into_raw(Box::new(nativeMessageHandler {
475                 chan_handler: chan_handler_arg,
476                 route_handler: route_handler_arg,
477         })), is_owned: true }
478 }
479 /// Provides an object which can be used to send data to and which uniquely identifies a connection
480 /// to a remote host. You will need to be able to generate multiple of these which meet Eq and
481 /// implement Hash to meet the PeerManager API.
482 ///
483 /// For efficiency, Clone should be relatively cheap for this type.
484 ///
485 /// Two descriptors may compare equal (by [`cmp::Eq`] and [`hash::Hash`]) as long as the original
486 /// has been disconnected, the [`PeerManager`] has been informed of the disconnection (either by it
487 /// having triggered the disconnection or a call to [`PeerManager::socket_disconnected`]), and no
488 /// further calls to the [`PeerManager`] related to the original socket occur. This allows you to
489 /// use a file descriptor for your SocketDescriptor directly, however for simplicity you may wish
490 /// to simply use another value which is guaranteed to be globally unique instead.
491 #[repr(C)]
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.
497         ///
498         /// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
499         /// Note that in the disconnected case, [`PeerManager::socket_disconnected`] must still be
500         /// called and further write attempts may occur until that time.
501         ///
502         /// If the returned size is smaller than `data.len()`, a
503         /// [`PeerManager::write_buffer_space_avail`] call must be made the next time more data can be
504         /// written. Additionally, until a `send_data` event completes fully, no further
505         /// [`PeerManager::read_event`] calls should be made for the same peer! Because this is to
506         /// prevent denial-of-service issues, you should not read or buffer any data from the socket
507         /// until then.
508         ///
509         /// If a [`PeerManager::read_event`] call on this descriptor had previously returned true
510         /// (indicating that read events should be paused to prevent DoS in the send buffer),
511         /// `resume_read` may be set indicating that read events on this descriptor should resume. A
512         /// `resume_read` of false carries no meaning, and should not cause any action.
513         #[must_use]
514         pub send_data: extern "C" fn (this_arg: *mut c_void, data: crate::c_types::u8slice, resume_read: bool) -> usize,
515         /// Disconnect the socket pointed to by this SocketDescriptor.
516         ///
517         /// You do *not* need to call [`PeerManager::socket_disconnected`] with this socket after this
518         /// call (doing so is a noop).
519         pub disconnect_socket: extern "C" fn (this_arg: *mut c_void),
520         /// Checks if two objects are equal given this object's this_arg pointer and another object.
521         pub eq: extern "C" fn (this_arg: *const c_void, other_arg: &SocketDescriptor) -> bool,
522         /// Calculate a succinct non-cryptographic hash for an object given its this_arg pointer.
523         /// This is used, for example, for inclusion of this object in a hash map.
524         pub hash: extern "C" fn (this_arg: *const c_void) -> u64,
525         /// Called, if set, after this SocketDescriptor has been cloned into a duplicate object.
526         /// The new SocketDescriptor is provided, and should be mutated as needed to perform a
527         /// deep copy of the object pointed to by this_arg or avoid any double-freeing.
528         pub cloned: Option<extern "C" fn (new_SocketDescriptor: &mut SocketDescriptor)>,
529         /// Frees any resources associated with this object given its this_arg pointer.
530         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
531         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
532 }
533 unsafe impl Send for SocketDescriptor {}
534 unsafe impl Sync for SocketDescriptor {}
535 #[no_mangle]
536 pub(crate) extern "C" fn SocketDescriptor_clone_fields(orig: &SocketDescriptor) -> SocketDescriptor {
537         SocketDescriptor {
538                 this_arg: orig.this_arg,
539                 send_data: Clone::clone(&orig.send_data),
540                 disconnect_socket: Clone::clone(&orig.disconnect_socket),
541                 eq: Clone::clone(&orig.eq),
542                 hash: Clone::clone(&orig.hash),
543                 cloned: Clone::clone(&orig.cloned),
544                 free: Clone::clone(&orig.free),
545         }
546 }
547 impl std::cmp::Eq for SocketDescriptor {}
548 impl std::cmp::PartialEq for SocketDescriptor {
549         fn eq(&self, o: &Self) -> bool { (self.eq)(self.this_arg, o) }
550 }
551 impl std::hash::Hash for SocketDescriptor {
552         fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { hasher.write_u64((self.hash)(self.this_arg)) }
553 }
554 #[no_mangle]
555 /// Creates a copy of a SocketDescriptor
556 pub extern "C" fn SocketDescriptor_clone(orig: &SocketDescriptor) -> SocketDescriptor {
557         let mut res = SocketDescriptor_clone_fields(orig);
558         if let Some(f) = orig.cloned { (f)(&mut res) };
559         res
560 }
561 impl Clone for SocketDescriptor {
562         fn clone(&self) -> Self {
563                 SocketDescriptor_clone(self)
564         }
565 }
566
567 use lightning::ln::peer_handler::SocketDescriptor as rustSocketDescriptor;
568 impl rustSocketDescriptor for SocketDescriptor {
569         fn send_data(&mut self, mut data: &[u8], mut resume_read: bool) -> usize {
570                 let mut local_data = crate::c_types::u8slice::from_slice(data);
571                 let mut ret = (self.send_data)(self.this_arg, local_data, resume_read);
572                 ret
573         }
574         fn disconnect_socket(&mut self) {
575                 (self.disconnect_socket)(self.this_arg)
576         }
577 }
578
579 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
580 // directly as a Deref trait in higher-level structs:
581 impl std::ops::Deref for SocketDescriptor {
582         type Target = Self;
583         fn deref(&self) -> &Self {
584                 self
585         }
586 }
587 /// Calls the free function if one is set
588 #[no_mangle]
589 pub extern "C" fn SocketDescriptor_free(this_ptr: SocketDescriptor) { }
590 impl Drop for SocketDescriptor {
591         fn drop(&mut self) {
592                 if let Some(f) = self.free {
593                         f(self.this_arg);
594                 }
595         }
596 }
597
598 use lightning::ln::peer_handler::PeerHandleError as nativePeerHandleErrorImport;
599 type nativePeerHandleError = nativePeerHandleErrorImport;
600
601 /// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
602 /// generate no further read_event/write_buffer_space_avail/socket_disconnected calls for the
603 /// descriptor.
604 #[must_use]
605 #[repr(C)]
606 pub struct PeerHandleError {
607         /// A pointer to the opaque Rust object.
608
609         /// Nearly everywhere, inner must be non-null, however in places where
610         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
611         pub inner: *mut nativePeerHandleError,
612         /// Indicates that this is the only struct which contains the same pointer.
613
614         /// Rust functions which take ownership of an object provided via an argument require
615         /// this to be true and invalidate the object pointed to by inner.
616         pub is_owned: bool,
617 }
618
619 impl Drop for PeerHandleError {
620         fn drop(&mut self) {
621                 if self.is_owned && !<*mut nativePeerHandleError>::is_null(self.inner) {
622                         let _ = unsafe { Box::from_raw(self.inner) };
623                 }
624         }
625 }
626 /// Frees any resources used by the PeerHandleError, if is_owned is set and inner is non-NULL.
627 #[no_mangle]
628 pub extern "C" fn PeerHandleError_free(this_obj: PeerHandleError) { }
629 #[allow(unused)]
630 /// Used only if an object of this type is returned as a trait impl by a method
631 extern "C" fn PeerHandleError_free_void(this_ptr: *mut c_void) {
632         unsafe { let _ = Box::from_raw(this_ptr as *mut nativePeerHandleError); }
633 }
634 #[allow(unused)]
635 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
636 impl PeerHandleError {
637         pub(crate) fn take_inner(mut self) -> *mut nativePeerHandleError {
638                 assert!(self.is_owned);
639                 let ret = self.inner;
640                 self.inner = std::ptr::null_mut();
641                 ret
642         }
643 }
644 /// Used to indicate that we probably can't make any future connections to this peer, implying
645 /// we should go ahead and force-close any channels we have with it.
646 #[no_mangle]
647 pub extern "C" fn PeerHandleError_get_no_connection_possible(this_ptr: &PeerHandleError) -> bool {
648         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.no_connection_possible;
649         *inner_val
650 }
651 /// Used to indicate that we probably can't make any future connections to this peer, implying
652 /// we should go ahead and force-close any channels we have with it.
653 #[no_mangle]
654 pub extern "C" fn PeerHandleError_set_no_connection_possible(this_ptr: &mut PeerHandleError, mut val: bool) {
655         unsafe { &mut *this_ptr.inner }.no_connection_possible = val;
656 }
657 /// Constructs a new PeerHandleError given each field
658 #[must_use]
659 #[no_mangle]
660 pub extern "C" fn PeerHandleError_new(mut no_connection_possible_arg: bool) -> PeerHandleError {
661         PeerHandleError { inner: Box::into_raw(Box::new(nativePeerHandleError {
662                 no_connection_possible: no_connection_possible_arg,
663         })), is_owned: true }
664 }
665 impl Clone for PeerHandleError {
666         fn clone(&self) -> Self {
667                 Self {
668                         inner: if <*mut nativePeerHandleError>::is_null(self.inner) { std::ptr::null_mut() } else {
669                                 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
670                         is_owned: true,
671                 }
672         }
673 }
674 #[allow(unused)]
675 /// Used only if an object of this type is returned as a trait impl by a method
676 pub(crate) extern "C" fn PeerHandleError_clone_void(this_ptr: *const c_void) -> *mut c_void {
677         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativePeerHandleError)).clone() })) as *mut c_void
678 }
679 #[no_mangle]
680 /// Creates a copy of the PeerHandleError
681 pub extern "C" fn PeerHandleError_clone(orig: &PeerHandleError) -> PeerHandleError {
682         orig.clone()
683 }
684
685 use lightning::ln::peer_handler::PeerManager as nativePeerManagerImport;
686 type nativePeerManager = nativePeerManagerImport<crate::lightning::ln::peer_handler::SocketDescriptor, crate::lightning::ln::msgs::ChannelMessageHandler, crate::lightning::ln::msgs::RoutingMessageHandler, crate::lightning::util::logger::Logger>;
687
688 /// A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
689 /// socket events into messages which it passes on to its [`MessageHandler`].
690 ///
691 /// Locks are taken internally, so you must never assume that reentrancy from a
692 /// [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
693 ///
694 /// Calls to [`read_event`] will decode relevant messages and pass them to the
695 /// [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of
696 /// parallelism in Rust-Lightning is in calls to [`read_event`]. Note, however, that calls to any
697 /// [`PeerManager`] functions related to the same connection must occur only in serial, making new
698 /// calls only after previous ones have returned.
699 ///
700 /// Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
701 /// a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
702 /// essentially you should default to using a SimpleRefPeerManager, and use a
703 /// SimpleArcPeerManager when you require a PeerManager with a static lifetime, such as when
704 /// you're using lightning-net-tokio.
705 ///
706 /// [`read_event`]: PeerManager::read_event
707 #[must_use]
708 #[repr(C)]
709 pub struct PeerManager {
710         /// A pointer to the opaque Rust object.
711
712         /// Nearly everywhere, inner must be non-null, however in places where
713         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
714         pub inner: *mut nativePeerManager,
715         /// Indicates that this is the only struct which contains the same pointer.
716
717         /// Rust functions which take ownership of an object provided via an argument require
718         /// this to be true and invalidate the object pointed to by inner.
719         pub is_owned: bool,
720 }
721
722 impl Drop for PeerManager {
723         fn drop(&mut self) {
724                 if self.is_owned && !<*mut nativePeerManager>::is_null(self.inner) {
725                         let _ = unsafe { Box::from_raw(self.inner) };
726                 }
727         }
728 }
729 /// Frees any resources used by the PeerManager, if is_owned is set and inner is non-NULL.
730 #[no_mangle]
731 pub extern "C" fn PeerManager_free(this_obj: PeerManager) { }
732 #[allow(unused)]
733 /// Used only if an object of this type is returned as a trait impl by a method
734 extern "C" fn PeerManager_free_void(this_ptr: *mut c_void) {
735         unsafe { let _ = Box::from_raw(this_ptr as *mut nativePeerManager); }
736 }
737 #[allow(unused)]
738 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
739 impl PeerManager {
740         pub(crate) fn take_inner(mut self) -> *mut nativePeerManager {
741                 assert!(self.is_owned);
742                 let ret = self.inner;
743                 self.inner = std::ptr::null_mut();
744                 ret
745         }
746 }
747 /// Constructs a new PeerManager with the given message handlers and node_id secret key
748 /// ephemeral_random_data is used to derive per-connection ephemeral keys and must be
749 /// cryptographically secure random bytes.
750 #[must_use]
751 #[no_mangle]
752 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) -> PeerManager {
753         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);
754         PeerManager { inner: Box::into_raw(Box::new(ret)), is_owned: true }
755 }
756
757 /// Get the list of node ids for peers which have completed the initial handshake.
758 ///
759 /// For outbound connections, this will be the same as the their_node_id parameter passed in to
760 /// new_outbound_connection, however entries will only appear once the initial handshake has
761 /// completed and we are sure the remote peer has the private key for the given node_id.
762 #[must_use]
763 #[no_mangle]
764 pub extern "C" fn PeerManager_get_peer_node_ids(this_arg: &PeerManager) -> crate::c_types::derived::CVec_PublicKeyZ {
765         let mut ret = unsafe { &*this_arg.inner }.get_peer_node_ids();
766         let mut local_ret = Vec::new(); for mut item in ret.drain(..) { local_ret.push( { crate::c_types::PublicKey::from_rust(&item) }); };
767         local_ret.into()
768 }
769
770 /// Indicates a new outbound connection has been established to a node with the given node_id.
771 /// Note that if an Err is returned here you MUST NOT call socket_disconnected for the new
772 /// descriptor but must disconnect the connection immediately.
773 ///
774 /// Returns a small number of bytes to send to the remote node (currently always 50).
775 ///
776 /// Panics if descriptor is duplicative with some other descriptor which has not yet been
777 /// [`socket_disconnected()`].
778 ///
779 /// [`socket_disconnected()`]: PeerManager::socket_disconnected
780 #[must_use]
781 #[no_mangle]
782 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 {
783         let mut ret = unsafe { &*this_arg.inner }.new_outbound_connection(their_node_id.into_rust(), descriptor);
784         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
785         local_ret
786 }
787
788 /// Indicates a new inbound connection has been established.
789 ///
790 /// May refuse the connection by returning an Err, but will never write bytes to the remote end
791 /// (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
792 /// call socket_disconnected for the new descriptor but must disconnect the connection
793 /// immediately.
794 ///
795 /// Panics if descriptor is duplicative with some other descriptor which has not yet been
796 /// [`socket_disconnected()`].
797 ///
798 /// [`socket_disconnected()`]: PeerManager::socket_disconnected
799 #[must_use]
800 #[no_mangle]
801 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 {
802         let mut ret = unsafe { &*this_arg.inner }.new_inbound_connection(descriptor);
803         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
804         local_ret
805 }
806
807 /// Indicates that there is room to write data to the given socket descriptor.
808 ///
809 /// May return an Err to indicate that the connection should be closed.
810 ///
811 /// May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
812 /// returning. Thus, be very careful with reentrancy issues! The invariants around calling
813 /// [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
814 /// ready to call `[write_buffer_space_avail`] again if a write call generated here isn't
815 /// sufficient!
816 ///
817 /// [`send_data`]: SocketDescriptor::send_data
818 /// [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
819 #[must_use]
820 #[no_mangle]
821 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 {
822         let mut ret = unsafe { &*this_arg.inner }.write_buffer_space_avail(descriptor);
823         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
824         local_ret
825 }
826
827 /// Indicates that data was read from the given socket descriptor.
828 ///
829 /// May return an Err to indicate that the connection should be closed.
830 ///
831 /// Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity.
832 /// Thus, however, you should call [`process_events`] after any `read_event` to generate
833 /// [`send_data`] calls to handle responses.
834 ///
835 /// If `Ok(true)` is returned, further read_events should not be triggered until a
836 /// [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
837 /// send buffer).
838 ///
839 /// [`send_data`]: SocketDescriptor::send_data
840 /// [`process_events`]: PeerManager::process_events
841 #[must_use]
842 #[no_mangle]
843 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 {
844         let mut ret = unsafe { &*this_arg.inner }.read_event(peer_descriptor, data.to_slice());
845         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: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
846         local_ret
847 }
848
849 /// Checks for any events generated by our handlers and processes them. Includes sending most
850 /// response messages as well as messages generated by calls to handler functions directly (eg
851 /// functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
852 ///
853 /// May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
854 /// issues!
855 ///
856 /// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
857 /// [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
858 /// [`send_data`]: SocketDescriptor::send_data
859 #[no_mangle]
860 pub extern "C" fn PeerManager_process_events(this_arg: &PeerManager) {
861         unsafe { &*this_arg.inner }.process_events()
862 }
863
864 /// Indicates that the given socket descriptor's connection is now closed.
865 #[no_mangle]
866 pub extern "C" fn PeerManager_socket_disconnected(this_arg: &PeerManager, descriptor: &crate::lightning::ln::peer_handler::SocketDescriptor) {
867         unsafe { &*this_arg.inner }.socket_disconnected(descriptor)
868 }
869
870 /// Disconnect a peer given its node id.
871 ///
872 /// Set `no_connection_possible` to true to prevent any further connection with this peer,
873 /// force-closing any channels we have with it.
874 ///
875 /// If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
876 /// peer. Thus, be very careful about reentrancy issues.
877 ///
878 /// [`disconnect_socket`]: SocketDescriptor::disconnect_socket
879 #[no_mangle]
880 pub extern "C" fn PeerManager_disconnect_by_node_id(this_arg: &PeerManager, mut node_id: crate::c_types::PublicKey, mut no_connection_possible: bool) {
881         unsafe { &*this_arg.inner }.disconnect_by_node_id(node_id.into_rust(), no_connection_possible)
882 }
883
884 /// This function should be called roughly once every 30 seconds.
885 /// It will send pings to each peer and disconnect those which did not respond to the last
886 /// round of pings.
887 ///
888 /// May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
889 /// issues!
890 ///
891 /// [`send_data`]: SocketDescriptor::send_data
892 #[no_mangle]
893 pub extern "C" fn PeerManager_timer_tick_occurred(this_arg: &PeerManager) {
894         unsafe { &*this_arg.inner }.timer_tick_occurred()
895 }
896