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),
69 _securePort(0), d_num_children(0)
72 string err(
"Null handler passed to PPTServer");
76 string err(
"Null listener passed to PPTServer");
79#if !defined HAVE_OPENSSL && defined NOTTHERE
82 string err(
"Server requested to be secure but OpenSSL is not built in");
93PPTServer::~PPTServer()
97void PPTServer::get_secure_files()
101 if (!found || _cfile.empty()) {
102 string err =
"Unable to determine server certificate file.";
108 if (!found || _cafile.empty()) {
109 string err =
"Unable to determine server certificate authority file.";
115 if (!found || _kfile.empty()) {
116 string err =
"Unable to determine server key file.";
123 if (!found || portstr.empty()) {
124 string err =
"Unable to determine secure connection port.";
127 _securePort = atoi(portstr.c_str());
129 string err = (string)
"Unable to determine secure connection port " +
"from string " + portstr;
141 _mySock = _listener->
accept();
144 if (_mySock->allowConnection() ==
true) {
146 BESDEBUG(MODULE, prolog <<
"Calling welcomeClient()" << endl);
147 if (welcomeClient() != -1) {
150 BESDEBUG(MODULE, prolog <<
"number of children: " << get_num_children() << endl);
153 _handler->handle(
this);
163 BESDEBUG(MODULE, prolog <<
"allowConnection() is FALSE! Closing Socket. " << endl);
169void PPTServer::closeConnection()
171 if (_mySock) _mySock->close();
174int PPTServer::welcomeClient()
176 const unsigned int ppt_buffer_size = 64;
177 char inBuff[ppt_buffer_size + 1];
192 int bytesRead =
readBuffer(inBuff, ppt_buffer_size);
194 BESDEBUG(MODULE, prolog <<
"bytesRead: " << bytesRead << endl);
197 if (bytesRead == -1) {
202 string status(inBuff, bytesRead);
204 if (status != PPT_CLIENT_TESTING_CONNECTION) {
210 string err =
"PPT cannot negotiate, client started the connection with " + status;
212 BESDEBUG(MODULE, prolog <<
"Sent '" << err <<
"' to PPT client." << endl);
225 send(PPT_SERVER_CONNECTION_OK);
226 BESDEBUG(MODULE, prolog <<
"Sent " << PPT_SERVER_CONNECTION_OK <<
" to PPT client." << endl);
229 authenticateClient();
235void PPTServer::authenticateClient()
237#if defined HAVE_OPENSSL && defined NOTTHERE
238 BESDEBUG( MODULE, prolog <<
"Requiring secure connection: port = " << _securePort << endl );
240 send(PPT_SERVER_AUTHENTICATE );
245 const unsigned int ppt_buffer_size = 64;
247 char inBuff[ppt_buffer_size];
248 int bytesRead = _mySock->receive( inBuff, ppt_buffer_size );
249 string portRequest( inBuff, bytesRead );
251 if( portRequest != PPT_CLIENT_REQUEST_AUTHPORT )
252 throw BESInternalError(
string(
"Secure connection ... expecting request for port client requested ") + portRequest, __FILE__, __LINE__ );
255 ostringstream portResponse;
256 portResponse << _securePort << PPT_COMPLETE_DATA_TRANSMISSION;
257 send( portResponse.str() );
260 SSLServer server( _securePort, _cfile, _cafile, _kfile );
261 server.initConnection();
262 server.closeConnection();
267 throw BESInternalError(
"Authentication requested for this server but OpenSSL is not built into the server", __FILE__, __LINE__);
279 strm << BESIndent::LMarg <<
"PPTServer::dump - (" << (
void *)
this <<
")" << endl;
282 strm << BESIndent::LMarg <<
"server handler:" << endl;
284 _handler->
dump(strm);
285 BESIndent::UnIndent();
288 strm << BESIndent::LMarg <<
"server handler: null" << endl;
291 strm << BESIndent::LMarg <<
"listener:" << endl;
293 _listener->
dump(strm);
294 BESIndent::UnIndent();
297 strm << BESIndent::LMarg <<
"listener: null" << endl;
299 strm << BESIndent::LMarg <<
"secure? " << _secure << endl;
302 strm << BESIndent::LMarg <<
"cert file: " << _cfile << endl;
303 strm << BESIndent::LMarg <<
"cert authority file: " << _cafile << endl;
304 strm << BESIndent::LMarg <<
"key file: " << _kfile << endl;
305 strm << BESIndent::LMarg <<
"secure port: " << _securePort << endl;
306 BESIndent::UnIndent();
309 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
virtual void initConnection()
virtual void dump(std::ostream &strm) const
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()