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;
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);
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)