From 2e74bce713c8935311a8e46d410834613acdab5c Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 21 Jun 2018 04:11:55 -0700 Subject: [PATCH 1/4] getDatabase and associated functions --- include/llarp.h | 7 +++++-- include/llarp.hpp | 3 +++ llarp/context.cpp | 10 ++++++++++ llarp/encode.cpp | 21 +++++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 llarp/encode.cpp diff --git a/include/llarp.h b/include/llarp.h index 3cef50ab5..97d1f89af 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -39,12 +39,15 @@ llarp_main_loadDatabase(struct llarp_main *ptr); int llarp_main_iterateDatabase(struct llarp_main *ptr, struct llarp_nodedb_iter i); - + /// put RC into nodeDB bool llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc); - +struct llarp_rc * +llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk); + + void llarp_main_free(struct llarp_main *ptr); diff --git a/include/llarp.hpp b/include/llarp.hpp index 0c4a68148..8448dbb35 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -42,6 +42,9 @@ namespace llarp bool PutDatabase(struct llarp_rc *rc); + struct llarp_rc * + GetDatabase(const byte_t *pk); + int Run(); diff --git a/llarp/context.cpp b/llarp/context.cpp index da6c731a2..c3b759e88 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -118,6 +118,11 @@ namespace llarp return llarp_nodedb_put_rc(nodedb, rc); } + struct llarp_rc * + Context::GetDatabase(const byte_t *pk) + { + return llarp_nodedb_get_rc(nodedb, pk); + } int Context::Run() @@ -328,6 +333,11 @@ llarp_main_putDatabase(struct llarp_main *ptr, struct llarp_rc *rc) return ptr->ctx->PutDatabase(rc); } +struct llarp_rc * +llarp_main_getDatabase(struct llarp_main *ptr, byte_t *pk) +{ + return ptr->ctx->GetDatabase(pk); +} void llarp_main_free(struct llarp_main *ptr) diff --git a/llarp/encode.cpp b/llarp/encode.cpp new file mode 100644 index 000000000..ed89f9cfb --- /dev/null +++ b/llarp/encode.cpp @@ -0,0 +1,21 @@ +#include + +int char2int(char input) +{ + if(input >= '0' && input <= '9') + return input - '0'; + if(input >= 'A' && input <= 'F') + return input - 'A' + 10; + if(input >= 'a' && input <= 'f') + return input - 'a' + 10; + throw std::invalid_argument("Invalid input string"); +} + +void HexDecode(const char* src, uint8_t* target) +{ + while(*src && src[1]) + { + *(target++) = char2int(*src)*16 + char2int(src[1]); + src += 2; + } +} From 53c5474dc6fe76d4325601ac36b1031d624fa565 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 21 Jun 2018 04:13:40 -0700 Subject: [PATCH 2/4] hexDecode helper --- include/llarp/encode.hpp | 3 +++ llarp/encode.cpp | 34 ++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/include/llarp/encode.hpp b/include/llarp/encode.hpp index 10f275870..3a4840974 100644 --- a/include/llarp/encode.hpp +++ b/include/llarp/encode.hpp @@ -26,6 +26,9 @@ namespace llarp *ptr = 0; return &stack[0]; } + + int char2int(char input); + void HexDecode(const char* src, uint8_t* target); } #endif diff --git a/llarp/encode.cpp b/llarp/encode.cpp index ed89f9cfb..f337a1042 100644 --- a/llarp/encode.cpp +++ b/llarp/encode.cpp @@ -1,21 +1,27 @@ #include +#include -int char2int(char input) +namespace llarp { - if(input >= '0' && input <= '9') - return input - '0'; - if(input >= 'A' && input <= 'F') - return input - 'A' + 10; - if(input >= 'a' && input <= 'f') - return input - 'a' + 10; - throw std::invalid_argument("Invalid input string"); -} -void HexDecode(const char* src, uint8_t* target) -{ - while(*src && src[1]) + int char2int(char input) { - *(target++) = char2int(*src)*16 + char2int(src[1]); - src += 2; + if(input >= '0' && input <= '9') + return input - '0'; + if(input >= 'A' && input <= 'F') + return input - 'A' + 10; + if(input >= 'a' && input <= 'f') + return input - 'a' + 10; + throw std::invalid_argument("Invalid input string"); } + + void HexDecode(const char* src, uint8_t* target) + { + while(*src && src[1]) + { + *(target++) = char2int(*src)*16 + char2int(src[1]); + src += 2; + } + } + } From 10f9fc86e88425a19e8d7fbf37323a9881933b6d Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 21 Jun 2018 04:14:14 -0700 Subject: [PATCH 3/4] skip directories and .DS_Store on MacOS, remove some dead code --- llarp/nodedb.cpp | 75 +++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index c7b9adb0d..2db9e6fd9 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -47,6 +47,24 @@ struct llarp_nodedb return entries.find(pk) != entries.end(); } +/* + bool + Has(const byte_t *pk) + { + llarp::PubKey test(pk); + auto itr = this->entries.begin(); + while(itr != this->entries.end()) + { + llarp::Info("Has byte_t [", test.size(), "] vs [", itr->first.size(), "]"); + if (memcmp(test.data(), itr->first.data(), 32) == 0) { + llarp::Info("Match"); + } + itr++; + } + return entries.find(pk) != entries.end(); + } +*/ + bool pubKeyExists(llarp_rc *rc) { @@ -167,8 +185,9 @@ struct llarp_nodedb auto itr = i.begin(); while(itr != itr.end()) { - if(loadfile(*itr)) + if (fs::is_regular_file(itr->symlink_status()) && loadfile(*itr)) sz++; + ++itr; } return sz; @@ -177,49 +196,26 @@ struct llarp_nodedb bool loadfile(const fs::path &fpath) { +#if __APPLE__ && __MACH__ + // skip .DS_Store files + if (strstr(fpath.c_str(), ".DS_Store") != 0) { + return false; + } +#endif llarp_rc *rc = llarp_rc_read(fpath.c_str()); - if (rc) + if (!rc) { - if(llarp_rc_verify_sig(crypto, rc)) - { - llarp::PubKey pk(rc->pubkey); - entries[pk] = *rc; - return true; - } + llarp::Error("Signature read failed", fpath); + return false; } - /* - std::ifstream f(fpath, std::ios::binary); - if(!f.is_open()) - return false; - - byte_t tmp[MAX_RC_SIZE]; - - auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); - f.seekg(0, std::ios::end); - size_t sz = f.tellg(); - f.seekg(0, std::ios::beg); - - if(sz > buf.sz) - return false; - - // TODO: error checking - f.read((char *)buf.base, sz); - buf.sz = sz; - - llarp_rc rc; - llarp::Zero(&rc, sizeof(llarp_rc)); - if(llarp_rc_bdecode(&rc, &buf)) + if(!llarp_rc_verify_sig(crypto, rc)) { - if(llarp_rc_verify_sig(crypto, &rc)) - { - llarp::PubKey pk(rc.pubkey); - entries[pk] = rc; - return true; - } + llarp::Error("Signature verify failed", fpath); + return false; } - llarp_rc_free(&rc); - */ - return false; + llarp::PubKey pk(rc->pubkey); + entries[pk] = *rc; + return true; } bool iterate() { @@ -414,6 +410,7 @@ llarp_nodedb_async_load_rc(struct llarp_async_load_rc *job) struct llarp_rc * llarp_nodedb_get_rc(struct llarp_nodedb *n, const byte_t *pk) { + //llarp::Info("llarp_nodedb_get_rc [", pk, "]"); if(n->Has(pk)) return n->getRC(pk); else From 15eab472161a532e0d1654d036418a2b35fd8f79 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 21 Jun 2018 04:15:28 -0700 Subject: [PATCH 4/4] finish implementing --export --- daemon/rcutil.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/daemon/rcutil.cpp b/daemon/rcutil.cpp index 47a697642..f50e06200 100644 --- a/daemon/rcutil.cpp +++ b/daemon/rcutil.cpp @@ -30,7 +30,7 @@ bool printNode(struct llarp_nodedb_iter *iter) { const char *hexname = llarp::HexEncode< llarp::PubKey, decltype(ftmp) >(iter->rc->pubkey, ftmp); - printf("[%d]=>[%s]\n", iter->index, hexname); + printf("[%zu]=>[%s]\n", iter->index, hexname); return false; } @@ -217,7 +217,20 @@ main(int argc, char *argv[]) } if (exportMode) { llarp_main_loadDatabase(ctx); - // TODO: write me + //llarp::Info("Looking for string: ", rcfname); + + llarp::PubKey binaryPK; + llarp::HexDecode(rcfname, binaryPK.data()); + + llarp::Info("Looking for binary: ", binaryPK); + struct llarp_rc *rc = llarp_main_getDatabase(ctx, binaryPK.data()); + if (!rc) { + llarp::Error("Can't load RC from database"); + } + std::string filename(rcfname); + filename.append(".signed"); + llarp::Info("Writing out: ", filename); + llarp_rc_write(rc, filename.c_str()); } llarp_main_free(ctx); return 1; // success