|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
SSL/TLS-encrypted TCP connection (OpenSSL) with configurable peer verification. More...
#include <NetworkConnections.h>
Public Member Functions | |
| SSLConnection () | |
| ~SSLConnection () | |
| bool | init () |
| Initialise the OpenSSL context for a client-side connection. | |
| bool | init (const char *certFile, const char *keyFile) |
| Initialise the OpenSSL context for a server-side connection with a certificate. | |
| void | setAllowSelfSigned (bool allow) |
| bool | getAllowSelfSigned () const |
| void | setVerifyHostName (const char *host) |
| const char * | getVerifyHostName () const |
| void | setCALocation (const char *caFile, const char *caPath) |
| bool | connect (SOCKET s, uint64 localAddr, NetworkDataReceiver *receiver=NULL) |
| Adopt an already-accepted socket and perform the server-side TLS handshake. | |
| bool | connect (uint64 addr, uint32 timeoutMS, NetworkDataReceiver *receiver=NULL) |
| Connect and handshake to a packed uint64 endpoint (client side). | |
| bool | connect (const char *addr, uint16 port, uint64 &location, uint32 timeoutMS, NetworkDataReceiver *receiver=NULL) |
| Connect and handshake to a host by name/IP and port (client side). | |
| bool | connect (const uint32 *addresses, uint16 addressCount, uint16 port, uint64 &location, uint32 timeoutMS, NetworkDataReceiver *receiver=NULL) |
| Try several candidate IPv4 addresses until one connects and handshakes. | |
| bool | delayedConnect (uint64 addr, uint32 timeoutMS, NetworkDataReceiver *receiver) |
| Begin a non-blocking connect (TLS handshake completes in didConnect()). | |
| bool | delayedConnect (const char *addr, uint16 port, uint64 &location, uint32 timeoutMS, NetworkDataReceiver *receiver) |
| Begin a non-blocking connect to a host name/IP (see TCPConnection::delayedConnect()). | |
| bool | send (const char *data, uint32 size, uint64 receiver=0) |
| Send bytes over the encrypted stream. | |
| bool | reconnect (uint32 timeoutMS) |
| Reconnect and re-handshake to the previous endpoint. | |
| bool | isConnected (int timeout=0) |
| Test whether the connection is currently alive. | |
| bool | didConnect (int timeout=0) |
| Check/complete an in-progress (delayed) connect on the existing socket. | |
| bool | disconnect (uint16 error=0) |
| Shut down the TLS session and close the socket. | |
| int32 | peekStream () |
| Peek how many decrypted bytes are pending inside the SSL layer. | |
| int32 | readIntoBuffer () |
| Read decrypted bytes from the SSL layer into the internal buffer. | |
| bool | receive (char *data, uint32 size, uint32 timeout, bool peek=false) |
Receive exactly size bytes into data, waiting up to timeout ms. | |
| bool | receiveAvailable (char *data, uint32 &size, uint32 maxSize, uint32 timeout, bool peek=false) |
Receive whatever bytes are available (up to maxSize). | |
| Public Member Functions inherited from cmlabs::NetworkConnection | |
| NetworkConnection () | |
| ~NetworkConnection () | |
| virtual bool | didConnect (SOCKET s, int timeout) |
| Variant of didConnect() testing an explicit socket handle. | |
| virtual bool | isRemote () |
| virtual uint64 | getRemoteAddress () |
| bool | setConnectTimeout (uint32 timeoutMS) |
| Set the timeout used by subsequent connect()/reconnect() attempts. | |
| virtual bool | discard (uint32 size) |
Drop size bytes from the front of the receive buffer (after a peek). | |
| virtual uint32 | clearBuffer () |
| Discard all currently buffered input. | |
| virtual bool | waitForDataToRead (uint32 timeout) |
| Block until data is readable (buffered or on the socket). | |
| virtual bool | waitForDataToBeWritten (uint32 timeout) |
| Block until the socket is writable. | |
| virtual uint32 | getOutputSpeed () |
| virtual uint32 | getInputSpeed () |
| virtual uint8 | getConnectionType () |
| bool | setGreetingData (const char *data, uint32 size) |
| Set greeting bytes sent automatically right after a connection is established (used e.g. | |
| Public Member Functions inherited from cmlabs::Runnable | |
| Runnable () | |
| Initialise flags: not running, allowed to continue. | |
| virtual | ~Runnable () |
| Destructor requests a stop (with the default timeout) before destruction proceeds. | |
| virtual bool | stop (uint32 timeout=200) |
| Ask the worker loop to finish and wait for it to do so. | |
Static Public Member Functions | |
| static void | SetDefaultAllowSelfSigned (bool allow) |
| static bool | GetDefaultAllowSelfSigned () |
| static void | SetDefaultCALocation (const char *caFile, const char *caPath) |
Public Attributes | |
| std::string | certinfo |
| Human-readable summary of the peer certificate (subject/issuer), filled after handshake. | |
| Public Attributes inherited from cmlabs::NetworkConnection | |
| char * | greetingData |
| Owned copy of the greeting bytes (NULL if unset). | |
| uint32 | greetingSize |
| Size of ::greetingData in bytes. | |
Protected Member Functions | |
| bool | findRemoteAddress (uint64 &addr) |
| Protected Member Functions inherited from cmlabs::NetworkConnection | |
| void | disconnectInternal (uint16 error) |
| virtual bool | resizeBuffer (uint32 len) |
| virtual bool | run () |
Protected Attributes | |
| bool | allowSelfSigned |
| std::string | verifyHostName |
| std::string | caFile |
| std::string | caPath |
| Protected Attributes inherited from cmlabs::NetworkConnection | |
| uint8 | type |
| uint64 | remoteAddress |
| uint64 | localAddress |
| uint64 | lastActivity |
| uint32 | threadID |
| SOCKET | socket |
| bool | remote |
| uint32 | bufferLen |
| uint32 | bufferContentLen |
| uint32 | bufferContentPos |
| NetworkDataReceiver * | receiver |
| utils::Mutex | mutex |
| utils::Mutex | sendMutex |
| char * | buffer |
| uint32 | connectTimeoutMS |
| uint32 | inputSpeed |
| uint32 | outputSpeed |
| uint64 | inputBytes |
| uint64 | outputBytes |
| Protected Attributes inherited from cmlabs::Runnable | |
| uint32 | threadID |
| ThreadManager slot ID of the worker thread (0 until known). | |
| bool | shouldContinue |
| Loop-continuation flag; cleared by stop(). | |
| bool | isRunning |
| Set by the worker while its loop is active. | |
Static Protected Attributes | |
| static bool | DefaultAllowSelfSigned = false |
| static std::string | DefaultCAFile |
| static std::string | DefaultCAPath |
Friends | |
| THREAD_RET THREAD_FUNCTION_CALL | SSLConnectionRun (THREAD_ARG arg) |
SSL/TLS-encrypted TCP connection (OpenSSL) with configurable peer verification.
Mirrors the TCPConnection API but wraps the socket in an OpenSSL session. The existing inline comments below document the verification policy precisely; in summary: peer certificates are verified against the OS/CA trust store by default, hostname verification (SNI + SSL_set1_host) is applied when the hostname is known, self-signed peers can be allowed per connection or process-wide, and a custom CA file/path can replace the OS trust store. Full SSL support requires building with _USE_SSL_; without it the class exists but cannot establish encrypted sessions.
Definition at line 766 of file NetworkConnections.h.
| cmlabs::SSLConnection::SSLConnection | ( | ) |
Definition at line 1728 of file NetworkConnections.cpp.
References allowSelfSigned, caFile, caPath, DefaultAllowSelfSigned, DefaultCAFile, DefaultCAPath, cmlabs::IsSSLInitialised, cmlabs::NetworkConnection::NetworkConnection(), SSLCON, and cmlabs::NetworkConnection::type.
Referenced by SSLConnectionRun.
| cmlabs::SSLConnection::~SSLConnection | ( | ) |
Definition at line 1754 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::mutex.
| bool cmlabs::SSLConnection::connect | ( | const char * | addr, |
| uint16 | port, | ||
| uint64 & | location, | ||
| uint32 | timeoutMS, | ||
| NetworkDataReceiver * | receiver = NULL ) |
Connect and handshake to a host by name/IP and port (client side).
When addr is a DNS name (not an IP literal) it is recorded for hostname verification and sent as SNI.
| addr | Host name or IPv4 string. |
| port | Remote port. |
| location | Out: resolved endpoint packed as uint64. |
| timeoutMS | Connect+handshake timeout in ms. |
| receiver | Optional push-mode data receiver. |
Definition at line 2377 of file NetworkConnections.cpp.
References connect(), GETIPADDRESSPORT, cmlabs::utils::LookupIPAddress(), cmlabs::NetworkConnection::receiver, and verifyHostName.
| bool cmlabs::SSLConnection::connect | ( | const uint32 * | addresses, |
| uint16 | addressCount, | ||
| uint16 | port, | ||
| uint64 & | location, | ||
| uint32 | timeoutMS, | ||
| NetworkDataReceiver * | receiver = NULL ) |
Try several candidate IPv4 addresses until one connects and handshakes.
| addresses | IPv4 candidates. |
| addressCount | Count. |
| port | Remote port. |
| location | Out: winning endpoint. |
| timeoutMS | Total budget in ms. |
| receiver | Optional push-mode data receiver. |
Definition at line 2392 of file NetworkConnections.cpp.
References connect(), GETIPADDRESSPORT, and cmlabs::NetworkConnection::receiver.
| bool cmlabs::SSLConnection::connect | ( | SOCKET | s, |
| uint64 | localAddr, | ||
| NetworkDataReceiver * | receiver = NULL ) |
Adopt an already-accepted socket and perform the server-side TLS handshake.
| s | Connected socket (ownership transfers). |
| localAddr | Packed local endpoint. |
| receiver | Optional push-mode data receiver. |
Definition at line 2133 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::bufferContentLen, cmlabs::NetworkConnection::bufferContentPos, cmlabs::ThreadManager::CreateThread(), cmlabs::NetworkConnection::disconnectInternal(), findRemoteAddress(), cmlabs::NetworkConnection::localAddress, LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, cmlabs::NetworkConnection::receiver, cmlabs::NetworkConnection::remote, cmlabs::NetworkConnection::remoteAddress, cmlabs::utils::SetSocketNonBlockingMode(), SOCKET, cmlabs::NetworkConnection::socket, SSLConnectionRun, cmlabs::NetworkConnection::threadID, and cmlabs::utils::WaitForSocketReadability().
Referenced by cmlabs::TCPListener::acceptConnection(), connect(), connect(), connect(), cmlabs::NetworkChannel::createTCPConnection(), cmlabs::NetworkChannel::createTCPConnection(), cmlabs::NetworkChannel::createTCPConnection(), cmlabs::NetworkChannel::createWebsocketConnection(), cmlabs::LLMConnection::interactStream(), and reconnect().
| bool cmlabs::SSLConnection::connect | ( | uint64 | addr, |
| uint32 | timeoutMS, | ||
| NetworkDataReceiver * | receiver = NULL ) |
Connect and handshake to a packed uint64 endpoint (client side).
| addr | Packed endpoint. |
| timeoutMS | Connect+handshake timeout in ms. |
| receiver | Optional push-mode data receiver. |
Definition at line 2212 of file NetworkConnections.cpp.
References allowSelfSigned, cmlabs::NetworkConnection::bufferContentLen, cmlabs::NetworkConnection::bufferContentPos, certinfo, connect(), cmlabs::NetworkConnection::connectTimeoutMS, cmlabs::ThreadManager::CreateThread(), didConnect(), cmlabs::NetworkConnection::disconnectInternal(), GETIPPORT, cmlabs::utils::GetLastOSErrorNumber(), init(), INVALID_SOCKET, LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, cmlabs::NetworkConnection::receiver, cmlabs::NetworkConnection::remote, cmlabs::NetworkConnection::remoteAddress, cmlabs::utils::SetSocketNonBlockingMode(), cmlabs::NetworkConnection::socket, SOCKETTRYAGAIN, SOCKETWOULDBLOCK, SSLConnectionRun, cmlabs::NetworkConnection::threadID, verifyHostName, cmlabs::utils::WaitForSocketReadability(), and cmlabs::utils::WaitForSocketWriteability().
| bool cmlabs::SSLConnection::delayedConnect | ( | const char * | addr, |
| uint16 | port, | ||
| uint64 & | location, | ||
| uint32 | timeoutMS, | ||
| NetworkDataReceiver * | receiver ) |
Begin a non-blocking connect to a host name/IP (see TCPConnection::delayedConnect()).
| addr | Host name or IPv4 string. |
| port | Remote port. |
| location | Out: resolved endpoint. |
| timeoutMS | Completion timeout. |
| receiver | Optional push-mode data receiver. |
Definition at line 2430 of file NetworkConnections.cpp.
References delayedConnect(), GETIPADDRESSPORT, cmlabs::utils::LookupIPAddress(), cmlabs::NetworkConnection::receiver, and verifyHostName.
| bool cmlabs::SSLConnection::delayedConnect | ( | uint64 | addr, |
| uint32 | timeoutMS, | ||
| NetworkDataReceiver * | receiver ) |
Begin a non-blocking connect (TLS handshake completes in didConnect()).
| addr | Packed endpoint. |
| timeoutMS | Completion timeout. |
| receiver | Optional push-mode data receiver. |
Definition at line 2405 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::connectTimeoutMS, cmlabs::ThreadManager::CreateThread(), LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, cmlabs::NetworkConnection::receiver, cmlabs::NetworkConnection::remote, cmlabs::NetworkConnection::remoteAddress, SSLConnectionRun, and cmlabs::NetworkConnection::threadID.
Referenced by cmlabs::NetworkChannel::addTCPConnection(), cmlabs::NetworkChannel::addTCPConnection(), and delayedConnect().
|
virtual |
Check/complete an in-progress (delayed) connect on the existing socket.
| timeout | Milliseconds to wait for the connect to complete (0 = poll). |
Reimplemented from cmlabs::NetworkConnection.
Definition at line 1948 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::didConnect().
Referenced by connect().
|
virtual |
Shut down the TLS session and close the socket.
| error | Optional NETWORKERROR_* reason. |
Reimplemented from cmlabs::NetworkConnection.
Definition at line 2115 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::disconnectInternal(), and cmlabs::NetworkConnection::mutex.
Referenced by isConnected(), receive(), receiveAvailable(), reconnect(), and send().
|
protected |
Definition at line 2526 of file NetworkConnections.cpp.
References GETIPADDRESSPORT, cmlabs::utils::GetLocalIPAddress(), INVALID_SOCKET, LOCALHOSTIP, and cmlabs::NetworkConnection::socket.
Referenced by connect().
|
inline |
Definition at line 787 of file NetworkConnections.h.
References allowSelfSigned.
|
static |
Definition at line 1776 of file NetworkConnections.cpp.
References DefaultAllowSelfSigned.
|
inline |
Definition at line 798 of file NetworkConnections.h.
References verifyHostName.
| bool cmlabs::SSLConnection::init | ( | ) |
Initialise the OpenSSL context for a client-side connection.
Definition at line 1921 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::mutex.
Referenced by cmlabs::TCPListener::acceptConnection(), cmlabs::NetworkChannel::addTCPConnection(), cmlabs::NetworkChannel::addTCPConnection(), connect(), and cmlabs::NetworkChannel::createTCPConnection().
| bool cmlabs::SSLConnection::init | ( | const char * | certFile, |
| const char * | keyFile ) |
Initialise the OpenSSL context for a server-side connection with a certificate.
| certFile | Path to PEM certificate. |
| keyFile | Path to PEM private key. |
Definition at line 1875 of file NetworkConnections.cpp.
References LOG_NETWORK, LogPrint, and cmlabs::NetworkConnection::mutex.
|
virtual |
Test whether the connection is currently alive.
| timeout | Milliseconds to allow for probing the socket (0 = immediate). |
Reimplemented from cmlabs::NetworkConnection.
Definition at line 1952 of file NetworkConnections.cpp.
References disconnect(), cmlabs::utils::GetLastOSErrorNumber(), INVALID_SOCKET, cmlabs::NetworkConnection::mutex, cmlabs::NetworkConnection::socket, SOCKETTRYAGAIN, SOCKETWOULDBLOCK, cmlabs::NetworkConnection::type, and UDPCON.
| int32 cmlabs::SSLConnection::peekStream | ( | ) |
Peek how many decrypted bytes are pending inside the SSL layer.
Definition at line 2561 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::mutex.
|
virtual |
Read decrypted bytes from the SSL layer into the internal buffer.
Reimplemented from cmlabs::NetworkConnection.
Definition at line 2578 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::buffer, cmlabs::NetworkConnection::bufferContentLen, cmlabs::NetworkConnection::bufferContentPos, cmlabs::NetworkConnection::bufferLen, cmlabs::NetworkConnection::disconnectInternal(), INITIALBUFFERSIZE, LOG_NETWORK, LogPrint, NETWORKERROR_RECEIVE, and cmlabs::NetworkConnection::resizeBuffer().
|
virtual |
Receive exactly size bytes into data, waiting up to timeout ms.
| data | Destination buffer of at least size bytes. |
| size | Exact number of bytes required. |
| timeout | Maximum wait in milliseconds. |
| peek | When true the bytes remain in the internal buffer (subsequent reads see them again). |
size bytes were delivered before the timeout. Reimplemented from cmlabs::NetworkConnection.
Definition at line 2711 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::buffer, cmlabs::NetworkConnection::bufferContentLen, cmlabs::NetworkConnection::bufferContentPos, cmlabs::NetworkConnection::bufferLen, disconnect(), cmlabs::GetTimeAge(), cmlabs::GetTimeAgeMS(), cmlabs::GetTimeNow(), cmlabs::NetworkConnection::inputBytes, cmlabs::NetworkConnection::inputSpeed, LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, NETWORKERROR_RECEIVE, cmlabs::NetworkConnection::resizeBuffer(), cmlabs::NetworkConnection::socket, and cmlabs::utils::WaitForSocketReadability().
|
virtual |
Receive whatever bytes are available (up to maxSize).
| data | Destination buffer. |
| size | Out: number of bytes actually delivered. |
| maxSize | Capacity of data. |
| timeout | Maximum wait in milliseconds for at least one byte. |
| peek | When true the bytes remain buffered. |
Reimplemented from cmlabs::NetworkConnection.
Definition at line 2629 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::buffer, cmlabs::NetworkConnection::bufferContentLen, cmlabs::NetworkConnection::bufferContentPos, cmlabs::NetworkConnection::bufferLen, disconnect(), cmlabs::GetTimeNow(), cmlabs::NetworkConnection::inputBytes, LOG_NETWORK, LogPrint, cmlabs::NetworkConnection::mutex, NETWORKERROR_RECEIVE, cmlabs::NetworkConnection::resizeBuffer(), cmlabs::NetworkConnection::socket, and cmlabs::utils::WaitForSocketReadability().
|
virtual |
Reconnect and re-handshake to the previous endpoint.
| timeoutMS | Timeout in ms. |
Implements cmlabs::NetworkConnection.
Definition at line 2516 of file NetworkConnections.cpp.
References connect(), disconnect(), cmlabs::NetworkConnection::receiver, and cmlabs::NetworkConnection::remoteAddress.
|
virtual |
Send bytes over the encrypted stream.
| data | Bytes to send. |
| size | Byte count. |
| receiver | Ignored. |
Implements cmlabs::NetworkConnection.
Definition at line 2446 of file NetworkConnections.cpp.
References disconnect(), cmlabs::GetTimeAge(), cmlabs::GetTimeNow(), LOG_NETWORK, LogPrint, NETWORKERROR_RECEIVE, NETWORKERROR_SEND_TIMEOUT, cmlabs::NetworkConnection::outputBytes, cmlabs::NetworkConnection::outputSpeed, cmlabs::NetworkConnection::receiver, cmlabs::NetworkConnection::sendMutex, cmlabs::NetworkConnection::socket, cmlabs::utils::WaitForSocketReadability(), and cmlabs::utils::WaitForSocketWriteability().
| void cmlabs::SSLConnection::setAllowSelfSigned | ( | bool | allow | ) |
Definition at line 1803 of file NetworkConnections.cpp.
References allowSelfSigned.
Referenced by cmlabs::NetworkChannel::applySSLClientPolicy().
| void cmlabs::SSLConnection::setCALocation | ( | const char * | caFile, |
| const char * | caPath ) |
Definition at line 1790 of file NetworkConnections.cpp.
References caFile, and caPath.
Referenced by cmlabs::NetworkChannel::applySSLClientPolicy().
|
static |
Definition at line 1772 of file NetworkConnections.cpp.
References DefaultAllowSelfSigned.
|
static |
Definition at line 1785 of file NetworkConnections.cpp.
References caFile, caPath, DefaultCAFile, and DefaultCAPath.
| void cmlabs::SSLConnection::setVerifyHostName | ( | const char * | host | ) |
Definition at line 1799 of file NetworkConnections.cpp.
References verifyHostName.
|
friend |
Definition at line 2828 of file NetworkConnections.cpp.
References cmlabs::NetworkConnection::run(), SSLConnection(), THREAD_ARG, THREAD_FUNCTION_CALL, THREAD_RET, and thread_ret_val.
Referenced by connect(), connect(), and delayedConnect().
|
protected |
Definition at line 871 of file NetworkConnections.h.
Referenced by connect(), getAllowSelfSigned(), setAllowSelfSigned(), and SSLConnection().
|
protected |
Definition at line 874 of file NetworkConnections.h.
Referenced by setCALocation(), SetDefaultCALocation(), and SSLConnection().
|
protected |
Definition at line 875 of file NetworkConnections.h.
Referenced by setCALocation(), SetDefaultCALocation(), and SSLConnection().
| std::string cmlabs::SSLConnection::certinfo |
Human-readable summary of the peer certificate (subject/issuer), filled after handshake.
Definition at line 867 of file NetworkConnections.h.
Referenced by connect().
|
staticprotected |
Definition at line 872 of file NetworkConnections.h.
Referenced by GetDefaultAllowSelfSigned(), SetDefaultAllowSelfSigned(), and SSLConnection().
|
staticprotected |
Definition at line 876 of file NetworkConnections.h.
Referenced by SetDefaultCALocation(), and SSLConnection().
|
staticprotected |
Definition at line 877 of file NetworkConnections.h.
Referenced by SetDefaultCALocation(), and SSLConnection().
|
protected |
Definition at line 873 of file NetworkConnections.h.
Referenced by connect(), connect(), delayedConnect(), getVerifyHostName(), and setVerifyHostName().