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