Include protobuf. Not sure if we're supposed to commit protoc :S

This commit is contained in:
momo5502 2016-02-10 14:54:50 +01:00
parent 029c1e4c7d
commit 75e76226d1
10 changed files with 78 additions and 70 deletions

View File

@ -1,2 +1,3 @@
@echo off @echo off
call tools\protogen.bat
premake5 %* vs2015 premake5 %* vs2015

View File

@ -1,4 +1,3 @@
-- Option to allow copying the DLL file to a custom folder after build -- Option to allow copying the DLL file to a custom folder after build
newoption { newoption {
trigger = "copy-to", trigger = "copy-to",
@ -36,7 +35,7 @@ newaction {
-- get old version number from version.hpp if any -- get old version number from version.hpp if any
local oldRevNumber = "(none)" local oldRevNumber = "(none)"
local oldVersionHeader = io.open(wks.location .. "/version.hpp", "r") local oldVersionHeader = io.open(wks.location .. "/src/version.hpp", "r")
if oldVersionHeader ~=nil then if oldVersionHeader ~=nil then
local oldVersionHeaderContent = assert(oldVersionHeader:read('*a')) local oldVersionHeaderContent = assert(oldVersionHeader:read('*a'))
oldRevNumber = string.match(oldVersionHeaderContent, "#define REVISION (%d+)") oldRevNumber = string.match(oldVersionHeaderContent, "#define REVISION (%d+)")
@ -49,7 +48,7 @@ newaction {
-- generate version.hpp with a revision number if not equal -- generate version.hpp with a revision number if not equal
if oldRevNumber ~= revNumber then if oldRevNumber ~= revNumber then
print ("Update " .. oldRevNumber .. " -> " .. revNumber) print ("Update " .. oldRevNumber .. " -> " .. revNumber)
local versionHeader = assert(io.open(wks.location .. "/version.hpp", "w")) local versionHeader = assert(io.open(wks.location .. "/src/version.hpp", "w"))
versionHeader:write("/*\n") versionHeader:write("/*\n")
versionHeader:write(" * Automatically generated by premake5.\n") versionHeader:write(" * Automatically generated by premake5.\n")
versionHeader:write(" * Do not touch, you fucking moron!\n") versionHeader:write(" * Do not touch, you fucking moron!\n")
@ -92,38 +91,38 @@ workspace "iw4x"
kind "SharedLib" kind "SharedLib"
language "C++" language "C++"
files { "./src/**.hpp", "./src/**.cpp" } files { "./src/**.hpp", "./src/**.cpp" }
includedirs { "%{prj.location}", "./src" } includedirs { "%{prj.location}/src", "./src" }
-- Pre-compiled header -- Pre-compiled header
pchheader "STDInclude.hpp" -- must be exactly same as used in #include directives pchheader "STDInclude.hpp" -- must be exactly same as used in #include directives
pchsource "src/STDInclude.cpp" -- real path pchsource "src/STDInclude.cpp" -- real path
buildoptions { "-Zm88" } buildoptions { "/Zm100" }
-- Dependency on zlib, json11 and asio -- Dependency on zlib, json11 and asio
links { "zlib", "json11", "pdcurses", "libtomcrypt", "libtommath", "protobuf" } links { "zlib", "json11", "pdcurses", "libtomcrypt", "libtommath", "protobuf" }
includedirs includedirs
{ {
"./deps/zlib", "./deps/zlib",
"./deps/json11", "./deps/json11",
"./deps/pdcurses", "./deps/pdcurses",
"./deps/asio/asio/include", "./deps/asio/asio/include",
"./deps/libtomcrypt/src/headers", "./deps/libtomcrypt/src/headers",
"./deps/libtommath", "./deps/libtommath",
"./deps/protobuf/src", "./deps/protobuf/src",
} }
-- Virtual paths -- Virtual paths
if not _OPTIONS["no-new-structure"] then if not _OPTIONS["no-new-structure"] then
vpaths { vpaths {
["Headers/*"] = "./src/**.hpp", ["Headers/*"] = { "./src/**.hpp" },
["Sources/*"] = {"./src/**.cpp"} ["Sources/*"] = { "./src/**.cpp" }
} }
end end
vpaths { vpaths {
["Docs/*"] = {"**.txt","**.md"} ["Docs/*"] = { "**.txt","**.md" },
} }
-- Pre-build -- Pre-build
prebuildcommands { prebuildcommands {
"cd %{_MAIN_SCRIPT_DIR}", "cd %{_MAIN_SCRIPT_DIR}",
@ -259,8 +258,12 @@ workspace "iw4x"
"./deps/protobuf/src", "./deps/protobuf/src",
} }
-- default protobuf sources
files { "./deps/protobuf/src/**.cc" } files { "./deps/protobuf/src/**.cc" }
-- our generated sources
files { "./%{prj.location}/src/proto/**.cc" }
-- remove unnecessary sources -- remove unnecessary sources
removefiles removefiles
{ {

View File

@ -229,7 +229,9 @@ namespace Components
node.challenge = Utils::VA("%X", Utils::Cryptography::Rand::GenerateInt()); node.challenge = Utils::VA("%X", Utils::Cryptography::Rand::GenerateInt());
std::string data; std::string data;
Utils::Message::WriteBuffer(data, node.challenge); Proto::NodePacket packet;
packet.set_challenge(node.challenge);
packet.SerializePartialToString(&data);
Logger::Print("Sending registration request to %s\n", node.address.GetString()); Logger::Print("Sending registration request to %s\n", node.address.GetString());
Network::SendRaw(node.address, "nodeRegisterRequest\n" + data); Network::SendRaw(node.address, "nodeRegisterRequest\n" + data);
@ -311,8 +313,11 @@ namespace Components
{ {
std::string data, challenge; std::string data, challenge;
challenge = Utils::VA("X", Utils::Cryptography::Rand::GenerateInt()); challenge = Utils::VA("X", Utils::Cryptography::Rand::GenerateInt());
Utils::Message::WriteBuffer(data, challenge);
Utils::Message::WriteBuffer(data, Utils::Cryptography::ECDSA::SignMessage(Node::SignatureKey, challenge)); Proto::NodePacket packet;
packet.set_challenge(challenge);
packet.set_signature(Utils::Cryptography::ECDSA::SignMessage(Node::SignatureKey, challenge));
packet.SerializePartialToString(&data);
for (auto node : Node::Nodes) for (auto node : Node::Nodes)
{ {
@ -336,16 +341,19 @@ namespace Components
Logger::Print("Received registration request from %s\n", address.GetString()); Logger::Print("Received registration request from %s\n", address.GetString());
std::string response, challenge; Proto::NodePacket packet;
if (!Utils::Message::ReadBuffer(data, challenge)) return; if (!packet.ParseFromString(data)) return;
if (!packet.has_challenge()) return;
std::string publicKey = Node::SignatureKey.GetPublicKey(); std::string response;
std::string signature = Utils::Cryptography::ECDSA::SignMessage(Node::SignatureKey, challenge); std::string signature = Utils::Cryptography::ECDSA::SignMessage(Node::SignatureKey, packet.challenge());
challenge = Utils::VA("%X", Utils::Cryptography::Rand::GenerateInt()); std::string challenge = Utils::VA("%X", Utils::Cryptography::Rand::GenerateInt());
Utils::Message::WriteBuffer(response, signature); packet.Clear();
Utils::Message::WriteBuffer(response, publicKey); packet.set_challenge(challenge);
Utils::Message::WriteBuffer(response, challenge); packet.set_signature(signature);
packet.set_publickey(Node::SignatureKey.GetPublicKey());
packet.SerializeToString(&response);
entry->lastTime = Game::Com_Milliseconds(); entry->lastTime = Game::Com_Milliseconds();
entry->challenge = challenge; entry->challenge = challenge;
@ -361,10 +369,15 @@ namespace Components
Logger::Print("Received synchronization data for registration from %s!\n", address.GetString()); Logger::Print("Received synchronization data for registration from %s!\n", address.GetString());
std::string challenge, publicKey, signature; Proto::NodePacket packet;
if (!Utils::Message::ReadBuffer(data, signature)) return; if (!packet.ParseFromString(data)) return;
if (!Utils::Message::ReadBuffer(data, publicKey)) return; if (!packet.has_challenge()) return;
if (!Utils::Message::ReadBuffer(data, challenge)) return; if (!packet.has_publickey()) return;
if (!packet.has_signature()) return;
std::string challenge = packet.challenge();
std::string publicKey = packet.publickey();
std::string signature = packet.signature();
// Verify signature // Verify signature
entry->publicKey.Set(publicKey); entry->publicKey.Set(publicKey);
@ -388,8 +401,10 @@ namespace Components
publicKey = Node::SignatureKey.GetPublicKey(); publicKey = Node::SignatureKey.GetPublicKey();
signature = Utils::Cryptography::ECDSA::SignMessage(Node::SignatureKey, challenge); signature = Utils::Cryptography::ECDSA::SignMessage(Node::SignatureKey, challenge);
Utils::Message::WriteBuffer(data, signature); packet.Clear();
Utils::Message::WriteBuffer(data, publicKey); packet.set_signature(signature);
packet.set_publickey(publicKey);
packet.SerializePartialToString(&data);
Network::SendRaw(address, "nodeRegisterAcknowledge\n" + data); Network::SendRaw(address, "nodeRegisterAcknowledge\n" + data);
}); });
@ -402,9 +417,13 @@ namespace Components
Logger::Print("Received acknowledgment from %s\n", address.GetString()); Logger::Print("Received acknowledgment from %s\n", address.GetString());
std::string publicKey, signature; Proto::NodePacket packet;
if (!Utils::Message::ReadBuffer(data, signature)) return; if (!packet.ParseFromString(data)) return;
if (!Utils::Message::ReadBuffer(data, publicKey)) return; if (!packet.has_signature()) return;
if (!packet.has_publickey()) return;
std::string publicKey = packet.publickey();
std::string signature = packet.signature();
entry->publicKey.Set(publicKey); entry->publicKey.Set(publicKey);
@ -464,9 +483,13 @@ namespace Components
Node::NodeEntry* entry = Node::FindNode(address); Node::NodeEntry* entry = Node::FindNode(address);
if (!entry || !entry->registered) return; if (!entry || !entry->registered) return;
std::string challenge, signature; Proto::NodePacket packet;
if (!Utils::Message::ReadBuffer(data, challenge)) return; if (!packet.ParseFromString(data)) return;
if (!Utils::Message::ReadBuffer(data, signature)) return; if (!packet.has_challenge()) return;
if (!packet.has_signature()) return;
std::string challenge = packet.challenge();
std::string signature = packet.signature();
if (Utils::Cryptography::ECDSA::VerifyMessage(entry->publicKey, challenge, signature)) if (Utils::Cryptography::ECDSA::VerifyMessage(entry->publicKey, challenge, signature))
{ {

View File

@ -60,6 +60,9 @@
#include <json11.hpp> #include <json11.hpp>
#include <tomcrypt.h> #include <tomcrypt.h>
// Protobuf
#include <proto/node.pb.h>
#pragma warning(pop) #pragma warning(pop)
// Version number // Version number

View File

@ -43,7 +43,7 @@ namespace Utils
for (int i = 0; i < CSV::GetRows(); ++i) for (int i = 0; i < CSV::GetRows(); ++i)
{ {
count = max(CSV::GetColumns(i), count); count = std::max(CSV::GetColumns(i), count);
} }
return count; return count;

View File

@ -246,31 +246,4 @@ namespace Utils
this->KeyValuePairs[KeyValues[i]] = KeyValues[i + 1]; this->KeyValuePairs[KeyValues[i]] = KeyValues[i + 1];
} }
} }
namespace Message
{
void WriteBuffer(std::string& message, std::string buffer)
{
DWORD length = buffer.size();
message.append(reinterpret_cast<char*>(&length), 4);
message.append(buffer);
}
bool ReadBuffer(std::string& message, std::string& buffer)
{
if (message.size() < 4) return false;
char* messagePtr = const_cast<char*>(message.data());
DWORD length = *reinterpret_cast<DWORD*>(messagePtr);
if (message.size() < (length + 4)) return false;
buffer.clear();
buffer.append(messagePtr + 4, length);
message = std::string(messagePtr + 4 + length, message.size() - (4 + length));
return true;
}
}
} }

View File

@ -59,12 +59,4 @@ namespace Utils
target->push_back(entry); target->push_back(entry);
} }
} }
// TODO: Implement a bytebuffer class
// Maybe convery's or protobuf
namespace Message
{
void WriteBuffer(std::string& message, std::string buffer);
bool ReadBuffer(std::string& message, std::string& buffer);
}
} }

9
tools/proto/node.proto Normal file
View File

@ -0,0 +1,9 @@
syntax = "proto3";
package Proto;
message NodePacket {
string challenge = 1;
string signature = 2;
string publicKey = 3;
}

BIN
tools/protoc.exe Normal file

Binary file not shown.

4
tools/protogen.bat Normal file
View File

@ -0,0 +1,4 @@
pushd "%~dp0"
mkdir "..\build\src\proto" 2>NUL
protoc.exe -I=proto --cpp_out=../build/src/proto/ proto/node.proto
popd