Cuberite
A lightweight, fast and extensible game server for Minecraft
Classes | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Types | List of all members
cTCPLinkImpl Class Reference

#include <TCPLinkImpl.h>

Inheritance diagram for cTCPLinkImpl:
Inheritance graph
[legend]
Collaboration diagram for cTCPLinkImpl:
Collaboration graph
[legend]

Classes

class  cLinkTlsContext
 Wrapper around cSslContext that is used when this link is being encrypted by SSL. More...
 

Public Member Functions

virtual void Close (void) override
 Drops the connection without any more processing. More...
 
 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. More...
 
void Enable (cTCPLinkImplPtr a_Self)
 Enables communication over the link. More...
 
virtual AString GetLocalIP (void) const override
 Returns the IP address of the local endpoint of the connection. More...
 
virtual UInt16 GetLocalPort (void) const override
 Returns the port used by the local endpoint of the connection. More...
 
virtual AString GetRemoteIP (void) const override
 Returns the IP address of the remote endpoint of the connection. More...
 
virtual UInt16 GetRemotePort (void) const override
 Returns the port used by the remote endpoint of the connection. More...
 
virtual bool Send (const void *a_Data, size_t a_Length) override
 Queues the specified data for sending to the remote peer. More...
 
virtual void Shutdown (void) override
 Closes the link gracefully. More...
 
virtual AString StartTLSClient (cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey, cX509CertPtr a_TrustedRootCAs) override
 Starts a TLS handshake as a client connection. More...
 
virtual AString StartTLSServer (cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey, const AString &a_StartTLSData) override
 Starts a TLS handshake as a server connection. More...
 
virtual ~cTCPLinkImpl () override
 Destroys the LibEvent handle representing the link. More...
 
- Public Member Functions inherited from cTCPLink
cCallbacksPtr GetCallbacks (void) const
 Returns the callbacks that are used. More...
 
bool Send (const AString &a_Data)
 Queues the specified data for sending to the remote peer. More...
 
virtual ~cTCPLink ()
 

Static Public Member Functions

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. More...
 

Protected Types

typedef std::shared_ptr< cLinkTlsContextcLinkTlsContextPtr
 
typedef std::weak_ptr< cLinkTlsContextcLinkTlsContextWPtr
 

Protected Member Functions

 cTCPLinkImpl (const std::string &a_Host, const cCallbacksPtr a_LinkCallbacks)
 Creates a new link to be queued to connect to a specified host:port. More...
 
void DoActualShutdown (void)
 Calls shutdown on the link and disables LibEvent writing. More...
 
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. More...
 
bool SendRaw (const void *a_Data, size_t a_Length)
 Sends the data directly to the socket (without the optional TLS). More...
 
void UpdateLocalAddress (void)
 Updates m_LocalIP and m_LocalPort based on the metadata read from the socket. More...
 
void UpdateRemoteAddress (void)
 Updates m_RemoteIP and m_RemotePort based on the metadata read from the socket. More...
 
- Protected Member Functions inherited from cTCPLink
 cTCPLink (cCallbacksPtr a_Callbacks)
 Creates a new link, with the specified callbacks. More...
 

Static Protected Member Functions

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. More...
 
static void ReadCallback (bufferevent *a_BufferEvent, void *a_Self)
 Callback that LibEvent calls when there's data available from the remote peer. More...
 
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. More...
 
static void WriteCallback (bufferevent *a_BufferEvent, void *a_Self)
 Callback that LibEvent calls when the remote peer can receive more data. More...
 

Protected Attributes

bufferevent * m_BufferEvent
 The LibEvent handle representing this connection. More...
 
cNetwork::cConnectCallbacksPtr m_ConnectCallbacks
 Callbacks to call when the connection is established. More...
 
AString m_LocalIP
 The IP address of the local endpoint. More...
 
UInt16 m_LocalPort
 The port of the local endpoint. More...
 
AString m_RemoteHost
 The original host parameter which was used for creating the link, either hostname or IP address. More...
 
AString m_RemoteIP
 The IP address of the remote endpoint. More...
 
UInt16 m_RemotePort
 The port of the remote endpoint. More...
 
cTCPLinkImplPtr m_Self
 SharedPtr to self, used to keep this object alive as long as the callbacks are coming. More...
 
