Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

SslConnection.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00007 
00008 SslConnection::SslConnection() {
00009     m_connection = new TcpConnection();
00010     m_sslStream = NULL;
00011 } // ctor
00012 
00013 
00014 SslConnection::~SslConnection() {
00015     close();
00016     delete m_connection;
00017     m_connection = NULL;
00018 } // dtor
00019 
00020 
00021 ConnectionInterface::ObjectType
00022 SslConnection::getConnectionType() {
00023     return SSL_CONNECTION;
00024 }
00025 
00026 
00031 int
00032 SslConnection::connect()
00033 {
00034     if (!m_connection->isConnected()) {
00035         m_connection->connect();
00036 
00037         // SSL connect
00038         m_sslStream = new Ssl();
00039         if (GlobalObjects::instance()->getConfig()->getRWTimeout() > 0) {
00040             m_sslStream->setTimeOut(false, GlobalObjects::instance()->getConfig()->getRWTimeout(), 0);
00041         } else {
00042             m_sslStream->setTimeOut(true, 0, 0);
00043         }
00044 
00045         debug(DEBUG_SSL, "before SSL_connect...");
00046         SslCtx* clientCtx = GlobalObjects::instance()->getConfig()->getClientSslCtx();
00047         if (!m_sslStream->open(false, *(clientCtx), m_connection->getStream())) {
00048             debug(DEBUG_SSL, "SSL connect() error: %s", strerror(errno));
00049             close();
00050             return 0;
00051         }
00052 
00053         fireEvent(CONNECTION_OPENED);
00054 
00055         debug(DEBUG_SSL,"ssl connection established!");
00056             
00057         return 1;
00058     } // if (!isConnected())
00059     else {
00060         debug(DEBUG_SSL, "already connected");
00061     }
00062     return 0;
00063 } // fn connect
00064 
00065 
00076 int
00077 SslConnection::read(unsigned char* buffer, int bufferSize, int amountToRead)
00078 {
00079     if (!m_connection->isConnected()) {
00080         debug(DEBUG_SSL, "trying to read from connection that isnt connected!");
00081         return -1;
00082     }
00083 
00084     if (amountToRead > bufferSize) {
00085         debug(DEBUG_SSL, "ERROR: (amountToRead > bufferSize)");
00086         return -1;
00087     }
00088 
00089     memset(&buffer[0], 0, bufferSize);
00090 
00091     int bytesRead = 0;
00092     int totalBytesRead = 0;
00093 
00094     if (amountToRead == 0) {
00095         totalBytesRead = m_sslStream->read((void*)buffer, bufferSize);
00096         // check for read errors
00097         if (totalBytesRead <= 0) {
00098             debug(DEBUG_SSL, "SSL read error");
00099         }
00100     }
00101     else {
00102         while ((bytesRead >= 0) && (totalBytesRead != amountToRead)) { 
00103             bytesRead = m_sslStream->read((void*)(buffer + totalBytesRead), amountToRead - totalBytesRead);
00104             // check for read errors
00105             if (bytesRead <= 0) {
00106                 debug(DEBUG_SSL, "SSL read error");
00107                 // return the error code from recv()
00108                 totalBytesRead = bytesRead;
00109                 break;
00110             }
00111             totalBytesRead += bytesRead;
00112         }
00113     }
00114 
00115         debug(DEBUG_SSL, "read data: %d bytes", totalBytesRead);
00116         return totalBytesRead;
00117 } // fn read
00118 
00119 
00124 int
00125 SslConnection::write(unsigned char* buffer, int amountToWrite) {
00126         int bytesWritten = 0;
00127     int sendResult = 0;
00128 
00129     do {
00130         sendResult = m_sslStream->write(&buffer[bytesWritten], amountToWrite - bytesWritten);
00131         bytesWritten += sendResult;
00132     } while ((bytesWritten != amountToWrite) && (sendResult > 0));
00133 
00134     if (bytesWritten != amountToWrite) {
00135         debug(DEBUG_SSL, "Error - did not send all data");
00136     }
00137 
00138     if (sendResult < 0) {
00139         printWsaErrorCode();
00140         return sendResult;
00141     }
00142     return bytesWritten;
00143 } // fn writePacket
00144 
00145 
00153 int 
00154 SslConnection::listen() {
00155     return m_connection->listen();
00156 } // fn listen
00157 
00158 
00166 ConnectionInterface*
00167 SslConnection::accept()
00168 {
00169     ConnectionInterface* connection = m_connection->accept();        
00170 
00171     if (connection == NULL) {
00172         return NULL;
00173     }
00174 
00175     // SSL accept
00176     Ssl* newssl = new Ssl();
00177     if (GlobalObjects::instance()->getConfig()->getRWTimeout() > 0) {
00178         newssl->setTimeOut(false, GlobalObjects::instance()->getConfig()->getRWTimeout(), 0);
00179     } else {
00180         newssl->setTimeOut(true, 0, 0);
00181     }
00182     SslCtx* serverCtx = GlobalObjects::instance()->getConfig()->getServerSslCtx();
00183     if (!newssl->open(true, *(serverCtx), connection->getStream())) {
00184         debug(DEBUG_SSL, "SSL accept() error: %s", strerror(errno));
00185         delete connection;
00186         delete newssl;
00187         return NULL;
00188     }
00189     
00190     debug(DEBUG_SSL, "SSL accepted from %s", connection->getSocketAddress()->toCStr());
00191     
00192     // Create the node and remember its info
00193     SslConnection* newConnection = new SslConnection();
00194     newConnection->m_connection = connection;
00195     newConnection->m_sslStream = newssl;
00196     return newConnection;
00197 } // fn accept
00198 
00199   
00203 void 
00204 SslConnection::close() {
00205     if(isConnected()) {
00206         if (m_sslStream) {
00207             delete m_sslStream;
00208             m_sslStream = NULL;
00209         }
00210         m_connection->close();
00211         debug(DEBUG_SSL, "SSL connection closed");
00212         fireEvent(ConnectionInterface::CONNECTION_CLOSED);
00213     }
00214 } // fn close
00215 
00216 
00220 int 
00221 SslConnection::getStream() {
00222     return m_connection->getStream();
00223 } // fn getStream
00224 
00225 
00229 bool 
00230 SslConnection::isConnected() {
00231     return m_connection->isConnected();
00232 } // fn isConnected
00233 
00234 
00239 bool 
00240 SslConnection::isConnectedTo(IpAddress ipAddr) {
00241     return m_connection->isConnectedTo(ipAddr);
00242 } // fn isConnectedTo
00243 
00244 
00248 void 
00249 SslConnection::setStream(int stream) {
00250     m_connection->setStream(stream);
00251 } // fn setStream
00252 
00253 
00254 SocketAddress* 
00255 SslConnection::getSocketAddress() {
00256     return m_connection->getSocketAddress();
00257 } // fn getSocketAddress
00258 
00259 
00260 void 
00261 SslConnection::setSocketAddress(SocketAddress* socketAddress) {
00262     m_connection->setSocketAddress(socketAddress);
00263 } // fn setSocketAddress
00264 
00265 
00266 void
00267 SslConnection::toStream(std::ostream& out) {
00268     out << *m_connection->getSocketAddress();
00269 } // fn toStream
00270 
00271 

Generated at Thu Jul 11 13:31:52 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001