From f8486604c411b10c66716116961d3721b3d16533 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Tue, 2 Mar 2021 19:22:59 -0400 Subject: [PATCH] Export UDP file descriptor (for android) --- llarp/ev/ev_libuv.cpp | 10 ++++++++++ llarp/ev/udp_handle.hpp | 8 ++++++++ llarp/link/server.cpp | 6 ++++++ llarp/link/server.hpp | 4 ++++ llarp/router/router.cpp | 2 +- 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/llarp/ev/ev_libuv.cpp b/llarp/ev/ev_libuv.cpp index c4b758515..02f836cf3 100644 --- a/llarp/ev/ev_libuv.cpp +++ b/llarp/ev/ev_libuv.cpp @@ -66,6 +66,16 @@ namespace llarp::uv bool send(const SockAddr& dest, const llarp_buffer_t& buf) override; + std::optional + file_descriptor() override + { +#ifndef _WIN32 + if (int fd = handle->fd(); fd >= 0) + return fd; +#endif + return std::nullopt; + } + void close() override; diff --git a/llarp/ev/udp_handle.hpp b/llarp/ev/udp_handle.hpp index 94d24c950..8169bd8a1 100644 --- a/llarp/ev/udp_handle.hpp +++ b/llarp/ev/udp_handle.hpp @@ -25,6 +25,14 @@ namespace llarp virtual void close() = 0; + // Returns the file descriptor of the socket, if available. This generally exists only after + // listen() has been called, and never exists on Windows. + virtual std::optional + file_descriptor() + { + return std::nullopt; + } + // Base class destructor virtual ~UDPHandle() = default; diff --git a/llarp/link/server.cpp b/llarp/link/server.cpp index a9f0108c0..839a6a734 100644 --- a/llarp/link/server.cpp +++ b/llarp/link/server.cpp @@ -489,4 +489,10 @@ namespace llarp return true; } + std::optional + ILinkLayer::GetUDPFD() const + { + return m_udp->file_descriptor(); + } + } // namespace llarp diff --git a/llarp/link/server.hpp b/llarp/link/server.hpp index 01f54504e..2e9f828a0 100644 --- a/llarp/link/server.hpp +++ b/llarp/link/server.hpp @@ -222,6 +222,10 @@ namespace llarp return m_Pending.size(); } + // Returns the file description of the UDP server, if available. + std::optional + GetUDPFD() const; + private: const SecretKey& m_RouterEncSecret; diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 0d22cd488..21507980f 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -1326,7 +1326,7 @@ namespace llarp continue; #if defined(ANDROID) - m_OutboundUDPSocket = link->GetUDPSocket(); + m_OutboundUDPSocket = link->GetUDPFD().value_or(-1); #endif _linkManager.AddLink(std::move(link), false); return true;