]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Send peers the remote address we have for them in ldk-net
authorMatt Corallo <git@bluematt.me>
Mon, 4 Apr 2022 18:04:40 +0000 (18:04 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 4 Apr 2022 18:16:31 +0000 (18:16 +0000)
The majority of this commit was authored by
Jeffrey Czyz <jkczyz@gmail.com>

ldk-net/ldk_net.c

index 8bae7af192c1c08b0f41f1bbf1310718fb635c5b..97521368fc7c94f20a14942a9f40d8fb0e1b6def 100644 (file)
@@ -168,6 +168,31 @@ static inline LDKSocketDescriptor get_descriptor(struct SocketHandler *handler,
        return ret;
 }
 
+static LDKCOption_NetAddressZ get_remote_network_address(int fd) {
+       struct sockaddr_storage sockaddr;
+       socklen_t remote_addr_len = sizeof(sockaddr);
+       if (getpeername(fd, (struct sockaddr*)&sockaddr, &remote_addr_len) == -1) {
+               return COption_NetAddressZ_none();
+       }
+
+       switch (sockaddr.ss_family) {
+       case AF_INET: {
+               const struct sockaddr_in *remote_addr = (struct sockaddr_in*)&sockaddr;
+               LDKFourBytes addr;
+               memcpy(&addr, &remote_addr->sin_addr.s_addr, 4);
+               return COption_NetAddressZ_some(NetAddress_ipv4(addr, ntohs(remote_addr->sin_port)));
+       }
+       case AF_INET6: {
+               const struct sockaddr_in6 *remote_addr = (struct sockaddr_in6*)&sockaddr;
+               LDKSixteenBytes addr;
+               memcpy(&addr, &remote_addr->sin6_addr.s6_addr, 16);
+               return COption_NetAddressZ_some(NetAddress_ipv6(addr, ntohs(remote_addr->sin6_port)));
+       }
+       default:
+               return COption_NetAddressZ_none();
+       }
+}
+
 static void *sock_thread_fn(void* arg) {
        struct SocketHandler *handler = (struct SocketHandler*) arg;
 
@@ -214,10 +239,7 @@ static void *sock_thread_fn(void* arg) {
                                                                if (newfd >= 0) {
                                                                        // Received a new connection, register it!
                                                                        LDKSocketDescriptor new_descriptor = get_descriptor(handler, newfd);
-                                                                       LDKCOption_NetAddressZ remote_network_address = {
-                                                                               .tag = LDKCOption_NetAddressZ_None,
-                                                                       };
-                                                                       LDKCResult_NonePeerHandleErrorZ con_res = PeerManager_new_inbound_connection(&handler->ldk_peer_manager, new_descriptor, remote_network_address);
+                                                                       LDKCResult_NonePeerHandleErrorZ con_res = PeerManager_new_inbound_connection(&handler->ldk_peer_manager, new_descriptor, get_remote_network_address(newfd));
                                                                        if (con_res.result_ok) {
                                                                                if (register_socket(handler, newfd, 0))
                                                                                        shutdown(newfd, SHUT_RDWR);
@@ -381,10 +403,7 @@ int socket_connect(void* arg, LDKPublicKey pubkey, struct sockaddr *addr, size_t
        if (register_socket(handler, fd, 0)) return -4;
 
        LDKSocketDescriptor descriptor = get_descriptor(handler, fd);
-       LDKCOption_NetAddressZ remote_network_address = {
-               .tag = LDKCOption_NetAddressZ_None,
-       };
-       LDKCResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&handler->ldk_peer_manager, pubkey, descriptor, remote_network_address);
+       LDKCResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&handler->ldk_peer_manager, pubkey, descriptor, get_remote_network_address(fd));
        if (con_res.result_ok) {
                ssize_t write_count = send(fd, con_res.contents.result->data, con_res.contents.result->datalen, MSG_NOSIGNAL);
                if (write_count != con_res.contents.result->datalen)