cServerHandleImplPtr m_Server
 The server handle that has created this link. More...
 
bool m_ShouldShutdown
 If true, Shutdown() has been called and is in queue. More...
 
cLinkTlsContextPtr m_TlsContext
 The SSL context used for encryption, if this link uses SSL. More...
 
- Protected Attributes inherited from cTCPLink
cCallbacksPtr m_Callbacks
 Callbacks to be used for the various situations. More...
 

Private Types

using Super = cTCPLink
 

Additional Inherited Members

- Public Types inherited from cTCPLink
typedef std::shared_ptr< cCallbackscCallbacksPtr
 

Detailed Description

Definition at line 34 of file TCPLinkImpl.h.

Member Typedef Documentation

◆ cLinkTlsContextPtr

typedef std::shared_ptr<cLinkTlsContext> cTCPLinkImpl::cLinkTlsContextPtr
protected

Definition at line 91 of file TCPLinkImpl.h.

◆ cLinkTlsContextWPtr

typedef std::weak_ptr<cLinkTlsContext> cTCPLinkImpl::cLinkTlsContextWPtr
protected

Definition at line 92 of file TCPLinkImpl.h.

◆ Super

using cTCPLinkImpl::Super = cTCPLink
private

Definition at line 37 of file TCPLinkImpl.h.

Constructor & Destructor Documentation

◆ cTCPLinkImpl() [1/2]

cTCPLinkImpl::cTCPLinkImpl ( evutil_socket_t  a_Socket,
cTCPLink::cCallbacksPtr  a_LinkCallbacks,
cServerHandleImplPtr  a_Server,
const sockaddr *  a_Address,
socklen_t  a_AddrLen 
)

Creates a new link based on the given socket.

Used for connections accepted in a server using cNetwork::Listen(). a_Host is the hostname used for TLS SNI (can be empty in cases TLS is not used). a_Address and a_AddrLen describe the remote peer that has connected. The link is created disabled, you need to call Enable() to start the regular communication.

Definition at line 34 of file TCPLinkImpl.cpp.

◆ ~cTCPLinkImpl()

cTCPLinkImpl::~cTCPLinkImpl ( )
overridevirtual

Destroys the LibEvent handle representing the link.

Definition at line 57 of file TCPLinkImpl.cpp.

◆ cTCPLinkImpl() [2/2]

cTCPLinkImpl::cTCPLinkImpl ( const std::string &  a_Host,
const cCallbacksPtr  a_LinkCallbacks 
)
protected

Creates a new link to be queued to connect to a specified host:port.

Used for outgoing connections created using cNetwork::Connect(). To be used only by the Connect() factory function. The link is created disabled, you need to call Enable() to start the regular communication.

Definition at line 20 of file TCPLinkImpl.cpp.

Member Function Documentation

◆ Close()

void cTCPLinkImpl::Close ( void  )
overridevirtual

Drops the connection without any more processing.

Sends the RST packet, queued outgoing and incoming data is lost.

Implements cTCPLink.

Definition at line 218 of file TCPLinkImpl.cpp.

◆ Connect()

cTCPLinkImplPtr cTCPLinkImpl::Connect ( const AString a_Host,
UInt16  a_Port,
cTCPLink::cCallbacksPtr  a_LinkCallbacks,
cNetwork::cConnectCallbacksPtr  a_ConnectCallbacks 
)
static

Queues a connection request to the specified host.

a_ConnectCallbacks must be valid. Returns a link that has the connection request queued, or NULL for failure.

Definition at line 69 of file TCPLinkImpl.cpp.

◆ DoActualShutdown()

void cTCPLinkImpl::DoActualShutdown ( void  )
protected

Calls shutdown on the link and disables LibEvent writing.

Called after all data from LibEvent buffers is sent to the OS TCP stack and shutdown() has been called before.

Definition at line 530 of file TCPLinkImpl.cpp.

◆ Enable()

void cTCPLinkImpl::Enable ( cTCPLinkImplPtr  a_Self)

Enables communication over the link.

Links are created with communication disabled, so that creation callbacks can be called first. This function then enables the regular communication to be reported. The a_Self parameter is used so that the socket can keep itself alive as long as the callbacks are coming.

Definition at line 156 of file TCPLinkImpl.cpp.

◆ EventCallback()

