00001 #ifndef SSLSOCKET_H 00002 #define SSLSOCKET_H 00003 // $Id: sslsocket.h,v 1.5 2002/10/19 10:36:49 dvermeir Exp $ 00004 00005 #include <dvssl/sslbuffer.h> 00006 #include <dvnet/socket.h> 00007 /*! \file 00008 This file declares the Dv::Ssl::SslSocket class. 00009 */ 00010 namespace Dv { 00011 namespace Ssl { 00012 00013 using Dv::Util::ref; 00014 00015 class SslServerSocket; 00016 00017 //! An iostream derived from Dv::Net::Socket. 00018 /*! Example usage: 00019 \code 00020 SslContextV23 context; 00021 SslSocket client(context,"host.domain",9999); 00022 00023 if (!client) { 00024 cerr << "connection failed: " << client.strerror() << endl; 00025 return 1; 00026 } 00027 00028 X509Certificate cert(client); 00029 cout << "client: certificate name = " << cert.name() << endl 00030 << "client: certificate issuer = " << cert.issuer() << endl; 00031 00032 const string out("hello world"); 00033 client << out << endl; 00034 00035 string line; 00036 getline(client,line); 00037 if (line!=out) { 00038 cerr << "Client expected \"" << out << "\", got \"" << line << "\"" << endl; 00039 return 1; 00040 } 00041 00042 cout << "Client exit status:" << client.strerror() << endl; 00043 return client.error(); 00044 \endcode 00045 */ 00046 class SslSocket: public Net::Socket { 00047 00048 /** SslServerSocket is a friend. */ 00049 friend class SslServerSocket; 00050 00051 public: 00052 //! Extra error code (see Dv::Net::Socket) for SSL connections. 00053 enum { SSL_CONNECTION_FAILED = -30, SSL_ACCEPT_FAILED = -31 }; 00054 00055 //! Constructor. 00056 /*! The Dv::Ssl::SslContext parameter need not contain private key or 00057 certificate file information since it is not used. 00058 */ 00059 SslSocket::SslSocket(SslContext& context,const std::string& host,int port, 00060 size_t bufsize = 1024, int msecs=0); 00061 //! Destructor. 00062 ~SslSocket(); 00063 00064 //! Return SSL* pointer, opaque to avoid inclusion of openssl header files. 00065 void* ssl() const; 00066 //! Return string representation of used cipher. 00067 const char* cipher() const; 00068 //! Overrides Dv::Net::Socket::strerror. 00069 std::string strerror() const; 00070 00071 private: 00072 //! Constructor version used by Dv::Ssl::SslSocket::fs2socket. 00073 SslSocket::SslSocket(SslContext& context,int fd,size_t bufsize=1024, int msecs = 0); 00074 //! Used internally by Dv::Ssl::SslServerSocket::accept. 00075 static ref<SslSocket> fd2sslsocket(SslContext&, int fd, size_t bufsz=1024, int msecs=0); 00076 00077 //! Associated SSL context. 00078 SslContext& context_; 00079 //! Associated streambuf. 00080 SslBuffer* sslbuf_; 00081 }; 00082 00083 }} 00084 #endif