Skip to content

Commit cfc5b3e

Browse files
committed
Update to version 3.1
- 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
2 parents f458f90 + edad9a9 commit cfc5b3e

File tree

64 files changed

+620
-289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+620
-289
lines changed

example/CMakeLists.txt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1-
# For debugging please use the command "cmake -DCMAKE_BUILD_TYPE=Debug .."
1+
# For debugging please use the command "cmake -DCMAKE_BUILD_TYPE=DEBUG .."
22

33
cmake_minimum_required(VERSION 3.10)
44

55
set(CMAKE_CXX_STANDARD 17)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

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

10-
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../build/include)
11-
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../build/lib)
10+
add_executable(server server.cpp)
11+
add_executable(client client.cpp)
1212

13-
file(GLOB sourcefiles_server server.cpp)
14-
file(GLOB sourcefiles_client client.cpp)
15-
add_executable(server ${sourcefiles_server})
16-
add_executable(client ${sourcefiles_client})
17-
18-
target_compile_options(server PRIVATE -fexceptions -Wall $<$<NOT:$<CONFIG:Debug>>: -O3> $<$<CONFIG:DEBUG>: -g -Og -DDEVELOP>)
19-
target_compile_options(client PRIVATE -fexceptions -Wall $<$<NOT:$<CONFIG:Debug>>: -O3> $<$<CONFIG:DEBUG>: -g -Og -DDEVELOP>)
13+
target_compile_options(server PRIVATE -fexceptions -Wall $<$<NOT:$<CONFIG:DEBUG>>: -O3> $<$<CONFIG:DEBUG>: -g -Og -DDEVELOP>)
14+
target_compile_options(client PRIVATE -fexceptions -Wall $<$<NOT:$<CONFIG:DEBUG>>: -O3> $<$<CONFIG:DEBUG>: -g -Og -DDEVELOP>)
2015

2116
target_link_libraries(server -lcrypto -lcrypt -lssl -pthread)
2217
target_link_libraries(client -lcrypto -lcrypt -lssl -pthread)
2318

2419
# Message build type
25-
string(TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_UPPER)
26-
if ("${CMAKE_BUILD_TYPE_UPPER}" STREQUAL "DEBUG")
27-
message(STATUS "Build type: ${CMAKE_BUILD_TYPE_UPPER}")
20+
if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
21+
message(STATUS "Build type: DEBUG")
2822
endif()
2923

3024
# ---------- Commands to be executed when creating makefile ----------

src/TcpClient.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file TcpClient.hpp
33
* @author Nils Henrich
44
* @brief TCP client for unencrypted data transfer without authentication.
5-
* @version 3.0.0
5+
* @version 3.1.0
66
* @date 2021-12-27
77
*
88
* @copyright Copyright (c) 2021
@@ -24,17 +24,18 @@ namespace tcp
2424
/**
2525
* @brief Constructor for continuous stream forwarding
2626
*
27-
* @param os Stream to forward incoming stream to
27+
* @param os Stream to forward incoming stream to
2828
*/
2929
TcpClient(::std::ostream &os = ::std::cout) : Client(os) {}
3030

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

3940
/**
4041
* @brief Destructor
@@ -92,7 +93,7 @@ namespace tcp
9293
bool writeMsg(const ::std::string &msg) override final
9394
{
9495
#ifdef DEVELOP
95-
::std::cout << typeid(this).name() << "::" << __func__ << ": Send to server: " << msg << ::std::endl;
96+
::std::cout << DEBUGINFO << ": Send to server: " << msg << ::std::endl;
9697
#endif // DEVELOP
9798

9899
const size_t lenMsg{msg.size()};

src/TcpServer.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file TcpServer.hpp
33
* @author Nils Henrich
44
* @brief TCP server for unencrypted data transfer without authentication.
5-
* @version 3.0.0
5+
* @version 3.1.0
66
* @date 2021-12-27
77
*
88
* @copyright Copyright (c) 2021
@@ -30,9 +30,10 @@ namespace tcp
3030
* @brief Constructor for fragmented messages
3131
*
3232
* @param delimiter Character to split messages on
33-
* @param messageMaxLen Maximum message length
33+
* @param messageAppend String to append to the end of each fragmented message (before the delimiter)
34+
* @param messageMaxLen Maximum message length (actual message + length of append string) (default is 2³² - 2 = 4294967294)
3435
*/
35-
TcpServer(char delimiter, size_t messageMaxLen = ::std::numeric_limits<size_t>::max() - 1) : Server{delimiter, messageMaxLen} {}
36+
TcpServer(char delimiter, const ::std::string &messageAppend = "", size_t messageMaxLen = ::std::numeric_limits<size_t>::max() - 1) : Server{delimiter, messageAppend, messageMaxLen} {}
3637

3738
/**
3839
* @brief Destructor
@@ -90,13 +91,12 @@ namespace tcp
9091
*
9192
* @param clientId
9293
* @param msg
93-
* @return true
94-
* @return false
94+
* @return bool (true on success, false on failure)
9595
*/
9696
bool writeMsg(const int clientId, const ::std::string &msg) override final
9797
{
9898
#ifdef DEVELOP
99-
::std::cout << typeid(this).name() << "::" << __func__ << ": Send to client " << clientId << ": " << msg << ::std::endl;
99+
::std::cout << DEBUGINFO << ": Send to client " << clientId << ": " << msg << ::std::endl;
100100
#endif // DEVELOP
101101

102102
const size_t lenMsg{msg.size()};

src/TlsClient.hpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file TlsClient.hpp
33
* @author Nils Henrich
44
* @brief TLS client for encrypted data transfer with authentication.
5-
* @version 3.0.0
5+
* @version 3.1.0
66
* @date 2021-12-27
77
*
88
* @copyright Copyright (c) 2021
@@ -40,17 +40,18 @@ namespace tcp
4040
/**
4141
* @brief Constructor for continuous stream forwarding
4242
*
43-
* @param os Stream to forward incoming stream to
43+
* @param os Stream to forward incoming stream to
4444
*/
4545
TlsClient(::std::ostream &os = ::std::cout) : Client(os) {}
4646

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

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

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

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

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

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

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

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

151152
stop();
@@ -180,7 +181,7 @@ namespace tcp
180181
if (!SSL_CTX_set_ciphersuites(clientContext.get(), "TLS_AES_256_GCM_SHA384"))
181182
{
182183
#ifdef DEVELOP
183-
::std::cerr << typeid(this).name() << "::" << __func__ << ": Error when setting cipher suites" << ::std::endl;
184+
::std::cerr << DEBUGINFO << ": Error when setting cipher suites" << ::std::endl;
184185
#endif // DEVELOP
185186

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

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

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

219220
SSL_free(tlsSocket);
@@ -222,7 +223,7 @@ namespace tcp
222223
}
223224

224225
#ifdef DEVELOP
225-
::std::cout << typeid(this).name() << "::" << __func__ << ": Encrypted connection to server established" << ::std::endl;
226+
::std::cout << DEBUGINFO << ": Encrypted connection to server established" << ::std::endl;
226227
#endif // DEVELOP
227228

228229
return tlsSocket;
@@ -267,7 +268,7 @@ namespace tcp
267268
bool writeMsg(const ::std::string &msg) override final
268269
{
269270
#ifdef DEVELOP
270-
::std::cout << typeid(this).name() << "::" << __func__ << ": Send to server: " << msg << ::std::endl;
271+
::std::cout << DEBUGINFO << ": Send to server: " << msg << ::std::endl;
271272
#endif // DEVELOP
272273

273274
// Get size of message to send

0 commit comments

Comments
 (0)