Trace socket/connection events.

This commit is contained in:
Vincent Richard 2014-03-25 23:34:03 +01:00
parent 58ca0d1dd5
commit bbfdcc36a0
17 changed files with 147 additions and 5 deletions

View File

@ -119,6 +119,7 @@ void IMAPConnection::connect()
// Create and connect the socket
m_socket = store->getSocketFactory()->create(m_timeoutHandler);
m_socket->setTracer(m_tracer);
#if VMIME_HAVE_TLS_SUPPORT
if (store->isIMAPS()) // dedicated port/IMAPS

View File

@ -106,6 +106,7 @@ void POP3Connection::connect()
// Create and connect the socket
m_socket = store->getSocketFactory()->create(m_timeoutHandler);
m_socket->setTracer(m_tracer);
#if VMIME_HAVE_TLS_SUPPORT
if (store->isPOP3S()) // dedicated port/POP3S

View File

@ -107,6 +107,7 @@ void SMTPConnection::connect()
// Create and connect the socket
m_socket = transport->getSocketFactory()->create(m_timeoutHandler);
m_socket->setTracer(m_tracer);
#if VMIME_HAVE_TLS_SUPPORT
if (transport->isSMTPS()) // dedicated port/SMTPS

View File

@ -34,6 +34,7 @@
#include "vmime/base.hpp"
#include "vmime/net/timeoutHandler.hpp"
#include "vmime/net/tracer.hpp"
namespace vmime {
@ -168,6 +169,20 @@ public:
*/
virtual shared_ptr <timeoutHandler> getTimeoutHandler() = 0;
/** Set the tracer used by this socket. Tracer will only be used
* to report socket-specific events such as connection (not when
* sending/receiving data).
*
* @param tracer tracer to use
*/
virtual void setTracer(shared_ptr <tracer> tracer) = 0;
/** Return the tracer used by this socket.
*
* @return tracer, or NULL if none is set
*/
virtual shared_ptr <tracer> getTracer() = 0;
protected:
socket() { }

View File

@ -74,7 +74,7 @@ public:
* @return server certificate chain, or NULL if the handshake
* has not been performed yet
*/
virtual shared_ptr <security::cert::certificateChain> getPeerCertificates() const = 0;
virtual shared_ptr <security::cert::certificateChain> getPeerCertificates() = 0;
};

View File

