Skip to content

Commit

Permalink
Join with refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Oct 13, 2012
1 parent d3de464 commit 3e43529
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 136 deletions.
4 changes: 2 additions & 2 deletions .cproject
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
<folderInfo id="cdt.managedbuild.toolchain.gnu.base.1634143561.823028353" name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.base.906133197" name="cdt.managedbuild.toolchain.gnu.base" superClass="cdt.managedbuild.toolchain.gnu.base">
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.target.gnu.platform.base.1115410077" name="Debug Platform" osList="linux,hpux,aix,qnx" superClass="cdt.managedbuild.target.gnu.platform.base"/>
<builder buildPath="${workspace_loc:/kad/build}" enableAutoBuild="false" id="cdt.managedbuild.target.gnu.builder.base.1288354396" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.base"/>
<builder arguments="-j 8" buildPath="${workspace_loc:/kad/build}" command="make" enableAutoBuild="false" id="cdt.managedbuild.target.gnu.builder.base.1288354396" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.base"/>
<tool id="cdt.managedbuild.tool.gnu.archiver.base.1992286236" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.base.435969102" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.base">
<option id="gnu.cpp.compiler.option.include.paths.1156013268" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" valueType="includePath"/>
<option id="gnu.cpp.compiler.option.include.paths.1156013268" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.715850647" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.compiler.base.1341550651" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.base">
Expand Down
4 changes: 2 additions & 2 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildArguments</key>
<value></value>
<value>-j 8</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildCommand</key>
Expand Down Expand Up @@ -63,7 +63,7 @@
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
<value>true</value>
<value>false</value>
</dictionary>
</arguments>
</buildCommand>
Expand Down
29 changes: 16 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ cmake_minimum_required (VERSION 2.6)

project (Kademlia)

set(CMAKE_BUILD_TYPE "Debug")
if (WIN32)
else (WIN32)
set(CMAKE_CXX_FLAGS_DEBUG "-g3 -rdynamic -W -Wall -Wvla -Wextra -Wshadow -Winline -Wconversion -Wcast-align -Wno-long-long -Wwrite-strings -Wpointer-arith -Wredundant-decls -Wunsafe-loop-optimizations -Wno-variadic-macros -Wno-unused-parameter -Woverloaded-virtual -fstack-protector-all")
set(CMAKE_CXX_FLAGS_RELEASE "-O4 -fno-stack-protector")
endif (WIN32)
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") # remove -rdynamic flag
SET(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "") # remove -rdynamic flag

#set(CMAKE_BUILD_TYPE "Debug")
#if (NOT WIN32)
# set(CMAKE_CXX_FLAGS_DEBUG "-g3 -pthread -rdynamic -W -Wall -Wvla -Wextra -Wshadow -Winline -Wconversion -Wcast-align -Wno-long-long -Wwrite-strings -Wpointer-arith -Wredundant-decls -Wunsafe-loop-optimizations -Wno-variadic-macros -Wno-unused-parameter -Woverloaded-virtual -fstack-protector-all")
# set(CMAKE_CXX_FLAGS_RELEASE "-O4 -pthread -fno-stack-protector")
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
#endif()

# The version number.
set (KAD_VERSION_MAJOR 0)
set (KAD_VERSION_MINOR 1)
set (KAD_VERSION_PATCH 0)

