1 package org.ldk.batteries;
3 import org.ldk.impl.bindings;
4 import org.ldk.structs.*;
6 import java.io.IOException;
7 import java.lang.reflect.Field;
8 import java.lang.ref.Reference;
10 import java.util.LinkedList;
11 import java.nio.Buffer;
12 import java.nio.ByteBuffer;
13 import java.nio.channels.*;
16 * A NioPeerHandler maps LDK's PeerHandler to Java's NIO I/O interface. It spawns a single background thread which
17 * processes socket events and provides the data to LDK for decryption and processing.
19 public class NioPeerHandler {
20 private static class Peer {
21 SocketDescriptor descriptor;
22 long descriptor_raw_pointer;
26 // Android's java.nio implementation has a big lock inside the selector, preventing any concurrent access to it.
27 // This appears to largely defeat the entire purpose of java.nio, but we work around it here by explicitly checking
28 // for an Android environment and passing any selector access on any thread other than our internal one through
29 // do_selector_action, which wakes up the selector before accessing it.
30 private static boolean IS_ANDROID;
32 IS_ANDROID = System.getProperty("java.vendor").toLowerCase().contains("android");
34 private boolean wakeup_selector = false;
35 private interface SelectorCall {
36 void meth() throws IOException;
38 private void do_selector_action(SelectorCall meth) throws IOException {
40 wakeup_selector = true;
41 this.selector.wakeup();
42 synchronized (this.selector) {
44 wakeup_selector = false;
51 static private Field CommonBasePointer;
54 Class c = PeerManager.class.getSuperclass();
55 CommonBasePointer = c.getDeclaredField("ptr");
56 CommonBasePointer.setAccessible(true);
57 long _dummy_check = CommonBasePointer.getLong(Ping.of((short)0, (short)0));
58 } catch (NoSuchFieldException | IllegalAccessException e) {
59 throw new IllegalArgumentException(
60 "We currently use reflection to access protected fields as Java has no reasonable access controls", e);
64 private Peer setup_socket(SocketChannel chan) throws IOException {
65 chan.configureBlocking(false);
66 // Lightning tends to send a number of small messages back and forth between peers quickly, which Nagle is
67 // particularly bad at handling, so we disable it here.
68 chan.setOption(StandardSocketOptions.TCP_NODELAY, true);
71 this.socket_id = this.socket_id + 1;
72 our_id = this.socket_id;
75 final Peer peer = new Peer();
76 SocketDescriptor descriptor = SocketDescriptor.new_impl(new SocketDescriptor.SocketDescriptorInterface() {
78 public long send_data(byte[] data, boolean resume_read) {
80 long written = chan.write(ByteBuffer.wrap(data));
81 if (written != data.length) {
82 do_selector_action(() -> peer.key.interestOps(
83 (peer.key.interestOps() | SelectionKey.OP_WRITE) & (~SelectionKey.OP_READ)));
84 } else if (resume_read) {
85 do_selector_action(() -> peer.key.interestOps(
86 (peer.key.interestOps() | SelectionKey.OP_READ) & (~SelectionKey.OP_WRITE)));
89 } catch (IOException|CancelledKeyException ignored) {
90 // Most likely the socket is disconnected, let the background thread handle it.
96 public void disconnect_socket() {
98 do_selector_action(() -> {
99 try { peer.key.cancel(); } catch (CancelledKeyException ignored) {}
100 peer.key.channel().close();
102 } catch (IOException ignored) { }
104 @Override public boolean eq(SocketDescriptor other_arg) { return other_arg.hash() == our_id; }
105 @Override public long hash() { return our_id; }
107 peer.descriptor = descriptor;
109 peer.descriptor_raw_pointer = CommonBasePointer.getLong(descriptor);
110 } catch (IllegalAccessException e) {
111 throw new IllegalArgumentException(
112 "We currently use reflection to access protected fields as Java has no reasonable access controls", e);
117 PeerManager peer_manager;
119 final Selector selector;
121 volatile boolean shutdown = false;
123 private static Option_NetAddressZ get_netaddr_from_sockaddr(SocketAddress sockaddr) {
124 if (sockaddr instanceof InetSocketAddress) {
125 InetAddress addr = ((InetSocketAddress) sockaddr).getAddress();
126 short port = (short) ((InetSocketAddress) sockaddr).getPort();
127 if (addr instanceof Inet4Address) {
128 return Option_NetAddressZ.some(NetAddress.ipv4(addr.getAddress(), port));
129 } else if (addr instanceof Inet6Address) {
130 return Option_NetAddressZ.some(NetAddress.ipv6(addr.getAddress(), port));
133 return Option_NetAddressZ.none();
137 * Constructs a new peer handler, spawning a thread to monitor for socket events.
139 * @param manager The LDK PeerManager which connection data will be provided to.
140 * @throws IOException If an internal java.nio error occurs.
142 public NioPeerHandler(PeerManager manager) throws IOException {
143 this.peer_manager = manager;
144 this.selector = Selector.open();
145 io_thread = new Thread(() -> {
146 int BUF_SZ = 16 * 1024;
147 byte[] max_buf_byte_object = new byte[BUF_SZ];
148 ByteBuffer buf = ByteBuffer.allocate(BUF_SZ);
150 long peer_manager_raw_pointer;
152 peer_manager_raw_pointer = CommonBasePointer.getLong(this.peer_manager);
153 } catch (IllegalAccessException e) {
154 throw new RuntimeException(e);
160 synchronized (this.selector) {
161 if (!wakeup_selector) {
162 this.selector.select(1000);
168 this.selector.select(1000);
170 } catch (IOException ignored) {
171 System.err.println("java.nio threw an unexpected IOException. Stopping PeerHandler thread!");
174 if (shutdown) return;
175 if (Thread.interrupted()) return;
176 for (SelectionKey key : this.selector.selectedKeys()) {
178 if ((key.interestOps() & SelectionKey.OP_ACCEPT) != 0) {
179 if (key.isAcceptable()) {
182 chan = ((ServerSocketChannel) key.channel()).accept();
183 } catch (IOException ignored) {
187 if (chan == null) continue;
189 Peer peer = setup_socket(chan);
190 peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer);
191 Option_NetAddressZ netaddr = get_netaddr_from_sockaddr(chan.getRemoteAddress());
192 Result_NonePeerHandleErrorZ res = this.peer_manager.new_inbound_connection(peer.descriptor, netaddr);
193 if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_Err) {
194 peer.descriptor.disconnect_socket();
196 } catch (IOException ignored) { }
198 continue; // There is no attachment so the rest of the loop is useless
200 Peer peer = (Peer) key.attachment();
202 if (key.isValid() && (key.interestOps() & SelectionKey.OP_WRITE) != 0 && key.isWritable()) {
203 Result_NonePeerHandleErrorZ res = this.peer_manager.write_buffer_space_avail(peer.descriptor);
204 if (res instanceof Result_NonePeerHandleErrorZ.Result_NonePeerHandleErrorZ_Err) {
206 key.channel().close();
209 if (key.isValid() && (key.interestOps() & SelectionKey.OP_READ) != 0 && key.isReadable()) {
210 ((Buffer)buf).clear();
211 int read = ((SocketChannel) key.channel()).read(buf);
213 this.peer_manager.socket_disconnected(peer.descriptor);
215 key.channel().close(); // This may throw, we read -1 so the channel should already be closed, but do this to be safe
216 } else if (read > 0) {
217 ((Buffer)buf).flip();
218 // This code is quite hot during initial network graph sync, so we go a ways out of
219 // our way to avoid object allocations that'll make the GC sweat later -
220 // * when we're hot, we'll likely often be reading the full buffer, so we keep
221 // around a full-buffer-sized byte array to reuse across reads,
222 // * We use the manual memory management call logic directly in bindings instead of
223 // the nice "human-readable" wrappers. This puts us at risk of memory issues,
224 // so we indirectly ensure compile fails if the types change by writing the
225 // "human-readable" form of the same code in the dummy function below.
227 if (read == BUF_SZ) {
228 read_bytes = max_buf_byte_object;
230 read_bytes = new byte[read];
232 buf.get(read_bytes, 0, read);
233 long read_result_pointer = bindings.PeerManager_read_event(
234 peer_manager_raw_pointer, peer.descriptor_raw_pointer, read_bytes);
235 if (bindings.CResult_boolPeerHandleErrorZ_is_ok(read_result_pointer)) {
236 if (bindings.CResult_boolPeerHandleErrorZ_get_ok(read_result_pointer)) {
237 key.interestOps(key.interestOps() & (~SelectionKey.OP_READ));
241 key.channel().close();
243 bindings.CResult_boolPeerHandleErrorZ_free(read_result_pointer);
246 } catch (IOException ignored) {
248 try { key.channel().close(); } catch (IOException ignored2) { }
249 peer_manager.socket_disconnected(peer.descriptor);
251 } catch (CancelledKeyException e) {
252 try { key.channel().close(); } catch (IOException ignored) { }
253 // The key is only cancelled when we have notified the PeerManager that the socket is closed, so
254 // no need to do anything here with the PeerManager.
257 peer_manager.process_events();
259 }, "NioPeerHandler NIO Thread");
263 // Ensure the types used in the above manual code match what they were when the code was written.
264 // Ensure the above manual bindings.* code changes if this fails to compile.
265 private void dummy_check_return_type_matches_manual_memory_code_above(Peer peer) {
266 byte[] read_bytes = new byte[32];
267 Result_boolPeerHandleErrorZ res = this.peer_manager.read_event(peer.descriptor, read_bytes);
271 * Connect to a peer given their node id and socket address. Blocks until a connection is established (or returns
272 * IOException) and then the connection handling runs in the background.
274 * @param their_node_id A valid 33-byte public key representing the peer's Lightning Node ID. If this is invalid,
275 * undefined behavior (read: Segfault, etc) may occur.
276 * @param remote The socket address to connect to.
277 * @param timeout_ms The amount of time, in milliseconds, up to which we will wait for connection to complete.
278 * @throws IOException If connecting to the remote endpoint fails or internal java.nio errors occur.
280 public void connect(byte[] their_node_id, SocketAddress remote, int timeout_ms) throws IOException {
281 SocketChannel chan = SocketChannel.open();
284 chan.configureBlocking(false);
285 Selector open_selector = Selector.open();
286 chan.register(open_selector, SelectionKey.OP_CONNECT);
287 if (!chan.connect(remote)) {
288 open_selector.select(timeout_ms);
290 connected = chan.finishConnect();
291 } catch (IOException e) {
292 try { chan.close(); } catch (IOException _e) { }
296 try { chan.close(); } catch (IOException _e) { }
297 throw new IOException("Timed out");
299 Peer peer = setup_socket(chan);
300 do_selector_action(() -> peer.key = chan.register(this.selector, SelectionKey.OP_READ, peer));
301 Result_CVec_u8ZPeerHandleErrorZ res = this.peer_manager.new_outbound_connection(their_node_id, peer.descriptor, get_netaddr_from_sockaddr(remote));
302 if (res instanceof Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) {
303 byte[] initial_bytes = ((Result_CVec_u8ZPeerHandleErrorZ.Result_CVec_u8ZPeerHandleErrorZ_OK) res).res;
304 if (chan.write(ByteBuffer.wrap(initial_bytes)) != initial_bytes.length) {
305 peer.descriptor.disconnect_socket();
306 this.peer_manager.socket_disconnected(peer.descriptor);
307 throw new IOException("We assume TCP socket buffer is at least a single packet in length");
310 peer.descriptor.disconnect_socket();
311 throw new IOException("LDK rejected outbound connection. This likely shouldn't ever happen.");
316 * Disconnects any connections currently open with the peer with the given node id.
318 * @param their_node_id must be a valid 33-byte public key
320 public void disconnect(byte[] their_node_id) {
321 this.peer_manager.disconnect_by_node_id(their_node_id, false);
325 * Before shutdown, we have to ensure all of our listening sockets are closed manually, as they appear
326 * to otherwise remain open and lying around on OSX (though no other platform).
328 private LinkedList<ServerSocketChannel> listening_sockets = new LinkedList();
330 * Binds a listening socket to the given address, accepting incoming connections and handling them on the background
333 * @param socket_address The address to bind the listening socket to.
334 * @throws IOException if binding the listening socket fail.
336 public void bind_listener(SocketAddress socket_address) throws IOException {
337 ServerSocketChannel listen_channel = ServerSocketChannel.open();
338 listen_channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
339 listen_channel.bind(socket_address);
340 listen_channel.configureBlocking(false);
341 do_selector_action(() -> listen_channel.register(this.selector, SelectionKey.OP_ACCEPT));
342 synchronized(listening_sockets) {
343 listening_sockets.add(listen_channel);
348 * Interrupt the background thread, stopping all peer handling.
350 * After this method is called, the behavior of future calls to methods on this NioPeerHandler are undefined.
352 public void interrupt() {
353 this.peer_manager.disconnect_all_peers();
358 } catch (InterruptedException ignored) { }
359 synchronized(listening_sockets) {
362 for (ServerSocketChannel chan : listening_sockets) {
365 listening_sockets.clear();
366 } catch (IOException ignored) {}
368 Reference.reachabilityFence(this.peer_manager); // Almost certainly overkill, but no harm in it
372 * Calls process_events on the PeerManager immediately. Normally process_events is polled regularly to check for new
373 * messages which need to be sent, but you can interrupt the poll and check immediately by calling this function.
375 public void check_events() {