Added character validation

This commit is contained in:
2016-12-09 22:38:11 +11:00
parent 55abdfc7d0
commit 9c9f30790e
4 changed files with 123 additions and 2 deletions
+1 -1
View File
@@ -154,7 +154,7 @@ void World::Update() {
throw(e);
}
catch(std::exception& e) {
std::cerr << "HandlePacket Error: " << e.what() << std::endl;
std::cerr << "HandlePacket Error (" << (int)(reinterpret_cast<SerialPacket*>(packetBuffer)->type) << "): " << e.what() << std::endl;
}
//free the buffer
+60
View File
@@ -0,0 +1,60 @@
/* Copyright: (c) Kayne Ruse 2016
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#include "file_hash.hpp"
#include <fstream>
//hash a byte array into a 32-bit integer
unsigned fnv_hash_1a_32(void *key, int len) {
unsigned char *p = static_cast<unsigned char*>(key);
unsigned h = 0x811c9dc5;
for (int i = 0; i < len; i++) {
h = ( h ^ p[i] ) * 0x01000193;
}
return h;
}
int getFileHash(std::string fname) {
std::ifstream is(fname, std::ios::in | std::ios::binary);
//if the file doesn't exist, return a hash of -1
if (!is.is_open()) {
return -1;
}
//get the file size
is.seekg(0, std::ios_base::end);
int size = is.tellg();
is.seekg(0);
//create a buffer of that size
char buffer[size];
//load the data
is.read(buffer, size);
//cleanup
is.close();
//finally, return the hash value
return fnv_hash_1a_32(buffer, size);
}
+26
View File
@@ -0,0 +1,26 @@
/* Copyright: (c) Kayne Ruse 2016
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#pragma once
#include <string>
int getFileHash(std::string fname);
+36 -1
View File
@@ -22,6 +22,7 @@
#include "server_application.hpp"
//utility functions
#include "file_hash.hpp"
#include "sql_tools.hpp"
//std & STL
@@ -181,7 +182,7 @@ void ServerApplication::Proc() {
HandlePacket(packetBuffer);
}
catch(std::exception& e) {
std::cerr << "HandlePacket Error: " << e.what() << std::endl;
std::cerr << "HandlePacket Error (" << (int)(packetBuffer->type) << "): " << e.what() << std::endl;
}
//reset the buffer
memset(packetBuffer, 0, MAX_PACKET_SIZE);
@@ -607,8 +608,25 @@ void ServerApplication::hQueryCharacterExists(CharacterPacket* const argPacket)
}
void ServerApplication::hCharacterCreate(CharacterPacket* const argPacket) {
//check to see of the character's files are valid
if (getFileHash(config["dir.sprites"] + argPacket->avatar) == -1) {
//build the error message
std::ostringstream msg;
msg << "Character avatar is invalid on this server: " << argPacket->avatar;
//build & send the packet
TextPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_REJECTION;
strncpy(newPacket.text, msg.str().c_str(), PACKET_STRING_SIZE);
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
return;
}
//create the character
int characterIndex = characterMgr.Create(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
//check to see of the character already exists
if (characterIndex < 0) {
//build the error message
std::ostringstream msg;
@@ -683,8 +701,25 @@ void ServerApplication::hCharacterDelete(CharacterPacket* const argPacket) {
}
void ServerApplication::hCharacterLoad(CharacterPacket* const argPacket) {
//check to see of the character's files are valid
if (getFileHash(config["dir.sprites"] + argPacket->avatar) == -1) {
//build the error message
std::ostringstream msg;
msg << "Character avatar is invalid on this server: " << argPacket->avatar;
//build & send the packet
TextPacket newPacket;
newPacket.type = SerialPacketType::CHARACTER_REJECTION;
strncpy(newPacket.text, msg.str().c_str(), PACKET_STRING_SIZE);
network.SendTo(argPacket->srcAddress, static_cast<SerialPacket*>(&newPacket));
return;
}
//load the character
int characterIndex = characterMgr.Load(argPacket->accountIndex, argPacket->handle, argPacket->avatar);
//check to see if the character is already loaded
if (characterIndex < 0) {
//build the error message
std::ostringstream msg;