* don't heap allocate llarp_rc

* store rc into network database cache on connect
pull/7/head
Jeff Becker 6 years ago
parent 754fb8302d
commit 17ad12cd2c

@ -300,10 +300,12 @@ main(int argc, char *argv[])
if(updMode) if(updMode)
{ {
printf("rcutil.cpp - Loading [%s]\n", rcfname); 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 // set updated timestamp
rc->last_updated = llarp_time_now_ms(); rc.last_updated = llarp_time_now_ms();
// load longterm identity // load longterm identity
llarp_crypto crypt; llarp_crypto crypt;
llarp_crypto_libsodium_init(&crypt); llarp_crypto_libsodium_init(&crypt);
@ -312,8 +314,8 @@ main(int argc, char *argv[])
llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity); llarp_findOrCreateIdentity(&crypt, ident_keyfile.c_str(), identity);
// get identity public key // get identity public key
uint8_t *pubkey = llarp::seckey_topublic(identity); uint8_t *pubkey = llarp::seckey_topublic(identity);
llarp_rc_set_pubsigkey(rc, pubkey); llarp_rc_set_pubsigkey(&rc, pubkey);
llarp_rc_sign(&crypt, identity, rc); llarp_rc_sign(&crypt, identity, &rc);
// set filename // set filename
fs::path our_rc_file_out = "update_debug.rc"; fs::path our_rc_file_out = "update_debug.rc";
@ -331,13 +333,14 @@ main(int argc, char *argv[])
{ {
llarp_main_loadDatabase(ctx); llarp_main_loadDatabase(ctx);
llarp::LogInfo("Loading ", rcfname); llarp::LogInfo("Loading ", rcfname);
llarp_rc *rc = llarp_rc_read(rcfname); llarp_rc rc;
if(!rc) llarp_rc_clear(&rc);
if(!llarp_rc_read(rcfname, &rc))
{ {
llarp::LogError("Can't load RC"); llarp::LogError("Can't load RC");
return 0; return 0;
} }
llarp_main_putDatabase(ctx, rc); llarp_main_putDatabase(ctx, &rc);
} }
if(exportMode) if(exportMode)
{ {
@ -394,8 +397,10 @@ main(int argc, char *argv[])
} }
if(readMode) if(readMode)
{ {
llarp_rc *rc = llarp_rc_read(rcfname); llarp_rc result;
displayRC(rc); llarp_rc_clear(&result);
llarp_rc_read(rcfname, &result);
displayRC(&result);
} }
// it's a unique_ptr, should clean up itself // it's a unique_ptr, should clean up itself
// llarp_main_free(ctx); // llarp_main_free(ctx);

@ -81,8 +81,8 @@ llarp_rc_clear(struct llarp_rc *rc);
bool bool
llarp_rc_addr_list_iter(struct llarp_ai_list_iter *iter, struct llarp_ai *ai); llarp_rc_addr_list_iter(struct llarp_ai_list_iter *iter, struct llarp_ai *ai);
struct llarp_rc * bool
llarp_rc_read(const char *fpath); llarp_rc_read(const char *fpath, struct llarp_rc *result);
bool bool
llarp_rc_write(struct llarp_rc *rc, const char *our_rc_file); llarp_rc_write(struct llarp_rc *rc, const char *our_rc_file);

@ -375,9 +375,13 @@ llarp_main_getLocalRC(struct llarp_main *ptr)
iter.visit = &iter_config; iter.visit = &iter_config;
llarp_config_iter(ctx->config, &iter); llarp_config_iter(ctx->config, &iter);
*/ */
llarp_rc *rc = new llarp_rc;
llarp_rc_new(rc);
llarp::LogInfo("Loading ", ptr->ctx->conatctFile); llarp::LogInfo("Loading ", ptr->ctx->conatctFile);
llarp_rc *rc = llarp_rc_read(ptr->ctx->conatctFile); if(llarp_rc_read(ptr->ctx->conatctFile, rc))
return rc; return rc;
else
return nullptr;
} }
void void

