diff options
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/account-server/accounthandler.cpp | 13 | ||||
-rw-r--r-- | src/account-server/main-account.cpp | 3 | ||||
-rw-r--r-- | src/account-server/serverhandler.cpp | 13 | ||||
-rw-r--r-- | src/chat-server/chathandler.cpp | 3 | ||||
-rw-r--r-- | src/game-server/accountconnection.cpp | 7 | ||||
-rw-r--r-- | src/game-server/gamehandler.cpp | 14 | ||||
-rw-r--r-- | src/game-server/main-game.cpp | 3 | ||||
-rw-r--r-- | src/utils/logger.h | 4 | ||||
-rw-r--r-- | src/utils/tokendispenser.cpp | 38 | ||||
-rw-r--r-- | src/utils/tokendispenser.hpp | 47 |
12 files changed, 129 insertions, 34 deletions
@@ -1,3 +1,13 @@ +2007-03-10 Rogier Polak <rogier.l.a.polak@gmail.com> + + * src/Makefile.am, src/account-server/accounthandler.cpp, + src/account-server/main-account.cpp, + src/account-server/serverhandler.cpp, src/chat-server/chathandler.cpp, + src/game-server/accountconnection.cpp, + src/game-server/gamehandler.cpp, src/game-server/main-game.cpp, + src/utils/tokendispenser.hpp, src/utils/tokendispenser.cpp: Added a + utility function for creating magic_tokens. + 2007-03-05 Rogier Polak <rogier.l.a.polak@gmail.com> * src/defines.h, src/Makefile.am, src/abstractcharacterdata.hpp, @@ -11,7 +21,7 @@ src/account-server/dalstoragesql.hpp, src/account-server/serverhandler.hpp, src/account-server/serverhandler.cpp, src/account-server/storage.hpp - src/game-server/accountconnection.cpp: Added an abstrart base class + src/game-server/accountconnection.cpp: Added an abstract base class for characterdata, in order to use the same serialize and deserialize functions on both the accountserver and the gameserver. Added the CharacterData class to the accountserver, a specialisation of diff --git a/src/Makefile.am b/src/Makefile.am index e4c6d30e..dbc67865 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -57,7 +57,9 @@ tmwserv_account_SOURCES = \ utils/logger.h \ utils/logger.cpp \ utils/stringfilter.h \ - utils/stringfilter.cpp + utils/stringfilter.cpp \ + utils/tokendispenser.hpp \ + utils/tokendispenser.cpp tmwserv_game_SOURCES = \ game-server/main-game.cpp \ @@ -124,6 +126,8 @@ tmwserv_game_SOURCES = \ utils/stringfilter.cpp \ utils/timer.h \ utils/timer.cpp \ + utils/tokendispenser.hpp \ + utils/tokendispenser.cpp \ utils/xml.hpp \ utils/xml.cpp \ utils/zlib.hpp \ diff --git a/src/account-server/accounthandler.cpp b/src/account-server/accounthandler.cpp index d5d5d35f..2b1f2ff0 100644 --- a/src/account-server/accounthandler.cpp +++ b/src/account-server/accounthandler.cpp @@ -38,8 +38,7 @@ #include "net/netcomputer.hpp" #include "utils/logger.h" #include "utils/stringfilter.h" - -// TODO: Implement a class to handle these tokens more generally +#include "utils/tokendispenser.hpp" typedef std::map< std::string, AccountClient* > AccountPendingClients; typedef std::map< std::string, int > AccountPendingReconnects; @@ -226,12 +225,8 @@ AccountHandler::processMessage(NetComputer *comp, MessageIn &message) LOG_DEBUG(selectedChar->getName() << " is trying to enter the servers."); - std::string magic_token(32, ' '); - for (int i = 0; i < 32; ++i) { - magic_token[i] = - 1 + (int) (127 * (rand() / (RAND_MAX + 1.0))); - } - result.writeString(magic_token, 32); + std::string magic_token(utils::getMagicToken()); + result.writeString(magic_token, MAGIC_TOKEN_LENGTH); result.writeString(address); result.writeShort(port); // TODO: get correct address and port for the chat server @@ -390,7 +385,7 @@ AccountHandler::handleReconnectMessage(AccountClient &computer, MessageIn &msg) { if (computer.getAccount().get() == NULL) { - std::string magic_token = msg.readString(32); + std::string magic_token = msg.readString(MAGIC_TOKEN_LENGTH); AccountPendingReconnects::iterator i = pendingReconnects.find(magic_token); if (i == pendingReconnects.end()) diff --git a/src/account-server/main-account.cpp b/src/account-server/main-account.cpp index 6e2ef77a..209e4a24 100644 --- a/src/account-server/main-account.cpp +++ b/src/account-server/main-account.cpp @@ -175,6 +175,9 @@ void initialize() config.setValue("dbuser", ""); config.setValue("dbpass", ""); config.setValue("dbhost", ""); + + //Seed the random number generator + std::srand( time(NULL) ); } diff --git a/src/account-server/serverhandler.cpp b/src/account-server/serverhandler.cpp index 9a6dd667..d2159d44 100644 --- a/src/account-server/serverhandler.cpp +++ b/src/account-server/serverhandler.cpp @@ -31,6 +31,7 @@ #include "net/messageout.hpp" #include "net/netcomputer.hpp" #include "utils/logger.h" +#include "utils/tokendispenser.hpp" extern void registerAccountReconnect(int accountID, const std::string &magic_token); @@ -79,7 +80,7 @@ void ServerHandler::registerGameClient(std::string const &token, CharacterPtr pt unsigned mapId = ptr->getMapId(); MessageOut msg(AGMSG_PLAYER_ENTER); - msg.writeString(token, 32); + msg.writeString(token, MAGIC_TOKEN_LENGTH); ptr->serialize(msg); //Characterdata Servers::const_iterator i = servers.find(mapId); @@ -138,11 +139,7 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) { LOG_DEBUG("GAMSG_REDIRECT"); int id = msg.readLong(); - std::string magic_token(32, ' '); - for (int i = 0; i < 32; ++i) - { - magic_token[i] = 1 + (int)(127 * (rand() / (RAND_MAX + 1.0))); - } + std::string magic_token(utils::getMagicToken()); Storage &store = Storage::instance("tmw"); CharacterPtr ptr = store.getCharacter(id); std::string address; @@ -153,7 +150,7 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) registerGameClient(magic_token, ptr); result.writeShort(AGMSG_REDIRECT_RESPONSE); result.writeLong(ptr->getDatabaseID()); - result.writeString(magic_token, 32); + result.writeString(magic_token, MAGIC_TOKEN_LENGTH); result.writeString(address); result.writeShort(port); } @@ -168,7 +165,7 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) { LOG_DEBUG("GAMSG_PLAYER_RECONNECT"); int characterID = msg.readLong(); - std::string magic_token = msg.readString(32); + std::string magic_token = msg.readString(MAGIC_TOKEN_LENGTH); Storage &store = Storage::instance("tmw"); CharacterPtr ptr = store.getCharacter(characterID); diff --git a/src/chat-server/chathandler.cpp b/src/chat-server/chathandler.cpp index d5b84534..75c8d915 100644 --- a/src/chat-server/chathandler.cpp +++ b/src/chat-server/chathandler.cpp @@ -30,6 +30,7 @@ #include "net/netcomputer.hpp" #include "utils/logger.h" #include "utils/stringfilter.h" +#include "utils/tokendispenser.hpp" class ChatClient: public NetComputer @@ -141,7 +142,7 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message) if (computer.characterName.empty()) { if (message.getId() != PCMSG_CONNECT) return; - std::string magic_token = message.readString(32); + std::string magic_token = message.readString(MAGIC_TOKEN_LENGTH); ChatPendingLogins::iterator i = pendingLogins.find(magic_token); if (i == pendingLogins.end()) { diff --git a/src/game-server/accountconnection.cpp b/src/game-server/accountconnection.cpp index 2c3fc85b..1178838c 100644 --- a/src/game-server/accountconnection.cpp +++ b/src/game-server/accountconnection.cpp @@ -30,6 +30,7 @@ #include "net/messagein.hpp" #include "net/messageout.hpp" #include "utils/logger.h" +#include "utils/tokendispenser.hpp" extern void registerGameClient(std::string const &, Player *); @@ -67,7 +68,7 @@ void AccountConnection::processMessage(MessageIn &msg) { case AGMSG_PLAYER_ENTER: { - std::string token = msg.readString(32); + std::string token = msg.readString(MAGIC_TOKEN_LENGTH); int id = msg.readLong(); std::string name = msg.readString(); Player *ptr = new Player(name, id); @@ -87,7 +88,7 @@ void AccountConnection::processMessage(MessageIn &msg) case AGMSG_REDIRECT_RESPONSE: { int id = msg.readLong(); - std::string token = msg.readString(32); + std::string token = msg.readString(MAGIC_TOKEN_LENGTH); std::string address = msg.readString(); int port = msg.readShort(); gameHandler->completeServerChange(id, token, address, port); @@ -104,7 +105,7 @@ void AccountConnection::playerReconnectAccount(int id, const std::string magic_t LOG_INFO("Send GAMSG_PLAYER_RECONNECT."); MessageOut msg(GAMSG_PLAYER_RECONNECT); msg.writeLong(id); - msg.writeString(magic_token, 32); + msg.writeString(magic_token, MAGIC_TOKEN_LENGTH); send(msg); } diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp index 6b11f542..d3da0526 100644 --- a/src/game-server/gamehandler.cpp +++ b/src/game-server/gamehandler.cpp @@ -36,6 +36,7 @@ #include "net/messageout.hpp" #include "net/netcomputer.hpp" #include "utils/logger.h" +#include "utils/tokendispenser.hpp" enum { @@ -167,7 +168,7 @@ void GameHandler::completeServerChange(int id, std::string const &token, c->character->getDatabaseID() == id) { MessageOut msg(GPMSG_PLAYER_SERVER_CHANGE); - msg.writeString(token, 32); + msg.writeString(token, MAGIC_TOKEN_LENGTH); msg.writeString(address); msg.writeShort(port); c->send(msg); @@ -207,7 +208,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) if (computer.status == CLIENT_LOGIN) { if (message.getId() != PGMSG_CONNECT) return; - std::string magic_token = message.readString(32); + std::string magic_token = message.readString(MAGIC_TOKEN_LENGTH); GamePendingLogins::iterator i = pendingLogins.find(magic_token); if (i == pendingLogins.end()) { @@ -336,13 +337,8 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) if (reconnectAccount) { - LOG_INFO("Making a magic_token."); - std::string magic_token(32, ' '); - for (int i = 0; i < 32; ++i) { - magic_token[i] = - 1 + (int) (127 * (rand() / (RAND_MAX + 1.0))); - } - result.writeString(magic_token, 32); + std::string magic_token(utils::getMagicToken()); + result.writeString(magic_token, MAGIC_TOKEN_LENGTH); //No accountserver data, the client should remember that accountHandler->playerReconnectAccount( computer.character->getDatabaseID(), diff --git a/src/game-server/main-game.cpp b/src/game-server/main-game.cpp index da1b60f2..a63dc585 100644 --- a/src/game-server/main-game.cpp +++ b/src/game-server/main-game.cpp @@ -210,6 +210,9 @@ void initialize() // Pre-calulate the needed trigomic function values utils::math::init(); + + //Seed the random number generator + std::srand( time(NULL) ); } diff --git a/src/utils/logger.h b/src/utils/logger.h index a7f7c8f6..29147b46 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -158,8 +158,8 @@ class Logger #define LOG(level, msg) \ do if (::utils::Logger::mVerbosity >= ::utils::Logger::level) { \ - std::ostringstream os; \ - os << msg; \ + std::ostringstream os; \ + os << msg; \ ::utils::Logger::output(os.str(), ::utils::Logger::level); \ } while (0) diff --git a/src/utils/tokendispenser.cpp b/src/utils/tokendispenser.cpp new file mode 100644 index 00000000..50148144 --- /dev/null +++ b/src/utils/tokendispenser.cpp @@ -0,0 +1,38 @@ +/* + * The Mana World Server + * Copyright 2007 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "utils/tokendispenser.hpp" + +#include <cstdlib> + +std::string utils::getMagicToken() +{ + std::string magic_token(MAGIC_TOKEN_LENGTH, ' '); + + for (int i = 0; i < MAGIC_TOKEN_LENGTH; ++i) + { + // A random integer with uniform distribution in the interval [1, 127] + magic_token[i] = 1 + std::rand() % 127; + } + + return magic_token; +} diff --git a/src/utils/tokendispenser.hpp b/src/utils/tokendispenser.hpp new file mode 100644 index 00000000..a6f0b3ee --- /dev/null +++ b/src/utils/tokendispenser.hpp @@ -0,0 +1,47 @@ +/* + * The Mana World Server + * Copyright 2007 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or any later version. + * + * The Mana World is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with The Mana World; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMWSERV_TOKENDISPENSER_HPP +#define _TMWSERV_TOKENDISPENSER_HPP + +#define MAGIC_TOKEN_LENGTH 32 + +#include <string> + +namespace utils +{ + /** + * \brief Returns a magic_token. + * + * The tokens are used for spanning a user's session across multiple + * servers. + * NOTE: Uniqueness is not guaranteed, store the account- or characterId + * with the token if that is an issue. + * NOTE: Not passed-by-reference by design. + * NOTE: Store the token in a variable in this namespace if you want to + * avoid 1 copy operation per use. + */ + std::string getMagicToken(); + +} // namespace utils + +#endif // _TMWSERV_TOKENDISPENSER_HPP |