OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
SSLConnection.cc
Go to the documentation of this file.
1 // SSLConnection.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <openssl/ssl.h>
34 #include <openssl/err.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <netdb.h>
39 
40 #include <iostream>
41 
42 using std::flush ;
43 
44 #include "SSLConnection.h"
45 #include "BESInternalError.h"
46 
48  : _method( NULL ),
49  _context( NULL ),
50  _connection( NULL ),
51  _connected( false )
52 {
53 }
54 
56 {
57 }
58 
59 void
61 {
62  if( _connected && _connection )
63  {
64  if( SSL_shutdown( _connection ) == 0 )
65  {
66  SSL_shutdown( _connection ) ;
67  }
68  }
69  SSL_clear( _connection ) ;
70 
71  if( _context ) SSL_CTX_free( _context ) ; _context = NULL ;
72  _connected = false ;
73 
74  SSL_free( _connection ) ;
75  _connection = NULL ;
76 }
77 
78 void
79 SSLConnection::send( const string &buf )
80 {
81  if( _connected )
82  {
83  int len = SSL_write( _connection, (void *)buf.c_str(), buf.length() ) ;
84  if( len <= 0 )
85  {
86  string msg = "FAILED to write to SSL connection\n" ;
87  msg += ERR_error_string( ERR_get_error(), NULL ) ;
88  throw BESInternalError( msg, __FILE__, __LINE__ ) ;
89  }
90  }
91 }
92 
99 void
100 SSLConnection::dump( ostream &strm ) const
101 {
102  strm << BESIndent::LMarg << "SSLConnection::dump - ("
103  << (void *)this << ")" << endl ;
105  strm << BESIndent::LMarg << "ssl method: " << (void *)_method << endl ;
106  strm << BESIndent::LMarg << "ssl context: " << (void *)_context << endl ;
107  strm << BESIndent::LMarg << "ssl connection: " << (void *)_connection << endl ;
108  strm << BESIndent::LMarg << "is connected? " << (void *)_connected << endl ;
109  Connection::dump( strm ) ;
111 }
112 
virtual void dump(ostream &strm) const
dumps information about this object
exception thrown if inernal error encountered
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Connection.cc:42
static void Indent()
Definition: BESIndent.cc:38
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
SSL_CTX * _context
Definition: SSLConnection.h:50
SSL_METHOD * _method
Definition: SSLConnection.h:49
virtual void send(const string &buffer)
virtual void closeConnection()
static void UnIndent()
Definition: BESIndent.cc:44