From 25f85d499b97c47dc9ca2be2fff4a9755eeab9e5 Mon Sep 17 00:00:00 2001 From: Sergey Protserov Date: Fri, 22 Feb 2019 23:03:04 +0300 Subject: [PATCH 1/2] Add support for running this server only on local interface --- include/afina/network/Server.h | 7 +++++-- src/main.cpp | 13 +++++++++---- src/network/mt_blocking/ServerImpl.cpp | 13 +++++++++---- src/network/mt_blocking/ServerImpl.h | 2 +- src/network/mt_nonblocking/ServerImpl.cpp | 13 +++++++++---- src/network/mt_nonblocking/ServerImpl.h | 2 +- src/network/st_blocking/ServerImpl.cpp | 13 +++++++++---- src/network/st_blocking/ServerImpl.h | 2 +- src/network/st_nonblocking/ServerImpl.cpp | 13 +++++++++---- src/network/st_nonblocking/ServerImpl.h | 2 +- 10 files changed, 54 insertions(+), 26 deletions(-) diff --git a/include/afina/network/Server.h b/include/afina/network/Server.h index 486de4868..56724b042 100644 --- a/include/afina/network/Server.h +++ b/include/afina/network/Server.h @@ -17,8 +17,8 @@ namespace Network { */ class Server { public: - Server(std::shared_ptr ps, std::shared_ptr pl) - : pStorage(ps), pLogging(pl) {} + Server(std::shared_ptr ps, std::shared_ptr pl, bool local) + : pStorage(ps), pLogging(pl), _local(local) {} virtual ~Server() {} /** @@ -55,6 +55,9 @@ class Server { * Logging service to be used in order to report application progress */ std::shared_ptr pLogging; + + // Whether to run locally + bool _local; }; } // namespace Network diff --git a/src/main.cpp b/src/main.cpp index 3013d17e3..dba958e86 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,18 +60,22 @@ class Application { // Step 2: Configure network std::string network_type = "st_block"; + bool local = false; if (options.count("network") > 0) { network_type = options["network"].as(); } + if (options.count("local") > 0) { + local = true; + } if (network_type == "st_block") { - server = std::make_shared(storage, logService); + server = std::make_shared(storage, logService, local); } else if (network_type == "mt_block") { - server = std::make_shared(storage, logService); + server = std::make_shared(storage, logService, local); } else if (network_type == "st_nonblock") { - server = std::make_shared(storage, logService); + server = std::make_shared(storage, logService, local); } else if (network_type == "mt_nonblock") { - server = std::make_shared(storage, logService); + server = std::make_shared(storage, logService, local); } else { throw std::runtime_error("Unknown network type"); } @@ -130,6 +134,7 @@ int main(int argc, char **argv) { options.add_options()("s,storage", "Type of storage service to use", cxxopts::value()); options.add_options()("n,network", "Type of network service to use", cxxopts::value()); options.add_options()("h,help", "Print usage info"); + options.add_options()("l,local", "Bind to local interface only"); options.parse(argc, argv); if (options.count("help") > 0) { diff --git a/src/network/mt_blocking/ServerImpl.cpp b/src/network/mt_blocking/ServerImpl.cpp index 29533a962..831c902e9 100644 --- a/src/network/mt_blocking/ServerImpl.cpp +++ b/src/network/mt_blocking/ServerImpl.cpp @@ -28,7 +28,8 @@ namespace Network { namespace MTblocking { // See Server.h -ServerImpl::ServerImpl(std::shared_ptr ps, std::shared_ptr pl) : Server(ps, pl) {} +ServerImpl::ServerImpl(std::shared_ptr ps, std::shared_ptr pl, bool local) + : Server(ps, pl, local) {} // See Server.h ServerImpl::~ServerImpl() {} @@ -47,9 +48,13 @@ void ServerImpl::Start(uint16_t port, uint32_t n_accept, uint32_t n_workers) { struct sockaddr_in server_addr; std::memset(&server_addr, 0, sizeof(server_addr)); - server_addr.sin_family = AF_INET; // IPv4 - server_addr.sin_port = htons(port); // TCP port number - server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any address + server_addr.sin_family = AF_INET; // IPv4 + server_addr.sin_port = htons(port); // TCP port number + if (!_local) { + server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any address + } else { + server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Bind only to loopback interface + } _server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (_server_socket == -1) { diff --git a/src/network/mt_blocking/ServerImpl.h b/src/network/mt_blocking/ServerImpl.h index df8ecd487..04964816c 100644 --- a/src/network/mt_blocking/ServerImpl.h +++ b/src/network/mt_blocking/ServerImpl.h @@ -20,7 +20,7 @@ namespace MTblocking { */ class ServerImpl : public Server { public: - ServerImpl(std::shared_ptr ps, std::shared_ptr pl); + ServerImpl(std::shared_ptr ps, std::shared_ptr pl, bool local); ~ServerImpl(); // See Server.h diff --git a/src/network/mt_nonblocking/ServerImpl.cpp b/src/network/mt_nonblocking/ServerImpl.cpp index 316a69586..03c06807b 100644 --- a/src/network/mt_nonblocking/ServerImpl.cpp +++ b/src/network/mt_nonblocking/ServerImpl.cpp @@ -30,7 +30,8 @@ namespace Network { namespace MTnonblock { // See Server.h -ServerImpl::ServerImpl(std::shared_ptr ps, std::shared_ptr pl) : Server(ps, pl) {} +ServerImpl::ServerImpl(std::shared_ptr ps, std::shared_ptr pl, bool local) + : Server(ps, pl, local) {} // See Server.h ServerImpl::~ServerImpl() {} @@ -50,9 +51,13 @@ void ServerImpl::Start(uint16_t port, uint32_t n_acceptors, uint32_t n_workers) // Create server socket struct sockaddr_in server_addr; std::memset(&server_addr, 0, sizeof(server_addr)); - server_addr.sin_family = AF_INET; // IPv4 - server_addr.sin_port = htons(port); // TCP port number - server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any address + server_addr.sin_family = AF_INET; // IPv4 + server_addr.sin_port = htons(port); // TCP port number + if (!_local) { + server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any address + } else { + server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Bind to loopback interface only + } _server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (_server_socket == -1) { diff --git a/src/network/mt_nonblocking/ServerImpl.h b/src/network/mt_nonblocking/ServerImpl.h index d49f75ff3..4862b864d 100644 --- a/src/network/mt_nonblocking/ServerImpl.h +++ b/src/network/mt_nonblocking/ServerImpl.h @@ -23,7 +23,7 @@ class Worker; */ class ServerImpl : public Server { public: - ServerImpl(std::shared_ptr ps, std::shared_ptr pl); + ServerImpl(std::shared_ptr ps, std::shared_ptr pl, bool local); ~ServerImpl(); // See Server.h diff --git a/src/network/st_blocking/ServerImpl.cpp b/src/network/st_blocking/ServerImpl.cpp index ea743cad2..505c6034e 100644 --- a/src/network/st_blocking/ServerImpl.cpp +++ b/src/network/st_blocking/ServerImpl.cpp @@ -28,7 +28,8 @@ namespace Network { namespace STblocking { // See Server.h -ServerImpl::ServerImpl(std::shared_ptr ps, std::shared_ptr pl) : Server(ps, pl) {} +ServerImpl::ServerImpl(std::shared_ptr ps, std::shared_ptr pl, bool local) + : Server(ps, pl, local) {} // See Server.h ServerImpl::~ServerImpl() {} @@ -59,9 +60,13 @@ void ServerImpl::Start(uint16_t port, uint32_t n_accept, uint32_t n_workers) { // Note we need to convert the port to network order struct sockaddr_in server_addr; std::memset(&server_addr, 0, sizeof(server_addr)); - server_addr.sin_family = AF_INET; // IPv4 - server_addr.sin_port = htons(port); // TCP port number - server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any address + server_addr.sin_family = AF_INET; // IPv4 + server_addr.sin_port = htons(port); // TCP port number + if (!_local) { + server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any address + } else { + server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Bind only to loopback interface + } // Arguments are: // - Family: IPv4 diff --git a/src/network/st_blocking/ServerImpl.h b/src/network/st_blocking/ServerImpl.h index 018524424..c1144b23d 100644 --- a/src/network/st_blocking/ServerImpl.h +++ b/src/network/st_blocking/ServerImpl.h @@ -20,7 +20,7 @@ namespace STblocking { */ class ServerImpl : public Server { public: - ServerImpl(std::shared_ptr ps, std::shared_ptr pl); + ServerImpl(std::shared_ptr ps, std::shared_ptr pl, bool local); ~ServerImpl(); // See Server.h diff --git a/src/network/st_nonblocking/ServerImpl.cpp b/src/network/st_nonblocking/ServerImpl.cpp index 80b79a4fe..e934852c6 100644 --- a/src/network/st_nonblocking/ServerImpl.cpp +++ b/src/network/st_nonblocking/ServerImpl.cpp @@ -29,7 +29,8 @@ namespace Network { namespace STnonblock { // See Server.h -ServerImpl::ServerImpl(std::shared_ptr ps, std::shared_ptr pl) : Server(ps, pl) {} +ServerImpl::ServerImpl(std::shared_ptr ps, std::shared_ptr pl, bool local) + : Server(ps, pl, local) {} // See Server.h ServerImpl::~ServerImpl() {} @@ -49,9 +50,13 @@ void ServerImpl::Start(uint16_t port, uint32_t n_acceptors, uint32_t n_workers) // Create server socket struct sockaddr_in server_addr; std::memset(&server_addr, 0, sizeof(server_addr)); - server_addr.sin_family = AF_INET; // IPv4 - server_addr.sin_port = htons(port); // TCP port number - server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any address + server_addr.sin_family = AF_INET; // IPv4 + server_addr.sin_port = htons(port); // TCP port number + if (!_local) { + server_addr.sin_addr.s_addr = INADDR_ANY; // Bind to any address + } else { + server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Bind only to loopback interface + } _server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (_server_socket == -1) { diff --git a/src/network/st_nonblocking/ServerImpl.h b/src/network/st_nonblocking/ServerImpl.h index 21dc4973c..860e4daad 100644 --- a/src/network/st_nonblocking/ServerImpl.h +++ b/src/network/st_nonblocking/ServerImpl.h @@ -23,7 +23,7 @@ class Worker; */ class ServerImpl : public Server { public: - ServerImpl(std::shared_ptr ps, std::shared_ptr pl); + ServerImpl(std::shared_ptr ps, std::shared_ptr pl, bool local); ~ServerImpl(); // See Server.h From edc686ac52b34db4cb57695d7e1328e123129414 Mon Sep 17 00:00:00 2001 From: Sergey Protserov Date: Fri, 1 Mar 2019 09:32:30 +0300 Subject: [PATCH 2/2] Commit for pull request: fix Execute function prototype to enable perfect forwarding --- include/afina/concurrency/Executor.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/afina/concurrency/Executor.h b/include/afina/concurrency/Executor.h index d036140ea..d3f849825 100644 --- a/include/afina/concurrency/Executor.h +++ b/include/afina/concurrency/Executor.h @@ -28,7 +28,7 @@ class Executor { kStopped }; - Executor(std::string name, int size); + Executor(const std::string &name, int size); ~Executor(); /** @@ -46,7 +46,7 @@ class Executor { * That function doesn't wait for function result. Function could always be written in a way to notify caller about * execution finished by itself */ - template bool Execute(F &&func, Types... args) { + template bool Execute(F &&func, Types&&... args) { // Prepare "task" auto exec = std::bind(std::forward(func), std::forward(args)...);