ServerConnectHandler.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <server/ServerConnectHandler.h>
00022 #include <server/ScorchedServer.h>
00023 #include <server/ServerCommon.h>
00024 #include <server/ServerAuthHandler.h>
00025 #include <server/ScorchedServerUtil.h>
00026 #include <common/Logger.h>
00027 #include <common/OptionsScorched.h>
00028 #include <tank/TankContainer.h>
00029 #include <coms/ComsConnectAuthMessage.h>
00030 #include <coms/ComsConnectMessage.h>
00031 #include <coms/ComsMessageSender.h>
00032 
00033 ServerConnectHandler *ServerConnectHandler::instance_ = 0;
00034 
00035 ServerConnectHandler *ServerConnectHandler::instance()
00036 {
00037         if (!instance_)
00038         {
00039                 instance_ = new ServerConnectHandler;
00040         }
00041         return instance_;
00042 }
00043 
00044 ServerConnectHandler::ServerConnectHandler()
00045 {
00046         ScorchedServer::instance()->getComsMessageHandler().addHandler(
00047                 "ComsConnectMessage",
00048                 this);
00049 }
00050 
00051 ServerConnectHandler::~ServerConnectHandler()
00052 {
00053 }
00054 
00055 bool ServerConnectHandler::processMessage(
00056         NetMessage &netMessage,
00057         const char *messageType, NetBufferReader &reader)
00058 {
00059         unsigned int destinationId = netMessage.getDestinationId();
00060         unsigned int ipAddress = netMessage.getIpAddress();
00061 
00062         // Check for acceptance bassed on standard checks
00063         if (!checkStandardParams(destinationId, ipAddress)) return true;
00064 
00065         // Decode the connect message
00066         ComsConnectMessage message;
00067 
00068         // Check the player protocol versions are the same (correct)
00069         if (!message.readMessage(reader) ||
00070                 (0 != strcmp(message.getProtocolVersion(), S3D::ScorchedProtocolVersion.c_str())))
00071         {
00072                 std::string kickMessage = 
00073                         S3D::formatStringBuffer(
00074                         "--------------------------------------------------\n"
00075                         "The version of Scorched you are running\n"
00076                         "does not match the server's version.\n"
00077                         "This server is running Scorched build %s (%s).\n"
00078                         "You are running Scorched build %s (%s).\n"
00079                         "New versions can be downloaded from\n"
00080                         "http://www.scorched3d.co.uk\n"
00081                         "Connection failed.\n"
00082                         "--------------------------------------------------", 
00083                         S3D::ScorchedVersion.c_str(), S3D::ScorchedProtocolVersion.c_str(),
00084                         message.getVersion(), message.getProtocolVersion());
00085                 Logger::log( 
00086                         S3D::formatStringBuffer(
00087                         "Player connected with out of date version \"%s(%s)\"",
00088                         message.getVersion(),
00089                         message.getProtocolVersion()));
00090 
00091                 ServerCommon::kickDestination(destinationId, kickMessage);
00092                 return true;
00093         }
00094 
00095         // Tell the client to continue auth connection
00096         ComsConnectAuthMessage comsConnectAuthMessage;
00097         ServerAuthHandler *authHandler = ScorchedServerUtil::instance()->getAuthHandler();
00098         if (authHandler)
00099         {
00100                 authHandler->createAuthentication(comsConnectAuthMessage);
00101         }
00102         if (!ComsMessageSender::sendToSingleClient(
00103                 comsConnectAuthMessage, destinationId)) return false;
00104 
00105         return true;
00106 }
00107 
00108 bool ServerConnectHandler::checkStandardParams(unsigned int destinationId, unsigned int ipAddress)
00109 {
00110         // Only do this on the server, the client can have all bots
00111 #ifdef S3D_SERVER
00112         {
00113                 // First things, first
00114                 // Check we can actually accept the connection
00115                 if (ScorchedServer::instance()->getOptionsGame().getNoMaxPlayers() <=
00116                         ScorchedServer::instance()->getTankContainer().getNoOfTanks())
00117                 {
00118                         const char *kickMessage =
00119                                 "--------------------------------------------------\n"
00120                                 "This server is full, you cannot join!\n"
00121                                 "--------------------------------------------------";
00122                         ServerCommon::serverLog("Server full, kicking");
00123                         ServerCommon::kickDestination(destinationId, kickMessage);
00124                         return false;           
00125                 }
00126         }
00127 #endif // S3D_SERVER
00128 
00129         // Check if this ip address has been banned
00130         ServerBanned::BannedType type = 
00131                 ScorchedServerUtil::instance()->bannedPlayers.getBanned(ipAddress);
00132         if (type == ServerBanned::Banned)
00133         {
00134                 Logger::log(S3D::formatStringBuffer("Banned ipaddress connection from destination \"%i\"", 
00135                         destinationId));
00136                 ServerCommon::kickDestination(destinationId);
00137                 return false;
00138         }
00139 
00140         return true;
00141 }
00142 

Generated on Mon Feb 16 15:14:53 2009 for Scorched3D by  doxygen 1.5.3