configure_file (
"${PROJECT_SOURCE_DIR}/src/KadConfig.h.in"
"${PROJECT_BINARY_DIR}/KadConfig.h"
Expand All @@ -34,8 +37,8 @@ add_custom_command (

add_subdirectory (xplat)

add_subdirectory (udt4)
include_directories ("${PROJECT_SOURCE_DIR}/udt4/include")
add_subdirectory (udt4)
include_directories ("${PROJECT_SOURCE_DIR}/udt4/include")

#if (WIN32)
#link_directories("${PROJECT_SOURCE_DIR}/cryptopp/x64/Output/")
Expand All @@ -45,11 +48,11 @@ add_subdirectory (xplat)
add_library (kad ${kad_src})
target_link_libraries (kad xplat udt msgpack cryptopp) # Win32: cryptlib

#add_executable(kad_test ${CMAKE_BINARY_DIR}/kad_test.cpp)
#target_link_libraries (kad_test kad)
add_executable(kad_test ${CMAKE_BINARY_DIR}/kad_test.cpp)
target_link_libraries (kad_test kad)

#add_executable(kad_sim kad_sim.cpp)
#target_link_libraries (kad_sim kad)
add_executable(kad_sim kad_sim.cpp)
target_link_libraries (kad_sim kad)

add_executable(signtool signtool.cpp)
target_link_libraries (signtool msgpack cryptopp) # Win32: cryptlib
Expand Down
3 changes: 1 addition & 2 deletions bsp.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
vshymanskyy.org.ua:7777
;192.168.1.50:7777
;192.168.1.10:7777
localhost:7777
7 changes: 7 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dir=$(pwd)
mkdir $dir/build
#mkdir $dir/build/linux-amd64; cd $dir/build/linux-amd64; cmake ../..; make
mkdir $dir/build/linux-armhf; cd $dir/build/linux-armhf; cmake -DCMAKE_TOOLCHAIN_FILE=../../linux-armhf.cmake ../..; make
#mkdir $dir/build/win32-x86; cd $dir/build/win32-x86; cmake -DCMAKE_TOOLCHAIN_FILE=../../Win32.cmake ../..; make
#mkdir $dir/build/win32-x64; cd $dir/build/win32-x64; cmake -DCMAKE_TOOLCHAIN_FILE=../../Win64.cmake ../..; make
#mkdir $dir/build/linux-i386; cd $dir/build/linux-i386; cmake -DCMAKE_TOOLCHAIN_FILE=../../Win32.cmake ../..; make
104 changes: 58 additions & 46 deletions kad_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "XLogUtils.h"
#include "XCmdShell.h"
#include "XTimeCounter.h"
#include "XFileSystem.h"

#include "KadOpMgr.h"
#include "KadContact.h"
Expand All @@ -12,6 +13,10 @@ KadOpMgr* gMgr;
static
int Peers(int argc, char* argv[])
{
if (!gMgr) {
return 1;
}
printf("Contacts: %d\n", gMgr->GetContacts().Count());
return 0;
}

Expand All @@ -27,6 +32,16 @@ int Update(int argc, char* argv[])
return 0;
}

static
int Ping(int argc, char* argv[])
{
if (!gMgr || argc != 2) {
return 1;
}
gMgr->Ping(XSockAddr::Lookup(argv[1]));
return 0;
}

#include <iostream>
#include <fstream>
#include <string>
Expand Down Expand Up @@ -56,42 +71,17 @@ void SaveBspToFile(const char* fn, const XList<XSockAddr>& bsps)
outfile.open(fn);
if (outfile.is_open()) {
for (XList<XSockAddr>::It it = bsps.First(); it != bsps.End(); ++it) {
//XString name = bsps[it].ResolveName();
//if (name.Length()) {
// outfile << name << ":" << bsps[it].Port() << endl;
//} else {
XString name = bsps[it].ResolveName();
if (name.Length()) {
outfile << name << ":" << bsps[it].Port() << endl;
} else {
outfile << bsps[it].ToString() << endl;
//}
}
}
outfile.close();
}
}

#include <sys/stat.h>
#include <unistd.h>

bool FileExists(const char* fn) {
struct stat sts;
if (stat(fn, &sts) == -1)
{
if (errno == ENOENT) {
return false;
}
X_FATAL("stat failed: %d (%s)", errno, strerror(errno));
}
return true;
}

bool FileDelete(const char* fn) {
if (unlink(fn) == -1)
{
if (errno != ENOENT) {
return false;
}
}
return true;
}

