diff --git a/daemon/rcutil.cpp b/daemon/rcutil.cpp index 1956cfc72..a22d65a70 100644 --- a/daemon/rcutil.cpp +++ b/daemon/rcutil.cpp @@ -300,10 +300,12 @@ main(int argc, char *argv[]) if(updMode) { printf("rcutil.cpp - Loading [%s]\n", rcfname); - llarp_rc *rc = llarp_rc_read(rcfname); + llarp_rc rc; + llarp_rc_clear(&rc); + llarp_rc_read(rcfname, &rc); // set updated timestamp - rc->last_updated = llarp_time_now_ms(); + rc.last_updated = llarp_time_now_ms(); // load longterm identity llarp_crypto crypt; llarp_crypto_libsodium_init(&crypt); @@ -312,8 +314,8 @@ main(int argc, char *argv[]) llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity); // get identity public key uint8_t *pubkey = llarp::seckey_topublic(identity); - llarp_rc_set_pubsigkey(rc, pubkey); - llarp_rc_sign(&crypt, identity, rc); + llarp_rc_set_pubsigkey(&rc, pubkey); + llarp_rc_sign(&crypt, identity, &rc); // set filename fs::path our_rc_file_out = "update_debug.rc"; @@ -331,13 +333,14 @@ main(int argc, char *argv[]) { llarp_main_loadDatabase(ctx); llarp::LogInfo("Loading ", rcfname); - llarp_rc *rc = llarp_rc_read(rcfname); - if(!rc) + llarp_rc rc; + llarp_rc_clear(&rc); + if(!llarp_rc_read(rcfname, &rc)) { llarp::LogError("Can't load RC"); return 0; } - llarp_main_putDatabase(ctx, rc); + llarp_main_putDatabase(ctx, &rc); } if(exportMode) { @@ -394,8 +397,10 @@ main(int argc, char *argv[]) } if(readMode) { - llarp_rc *rc = llarp_rc_read(rcfname); - displayRC(rc); + llarp_rc result; + llarp_rc_clear(&result); + llarp_rc_read(rcfname, &result); + displayRC(&result); } // it's a unique_ptr, should clean up itself // llarp_main_free(ctx); diff --git a/include/llarp/router_contact.h b/include/llarp/router_contact.h index 5c7afe575..6f02eda0c 100644 --- a/include/llarp/router_contact.h +++ b/include/llarp/router_contact.h @@ -81,8 +81,8 @@ llarp_rc_clear(struct llarp_rc *rc); bool llarp_rc_addr_list_iter(struct llarp_ai_list_iter *iter, struct llarp_ai *ai); -struct llarp_rc * -llarp_rc_read(const char *fpath); +bool +llarp_rc_read(const char *fpath, struct llarp_rc *result); bool llarp_rc_write(struct llarp_rc *rc, const char *our_rc_file); diff --git a/llarp/context.cpp b/llarp/context.cpp index 58f489f64..77d6879ba 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -375,9 +375,13 @@ llarp_main_getLocalRC(struct llarp_main *ptr) iter.visit = &iter_config; llarp_config_iter(ctx->config, &iter); */ + llarp_rc *rc = new llarp_rc; + llarp_rc_new(rc); llarp::LogInfo("Loading ", ptr->ctx->conatctFile); - llarp_rc *rc = llarp_rc_read(ptr->ctx->conatctFile); - return rc; + if(llarp_rc_read(ptr->ctx->conatctFile, rc)) + return rc; + else + return nullptr; } void diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index 7aca063a6..62c957b45 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -203,19 +203,20 @@ struct llarp_nodedb return false; } #endif - llarp_rc *rc = llarp_rc_read(fpath.c_str()); - if(!rc) + llarp_rc rc; + llarp_rc_clear(&rc); + if(!llarp_rc_read(fpath.c_str(), &rc)) { llarp::LogError("Signature read failed", fpath); return false; } - if(!llarp_rc_verify_sig(crypto, rc)) + if(!llarp_rc_verify_sig(crypto, &rc)) { llarp::LogError("Signature verify failed", fpath); return false; } - llarp::PubKey pk(rc->pubkey); - entries[pk] = *rc; + llarp::PubKey pk(rc.pubkey); + entries[pk] = rc; return true; } diff --git a/llarp/router.cpp b/llarp/router.cpp index 5b5a57fec..b53057a4c 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -165,18 +165,23 @@ llarp_router::HandleDHTLookupForSendTo(llarp_router_lookup_job *job) void llarp_router::try_connect(fs::path rcfile) { - llarp_rc *remote = new llarp_rc; - llarp_rc_new(remote); - remote = llarp_rc_read(rcfile.c_str()); - if(!remote) + llarp_rc remote; + llarp_rc_new(&remote); + if(!llarp_rc_read(rcfile.c_str(), &remote)) { llarp::LogError("failure to decode or verify of remote RC"); return; } - if(llarp_rc_verify_sig(&crypto, remote)) + if(llarp_rc_verify_sig(&crypto, &remote)) { llarp::LogDebug("verified signature"); - if(!llarp_router_try_connect(this, remote, 10)) + // store into filesystem + // TODO: should this be async? + if(!llarp_nodedb_put_rc(nodedb, &remote)) + { + llarp::LogWarn("failed to store"); + } + if(!llarp_router_try_connect(this, &remote, 10)) { // or error? llarp::LogWarn("session already made"); @@ -184,7 +189,7 @@ llarp_router::try_connect(fs::path rcfile) } else llarp::LogError("failed to verify signature of RC", rcfile); - llarp_rc_free(remote); + llarp_rc_free(&remote); } bool @@ -907,21 +912,21 @@ llarp_rc_set_pubkey(struct llarp_rc *rc, const uint8_t *pubenckey, llarp_rc_set_pubsigkey(rc, pubsigkey); } -struct llarp_rc * -llarp_rc_read(const char *fpath) +bool +llarp_rc_read(const char *fpath, llarp_rc *result) { fs::path our_rc_file(fpath); std::error_code ec; if(!fs::exists(our_rc_file, ec)) { printf("File[%s] not found\n", fpath); - return 0; + return false; } std::ifstream f(our_rc_file, std::ios::binary); if(!f.is_open()) { printf("Can't open file [%s]\n", fpath); - return 0; + return false; } byte_t tmp[MAX_RC_SIZE]; llarp_buffer_t buf = llarp::StackBuffer< decltype(tmp) >(tmp); @@ -930,18 +935,17 @@ llarp_rc_read(const char *fpath) f.seekg(0, std::ios::beg); if(sz > buf.sz) - return 0; + return false; f.read((char *)buf.base, sz); // printf("contents[%s]\n", tmpc); - llarp_rc *rc = new llarp_rc; - llarp::Zero(rc, sizeof(llarp_rc)); - if(!llarp_rc_bdecode(rc, &buf)) + llarp::Zero(result, sizeof(llarp_rc)); + if(!llarp_rc_bdecode(result, &buf)) { printf("Can't decode [%s]\n", fpath); - return 0; + return false; } - return rc; + return true; } bool