102 lines
2.1 KiB
C++
102 lines
2.1 KiB
C++
|
|
|
||
|
|
#include "LuprexSockets.hpp"
|
||
|
|
|
||
|
|
#define UI UI_ST
|
||
|
|
THIRD_PARTY_INCLUDES_START
|
||
|
|
#include <openssl/ssl.h>
|
||
|
|
#include <openssl/rsa.h>
|
||
|
|
#include <openssl/x509.h>
|
||
|
|
#include <openssl/evp.h>
|
||
|
|
#include <openssl/err.h>
|
||
|
|
#include <openssl/bio.h>
|
||
|
|
#include <openssl/pem.h>
|
||
|
|
#include <openssl/conf.h>
|
||
|
|
THIRD_PARTY_INCLUDES_END
|
||
|
|
#undef UI
|
||
|
|
|
||
|
|
|
||
|
|
enum EChanState {
|
||
|
|
CHAN_INACTIVE,
|
||
|
|
CHAN_PLAINTEXT,
|
||
|
|
CHAN_SSL_CONNECTING,
|
||
|
|
CHAN_SSL_ACCEPTING,
|
||
|
|
CHAN_SSL_READWRITE,
|
||
|
|
};
|
||
|
|
|
||
|
|
// A communication socket.
|
||
|
|
class FLpxChannel
|
||
|
|
{
|
||
|
|
int ChannelID;
|
||
|
|
FSocket* Socket;
|
||
|
|
SSL* SSLState;
|
||
|
|
BIO* RecvBIO;
|
||
|
|
BIO* SendBIO;
|
||
|
|
|
||
|
|
// If recent_error is set, that means that a recent IO operation generated
|
||
|
|
// an error. As a special case, EOF on read is considered an error, we use
|
||
|
|
// the string "EOF" for this case.
|
||
|
|
std::string RecentError;
|
||
|
|
|
||
|
|
// OpenSSL has a rule: if you try to SSL_write and it returns
|
||
|
|
// SSL_ERROR_WANT_READ, then you have to retry the write with the same
|
||
|
|
// number of bytes. In this event, we record how many bytes we
|
||
|
|
// attempted to write, which will enable us to retry.
|
||
|
|
int RetryWriteNBytes;
|
||
|
|
|
||
|
|
// True if the channel needs to be advanced.
|
||
|
|
bool NeedAdvance;
|
||
|
|
|
||
|
|
EChanState State;
|
||
|
|
uint32_t NBytes;
|
||
|
|
const char* Bytes;
|
||
|
|
};
|
||
|
|
|
||
|
|
// A port-listening socket.
|
||
|
|
class FLpxListener
|
||
|
|
{
|
||
|
|
int BoundPort;
|
||
|
|
FSocket* Socket;
|
||
|
|
};
|
||
|
|
|
||
|
|
class FLpxSocketsI : public FLpxSockets
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
// We don't own the wrapper, we just have a pointer to it.
|
||
|
|
// We require a guarantee that it outlives us.
|
||
|
|
EngineWrapper* Luprex;
|
||
|
|
|
||
|
|
TArray<FLpxChannel> Channels;
|
||
|
|
TArray<FLpxListener> Listeners;
|
||
|
|
|
||
|
|
|
||
|
|
SSL_CTX* ServerCTX;
|
||
|
|
SSL_CTX* ClientSecureCTX;
|
||
|
|
SSL_CTX* ClientInsecureCTX;
|
||
|
|
|
||
|
|
FLpxSocketsI(EngineWrapper* w);
|
||
|
|
virtual ~FLpxSocketsI() override;
|
||
|
|
|
||
|
|
virtual void Update() override;
|
||
|
|
};
|
||
|
|
|
||
|
|
FLpxSocketsI::FLpxSocketsI(EngineWrapper *w)
|
||
|
|
{
|
||
|
|
Luprex = w;
|
||
|
|
ServerCTX = nullptr;
|
||
|
|
ClientSecureCTX = nullptr;
|
||
|
|
ClientInsecureCTX = nullptr;
|
||
|
|
}
|
||
|
|
|
||
|
|
FLpxSocketsI::~FLpxSocketsI()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
void FLpxSocketsI::Update()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
FLpxSockets* FLpxSockets::Create(EngineWrapper* w)
|
||
|
|
{
|
||
|
|
return new FLpxSocketsI(w);
|
||
|
|
}
|