00001 #ifndef DV_SSL_SSLSOCKET_H 00002 #define DV_SSL_SSLSOCKET_H 00003 // $Id: sslsocket.h,v 1.10 2008/03/15 10:15:25 dvermeir Exp $ 00004 00005 #include <dvssl/sslbuffer.h> 00006 #include <dvnet/socket.h> 00007 00008 namespace Dv { 00009 namespace Ssl { 00010 class ServerSocket; 00011 /** An iostream derived from Dv::Net::Socket. 00012 * Example usage: 00013 * @code 00014 * Dv::Ssl::ContextV23 context; 00015 * Dv::Ssl::Socket client(context, "host.domain", 9999); 00016 * 00017 * if (!client) { 00018 * std::cerr << "connection failed: " << client.strerror() << endl; 00019 * return 1; 00020 * } 00021 * 00022 * Dv::Ssl::X509Certificate cert(client); 00023 * std::cout << "client: certificate name = " << cert.name() << std::endl 00024 * << "client: certificate issuer = " << cert.issuer() << std::endl; 00025 * 00026 * const std::string out("hello world"); 00027 * client << out << std::endl; 00028 * 00029 * std::string line; 00030 * std::getline(client, line); 00031 * if (line!=out) { 00032 * std::cerr << "Client expected \"" << out << "\", got \"" << line << "\"" << std::endl; 00033 * return 1; 00034 * } 00035 * 00036 * std::cout << "Client exit status:" << client.strerror() << std::endl; 00037 * return client.error(); 00038 * @endcode 00039 */ 00040 class Socket: public Net::Socket { 00041 /** Dv::Ssl::ServerSocket is a friend. */ 00042 friend class Dv::Ssl::ServerSocket; 00043 public: 00044 /** Extra error codes (see Dv::Net::Socket) for SSL connections. */ 00045 enum { SSL_CONNECTION_FAILED = -30, SSL_ACCEPT_FAILED = -31 }; 00046 /** Constructor. The Dv::Ssl::Context parameter need not contain private key or 00047 * certificate file information since it is not used. 00048 * @param context existing Dv::Ssl::Context object. 00049 * @param host name of host to connect to 00050 * @param port on host to connect to 00051 * @param bufsize size of input and output buffers 00052 * @param delay (in milliseconds) time allowed for any I/O operation to complete. 00053 * A value of 0 means ``wait forever''. Note that timedout() makes no sense 00054 * if delay is 0. 00055 * @param min_debug_level if a debug_master is connected, logging info 00056 * will only be written if the master's level is at least @a min_debug_level 00057 * @param debug_master from where debug info will be taken 00058 * @see Dv::DebugSlave 00059 * @warning the Dv::Ssl::Socket object does not own the Dv::Ssl::Context 00060 */ 00061 Socket(Context& context,const std::string& host,int port, 00062 size_t bufsize = 1024, int delay=0, unsigned int min_debug_level = 0, 00063 Debugable* debug_master = 0); 00064 /** Destructor. */ 00065 ~Socket(); 00066 /** Return SSL* pointer, opaque to avoid inclusion of openssl header files. 00067 * @return pointer to underlying SSL structure 00068 */ 00069 void* ssl() const; 00070 /** Return string representation of used cipher. 00071 * @return pointer to string representation of used cypher 00072 */ 00073 const char* cipher() const; 00074 /** Overrides Dv::Net::Socket::strerror. 00075 * @return string representation of last error. 00076 */ 00077 std::string strerror() const; 00078 private: 00079 /** Constructor version used by Dv::Ssl::Socket::fs2socket. 00080 * @param context existing Dv::Ssl::Context object. 00081 * @param fd underlying file descriptor 00082 * @param bufsize size of input and output buffer 00083 * @param delay (in milliseconds) time allowed for any I/O operation to complete. 00084 * A value of 0 means ``wait forever''. Note that timedout() makes no sense 00085 * if delay is 0. 00086 * @param min_debug_level if a debug_master is connected, logging info 00087 * will only be written if the master's level is at least @a min_debug_level 00088 * @param debug_master from where debug info will be taken 00089 * @see Dv::DebugSlave 00090 * @warning the Dv::Ssl::Socket object does not own the Dv::Ssl::Context 00091 */ 00092 Socket(Context& context,int fd,size_t bufsize=1024, int delay = 0, 00093 unsigned int min_debug_level = 0, Debugable* debug_master = 0); 00094 /** This function is used internally by Dv::Ssl::ServerSocket::accept. 00095 * @param ctxt existing Dv::Ssl::Context object. 00096 * @param fd underlying file descriptor 00097 * @param bufsz size of input and output buffer 00098 * @param delay (in milliseconds) time allowed for any I/O operation to complete. 00099 * A value of 0 means ``wait forever''. Note that timedout() makes no sense 00100 * if delay is 0. 00101 * @param min_debug_level if a debug_master is connected, logging info 00102 * will only be written if the master's level is at least @a min_debug_level 00103 * @param debug_master from where debug info will be taken 00104 * @see Dv::DebugSlave 00105 * @warning the Dv::Ssl::Socket object does not own the Dv::Ssl::Context 00106 */ 00107 static Dv::shared_ptr<Dv::Ssl::Socket> fd2sslsocket(Dv::Ssl::Context& ctxt, 00108 int fd, size_t bufsz=1024, int delay=0, unsigned int min_debug_level = 0, 00109 Debugable* debug_master = 0); 00110 /** Associated SSL context. */ 00111 Context& context_; 00112 /** Associated streambuf. */ 00113 Buffer* sslbuf_; 00114 }; 00115 } 00116 } 00117 #endif