Cuberite
A lightweight, fast and extensible game server for Minecraft
BufferedSslContext.cpp
Go to the documentation of this file.
1 
2 // BufferedSslContext.cpp
3 
4 // Implements the cBufferedSslContext class representing a SSL context with the SSL peer data backed by a cByteBuffer
5 
6 #include "Globals.h"
7 #include "BufferedSslContext.h"
8 
9 
10 
11 
12 
14  m_OutgoingData(a_BufferSize),
15  m_IncomingData(a_BufferSize)
16 {
17 }
18 
19 
20 
21 
22 
23 size_t cBufferedSslContext::WriteIncoming(const void * a_Data, size_t a_NumBytes)
24 {
25  size_t NumBytes = std::min(m_IncomingData.GetFreeSpace(), a_NumBytes);
26  if (NumBytes > 0)
27  {
28  m_IncomingData.Write(a_Data, NumBytes);
29  return NumBytes;
30  }
31  return 0;
32 }
33 
34 
35 
36 
37 
38 size_t cBufferedSslContext::ReadOutgoing(void * a_Data, size_t a_DataMaxSize)
39 {
40  size_t NumBytes = std::min(m_OutgoingData.GetReadableSpace(), a_DataMaxSize);
41  if (NumBytes > 0)
42  {
43  m_OutgoingData.ReadBuf(a_Data, NumBytes);
45  return NumBytes;
46  }
47  return 0;
48 }
49 
50 
51 
52 
53 
54 int cBufferedSslContext::ReceiveEncrypted(unsigned char * a_Buffer, size_t a_NumBytes)
55 {
56  // Called when mbedTLS wants to read encrypted data from the SSL peer
57  // Read the data from the buffer inside this object, where the owner has stored them using WriteIncoming():
58  size_t NumBytes = std::min(a_NumBytes, m_IncomingData.GetReadableSpace());
59  if (NumBytes == 0)
60  {
61  return MBEDTLS_ERR_SSL_WANT_READ;
62  }
63  if (!m_IncomingData.ReadBuf(a_Buffer, NumBytes))
64  {
67  }
69  return static_cast<int>(NumBytes);
70 }
71 
72 
73 
74 
75 
76 int cBufferedSslContext::SendEncrypted(const unsigned char * a_Buffer, size_t a_NumBytes)
77 {
78  // Called when mbedTLS wants to write encrypted data to the SSL peer
79  // Write the data into the buffer inside this object, where the owner can later read them using ReadOutgoing():
80  if (!m_OutgoingData.CanWriteBytes(a_NumBytes))
81  {
82  return MBEDTLS_ERR_SSL_WANT_WRITE;
83  }
84  if (!m_OutgoingData.Write(reinterpret_cast<const char *>(a_Buffer), a_NumBytes))
85  {
87  }
88  return static_cast<int>(a_NumBytes);
89 }
90 
91 
92 
93 
size_t GetReadableSpace(void) const
Returns the number of bytes that are currently available for reading (may be less than UsedSpace due ...
Definition: ByteBuffer.cpp:194
size_t WriteIncoming(const void *a_Data, size_t a_NumBytes)
Stores the specified data in the "incoming" buffer, to be process by the SSL decryptor.
cBufferedSslContext(size_t a_BufferSize=64000)
Creates a new context with the buffers of specified size for the encrypted / decrypted data...
bool Write(const void *a_Bytes, size_t a_Count)
Writes the bytes specified to the ringbuffer.
Definition: ByteBuffer.cpp:111
virtual int ReceiveEncrypted(unsigned char *a_Buffer, size_t a_NumBytes) override
#define MBEDTLS_ERR_NET_SEND_FAILED
Sending information through the socket failed.
Definition: ErrorCodes.h:11
void CommitRead(void)
Removes the bytes that have been read from the ringbuffer.
Definition: ByteBuffer.cpp:885
size_t ReadOutgoing(void *a_Data, size_t a_DataMaxSize)
Retrieves data from the "outgoing" buffer, after being processed by the SSL encryptor.
cByteBuffer m_IncomingData
Buffer for the data that has come in and needs to be decrypted from the SSL stream.
void ResetRead(void)
Restarts next reading operation at the start of the ringbuffer.
Definition: ByteBuffer.cpp:896
size_t GetFreeSpace(void) const
Returns the number of bytes that can be successfully written to the ringbuffer.
Definition: ByteBuffer.cpp:160
bool ReadBuf(void *a_Buffer, size_t a_Count)
Reads a_Count bytes into a_Buffer; returns true if successful.
Definition: ByteBuffer.cpp:736
#define MBEDTLS_ERR_NET_RECV_FAILED
Reading information from the socket failed.
Definition: ErrorCodes.h:10
virtual int SendEncrypted(const unsigned char *a_Buffer, size_t a_NumBytes) override
bool CanWriteBytes(size_t a_Count) const
Returns true if the specified amount of bytes are available for writing.
Definition: ByteBuffer.cpp:224
cByteBuffer m_OutgoingData
Buffer for the data that has been encrypted into the SSL stream and should be sent out...