00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef CONNECTION_H__
00015 #define CONNECTION_H__
00016
00017 #ifdef WIN32
00018 # include "../config.h.win"
00019 #else
00020 # include "config.h"
00021 #endif
00022
00023 #include "gloox.h"
00024 #include "logsink.h"
00025
00026 #include <string>
00027
00028 #if defined( HAVE_OPENSSL )
00029 # define USE_OPENSSL
00030 # include <openssl/ssl.h>
00031 # define HAVE_TLS
00032 #elif defined( HAVE_GNUTLS )
00033 # define USE_GNUTLS
00034 # include <gnutls/gnutls.h>
00035 # include <gnutls/x509.h>
00036 # define HAVE_TLS
00037 #elif defined( HAVE_WINTLS )
00038 # define USE_WINTLS
00039 # define SECURITY_WIN32
00040 # include <windows.h>
00041 # include <security.h>
00042 # include <sspi.h>
00043 # define HAVE_TLS
00044 #endif
00045
00046 namespace gloox
00047 {
00048
00049 class Compression;
00050 class Packet;
00051 class Parser;
00052
00059 class GLOOX_API Connection
00060 {
00061 public:
00071 Connection( Parser *parser, const LogSink& logInstance, const std::string& server,
00072 unsigned short port = -1 );
00073
00077 virtual ~Connection();
00078
00083 ConnectionState connect();
00084
00090 ConnectionError recv( int timeout = -1 );
00091
00097 bool send( const std::string& data );
00098
00103 ConnectionError receive();
00104
00110 void disconnect( ConnectionError e );
00111
00116 bool isSecure() const { return m_secure; };
00117
00122 ConnectionState state() const { return m_state; };
00123
00130 int fileDescriptor();
00131
00132 #ifdef HAVE_ZLIB
00133
00140 bool initCompression( StreamFeature method );
00141
00146 void enableCompression();
00147 #endif
00148
00149 #ifdef HAVE_TLS
00150
00153 bool tlsHandshake();
00154
00160 void setCACerts( const StringList& cacerts ) { m_cacerts = cacerts; };
00161
00166 const CertInfo& fetchTLSInfo() const { return m_certInfo; };
00167
00179 void setClientCert( const std::string& clientKey, const std::string& clientCerts );
00180 #endif
00181
00182 private:
00183 Connection &operator = ( const Connection & );
00184 bool dataAvailable( int timeout = -1 );
00185
00186 void cancel();
00187 void cleanup();
00188
00189 #ifdef HAVE_TLS
00190 bool tls_send( const void *data, size_t len );
00191 int tls_recv( void *data, size_t len );
00192 bool tls_dataAvailable();
00193 void tls_cleanup();
00194 #endif
00195
00196 #if defined( USE_GNUTLS )
00197 bool verifyAgainstCAs( gnutls_x509_crt_t cert, gnutls_x509_crt_t *CAList, int CAListSize );
00198 bool verifyAgainst( gnutls_x509_crt_t cert, gnutls_x509_crt_t issuer );
00199
00200 gnutls_session_t m_session;
00201 gnutls_certificate_credentials m_credentials;
00202
00203 #elif defined( USE_OPENSSL )
00204 SSL *m_ssl;
00205 #elif defined( USE_WINTLS )
00206 bool handshakeLoop();
00207
00208 SecurityFunctionTableA *m_securityFunc;
00209 CredHandle m_credentials;
00210 CtxtHandle m_context;
00211 SecBufferDesc m_imessage;
00212 SecBufferDesc m_omessage;
00213 SecBuffer m_ibuffers[4];
00214 SecBuffer m_obuffers[4];
00215 SecPkgContext_StreamSizes m_streamSizes;
00216 HMODULE m_lib;
00217
00218 char *m_messageOffset;
00219 char *m_iBuffer;
00220 char *m_oBuffer;
00221 int m_bufferSize;
00222 int m_bufferOffset;
00223 int m_sspiFlags;
00224 #endif
00225
00226 StringList m_cacerts;
00227 std::string m_clientKey;
00228 std::string m_clientCerts;
00229
00230 Parser *m_parser;
00231 ConnectionState m_state;
00232 CertInfo m_certInfo;
00233 ConnectionError m_disconnect;
00234 const LogSink& m_logInstance;
00235 Compression *m_compression;
00236
00237 char *m_buf;
00238 std::string m_server;
00239 unsigned short m_port;
00240 int m_socket;
00241 const int m_bufsize;
00242 bool m_cancel;
00243 bool m_secure;
00244 bool m_fdRequested;
00245 bool m_enableCompression;
00246 };
00247
00248 }
00249
00250 #endif // CONNECTION_H__