void cTCPLinkImpl::EventCallback ( bufferevent *  a_BufferEvent,
short  a_What,
void *  a_Self 
)
staticprotected

Callback that LibEvent calls when there's a non-data-related event on the socket.

Definition at line 385 of file TCPLinkImpl.cpp.

◆ GetLocalIP()

virtual AString cTCPLinkImpl::GetLocalIP ( void  ) const
inlineoverridevirtual

Returns the IP address of the local endpoint of the connection.

Implements cTCPLink.

Definition at line 70 of file TCPLinkImpl.h.

◆ GetLocalPort()

virtual UInt16 cTCPLinkImpl::GetLocalPort ( void  ) const
inlineoverridevirtual

Returns the port used by the local endpoint of the connection.

Implements cTCPLink.

Definition at line 71 of file TCPLinkImpl.h.

◆ GetRemoteIP()

virtual AString cTCPLinkImpl::GetRemoteIP ( void  ) const
inlineoverridevirtual

Returns the IP address of the remote endpoint of the connection.

Implements cTCPLink.

Definition at line 72 of file TCPLinkImpl.h.

◆ GetRemotePort()

virtual UInt16 cTCPLinkImpl::GetRemotePort ( void  ) const
inlineoverridevirtual

Returns the port used by the remote endpoint of the connection.

Implements cTCPLink.

Definition at line 73 of file TCPLinkImpl.h.

◆ ReadCallback()

void cTCPLinkImpl::ReadCallback ( bufferevent *  a_BufferEvent,
void *  a_Self 
)
staticprotected

Callback that LibEvent calls when there's data available from the remote peer.

Definition at line 338 of file TCPLinkImpl.cpp.

◆ ReceivedCleartextData()

void cTCPLinkImpl::ReceivedCleartextData ( const char *  a_Data,
size_t  a_Length 
)
protected

Called by the TLS when it has decoded a piece of incoming cleartext data from the socket.

Definition at line 553 of file TCPLinkImpl.cpp.

◆ Send()

bool cTCPLinkImpl::Send ( const void *  a_Data,
size_t  a_Length 
)
overridevirtual

Queues the specified data for sending to the remote peer.

Returns true on success, false on failure. Note that this success or failure only reports the queue status, not the actual data delivery.

Implements cTCPLink.

Definition at line 170 of file TCPLinkImpl.cpp.

◆ SendRaw()

bool cTCPLinkImpl::SendRaw ( const void *  a_Data,
size_t  a_Length 
)
protected

Sends the data directly to the socket (without the optional TLS).

Definition at line 544 of file TCPLinkImpl.cpp.

◆ Shutdown()

void cTCPLinkImpl::Shutdown ( void  )
overridevirtual

Closes the link gracefully.

The link will send any queued outgoing data, then it will send the FIN packet. The link will still receive incoming data from remote until the remote closes the connection.

Implements cTCPLink.

Definition at line 193 of file TCPLinkImpl.cpp.

◆ StartTLSClient()

AString cTCPLinkImpl::StartTLSClient ( cX509CertPtr  a_OwnCert,
cCryptoKeyPtr  a_OwnPrivKey,
cX509CertPtr  a_TrustedRootCAs 
)
overridevirtual

Starts a TLS handshake as a client connection.

If a client certificate should be used for the connection, set the certificate into a_OwnCertData and its corresponding private key to a_OwnPrivKeyData. If both are empty, no client cert is presented. a_OwnPrivKeyPassword is the password to be used for decoding PrivKey, empty if not passworded. Returns empty string on success, non-empty error description on failure.

Implements cTCPLink.

Definition at line 245 of file TCPLinkImpl.cpp.

◆ StartTLSServer()

AString cTCPLinkImpl::StartTLSServer ( cX509CertPtr  a_OwnCert,
cCryptoKeyPtr  a_OwnPrivKey,
const AString a_StartTLSData 
)
overridevirtual

Starts a TLS handshake as a server connection.

Set the server certificate into a_CertData and its corresponding private key to a_OwnPrivKeyData. a_OwnPrivKeyPassword is the password to be used for decoding PrivKey, empty if not passworded. a_StartTLSData is any data that should be pushed into the TLS before reading more data from the remote. This is used mainly for protocols starting TLS in the middle of communication, when the TLS start command can be received together with the TLS Client Hello message in one OnReceivedData() call, to re-queue the Client Hello message into the TLS handshake buffer. Returns empty string on success, non-empty error description on failure.

