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