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