c06d0f1969c9c40f1574f6f9ff9ad65c42239159
[ldk-java] / c_sharp / src / org / ldk / structs / RoutingMessageHandler.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8 /**
9  * A trait to describe an object which can receive routing messages.
10  * 
11  * # Implementor DoS Warnings
12  * 
13  * For `gossip_queries` messages there are potential DoS vectors when handling
14  * inbound queries. Implementors using an on-disk network graph should be aware of
15  * repeated disk I/O for queries accessing different parts of the network graph.
16  */
17 public class RoutingMessageHandler : CommonBase {
18         internal readonly bindings.LDKRoutingMessageHandler bindings_instance;
19         internal RoutingMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
20         private RoutingMessageHandler(bindings.LDKRoutingMessageHandler arg, bindings.LDKMessageSendEventsProvider MessageSendEventsProvider) : base(bindings.LDKRoutingMessageHandler_new(arg, MessageSendEventsProvider)) {
21                 this.ptrs_to.AddLast(arg);
22                 this.ptrs_to.AddLast(MessageSendEventsProvider);
23                 this.bindings_instance = arg;
24         }
25         ~RoutingMessageHandler() {
26                 if (ptr != 0) { bindings.RoutingMessageHandler_free(ptr); }
27         }
28
29         public interface RoutingMessageHandlerInterface {
30                 /**
31                  * Handle an incoming node_announcement message, returning true if it should be forwarded on,
32                  * false or returning an Err otherwise.
33                  */
34                 Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement _msg);
35                 /**
36                  * Handle a channel_announcement message, returning true if it should be forwarded on, false
37                  * or returning an Err otherwise.
38                  */
39                 Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement _msg);
40                 /**
41                  * Handle an incoming channel_update message, returning true if it should be forwarded on,
42                  * false or returning an Err otherwise.
43                  */
44                 Result_boolLightningErrorZ handle_channel_update(ChannelUpdate _msg);
45                 /**
46                  * Gets channel announcements and updates required to dump our routing table to a remote node,
47                  * starting at the short_channel_id indicated by starting_point and including announcements
48                  * for a single channel.
49                  */
50                 Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ get_next_channel_announcement(long _starting_point);
51                 /**
52                  * Gets a node announcement required to dump our routing table to a remote node, starting at
53                  * the node *after* the provided pubkey and including up to one announcement immediately
54                  * higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
55                  * If None is provided for starting_point, we start at the first node.
56                  * 
57                  * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
58                  * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
59                  */
60                 NodeAnnouncement get_next_node_announcement(byte[] _starting_point);
61                 /**
62                  * Called when a connection is established with a peer. This can be used to
63                  * perform routing table synchronization using a strategy defined by the
64                  * implementor.
65                  * 
66                  * May return an `Err(())` if the features the peer supports are not sufficient to communicate
67                  * with us. Implementors should be somewhat conservative about doing so, however, as other
68                  * message handlers may still wish to communicate with this peer.
69                  */
70                 Result_NoneNoneZ peer_connected(byte[] _their_node_id, Init _init);
71                 /**
72                  * Handles the reply of a query we initiated to learn about channels
73                  * for a given range of blocks. We can expect to receive one or more
74                  * replies to a single query.
75                  */
76                 Result_NoneLightningErrorZ handle_reply_channel_range(byte[] _their_node_id, ReplyChannelRange _msg);
77                 /**
78                  * Handles the reply of a query we initiated asking for routing gossip
79                  * messages for a list of channels. We should receive this message when
80                  * a node has completed its best effort to send us the pertaining routing
81                  * gossip messages.
82                  */
83                 Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] _their_node_id, ReplyShortChannelIdsEnd _msg);
84                 /**
85                  * Handles when a peer asks us to send a list of short_channel_ids
86                  * for the requested range of blocks.
87                  */
88                 Result_NoneLightningErrorZ handle_query_channel_range(byte[] _their_node_id, QueryChannelRange _msg);
89                 /**
90                  * Handles when a peer asks us to send routing gossip messages for a
91                  * list of short_channel_ids.
92                  */
93                 Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] _their_node_id, QueryShortChannelIds _msg);
94                 /**
95                  * Gets the node feature flags which this handler itself supports. All available handlers are
96                  * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
97                  * which are broadcasted in our [`NodeAnnouncement`] message.
98                  */
99                 NodeFeatures provided_node_features();
100                 /**
101                  * Gets the init feature flags which should be sent to the given peer. All available handlers
102                  * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
103                  * which are sent in our [`Init`] message.
104                  * 
105                  * Note that this method is called before [`Self::peer_connected`].
106                  */
107                 InitFeatures provided_init_features(byte[] _their_node_id);
108         }
109         private class LDKRoutingMessageHandlerHolder { internal RoutingMessageHandler held; }
110         private class LDKRoutingMessageHandlerImpl : bindings.LDKRoutingMessageHandler {
111                 internal LDKRoutingMessageHandlerImpl(RoutingMessageHandlerInterface arg, LDKRoutingMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
112                 private RoutingMessageHandlerInterface arg;
113                 private LDKRoutingMessageHandlerHolder impl_holder;
114                 public long handle_node_announcement(long _msg) {
115                         org.ldk.structs.NodeAnnouncement _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.NodeAnnouncement(null, _msg); }
116                         Result_boolLightningErrorZ ret = arg.handle_node_announcement(_msg_hu_conv);
117                                 GC.KeepAlive(arg);
118                         long result = ret == null ? 0 : ret.clone_ptr();
119                         return result;
120                 }
121                 public long handle_channel_announcement(long _msg) {
122                         org.ldk.structs.ChannelAnnouncement _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.ChannelAnnouncement(null, _msg); }
123                         Result_boolLightningErrorZ ret = arg.handle_channel_announcement(_msg_hu_conv);
124                                 GC.KeepAlive(arg);
125                         long result = ret == null ? 0 : ret.clone_ptr();
126                         return result;
127                 }
128                 public long handle_channel_update(long _msg) {
129                         org.ldk.structs.ChannelUpdate _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.ChannelUpdate(null, _msg); }
130                         Result_boolLightningErrorZ ret = arg.handle_channel_update(_msg_hu_conv);
131                                 GC.KeepAlive(arg);
132                         long result = ret == null ? 0 : ret.clone_ptr();
133                         return result;
134                 }
135                 public long get_next_channel_announcement(long _starting_point) {
136                         Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ ret = arg.get_next_channel_announcement(_starting_point);
137                                 GC.KeepAlive(arg);
138                         long result = ret == null ? 0 : ret.clone_ptr();
139                         return result;
140                 }
141                 public long get_next_node_announcement(byte[] _starting_point) {
142                         NodeAnnouncement ret = arg.get_next_node_announcement(_starting_point);
143                                 GC.KeepAlive(arg);
144                         long result = ret == null ? 0 : ret.clone_ptr();
145                         return result;
146                 }
147                 public long peer_connected(byte[] _their_node_id, long _init) {
148                         org.ldk.structs.Init _init_hu_conv = null; if (_init < 0 || _init > 4096) { _init_hu_conv = new org.ldk.structs.Init(null, _init); }
149                         Result_NoneNoneZ ret = arg.peer_connected(_their_node_id, _init_hu_conv);
150                                 GC.KeepAlive(arg);
151                         long result = ret == null ? 0 : ret.clone_ptr();
152                         return result;
153                 }
154                 public long handle_reply_channel_range(byte[] _their_node_id, long _msg) {
155                         org.ldk.structs.ReplyChannelRange _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.ReplyChannelRange(null, _msg); }
156                         if (_msg_hu_conv != null) { _msg_hu_conv.ptrs_to.AddLast(this); };
157                         Result_NoneLightningErrorZ ret = arg.handle_reply_channel_range(_their_node_id, _msg_hu_conv);
158                                 GC.KeepAlive(arg);
159                         long result = ret == null ? 0 : ret.clone_ptr();
160                         return result;
161                 }
162                 public long handle_reply_short_channel_ids_end(byte[] _their_node_id, long _msg) {
163                         org.ldk.structs.ReplyShortChannelIdsEnd _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.ReplyShortChannelIdsEnd(null, _msg); }
164                         if (_msg_hu_conv != null) { _msg_hu_conv.ptrs_to.AddLast(this); };
165                         Result_NoneLightningErrorZ ret = arg.handle_reply_short_channel_ids_end(_their_node_id, _msg_hu_conv);
166                                 GC.KeepAlive(arg);
167                         long result = ret == null ? 0 : ret.clone_ptr();
168                         return result;
169                 }
170                 public long handle_query_channel_range(byte[] _their_node_id, long _msg) {
171                         org.ldk.structs.QueryChannelRange _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.QueryChannelRange(null, _msg); }
172                         if (_msg_hu_conv != null) { _msg_hu_conv.ptrs_to.AddLast(this); };
173                         Result_NoneLightningErrorZ ret = arg.handle_query_channel_range(_their_node_id, _msg_hu_conv);
174                                 GC.KeepAlive(arg);
175                         long result = ret == null ? 0 : ret.clone_ptr();
176                         return result;
177                 }
178                 public long handle_query_short_channel_ids(byte[] _their_node_id, long _msg) {
179                         org.ldk.structs.QueryShortChannelIds _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.QueryShortChannelIds(null, _msg); }
180                         if (_msg_hu_conv != null) { _msg_hu_conv.ptrs_to.AddLast(this); };
181                         Result_NoneLightningErrorZ ret = arg.handle_query_short_channel_ids(_their_node_id, _msg_hu_conv);
182                                 GC.KeepAlive(arg);
183                         long result = ret == null ? 0 : ret.clone_ptr();
184                         return result;
185                 }
186                 public long provided_node_features() {
187                         NodeFeatures ret = arg.provided_node_features();
188                                 GC.KeepAlive(arg);
189                         long result = ret == null ? 0 : ret.clone_ptr();
190                         return result;
191                 }
192                 public long provided_init_features(byte[] _their_node_id) {
193                         InitFeatures ret = arg.provided_init_features(_their_node_id);
194                                 GC.KeepAlive(arg);
195                         long result = ret == null ? 0 : ret.clone_ptr();
196                         return result;
197                 }
198         }
199         public static RoutingMessageHandler new_impl(RoutingMessageHandlerInterface arg, MessageSendEventsProvider.MessageSendEventsProviderInterface MessageSendEventsProvider_impl) {
200                 LDKRoutingMessageHandlerHolder impl_holder = new LDKRoutingMessageHandlerHolder();
201                 impl_holder.held = new RoutingMessageHandler(new LDKRoutingMessageHandlerImpl(arg, impl_holder), MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
202                 return impl_holder.held;
203         }
204
205         /**
206          * Gets the underlying MessageSendEventsProvider.
207          */
208         public MessageSendEventsProvider get_message_send_events_provider() {
209                 MessageSendEventsProvider res = new MessageSendEventsProvider(null, bindings.LDKRoutingMessageHandler_get_MessageSendEventsProvider(this.ptr));
210                 this.ptrs_to.AddLast(res);
211                 return res;
212         }
213
214         /**
215          * Handle an incoming node_announcement message, returning true if it should be forwarded on,
216          * false or returning an Err otherwise.
217          */
218         public Result_boolLightningErrorZ handle_node_announcement(org.ldk.structs.NodeAnnouncement msg) {
219                 long ret = bindings.RoutingMessageHandler_handle_node_announcement(this.ptr, msg == null ? 0 : msg.ptr);
220                 GC.KeepAlive(this);
221                 GC.KeepAlive(msg);
222                 if (ret >= 0 && ret <= 4096) { return null; }
223                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
224                 if (this != null) { this.ptrs_to.AddLast(msg); };
225                 return ret_hu_conv;
226         }
227
228         /**
229          * Handle a channel_announcement message, returning true if it should be forwarded on, false
230          * or returning an Err otherwise.
231          */
232         public Result_boolLightningErrorZ handle_channel_announcement(org.ldk.structs.ChannelAnnouncement msg) {
233                 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr);
234                 GC.KeepAlive(this);
235                 GC.KeepAlive(msg);
236                 if (ret >= 0 && ret <= 4096) { return null; }
237                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
238                 if (this != null) { this.ptrs_to.AddLast(msg); };
239                 return ret_hu_conv;
240         }
241
242         /**
243          * Handle an incoming channel_update message, returning true if it should be forwarded on,
244          * false or returning an Err otherwise.
245          */
246         public Result_boolLightningErrorZ handle_channel_update(org.ldk.structs.ChannelUpdate msg) {
247                 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr);
248                 GC.KeepAlive(this);
249                 GC.KeepAlive(msg);
250                 if (ret >= 0 && ret <= 4096) { return null; }
251                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
252                 if (this != null) { this.ptrs_to.AddLast(msg); };
253                 return ret_hu_conv;
254         }
255
256         /**
257          * Gets channel announcements and updates required to dump our routing table to a remote node,
258          * starting at the short_channel_id indicated by starting_point and including announcements
259          * for a single channel.
260          */
261         public Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ get_next_channel_announcement(long starting_point) {
262                 long ret = bindings.RoutingMessageHandler_get_next_channel_announcement(this.ptr, starting_point);
263                 GC.KeepAlive(this);
264                 GC.KeepAlive(starting_point);
265                 if (ret >= 0 && ret <= 4096) { return null; }
266                 org.ldk.structs.Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ ret_hu_conv = org.ldk.structs.Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ.constr_from_ptr(ret);
267                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
268                 return ret_hu_conv;
269         }
270
271         /**
272          * Gets a node announcement required to dump our routing table to a remote node, starting at
273          * the node *after* the provided pubkey and including up to one announcement immediately
274          * higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
275          * If None is provided for starting_point, we start at the first node.
276          * 
277          * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
278          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
279          */
280         public NodeAnnouncement get_next_node_announcement(byte[] starting_point) {
281                 long ret = bindings.RoutingMessageHandler_get_next_node_announcement(this.ptr, InternalUtils.check_arr_len(starting_point, 33));
282                 GC.KeepAlive(this);
283                 GC.KeepAlive(starting_point);
284                 if (ret >= 0 && ret <= 4096) { return null; }
285                 org.ldk.structs.NodeAnnouncement ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeAnnouncement(null, ret); }
286                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
287                 return ret_hu_conv;
288         }
289
290         /**
291          * Called when a connection is established with a peer. This can be used to
292          * perform routing table synchronization using a strategy defined by the
293          * implementor.
294          * 
295          * May return an `Err(())` if the features the peer supports are not sufficient to communicate
296          * with us. Implementors should be somewhat conservative about doing so, however, as other
297          * message handlers may still wish to communicate with this peer.
298          */
299         public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init) {
300                 long ret = bindings.RoutingMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr);
301                 GC.KeepAlive(this);
302                 GC.KeepAlive(their_node_id);
303                 GC.KeepAlive(init);
304                 if (ret >= 0 && ret <= 4096) { return null; }
305                 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
306                 if (this != null) { this.ptrs_to.AddLast(init); };
307                 return ret_hu_conv;
308         }
309
310         /**
311          * Handles the reply of a query we initiated to learn about channels
312          * for a given range of blocks. We can expect to receive one or more
313          * replies to a single query.
314          */
315         public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, org.ldk.structs.ReplyChannelRange msg) {
316                 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
317                 GC.KeepAlive(this);
318                 GC.KeepAlive(their_node_id);
319                 GC.KeepAlive(msg);
320                 if (ret >= 0 && ret <= 4096) { return null; }
321                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
322                 if (this != null) { this.ptrs_to.AddLast(msg); };
323                 return ret_hu_conv;
324         }
325
326         /**
327          * Handles the reply of a query we initiated asking for routing gossip
328          * messages for a list of channels. We should receive this message when
329          * a node has completed its best effort to send us the pertaining routing
330          * gossip messages.
331          */
332         public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, org.ldk.structs.ReplyShortChannelIdsEnd msg) {
333                 long ret = bindings.RoutingMessageHandler_handle_reply_short_channel_ids_end(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
334                 GC.KeepAlive(this);
335                 GC.KeepAlive(their_node_id);
336                 GC.KeepAlive(msg);
337                 if (ret >= 0 && ret <= 4096) { return null; }
338                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
339                 if (this != null) { this.ptrs_to.AddLast(msg); };
340                 return ret_hu_conv;
341         }
342
343         /**
344          * Handles when a peer asks us to send a list of short_channel_ids
345          * for the requested range of blocks.
346          */
347         public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, org.ldk.structs.QueryChannelRange msg) {
348                 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
349                 GC.KeepAlive(this);
350                 GC.KeepAlive(their_node_id);
351                 GC.KeepAlive(msg);
352                 if (ret >= 0 && ret <= 4096) { return null; }
353                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
354                 if (this != null) { this.ptrs_to.AddLast(msg); };
355                 return ret_hu_conv;
356         }
357
358         /**
359          * Handles when a peer asks us to send routing gossip messages for a
360          * list of short_channel_ids.
361          */
362         public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, org.ldk.structs.QueryShortChannelIds msg) {
363                 long ret = bindings.RoutingMessageHandler_handle_query_short_channel_ids(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
364                 GC.KeepAlive(this);
365                 GC.KeepAlive(their_node_id);
366                 GC.KeepAlive(msg);
367                 if (ret >= 0 && ret <= 4096) { return null; }
368                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
369                 if (this != null) { this.ptrs_to.AddLast(msg); };
370                 return ret_hu_conv;
371         }
372
373         /**
374          * Gets the node feature flags which this handler itself supports. All available handlers are
375          * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
376          * which are broadcasted in our [`NodeAnnouncement`] message.
377          */
378         public NodeFeatures provided_node_features() {
379                 long ret = bindings.RoutingMessageHandler_provided_node_features(this.ptr);
380                 GC.KeepAlive(this);
381                 if (ret >= 0 && ret <= 4096) { return null; }
382                 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
383                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
384                 return ret_hu_conv;
385         }
386
387         /**
388          * Gets the init feature flags which should be sent to the given peer. All available handlers
389          * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
390          * which are sent in our [`Init`] message.
391          * 
392          * Note that this method is called before [`Self::peer_connected`].
393          */
394         public InitFeatures provided_init_features(byte[] their_node_id) {
395                 long ret = bindings.RoutingMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
396                 GC.KeepAlive(this);
397                 GC.KeepAlive(their_node_id);
398                 if (ret >= 0 && ret <= 4096) { return null; }
399                 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
400                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
401                 return ret_hu_conv;
402         }
403
404 }
405 } } }