Merge pull request #36 from TheBlueMatt/main
[ldk-c-bindings] / ldk-net / ldk_net.c
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 /**
11  * This file implements what you need to get networking up with LDK using
12  * standard POSIX APIs. Its not particularly effecient (in fact quite the
13  * opposite) but should be more than sufficient for most use-cases.
14  */
15
16 #include "ldk_net.h"
17
18 #include <lightning.h>
19
20 #include <sys/socket.h>
21 #include <sys/time.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
24 #include <pthread.h>
25 #include <poll.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stddef.h>
33
34 #ifndef MSG_NOSIGNAL
35 #define MSG_NOSIGNAL 0
36 #define IS_BSD
37 #endif
38
39 #define MAX_CONNS 1024
40 struct SocketHandler {
41         const struct LDKPeerManager *ldk_peer_manager;
42         pthread_t socket_thread;
43         bool should_exit;
44         int pipefds[2];
45         // pollfds ands sockcount may only be written to with a lock on
46         // sockets_mutex.
47         // sockcount mut always be the number of sockets in pollfds
48         // Items in pollfds may only be removed in the socket_thread, other threads
49         // may only append new file descriptors at the end (via register_socket).
50         pthread_mutex_t sockets_mutex;
51         struct pollfd pollfds[MAX_CONNS];
52         nfds_t sockcount;
53 };
54
55 int register_socket(struct SocketHandler* handler, int fd, int is_listen_sock) {
56         int fd_flags = fcntl(fd, F_GETFL, 0);
57         if (fd_flags < 0) return -1;
58         if (fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK) == -1) return -1;
59
60         if (!is_listen_sock) {
61                 int opt = 1;
62                 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void*)&opt, sizeof(opt))) return -1;
63 #ifdef IS_BSD
64                 if (setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt))) return -1;
65 #endif
66         }
67
68         int lockres = pthread_mutex_lock(&handler->sockets_mutex);
69         assert(lockres == 0);
70
71         handler->pollfds[handler->sockcount].fd = fd;
72         handler->pollfds[handler->sockcount].events = POLLIN;
73         handler->pollfds[handler->sockcount].revents = 0;
74         handler->sockcount++;
75         assert(handler->sockcount <= MAX_CONNS);
76
77         lockres = pthread_mutex_unlock(&handler->sockets_mutex);
78         assert(lockres == 0);
79
80         uint8_t dummy = 0;
81         write(handler->pipefds[1], &dummy, 1);
82
83         return 0;
84 }
85
86 struct Descriptor {
87         struct SocketHandler *handler;
88         int fd;
89 };
90
91 static uintptr_t sock_send_data(void* desc, struct LDKu8slice data, bool resume_read) {
92         struct Descriptor *descriptor = (struct Descriptor*)desc;
93         ssize_t write_count = send(descriptor->fd, data.data, data.datalen, MSG_NOSIGNAL);
94         bool pause_read = false;
95         if (write_count <= 0) {
96                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
97                         pause_read = true;
98                         write_count = 0;
99                 } else {
100                         shutdown(descriptor->fd, SHUT_RDWR);
101                         uint8_t dummy = 0;
102                         write(descriptor->handler->pipefds[1], &dummy, 1);
103                         return 0;
104                 }
105         } else if (write_count < data.datalen) {
106                 pause_read = true;
107         }
108         if (pause_read || resume_read) {
109                 int lockres = pthread_mutex_lock(&descriptor->handler->sockets_mutex);
110                 assert(lockres == 0);
111                 for (int i = 0; i < descriptor->handler->sockcount; i++) {
112                         if (descriptor->handler->pollfds[i].fd == descriptor->fd) {
113                                 if (pause_read) {
114                                         descriptor->handler->pollfds[i].events &= POLLIN;
115                                         descriptor->handler->pollfds[i].events |= POLLOUT;
116                                 } else {
117                                         descriptor->handler->pollfds[i].events |= POLLIN;
118                                 }
119                                 break;
120                         }
121                 }
122                 lockres = pthread_mutex_unlock(&descriptor->handler->sockets_mutex);
123                 assert(lockres == 0);
124                 uint8_t dummy = 0;
125                 write(descriptor->handler->pipefds[1], &dummy, 1);
126         }
127
128         return write_count;
129 }
130 static void sock_disconnect(void* desc) {
131         struct Descriptor *descriptor = (struct Descriptor*)desc;
132         shutdown(descriptor->fd, SHUT_RDWR);
133         uint8_t dummy = 0;
134         write(descriptor->handler->pipefds[1], &dummy, 1);
135 }
136 static bool sock_eq(const void* desc, const struct LDKSocketDescriptor *other_arg) {
137         const struct Descriptor *descriptor = (const struct Descriptor*)desc;
138         const struct Descriptor *other_descriptor = (const struct Descriptor*)other_arg->this_arg;
139         return descriptor->fd == other_descriptor->fd;
140 }
141 static uint64_t sock_hash(const void* desc) {
142         const struct Descriptor *descriptor = (const struct Descriptor*)desc;
143         return (uint64_t)descriptor->fd;
144 }
145 static void* sock_clone(const void* desc) {
146         const struct Descriptor *descriptor = (const struct Descriptor*)desc;
147         struct Descriptor *new_desc = malloc(sizeof(struct Descriptor));
148         new_desc->handler = descriptor->handler;
149         new_desc->fd = descriptor->fd;
150         return new_desc;
151 }
152 static void sock_free(void* desc) {
153         free(desc);
154 }
155
156 static inline LDKSocketDescriptor get_descriptor(struct SocketHandler *handler, int fd) {
157         struct Descriptor *desc = malloc(sizeof(struct Descriptor));
158         desc->handler = handler;
159         desc->fd = fd;
160         LDKSocketDescriptor ret = {
161                 .this_arg = (void*)desc,
162                 .send_data = sock_send_data,
163                 .disconnect_socket = sock_disconnect,
164                 .eq = sock_eq,
165                 .hash = sock_hash,
166                 .clone = sock_clone,
167                 .free = sock_free,
168         };
169         return ret;
170 }
171
172 static void *sock_thread_fn(void* arg) {
173         struct SocketHandler *handler = (struct SocketHandler*) arg;
174
175         int lockres;
176         struct pollfd pollfds[MAX_CONNS + 1];
177         int fd_count;
178
179         int close_socks[MAX_CONNS];
180         int close_socks_count = 0;
181
182         uint8_t readbuf[8192];
183
184         while (!handler->should_exit) {
185                 {
186                         lockres = pthread_mutex_lock(&handler->sockets_mutex);
187                         assert(lockres == 0);
188                         memcpy(pollfds, handler->pollfds, sizeof(struct pollfd) * handler->sockcount);
189                         fd_count = handler->sockcount;
190                         lockres = pthread_mutex_unlock(&handler->sockets_mutex);
191                         assert(lockres == 0);
192                 }
193                 pollfds[fd_count].fd = handler->pipefds[0];
194                 pollfds[fd_count].events = POLLIN;
195                 fd_count++;
196
197                 int pollres = poll(pollfds, fd_count, 10000);
198                 assert(pollres != -1);
199                 close_socks_count = 0;
200
201                 read(pollfds[fd_count-1].fd, readbuf, sizeof(readbuf)); // Empty out the pipe
202
203                 if (pollres > 0) {
204                         for (int i = 0; i < fd_count - 1; i++) {
205                                 if (pollfds[i].revents) {
206                                         LDKSocketDescriptor descriptor = get_descriptor(handler, pollfds[i].fd);
207                                         if (pollfds[i].revents & POLLIN) {
208                                                 int readlen = read(pollfds[i].fd, readbuf, sizeof(readbuf));
209                                                 if (readlen < 0) {
210                                                         if (errno == EAGAIN || errno == EWOULDBLOCK) {
211                                                                 // Spurious wake.
212                                                         } else if (errno == EINVAL || errno == ENOTCONN) {
213                                                                 // Assume we're a listening socket!
214                                                                 int newfd = accept(pollfds[i].fd, NULL, NULL);
215                                                                 if (newfd >= 0) {
216                                                                         // Received a new connection, register it!
217                                                                         LDKSocketDescriptor new_descriptor = get_descriptor(handler, newfd);
218                                                                         LDKCResult_NonePeerHandleErrorZ con_res = PeerManager_new_inbound_connection(handler->ldk_peer_manager, new_descriptor);
219                                                                         if (con_res.result_ok) {
220                                                                                 if (register_socket(handler, newfd, 0))
221                                                                                         shutdown(newfd, SHUT_RDWR);
222                                                                         } else
223                                                                                 close(newfd);
224                                                                         CResult_NonePeerHandleErrorZ_free(con_res);
225                                                                 } else {
226                                                                         // Maybe it wasn't a listening socket, disconnect!
227                                                                         close_socks[close_socks_count++] = i;
228                                                                 }
229                                                         } else {
230                                                                 close_socks[close_socks_count++] = i;
231                                                         }
232                                                 } else if (readlen == 0) {
233                                                         // EOF
234                                                         close_socks[close_socks_count++] = i;
235                                                 } else {
236                                                         LDKu8slice data = {
237                                                                 .data = readbuf,
238                                                                 .datalen = readlen,
239                                                         };
240                                                         LDKCResult_boolPeerHandleErrorZ res = PeerManager_read_event(handler->ldk_peer_manager, &descriptor, data);
241                                                         if (res.result_ok) {
242                                                                 if (*res.contents.result) {
243                                                                         lockres = pthread_mutex_lock(&handler->sockets_mutex);
244                                                                         assert(lockres == 0);
245                                                                         assert(handler->pollfds[i - 1].fd == pollfds[i].fd); // Only we change fd order!
246                                                                         handler->pollfds[i - 1].events &= POLLIN;
247                                                                         handler->pollfds[i - 1].events |= POLLOUT;
248                                                                         lockres = pthread_mutex_unlock(&handler->sockets_mutex);
249                                                                         assert(lockres == 0);
250                                                                 }
251                                                         } else {
252                                                                 close_socks[close_socks_count++] = i;
253                                                         }
254                                                         CResult_boolPeerHandleErrorZ_free(res);
255                                                 }
256                                         }
257                                         if (pollfds[i].revents & POLLOUT) {
258                                                 LDKCResult_NonePeerHandleErrorZ res = PeerManager_write_buffer_space_avail(handler->ldk_peer_manager, &descriptor);
259                                                 if (!res.result_ok) {
260                                                         close_socks[close_socks_count++] = i;
261                                                 }
262                                                 CResult_NonePeerHandleErrorZ_free(res);
263                                         }
264                                         SocketDescriptor_free(descriptor);
265                                 }
266                         }
267                 }
268
269                 // We only do the actual socket disconnect handling in this thread,
270                 // other threads may append to pollfds and call shutdown().
271                 // Thus, in this thread, we first call socket_disconnected for each
272                 // socket we're gonna remove, then we walk the sockets and close() each
273                 // which should be disconnecting, shifting the remaining sockets down
274                 // as we walk.
275                 for (int i = 0; i < close_socks_count; i++) {
276                         LDKSocketDescriptor descriptor = get_descriptor(handler, handler->pollfds[close_socks[i]].fd);
277                         PeerManager_socket_disconnected(handler->ldk_peer_manager, &descriptor);
278                         SocketDescriptor_free(descriptor);
279                 }
280
281                 lockres = pthread_mutex_lock(&handler->sockets_mutex);
282                 assert(lockres == 0);
283                 int close_idx = 0;
284                 for (int i = 0; close_socks_count != 0 && i < handler->sockcount; i++) {
285                         if (close_idx < close_socks_count && close_socks[close_idx] == i) {
286                                 close(handler->pollfds[i].fd);
287                                 close_idx++;
288                         } else {
289                                 handler->pollfds[i-close_idx] = handler->pollfds[i];
290                         }
291                 }
292                 assert(close_idx == close_socks_count);
293                 handler->sockcount -= close_socks_count;
294                 lockres = pthread_mutex_unlock(&handler->sockets_mutex);
295                 assert(lockres == 0);
296
297                 PeerManager_process_events(handler->ldk_peer_manager);
298         }
299
300         lockres = pthread_mutex_lock(&handler->sockets_mutex);
301         assert(lockres == 0);
302         for (int i = 0; i < handler->sockcount; i++) {
303                 LDKSocketDescriptor descriptor = get_descriptor(handler, handler->pollfds[i].fd);
304                 PeerManager_socket_disconnected(handler->ldk_peer_manager, &descriptor);
305                 SocketDescriptor_free(descriptor);
306         }
307
308         for (int i = 0; i < handler->sockcount; i++) {
309                 close(handler->pollfds[i].fd);
310         }
311         close(handler->pipefds[0]);
312         close(handler->pipefds[1]);
313         handler->sockcount = 0;
314         lockres = pthread_mutex_unlock(&handler->sockets_mutex);
315         assert(lockres == 0);
316
317         return NULL;
318 }
319
320
321 void* init_socket_handling(const struct LDKPeerManager *NONNULL_PTR ldk_peer_manager) {
322         struct SocketHandler *handler = NULL;
323
324         handler = (struct SocketHandler*) calloc(1, sizeof(struct SocketHandler));
325         if (!handler) goto err;
326         handler->pipefds[0] = -1;
327         handler->pipefds[1] = -1;
328
329         handler->ldk_peer_manager = ldk_peer_manager;
330         handler->should_exit = false;
331
332         if (pipe(handler->pipefds) != 0) goto err;
333
334         int fd_flags = fcntl(handler->pipefds[0], F_GETFL, 0);
335         if (fd_flags < 0) goto err;
336         if (fcntl(handler->pipefds[0], F_SETFL, fd_flags | O_NONBLOCK) == -1) goto err;
337
338         if (pthread_mutex_init(&handler->sockets_mutex, NULL) != 0) goto err;
339         if (pthread_create(&handler->socket_thread, NULL, sock_thread_fn, handler) != 0) {
340                 pthread_mutex_destroy(&handler->sockets_mutex);
341                 goto err;
342         }
343
344         return handler;
345
346 err:
347         if (handler) {
348                 if (handler->pipefds[0] != -1) close(handler->pipefds[0]);
349                 if (handler->pipefds[1] != -1) close(handler->pipefds[1]);
350                 free(handler);
351         }
352         return NULL;
353 }
354
355 void interrupt_socket_handling(void* arg) {
356         struct SocketHandler *handler = (struct SocketHandler*) arg;
357         handler->should_exit = true;
358         uint8_t dummy = 0;
359         write(handler->pipefds[1], &dummy, 1);
360         void *retval;
361         int ret = pthread_join(handler->socket_thread, &retval);
362         assert(ret == 0);
363         free(handler);
364 }
365
366 int socket_connect(void* arg, LDKPublicKey pubkey, struct sockaddr *addr, size_t addrlen) {
367         struct SocketHandler *handler = (struct SocketHandler*) arg;
368
369         int fd = socket(addr->sa_family, SOCK_STREAM, 0);
370         if (fd < 0) return -1;
371
372         struct timeval timeout;
373         timeout.tv_sec = 1;
374         timeout.tv_usec = 0;
375         if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout))) return -2;
376         if (connect(fd, addr, addrlen)) return -3;
377         timeout.tv_sec = 120;
378         if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout))) return -3;
379
380         if (register_socket(handler, fd, 0)) return -4;
381
382         LDKSocketDescriptor descriptor = get_descriptor(handler, fd);
383         LDKCResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(handler->ldk_peer_manager, pubkey, descriptor);
384         if (con_res.result_ok) {
385                 ssize_t write_count = send(fd, con_res.contents.result->data, con_res.contents.result->datalen, MSG_NOSIGNAL);
386                 if (write_count != con_res.contents.result->datalen)
387                         shutdown(fd, SHUT_RDWR);
388         } else {
389                 shutdown(fd, SHUT_RDWR);
390         }
391         CResult_CVec_u8ZPeerHandleErrorZ_free(con_res);
392
393         return 0;
394 }
395
396 int socket_bind(void* arg, struct sockaddr *addr, socklen_t addrlen) {
397         struct SocketHandler *handler = (struct SocketHandler*) arg;
398         int fd = socket(addr->sa_family, SOCK_STREAM, 0);
399         if (fd < 0) return -1;
400
401         int reuseon = 1;
402         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseon, sizeof(reuseon))) return -2;
403
404         if (bind(fd, addr, addrlen)) return -3;
405         if (listen(fd, 32)) return -4;
406
407         if (register_socket(handler, fd, 1)) return -5;
408
409         return 0;
410 }