More backports from Pro edition, among which HTTPS/TLS support

This commit is contained in:
Thulinma 2019-07-13 14:15:43 +02:00
parent 2432bbdfc3
commit cc9e970ea3
26 changed files with 733 additions and 324 deletions

View file

@ -71,6 +71,7 @@ namespace Socket{
/// If they are not identical and sRecv is closed but sSend still open, reading from sSend will be attempted.
class Connection{
protected:
void clear(); ///< Clears the internal data structure. Meant only for use in constructors.
bool isTrueSocket;
int sSend; ///< Write end of socket.
int sRecv; ///< Read end of socket.
@ -81,24 +82,46 @@ namespace Socket{
uint64_t down;
long long int conntime;
Buffer downbuffer; ///< Stores temporary data coming in.
virtual int iread(void *buffer, int len, int flags = 0); ///< Incremental read call.
virtual unsigned int iwrite(const void *buffer, int len); ///< Incremental write call.
int iread(void *buffer, int len, int flags = 0); ///< Incremental read call.
unsigned int iwrite(const void *buffer, int len); ///< Incremental write call.
bool iread(Buffer &buffer, int flags = 0); ///< Incremental write call that is compatible with Socket::Buffer.
bool iwrite(std::string &buffer); ///< Write call that is compatible with std::string.
void setBoundAddr();
#ifdef SSL
/// optional extension that uses mbedtls for SSL
protected:
bool sslConnected;
int ssl_iread(void *buffer, int len, int flags = 0); ///< Incremental read call.
unsigned int ssl_iwrite(const void *buffer, int len); ///< Incremental write call.
mbedtls_net_context * server_fd;
mbedtls_entropy_context * entropy;
mbedtls_ctr_drbg_context * ctr_drbg;
mbedtls_ssl_context * ssl;
mbedtls_ssl_config * conf;
#endif
public:
// friends
friend class ::Buffer::user;
// constructors
Connection(); ///< Create a new disconnected base socket.
Connection(int sockNo); ///< Create a new base socket.
Connection(std::string hostname, int port, bool nonblock); ///< Create a new TCP socket.
Connection(std::string hostname, int port, bool nonblock, bool with_ssl = false); ///< Create a new TCP socket.
Connection(std::string adres, bool nonblock = false); ///< Create a new Unix Socket.
Connection(int write, int read); ///< Simulate a socket using two file descriptors.
// copy/assignment constructors
Connection(const Connection& rhs);
Connection& operator=(const Connection& rhs);
// destructor
~Connection();
// generic methods
virtual void close(); ///< Close connection.
void open(int sockNo);//Open from existing socket connection.
void open(std::string hostname, int port, bool nonblock, bool with_ssl = false);//Open TCP connection.
void open(std::string adres, bool nonblock = false);//Open Unix connection.
void open(int write, int read);//Open from two existing file descriptors.
void close(); ///< Close connection.
void drop(); ///< Close connection without shutdown.
virtual void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false).
void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false).
bool isBlocking(); ///< Check if this socket is blocking (true) or nonblocking (false).
std::string getHost() const; ///< Gets hostname for connection, if available.
std::string getBinHost();
@ -107,7 +130,7 @@ namespace Socket{
int getSocket(); ///< Returns internal socket number.
int getPureSocket(); ///< Returns non-piped internal socket number.
std::string getError(); ///< Returns a string describing the last error that occured.
virtual bool connected() const; ///< Returns the connected-state for this socket.
bool connected() const; ///< Returns the connected-state for this socket.
bool isAddress(const std::string &addr);
bool isLocal(); ///< Returns true if remote address is a local address.
// buffered i/o methods
@ -136,27 +159,6 @@ namespace Socket{
operator bool() const;
};
#ifdef SSL
/// Version of Socket::Connection that uses mbedtls for SSL
class SSLConnection : public Connection{
public:
SSLConnection();
SSLConnection(std::string hostname, int port, bool nonblock); ///< Create a new TCP socket.
void close(); ///< Close connection.
bool connected() const; ///< Returns the connected-state for this socket.
void setBlocking(bool blocking); ///< Set this socket to be blocking (true) or nonblocking (false).
protected:
bool isConnected;
int iread(void *buffer, int len, int flags = 0); ///< Incremental read call.
unsigned int iwrite(const void *buffer, int len); ///< Incremental write call.
mbedtls_net_context * server_fd;
mbedtls_entropy_context * entropy;
mbedtls_ctr_drbg_context * ctr_drbg;
mbedtls_ssl_context * ssl;
mbedtls_ssl_config * conf;
};
#endif
/// This class is for easily setting up listening socket, either TCP or Unix.
class Server{
private: