41#include "ServerExitConditions.h"
42#include "BESInternalError.h"
43#include "BESInternalFatalError.h"
44#include "BESSyntaxUserError.h"
45#include "PPTProtocolNames.h"
46#include "SocketListener.h"
47#include "ServerHandler.h"
49#include "TheBESKeys.h"
54using std::ostringstream;
59#if defined HAVE_OPENSSL && defined NOTTHERE
63#define prolog string("PPTServer::").append(__func__).append("() - ")
65#define PPT_SERVER_DEFAULT_TIMEOUT 1
68 PPTConnection(PPT_SERVER_DEFAULT_TIMEOUT), _handler(handler), _listener(listener), _secure(isSecure)
71 string err(
"Null handler passed to PPTServer");
75 string err(
"Null listener passed to PPTServer");
78#if !defined HAVE_OPENSSL && defined NOTTHERE
81 string err(
"Server requested to be secure but OpenSSL is not built in");
93PPTServer::~PPTServer()
98void PPTServer::get_secure_files()
102 if (!found || _cfile.empty()) {
103 string err =
"Unable to determine server certificate file.";
109 if (!found || _cafile.empty()) {
110 string err =
"Unable to determine server certificate authority file.";
116 if (!found || _kfile.empty()) {
117 string err =
"Unable to determine server key file.";
124 if (!found || portstr.empty()) {
125 string err =
"Unable to determine secure connection port.";
128 _securePort = atoi(portstr.c_str());
130 string err = (string)
"Unable to determine secure connection port " +
"from string " + portstr;
142 _mySock = _listener->
accept();
145 if (_mySock->allowConnection() ==
true) {
147 BESDEBUG(MODULE, prolog <<
"Calling welcomeClient()" << endl);
148 if (welcomeClient() != -1) {
151 BESDEBUG(MODULE, prolog <<
"number of children: " << get_num_children() << endl);
154 _handler->handle(
this);
164 BESDEBUG(MODULE, prolog <<
"allowConnection() is FALSE! Closing Socket. " << endl);
170void PPTServer::closeConnection()
172 if (_mySock) _mySock->close();
175int PPTServer::welcomeClient()
177 const unsigned int ppt_buffer_size = 64;
178 char inBuff[ppt_buffer_size + 1];
193 int bytesRead =
readBuffer(inBuff, ppt_buffer_size);
195 BESDEBUG(MODULE, prolog <<
"bytesRead: " << bytesRead << endl);
198 if (bytesRead == -1) {
203 string status(inBuff, bytesRead);
205 if (status != PPT_CLIENT_TESTING_CONNECTION) {
211 string err =
"PPT cannot negotiate, client started the connection with " + status;
213 BESDEBUG(MODULE, prolog <<
"Sent '" << err <<
"' to PPT client." << endl);
226 send(PPT_SERVER_CONNECTION_OK);
227 BESDEBUG(MODULE, prolog <<
"Sent " << PPT_SERVER_CONNECTION_OK <<
" to PPT client." << endl);
230 authenticateClient();
236void PPTServer::authenticateClient()
238#if defined HAVE_OPENSSL && defined NOTTHERE
239 BESDEBUG( MODULE, prolog <<
"Requiring secure connection: port = " << _securePort << endl );
241 send(PPT_SERVER_AUTHENTICATE );
246 const unsigned int ppt_buffer_size = 64;
248 char inBuff[ppt_buffer_size];
249 int bytesRead = _mySock->receive( inBuff, ppt_buffer_size );
250 string portRequest( inBuff, bytesRead );
252 if( portRequest != PPT_CLIENT_REQUEST_AUTHPORT )
253 throw BESInternalError(
string(
"Secure connection ... expecting request for port client requested ") + portRequest, __FILE__, __LINE__ );
256 ostringstream portResponse;
257 portResponse << _securePort << PPT_COMPLETE_DATA_TRANSMISSION;
258 send( portResponse.str() );
261 SSLServer server( _securePort, _cfile, _cafile, _kfile );
262 server.initConnection();
263 server.closeConnection();
268 throw BESInternalError(
"Authentication requested for this server but OpenSSL is not built into the server", __FILE__, __LINE__);
280 strm << BESIndent::LMarg <<
"PPTServer::dump - (" << (
void *)
this <<
")" << endl;
283 strm << BESIndent::LMarg <<
"server handler:" << endl;
285 _handler->
dump(strm);
286 BESIndent::UnIndent();
289 strm << BESIndent::LMarg <<
"server handler: null" << endl;
292 strm << BESIndent::LMarg <<
"listener:" << endl;
294 _listener->
dump(strm);
295 BESIndent::UnIndent();
298 strm << BESIndent::LMarg <<
"listener: null" << endl;
300 strm << BESIndent::LMarg <<
"secure? " << _secure << endl;
303 strm << BESIndent::LMarg <<
"cert file: " << _cfile << endl;
304 strm << BESIndent::LMarg <<
"cert authority file: " << _cafile << endl;
305 strm << BESIndent::LMarg <<
"key file: " << _kfile << endl;
306 strm << BESIndent::LMarg <<
"secure port: " << _securePort << endl;
307 BESIndent::UnIndent();
310 BESIndent::UnIndent();
exception thrown if internal error encountered
error thrown if there is a user syntax error in the request or any other user error
virtual int readBuffer(char *inBuff, const unsigned int buff_size)
read a buffer of data from the socket
virtual void send(const std::string &buffer)
sends the buffer to the socket
virtual void dump(std::ostream &strm) const
dumps information about this object
void initConnection() override
void dump(std::ostream &strm) const override
dumps information about this object
virtual void dump(std::ostream &strm) const =0
dump the contents of this object to the specified ostream
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual Socket * accept()
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()