OPeNDAP Hyrax Back End Server (BES)
Updated for version 3.8.3
|
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-2009 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 "BESInternalError.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 BESInternalError( msg, __FILE__, __LINE__ ) ; 00089 } 00090 } 00091 } 00092 00099 void 00100 SSLConnection::dump( ostream &strm ) const 00101 { 00102 strm << BESIndent::LMarg << "SSLConnection::dump - (" 00103 << (void *)this << ")" << endl ; 00104 BESIndent::Indent() ; 00105 strm << BESIndent::LMarg << "ssl method: " << (void *)_method << endl ; 00106 strm << BESIndent::LMarg << "ssl context: " << (void *)_context << endl ; 00107 strm << BESIndent::LMarg << "ssl connection: " << (void *)_connection << endl ; 00108 strm << BESIndent::LMarg << "is connected? " << (void *)_connected << endl ; 00109 Connection::dump( strm ) ; 00110 BESIndent::UnIndent() ; 00111 } 00112