Implements cTCPLink.

Definition at line 301 of file TCPLinkImpl.cpp.

◆ UpdateAddress()

void cTCPLinkImpl::UpdateAddress ( const sockaddr *  a_Address,
socklen_t  a_AddrLen,
AString a_IP,
UInt16 a_Port 
)
staticprotected

Sets a_IP and a_Port to values read from a_Address, based on the correct address family.

Definition at line 471 of file TCPLinkImpl.cpp.

◆ UpdateLocalAddress()

void cTCPLinkImpl::UpdateLocalAddress ( void  )
protected

Updates m_LocalIP and m_LocalPort based on the metadata read from the socket.

Definition at line 506 of file TCPLinkImpl.cpp.

◆ UpdateRemoteAddress()

void cTCPLinkImpl::UpdateRemoteAddress ( void  )
protected

Updates m_RemoteIP and m_RemotePort based on the metadata read from the socket.

Definition at line 518 of file TCPLinkImpl.cpp.

◆ WriteCallback()

void cTCPLinkImpl::WriteCallback ( bufferevent *  a_BufferEvent,
void *  a_Self 
)
staticprotected

Callback that LibEvent calls when the remote peer can receive more data.

Definition at line 367 of file TCPLinkImpl.cpp.

Member Data Documentation

◆ m_BufferEvent

bufferevent* cTCPLinkImpl::m_BufferEvent
protected

The LibEvent handle representing this connection.

Definition at line 149 of file TCPLinkImpl.h.

◆ m_ConnectCallbacks

cNetwork::cConnectCallbacksPtr cTCPLinkImpl::m_ConnectCallbacks
protected

Callbacks to call when the connection is established.

May be NULL if not used. Only used for outgoing connections (cNetwork::Connect()).

Definition at line 146 of file TCPLinkImpl.h.

◆ m_LocalIP

AString cTCPLinkImpl::m_LocalIP
protected

The IP address of the local endpoint.

Valid only after the socket has been connected.

Definition at line 156 of file TCPLinkImpl.h.

◆ m_LocalPort

UInt16 cTCPLinkImpl::m_LocalPort
protected

The port of the local endpoint.

Valid only after the socket has been connected.

Definition at line 159 of file TCPLinkImpl.h.

◆ m_RemoteHost

AString cTCPLinkImpl::m_RemoteHost
protected

The original host parameter which was used for creating the link, either hostname or IP address.

Used for TLS SNI.

Definition at line 163 of file TCPLinkImpl.h.

◆ m_RemoteIP

AString cTCPLinkImpl::m_RemoteIP
protected

The IP address of the remote endpoint.

Valid only after the socket has been connected.

Definition at line 166 of file TCPLinkImpl.h.

◆ m_RemotePort

UInt16 cTCPLinkImpl::m_RemotePort
protected

The port of the remote endpoint.

Valid only after the socket has been connected.

Definition at line 169 of file TCPLinkImpl.h.

◆ m_Self

cTCPLinkImplPtr cTCPLinkImpl::m_Self
protected

SharedPtr to self, used to keep this object alive as long as the callbacks are coming.

Initialized in Enable(), cleared in Close() and EventCallback(RemoteClosed).

Definition at line 173 of file TCPLinkImpl.h.

◆ m_Server

cServerHandleImplPtr cTCPLinkImpl::m_Server
protected

The server handle that has created this link.

Only valid for incoming connections, nullptr for outgoing connections.

Definition at line 153 of file TCPLinkImpl.h.

◆ m_ShouldShutdown

bool cTCPLinkImpl::m_ShouldShutdown
protected

If true, Shutdown() has been called and is in queue.

No more data is allowed to be sent via Send() and after all the currently buffered data is sent to the OS TCP stack, the socket gets shut down.

Definition at line 178 of file TCPLinkImpl.h.

◆ m_TlsContext

cLinkTlsContextPtr cTCPLinkImpl::m_TlsContext
protected

The SSL context used for encryption, if this link uses SSL.

If valid, the link uses encryption through this context.

Definition at line 182 of file TCPLinkImpl.h.


The documentation for this class was generated from the following files: