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:
25 typedef std::shared_ptr<cServerHandleImpl> cServerHandleImplPtr;
27 typedef std::shared_ptr<cTCPLinkImpl> cTCPLinkImplPtr;
28 typedef std::vector<cTCPLinkImplPtr> cTCPLinkImplPtrs;
29 
30 
31 
32 
33 
35  public cTCPLink
36 {
37  typedef cTCPLink super;
38 
39 public:
44  cTCPLinkImpl(evutil_socket_t a_Socket, cCallbacksPtr a_LinkCallbacks, cServerHandleImplPtr a_Server, const sockaddr * a_Address, socklen_t a_AddrLen);
45 
47  virtual ~cTCPLinkImpl() override;
48 
52  static cTCPLinkImplPtr Connect(const AString & a_Host, UInt16 a_Port, cTCPLink::cCallbacksPtr a_LinkCallbacks, cNetwork::cConnectCallbacksPtr a_ConnectCallbacks);
53 
58  void Enable(cTCPLinkImplPtr a_Self);
59 
60  // cTCPLink overrides:
61  virtual bool Send(const void * a_Data, size_t a_Length) override;
62  virtual AString GetLocalIP(void) const override { return m_LocalIP; }
63  virtual UInt16 GetLocalPort(void) const override { return m_LocalPort; }
64  virtual AString GetRemoteIP(void) const override { return m_RemoteIP; }
65  virtual UInt16 GetRemotePort(void) const override { return m_RemotePort; }
66  virtual void Shutdown(void) override;
67  virtual void Close(void) override;
68  virtual AString StartTLSClient(
69  cX509CertPtr a_OwnCert,
70  cCryptoKeyPtr a_OwnPrivKey
71  ) override;
72  virtual AString StartTLSServer(
73  cX509CertPtr a_OwnCert,
74  cCryptoKeyPtr a_OwnPrivKey,
75  const AString & a_StartTLSData
76  ) override;
77 
78 protected:
79 
80  // fwd:
82  typedef std::shared_ptr<cLinkTlsContext> cLinkTlsContextPtr;
83  typedef std::weak_ptr<cLinkTlsContext> cLinkTlsContextWPtr;
84 
87  public cSslContext
88  {
90 
93 
96 
98  cLinkTlsContextWPtr m_Self;
99 
100  public:
101  cLinkTlsContext(cTCPLinkImpl & a_Link);
102 
104  void SetSelf(cLinkTlsContextWPtr a_Self);
105 
107  void ResetSelf(void);
108 
111  void StoreReceivedData(const char * a_Data, size_t a_NumBytes);
112 
114  void FlushBuffers(void);
115 
117  void TryFinishHandshaking(void);
118 
121  void Send(const void * a_Data, size_t a_Length);
122 
123  // cSslContext overrides:
124  virtual int ReceiveEncrypted(unsigned char * a_Buffer, size_t a_NumBytes) override;
125  virtual int SendEncrypted(const unsigned char * a_Buffer, size_t a_NumBytes) override;
126 
128  bool IsLink(cTCPLinkImpl * a_Link)
129  {
130  return (a_Link == &m_Link);
131  }
132  };
133 
134 
138 
140  bufferevent * m_BufferEvent;
141 
145 
148 
151 
154 
157 
161 
166 
169  cLinkTlsContextPtr m_TlsContext;
170 
171 
176  cTCPLinkImpl(const cCallbacksPtr a_LinkCallbacks);
177 
179  static void ReadCallback(bufferevent * a_BufferEvent, void * a_Self);
180 
182  static void WriteCallback(bufferevent * a_BufferEvent, void * a_Self);
183 
185  static void EventCallback(bufferevent * a_BufferEvent, short a_What, void * a_Self);
186 
188  static void UpdateAddress(const sockaddr * a_Address, socklen_t a_AddrLen, AString & a_IP, UInt16 & a_Port);
189 
191  void UpdateLocalAddress(void);
192 
194  void UpdateRemoteAddress(void);
195 
198  void DoActualShutdown(void);
199 
201  bool SendRaw(const void * a_Data, size_t a_Length);
202 
204  void ReceivedCleartextData(const char * a_Data, size_t a_Length);
205 };
206 
207 
208 
209 
cTCPLink super
Definition: TCPLinkImpl.h:37
void FlushBuffers(void)
Tries to read any cleartext data available through the SSL, reports it in the link.
virtual int SendEncrypted(const unsigned char *a_Buffer, size_t a_NumBytes) override
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.
virtual AString StartTLSServer(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey, const AString &a_StartTLSData) override
Starts a TLS handshake as a server connection.
void TryFinishHandshaking(void)
Tries to finish handshaking the SSL.
cServerHandleImplPtr m_Server
The server handle that has created this link.
Definition: TCPLinkImpl.h:144
std::shared_ptr< cX509Cert > cX509CertPtr
Definition: SslConfig.h:13
bool m_ShouldShutdown
If true, Shutdown() has been called and is in queue.
Definition: TCPLinkImpl.h:165
cNetwork::cConnectCallbacksPtr m_ConnectCallbacks
Callbacks to call when the connection is established.
Definition: TCPLinkImpl.h:137
void UpdateRemoteAddress(void)
Updates m_RemoteIP and m_RemotePort based on the metadata read from the socket.
virtual UInt16 GetLocalPort(void) const override
Returns the port used by the local endpoint of the connection.
Definition: TCPLinkImpl.h:63
bool IsLink(cTCPLinkImpl *a_Link)
Returns true if the context&#39;s associated TCP link is the same link as a_Link.
Definition: TCPLinkImpl.h:128
std::shared_ptr< cCryptoKey > cCryptoKeyPtr
Definition: CryptoKey.h:72
cTCPLinkImplPtr m_Self
SharedPtr to self, used to keep this object alive as long as the callbacks are coming.
Definition: TCPLinkImpl.h:160
Wrapper around cSslContext that is used when this link is being encrypted by SSL. ...
Definition: TCPLinkImpl.h:86
AString m_LocalIP
The IP address of the local endpoint.
Definition: TCPLinkImpl.h:147
cLinkTlsContextWPtr m_Self
Shared ownership of self, so that this object can keep itself alive for as long as it needs...
Definition: TCPLinkImpl.h:98
cLinkTlsContext(cTCPLinkImpl &a_Link)
virtual UInt16 GetRemotePort(void) const override
Returns the port used by the remote endpoint of the connection.
Definition: TCPLinkImpl.h:65
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:33
virtual AString StartTLSClient(cX509CertPtr a_OwnCert, cCryptoKeyPtr a_OwnPrivKey) override
Starts a TLS handshake as a client connection.
AString m_EncryptedData
Buffer for storing the incoming encrypted data until it is requested by the SSL decryptor.
Definition: TCPLinkImpl.h:92
std::shared_ptr< cLinkTlsContext > cLinkTlsContextPtr
Definition: TCPLinkImpl.h:81
virtual AString GetLocalIP(void) const override
Returns the IP address of the local endpoint of the connection.
Definition: TCPLinkImpl.h:62
virtual void Close(void) override
Drops the connection without any more processing.
static void EventCallback(bufferevent *a_BufferEvent, short a_What, void *a_Self)
Callback that LibEvent calls when there&#39;s a non-data-related event on the socket. ...
static void ReadCallback(bufferevent *a_BufferEvent, void *a_Self)
Callback that LibEvent calls when there&#39;s data available from the remote peer.
virtual ~cTCPLinkImpl() override
Destroys the LibEvent handle representing the link.
Definition: TCPLinkImpl.cpp:50
std::shared_ptr< cTCPLinkImpl > cTCPLinkImplPtr
void Send(const void *a_Data, size_t a_Length)
Sends the specified cleartext data over the SSL to the remote peer.
UInt16 m_LocalPort
The port of the local endpoint.
Definition: TCPLinkImpl.h:150
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)...
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...
void UpdateLocalAddress(void)
Updates m_LocalIP and m_LocalPort based on the metadata read from the socket.
bufferevent * m_BufferEvent
The LibEvent handle representing this connection.
Definition: TCPLinkImpl.h:140
void DoActualShutdown(void)
Calls shutdown on the link and disables LibEvent writing.
AString m_RemoteIP
The IP address of the remote endpoint.
Definition: TCPLinkImpl.h:153
std::shared_ptr< cServerHandleImpl > cServerHandleImplPtr
static void WriteCallback(bufferevent *a_BufferEvent, void *a_Self)
Callback that LibEvent calls when the remote peer can receive more data.
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:62
unsigned short UInt16
Definition: Globals.h:114
std::shared_ptr< cCallbacks > cCallbacksPtr
Definition: Network.h:63
std::weak_ptr< cLinkTlsContext > cLinkTlsContextWPtr
Definition: TCPLinkImpl.h:83
std::shared_ptr< cServerHandleImpl > cServerHandleImplPtr
Definition: TCPLinkImpl.h:24
std::string AString
Definition: StringUtils.h:13
std::vector< cTCPLinkImplPtr > cTCPLinkImplPtrs
Definition: TCPLinkImpl.h:28
cLinkTlsContextPtr m_TlsContext
The SSL context used for encryption, if this link uses SSL.
Definition: TCPLinkImpl.h:169
std::shared_ptr< cConnectCallbacks > cConnectCallbacksPtr
Definition: Network.h:240
Interface that provides the methods available on a single TCP connection.
Definition: Network.h:33
void Enable(cTCPLinkImplPtr a_Self)
Enables communication over the link.
virtual bool Send(const void *a_Data, size_t a_Length) override
Queues the specified data for sending to the remote peer.
bool SendRaw(const void *a_Data, size_t a_Length)
Sends the data directly to the socket (without the optional TLS).
void SetSelf(cLinkTlsContextWPtr a_Self)
Shares ownership of self, so that this object can keep itself alive for as long as it needs...
virtual int ReceiveEncrypted(unsigned char *a_Buffer, size_t a_NumBytes) override
virtual AString GetRemoteIP(void) const override
Returns the IP address of the remote endpoint of the connection.
Definition: TCPLinkImpl.h:64
void ResetSelf(void)
Removes the self ownership so that we can detect the SSL closure.
virtual void Shutdown(void) override
Closes the link gracefully.
AString m_CleartextData
Buffer for storing the outgoing cleartext data until the link has finished handshaking.
Definition: TCPLinkImpl.h:95
UInt16 m_RemotePort
The port of the remote endpoint.
Definition: TCPLinkImpl.h:156
std::shared_ptr< cTCPLinkImpl > cTCPLinkImplPtr
Definition: TCPLinkImpl.h:26