diff --git a/CMakeLists.txt b/CMakeLists.txt index 120c1ab53..f95e62f68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -511,7 +511,6 @@ set(LIB_SRC llarp/encrypted_ack.cpp llarp/encrypted_frame.cpp llarp/establish_job.cpp - llarp/exit.cpp llarp/exit/close_exit.cpp llarp/exit/context.cpp llarp/exit/endpoint.cpp @@ -573,7 +572,6 @@ set(LIB_SRC llarp/routing/path_transfer.cpp llarp/routing_endpoint.cpp llarp/rpc.cpp - llarp/service.cpp llarp/service/Identity.cpp llarp/service/Intro.cpp llarp/service/IntroSet.cpp @@ -608,30 +606,29 @@ set(DNS_SRC ) set(TEST_SRC - test/base32_unittest.cpp - test/dht_unittest.cpp - test/encrypted_frame_unittest.cpp - test/exit_unittest.cpp - test/hiddenservice_unittest.cpp + test/dht/test_llarp_dht.cpp + test/dns/test_llarp_dns_dns.cpp + test/exit/test_llarp_exit_context.cpp + test/link/test_llarp_link.cpp test/main.cpp - test/net_unittest.cpp - test/obtain_exit_unittest.cpp - test/pq_unittest.cpp - test/net_unittest.cpp - test/link_layer_unittest.cpp - test/test_dns_unit.cpp - test/test_dnsc_unit.cpp - test/test_dnsd_unit.cpp - test/test_dnslib.cpp - test/test_llarp_aligned.cpp - test/test_llarp_bencode.cpp + test/routing/llarp_routing_transfer_traffic.cpp + test/routing/test_llarp_routing_obtainexitmessage.cpp + test/service/test_llarp_service_address.cpp + test/service/test_llarp_service_identity.cpp + test/test_llarp_crypto.cpp test/test_llarp_dht_key.cpp - test/test_llarp_queue_manager.cpp - test/test_llarp_queue.cpp - test/test_llarp_thread_pool.cpp - test/test_router_contact.cpp - test/test_service_address.cpp - test/traffic_transfer_unittest.cpp + test/test_llarp_dns.cpp + test/test_llarp_dnsd.cpp + test/test_llarp_encrypted_frame.cpp + test/test_llarp_net_inaddr.cpp + test/test_llarp_net.cpp + test/test_llarp_router_contact.cpp + test/util/test_llarp_util_aligned.cpp + test/util/test_llarp_util_bencode.cpp + test/util/test_llarp_util_encode.cpp + test/util/test_llarp_util_queue_manager.cpp + test/util/test_llarp_util_queue.cpp + test/util/test_llarp_util_thread_pool.cpp ) set(TEST_EXE testAll) @@ -674,7 +671,7 @@ else() target_link_libraries(${ABYSS_EXE} ${UTIL_LIB} ${PLATFORM_LIB} ws2_32 iphlpapi) endif(NOT WIN32) - set(TEST_SRC ${TEST_SRC} test/jsonrpc_unittest.cpp) + set(TEST_SRC ${TEST_SRC} test/test_libabyss.cpp) # for freebsd if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") include_directories(/usr/local/include) diff --git a/include/llarp.hpp b/include/llarp.hpp index 64c3b1629..ba569ea10 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -1,8 +1,7 @@ #ifndef LLARP_HPP #define LLARP_HPP -#include -#include +#include #include #include @@ -12,9 +11,16 @@ struct llarp_config; struct llarp_config_iterator; +struct llarp_ev_loop; +struct llarp_nodedb; +struct llarp_nodedb_iter; namespace llarp { + class Logic; + struct Router; + struct RouterContact; + struct Context { ~Context(); @@ -40,7 +46,7 @@ namespace llarp LoadDatabase(); int - IterateDatabase(struct llarp_nodedb_iter i); + IterateDatabase(llarp_nodedb_iter &i); bool PutDatabase(struct llarp::RouterContact &rc); diff --git a/llarp/address_info.hpp b/llarp/address_info.hpp index a434dd02c..c387f2f8a 100644 --- a/llarp/address_info.hpp +++ b/llarp/address_info.hpp @@ -1,7 +1,6 @@ #ifndef LLARP_AI_HPP #define LLARP_AI_HPP -#include #include #include #include diff --git a/llarp/context.cpp b/llarp/context.cpp index 67dfb345e..8c2e5f95d 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -1,11 +1,15 @@ +#include +#include + +#include #include #include #include -#include -#include -#include #include #include +#include + +#include #include #include // for MIN @@ -103,7 +107,7 @@ namespace llarp } int - Context::IterateDatabase(struct llarp_nodedb_iter i) + Context::IterateDatabase(llarp_nodedb_iter &i) { return nodedb->iterate_all(i); } diff --git a/llarp/crypto.h b/llarp/crypto.h deleted file mode 100644 index bea89e531..000000000 --- a/llarp/crypto.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef LLARP_CRYPTO_H_ -#define LLARP_CRYPTO_H_ -#ifdef __cplusplus -#include "crypto.hpp" -#endif -#endif diff --git a/llarp/crypto.hpp b/llarp/crypto.hpp index 8e881e97c..9e6c3ec28 100644 --- a/llarp/crypto.hpp +++ b/llarp/crypto.hpp @@ -13,7 +13,7 @@ #include /** - * crypto.h + * crypto.hpp * * libsodium abstraction layer * potentially allow libssl support in the future diff --git a/llarp/crypto_libsodium.cpp b/llarp/crypto_libsodium.cpp index afcb9d6b3..826c510a4 100644 --- a/llarp/crypto_libsodium.cpp +++ b/llarp/crypto_libsodium.cpp @@ -1,4 +1,3 @@ -#include #include #include #include diff --git a/llarp/dht.hpp b/llarp/dht.hpp deleted file mode 100644 index 6347888c5..000000000 --- a/llarp/dht.hpp +++ /dev/null @@ -1,5 +0,0 @@ -#include -#include -#include -#include -#include diff --git a/llarp/dns/dns.hpp b/llarp/dns/dns.hpp index d0f1b96e1..01377a4c5 100644 --- a/llarp/dns/dns.hpp +++ b/llarp/dns/dns.hpp @@ -1,10 +1,7 @@ #ifndef LLARP_DNS_DNS_HPP #define LLARP_DNS_DNS_HPP -#include -#include -#include -#include -#include + +#include namespace llarp { diff --git a/llarp/encrypted_frame.hpp b/llarp/encrypted_frame.hpp index 9aae9a1b5..3b516478e 100644 --- a/llarp/encrypted_frame.hpp +++ b/llarp/encrypted_frame.hpp @@ -1,7 +1,7 @@ #ifndef LLARP_ENCRYPTED_FRAME_HPP #define LLARP_ENCRYPTED_FRAME_HPP -#include +#include #include #include #include diff --git a/llarp/exit.cpp b/llarp/exit.cpp deleted file mode 100644 index 2861839af..000000000 --- a/llarp/exit.cpp +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/llarp/exit.hpp b/llarp/exit.hpp deleted file mode 100644 index f46211b4f..000000000 --- a/llarp/exit.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef LLARP_EXIT_HPP -#define LLARP_EXIT_HPP -#include -#include -#include -#endif diff --git a/llarp/exit_info.h b/llarp/exit_info.h deleted file mode 100644 index c2a9a62ce..000000000 --- a/llarp/exit_info.h +++ /dev/null @@ -1,98 +0,0 @@ -#ifndef LLARP_XI_H -#define LLARP_XI_H - -#include -#include -#include - -#ifdef __cplusplus -#include -#include -#include -#endif - -/** - * exit_info.h - * - * utilities for handling exits on the llarp network - */ - -struct llarp_xi; - -bool -llarp_xi_bdecode(struct llarp_xi *xi, llarp_buffer_t *buf); -bool -llarp_xi_bencode(const struct llarp_xi *xi, llarp_buffer_t *buf); - -/// Exit info model -struct llarp_xi -{ - struct in6_addr address; - struct in6_addr netmask; - byte_t pubkey[PUBKEYSIZE]; - -#ifdef __cplusplus - bool - BEncode(llarp_buffer_t *buf) const - { - return llarp_xi_bencode(this, buf); - } - - bool - BDecode(llarp_buffer_t *buf) - { - return llarp_xi_bdecode(this, buf); - } - - friend std::ostream & - operator<<(std::ostream &out, const llarp_xi &xi) - { - char tmp[128] = {0}; - if(inet_ntop(AF_INET6, (void *)&xi.address, tmp, sizeof(tmp))) - out << std::string(tmp); - else - return out; - out << std::string("/"); - return out << std::to_string( - llarp::bits::count_array_bits(xi.netmask.s6_addr)); - } -#endif -}; - -struct llarp_xi_list; - -struct llarp_xi_list * -llarp_xi_list_new(); - -void -llarp_xi_list_free(struct llarp_xi_list *l); - -bool -llarp_xi_list_bdecode(struct llarp_xi_list *l, llarp_buffer_t *buf); - -bool -llarp_xi_list_bencode(struct llarp_xi_list *l, llarp_buffer_t *buf); - -void -llarp_xi_list_pushback(struct llarp_xi_list *l, struct llarp_xi *xi); - -void -llarp_xi_list_copy(struct llarp_xi_list *dst, struct llarp_xi_list *src); - -size_t -llarp_xi_list_size(const struct llarp_xi_list *l); - -void -llarp_xi_copy(struct llarp_xi *dst, struct llarp_xi *src); - -struct llarp_xi_list_iter -{ - void *user; - struct llarp_xi_list *list; - bool (*visit)(struct llarp_xi_list_iter *, struct llarp_xi *); -}; - -void -llarp_xi_list_iterate(struct llarp_xi_list *l, struct llarp_xi_list_iter *iter); - -#endif diff --git a/llarp/messages/dht.hpp b/llarp/messages/dht.hpp index 6233410b4..c1383ac52 100644 --- a/llarp/messages/dht.hpp +++ b/llarp/messages/dht.hpp @@ -1,6 +1,7 @@ #ifndef LLARP_MESSAGES_DHT_HPP #define LLARP_MESSAGES_DHT_HPP -#include + +#include #include #include @@ -25,13 +26,12 @@ namespace llarp bool HandleMessage(IMessageHandler* h, llarp::Router* r) const override; - void - Clear() override + void + Clear() override { M.clear(); V = 0; } - }; } // namespace routing } // namespace llarp diff --git a/llarp/messages/dht_immediate.hpp b/llarp/messages/dht_immediate.hpp index 70c1d625b..3cb129b2a 100644 --- a/llarp/messages/dht_immediate.hpp +++ b/llarp/messages/dht_immediate.hpp @@ -1,6 +1,7 @@ #ifndef LLARP_MESSAGES_DHT_IMMEDIATE_HPP #define LLARP_MESSAGES_DHT_IMMEDIATE_HPP -#include + +#include #include #include diff --git a/llarp/nodedb.cpp b/llarp/nodedb.cpp index a65f561bf..9eba9fe0a 100644 --- a/llarp/nodedb.cpp +++ b/llarp/nodedb.cpp @@ -202,7 +202,7 @@ llarp_nodedb::visit(std::function< bool(const llarp::RouterContact &) > visit) } bool -llarp_nodedb::iterate(struct llarp_nodedb_iter &i) +llarp_nodedb::iterate(llarp_nodedb_iter &i) { i.index = 0; llarp::util::Lock lock(access); diff --git a/llarp/nodedb.hpp b/llarp/nodedb.hpp index f2e533e79..a5f63271f 100644 --- a/llarp/nodedb.hpp +++ b/llarp/nodedb.hpp @@ -1,7 +1,7 @@ #ifndef LLARP_NODEDB_HPP #define LLARP_NODEDB_HPP -#include +#include #include #include #include @@ -82,7 +82,7 @@ struct llarp_nodedb visit(std::function< bool(const llarp::RouterContact &) > visit); bool - iterate(struct llarp_nodedb_iter &i); + iterate(llarp_nodedb_iter &i); void set_dir(const char *dir); diff --git a/llarp/path.hpp b/llarp/path.hpp index ca1357654..4f0f0c3ca 100644 --- a/llarp/path.hpp +++ b/llarp/path.hpp @@ -2,7 +2,6 @@ #define LLARP_PATH_HPP #include -#include #include #include #include diff --git a/llarp/path_types.hpp b/llarp/path_types.hpp index ce11ae460..0bfa31570 100644 --- a/llarp/path_types.hpp +++ b/llarp/path_types.hpp @@ -1,8 +1,8 @@ #ifndef LLARP_PATH_TYPES_HPP #define LLARP_PATH_TYPES_HPP +#include #include -#include namespace llarp { diff --git a/llarp/pow.hpp b/llarp/pow.hpp index 7925f5eeb..7ed2cc294 100644 --- a/llarp/pow.hpp +++ b/llarp/pow.hpp @@ -1,6 +1,6 @@ #ifndef LLARP_POW_HPP #define LLARP_POW_HPP -#include +#include #include #include diff --git a/llarp/router.cpp b/llarp/router.cpp index 5175627db..9bb17dd41 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include diff --git a/llarp/router.hpp b/llarp/router.hpp index 3ac4067f8..2a5f648ae 100644 --- a/llarp/router.hpp +++ b/llarp/router.hpp @@ -4,10 +4,9 @@ #include #include #include -#include #include #include -#include +#include #include #include #include @@ -19,7 +18,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/llarp/router_contact.hpp b/llarp/router_contact.hpp index 950157a1c..c14c49688 100644 --- a/llarp/router_contact.hpp +++ b/llarp/router_contact.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include diff --git a/llarp/routing/handler.hpp b/llarp/routing/handler.hpp index cdb1907e4..de6f7ff06 100644 --- a/llarp/routing/handler.hpp +++ b/llarp/routing/handler.hpp @@ -1,7 +1,6 @@ #ifndef LLARP_ROUTING_HANDLER_HPP #define LLARP_ROUTING_HANDLER_HPP -#include #include #include #include @@ -19,7 +18,6 @@ namespace llarp struct IMessageHandler { - virtual bool HandleObtainExitMessage(const ObtainExitMessage *msg, llarp::Router *r) = 0; diff --git a/llarp/service.cpp b/llarp/service.cpp deleted file mode 100644 index a341e63d6..000000000 --- a/llarp/service.cpp +++ /dev/null @@ -1,376 +0,0 @@ -#include - -#include -#include -#include - -namespace llarp -{ - namespace service - { - IntroSet::~IntroSet() - { - if(W) - delete W; - } - - bool - IntroSet::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) - { - bool read = false; - if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf)) - return false; - - if(llarp_buffer_eq(key, "i")) - { - return BEncodeReadList(I, buf); - } - if(!BEncodeMaybeReadDictEntry("k", K, read, key, buf)) - return false; - - if(!BEncodeMaybeReadDictEntry("n", topic, read, key, buf)) - return false; - - if(!BEncodeMaybeReadDictInt("t", T, read, key, buf)) - return false; - - if(llarp_buffer_eq(key, "w")) - { - if(W) - delete W; - W = new PoW(); - return W->BDecode(buf); - } - - if(!BEncodeMaybeReadDictInt("v", version, read, key, buf)) - return false; - - if(!BEncodeMaybeReadDictEntry("z", Z, read, key, buf)) - return false; - - return read; - } - - bool - IntroSet::BEncode(llarp_buffer_t* buf) const - { - if(!bencode_start_dict(buf)) - return false; - if(!BEncodeWriteDictEntry("a", A, buf)) - return false; - // start introduction list - if(!bencode_write_bytestring(buf, "i", 1)) - return false; - if(!BEncodeWriteList(I.begin(), I.end(), buf)) - return false; - // end introduction list - - // pq pubkey - if(!BEncodeWriteDictEntry("k", K, buf)) - return false; - - // topic tag - if(topic.ToString().size()) - { - if(!BEncodeWriteDictEntry("n", topic, buf)) - return false; - } - // Timestamp published - if(!BEncodeWriteDictInt("t", T, buf)) - return false; - - // write version - if(!BEncodeWriteDictInt("v", version, buf)) - return false; - if(W) - { - if(!BEncodeWriteDictEntry("w", *W, buf)) - return false; - } - if(!BEncodeWriteDictEntry("z", Z, buf)) - return false; - - return bencode_end(buf); - } - - bool - IntroSet::HasExpiredIntros(llarp_time_t now) const - { - for(const auto& i : I) - if(now >= i.expiresAt) - return true; - return false; - } - - bool - IntroSet::IsExpired(llarp_time_t now) const - { - return GetNewestIntroExpiration() < now; - } - - Introduction::~Introduction() - { - } - - bool - Introduction::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) - { - bool read = false; - if(!BEncodeMaybeReadDictEntry("k", router, read, key, buf)) - return false; - if(!BEncodeMaybeReadDictInt("l", latency, read, key, buf)) - return false; - if(!BEncodeMaybeReadDictEntry("p", pathID, read, key, buf)) - return false; - if(!BEncodeMaybeReadDictInt("v", version, read, key, buf)) - return false; - if(!BEncodeMaybeReadDictInt("x", expiresAt, read, key, buf)) - return false; - return read; - } - - bool - Introduction::BEncode(llarp_buffer_t* buf) const - { - if(!bencode_start_dict(buf)) - return false; - - if(!BEncodeWriteDictEntry("k", router, buf)) - return false; - if(latency) - { - if(!BEncodeWriteDictInt("l", latency, buf)) - return false; - } - if(!BEncodeWriteDictEntry("p", pathID, buf)) - return false; - if(!BEncodeWriteDictInt("v", version, buf)) - return false; - if(!BEncodeWriteDictInt("x", expiresAt, buf)) - return false; - return bencode_end(buf); - } - - void - Introduction::Clear() - { - router.Zero(); - pathID.Zero(); - latency = 0; - expiresAt = 0; - } - - Identity::~Identity() - { - } - - bool - Identity::BEncode(llarp_buffer_t* buf) const - { - if(!bencode_start_dict(buf)) - return false; - if(!BEncodeWriteDictEntry("e", enckey, buf)) - return false; - if(!BEncodeWriteDictEntry("q", pq, buf)) - return false; - if(!BEncodeWriteDictEntry("s", signkey, buf)) - return false; - if(!BEncodeWriteDictInt("v", version, buf)) - return false; - if(!BEncodeWriteDictEntry("x", vanity, buf)) - return false; - return bencode_end(buf); - } - - bool - Identity::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) - { - bool read = false; - if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, buf)) - return false; - if(llarp_buffer_eq(key, "q")) - { - llarp_buffer_t str; - if(!bencode_read_string(buf, &str)) - return false; - if(str.sz == 3200 || str.sz == 2818) - { - pq = str.base; - return true; - } - else - return false; - } - if(!BEncodeMaybeReadDictEntry("s", signkey, read, key, buf)) - return false; - if(!BEncodeMaybeReadDictInt("v", version, read, key, buf)) - return false; - if(!BEncodeMaybeReadDictEntry("x", vanity, read, key, buf)) - return false; - return read; - } - - void - Identity::RegenerateKeys(llarp::Crypto* crypto) - { - crypto->encryption_keygen(enckey); - crypto->identity_keygen(signkey); - pub.Update(llarp::seckey_topublic(enckey), - llarp::seckey_topublic(signkey)); - crypto->pqe_keygen(pq); - } - - bool - Identity::KeyExchange(path_dh_func dh, SharedSecret& result, - const ServiceInfo& other, - const KeyExchangeNonce& N) const - { - return dh(result, other.EncryptionPublicKey(), enckey, N); - } - - bool - Identity::Sign(Crypto* c, Signature& sig, llarp_buffer_t buf) const - { - return c->sign(sig, signkey, buf); - } - - bool - Identity::EnsureKeys(const std::string& fname, llarp::Crypto* c) - { - byte_t tmp[4096]; - auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); - std::error_code ec; - // check for file - if(!fs::exists(fname, ec)) - { - if(ec) - { - llarp::LogError(ec); - return false; - } - // regen and encode - RegenerateKeys(c); - if(!BEncode(&buf)) - return false; - // rewind - buf.sz = buf.cur - buf.base; - buf.cur = buf.base; - // write - std::ofstream f; - f.open(fname, std::ios::binary); - if(!f.is_open()) - return false; - f.write((char*)buf.cur, buf.sz); - } - // read file - std::ifstream inf(fname, std::ios::binary); - inf.seekg(0, std::ios::end); - size_t sz = inf.tellg(); - inf.seekg(0, std::ios::beg); - - if(sz > sizeof(tmp)) - return false; - // decode - inf.read((char*)buf.base, sz); - if(!BDecode(&buf)) - return false; - - ServiceInfo::OptNonce van; - if(!vanity.IsZero()) - van = vanity; - // update pubkeys - pub.Update(llarp::seckey_topublic(enckey), - llarp::seckey_topublic(signkey), van); - return true; - } - - bool - Identity::SignIntroSet(IntroSet& i, llarp::Crypto* crypto, - llarp_time_t now) const - { - if(i.I.size() == 0) - return false; - // set timestamp - // TODO: round to nearest 1000 ms - i.T = now; - // set service info - i.A = pub; - // set public encryption key - i.K = pq_keypair_to_public(pq); - // zero out signature for signing process - i.Z.Zero(); - byte_t tmp[MAX_INTROSET_SIZE]; - auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); - if(!i.BEncode(&buf)) - return false; - // rewind and resize buffer - buf.sz = buf.cur - buf.base; - buf.cur = buf.base; - return Sign(crypto, i.Z, buf); - } - - bool - IntroSet::Verify(llarp::Crypto* crypto, llarp_time_t now) const - { - byte_t tmp[MAX_INTROSET_SIZE]; - auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); - IntroSet copy; - copy = *this; - copy.Z.Zero(); - if(!copy.BEncode(&buf)) - return false; - // rewind and resize buffer - buf.sz = buf.cur - buf.base; - buf.cur = buf.base; - if(!A.Verify(crypto, buf, Z)) - return false; - // validate PoW - if(W && !W->IsValid(crypto->shorthash, now)) - return false; - // valid timestamps - // add max clock skew - now += MAX_INTROSET_TIME_DELTA; - for(const auto& intro : I) - { - if(intro.expiresAt > now - && intro.expiresAt - now > DEFAULT_PATH_LIFETIME) - { - if(W && intro.expiresAt - W->extendedLifetime > DEFAULT_PATH_LIFETIME) - return false; - else if(W == nullptr) - { - llarp::LogWarn("intro has too high expire time"); - return false; - } - } - } - if(IsExpired(now)) - { - llarp::LogWarn("introset expired: ", *this); - return false; - } - return true; - } - - llarp_time_t - IntroSet::GetNewestIntroExpiration() const - { - llarp_time_t t = 0; - for(const auto& intro : I) - t = std::max(intro.expiresAt, t); - return t; - } - - bool - Config::Load(const std::string& fname) - { - ini::Parser parser(fname); - for(const auto& sec : parser.top().ordered_sections) - { - services.push_back({sec->first, sec->second.values}); - } - return services.size() > 0; - } - - } // namespace service -} // namespace llarp diff --git a/llarp/service.hpp b/llarp/service.hpp deleted file mode 100644 index 28f8207ee..000000000 --- a/llarp/service.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef LLARP_SERVICE_HPP -#define LLARP_SERVICE_HPP - -#include -#include -#include -#include -#include -#include -#include - -#include - -#endif diff --git a/llarp/service/Identity.cpp b/llarp/service/Identity.cpp index b05a050b6..a20b72e6d 100644 --- a/llarp/service/Identity.cpp +++ b/llarp/service/Identity.cpp @@ -1 +1,158 @@ #include + +#include + +namespace llarp +{ + namespace service + { + Identity::~Identity() + { + } + + bool + Identity::BEncode(llarp_buffer_t* buf) const + { + if(!bencode_start_dict(buf)) + return false; + if(!BEncodeWriteDictEntry("e", enckey, buf)) + return false; + if(!BEncodeWriteDictEntry("q", pq, buf)) + return false; + if(!BEncodeWriteDictEntry("s", signkey, buf)) + return false; + if(!BEncodeWriteDictInt("v", version, buf)) + return false; + if(!BEncodeWriteDictEntry("x", vanity, buf)) + return false; + return bencode_end(buf); + } + + bool + Identity::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) + { + bool read = false; + if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, buf)) + return false; + if(llarp_buffer_eq(key, "q")) + { + llarp_buffer_t str; + if(!bencode_read_string(buf, &str)) + return false; + if(str.sz == 3200 || str.sz == 2818) + { + pq = str.base; + return true; + } + else + return false; + } + if(!BEncodeMaybeReadDictEntry("s", signkey, read, key, buf)) + return false; + if(!BEncodeMaybeReadDictInt("v", version, read, key, buf)) + return false; + if(!BEncodeMaybeReadDictEntry("x", vanity, read, key, buf)) + return false; + return read; + } + + void + Identity::RegenerateKeys(llarp::Crypto* crypto) + { + crypto->encryption_keygen(enckey); + crypto->identity_keygen(signkey); + pub.Update(llarp::seckey_topublic(enckey), + llarp::seckey_topublic(signkey)); + crypto->pqe_keygen(pq); + } + + bool + Identity::KeyExchange(path_dh_func dh, SharedSecret& result, + const ServiceInfo& other, + const KeyExchangeNonce& N) const + { + return dh(result, other.EncryptionPublicKey(), enckey, N); + } + + bool + Identity::Sign(Crypto* c, Signature& sig, llarp_buffer_t buf) const + { + return c->sign(sig, signkey, buf); + } + + bool + Identity::EnsureKeys(const std::string& fname, llarp::Crypto* c) + { + byte_t tmp[4096]; + auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); + std::error_code ec; + // check for file + if(!fs::exists(fname, ec)) + { + if(ec) + { + llarp::LogError(ec); + return false; + } + // regen and encode + RegenerateKeys(c); + if(!BEncode(&buf)) + return false; + // rewind + buf.sz = buf.cur - buf.base; + buf.cur = buf.base; + // write + std::ofstream f; + f.open(fname, std::ios::binary); + if(!f.is_open()) + return false; + f.write((char*)buf.cur, buf.sz); + } + // read file + std::ifstream inf(fname, std::ios::binary); + inf.seekg(0, std::ios::end); + size_t sz = inf.tellg(); + inf.seekg(0, std::ios::beg); + + if(sz > sizeof(tmp)) + return false; + // decode + inf.read((char*)buf.base, sz); + if(!BDecode(&buf)) + return false; + + ServiceInfo::OptNonce van; + if(!vanity.IsZero()) + van = vanity; + // update pubkeys + pub.Update(llarp::seckey_topublic(enckey), + llarp::seckey_topublic(signkey), van); + return true; + } + + bool + Identity::SignIntroSet(IntroSet& i, llarp::Crypto* crypto, + llarp_time_t now) const + { + if(i.I.size() == 0) + return false; + // set timestamp + // TODO: round to nearest 1000 ms + i.T = now; + // set service info + i.A = pub; + // set public encryption key + i.K = pq_keypair_to_public(pq); + // zero out signature for signing process + i.Z.Zero(); + byte_t tmp[MAX_INTROSET_SIZE]; + auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); + if(!i.BEncode(&buf)) + return false; + // rewind and resize buffer + buf.sz = buf.cur - buf.base; + buf.cur = buf.base; + return Sign(crypto, i.Z, buf); + } + } // namespace service +} // namespace llarp diff --git a/llarp/service/Intro.cpp b/llarp/service/Intro.cpp index 05fcf5825..bb9ed77ac 100644 --- a/llarp/service/Intro.cpp +++ b/llarp/service/Intro.cpp @@ -1 +1,59 @@ #include + +namespace llarp +{ + namespace service + { + Introduction::~Introduction() + { + } + + bool + Introduction::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) + { + bool read = false; + if(!BEncodeMaybeReadDictEntry("k", router, read, key, buf)) + return false; + if(!BEncodeMaybeReadDictInt("l", latency, read, key, buf)) + return false; + if(!BEncodeMaybeReadDictEntry("p", pathID, read, key, buf)) + return false; + if(!BEncodeMaybeReadDictInt("v", version, read, key, buf)) + return false; + if(!BEncodeMaybeReadDictInt("x", expiresAt, read, key, buf)) + return false; + return read; + } + + bool + Introduction::BEncode(llarp_buffer_t* buf) const + { + if(!bencode_start_dict(buf)) + return false; + + if(!BEncodeWriteDictEntry("k", router, buf)) + return false; + if(latency) + { + if(!BEncodeWriteDictInt("l", latency, buf)) + return false; + } + if(!BEncodeWriteDictEntry("p", pathID, buf)) + return false; + if(!BEncodeWriteDictInt("v", version, buf)) + return false; + if(!BEncodeWriteDictInt("x", expiresAt, buf)) + return false; + return bencode_end(buf); + } + + void + Introduction::Clear() + { + router.Zero(); + pathID.Zero(); + latency = 0; + expiresAt = 0; + } + } +} diff --git a/llarp/service/IntroSet.cpp b/llarp/service/IntroSet.cpp index 4faaf671f..33382b1fd 100644 --- a/llarp/service/IntroSet.cpp +++ b/llarp/service/IntroSet.cpp @@ -1 +1,161 @@ #include + +#include + +namespace llarp +{ + namespace service + { + IntroSet::~IntroSet() + { + if(W) + delete W; + } + + bool + IntroSet::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf) + { + bool read = false; + if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf)) + return false; + + if(llarp_buffer_eq(key, "i")) + { + return BEncodeReadList(I, buf); + } + if(!BEncodeMaybeReadDictEntry("k", K, read, key, buf)) + return false; + + if(!BEncodeMaybeReadDictEntry("n", topic, read, key, buf)) + return false; + + if(!BEncodeMaybeReadDictInt("t", T, read, key, buf)) + return false; + + if(llarp_buffer_eq(key, "w")) + { + if(W) + delete W; + W = new PoW(); + return W->BDecode(buf); + } + + if(!BEncodeMaybeReadDictInt("v", version, read, key, buf)) + return false; + + if(!BEncodeMaybeReadDictEntry("z", Z, read, key, buf)) + return false; + + return read; + } + + bool + IntroSet::BEncode(llarp_buffer_t* buf) const + { + if(!bencode_start_dict(buf)) + return false; + if(!BEncodeWriteDictEntry("a", A, buf)) + return false; + // start introduction list + if(!bencode_write_bytestring(buf, "i", 1)) + return false; + if(!BEncodeWriteList(I.begin(), I.end(), buf)) + return false; + // end introduction list + + // pq pubkey + if(!BEncodeWriteDictEntry("k", K, buf)) + return false; + + // topic tag + if(topic.ToString().size()) + { + if(!BEncodeWriteDictEntry("n", topic, buf)) + return false; + } + // Timestamp published + if(!BEncodeWriteDictInt("t", T, buf)) + return false; + + // write version + if(!BEncodeWriteDictInt("v", version, buf)) + return false; + if(W) + { + if(!BEncodeWriteDictEntry("w", *W, buf)) + return false; + } + if(!BEncodeWriteDictEntry("z", Z, buf)) + return false; + + return bencode_end(buf); + } + + bool + IntroSet::HasExpiredIntros(llarp_time_t now) const + { + for(const auto& i : I) + if(now >= i.expiresAt) + return true; + return false; + } + + bool + IntroSet::IsExpired(llarp_time_t now) const + { + return GetNewestIntroExpiration() < now; + } + + bool + IntroSet::Verify(llarp::Crypto* crypto, llarp_time_t now) const + { + byte_t tmp[MAX_INTROSET_SIZE]; + auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); + IntroSet copy; + copy = *this; + copy.Z.Zero(); + if(!copy.BEncode(&buf)) + return false; + // rewind and resize buffer + buf.sz = buf.cur - buf.base; + buf.cur = buf.base; + if(!A.Verify(crypto, buf, Z)) + return false; + // validate PoW + if(W && !W->IsValid(crypto->shorthash, now)) + return false; + // valid timestamps + // add max clock skew + now += MAX_INTROSET_TIME_DELTA; + for(const auto& intro : I) + { + if(intro.expiresAt > now + && intro.expiresAt - now > DEFAULT_PATH_LIFETIME) + { + if(W && intro.expiresAt - W->extendedLifetime > DEFAULT_PATH_LIFETIME) + return false; + else if(W == nullptr) + { + llarp::LogWarn("intro has too high expire time"); + return false; + } + } + } + if(IsExpired(now)) + { + llarp::LogWarn("introset expired: ", *this); + return false; + } + return true; + } + + llarp_time_t + IntroSet::GetNewestIntroExpiration() const + { + llarp_time_t t = 0; + for(const auto& intro : I) + t = std::max(intro.expiresAt, t); + return t; + } + } // namespace service +} // namespace llarp diff --git a/llarp/service/config.cpp b/llarp/service/config.cpp index 592eec747..c4fab9047 100644 --- a/llarp/service/config.cpp +++ b/llarp/service/config.cpp @@ -1 +1,21 @@ #include + +#include + +namespace llarp +{ + namespace service + { + bool + Config::Load(const std::string& fname) + { + ini::Parser parser(fname); + for(const auto& sec : parser.top().ordered_sections) + { + services.push_back({sec->first, sec->second.values}); + } + return services.size() > 0; + } + + } // namespace service +} // namespace llarp diff --git a/llarp/transit_hop.cpp b/llarp/transit_hop.cpp index 00752c64a..bd5e34ed0 100644 --- a/llarp/transit_hop.cpp +++ b/llarp/transit_hop.cpp @@ -1,5 +1,7 @@ -#include #include + +#include +#include #include #include #include diff --git a/test/dht_unittest.cpp b/test/dht/test_llarp_dht.cpp similarity index 95% rename from test/dht_unittest.cpp rename to test/dht/test_llarp_dht.cpp index 521fb0267..1e6038206 100644 --- a/test/dht_unittest.cpp +++ b/test/dht/test_llarp_dht.cpp @@ -1,5 +1,8 @@ +#include +#include +#include + #include -#include using Key_t = llarp::dht::Key_t; @@ -77,7 +80,7 @@ TEST_F(KademliaDHTTest, TestBucketOperators) ASSERT_TRUE((one ^ three) == (three ^ one)); }; -TEST_F(KademliaDHTTest, TestBucketRandomzied_1000) +TEST_F(KademliaDHTTest, TestBucketRandomized_1000) { size_t moreNodes = 100; while(moreNodes--) diff --git a/test/test_dnslib.cpp b/test/dns/test_llarp_dns_dns.cpp similarity index 98% rename from test/test_dnslib.cpp rename to test/dns/test_llarp_dns_dns.cpp index 0a0eccb65..0996a35fe 100644 --- a/test/test_dnslib.cpp +++ b/test/dns/test_llarp_dns_dns.cpp @@ -1,6 +1,9 @@ #include #include +#include +#include +#include #include #include diff --git a/test/exit_unittest.cpp b/test/exit/test_llarp_exit_context.cpp similarity index 96% rename from test/exit_unittest.cpp rename to test/exit/test_llarp_exit_context.cpp index 3edc75cda..4d73ea86a 100644 --- a/test/exit_unittest.cpp +++ b/test/exit/test_llarp_exit_context.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include struct ExitTest : public ::testing::Test diff --git a/test/link_layer_unittest.cpp b/test/link/test_llarp_link.cpp similarity index 99% rename from test/link_layer_unittest.cpp rename to test/link/test_llarp_link.cpp index 840c53438..81aa1683f 100644 --- a/test/link_layer_unittest.cpp +++ b/test/link/test_llarp_link.cpp @@ -1,10 +1,11 @@ -#include #include #include #include #include #include +#include + struct LinkLayerTest : public ::testing::Test { static constexpr uint16_t AlicePort = 5000; diff --git a/test/net_unittest.cpp b/test/net_unittest.cpp deleted file mode 100644 index ba5c99f31..000000000 --- a/test/net_unittest.cpp +++ /dev/null @@ -1,164 +0,0 @@ -#include - -#include -#include - -struct NetTest : public ::testing::Test -{ -}; - -TEST_F(NetTest, TestinAddrFrom4Int) -{ - llarp::inAddr test; - test.from4int(127, 0, 0, 1); - char str[INET6_ADDRSTRLEN]; - if (inet_ntop(AF_INET, &test._addr, str, INET6_ADDRSTRLEN) == NULL) { - ASSERT_TRUE(false); - } - ASSERT_TRUE(strcmp("127.0.0.1", str) == 0); -} - -TEST_F(NetTest, TestinAddrFromStr) -{ - llarp::inAddr test; - test.from_char_array("127.0.0.1"); - char str[INET6_ADDRSTRLEN]; - if (inet_ntop(AF_INET, &test._addr, str, INET6_ADDRSTRLEN) == NULL) { - ASSERT_TRUE(false); - } - ASSERT_TRUE(strcmp("127.0.0.1", str) == 0); -} - -TEST_F(NetTest, TestinAddrReset) -{ - llarp::inAddr test; - test.from_char_array("127.0.0.1"); - test.reset(); - char str[INET6_ADDRSTRLEN]; - if (inet_ntop(AF_INET, &test._addr, str, INET6_ADDRSTRLEN) == NULL) { - ASSERT_TRUE(false); - } - ASSERT_TRUE(strcmp("0.0.0.0", str) == 0); -} - -TEST_F(NetTest, TestinAddrModeSet) -{ - llarp::inAddr test; - test.from_char_array("127.0.0.1"); - //test.hexDebug(); - ASSERT_TRUE(test.isIPv4Mode()); - - // corrupt it - test._addr.s6_addr[10] = 0xfe; - test._addr.s6_addr[11] = 0xfe; - - test.setIPv4Mode(); - //test.hexDebug(); - ASSERT_TRUE(test.isIPv4Mode()); -} - -TEST_F(NetTest, TestinAddrSIIT) -{ - llarp::inAddr test; - test.from_char_array("127.0.0.1"); - - test.toSIIT(); - //test.hexDebug(); - ASSERT_TRUE(llarp::ipv6_is_siit(test._addr)); - - test.fromSIIT(); - //test.hexDebug(); - ASSERT_TRUE(!llarp::ipv6_is_siit(test._addr)); -} - - -TEST_F(NetTest, TestinAddrN32) -{ - llarp::inAddr test; - test.from_char_array("127.0.0.1"); - llarp::nuint32_t netOrder = test.toN32(); - llarp::inAddr test2; - test2.fromN32(netOrder); - char str[INET6_ADDRSTRLEN]; - if (inet_ntop(AF_INET, &test2._addr, str, INET6_ADDRSTRLEN) == NULL) { - ASSERT_TRUE(false); - } - //printf("[%s]\n", str); - ASSERT_TRUE(strcmp("127.0.0.1", str) == 0); -} - -TEST_F(NetTest, TestinAddrH32) -{ - llarp::inAddr test; - test.from_char_array("127.0.0.1"); - llarp::huint32_t netOrder = test.toH32(); - llarp::inAddr test2; - test2.fromH32(netOrder); - char str[INET6_ADDRSTRLEN]; - if (inet_ntop(AF_INET, &test2._addr, str, INET6_ADDRSTRLEN) == NULL) { - ASSERT_TRUE(false); - } - //printf("[%s]\n", str); - ASSERT_TRUE(strcmp("127.0.0.1", str) == 0); -} - - -TEST_F(NetTest, TestRangeContains8) -{ - ASSERT_TRUE(llarp::iprange_ipv4(10, 0, 0, 1, 8) - .Contains(llarp::ipaddr_ipv4_bits(10, 40, 11, 6))); -} - -TEST_F(NetTest, TestRangeContains24) -{ - ASSERT_TRUE(llarp::iprange_ipv4(10, 200, 0, 1, 24) - .Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253))); -} - -TEST_F(NetTest, TestRangeContainsFail) -{ - ASSERT_TRUE(!llarp::iprange_ipv4(192, 168, 0, 1, 24) - .Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253))); -} - - -TEST_F(NetTest, TestIPv4Netmask) -{ - ASSERT_TRUE(llarp::netmask_ipv4_bits(8) - == llarp::huint32_t{0xFF000000}); - ASSERT_TRUE(llarp::netmask_ipv4_bits(24) - == llarp::huint32_t{0xFFFFFF00}); -}; - -TEST_F(NetTest, TestBogon_10_8) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(10, 40, 11, 6))); -}; - -TEST_F(NetTest, TestBogon_192_168_16) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(192, 168, 1, 111))); -}; - -TEST_F(NetTest, TestBogon_DoD_8) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(21, 3, 37, 70))); -}; - -TEST_F(NetTest, TestBogon_127_8) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(127, 0, 0, 1))); -}; - -TEST_F(NetTest, TestBogon_0_8) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(0, 0, 0, 0))); -}; - -TEST_F(NetTest, TestBogon_NonBogon) -{ - ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(1, 1, 1, 1))); - ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(8, 8, 6, 6))); - ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(141, 55, 12, 99))); - ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(79, 12, 3, 4))); -} diff --git a/test/traffic_transfer_unittest.cpp b/test/routing/llarp_routing_transfer_traffic.cpp similarity index 100% rename from test/traffic_transfer_unittest.cpp rename to test/routing/llarp_routing_transfer_traffic.cpp diff --git a/test/obtain_exit_unittest.cpp b/test/routing/test_llarp_routing_obtainexitmessage.cpp similarity index 100% rename from test/obtain_exit_unittest.cpp rename to test/routing/test_llarp_routing_obtainexitmessage.cpp diff --git a/test/test_service_address.cpp b/test/service/test_llarp_service_address.cpp similarity index 100% rename from test/test_service_address.cpp rename to test/service/test_llarp_service_address.cpp diff --git a/test/hiddenservice_unittest.cpp b/test/service/test_llarp_service_identity.cpp similarity index 89% rename from test/hiddenservice_unittest.cpp rename to test/service/test_llarp_service_identity.cpp index 24578bad4..88c5bc2e0 100644 --- a/test/hiddenservice_unittest.cpp +++ b/test/service/test_llarp_service_identity.cpp @@ -1,6 +1,10 @@ #include -#include +#include +#include +#include +#include +#include #include struct HiddenServiceTest : public ::testing::Test diff --git a/test/test_dnsc_unit.cpp b/test/test_dnsc_unit.cpp deleted file mode 100644 index 9799e6792..000000000 --- a/test/test_dnsc_unit.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -#include -#include // for llarp_main_init -#include // for threadpool/llarp::Logic -#include // for llarp::Addr diff --git a/test/test_dtlslink_unit.cpp b/test/test_dtlslink_unit.cpp deleted file mode 100644 index b284d5c60..000000000 --- a/test/test_dtlslink_unit.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - -struct DTLSTest : public ::testing::Test -{ -}; - -TEST_F(DTLSTest, TestAliceConnectToBob) -{ -} diff --git a/test/jsonrpc_unittest.cpp b/test/test_libabyss.cpp similarity index 100% rename from test/jsonrpc_unittest.cpp rename to test/test_libabyss.cpp diff --git a/test/pq_unittest.cpp b/test/test_llarp_crypto.cpp similarity index 100% rename from test/pq_unittest.cpp rename to test/test_llarp_crypto.cpp diff --git a/test/test_dns_unit.cpp b/test/test_llarp_dns.cpp similarity index 100% rename from test/test_dns_unit.cpp rename to test/test_llarp_dns.cpp diff --git a/test/test_dnsd_unit.cpp b/test/test_llarp_dnsd.cpp similarity index 99% rename from test/test_dnsd_unit.cpp rename to test/test_llarp_dnsd.cpp index c8d2692b2..4e47700b0 100644 --- a/test/test_dnsd_unit.cpp +++ b/test/test_llarp_dnsd.cpp @@ -1,9 +1,10 @@ #include +#include + #include // for llarp_main_init #include // for threadpool/llarp::Logic #include // for llarp::Addr -#include unsigned int g_length = 0; std::string g_result = ""; diff --git a/test/encrypted_frame_unittest.cpp b/test/test_llarp_encrypted_frame.cpp similarity index 100% rename from test/encrypted_frame_unittest.cpp rename to test/test_llarp_encrypted_frame.cpp diff --git a/test/test_llarp_net.cpp b/test/test_llarp_net.cpp new file mode 100644 index 000000000..da2093936 --- /dev/null +++ b/test/test_llarp_net.cpp @@ -0,0 +1,65 @@ +#include + +#include +#include + +struct TestNet : public ::testing::Test +{ +}; + +TEST_F(TestNet, TestRangeContains8) +{ + ASSERT_TRUE(llarp::iprange_ipv4(10, 0, 0, 1, 8) + .Contains(llarp::ipaddr_ipv4_bits(10, 40, 11, 6))); +} + +TEST_F(TestNet, TestRangeContains24) +{ + ASSERT_TRUE(llarp::iprange_ipv4(10, 200, 0, 1, 24) + .Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253))); +} + +TEST_F(TestNet, TestRangeContainsFail) +{ + ASSERT_TRUE(!llarp::iprange_ipv4(192, 168, 0, 1, 24) + .Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253))); +} + +TEST_F(TestNet, TestIPv4Netmask) +{ + ASSERT_TRUE(llarp::netmask_ipv4_bits(8) == llarp::huint32_t{0xFF000000}); + ASSERT_TRUE(llarp::netmask_ipv4_bits(24) == llarp::huint32_t{0xFFFFFF00}); +}; + +TEST_F(TestNet, TestBogon_10_8) +{ + ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(10, 40, 11, 6))); +}; + +TEST_F(TestNet, TestBogon_192_168_16) +{ + ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(192, 168, 1, 111))); +}; + +TEST_F(TestNet, TestBogon_DoD_8) +{ + ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(21, 3, 37, 70))); +}; + +TEST_F(TestNet, TestBogon_127_8) +{ + ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(127, 0, 0, 1))); +}; + +TEST_F(TestNet, TestBogon_0_8) +{ + ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(0, 0, 0, 0))); +}; + +TEST_F(TestNet, TestBogon_NonBogon) +{ + ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(1, 1, 1, 1))); + ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(8, 8, 6, 6))); + ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(141, 55, 12, 99))); + ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(79, 12, 3, 4))); +} diff --git a/test/test_llarp_net_inaddr.cpp b/test/test_llarp_net_inaddr.cpp new file mode 100644 index 000000000..dbe020342 --- /dev/null +++ b/test/test_llarp_net_inaddr.cpp @@ -0,0 +1,106 @@ +#include + +#include + +struct TestNetInAddr : public ::testing::Test +{ +}; + +TEST_F(TestNetInAddr, TestinAddrFrom4Int) +{ + llarp::inAddr test; + test.from4int(127, 0, 0, 1); + char str[INET6_ADDRSTRLEN]; + if(inet_ntop(AF_INET, &test._addr, str, INET6_ADDRSTRLEN) == NULL) + { + ASSERT_TRUE(false); + } + ASSERT_TRUE(strcmp("127.0.0.1", str) == 0); +} + +TEST_F(TestNetInAddr, TestinAddrFromStr) +{ + llarp::inAddr test; + test.from_char_array("127.0.0.1"); + char str[INET6_ADDRSTRLEN]; + if(inet_ntop(AF_INET, &test._addr, str, INET6_ADDRSTRLEN) == NULL) + { + ASSERT_TRUE(false); + } + ASSERT_TRUE(strcmp("127.0.0.1", str) == 0); +} + +TEST_F(TestNetInAddr, TestinAddrReset) +{ + llarp::inAddr test; + test.from_char_array("127.0.0.1"); + test.reset(); + char str[INET6_ADDRSTRLEN]; + if(inet_ntop(AF_INET, &test._addr, str, INET6_ADDRSTRLEN) == NULL) + { + ASSERT_TRUE(false); + } + ASSERT_TRUE(strcmp("0.0.0.0", str) == 0); +} + +TEST_F(TestNetInAddr, TestinAddrModeSet) +{ + llarp::inAddr test; + test.from_char_array("127.0.0.1"); + // test.hexDebug(); + ASSERT_TRUE(test.isIPv4Mode()); + + // corrupt it + test._addr.s6_addr[10] = 0xfe; + test._addr.s6_addr[11] = 0xfe; + + test.setIPv4Mode(); + // test.hexDebug(); + ASSERT_TRUE(test.isIPv4Mode()); +} + +TEST_F(TestNetInAddr, TestinAddrSIIT) +{ + llarp::inAddr test; + test.from_char_array("127.0.0.1"); + + test.toSIIT(); + // test.hexDebug(); + ASSERT_TRUE(llarp::ipv6_is_siit(test._addr)); + + test.fromSIIT(); + // test.hexDebug(); + ASSERT_TRUE(!llarp::ipv6_is_siit(test._addr)); +} + +TEST_F(TestNetInAddr, TestinAddrN32) +{ + llarp::inAddr test; + test.from_char_array("127.0.0.1"); + llarp::nuint32_t netOrder = test.toN32(); + llarp::inAddr test2; + test2.fromN32(netOrder); + char str[INET6_ADDRSTRLEN]; + if(inet_ntop(AF_INET, &test2._addr, str, INET6_ADDRSTRLEN) == NULL) + { + ASSERT_TRUE(false); + } + // printf("[%s]\n", str); + ASSERT_TRUE(strcmp("127.0.0.1", str) == 0); +} + +TEST_F(TestNetInAddr, TestinAddrH32) +{ + llarp::inAddr test; + test.from_char_array("127.0.0.1"); + llarp::huint32_t netOrder = test.toH32(); + llarp::inAddr test2; + test2.fromH32(netOrder); + char str[INET6_ADDRSTRLEN]; + if(inet_ntop(AF_INET, &test2._addr, str, INET6_ADDRSTRLEN) == NULL) + { + ASSERT_TRUE(false); + } + // printf("[%s]\n", str); + ASSERT_TRUE(strcmp("127.0.0.1", str) == 0); +} diff --git a/test/test_router_contact.cpp b/test/test_llarp_router_contact.cpp similarity index 100% rename from test/test_router_contact.cpp rename to test/test_llarp_router_contact.cpp diff --git a/test/test_llarp_aligned.cpp b/test/util/test_llarp_util_aligned.cpp similarity index 100% rename from test/test_llarp_aligned.cpp rename to test/util/test_llarp_util_aligned.cpp diff --git a/test/test_llarp_bencode.cpp b/test/util/test_llarp_util_bencode.cpp similarity index 100% rename from test/test_llarp_bencode.cpp rename to test/util/test_llarp_util_bencode.cpp diff --git a/test/base32_unittest.cpp b/test/util/test_llarp_util_encode.cpp similarity index 75% rename from test/base32_unittest.cpp rename to test/util/test_llarp_util_encode.cpp index 671b8ddb5..d8d068bfe 100644 --- a/test/base32_unittest.cpp +++ b/test/util/test_llarp_util_encode.cpp @@ -1,16 +1,13 @@ #include -#include +#include #include -#include struct Base32Test : public ::testing::Test { - Base32Test() : crypto(llarp::Crypto::sodium{}) + Base32Test() { } - - llarp::Crypto crypto; }; TEST_F(Base32Test, Serialize) diff --git a/test/test_llarp_queue.cpp b/test/util/test_llarp_util_queue.cpp similarity index 100% rename from test/test_llarp_queue.cpp rename to test/util/test_llarp_util_queue.cpp diff --git a/test/test_llarp_queue_manager.cpp b/test/util/test_llarp_util_queue_manager.cpp similarity index 100% rename from test/test_llarp_queue_manager.cpp rename to test/util/test_llarp_util_queue_manager.cpp diff --git a/test/test_llarp_thread_pool.cpp b/test/util/test_llarp_util_thread_pool.cpp similarity index 100% rename from test/test_llarp_thread_pool.cpp rename to test/util/test_llarp_util_thread_pool.cpp diff --git a/test/utp_unittest.cpp b/test/utp_unittest.cpp deleted file mode 100644 index 1b8d56e78..000000000 --- a/test/utp_unittest.cpp +++ /dev/null @@ -1,334 +0,0 @@ -#include -#include -#include -#include -#include - -struct UTPTest : public ::testing::Test -{ - using Link_t = llarp::utp::LinkLayer; - - static constexpr uint16_t AlicePort = 5000; - static constexpr uint16_t BobPort = 6000; - - struct Context - { - Context(llarp::Crypto& c) - { - crypto = &c; - crypto->identity_keygen(signingKey); - crypto->encryption_keygen(encryptionKey); - rc.pubkey = llarp::seckey_topublic(signingKey); - rc.enckey = llarp::seckey_topublic(encryptionKey); - } - - llarp::SecretKey signingKey; - llarp::SecretKey encryptionKey; - - llarp::RouterContact rc; - - llarp::Crypto* crypto; - - bool gotLIM = false; - - const llarp::RouterContact& - GetRC() const - { - return rc; - } - - llarp::RouterID - GetRouterID() const - { - return rc.pubkey; - } - - /// regenerate rc and rotate onion key - bool - Regen() - { - crypto->encryption_keygen(encryptionKey); - rc.enckey = llarp::seckey_topublic(encryptionKey); - return rc.Sign(crypto, signingKey); - } - - std::unique_ptr< ILinkLayer > link; - - static std::string - localLoopBack() - { -#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ - || (__APPLE__ && __MACH__) - return "lo0"; -#else - return "lo"; -#endif - } - - bool - Start(llarp::Logic* logic, llarp_ev_loop* loop, uint16_t port) - { - if(!link->Configure(loop, localLoopBack(), AF_INET, port)) - return false; - if(!link->GenEphemeralKeys()) - return false; - rc.addrs.emplace_back(); - if(!link->GetOurAddressInfo(rc.addrs[0])) - return false; - if(!rc.Sign(crypto, signingKey)) - return false; - return link->Start(logic); - } - - void - Stop() - { - link->Stop(); - } - - void - TearDown() - { - Stop(); - link.reset(); - } - }; - - llarp::Crypto crypto; - - Context Alice; - Context Bob; - - bool success = false; - - llarp_ev_loop* netLoop; - - llarp_time_t oldRCLifetime; - - std::unique_ptr< llarp::Logic > logic; - - UTPTest() - : crypto(llarp::Crypto::sodium{}) - , Alice(crypto) - , Bob(crypto) - , netLoop(nullptr) - , oldRCLifetime(llarp::RouterContact::Lifetime) - { - llarp::RouterContact::IgnoreBogons = true; - llarp::RouterContact::Lifetime = 500; - llarp_ev_loop_alloc(&netLoop); - logic.reset(new llarp::Logic()); - } - - ~UTPTest() - { - Alice.TearDown(); - Bob.TearDown(); - logic.reset(); - llarp_ev_loop_free(&netLoop); - llarp::RouterContact::IgnoreBogons = false; - llarp::RouterContact::Lifetime = oldRCLifetime; - } - - static void - OnTimeout(void* u, uint64_t, uint64_t left) - { - if(left) - return; - static_cast< UTPTest* >(u)->Stop(); - } - - void - RunMainloop() - { - logic->call_later({5000, this, &OnTimeout}); - llarp_ev_loop_run_single_process(netLoop, logic->thread, logic.get()); - } - - void - Stop() - { - llarp_ev_loop_stop(netLoop); - } - - bool AliceGotMessage(llarp_buffer_t) - { - success = true; - Stop(); - return true; - } -}; - -TEST_F(UTPTest, TestAliceRenegWithBob) -{ - Alice.link.reset(new Link_t( - &crypto, Alice.encryptionKey, - [&]() -> const llarp::RouterContact& { return Alice.GetRC(); }, - [&](llarp::ILinkSession* s, llarp_buffer_t buf) -> bool { - if(Alice.gotLIM) - { - Alice.Regen(); - return s->RenegotiateSession(); - } - else - { - llarp::LinkIntroMessage msg; - if(!msg.BDecode(&buf)) - return false; - if(!s->GotLIM(&msg)) - return false; - Alice.gotLIM = true; - return true; - } - }, - [&](llarp::Signature& sig, llarp_buffer_t buf) -> bool { - return crypto.sign(sig, Alice.signingKey, buf); - }, - [&](llarp::RouterContact rc) { - ASSERT_EQ(rc, Bob.GetRC()); - llarp::LogInfo("alice established with bob"); - }, - [&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; }, - [&](llarp::ILinkSession* session) { - ASSERT_FALSE(session->IsEstablished()); - Stop(); - }, - [&](llarp::RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); })); - - auto sendDiscardMessage = [](llarp::ILinkSession* s) -> bool { - // send discard message in reply to complete unit test - byte_t tmp[32] = {0}; - auto otherBuf = llarp::StackBuffer< decltype(tmp) >(tmp); - llarp::DiscardMessage discard; - if(!discard.BEncode(&otherBuf)) - return false; - otherBuf.sz = otherBuf.cur - otherBuf.base; - otherBuf.cur = otherBuf.base; - return s->SendMessageBuffer(otherBuf); - }; - - Bob.link.reset(new Link_t( - &crypto, Bob.encryptionKey, - [&]() -> const llarp::RouterContact& { return Bob.GetRC(); }, - [&](llarp::ILinkSession* s, llarp_buffer_t buf) -> bool { - llarp::LinkIntroMessage msg; - if(!msg.BDecode(&buf)) - return false; - if(!s->GotLIM(&msg)) - return false; - Bob.gotLIM = true; - return sendDiscardMessage(s); - }, - [&](llarp::Signature& sig, llarp_buffer_t buf) -> bool { - return crypto.sign(sig, Bob.signingKey, buf); - }, - [&](llarp::RouterContact rc) { - ASSERT_EQ(rc, Alice.GetRC()); - llarp::LogInfo("bob established with alice"); - Bob.link->VisitSessionByPubkey(Alice.GetRC().pubkey.as_array(), - sendDiscardMessage); - }, - [&](llarp::RouterContact newrc, llarp::RouterContact oldrc) -> bool { - success = newrc.pubkey == oldrc.pubkey; - return true; - }, - [&](llarp::ILinkSession* session) { - ASSERT_FALSE(session->IsEstablished()); - }, - [&](llarp::RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); })); - - ASSERT_TRUE(Alice.Start(logic.get(), netLoop, AlicePort)); - ASSERT_TRUE(Bob.Start(logic.get(), netLoop, BobPort)); - - ASSERT_TRUE(Alice.link->TryEstablishTo(Bob.GetRC())); - - RunMainloop(); - ASSERT_TRUE(Alice.gotLIM); - ASSERT_TRUE(Bob.gotLIM); - ASSERT_TRUE(success); -} - -TEST_F(UTPTest, TestAliceConnectToBob) -{ - Alice.link.reset(new Link_t( - &crypto, Alice.encryptionKey, - [&]() -> const llarp::RouterContact& { return Alice.GetRC(); }, - [&](llarp::ILinkSession* s, llarp_buffer_t buf) -> bool { - if(Alice.gotLIM) - { - return AliceGotMessage(buf); - } - else - { - llarp::LinkIntroMessage msg; - if(!msg.BDecode(&buf)) - return false; - if(!s->GotLIM(&msg)) - return false; - Alice.gotLIM = true; - return true; - } - }, - [&](llarp::Signature& sig, llarp_buffer_t buf) -> bool { - return crypto.sign(sig, Alice.signingKey, buf); - }, - [&](llarp::RouterContact rc) { - ASSERT_EQ(rc, Bob.GetRC()); - llarp::LogInfo("alice established with bob"); - }, - [&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; }, - [&](llarp::ILinkSession* session) { - ASSERT_FALSE(session->IsEstablished()); - Stop(); - }, - [&](llarp::RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); })); - - auto sendDiscardMessage = [](llarp::ILinkSession* s) -> bool { - // send discard message in reply to complete unit test - byte_t tmp[32] = {0}; - auto otherBuf = llarp::StackBuffer< decltype(tmp) >(tmp); - llarp::DiscardMessage discard; - if(!discard.BEncode(&otherBuf)) - return false; - otherBuf.sz = otherBuf.cur - otherBuf.base; - otherBuf.cur = otherBuf.base; - return s->SendMessageBuffer(otherBuf); - }; - - Bob.link.reset(new Link_t( - &crypto, Bob.encryptionKey, - [&]() -> const llarp::RouterContact& { return Bob.GetRC(); }, - [&](llarp::ILinkSession* s, llarp_buffer_t buf) -> bool { - llarp::LinkIntroMessage msg; - if(!msg.BDecode(&buf)) - return false; - if(!s->GotLIM(&msg)) - return false; - Bob.gotLIM = true; - return true; - }, - [&](llarp::Signature& sig, llarp_buffer_t buf) -> bool { - return crypto.sign(sig, Bob.signingKey, buf); - }, - [&](llarp::RouterContact rc) { - ASSERT_EQ(rc, Alice.GetRC()); - llarp::LogInfo("bob established with alice"); - Bob.link->VisitSessionByPubkey(Alice.GetRC().pubkey.as_array(), - sendDiscardMessage); - }, - [&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; }, - [&](llarp::ILinkSession* session) { - ASSERT_FALSE(session->IsEstablished()); - }, - [&](llarp::RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); })); - - ASSERT_TRUE(Alice.Start(logic.get(), netLoop, AlicePort)); - ASSERT_TRUE(Bob.Start(logic.get(), netLoop, BobPort)); - - ASSERT_TRUE(Alice.link->TryEstablishTo(Bob.GetRC())); - - RunMainloop(); - ASSERT_TRUE(Alice.gotLIM); - ASSERT_TRUE(Bob.gotLIM); - ASSERT_TRUE(success); -}