41 using std::ostringstream ;
44 #include "PPTClient.h"
45 #include "TcpSocket.h"
46 #include "UnixSocket.h"
47 #include "PPTProtocol.h"
48 #include "BESInternalError.h"
49 #include "BESSyntaxUserError.h"
50 #include "TheBESKeys.h"
53 #if defined HAVE_OPENSSL && defined NOTTHERE
54 #include "SSLClient.h"
57 PPTClient::PPTClient(
const string &hostStr,
int portVal,
int timeout )
64 _mySock =
new TcpSocket( hostStr, portVal ) ;
66 _connected = _mySock->isConnected();
69 PPTClient::PPTClient(
const string &unix_socket,
int timeout )
80 PPTClient::get_secure_files()
84 if( !found || _cfile.empty() )
86 string err =
"Unable to determine client certificate file." ;
92 if( !found || _cafile.empty() )
94 string err =
"Unable to determine client certificate authority file." ;
100 if( !found || _kfile.empty() )
102 string err =
"Unable to determine client key file." ;
107 PPTClient::~PPTClient()
121 PPTClient::initConnection()
125 send( PPTProtocol::PPTCLIENT_TESTING_CONNECTION ) ;
129 string msg =
"Failed to initialize connection to server\n" ;
136 const int ppt_buffer_size = 64 ;
137 char *inBuff =
new char[ppt_buffer_size+1] ;
142 string err =
"Could not connect to server, server may be down or busy" ;
146 if( bytesRead > ppt_buffer_size )
147 bytesRead = ppt_buffer_size ;
148 inBuff[bytesRead] =
'\0' ;
149 string status( inBuff, 0, bytesRead ) ;
152 if( status == PPTProtocol::PPT_PROTOCOL_UNDEFINED )
154 string err =
"Could not connect to server, server may be down or busy" ;
158 if( status == PPTProtocol::PPTSERVER_AUTHENTICATE )
160 authenticateWithServer() ;
162 else if( status != PPTProtocol::PPTSERVER_CONNECTION_OK )
164 string err =
"Server reported an invalid connection, \""
171 PPTClient::authenticateWithServer()
173 #if defined HAVE_OPENSSL && defined NOTTHERE
178 send( PPTProtocol::PPTCLIENT_REQUEST_AUTHPORT ) ;
183 const int ppt_buffer_size = 64 ;
184 char *inBuff =
new char[ppt_buffer_size+1] ;
189 string err =
"Expecting secure port number response" ;
193 if( bytesRead > ppt_buffer_size )
195 bytesRead = ppt_buffer_size ;
197 inBuff[bytesRead] =
'\0' ;
198 ostringstream portResponse( inBuff ) ;
201 int portVal = atoi( portResponse.str().c_str() ) ;
204 string err =
"Expecting valid secure port number response" ;
209 SSLClient client( _host, portVal, _cfile, _cafile, _kfile ) ;
210 client.initConnection() ;
211 client.closeConnection() ;
216 throw BESInternalError(
"Server has requested authentication, but OpenSSL is not built into this client", __FILE__, __LINE__ ) ;
221 PPTClient::closeConnection()
233 cerr <<
"Failed to inform server that the client is exiting, "
234 <<
"continuing" << endl ;
242 _brokenPipe = false ;
255 strm << BESIndent::LMarg <<
"PPTClient::dump - ("
256 << (
void *)
this <<
")" << endl ;
257 BESIndent::Indent() ;
258 strm << BESIndent::LMarg <<
"connected? " << _connected << endl ;
259 strm << BESIndent::LMarg <<
"host: " << _host << endl ;
261 BESIndent::UnIndent() ;