@ -147,6 +147,18 @@ shared_ptr <timeoutHandler> TLSSocket_GnuTLS::getTimeoutHandler()
}
void TLSSocket_GnuTLS::setTracer(shared_ptr <net::tracer> tracer)
{
m_wrapped->setTracer(tracer);
}
shared_ptr <net::tracer> TLSSocket_GnuTLS::getTracer()
{
return m_wrapped->getTracer();
}
bool TLSSocket_GnuTLS::waitForRead(const int msecs)
{
return m_wrapped->waitForRead(msecs);
@ -287,6 +299,9 @@ void TLSSocket_GnuTLS::handshake()
if (toHandler)
toHandler->resetTimeOut();
if (getTracer())
getTracer()->traceSend("Beginning SSL/TLS handshake");
// Start handshaking process
try
{
@ -406,8 +421,11 @@ ssize_t TLSSocket_GnuTLS::gnutlsPullFunc
}
shared_ptr <security::cert::certificateChain> TLSSocket_GnuTLS::getPeerCertificates() const
shared_ptr <security::cert::certificateChain> TLSSocket_GnuTLS::getPeerCertificates()
{
if (getTracer())
getTracer()->traceSend("Getting peer certificates");
unsigned int certCount = 0;
const gnutls_datum* rawData = gnutls_certificate_get_peers
(*m_session->m_gnutlsSession, &certCount);

View File

@ -56,7 +56,7 @@ public:
void handshake();
shared_ptr <security::cert::certificateChain> getPeerCertificates() const;
shared_ptr <security::cert::certificateChain> getPeerCertificates();
// Implementation of 'socket'
void connect(const string& address, const port_t port);
@ -83,6 +83,9 @@ public:
shared_ptr <timeoutHandler> getTimeoutHandler();
void setTracer(shared_ptr <net::tracer> tracer);
shared_ptr <net::tracer> getTracer();
private:
void internalThrow();

View File

@ -193,6 +193,18 @@ shared_ptr <timeoutHandler> TLSSocket_OpenSSL::getTimeoutHandler()
}
void TLSSocket_OpenSSL::setTracer(shared_ptr <net::tracer> tracer)
{
m_wrapped->setTracer(tracer);
}
shared_ptr <net::tracer> TLSSocket_OpenSSL::getTracer()
{
return m_wrapped->getTracer();
}
bool TLSSocket_OpenSSL::waitForRead(const int msecs)
{
return m_wrapped->waitForRead(msecs);
@ -343,6 +355,9 @@ void TLSSocket_OpenSSL::handshake()
if (toHandler)
toHandler->resetTimeOut();
if (getTracer())
getTracer()->traceSend("Beginning SSL/TLS handshake");
// Start handshaking process
if (!m_ssl)
createSSLHandle();
@ -389,8 +404,11 @@ void TLSSocket_OpenSSL::handshake()
}
shared_ptr <security::cert::certificateChain> TLSSocket_OpenSSL::getPeerCertificates() const
shared_ptr <security::cert::certificateChain> TLSSocket_OpenSSL::getPeerCertificates()
{
if (getTracer())
getTracer()->traceSend("Getting peer certificates");
STACK_OF(X509)* chain = SSL_get_peer_cert_chain(m_ssl);
if (chain == NULL)

View File

@ -60,7 +60,7 @@ public:
void handshake();
shared_ptr <security::cert::certificateChain> getPeerCertificates() const;
shared_ptr <security::cert::certificateChain> getPeerCertificates();
// Implementation of 'socket'
void connect(const string& address, const port_t port);
@ -87,6 +87,9 @@ public:
shared_ptr <timeoutHandler> getTimeoutHandler();
void setTracer(shared_ptr <net::tracer> tracer);
shared_ptr <net::tracer> getTracer();
private:
static BIO_METHOD sm_customBIOMethod;

View File

@ -84,6 +84,14 @@ void posixSocket::connect(const vmime::string& address, const vmime::port_t port
m_desc = -1;
}
if (m_tracer)
{
std::ostringstream trace;
trace << "Connecting to " << address << ", port " << port;
m_tracer->traceSend(trace.str());
}
#if VMIME_HAVE_GETADDRINFO // use thread-safe and IPv6-aware getaddrinfo() if available
// Resolve address, if needed
@ -370,6 +378,9 @@ void posixSocket::disconnect()
{
if (m_desc != -1)
{
if (m_tracer)
m_tracer->traceSend("Disconnecting");
::shutdown(m_desc, SHUT_RDWR);
::close(m_desc);
@ -730,6 +741,18 @@ shared_ptr <net::timeoutHandler> posixSocket::getTimeoutHandler()
}
void posixSocket::setTracer(shared_ptr <net::tracer> tracer)
{
m_tracer = tracer;
}
shared_ptr <net::tracer> posixSocket::getTracer()
{
return m_tracer;
}
//
// posixSocketFactory

View File

@ -70,6 +70,9 @@ public:
shared_ptr <net::timeoutHandler> getTimeoutHandler();
void setTracer(shared_ptr <net::tracer> tracer);
shared_ptr <net::tracer> getTracer();
protected:
bool waitForData(const bool read, const bool write, const int msecs);
@ -79,6 +82,7 @@ protected:
private:
shared_ptr <vmime::net::timeoutHandler> m_timeoutHandler;
shared_ptr <net::tracer> m_tracer;
byte_t m_buffer[65536];
int m_desc;

View File

@ -81,6 +81,14 @@ void windowsSocket::connect(const vmime::string& address, const vmime::port_t po
addr.sin_port = htons(static_cast <unsigned short>(port));
addr.sin_addr.s_addr = ::inet_addr(address.c_str());
if (m_tracer)
{
std::ostringstream trace;
trace << "Connecting to " << address << ", port " << port;
m_tracer->traceSend(trace.str());
}
if (addr.sin_addr.s_addr == static_cast <int>(-1))
{
::hostent* hostInfo = ::gethostbyname(address.c_str());
@ -153,6 +161,9 @@ void windowsSocket::disconnect()
{
if (m_desc != INVALID_SOCKET)
{
if (m_tracer)
m_tracer->traceSend("Disconnecting");
::shutdown(m_desc, SD_BOTH);
::closesocket(m_desc);
@ -491,6 +502,18 @@ shared_ptr <net::timeoutHandler> windowsSocket::getTimeoutHandler()
}
void windowsSocket::setTracer(shared_ptr <net::tracer> tracer)
{
m_tracer = tracer;
}
shared_ptr <net::tracer> windowsSocket::getTracer()
{
return m_tracer;
}
//
// posixSocketFactory

View File

@ -74,6 +74,9 @@ public:
shared_ptr <net::timeoutHandler> getTimeoutHandler();
void setTracer(shared_ptr <net::tracer> tracer);
shared_ptr <net::tracer> getTracer();
protected:
void throwSocketError(const int err);

View File

@ -102,6 +102,18 @@ shared_ptr <net::timeoutHandler> SASLSocket::getTimeoutHandler()
}
void SASLSocket::setTracer(shared_ptr <net::tracer> tracer)
{
m_wrapped->setTracer(tracer);
}
shared_ptr <net::tracer> SASLSocket::getTracer()
{
return m_wrapped->getTracer();
}
bool SASLSocket::waitForRead(const int msecs)
{
return m_wrapped->waitForRead(msecs);

View File

@ -78,6 +78,9 @@ public:
shared_ptr <net::timeoutHandler> getTimeoutHandler();
void setTracer(shared_ptr <net::tracer> tracer);
shared_ptr <net::tracer> getTracer();
private:
shared_ptr <SASLSession> m_session;

View File

@ -85,6 +85,17 @@ vmime::shared_ptr <vmime::net::timeoutHandler> testSocket::getTimeoutHandler()
}
void testSocket::setTracer(vmime::shared_ptr <vmime::net::tracer> tracer)
{
}
vmime::shared_ptr <vmime::net::tracer> testSocket::getTracer()
{
return vmime::null;
}
bool testSocket::waitForRead(const int msecs)
{
return true;

View File

@ -268,6 +268,9 @@ public:
vmime::shared_ptr <vmime::net::timeoutHandler> getTimeoutHandler();
void setTracer(vmime::shared_ptr <vmime::net::tracer> tracer);
vmime::shared_ptr <vmime::net::tracer> getTracer();
/** Send data to client.
*
* @param buffer data to send