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(); }
246 virtual int write(
const void *buf,
int count);
287 bool isIdle(
void)
const {
return sock == -1; }
328 bool isServer(
void)
const {
return m_ssl_is_server; }
495 return verifyPeer(
this, preverify_ok, store_ctx);
501 enum SslStatus { SSLSTATUS_OK, SSLSTATUS_WANT_IO, SSLSTATUS_FAIL };
509 static_assert(
sizeof *
this ==
sizeof value,
"invalid size");
510 static_assert(__alignof *
this == __alignof value,
"invalid alignment");
514 static constexpr const size_t DEFAULT_BUF_SIZE = 1024;
516 static std::map<SSL*, TcpConnection*> ssl_con_map;
518 IpAddress remote_addr;
519 uint16_t remote_port = 0;
522 std::vector<Char> m_recv_buf;
524 std::vector<char> m_write_buf;
526 SslContext* m_ssl_ctx =
nullptr;
527 bool m_ssl_is_server =
false;
528 SSL* m_ssl =
nullptr;
529 BIO* m_ssl_rd_bio =
nullptr;
530 BIO* m_ssl_wr_bio =
nullptr;
531 std::vector<char> m_ssl_encrypt_buf;
533 bool m_freezed =
false;
537 auto it = ssl_con_map.find(ssl);
538 return (it != ssl_con_map.end()) ? it->second :
nullptr;
540 static int sslVerifyCallback(
int preverify_ok,
541 X509_STORE_CTX* x509_store_ctx);
543 void recvHandler(FdWatch *watch);
544 void processRecvBuf(
void);
545 void addToWriteBuf(
const char *buf,
size_t len);
547 int rawWrite(
const void* buf,
int count);
549 SslStatus sslGetStatus(
int n);
550 int sslRecvHandler(
char* src,
int count);
551 SslStatus sslDoHandshake(
void);
552 int sslEncrypt(
void);
553 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 watching file descriptors.
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.
A base class for creating a TCP client connection.
A class for handling exiting TCP connections.
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.
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
sigc::signal< int, TcpConnection *, void *, int > dataReceived
A signal that is emitted when data has been received on the connection.
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.
void unfreeze(void)
Reenable all communication.
uint16_t localPort(void) const
Get the local TCP port associated with this 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.
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.
SslContext * sslContext(void)
sigc::signal< void, TcpConnection * > sslConnectionReady
A signal that is emitted when the SSL connection is ready.
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.
sigc::signal< void, TcpConnection *, DisconnectReason > disconnected
Get common name for the SSL connection.
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)