SSLConnection.cc

Go to the documentation of this file.
00001 // SSLConnection.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #include <openssl/ssl.h>
00034 #include <openssl/err.h>
00035 #include <sys/socket.h>
00036 #include <netinet/in.h>
00037 #include <arpa/inet.h>
00038 #include <netdb.h>
00039 
00040 #include <iostream>
00041 
00042 using std::flush ;
00043 
00044 #include "SSLConnection.h"
00045 #include "PPTException.h"
00046 
00047 SSLConnection::SSLConnection( )
00048     : _method( NULL ),
00049       _context( NULL ),
00050       _connection( NULL ),
00051       _connected( false )
00052 {
00053 }
00054     
00055 SSLConnection::~SSLConnection()
00056 {
00057 }
00058 
00059 void
00060 SSLConnection::closeConnection()
00061 {
00062     if( _connected && _connection )
00063     {
00064         if( SSL_shutdown( _connection ) == 0 )
00065         {
00066             SSL_shutdown( _connection ) ;
00067         }
00068     }
00069     SSL_clear( _connection ) ;
00070 
00071     if( _context ) SSL_CTX_free( _context ) ; _context = NULL ;
00072     _connected = false ;
00073 
00074     SSL_free( _connection ) ;
00075     _connection = NULL ;
00076 }
00077 
00078 void
00079 SSLConnection::send( const string &buf )
00080 {
00081     if( _connected )
00082     {
00083         int len = SSL_write( _connection, (void *)buf.c_str(), buf.length() ) ;
00084         if( len <= 0 )
00085         {
00086             string msg = "FAILED to write to SSL connection\n" ;
00087             msg += ERR_error_string( ERR_get_error(), NULL ) ;
00088             throw PPTException( msg ) ;
00089         }
00090     }
00091 }
00092 
00093 bool
00094 SSLConnection::receive( ostream *strm )
00095 {
00096     bool isDone = false ;
00097     if( _connected )
00098     {
00099         char retbuf[1024] ;
00100         int retlen = SSL_read( _connection, (void *)retbuf, 1024 ) ;
00101         if( retlen <= 0 )
00102         {
00103             if( retlen == 0 )
00104             {
00105                 isDone = true ;
00106             }
00107             else
00108             {
00109                 string msg = "FAILED to read from SSL connection\n" ;
00110                 msg += ERR_error_string( ERR_get_error(), NULL ) ;
00111                 throw PPTException( msg ) ;
00112             }
00113         }
00114         else
00115         {
00116             retbuf[retlen] = '\0' ;
00117             *strm << retbuf << flush ;
00118         }
00119     }
00120 
00121     return isDone ;
00122 }
00123 
00130 void
00131 SSLConnection::dump( ostream &strm ) const
00132 {
00133     strm << BESIndent::LMarg << "SSLConnection::dump - ("
00134                              << (void *)this << ")" << endl ;
00135     BESIndent::Indent() ;
00136     strm << BESIndent::LMarg << "ssl method: " << (void *)_method << endl ;
00137     strm << BESIndent::LMarg << "ssl context: " << (void *)_context << endl ;
00138     strm << BESIndent::LMarg << "ssl connection: " << (void *)_connection << endl ;
00139     strm << BESIndent::LMarg << "is connected? " << (void *)_connected << endl ;
00140     Connection::dump( strm ) ;
00141     BESIndent::UnIndent() ;
00142 }
00143 

Generated on Wed Aug 29 03:14:17 2007 for OPeNDAP Back End Server (BES) by  doxygen 1.5.2