From 0840c07e24bacf5b21331fac443804e63b1b9cb1 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 24 Feb 2018 21:14:43 +0000 Subject: [PATCH] Improving build system Instead of #ifdefing files out, they are no longer included by CMake instead. --- .gitignore | 2 ++ CMakeLists.txt | 4 +-- include/frnetlib/SSLContext.h | 5 ---- include/frnetlib/SSLListener.h | 4 --- include/frnetlib/SSLSocket.h | 26 ++++++++----------- include/frnetlib/Socket.h | 47 +++++++++++++++++++--------------- include/frnetlib/TcpSocket.h | 2 +- src/SSLListener.cpp | 21 ++++++++------- src/SSLSocket.cpp | 25 ++++++++---------- 9 files changed, 62 insertions(+), 74 deletions(-) diff --git a/.gitignore b/.gitignore index 82aa793..fa74d57 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .idea cmake-build-debug cmake-build-release + +CMakeCache\.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 490dabe..1703890 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ add_definitions(-DLISTEN_QUEUE_SIZE=${LISTEN_QUEUE_SIZE}) if(USE_SSL) FIND_PACKAGE(MBEDTLS) INCLUDE_DIRECTORIES(${MBEDTLS_INCLUDE_DIR}) - add_definitions(-DSSL_ENABLED) + set(SOURCE_FILES ${SOURCE_FILES} src/SSLSocket.cpp include/frnetlib/SSLSocket.h src/SSLListener.cpp include/frnetlib/SSLListener.h include/frnetlib/SSLContext.h) endif() add_definitions(-DNOMINMAX) @@ -37,7 +37,7 @@ add_definitions(-Dntodh) set( INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include" ) set( SOURCE_PATH "${PROJECT_SOURCE_DIR}/src" ) -set(SOURCE_FILES main.cpp src/TcpSocket.cpp include/frnetlib/TcpSocket.h src/TcpListener.cpp include/frnetlib/TcpListener.h src/Socket.cpp include/frnetlib/Socket.h src/Packet.cpp include/frnetlib/Packet.h include/frnetlib/NetworkEncoding.h src/SocketSelector.cpp include/frnetlib/SocketSelector.h src/HttpRequest.cpp include/frnetlib/HttpRequest.h src/HttpResponse.cpp include/frnetlib/HttpResponse.h src/Http.cpp include/frnetlib/Http.h src/SSLSocket.cpp include/frnetlib/SSLSocket.h src/SSLListener.cpp include/frnetlib/SSLListener.h include/frnetlib/SSLContext.h src/SocketReactor.cpp include/frnetlib/SocketReactor.h include/frnetlib/Packetable.h include/frnetlib/Listener.h src/URL.cpp include/frnetlib/URL.h include/frnetlib/Sendable.h) +set(SOURCE_FILES ${SOURCE_FILES} main.cpp src/TcpSocket.cpp include/frnetlib/TcpSocket.h src/TcpListener.cpp include/frnetlib/TcpListener.h src/Socket.cpp include/frnetlib/Socket.h src/Packet.cpp include/frnetlib/Packet.h include/frnetlib/NetworkEncoding.h src/SocketSelector.cpp include/frnetlib/SocketSelector.h src/HttpRequest.cpp include/frnetlib/HttpRequest.h src/HttpResponse.cpp include/frnetlib/HttpResponse.h src/Http.cpp include/frnetlib/Http.h src/SocketReactor.cpp include/frnetlib/SocketReactor.h include/frnetlib/Packetable.h include/frnetlib/Listener.h src/URL.cpp include/frnetlib/URL.h include/frnetlib/Sendable.h) include_directories(include) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") diff --git a/include/frnetlib/SSLContext.h b/include/frnetlib/SSLContext.h index 643b1cb..02fbbc1 100644 --- a/include/frnetlib/SSLContext.h +++ b/include/frnetlib/SSLContext.h @@ -5,8 +5,6 @@ #ifndef FRNETLIB_SSLCONTEXT_H #define FRNETLIB_SSLCONTEXT_H -#ifdef SSL_ENABLED - #include #include #include @@ -86,7 +84,4 @@ namespace fr }; } -#endif // SSL_ENABLED - - #endif //FRNETLIB_SSLCONTEXT_H diff --git a/include/frnetlib/SSLListener.h b/include/frnetlib/SSLListener.h index 858126a..44583cd 100644 --- a/include/frnetlib/SSLListener.h +++ b/include/frnetlib/SSLListener.h @@ -5,8 +5,6 @@ #ifndef FRNETLIB_SSLLISTENER_H #define FRNETLIB_SSLLISTENER_H -#ifdef SSL_ENABLED - #include #include #include @@ -83,6 +81,4 @@ namespace fr }; } - -#endif //SLL_ENABLED #endif //FRNETLIB_SSLLISTENER_H diff --git a/include/frnetlib/SSLSocket.h b/include/frnetlib/SSLSocket.h index 7767e46..f754a48 100644 --- a/include/frnetlib/SSLSocket.h +++ b/include/frnetlib/SSLSocket.h @@ -4,8 +4,6 @@ #ifndef FRNETLIB_SSL_SOCKET_H #define FRNETLIB_SSL_SOCKET_H -#ifdef SSL_ENABLED - #include "TcpSocket.h" #include "SSLContext.h" #include @@ -64,6 +62,13 @@ namespace fr */ virtual Socket::Status connect(const std::string &address, const std::string &port, std::chrono::seconds timeout) override; + /*! + * Sets the socket file descriptor. + * + * @param descriptor The socket descriptor. + */ + virtual void set_descriptor(int descriptor) override; + /*! * Set the SSL context * @@ -71,13 +76,6 @@ namespace fr */ void set_ssl_context(std::unique_ptr context); - /*! - * Set the NET context - * - * @param context The NET context to use - */ - void set_net_context(std::unique_ptr context); - /*! * Gets the underlying socket descriptor. * @@ -85,9 +83,7 @@ namespace fr */ int32_t get_socket_descriptor() const override { - if(!ssl_socket_descriptor) - return -1; - return ssl_socket_descriptor->fd; + return ssl_socket_descriptor.fd; } /*! @@ -107,7 +103,7 @@ namespace fr */ inline bool connected() const final { - return ssl_socket_descriptor && ssl_socket_descriptor->fd > -1; + return ssl_socket_descriptor.fd > -1; } /*! @@ -123,7 +119,7 @@ namespace fr private: std::shared_ptr ssl_context; - std::unique_ptr ssl_socket_descriptor; + mbedtls_net_context ssl_socket_descriptor; std::unique_ptr ssl; mbedtls_ssl_config conf; uint32_t flags; @@ -131,6 +127,4 @@ namespace fr }; } -#endif - #endif //FRNETLIB_SSLSOCKET_H diff --git a/include/frnetlib/Socket.h b/include/frnetlib/Socket.h index 7cc2e1c..d167071 100644 --- a/include/frnetlib/Socket.h +++ b/include/frnetlib/Socket.h @@ -68,25 +68,6 @@ namespace fr */ virtual Socket::Status connect(const std::string &address, const std::string &port, std::chrono::seconds timeout)=0; - /*! - * Gets the socket's printable remote address - * - * @return The string address - */ - inline const std::string &get_remote_address() - { - return remote_address; - } - - /*! - * Sets the connections remote address. - * - * @param addr The remote address to use - */ - void set_remote_address(const std::string &addr) - { - remote_address = addr; - } /*! * Sets the socket to blocking or non-blocking. @@ -106,7 +87,6 @@ namespace fr */ virtual Status send_raw(const char *data, size_t size) = 0; - /*! * Receives raw data from the socket, without any of * frnetlib's framing. Useful for communicating through @@ -135,6 +115,33 @@ namespace fr */ virtual bool connected() const =0; + /*! + * Sets the socket file descriptor. + * + * @param descriptor The socket descriptor. + */ + virtual void set_descriptor(int descriptor)=0; + + /*! + * Gets the socket's printable remote address + * + * @return The string address + */ + inline const std::string &get_remote_address() + { + return remote_address; + } + + /*! + * Sets the connections remote address. + * + * @param addr The remote address to use + */ + void set_remote_address(const std::string &addr) + { + remote_address = addr; + } + /*! * Send a Sendable object through the socket * diff --git a/include/frnetlib/TcpSocket.h b/include/frnetlib/TcpSocket.h index 1ac038f..7fbc087 100644 --- a/include/frnetlib/TcpSocket.h +++ b/include/frnetlib/TcpSocket.h @@ -40,7 +40,7 @@ public: * * @param descriptor The socket descriptor. */ - virtual void set_descriptor(int descriptor); + virtual void set_descriptor(int descriptor) override; /*! * Attempts to send raw data down the socket, without diff --git a/src/SSLListener.cpp b/src/SSLListener.cpp index ce6001a..4e00986 100644 --- a/src/SSLListener.cpp +++ b/src/SSLListener.cpp @@ -7,9 +7,9 @@ #include "frnetlib/NetworkEncoding.h" #include "frnetlib/TcpListener.h" #include "frnetlib/SSLListener.h" -#ifdef SSL_ENABLED #include +#include namespace fr { @@ -86,16 +86,16 @@ namespace fr Socket::Status SSLListener::accept(Socket &client_) { //Cast to SSLSocket. Will throw bad cast on failure. - auto &client = dynamic_cast(client_); + SSLSocket &client = dynamic_cast(client_); //Initialise mbedtls int error = 0; std::unique_ptr ssl(new mbedtls_ssl_context); - std::unique_ptr client_fd(new mbedtls_net_context); + mbedtls_net_context client_fd; mbedtls_ssl_init(ssl.get()); - mbedtls_net_init(client_fd.get()); - auto free_contexts = [&](){mbedtls_ssl_free(ssl.get()); mbedtls_net_free(client_fd.get());}; + mbedtls_net_init(&client_fd); + auto free_contexts = [&](){mbedtls_ssl_free(ssl.get()); mbedtls_net_free(&client_fd);}; if((error = mbedtls_ssl_setup(ssl.get(), &conf ) ) != 0) { std::cout << "Failed to apply SSL setings: " << error << std::endl; @@ -106,14 +106,14 @@ namespace fr //Accept a connection char client_ip[INET6_ADDRSTRLEN] = {0}; size_t ip_len = 0; - if((error = mbedtls_net_accept(&listen_fd, client_fd.get(), client_ip, sizeof(client_ip), &ip_len)) != 0) + if((error = mbedtls_net_accept(&listen_fd, &client_fd, client_ip, sizeof(client_ip), &ip_len)) != 0) { std::cout << "Accept error: " << error << std::endl; free_contexts(); return Socket::Error; } - mbedtls_ssl_set_bio(ssl.get(), client_fd.get(), mbedtls_net_send, mbedtls_net_recv, nullptr); + mbedtls_ssl_set_bio(ssl.get(), &client_fd, mbedtls_net_send, mbedtls_net_recv, nullptr); //SSL Handshake while((error = mbedtls_ssl_handshake(ssl.get())) != 0) @@ -132,14 +132,14 @@ namespace fr char client_printable_addr[INET6_ADDRSTRLEN]; struct sockaddr_storage socket_address{}; socklen_t socket_length; - error = getpeername(client_fd->fd, (struct sockaddr*)&socket_address, &socket_length); + error = getpeername(client_fd.fd, (struct sockaddr*)&socket_address, &socket_length); if(error == 0) error = getnameinfo((sockaddr*)&socket_address, socket_length, client_printable_addr, sizeof(client_printable_addr), nullptr,0,NI_NUMERICHOST); if(error != 0) strcpy(client_printable_addr, "unknown"); client.set_ssl_context(std::move(ssl)); - client.set_net_context(std::move(client_fd)); + client.set_descriptor(client_fd.fd); client.set_remote_address(client_printable_addr); return Socket::Success; } @@ -168,5 +168,4 @@ namespace fr } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/SSLSocket.cpp b/src/SSLSocket.cpp index 14a306f..daac22c 100644 --- a/src/SSLSocket.cpp +++ b/src/SSLSocket.cpp @@ -6,8 +6,6 @@ #include #include -#ifdef SSL_ENABLED - #include namespace fr @@ -18,7 +16,7 @@ namespace fr { //Initialise mbedtls structures mbedtls_ssl_config_init(&conf); - + ssl_socket_descriptor.fd = -1; } SSLSocket::~SSLSocket() noexcept @@ -26,20 +24,20 @@ namespace fr //Close connection if active close_socket(); - //Cleanup mbedsql stuff + //Cleanup mbedtls stuff mbedtls_ssl_config_free(&conf); } void SSLSocket::close_socket() { - if(ssl_socket_descriptor) - mbedtls_net_free(ssl_socket_descriptor.get()); if(ssl) { mbedtls_ssl_close_notify(ssl.get()); mbedtls_ssl_free(ssl.get()); } - + if(ssl_socket_descriptor.fd > -1) + mbedtls_net_free(&ssl_socket_descriptor); + ssl_socket_descriptor.fd = -1; } Socket::Status SSLSocket::send_raw(const char *data, size_t size) @@ -88,9 +86,8 @@ namespace fr { //Initialise mbedtls stuff ssl = std::make_unique(); - ssl_socket_descriptor = std::make_unique(); mbedtls_ssl_init(ssl.get()); - mbedtls_net_init(ssl_socket_descriptor.get()); + mbedtls_net_init(&ssl_socket_descriptor); //Do to mbedtls not supporting connect timeouts, we have to use an fr::TcpSocket to //Open the descriptor, and then steal it. This is a hack. @@ -99,7 +96,7 @@ namespace fr auto ret = socket.connect(address, port, timeout); if(ret != fr::Socket::Success) return ret; - ssl_socket_descriptor->fd = socket.get_socket_descriptor(); + ssl_socket_descriptor.fd = socket.get_socket_descriptor(); remote_address = socket.get_remote_address(); socket.set_descriptor(-1); } @@ -125,7 +122,7 @@ namespace fr return Socket::Status::Error; } - mbedtls_ssl_set_bio(ssl.get(), ssl_socket_descriptor.get(), mbedtls_net_send, mbedtls_net_recv, nullptr); + mbedtls_ssl_set_bio(ssl.get(), &ssl_socket_descriptor, mbedtls_net_send, mbedtls_net_recv, nullptr); //Do SSL handshake while((error = mbedtls_ssl_handshake(ssl.get())) != 0) @@ -158,9 +155,9 @@ namespace fr ssl = std::move(context); } - void SSLSocket::set_net_context(std::unique_ptr context) + void SSLSocket::set_descriptor(int descriptor) { - ssl_socket_descriptor = std::move(context); + ssl_socket_descriptor.fd = descriptor; reconfigure_socket(); } @@ -169,5 +166,3 @@ namespace fr should_verify = should_verify_; } } - -#endif \ No newline at end of file