KadRSA* TryLoadKeys() {
if (FileExists("key.priv") && FileExists("key.pub")) {
KadRSA* rsa = new KadRSA("key.priv", "key.pub");
Expand All @@ -111,34 +101,53 @@ KadRSA* TryLoadKeys() {

int main(int argc, char *argv[])
{
const char* bindaddr = "[::]:7777";
if (argc == 2) {
bindaddr = argv[1];
}

/************************************************
* General initialization
*/
XStackTrace();
//XStackTrace();
RandInit();
//XLogManager::Get().SetDefaultLogger(new XFileLogger("log.txt"));

/************************************************
* Handle command line
*/
char* bindaddr = "[::]:7777";
bool temporary = false;
if (argc == 2) {
bindaddr = argv[1];
}

if (argc == 3 && !strcmp(argv[2], "-temp")) {
temporary = true;
} else if (argc == 2 && !strcmp(argv[1], "-temp")) {
temporary = true;
bindaddr = new char[64];
sprintf(bindaddr, "[::]:%d", RandRange(1024, 32000));
}

/************************************************
* Generate/Load Local identification data
*/
KadRSA* rsa = NULL; //TryLoadKeys();
if (!rsa) {
printf("Generating new RSA keys\n");
rsa = new KadRSA();
KadRSA* rsa = NULL;
if (!temporary) {
rsa = TryLoadKeys();
if (!rsa) {
printf("Generating new RSA keys\n");
rsa = new KadRSA();
if (!rsa->ValidateKeys()) {
X_FATAL("Invalid RSA keys generated!");
}

rsa->SaveKeys("key.priv", "key.pub");
printf("RSA keys saved\n");
}
} else {
printf("Generating temporary RSA keys\n");
rsa = new KadRSA();
if (!rsa->ValidateKeys()) {
X_FATAL("Invalid RSA keys generated!");
}

rsa->SaveKeys("key.priv", "key.pub");
printf("RSA keys saved\n");
}

std::string pubKeyStr = KadRSA::PubEncode(rsa->GetPublicKey());
KadId localId = KadId::FromHash(pubKeyStr.c_str(), pubKeyStr.size());

Expand All @@ -151,7 +160,7 @@ int main(int argc, char *argv[])
*/
XList<XSockAddr> bspLst = LoadBspFromFile("bsp.cfg");

// No bsp's in file case
// No bsp's in the file
if (!bspLst.Count()) {
printf ("Please enter bootstrap node address: ");
char buff[256];
Expand All @@ -172,6 +181,7 @@ int main(int argc, char *argv[])
sh.RegisterCommand("peers", &Peers);
sh.RegisterCommand("find", &FindNode);
sh.RegisterCommand("update", &Update);
sh.RegisterCommand("ping", &Ping);
sh.Run();

/************************************************
Expand All @@ -183,7 +193,9 @@ int main(int argc, char *argv[])
/************************************************
* Save bootstrap contacts (bsp.txt)
*/
SaveBspToFile("bsp.cfg", bspLst);
if (bspLst.Count()) { // Not to produce empty file!
SaveBspToFile("bsp.cfg", bspLst);
}

return 0;
}
29 changes: 13 additions & 16 deletions signtool.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <KadConfig.h>

#include <iostream>
#include <fstream>
#include <string>
Expand All @@ -21,22 +19,20 @@
using namespace CryptoPP;
using namespace std;

static size_t FileSize(const char* filePath)
{
std::ifstream file(filePath, std::ios::binary);
AutoSeededRandomPool rng;

size_t fsize = 0;
if (file.is_open()) {
file.seekg(0, std::ios::end);
fsize = file.tellg();
file.close();
inline
uint64_t FileSize(const char* fn)
{
if (FILE* f = fopen(fn, "rb")) {
fseek(f, 0, SEEK_END);
uint64_t result = ftello64(f);
fclose(f);
return result;
}

return fsize;
return 0;
}

AutoSeededRandomPool rng;

static void Fatal(const char* fmt, ...)
{
va_list args;
Expand Down Expand Up @@ -139,7 +135,8 @@ bool VerifyFile(const char* fnIn, const char* fnOut)
Fatal("Hash verification FAIL!");
}

Info("Verification OK (version: %s, timestamp: %s)", descr["ver"].as<string>().c_str(), descr["timestamp"].as<string>().c_str());
msgpack_object_print(stdout, descrPack.get());
Info("\nVerification OK");

if (fnOut) {
ofstream outfile(fnOut, std::ios::binary);
Expand All @@ -166,7 +163,7 @@ bool SignFile(const char* fnIn, const char* fnOut)
{
// Version string
std::ostringstream version;
version << KADEMLIA_VERSION_MAJOR << "." << KADEMLIA_VERSION_MINOR << "." << KADEMLIA_VERSION_PATCH;
version << "0.1.0";

time_t result = time(NULL);
char gmTime[64];
Expand Down
4 changes: 2 additions & 2 deletions src/KadConfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
* If no reply received from a node in this period (ms)
* consider the node unresponsive.
**/
#define KADEMLIA_TIMEOUT_RESPONSE 15
#define KADEMLIA_TIMEOUT_RESPONSE 1500

/**
* Timeout for performing an operation (ms)
**/
#define KADEMLIA_TIMEOUT_OPERATION 100
#define KADEMLIA_TIMEOUT_OPERATION 5000

/**
* Maximum number of concurrent messages in transit.
Expand Down
Loading

0 comments on commit 3e43529

Please sign in to comment.