Skip to content

Commit

Permalink
Include protobuf. Not sure if we're supposed to commit protoc :S
Browse files Browse the repository at this point in the history
  • Loading branch information
momo5502 committed Feb 10, 2016
1 parent 029c1e4 commit 75e7622
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 70 deletions.
1 change: 1 addition & 0 deletions generate.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@echo off
call tools\protogen.bat
premake5 %* vs2015
25 changes: 14 additions & 11 deletions premake5.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

-- Option to allow copying the DLL file to a custom folder after build
newoption {
trigger = "copy-to",
Expand Down Expand Up @@ -36,7 +35,7 @@ newaction {

-- get old version number from version.hpp if any
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
local oldVersionHeaderContent = assert(oldVersionHeader:read('*a'))
oldRevNumber = string.match(oldVersionHeaderContent, "#define REVISION (%d+)")
Expand All @@ -49,7 +48,7 @@ newaction {
-- generate version.hpp with a revision number if not equal
if oldRevNumber ~= revNumber then
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(" * Automatically generated by premake5.\n")
versionHeader:write(" * Do not touch, you fucking moron!\n")
Expand Down Expand Up @@ -92,38 +91,38 @@ workspace "iw4x"
kind "SharedLib"
language "C++"
files { "./src/**.hpp", "./src/**.cpp" }
includedirs { "%{prj.location}", "./src" }
includedirs { "%{prj.location}/src", "./src" }

-- Pre-compiled header
pchheader "STDInclude.hpp" -- must be exactly same as used in #include directives
pchsource "src/STDInclude.cpp" -- real path
buildoptions { "-Zm88" }
buildoptions { "/Zm100" }

-- Dependency on zlib, json11 and asio
links { "zlib", "json11", "pdcurses", "libtomcrypt", "libtommath", "protobuf" }
includedirs
{
{
"./deps/zlib",
"./deps/json11",
"./deps/pdcurses",
"./deps/asio/asio/include",
"./deps/libtomcrypt/src/headers",
"./deps/libtommath",
"./deps/protobuf/src",
"./deps/protobuf/src",
}

-- Virtual paths
if not _OPTIONS["no-new-structure"] then
vpaths {
["Headers/*"] = "./src/**.hpp",
["Sources/*"] = {"./src/**.cpp"}
["Headers/*"] = { "./src/**.hpp" },
["Sources/*"] = { "./src/**.cpp" }
}
end

vpaths {
["Docs/*"] = {"**.txt","**.md"}
["Docs/*"] = { "**.txt","**.md" },
}

-- Pre-build
prebuildcommands {
"cd %{_MAIN_SCRIPT_DIR}",
Expand Down Expand Up @@ -259,8 +258,12 @@ workspace "iw4x"
"./deps/protobuf/src",
}

-- default protobuf sources
files { "./deps/protobuf/src/**.cc" }

-- our generated sources
files { "./%{prj.location}/src/proto/**.cc" }

-- remove unnecessary sources
removefiles
{
Expand Down
69 changes: 46 additions & 23 deletions src/Components/Modules/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ namespace Components
node.challenge = Utils::VA("%X", Utils::Cryptography::Rand::GenerateInt());

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());
Network::SendRaw(node.address, "nodeRegisterRequest\n" + data);
Expand Down Expand Up @@ -311,8 +313,11 @@ namespace Components
{
std::string data, challenge;
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)
{
Expand All @@ -336,16 +341,19 @@ namespace Components

Logger::Print("Received registration request from %s\n", address.GetString());

std::string response, challenge;
if (!Utils::Message::ReadBuffer(data, challenge)) return;
Proto::NodePacket packet;
if (!packet.ParseFromString(data)) return;
if (!packet.has_challenge()) return;

std::string publicKey = Node::SignatureKey.GetPublicKey();
std::string signature = Utils::Cryptography::ECDSA::SignMessage(Node::SignatureKey, challenge);
challenge = Utils::VA("%X", Utils::Cryptography::Rand::GenerateInt());
std::string response;
std::string signature = Utils::Cryptography::ECDSA::SignMessage(Node::SignatureKey, packet.challenge());
std::string challenge = Utils::VA("%X", Utils::Cryptography::Rand::GenerateInt());

Utils::Message::WriteBuffer(response, signature);
Utils::Message::WriteBuffer(response, publicKey);
Utils::Message::WriteBuffer(response, challenge);
packet.Clear();
packet.set_challenge(challenge);
packet.set_signature(signature);
packet.set_publickey(Node::SignatureKey.GetPublicKey());
packet.SerializeToString(&response);

entry->lastTime = Game::Com_Milliseconds();
entry->challenge = challenge;
Expand All @@ -361,10 +369,15 @@ namespace Components

Logger::Print("Received synchronization data for registration from %s!\n", address.GetString());

std::string challenge, publicKey, signature;
if (!Utils::Message::ReadBuffer(data, signature)) return;
if (!Utils::Message::ReadBuffer(data, publicKey)) return;
if (!Utils::Message::ReadBuffer(data, challenge)) return;
Proto::NodePacket packet;
if (!packet.ParseFromString(data)) return;
if (!packet.has_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
entry->publicKey.Set(publicKey);
Expand All @@ -388,8 +401,10 @@ namespace Components
publicKey = Node::SignatureKey.GetPublicKey();
signature = Utils::Cryptography::ECDSA::SignMessage(Node::SignatureKey, challenge);

Utils::Message::WriteBuffer(data, signature);
Utils::Message::WriteBuffer(data, publicKey);
packet.Clear();
packet.set_signature(signature);
packet.set_publickey(publicKey);
packet.SerializePartialToString(&data);

Network::SendRaw(address, "nodeRegisterAcknowledge\n" + data);
});
Expand All @@ -402,9 +417,13 @@ namespace Components

Logger::Print("Received acknowledgment from %s\n", address.GetString());

std::string publicKey, signature;
if (!Utils::Message::ReadBuffer(data, signature)) return;
if (!Utils::Message::ReadBuffer(data, publicKey)) return;
Proto::NodePacket packet;
if (!packet.ParseFromString(data)) 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);

Expand Down Expand Up @@ -464,9 +483,13 @@ namespace Components
Node::NodeEntry* entry = Node::FindNode(address);
if (!entry || !entry->registered) return;

std::string challenge, signature;
if (!Utils::Message::ReadBuffer(data, challenge)) return;
if (!Utils::Message::ReadBuffer(data, signature)) return;
Proto::NodePacket packet;
if (!packet.ParseFromString(data)) 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))
{
Expand Down
3 changes: 3 additions & 0 deletions src/STDInclude.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
#include <json11.hpp>
#include <tomcrypt.h>

// Protobuf
#include <proto/node.pb.h>

#pragma warning(pop)

// Version number
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/CSV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Utils

for (int i = 0; i < CSV::GetRows(); ++i)
{
count = max(CSV::GetColumns(i), count);
count = std::max(CSV::GetColumns(i), count);
}

return count;
Expand Down
27 changes: 0 additions & 27 deletions src/Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,31 +246,4 @@ namespace Utils
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;
}
}
}
8 changes: 0 additions & 8 deletions src/Utils/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,4 @@ namespace Utils
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 changes: 9 additions & 0 deletions tools/proto/node.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package Proto;

message NodePacket {
string challenge = 1;
string signature = 2;
string publicKey = 3;
}
Binary file added tools/protoc.exe
Binary file not shown.
4 changes: 4 additions & 0 deletions tools/protogen.bat
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 75e7622

Please sign in to comment.