30#ifndef ASYNC_TCP_CONNECTION_INCLUDED
31#define ASYNC_TCP_CONNECTION_INCLUDED
40#include <sigc++/sigc++.h>
42#include <openssl/bio.h>
43#include <openssl/err.h>
44#include <openssl/pem.h>
45#include <openssl/ssl.h>
167 for (; first != last; first ++)
169 if (!*first)
return 0;
199 uint16_t remote_port,
229 size_t recvBufLen(
void)
const {
return m_recv_buf.capacity(); }
250 virtual int write(
const void *buf,
int count);
291 bool isIdle(
void)
const {
return sock == -1; }
332 bool isServer(
void)
const {
return m_ssl_is_server; }
502 return verifyPeer(
this, preverify_ok, store_ctx);
508 enum SslStatus { SSLSTATUS_OK, SSLSTATUS_WANT_IO, SSLSTATUS_FAIL };
516 static_assert(
sizeof *
this ==
sizeof value,
"invalid size");
517 static_assert(__alignof *
this == __alignof value,
"invalid alignment");
521 static constexpr const size_t DEFAULT_BUF_SIZE = 1024;
523 static std::map<SSL*, TcpConnection*> ssl_con_map;
525 IpAddress remote_addr;
526 uint16_t remote_port = 0;
529 std::vector<Char> m_recv_buf;
530 Async::FdWatch m_wr_watch;
531 std::vector<char> m_write_buf;
533 SslContext* m_ssl_ctx =
nullptr;
534 bool m_ssl_is_server =
false;
535 SSL* m_ssl =
nullptr;
536 BIO* m_ssl_rd_bio =
nullptr;
537 BIO* m_ssl_wr_bio =
nullptr;
538 std::vector<char> m_ssl_encrypt_buf;
540 bool m_freezed =
false;
544 auto it = ssl_con_map.find(ssl);
545 return (it != ssl_con_map.end()) ? it->second :
nullptr;
547 static int sslVerifyCallback(
int preverify_ok,
548 X509_STORE_CTX* x509_store_ctx);
550 void recvHandler(FdWatch *watch);
551 void processRecvBuf(
void);
552 void addToWriteBuf(
const char *buf,
size_t len);
553 void onWriteSpaceAvailable(Async::FdWatch* w);
554 int rawWrite(
const void* buf,
int count);
556 SslStatus sslGetStatus(
int n);
557 int sslRecvHandler(
char* src,
int count);
558 SslStatus sslDoHandshake(
void);
559 int sslEncrypt(
void);
560 int sslWrite(
const void* buf,
int count);
Contains a watch for file descriptors.
Platform independent representation of an IP address.
SSL context meant to be used with TcpConnection and friends.
Implements a representation of a X.509 certificate.
A class for representing an IP address in an OS independent way.
SSL context meant to be used with TcpConnection and friends.
A class representing an X.509 certificate.
sigc::signal< void(TcpConnection *)> sslConnectionReady
A signal that is emitted when the SSL connection is ready.
static const char * disconnectReasonStr(DisconnectReason reason)
Translate disconnect reason to a string.
virtual void disconnect(void)
Disconnect from the remote host.
bool isIdle(void) const
Check if the connection is idle.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
Async::SslX509 sslCertificate(void) const
uint16_t remotePort(void) const
Return the remote port used.
static const int DEFAULT_RECV_BUF_LEN
The default size of the reception buffer.
DisconnectReason
Reason code for disconnects.
@ DR_HOST_NOT_FOUND
The specified host was not found in the DNS.
@ DR_SYSTEM_ERROR
A system error occured (check errno).
@ DR_BAD_STATE
The connection ended up in a bad state.
@ DR_PROTOCOL_ERROR
Protocol error.
@ DR_REMOTE_DISCONNECTED
The remote host disconnected.
@ DR_ORDERED_DISCONNECT
Disconnect ordered locally.
@ DR_SWITCH_PEER
A better peer was found so reconnecting.
virtual ~TcpConnection(void)
Destructor.
virtual void onDisconnected(DisconnectReason reason)
Called when a connection has been terminated.
sigc::signal< if_all_true_acc::result_type(TcpConnection *, int, X509_STORE_CTX *)>::accumulated< if_all_true_acc > verifyPeer
A signal that is emitted on SSL/TLS certificate verification.
void setSocket(int sock)
Setup information about the connection.
virtual int emitVerifyPeer(int preverify_ok, X509_STORE_CTX *store_ctx)
Emit the verifyPeer signal.
void setSslContext(SslContext &ctx, bool is_server)
Set the OpenSSL context to use when setting up the connection.
SslX509 sslPeerCertificate(void)
Get the peer certificate associated with this connection.
virtual TcpConnection & operator=(TcpConnection &&other)
Move assignmnt operator.
int socket(void) const
Return the socket file descriptor.
size_t recvBufLen(void) const
virtual int onDataReceived(void *buf, int count)
Called when data has been received on the connection.
bool isServer(void) const
void setRecvBufLen(size_t recv_buf_len)
Set a new receive buffer size.
TcpConnection(int sock, const IpAddress &remote_addr, uint16_t remote_port, size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
friend class TcpClientBase
void unfreeze(void)
Reenable all communication.
sigc::signal< void(TcpConnection *, DisconnectReason)> disconnected
Get common name for the SSL connection.
uint16_t localPort(void) const
Get the local TCP port associated with this connection.
sigc::signal< int(TcpConnection *, void *, int)> dataReceived
A signal that is emitted when data has been received on the connection.
void setRemotePort(uint16_t remote_port)
Setup information about the connection.
virtual void emitDisconnected(DisconnectReason reason)
Emit the disconnected signal.
void freeze(void)
Stop all communication.
void enableSsl(bool enable)
Enable or disable TLS for this connection.
long sslVerifyResult(void) const
Get the result of the certificate verification process.
virtual int write(const void *buf, int count)
Write data to the TCP connection.
SslContext * sslContext(void)
bool isConnected(void) const
Check if the connection is established or not.
IpAddress localHost(void) const
Get the local IP address associated with this connection.
TcpConnection(size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
void setRemoteAddr(const IpAddress &remote_addr)
Setup information about the connection.
virtual void closeConnection(void)
Disconnect from the remote peer.
Namespace for the asynchronous programming classes.
A sigc return value accumulator for signals returning bool.
result_type operator()(I first, I last)