From 42be2b343dac8eba64fa3065600b3ebfdba4b622 Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Wed, 13 Sep 2023 16:47:49 +0530 Subject: [PATCH] Move all common TCP sockets code to tcp_server_socket.h. --- internal/platform/implementation/linux/BUILD | 1 + .../implementation/linux/wifi_direct.cc | 60 +++--------------- .../linux/wifi_direct_server_socket.cc | 39 ++---------- .../linux/wifi_direct_server_socket.h | 8 ++- .../implementation/linux/wifi_direct_socket.h | 19 ++---- .../platform/implementation/linux/wifi_lan.cc | 61 +++---------------- .../linux/wifi_lan_server_socket.cc | 39 ++---------- .../linux/wifi_lan_server_socket.h | 10 +-- .../implementation/linux/wifi_lan_socket.h | 22 +++---- 9 files changed, 53 insertions(+), 206 deletions(-) diff --git a/internal/platform/implementation/linux/BUILD b/internal/platform/implementation/linux/BUILD index fa2e32f0..0e150f61 100644 --- a/internal/platform/implementation/linux/BUILD +++ b/internal/platform/implementation/linux/BUILD @@ -82,6 +82,7 @@ cc_library( "network_manager_active_connection.h", "network_manager_access_point.h", "stream.h", + "tcp_server_socket.h", "wifi_direct.h", "wifi_direct_server_socket.h", "wifi_direct_socket.h", diff --git a/internal/platform/implementation/linux/wifi_direct.cc b/internal/platform/implementation/linux/wifi_direct.cc index 5830b16d..37ef709b 100644 --- a/internal/platform/implementation/linux/wifi_direct.cc +++ b/internal/platform/implementation/linux/wifi_direct.cc @@ -17,6 +17,7 @@ #include #include +#include "internal/platform/implementation/linux/tcp_server_socket.h" #include "internal/platform/implementation/linux/wifi_direct.h" #include "internal/platform/implementation/linux/wifi_direct_server_socket.h" #include "internal/platform/implementation/linux/wifi_direct_socket.h" @@ -31,29 +32,10 @@ std::unique_ptr NetworkManagerWifiDirectMedium::ConnectToService( absl::string_view ip_address, int port, CancellationFlag *cancellation_flag) { - int sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0) { - NEARBY_LOGS(ERROR) << __func__ - << ": Error opening socket: " << std::strerror(errno); - return nullptr; - } + auto socket = TCPSocket::Connect(std::string(ip_address), port); + if (!socket.has_value()) return nullptr; - NEARBY_LOGS(VERBOSE) << __func__ << ": Connecting to " << ip_address << ":" - << port; - struct sockaddr_in addr; - addr.sin_addr.s_addr = inet_addr(std::string(ip_address).c_str()); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - - auto ret = - connect(sock, reinterpret_cast(&addr), sizeof(addr)); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": Error connecting to socket: " - << std::strerror(errno); - return nullptr; - } - - return std::make_unique(sock); + return std::make_unique(std::move(*socket)); } std::unique_ptr @@ -72,39 +54,11 @@ NetworkManagerWifiDirectMedium::ListenForService(int port) { return nullptr; } - auto sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0) { - NEARBY_LOGS(ERROR) << __func__ - << ": Error opening socket: " << std::strerror(errno); - return nullptr; - } - - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(ip4addresses[0].c_str()); - addr.sin_port = htons(port); - - auto ret = - bind(sock, reinterpret_cast(&addr), sizeof(addr)); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ - << ": Error binding to socket: " << std::strerror(errno); - return nullptr; - } - - NEARBY_LOGS(VERBOSE) << __func__ << ": Listening for services on " - << ip4addresses[0] << ":" << port << " on device " - << wireless_device_->getObjectPath(); - - ret = listen(sock, 0); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": Error listening on socket: " - << std::strerror(errno); - return nullptr; - } + auto socket = TCPServerSocket::Listen(std::ref(ip4addresses[0]), port); + if (!socket.has_value()) return nullptr; return std::make_unique( - sock, std::move(active_connection), network_manager_); + std::move(*socket), std::move(active_connection), network_manager_); } bool NetworkManagerWifiDirectMedium::ConnectWifiDirect( diff --git a/internal/platform/implementation/linux/wifi_direct_server_socket.cc b/internal/platform/implementation/linux/wifi_direct_server_socket.cc index b4e46cc5..395f412b 100644 --- a/internal/platform/implementation/linux/wifi_direct_server_socket.cc +++ b/internal/platform/implementation/linux/wifi_direct_server_socket.cc @@ -34,47 +34,18 @@ std::string NetworkManagerWifiDirectServerSocket::GetIPAddress() const { } int NetworkManagerWifiDirectServerSocket::GetPort() const { - struct sockaddr_in sin; - socklen_t len = sizeof(sin); - auto ret = - getsockname(fd_.get(), reinterpret_cast(&sin), &len); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": Error getting information for socket " - << fd_.get() << ": " << std::strerror(errno); - return 0; - } - - return ntohs(sin.sin_port); + return server_socket_.GetPort(); } std::unique_ptr NetworkManagerWifiDirectServerSocket::Accept() { - struct sockaddr_in addr; - socklen_t len = sizeof(addr); - - auto conn = - accept(fd_.get(), reinterpret_cast(&addr), &len); - if (conn < 0) { - NEARBY_LOGS(ERROR) << __func__ - << ": Error accepting incoming connections on socket " - << fd_.get() << ": " << std::strerror(errno); - return nullptr; - } - - return std::make_unique(conn); + auto sock = server_socket_.Accept(); + if (!sock.has_value()) return nullptr; + return std::make_unique(std::move(*sock)); } Exception NetworkManagerWifiDirectServerSocket::Close() { - int fd = fd_.release(); - shutdown(fd, SHUT_RDWR); - auto ret = close(fd); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": Error closing socket " << fd << ": " - << std::strerror(errno); - return {Exception::kFailed}; - } - - return {Exception::kSuccess}; + return server_socket_.Close(); } } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/wifi_direct_server_socket.h b/internal/platform/implementation/linux/wifi_direct_server_socket.h index 727ab490..03077626 100644 --- a/internal/platform/implementation/linux/wifi_direct_server_socket.h +++ b/internal/platform/implementation/linux/wifi_direct_server_socket.h @@ -17,6 +17,7 @@ #include #include "internal/platform/implementation/linux/network_manager_active_connection.h" +#include "internal/platform/implementation/linux/tcp_server_socket.h" #include "internal/platform/implementation/linux/wifi_medium.h" #include "internal/platform/implementation/wifi_direct.h" namespace nearby { @@ -25,9 +26,10 @@ class NetworkManagerWifiDirectServerSocket : public api::WifiDirectServerSocket { public: NetworkManagerWifiDirectServerSocket( - int socket, std::unique_ptr active_conn, + TCPServerSocket socket, + std::unique_ptr active_conn, std::shared_ptr network_manager) - : fd_(socket), + : server_socket_(std::move(socket)), active_conn_(std::move(active_conn)), network_manager_(std::move(network_manager)) {} @@ -37,7 +39,7 @@ class NetworkManagerWifiDirectServerSocket Exception Close() override; private: - sdbus::UnixFd fd_; + TCPServerSocket server_socket_; std::unique_ptr active_conn_; std::shared_ptr network_manager_; }; diff --git a/internal/platform/implementation/linux/wifi_direct_socket.h b/internal/platform/implementation/linux/wifi_direct_socket.h index 011b0617..293d1789 100644 --- a/internal/platform/implementation/linux/wifi_direct_socket.h +++ b/internal/platform/implementation/linux/wifi_direct_socket.h @@ -17,29 +17,22 @@ #include "internal/platform/exception.h" #include "internal/platform/implementation/linux/stream.h" +#include "internal/platform/implementation/linux/tcp_server_socket.h" #include "internal/platform/implementation/wifi_direct.h" namespace nearby { namespace linux { class WifiDirectSocket : public api::WifiDirectSocket { public: - explicit WifiDirectSocket(int socket) - : fd_(sdbus::UnixFd(socket)), output_stream_(fd_), input_stream_(fd_) {} + explicit WifiDirectSocket(TCPSocket socket) : socket_(std::move(socket)) {} - InputStream &GetInputStream() override { return input_stream_; }; - OutputStream &GetOutputStream() override { return output_stream_; }; + InputStream &GetInputStream() override { return socket_.GetInputStream(); } + OutputStream &GetOutputStream() override { return socket_.GetOutputStream(); } - Exception Close() override { - input_stream_.Close(); - output_stream_.Close(); - - return Exception{Exception::kSuccess}; - }; + Exception Close() override { return socket_.Close(); }; private: - sdbus::UnixFd fd_; - OutputStream output_stream_; - InputStream input_stream_; + TCPSocket socket_; }; } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/wifi_lan.cc b/internal/platform/implementation/linux/wifi_lan.cc index 99d52c31..7e825497 100644 --- a/internal/platform/implementation/linux/wifi_lan.cc +++ b/internal/platform/implementation/linux/wifi_lan.cc @@ -28,6 +28,7 @@ #include "absl/strings/substitute.h" #include "internal/platform/implementation/linux/avahi.h" #include "internal/platform/implementation/linux/dbus.h" +#include "internal/platform/implementation/linux/tcp_server_socket.h" #include "internal/platform/implementation/linux/wifi_lan.h" #include "internal/platform/implementation/linux/wifi_lan_server_socket.h" #include "internal/platform/implementation/linux/wifi_lan_socket.h" @@ -204,64 +205,18 @@ bool WifiLanMedium::StopDiscovery(const std::string &service_type) { std::unique_ptr WifiLanMedium::ConnectToService( const std::string &ip_address, int port, CancellationFlag *cancellation_flag) { - int sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0) { - NEARBY_LOGS(ERROR) << __func__ - << ": Error opening socket: " << std::strerror(errno); - return nullptr; - } - - NEARBY_LOGS(VERBOSE) << __func__ << ": Connecting to " << ip_address << ":" - << port; - struct sockaddr_in addr; - addr.sin_addr.s_addr = inet_addr(ip_address.c_str()); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - - auto ret = - connect(sock, reinterpret_cast(&addr), sizeof(addr)); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": Error connecting to socket: " - << std::strerror(errno); - return nullptr; - } - - sdbus::UnixFd fd(sock); - return std::make_unique(std::move(fd)); + auto socket = TCPSocket::Connect(ip_address, port); + if (!socket.has_value()) return nullptr; + return std::make_unique(*socket); } std::unique_ptr WifiLanMedium::ListenForService( int port) { - auto sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock < 0) { - NEARBY_LOGS(ERROR) << __func__ - << ": Error opening socket: " << std::strerror(errno); - return nullptr; - } + auto socket = TCPServerSocket::Listen(std::nullopt, port); + if (!socket.has_value()) return nullptr; - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(INADDR_ANY); - addr.sin_port = htons(port); - - auto ret = - bind(sock, reinterpret_cast(&addr), sizeof(addr)); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ - << ": Error binding to socket: " << std::strerror(errno); - return nullptr; - } - - ret = listen(sock, 0); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": Error listening on socket: " - << std::strerror(errno); - return nullptr; - } - - NEARBY_LOGS(VERBOSE) << __func__ << "Listening for services on port " << port; - - return std::make_unique(sock, network_manager_); + return std::make_unique(std::move(*socket), + network_manager_); } absl::optional> GetDynamicPortRange() { diff --git a/internal/platform/implementation/linux/wifi_lan_server_socket.cc b/internal/platform/implementation/linux/wifi_lan_server_socket.cc index b82ff2a4..cffde75c 100644 --- a/internal/platform/implementation/linux/wifi_lan_server_socket.cc +++ b/internal/platform/implementation/linux/wifi_lan_server_socket.cc @@ -27,7 +27,6 @@ #include "internal/platform/implementation/linux/dbus.h" #include "internal/platform/implementation/linux/wifi_lan_server_socket.h" #include "internal/platform/implementation/linux/wifi_lan_socket.h" -#include "internal/platform/implementation/linux/wifi_medium.h" #include "internal/platform/logging.h" namespace nearby { @@ -75,46 +74,18 @@ std::string WifiLanServerSocket::GetIPAddress() const { } int WifiLanServerSocket::GetPort() const { - struct sockaddr_in sin; - socklen_t len = sizeof(sin); - auto ret = - getsockname(fd_.get(), reinterpret_cast(&sin), &len); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": Error getting information for socket " - << fd_.get() << ": " << std::strerror(errno); - return 0; - } - - return ntohs(sin.sin_port); + return server_socket_.GetPort(); } std::unique_ptr WifiLanServerSocket::Accept() { - struct sockaddr_in addr; - socklen_t len = sizeof(addr); + auto sock = server_socket_.Accept(); + if (!sock.has_value()) return nullptr; - auto conn = - accept(fd_.get(), reinterpret_cast(&addr), &len); - if (conn < 0) { - NEARBY_LOGS(ERROR) << __func__ - << ": Error accepting incoming connections on socket " - << fd_.get() << ": " << std::strerror(errno); - return nullptr; - } - - return std::make_unique(sdbus::UnixFd(conn)); + return std::make_unique(std::move(*sock)); } Exception WifiLanServerSocket::Close() { - int fd = fd_.release(); - shutdown(fd, SHUT_RDWR); - auto ret = close(fd); - if (ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": Error closing socket " << fd << ": " - << std::strerror(errno); - return {Exception::kFailed}; - } - - return {Exception::kSuccess}; + return server_socket_.Close(); } } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/wifi_lan_server_socket.h b/internal/platform/implementation/linux/wifi_lan_server_socket.h index d81bd7a3..00730bac 100644 --- a/internal/platform/implementation/linux/wifi_lan_server_socket.h +++ b/internal/platform/implementation/linux/wifi_lan_server_socket.h @@ -22,15 +22,17 @@ #include "internal/platform/exception.h" #include "internal/platform/implementation/linux/network_manager.h" +#include "internal/platform/implementation/linux/tcp_server_socket.h" #include "internal/platform/implementation/wifi_lan.h" namespace nearby { namespace linux { class WifiLanServerSocket : public api::WifiLanServerSocket { public: - explicit WifiLanServerSocket(int socket, - std::shared_ptr network_manager) - : fd_(sdbus::UnixFd(socket)), + explicit WifiLanServerSocket( + TCPServerSocket socket, + std::shared_ptr network_manager) + : server_socket_(std::move(socket)), network_manager_(std::move(network_manager)), system_bus_(network_manager_->GetConnection()) {} @@ -41,7 +43,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket { Exception Close() override; private: - sdbus::UnixFd fd_; + TCPServerSocket server_socket_; std::shared_ptr network_manager_; std::shared_ptr system_bus_; }; diff --git a/internal/platform/implementation/linux/wifi_lan_socket.h b/internal/platform/implementation/linux/wifi_lan_socket.h index 63c29b63..816fa065 100644 --- a/internal/platform/implementation/linux/wifi_lan_socket.h +++ b/internal/platform/implementation/linux/wifi_lan_socket.h @@ -20,6 +20,7 @@ #include #include "internal/platform/implementation/linux/stream.h" +#include "internal/platform/implementation/linux/tcp_server_socket.h" #include "internal/platform/implementation/wifi_lan.h" #include "internal/platform/input_stream.h" #include "internal/platform/output_stream.h" @@ -28,21 +29,18 @@ namespace nearby { namespace linux { class WifiLanSocket : public api::WifiLanSocket { public: - explicit WifiLanSocket(sdbus::UnixFd fd) - : output_stream_(fd), input_stream_(fd) {} + explicit WifiLanSocket(TCPSocket sock) : socket_(std::move(sock)) {} - nearby::InputStream &GetInputStream() override { return input_stream_; }; - nearby::OutputStream &GetOutputStream() override { return output_stream_; }; - Exception Close() override { - input_stream_.Close(); - output_stream_.Close(); - - return Exception{Exception::kSuccess}; - }; + nearby::InputStream &GetInputStream() override { + return socket_.GetInputStream(); + } + nearby::OutputStream &GetOutputStream() override { + return socket_.GetOutputStream(); + } + Exception Close() override { return socket_.Close(); } private: - OutputStream output_stream_; - InputStream input_stream_; + TCPSocket socket_; }; } // namespace linux } // namespace nearby