15 #include <event2/event.h>
16 #include <event2/bufferevent.h>
17 #include "../mbedTLS++/SslContext.h"
47 evutil_socket_t a_Socket,
50 const sockaddr * a_Address,
69 virtual bool Send(
const void * a_Data,
size_t a_Length)
override;
74 virtual void Shutdown(
void)
override;
75 virtual void Close(
void)
override;
90 class cLinkTlsContext;
130 void Send(
const void * a_Data,
size_t a_Length);
133 virtual int ReceiveEncrypted(
unsigned char * a_Buffer,
size_t a_NumBytes)
override;
134 virtual int SendEncrypted(
const unsigned char * a_Buffer,
size_t a_NumBytes)
override;
139 return (a_Link == &
m_Link);
192 static void ReadCallback(bufferevent * a_BufferEvent,
void * a_Self);
195 static void WriteCallback(bufferevent * a_BufferEvent,
void * a_Self);
198 static void EventCallback(bufferevent * a_BufferEvent,
short a_What,
void * a_Self);
214 bool SendRaw(
const void * a_Data,
size_t a_Length);
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
std::shared_ptr< cX509Cert > cX509CertPtr
std::shared_ptr< cTCPLinkImpl > cTCPLinkImplPtr
std::shared_ptr< cServerHandleImpl > cServerHandleImplPtr
std::shared_ptr< cTCPLinkImpl > cTCPLinkImplPtr
std::vector< cTCPLinkImplPtr > cTCPLinkImplPtrs
std::shared_ptr< cServerHandleImpl > cServerHandleImplPtr
Interface that provides the methods available on a single TCP connection.
cTCPLink(cCallbacksPtr a_Callbacks)
Creates a new link, with the specified callbacks.
std::shared_ptr< cCallbacks > cCallbacksPtr
std::shared_ptr< cConnectCallbacks > cConnectCallbacksPtr
void ReceivedCleartextData(const char *a_Data, size_t a_Length)
Called by the TLS when it has decoded a piece of incoming cleartext data from the socket.
static void WriteCallback(bufferevent *a_BufferEvent, void *a_Self)
Callback that LibEvent calls when the remote peer can receive more data.
bool m_ShouldShutdown
If true, Shutdown() has been called and is in queue.
virtual UInt16 GetRemotePort(void) const override
Returns the port used by the remote endpoint of the connection.
virtual ~cTCPLinkImpl() override
Destroys the LibEvent handle representing the link.
cLinkTlsContextPtr m_TlsContext
The SSL context used for encryption, if this link uses SSL.
bufferevent * m_BufferEvent
The LibEvent handle representing this connection.
AString m_LocalIP
The IP address of the local endpoint.
static void EventCallback(bufferevent *a_BufferEvent, short a_What, void *a_Self)
Callback that LibEvent calls when there's a non-data-related event on the socket.
void UpdateLocalAddress(void)
Updates m_LocalIP and m_LocalPort based on the metadata read from the socket.
UInt16 m_LocalPort
The port of the local endpoint.
static cTCPLinkImplPtr Connect(const AString &a_Host, UInt16 a_Port, cTCPLink::cCallbacksPtr a_LinkCallbacks, cNetwork::cConnectCallbacksPtr a_ConnectCallbacks)
Queues a connection request to the specified host.
virtual bool Send(const void *a_Data, size_t a_Length) override
Queues the specified data for sending to the remote peer.
virtual AString GetRemoteIP(void) const override
Returns the IP address of the remote endpoint of the connection.
static void ReadCallback(bufferevent *a_BufferEvent, void *a_Self)
Callback that LibEvent calls when there's data available from the remote peer.
bool SendRaw(const void *a_Data, size_t a_Length)
Sends the data directly to the socket (without the optional TLS).
cNetwork::cConnectCallbacksPtr m_ConnectCallbacks
Callbacks to call when the connection is established.
cTCPLinkImpl(evutil_socket_t a_Socket, cCallbacksPtr a_LinkCallbacks, cServerHandleImplPtr a_Server, const sockaddr *a_Address, socklen_t a_AddrLen)
Creates a new link based on the given socket.
void UpdateRemoteAddress(void)
Updates m_RemoteIP and m_RemotePort based on the metadata read from the socket.
virtual AString GetLocalIP(void) const override
Returns the IP address of the local endpoint of the connection.
virtual void Shutdown(void) override
Closes the link gracefully.
virtual void Close(void) override
Drops the connection without any more processing.
void Enable(cTCPLinkImplPtr a_Self)
Enables communication over the link.
UInt16 m_RemotePort
The port of the remote endpoint.
std::shared_ptr< cLinkTlsContext > cLinkTlsContextPtr
AString m_RemoteHost
The original host parameter which was used for creating the link, either hostname or IP address.
static void UpdateAddress(const sockaddr *a_Address, socklen_t a_AddrLen, AString &a_IP, UInt16 &a_Port)
Sets a_IP and a_Port to values read from a_Address, based on the correct address family.
cServerHandleImplPtr m_Server
The server handle that has created this link.
virtual AString StartTLSServer(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey, const AString &a_StartTLSData) override
Starts a TLS handshake as a server connection.
virtual AString StartTLSClient(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey, cX509CertPtr a_TrustedRootCAs) override
Starts a TLS handshake as a client connection.
std::weak_ptr< cLinkTlsContext > cLinkTlsContextWPtr
virtual UInt16 GetLocalPort(void) const override
Returns the port used by the local endpoint of the connection.
AString m_RemoteIP
The IP address of the remote endpoint.
cTCPLinkImplPtr m_Self
SharedPtr to self, used to keep this object alive as long as the callbacks are coming.
void DoActualShutdown(void)
Calls shutdown on the link and disables LibEvent writing.
Wrapper around cSslContext that is used when this link is being encrypted by SSL.
cLinkTlsContext(cTCPLinkImpl &a_Link)
void SetSelf(cLinkTlsContextWPtr a_Self)
Shares ownership of self, so that this object can keep itself alive for as long as it needs.
void FlushBuffers(void)
Tries to read any cleartext data available through the SSL, reports it in the link.
void StoreReceivedData(const char *a_Data, size_t a_NumBytes)
Stores the specified block of data into the buffer of the data to be decrypted (incoming from remote)...
AString m_EncryptedData
Buffer for storing the incoming encrypted data until it is requested by the SSL decryptor.
void Send(const void *a_Data, size_t a_Length)
Sends the specified cleartext data over the SSL to the remote peer.
cLinkTlsContextWPtr m_Self
Shared ownership of self, so that this object can keep itself alive for as long as it needs.
bool IsLink(cTCPLinkImpl *a_Link)
Returns true if the context's associated TCP link is the same link as a_Link.
virtual int ReceiveEncrypted(unsigned char *a_Buffer, size_t a_NumBytes) override
AString m_CleartextData
Buffer for storing the outgoing cleartext data until the link has finished handshaking.
virtual int SendEncrypted(const unsigned char *a_Buffer, size_t a_NumBytes) override
void TryFinishHandshaking(void)
Tries to finish handshaking the SSL.
void ResetSelf(void)
Removes the self ownership so that we can detect the SSL closure.