@ -203,19 +203,20 @@ struct llarp_nodedb
return false; return false;
} }
#endif #endif
llarp_rc *rc = llarp_rc_read(fpath.c_str()); llarp_rc rc;
if(!rc) llarp_rc_clear(&rc);
if(!llarp_rc_read(fpath.c_str(), &rc))
{ {
llarp::LogError("Signature read failed", fpath); llarp::LogError("Signature read failed", fpath);
return false; return false;
} }
if(!llarp_rc_verify_sig(crypto, rc)) if(!llarp_rc_verify_sig(crypto, &rc))
{ {
llarp::LogError("Signature verify failed", fpath); llarp::LogError("Signature verify failed", fpath);
return false; return false;
} }
llarp::PubKey pk(rc->pubkey); llarp::PubKey pk(rc.pubkey);
entries[pk] = *rc; entries[pk] = rc;
return true; return true;
} }

@ -165,18 +165,23 @@ llarp_router::HandleDHTLookupForSendTo(llarp_router_lookup_job *job)
void void
llarp_router::try_connect(fs::path rcfile) llarp_router::try_connect(fs::path rcfile)
{ {
llarp_rc *remote = new llarp_rc; llarp_rc remote;
llarp_rc_new(remote); llarp_rc_new(&remote);
remote = llarp_rc_read(rcfile.c_str()); if(!llarp_rc_read(rcfile.c_str(), &remote))
if(!remote)
{ {
llarp::LogError("failure to decode or verify of remote RC"); llarp::LogError("failure to decode or verify of remote RC");
return; return;
} }
if(llarp_rc_verify_sig(&crypto, remote)) if(llarp_rc_verify_sig(&crypto, &remote))
{ {
llarp::LogDebug("verified signature"); 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? // or error?
llarp::LogWarn("session already made"); llarp::LogWarn("session already made");
@ -184,7 +189,7 @@ llarp_router::try_connect(fs::path rcfile)
} }
else else
llarp::LogError("failed to verify signature of RC", rcfile); llarp::LogError("failed to verify signature of RC", rcfile);
llarp_rc_free(remote); llarp_rc_free(&remote);
} }
bool bool
@ -907,21 +912,21 @@ llarp_rc_set_pubkey(struct llarp_rc *rc, const uint8_t *pubenckey,
llarp_rc_set_pubsigkey(rc, pubsigkey); llarp_rc_set_pubsigkey(rc, pubsigkey);
} }
struct llarp_rc * bool
llarp_rc_read(const char *fpath) llarp_rc_read(const char *fpath, llarp_rc *result)
{ {
fs::path our_rc_file(fpath); fs::path our_rc_file(fpath);
std::error_code ec; std::error_code ec;
if(!fs::exists(our_rc_file, ec)) if(!fs::exists(our_rc_file, ec))
{ {
printf("File[%s] not found\n", fpath); printf("File[%s] not found\n", fpath);
return 0; return false;
} }
std::ifstream f(our_rc_file, std::ios::binary); std::ifstream f(our_rc_file, std::ios::binary);
if(!f.is_open()) if(!f.is_open())
{ {
printf("Can't open file [%s]\n", fpath); printf("Can't open file [%s]\n", fpath);
return 0; return false;
} }
byte_t tmp[MAX_RC_SIZE]; byte_t tmp[MAX_RC_SIZE];
llarp_buffer_t buf = llarp::StackBuffer< decltype(tmp) >(tmp); 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); f.seekg(0, std::ios::beg);
if(sz > buf.sz) if(sz > buf.sz)
return 0; return false;
f.read((char *)buf.base, sz); f.read((char *)buf.base, sz);
// printf("contents[%s]\n", tmpc); // printf("contents[%s]\n", tmpc);
llarp_rc *rc = new llarp_rc; llarp::Zero(result, sizeof(llarp_rc));
llarp::Zero(rc, sizeof(llarp_rc)); if(!llarp_rc_bdecode(result, &buf))
if(!llarp_rc_bdecode(rc, &buf))
{ {
printf("Can't decode [%s]\n", fpath); printf("Can't decode [%s]\n", fpath);
return 0; return false;
} }
return rc; return true;
} }
bool bool

Loading…
Cancel
Save