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");
92void PPTServer::get_secure_files()
96 if (!found || _cfile.empty()) {
97 string err =
"Unable to determine server certificate file.";
98 throw BESSyntaxUserError(err, __FILE__, __LINE__);
103 if (!found || _cafile.empty()) {
104 string err =
"Unable to determine server certificate authority file.";
105 throw BESSyntaxUserError(err, __FILE__, __LINE__);
110 if (!found || _kfile.empty()) {
111 string err =
"Unable to determine server key file.";
112 throw BESSyntaxUserError(err, __FILE__, __LINE__);
118 if (!found || portstr.empty()) {
119 string err =
"Unable to determine secure connection port.";
120 throw BESSyntaxUserError(err, __FILE__, __LINE__);
122 _securePort = atoi(portstr.c_str());
124 string err = (string)
"Unable to determine secure connection port " +
"from string " + portstr;
125 throw BESSyntaxUserError(err, __FILE__, __LINE__);
136 _mySock = _listener->accept();
139 if (_mySock->allowConnection() ==
true) {
141 BESDEBUG(MODULE, prolog <<
"Calling welcomeClient()" << endl);
142 if (welcomeClient() != -1) {
145 BESDEBUG(MODULE, prolog <<
"number of children: " << get_num_children() << endl);
148 _handler->handle(
this);
158 BESDEBUG(MODULE, prolog <<
"allowConnection() is FALSE! Closing Socket. " << endl);
164void PPTServer::closeConnection()
166 if (_mySock) _mySock->close();
169int PPTServer::welcomeClient()
171 const unsigned int ppt_buffer_size = 64;
172 char inBuff[ppt_buffer_size + 1];
187 int bytesRead =
readBuffer(inBuff, ppt_buffer_size);
189 BESDEBUG(MODULE, prolog <<
"bytesRead: " << bytesRead << endl);
192 if (bytesRead == -1) {
197 string status(inBuff, bytesRead);
199 if (status != PPT_CLIENT_TESTING_CONNECTION) {
205 string err =
"PPT cannot negotiate, client started the connection with " + status;
207 BESDEBUG(MODULE, prolog <<
"Sent '" << err <<
"' to PPT client." << endl);
220 send(PPT_SERVER_CONNECTION_OK);
221 BESDEBUG(MODULE, prolog <<
"Sent " << PPT_SERVER_CONNECTION_OK <<
" to PPT client." << endl);
224 authenticateClient();
230void PPTServer::authenticateClient()
232#if defined HAVE_OPENSSL && defined NOTTHERE
233 BESDEBUG( MODULE, prolog <<
"Requiring secure connection: port = " << _securePort << endl );
235 send(PPT_SERVER_AUTHENTICATE );
240 const unsigned int ppt_buffer_size = 64;
242 char inBuff[ppt_buffer_size];
243 int bytesRead = _mySock->receive( inBuff, ppt_buffer_size );
244 string portRequest( inBuff, bytesRead );
246 if( portRequest != PPT_CLIENT_REQUEST_AUTHPORT )
247 throw BESInternalError(
string(
"Secure connection ... expecting request for port client requested ") + portRequest, __FILE__, __LINE__ );
250 ostringstream portResponse;
251 portResponse << _securePort << PPT_COMPLETE_DATA_TRANSMISSION;
252 send( portResponse.str() );
255 SSLServer server( _securePort, _cfile, _cafile, _kfile );
256 server.initConnection();
257 server.closeConnection();
262 throw BESInternalError(
"Authentication requested for this server but OpenSSL is not built into the server", __FILE__, __LINE__);
274 strm << BESIndent::LMarg <<
"PPTServer::dump - (" << (
void *)
this <<
")" << endl;
277 strm << BESIndent::LMarg <<
"server handler:" << endl;
279 _handler->dump(strm);
280 BESIndent::UnIndent();
283 strm << BESIndent::LMarg <<
"server handler: null" << endl;
286 strm << BESIndent::LMarg <<
"listener:" << endl;
288 _listener->dump(strm);
289 BESIndent::UnIndent();
292 strm << BESIndent::LMarg <<
"listener: null" << endl;
294 strm << BESIndent::LMarg <<
"secure? " << _secure << endl;
297 strm << BESIndent::LMarg <<
"cert file: " << _cfile << endl;
298 strm << BESIndent::LMarg <<
"cert authority file: " << _cafile << endl;
299 strm << BESIndent::LMarg <<
"key file: " << _kfile << endl;
300 strm << BESIndent::LMarg <<
"secure port: " << _securePort << endl;
301 BESIndent::UnIndent();
304 BESIndent::UnIndent();
exception thrown if internal error encountered
void send(const std::string &buffer) override
sends the buffer to the socket
virtual int readBuffer(char *inBuff, const unsigned int buff_size)
read a buffer of data from the socket
void dump(std::ostream &strm) const override
dumps information about this object
void initConnection() override
void dump(std::ostream &strm) const override
dumps information about this object
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
static TheBESKeys * TheKeys()
Access to the singleton.