Cuberite
A lightweight, fast and extensible game server for Minecraft
TCPLinkImpl.h
Go to the documentation of this file.
1 
2 // TCPLinkImpl.h
3 
4 // Declares the cTCPLinkImpl class implementing the TCP link functionality
5 
6 // This is an internal header, no-one outside OSSupport should need to include it; use Network.h instead
7 
8 
9 
10 
11 
12 #pragma once
13 
14 #include "Network.h"
15 #include <event2/event.h>
16 #include <event2/bufferevent.h>
17 #include "../mbedTLS++/SslContext.h"
18 
19 
20 
21 
22 
23 // fwd:
24 class cServerHandleImpl;
25 typedef std::shared_ptr<cServerHandleImpl> cServerHandleImplPtr;
26 class cTCPLinkImpl;
27 typedef std::shared_ptr<cTCPLinkImpl> cTCPLinkImplPtr;
28 typedef std::vector<cTCPLinkImplPtr> cTCPLinkImplPtrs;
29 
30 
31 
32 
33 
35  public cTCPLink
36 {
37  using Super = cTCPLink;
38 
39 public:
40 
47  evutil_socket_t a_Socket,
48  cCallbacksPtr a_LinkCallbacks,
49  cServerHandleImplPtr a_Server,
50  const sockaddr * a_Address,
51  socklen_t a_AddrLen
52  );
53 
55  virtual ~cTCPLinkImpl() override;
56 
60  static cTCPLinkImplPtr Connect(const AString & a_Host, UInt16 a_Port, cTCPLink::cCallbacksPtr a_LinkCallbacks, cNetwork::cConnectCallbacksPtr a_ConnectCallbacks);
61 
66  void Enable(cTCPLinkImplPtr a_Self);
67 
68  // cTCPLink overrides:
69  virtual bool Send(const void * a_Data, size_t a_Length) override;
70  virtual AString GetLocalIP(void) const override { return m_LocalIP; }
71  virtual UInt16 GetLocalPort(void) const override { return m_LocalPort; }
72  virtual AString GetRemoteIP(void) const override { return m_RemoteIP; }
73  virtual UInt16 GetRemotePort(void) const override { return m_RemotePort; }
74  virtual void Shutdown(void) override;
75  virtual void Close(void) override;
76  virtual AString StartTLSClient(
77  cX509CertPtr a_OwnCert,
78  cCryptoKeyPtr a_OwnPrivKey,
79  cX509CertPtr a_TrustedRootCAs
80  ) override;
81  virtual AString StartTLSServer(
82  cX509CertPtr a_OwnCert,
83  cCryptoKeyPtr a_OwnPrivKey,
84  const AString & a_StartTLSData
85  ) override;
86 
87 protected:
88 
89  // fwd:
90  class cLinkTlsContext;
91  typedef std::shared_ptr<cLinkTlsContext> cLinkTlsContextPtr;
92  typedef std::weak_ptr<cLinkTlsContext> cLinkTlsContextWPtr;
93 
96  public cSslContext
97  {
99 
102 
105 
108 
109  public:
110  cLinkTlsContext(cTCPLinkImpl & a_Link);
111 
113  void SetSelf(cLinkTlsContextWPtr a_Self);
114 
116  void ResetSelf(void);
117 
120  void StoreReceivedData(const char * a_Data, size_t a_NumBytes);
121 
123  void FlushBuffers(void);
124 
126  void TryFinishHandshaking(void);
127 
130  void Send(const void * a_Data, size_t a_Length);
131 
132  // cSslContext overrides:
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;
135 
137  bool IsLink(cTCPLinkImpl * a_Link)
138  {
139  return (a_Link == &m_Link);
140  }
141  };
142 
143 
147 
149  bufferevent * m_BufferEvent;
150 
154 
157 
160 
164 
167 
170 
174 
179 
183 
184 
189  cTCPLinkImpl(const std::string & a_Host, const cCallbacksPtr a_LinkCallbacks);
190 
192  static void ReadCallback(bufferevent * a_BufferEvent, void * a_Self);
193 
195  static void WriteCallback(bufferevent * a_BufferEvent, void * a_Self);
196 
198  static void EventCallback(bufferevent * a_BufferEvent, short a_What, void * a_Self);
199 
201  static void UpdateAddress(const sockaddr * a_Address, socklen_t a_AddrLen, AString & a_IP, UInt16 & a_Port);
202 
204  void UpdateLocalAddress(void);
205 
207  void UpdateRemoteAddress(void);
208 
211  void DoActualShutdown(void);
212 
214  bool SendRaw(const void * a_Data, size_t a_Length);
215 
217  void ReceivedCleartextData(const char * a_Data, size_t a_Length);
218 };
219 
220 
221 
222 
unsigned short UInt16
Definition: Globals.h:158
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
Definition: CryptoKey.h:72
std::shared_ptr< cX509Cert > cX509CertPtr
Definition: SslConfig.h:13
std::shared_ptr< cTCPLinkImpl > cTCPLinkImplPtr
std::shared_ptr< cServerHandleImpl > cServerHandleImplPtr
std::shared_ptr< cTCPLinkImpl > cTCPLinkImplPtr
Definition: TCPLinkImpl.h:26
std::vector< cTCPLinkImplPtr > cTCPLinkImplPtrs
Definition: TCPLinkImpl.h:28
std::shared_ptr< cServerHandleImpl > cServerHandleImplPtr
Definition: TCPLinkImpl.h:24
std::string AString
Definition: StringUtils.h:11
Interface that provides the methods available on a single TCP connection.
Definition: Network.h:42
cTCPLink(cCallbacksPtr a_Callbacks)
Creates a new link, with the specified callbacks.
Definition: Network.h:143
std::shared_ptr< cCallbacks > cCallbacksPtr
Definition: Network.h:71
std::shared_ptr< cConnectCallbacks > cConnectCallbacksPtr
Definition: Network.h:249
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.
Definition: TCPLinkImpl.h:178
virtual UInt16 GetRemotePort(void) const override
Returns the port used by the remote endpoint of the connection.
Definition: TCPLinkImpl.h:73
virtual ~cTCPLinkImpl() override
Destroys the LibEvent handle representing the link.
Definition: TCPLinkImpl.cpp:57
cLinkTlsContextPtr m_TlsContext
The SSL context used for encryption, if this link uses SSL.
Definition: TCPLinkImpl.h:182
bufferevent * m_BufferEvent
The LibEvent handle representing this connection.
Definition: TCPLinkImpl.h:149
AString m_LocalIP
The IP address of the local endpoint.
Definition: TCPLinkImpl.h:156
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.
Definition: TCPLinkImpl.h:159
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.
Definition: TCPLinkImpl.cpp:69
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.
Definition: TCPLinkImpl.h:72
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.
Definition: TCPLinkImpl.h:146
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.
Definition: TCPLinkImpl.cpp:34
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.
Definition: TCPLinkImpl.h:70
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.
Definition: TCPLinkImpl.h:169
std::shared_ptr< cLinkTlsContext > cLinkTlsContextPtr
Definition: TCPLinkImpl.h:90
AString m_RemoteHost
The original host parameter which was used for creating the link, either hostname or IP address.
Definition: TCPLinkImpl.h:163
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.
Definition: TCPLinkImpl.h:153
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
Definition: TCPLinkImpl.h:92
virtual UInt16 GetLocalPort(void) const override
Returns the port used by the local endpoint of the connection.
Definition: TCPLinkImpl.h:71
AString m_RemoteIP
The IP address of the remote endpoint.
Definition: TCPLinkImpl.h:166
cTCPLinkImplPtr m_Self
SharedPtr to self, used to keep this object alive as long as the callbacks are coming.
Definition: TCPLinkImpl.h:173
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.
Definition: TCPLinkImpl.h:97
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.
Definition: TCPLinkImpl.h:101
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.
Definition: TCPLinkImpl.h:107
bool IsLink(cTCPLinkImpl *a_Link)
Returns true if the context's associated TCP link is the same link as a_Link.
Definition: TCPLinkImpl.h:137
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.
Definition: TCPLinkImpl.h:104
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.