Skip to content

Commit

Permalink
Update to version 3.1
Browse files Browse the repository at this point in the history
- new features:
    - A custom text can be specified to be appended automatically to each sent message in fragmented mode. For both, server and client
    - Sending "DEBUGINFO" to an out stream writes debugging information automatically
  • Loading branch information
nilshenrich committed Nov 4, 2024
2 parents f458f90 + edad9a9 commit cfc5b3e
Show file tree
Hide file tree
Showing 64 changed files with 620 additions and 289 deletions.
22 changes: 8 additions & 14 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
# For debugging please use the command "cmake -DCMAKE_BUILD_TYPE=Debug .."
# For debugging please use the command "cmake -DCMAKE_BUILD_TYPE=DEBUG .."

cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(example VERSION 3.0.0 DESCRIPTION "Example of using the TCP client and server classes")
project(example VERSION 3.1.0 DESCRIPTION "Example of using the TCP client and server classes")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../build/include)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../build/lib)
add_executable(server server.cpp)
add_executable(client client.cpp)

file(GLOB sourcefiles_server server.cpp)
file(GLOB sourcefiles_client client.cpp)
add_executable(server ${sourcefiles_server})
add_executable(client ${sourcefiles_client})

target_compile_options(server PRIVATE -fexceptions -Wall $<$<NOT:$<CONFIG:Debug>>: -O3> $<$<CONFIG:DEBUG>: -g -Og -DDEVELOP>)
target_compile_options(client PRIVATE -fexceptions -Wall $<$<NOT:$<CONFIG:Debug>>: -O3> $<$<CONFIG:DEBUG>: -g -Og -DDEVELOP>)
target_compile_options(server PRIVATE -fexceptions -Wall $<$<NOT:$<CONFIG:DEBUG>>: -O3> $<$<CONFIG:DEBUG>: -g -Og -DDEVELOP>)
target_compile_options(client PRIVATE -fexceptions -Wall $<$<NOT:$<CONFIG:DEBUG>>: -O3> $<$<CONFIG:DEBUG>: -g -Og -DDEVELOP>)

target_link_libraries(server -lcrypto -lcrypt -lssl -pthread)
target_link_libraries(client -lcrypto -lcrypt -lssl -pthread)

# Message build type
string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPER)
if ("${CMAKE_BUILD_TYPE_UPPER}" STREQUAL "DEBUG")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE_UPPER}")
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
message(STATUS "Build type: DEBUG")
endif()

# ---------- Commands to be executed when creating makefile ----------
Expand Down
13 changes: 7 additions & 6 deletions src/TcpClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file TcpClient.hpp
* @author Nils Henrich
* @brief TCP client for unencrypted data transfer without authentication.
* @version 3.0.0
* @version 3.1.0
* @date 2021-12-27
*
* @copyright Copyright (c) 2021
Expand All @@ -24,17 +24,18 @@ namespace tcp
/**
* @brief Constructor for continuous stream forwarding
*
* @param os Stream to forward incoming stream to
* @param os Stream to forward incoming stream to
*/
TcpClient(::std::ostream &os = ::std::cout) : Client(os) {}

/**
* @brief Constructor for fragmented messages
*
* @param delimiter Character to split messages on
* @param messageMaxLen Maximum message length
* @param delimiter Character to split messages on
* @param messageAppend String to append to the end of each fragmented message (before the delimiter)
* @param messageMaxLen Maximum message length (actual message + length of append string) (default is 2³² - 2 = 4294967294)
*/
TcpClient(char delimiter, size_t messageMaxLen = ::std::numeric_limits<size_t>::max() - 1) : Client(delimiter, messageMaxLen) {}
TcpClient(char delimiter, const ::std::string &messageAppend = "", size_t messageMaxLen = ::std::numeric_limits<size_t>::max() - 1) : Client(delimiter, messageAppend, messageMaxLen) {}

/**
* @brief Destructor
Expand Down Expand Up @@ -92,7 +93,7 @@ namespace tcp
bool writeMsg(const ::std::string &msg) override final
{
#ifdef DEVELOP
::std::cout << typeid(this).name() << "::" << __func__ << ": Send to server: " << msg << ::std::endl;
::std::cout << DEBUGINFO << ": Send to server: " << msg << ::std::endl;
#endif // DEVELOP

const size_t lenMsg{msg.size()};
Expand Down
12 changes: 6 additions & 6 deletions src/TcpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file TcpServer.hpp
* @author Nils Henrich
* @brief TCP server for unencrypted data transfer without authentication.
* @version 3.0.0
* @version 3.1.0
* @date 2021-12-27
*
* @copyright Copyright (c) 2021
Expand Down Expand Up @@ -30,9 +30,10 @@ namespace tcp
* @brief Constructor for fragmented messages
*
* @param delimiter Character to split messages on
* @param messageMaxLen Maximum message length
* @param messageAppend String to append to the end of each fragmented message (before the delimiter)
* @param messageMaxLen Maximum message length (actual message + length of append string) (default is 2³² - 2 = 4294967294)
*/
TcpServer(char delimiter, size_t messageMaxLen = ::std::numeric_limits<size_t>::max() - 1) : Server{delimiter, messageMaxLen} {}
TcpServer(char delimiter, const ::std::string &messageAppend = "", size_t messageMaxLen = ::std::numeric_limits<size_t>::max() - 1) : Server{delimiter, messageAppend, messageMaxLen} {}

/**
* @brief Destructor
Expand Down Expand Up @@ -90,13 +91,12 @@ namespace tcp
*
* @param clientId
* @param msg
* @return true
* @return false
* @return bool (true on success, false on failure)
*/
bool writeMsg(const int clientId, const ::std::string &msg) override final
{
#ifdef DEVELOP
::std::cout << typeid(this).name() << "::" << __func__ << ": Send to client " << clientId << ": " << msg << ::std::endl;
::std::cout << DEBUGINFO << ": Send to client " << clientId << ": " << msg << ::std::endl;
#endif // DEVELOP

const size_t lenMsg{msg.size()};
Expand Down
37 changes: 19 additions & 18 deletions src/TlsClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file TlsClient.hpp
* @author Nils Henrich
* @brief TLS client for encrypted data transfer with authentication.
* @version 3.0.0
* @version 3.1.0
* @date 2021-12-27
*
* @copyright Copyright (c) 2021
Expand Down Expand Up @@ -40,17 +40,18 @@ namespace tcp
/**
* @brief Constructor for continuous stream forwarding
*
* @param os Stream to forward incoming stream to
* @param os Stream to forward incoming stream to
*/
TlsClient(::std::ostream &os = ::std::cout) : Client(os) {}

/**
* @brief Constructor for fragmented messages
*
* @param delimiter Character to split messages on
* @param messageMaxLen Maximum message length
* @param delimiter Character to split messages on
* @param messageAppend String to append to the end of each fragmented message (before the delimiter)
* @param messageMaxLen Maximum message length (actual message + length of append string) (default is 2³² - 2 = 4294967294)
*/
TlsClient(char delimiter, size_t messageMaxLen = ::std::numeric_limits<size_t>::max() - 1) : Client(delimiter, messageMaxLen) {}
TlsClient(char delimiter, const ::std::string &messageAppend = "", size_t messageMaxLen = ::std::numeric_limits<size_t>::max() - 1) : Client(delimiter, messageAppend, messageMaxLen) {}

/**
* @brief Destructor
Expand Down Expand Up @@ -79,7 +80,7 @@ namespace tcp
if (!clientContext.get())
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Error when setting encryption method to latest client side TLS version" << ::std::endl;
::std::cerr << DEBUGINFO << ": Error when setting encryption method to latest client side TLS version" << ::std::endl;
#endif // DEVELOP

stop();
Expand All @@ -90,7 +91,7 @@ namespace tcp
if (access(pathToCaCert, F_OK))
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": CA certificate file does not exist" << ::std::endl;
::std::cerr << DEBUGINFO << ": CA certificate file does not exist" << ::std::endl;
#endif // DEVELOP

stop();
Expand All @@ -101,7 +102,7 @@ namespace tcp
if (access(pathToCert, F_OK))
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Client certificate file does not exist" << ::std::endl;
::std::cerr << DEBUGINFO << ": Client certificate file does not exist" << ::std::endl;
#endif // DEVELOP

stop();
Expand All @@ -112,7 +113,7 @@ namespace tcp
if (access(pathToPrivKey, F_OK))
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Client private key file does not exist" << ::std::endl;
::std::cerr << DEBUGINFO << ": Client private key file does not exist" << ::std::endl;
#endif // DEVELOP

stop();
Expand All @@ -123,7 +124,7 @@ namespace tcp
if (1 != SSL_CTX_load_verify_locations(clientContext.get(), pathToCaCert, nullptr))
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Error when loading the CA certificate the client should trust: " << pathToCaCert << ::std::endl;
::std::cerr << DEBUGINFO << ": Error when loading the CA certificate the client should trust: " << pathToCaCert << ::std::endl;
#endif // DEVELOP

stop();
Expand All @@ -134,7 +135,7 @@ namespace tcp
if (1 != SSL_CTX_use_certificate_file(clientContext.get(), pathToCert, SSL_FILETYPE_PEM))
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Error when loading the client certificate: " << pathToCert << ::std::endl;
::std::cerr << DEBUGINFO << ": Error when loading the client certificate: " << pathToCert << ::std::endl;
#endif // DEVELOP

stop();
Expand All @@ -145,7 +146,7 @@ namespace tcp
if (1 != SSL_CTX_use_PrivateKey_file(clientContext.get(), pathToPrivKey, SSL_FILETYPE_PEM))
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Error when loading the client private key: " << pathToPrivKey << ::std::endl;
::std::cerr << DEBUGINFO << ": Error when loading the client private key: " << pathToPrivKey << ::std::endl;
#endif // DEVELOP

stop();
Expand Down Expand Up @@ -180,7 +181,7 @@ namespace tcp
if (!SSL_CTX_set_ciphersuites(clientContext.get(), "TLS_AES_256_GCM_SHA384"))
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Error when setting cipher suites" << ::std::endl;
::std::cerr << DEBUGINFO << ": Error when setting cipher suites" << ::std::endl;
#endif // DEVELOP

return nullptr;
Expand All @@ -191,7 +192,7 @@ namespace tcp
if (!tlsSocket)
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Error when creating new TLS channel" << ::std::endl;
::std::cerr << DEBUGINFO << ": Error when creating new TLS channel" << ::std::endl;
#endif // DEVELOP

return nullptr;
Expand All @@ -201,7 +202,7 @@ namespace tcp
if (!SSL_set_fd(tlsSocket, tcpSocket))
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Error when binding the TLS channel to the TCP socket" << ::std::endl;
::std::cerr << DEBUGINFO << ": Error when binding the TLS channel to the TCP socket" << ::std::endl;
#endif // DEVELOP

SSL_free(tlsSocket);
Expand All @@ -213,7 +214,7 @@ namespace tcp
if (1 != SSL_connect(tlsSocket))
{
#ifdef DEVELOP
::std::cerr << typeid(this).name() << "::" << __func__ << ": Error when doing TLS handshake" << ::std::endl;
::std::cerr << DEBUGINFO << ": Error when doing TLS handshake" << ::std::endl;
#endif // DEVELOP

SSL_free(tlsSocket);
Expand All @@ -222,7 +223,7 @@ namespace tcp
}

#ifdef DEVELOP
::std::cout << typeid(this).name() << "::" << __func__ << ": Encrypted connection to server established" << ::std::endl;
::std::cout << DEBUGINFO << ": Encrypted connection to server established" << ::std::endl;
#endif // DEVELOP

return tlsSocket;
Expand Down Expand Up @@ -267,7 +268,7 @@ namespace tcp
bool writeMsg(const ::std::string &msg) override final
{
#ifdef DEVELOP
::std::cout << typeid(this).name() << "::" << __func__ << ": Send to server: " << msg << ::std::endl;
::std::cout << DEBUGINFO << ": Send to server: " << msg << ::std::endl;
#endif // DEVELOP

// Get size of message to send
Expand Down
Loading

0 comments on commit cfc5b3e

Please sign in to comment.