diff --git a/include/llarp/router_contact.h b/include/llarp/router_contact.h index c1712a0b5..bced33a70 100644 --- a/include/llarp/router_contact.h +++ b/include/llarp/router_contact.h @@ -69,6 +69,9 @@ 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_write(struct llarp_rc *rc, const char *our_rc_file); diff --git a/llarp/router.cpp b/llarp/router.cpp index 0c9339c52..28768ce71 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -805,6 +805,43 @@ llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath, return false; } +struct llarp_rc * +llarp_rc_read(const char *fpath) +{ + 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; + } + std::ifstream f(our_rc_file, std::ios::binary); + if(!f.is_open()) + { + printf("Can't open file [%s]\n", fpath); + return 0; + } + byte_t tmp[MAX_RC_SIZE]; + llarp_buffer_t 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 0; + + 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)) + { + printf("Can't decode [%s]\n", fpath); + return 0; + } + return rc; +} + bool llarp_rc_write(struct llarp_rc *rc, const char *fpath) {