sptk2 logo
SPTK Home Page
Public Member Functions | Protected Member Functions | List of all members
sptk::SSLSocket Class Reference

#include <SSLSocket.h>

Inheritance diagram for sptk::SSLSocket:
sptk::TCPSocket sptk::BaseSocket

Public Member Functions

size_t socketBytes () override
 
void throwSSLError (const String &function, int resultCode) const
 
 SSLSocket (const String &cipherList="ALL")
 
 ~SSLSocket () override
 
void loadKeys (const SSLKeys &keys)
 
void setSNIHostName (const String &sniHostName)
 
void attach (SOCKET socketHandle, bool accept) override
 
void close () noexcept override
 
SSL * handle ()
 
size_t recv (uint8_t *buffer, size_t size) override
 
size_t send (const uint8_t *buffer, size_t len) override
 
- Public Member Functions inherited from sptk::TCPSocket
 TCPSocket (SOCKET_ADDRESS_FAMILY domain=AF_INET, int32_t type=SOCK_STREAM, int32_t protocol=0)
 
 ~TCPSocket () override
 
void setProxy (std::shared_ptr< Proxy > proxy)
 
void close () noexcept override
 
virtual bool accept (SOCKET &clientSocketFD, struct sockaddr_in &clientInfo, std::chrono::milliseconds timeout)
 
size_t socketBytes () override
 
bool readyToRead (std::chrono::milliseconds timeout) override
 
size_t readLine (char *buffer, size_t size, char delimiter='\n')
 
size_t readLine (Buffer &buffer, char delimiter='\n')
 
size_t readLine (String &s, char delimiter='\n')
 
size_t read (uint8_t *buffer, size_t size, sockaddr_in *from=nullptr) override
 
size_t read (Buffer &buffer, size_t size, sockaddr_in *from=nullptr) override
 
size_t read (String &buffer, size_t size, sockaddr_in *from=nullptr) override
 
template<typename T >
size_t read (T &value, sockaddr_in *from=nullptr)
 
- Public Member Functions inherited from sptk::BaseSocket
SOCKET fd () const
 
