Cuberite
A lightweight, fast and extensible game server for Minecraft
BlockingSslClientSocket.h
Go to the documentation of this file.
1 
2 // BlockingSslClientSocket.h
3 
4 // Declares the cBlockingSslClientSocket class representing a blocking TCP socket with client SSL encryption over it
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "../OSSupport/Network.h"
13 #include "CallbackSslContext.h"
14 
15 
16 
17 
18 
21 {
22 public:
24 
25  virtual ~cBlockingSslClientSocket(void) override
26  {
27  Disconnect();
28  }
29 
32  bool Connect(const AString & a_ServerName, UInt16 a_Port);
33 
36  bool Send(const void * a_Data, size_t a_NumBytes);
37 
42  int Receive(void * a_Data, size_t a_MaxBytes);
43 
46  void Disconnect(void);
47 
52  void SetExpectedPeerName(AString a_ExpectedPeerName);
53 
56  void SetSslConfig(std::shared_ptr<const cSslConfig> a_Config);
57 
59  const AString & GetLastErrorText(void) const { return m_LastErrorText; }
60 
61 protected:
64 
67 
70 
73 
75  std::shared_ptr<const cSslConfig> m_Config;
76 
79 
82 
85 
87  std::atomic<bool> m_IsConnected;
88 
91 
95 
96 
98  void OnConnected(void);
99 
101  void OnConnectError(const AString & a_ErrorMsg);
102 
104  void OnReceivedData(const char * a_Data, size_t a_Size);
105 
107  void SetLink(cTCPLinkPtr a_Link);
108 
110  void OnDisconnected(void);
111 
112  // cCallbackSslContext::cDataCallbacks overrides:
113  virtual int ReceiveEncrypted(unsigned char * a_Buffer, size_t a_NumBytes) override;
114  virtual int SendEncrypted(const unsigned char * a_Buffer, size_t a_NumBytes) override;
115 } ;
116 
117 
118 
119 
unsigned short UInt16
Definition: Globals.h:158
std::shared_ptr< cTCPLink > cTCPLinkPtr
Definition: Network.h:25
std::string AString
Definition: StringUtils.h:11
cCallbackSslContext m_Ssl
The SSL context used for the socket.
const AString & GetLastErrorText(void) const
Returns the text of the last error that has occurred in this instance.
void Disconnect(void)
Disconnects the connection gracefully, if possible.
bool Connect(const AString &a_ServerName, UInt16 a_Port)
Connects to the specified server and performs SSL handshake.
cTCPLinkPtr m_Socket
The underlying socket to the SSL server.
void SetSslConfig(std::shared_ptr< const cSslConfig > a_Config)
Set the config to be used by the SSL context.
void OnConnected(void)
Called when the connection is established successfully.
AString m_ServerName
The hostname to which the socket is connecting (stored for error reporting).
int Receive(void *a_Data, size_t a_MaxBytes)
Receives data from the connection.
virtual int ReceiveEncrypted(unsigned char *a_Buffer, size_t a_NumBytes) override
Called when mbedTLS wants to read encrypted data from the SSL peer.
virtual int SendEncrypted(const unsigned char *a_Buffer, size_t a_NumBytes) override
Called when mbedTLS wants to write encrypted data to the SSL peer.
void SetExpectedPeerName(AString a_ExpectedPeerName)
Sets the Expected peer name.
std::atomic< bool > m_IsConnected
Set to true if the connection established successfully.
void OnConnectError(const AString &a_ErrorMsg)
Called when an error occurs while connecting the socket.
std::shared_ptr< const cSslConfig > m_Config
The configuration to be used by the SSL context.
cEvent m_Event
The object used to signal state changes in the socket (the cause of the blocking).
bool Send(const void *a_Data, size_t a_NumBytes)
Sends the specified data over the connection.
AString m_ExpectedPeerName
The expected SSL peer's name, if we are to verify the cert strictly.
cCriticalSection m_CSIncomingData
Protects m_IncomingData against multithreaded access.
virtual ~cBlockingSslClientSocket(void) override
AString m_IncomingData
Buffer for the data incoming on the network socket.
void OnReceivedData(const char *a_Data, size_t a_Size)
Called when there's incoming data from the socket.
void OnDisconnected(void)
Called when the link is disconnected, either gracefully or by an error.
void SetLink(cTCPLinkPtr a_Link)
Called when the link for the connection is created.
AString m_LastErrorText
Text of the last error that has occurred.
Interface used as a data sink for the SSL peer data.
Definition: Event.h:18