void open_addr (OpenMode openMode=OpenMode::CREATE, const sockaddr_in *addr=nullptr, std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
 
 BaseSocket (SOCKET_ADDRESS_FAMILY domain=AF_INET, int32_t type=SOCK_STREAM, int32_t protocol=0)
 
 BaseSocket (const BaseSocket &other)=delete
 
 BaseSocket (BaseSocket &&other) noexcept=default
 
virtual ~BaseSocket ()
 Destructor.
 
BaseSocketoperator= (const BaseSocket &other)=delete
 
BaseSocketoperator= (BaseSocket &&other) noexcept=default
 
void blockingMode (bool blocking)
 
virtual size_t socketBytes ()
 
virtual void attach (SOCKET socketHandle, bool accept)
 
virtual SOCKET detach ()
 
void host (const Host &host)
 
const Hosthost () const
 
void open (const Host &host=Host(), OpenMode openMode=OpenMode::CONNECT, bool blockingMode=true, std::chrono::milliseconds timeoutMS=std::chrono::milliseconds(0))
 
void open (const struct sockaddr_in &address, OpenMode openMode=OpenMode::CONNECT, bool blockingMode=true, std::chrono::milliseconds timeoutMS=std::chrono::milliseconds(0))
 
void bind (const char *address, uint32_t portNumber)
 
void listen (uint16_t portNumber=0)
 
virtual void close () noexcept
 
bool active () const
 
int32_t control (int flag, const uint32_t *check) const
 
void setOption (int level, int option, int value) const
 
void getOption (int level, int option, int &value) const
 
virtual size_t recv (uint8_t *buffer, size_t len)
 
virtual size_t send (const uint8_t *buffer, size_t len)
 
virtual size_t read (uint8_t *buffer, size_t size, sockaddr_in *from)
 
virtual size_t read (uint8_t *buffer, size_t size)
 
virtual size_t read (Buffer &buffer, size_t size, sockaddr_in *from)
 
size_t read (Buffer &buffer, size_t size)
 
virtual size_t read (String &buffer, size_t size, sockaddr_in *from)
 
size_t read (String &buffer, size_t size)
 
virtual size_t write (const uint8_t *buffer, size_t size, const sockaddr_in *peer)
 
size_t write (const uint8_t *buffer, size_t size)
 
virtual size_t write (const Buffer &buffer, const sockaddr_in *peer)
 
size_t write (const Buffer &buffer)
 
virtual size_t write (const String &buffer, const sockaddr_in *peer)
 
size_t write (const String &buffer)
 
virtual bool readyToRead (std::chrono::milliseconds timeout)
 
virtual bool readyToWrite (std::chrono::milliseconds timeout)
 
bool blockingMode () const
 Return current blocking mode state. More...
 

Protected Member Functions

void initContextAndSocket ()
 
void _open (const Host &host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
 
void _open (const struct sockaddr_in &address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
 
virtual String getSSLError (const std::string &function, int32_t SSLError) const
 
- Protected Member Functions inherited from sptk::TCPSocket
TCPSocketReaderreader ()
 
void _open (const Host &host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
 
void _open (const struct sockaddr_in &address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeout) override
 
Proxyproxy ()
 
- Protected Member Functions inherited from sptk::BaseSocket
void setSocketFD (SOCKET socket)
 
int32_t domain () const
 
int32_t type () const
 
int32_t protocol () const
 
virtual void _open (const Host &host, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeoutMS)
 
virtual void _open (const struct sockaddr_in &address, OpenMode openMode, bool blockingMode, std::chrono::milliseconds timeoutMS)
 

Additional Inherited Members

- Public Types inherited from sptk::BaseSocket
enum class  OpenMode : uint8_t { CREATE , CONNECT , BIND }
 

Detailed Description

Encrypted TCP Socket

Constructor & Destructor Documentation

◆ SSLSocket()

sptk::SSLSocket::SSLSocket ( const String cipherList = "ALL")
explicit

Constructor

Parameters
cipherListOptional cipher list

◆ ~SSLSocket()

sptk::SSLSocket::~SSLSocket ( )
override

Destructor

Member Function Documentation

◆ _open() [1/2]

void sptk::SSLSocket::_open ( const Host host,
OpenMode  openMode,
bool  blockingMode,
std::chrono::milliseconds  timeout 
)
overrideprotectedvirtual

opens the socket connection by host and port

Initializes SSL first, if host name is empty or port is 0 then the current host and port values are used. They could be defined by previous calls of open(), port(), or host() methods.

Parameters
hostconst Host&, the host name
openModeSocket open mode
blockingModeSocket blocking (true) on non-blocking (false) mode
timeoutConnection timeout. The default is 0 (wait forever)

Reimplemented from sptk::BaseSocket.

◆ _open() [2/2]

void sptk::SSLSocket::_open ( const struct sockaddr_in &  address,
OpenMode  openMode,
bool  blockingMode,
std::chrono::milliseconds  timeout 
)
overrideprotectedvirtual

Opens the client socket connection by host and port

Parameters
addressAddress and port
openModeSocket open mode
blockingModeSocket blocking (true) on non-blocking (false) mode
timeoutConnection timeout. The default is 0 (wait forever)

Reimplemented from sptk::BaseSocket.

◆ attach()

void sptk::SSLSocket::attach ( SOCKET  socketHandle,
bool  accept 
)
overridevirtual

Attaches socket handle

This method is designed to only attach socket handles obtained with accept().

Parameters
socketHandleExternal socket handle.

Reimplemented from sptk::BaseSocket.

◆ close()

void sptk::SSLSocket::close ( )
overridevirtualnoexcept

Closes the socket connection

This method is not thread-safe.

Reimplemented from sptk::BaseSocket.

◆ getSSLError()

virtual String sptk::SSLSocket::getSSLError ( const std::string &  function,
int32_t  SSLError 
) const
protectedvirtual

Get error description for SSL error code

Parameters
functionSSL function
SSLErrorError code returned by SSL_get_error() result
Returns
Error description

◆ handle()

SSL * sptk::SSLSocket::handle ( )
inline

Returns SSL handle

◆ initContextAndSocket()

void sptk::SSLSocket::initContextAndSocket ( )
protected

Initialize SSL context and socket structures

◆ loadKeys()

void sptk::SSLSocket::loadKeys ( const SSLKeys keys)

Loads private key and certificate(s)

Key should be loaded once before the connection. There is no need to load keys for any consequent connection with the same keys. Private key and certificates must be encoded with PEM format. A single file containing private key and certificate can be used by supplying it for both, private key and certificate parameters. If private key is protected with password, then password can be supplied to auto-answer.

Parameters
keysSSL keys

◆ recv()

size_t sptk::SSLSocket::recv ( uint8_t *  buffer,
size_t  size 
)
overridevirtual

Reads data from SSL socket

Parameters
bufferDestination buffer
sizeDestination buffer size
Returns
the number of bytes read from the socket

Reimplemented from sptk::BaseSocket.

◆ send()

size_t sptk::SSLSocket::send ( const uint8_t *  buffer,
size_t  len 
)
overridevirtual

Sends data through SSL socket

Parameters
bufferSend buffer
lenSend data length
Returns
the number of bytes sent the socket

Reimplemented from sptk::BaseSocket.

◆ setSNIHostName()

void sptk::SSLSocket::setSNIHostName ( const String sniHostName)

Set SNI host name. This method only affects next connection.

Parameters
sniHostNameSNI host name

◆ socketBytes()

size_t sptk::SSLSocket::socketBytes ( )
overridevirtual

Returns number of bytes available for read

Reimplemented from sptk::BaseSocket.

◆ throwSSLError()

void sptk::SSLSocket::throwSSLError ( const String function,
int  resultCode 
) const

Throws SSL error based on SSL function return code

Parameters
functionSSL function name
resultCodeSSL function return code

The documentation for this class was generated from the following file:

Fri Oct 14 2022 09:58:45: SPTK 5.4.1