├── CMakeLists.txt
├── tests
├── CMakeLists.txt
├── test_main.cc
├── test_root_routing.cc
├── test_multi_routing.cc
└── test_root_routing_manager.cc
├── xwrouter.xcodeproj
├── xcuserdata
│ └── imac.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
└── project.pbxproj
├── demo
├── CMakeLists.txt
├── main.h
├── commands.h
└── demo_routing.h
├── root
├── root_message_handler.h
├── root_routing_manager.h
└── root_routing.h
├── register_message_handler.h
├── wrouter_utils
├── wrouter_base_routing.h
└── wrouter_utils.h
├── src
├── register_message_handler.cc
├── root_message_handler.cc
├── rumor_message_handler.cc
├── small_net_cache.cc
├── xwrouter_handler.cc
├── register_routing_table.cc
├── xwrouter.cc
├── root_routing_manager.cc
├── multi_routing.cc
├── xwrouter_xip_handler.cc
└── root_routing.cc
├── multi_routing
├── small_net_cache.h
└── multi_routing.h
├── message_handler
├── rumor_message_handler.h
├── xwrouter_xip_handler.h
├── xwrouter_handler.h
├── xwrouter_xid_handler.h
└── wrouter_message_handler.h
├── register_routing_table.h
├── README.md
└── xwrouter.h
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.8)
2 |
3 | aux_source_directory(./src xwrouter_src)
4 | add_library(xwrouter STATIC ${xwrouter_src})
5 |
6 | add_dependencies(xwrouter xkad)
7 |
8 | target_link_libraries(xwrouter PRIVATE xkad xgossip cpp_redis tacopie)
9 | if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
10 | if (XENABLE_CODE_COVERAGE)
11 | target_link_libraries(xwrouter PRIVATE gcov)
12 | endif()
13 | endif()
14 |
--------------------------------------------------------------------------------
/tests/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | aux_source_directory(./ xwrouter_test_dir)
2 |
3 | add_executable(xwrouter_test ${xwrouter_test_dir})
4 |
5 | add_dependencies(xwrouter_test xwrouter xxbase)
6 |
7 | target_link_libraries(xwrouter_test
8 | xwrouter
9 | xkad
10 | xtransport
11 | xpbase
12 | xledger
13 | xcrypto
14 | xutility
15 | xxbase
16 | common
17 | protobuf
18 | -lgtest
19 | -lgmock
20 | -lpthread -ldl
21 | -lrt
22 | )
23 |
24 |
--------------------------------------------------------------------------------
/xwrouter.xcodeproj/xcuserdata/imac.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | xwrouter.xcscheme_^#shared#^_
8 |
9 | orderHint
10 | 7
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/demo/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | aux_source_directory(./ xwrouter_demo_dir)
2 | add_executable(xwrouter_demo ${xwrouter_demo_dir})
3 |
4 | add_definitions(
5 | -DUSE_REDIS
6 | )
7 |
8 | add_dependencies(xwrouter_demo xgossip xkad xxbase xpbase)
9 |
10 | target_link_libraries(xwrouter_demo
11 | xwrouter
12 | xgossip
13 | xkad
14 | xstobject
15 | xtransport
16 | xpbase
17 | xcrypto
18 | xutility
19 | xxbase
20 | xledger
21 | protobuf
22 | -lgtest
23 | -lgmock
24 | -lpthread -ldl
25 | -lrt
26 | )
27 |
28 |
--------------------------------------------------------------------------------
/root/root_message_handler.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #pragma once
6 |
7 | #include "xbase/xpacket.h"
8 | #include "xkad/routing_table/routing_utils.h"
9 | #include "xkad/proto/kadmlia.pb.h"
10 | #include "xkad/routing_table/callback_manager.h"
11 |
12 | namespace top {
13 |
14 | namespace wrouter {
15 |
16 | class RootMessageHandler {
17 | public:
18 | RootMessageHandler();
19 | ~RootMessageHandler();
20 | void HandleMessage(transport::protobuf::RoutingMessage& message, base::xpacket_t& packet);
21 |
22 | private:
23 |
24 | DISALLOW_COPY_AND_ASSIGN(RootMessageHandler);
25 | };
26 |
27 | } // namespace wrouter
28 |
29 | } // namespace top
30 |
--------------------------------------------------------------------------------
/register_message_handler.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #pragma once
6 |
7 | #include
8 |
9 | #include "xkad/proto/kadmlia.pb.h"
10 | #include "xbase/xpacket.h"
11 | #include "xtransport/transport_message_register.h"
12 |
13 | namespace top {
14 |
15 | namespace wrouter {
16 |
17 | void WrouterRegisterMessageHandler(int msg_type, transport::HandlerProc handler_proc);
18 | void WrouterUnregisterMessageHandler(int msg_type);
19 | void WrouterRegisterMessageRequestType(int msg_type, int request_type);
20 | void WrouterUnregisterMessageRequestType(int msg_type);
21 | int WrouterGetRequestType(int msg_type);
22 | void WrouterSelfHandleMessage(
23 | transport::protobuf::RoutingMessage& message,
24 | base::xpacket_t& packet);
25 |
26 | } // namespace wrouter
27 |
28 | } // namespace top
29 |
--------------------------------------------------------------------------------
/tests/test_main.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #include
6 | #include
7 |
8 | #include "xpbase/base/top_log.h"
9 | #include "xpbase/base/top_config.h"
10 | #include "xkad/routing_table/routing_utils.h"
11 |
12 | namespace top {
13 |
14 | std::shared_ptr global_xid;
15 | uint32_t gloabl_platform_type = kPlatform;
16 | }
17 |
18 | int main(int argc, char *argv[]) {
19 | xinit_log("bitvpn_ut.log", true, true);
20 | xset_log_level(enum_xlog_level_debug);
21 | top::base::Config config;
22 | config.Init("./conf.ut/test_routing_table.conf");
23 | top::kadmlia::CreateGlobalXid(config);
24 |
25 | testing::GTEST_FLAG(output) = "xml:";
26 | testing::InitGoogleTest(&argc, argv);
27 | ::testing::InitGoogleMock(&argc, argv);
28 | int ret = RUN_ALL_TESTS();
29 | TOP_INFO("exit");
30 | return ret;
31 | }
32 |
33 |
--------------------------------------------------------------------------------
/wrouter_utils/wrouter_base_routing.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #pragma once
6 |
7 |
8 | #pragma once
9 |
10 | #include
11 | #include
12 | #include
13 | #include
14 |
15 | #include "xpbase/base/top_utils.h"
16 | #include "xpbase/base/top_config.h"
17 | #include "xkad/routing_table/routing_table.h"
18 | #include "xwrouter/wrouter_utils/wrouter_utils.h"
19 |
20 | namespace top {
21 |
22 | namespace wrouter {
23 |
24 | class WrouterBaseRouting : public kadmlia::RoutingTable {
25 | public:
26 | WrouterBaseRouting(
27 | std::shared_ptr transport,
28 | uint32_t kad_key_size,
29 | kadmlia::LocalNodeInfoPtr local_node_ptr)
30 | : kadmlia::RoutingTable(transport, kad_key_size, local_node_ptr) {}
31 | virtual ~WrouterBaseRouting() {}
32 |
33 | protected:
34 | DISALLOW_COPY_AND_ASSIGN(WrouterBaseRouting);
35 | };
36 |
37 | } // namespace wrouter
38 |
39 | } // namespace top
40 |
--------------------------------------------------------------------------------
/wrouter_utils/wrouter_utils.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #pragma once
6 |
7 | #include "xkad/routing_table/routing_utils.h"
8 |
9 | namespace top {
10 |
11 | namespace wrouter {
12 |
13 | enum RoutingMessageRequestType {
14 | kNone = 0,
15 | kRequestMsg,
16 | kResponseMsg,
17 | };
18 |
19 | enum ClientRelayMessageCode {
20 | kErrorReturn = 1,
21 | kContinueReturn,
22 | kFirstRelayRetrun,
23 | };
24 |
25 | enum enum_platform_xpacket_flag {
26 | kPlatformXpacketInvalid = 100,
27 | kPlatformXpacketRoot,
28 | kPlatformXpacketKadControl,
29 | kPlatformXpacketNatDetect,
30 | };
31 |
32 | enum enum_platform_xpacket_p2p_factor {
33 | kP2pRandomNumGeneral = 4,
34 | };
35 |
36 | // get closest node from routing table factor
37 | enum enum_platform_xpacket_broadcast_factor {
38 | kBroadcastGeneral = 1,
39 | kBroadcastMax = kadmlia::kRoutingMaxNodesSize,
40 | };
41 |
42 | enum enum_platform_xpacket_judge_own {
43 | kJudgeOwnError = 0,
44 | kJudgeOwnYes,
45 | kJudgeOwnYesAndContinue,
46 | kJudgeOwnNoAndContinue,
47 | kJudgeOwnNo,
48 | };
49 |
50 | enum enum_platform_xpacket_recv {
51 | kRecvError = 0,
52 | kRecvOwn,
53 | kRecvOk,
54 | };
55 |
56 | } // namespace wrouter
57 |
58 | } // namespace top
59 |
--------------------------------------------------------------------------------
/src/register_message_handler.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #include "xwrouter/register_message_handler.h"
6 |
7 | #include "xwrouter/message_handler/wrouter_message_handler.h"
8 | #include "xwrouter/message_handler/rumor_message_handler.h"
9 |
10 | namespace top {
11 |
12 | namespace wrouter {
13 |
14 | void WrouterRegisterMessageHandler(int msg_type, transport::HandlerProc handler_proc) {
15 | WrouterMessageHandler::Instance()->AddHandler(msg_type, handler_proc);
16 | }
17 |
18 | void WrouterUnregisterMessageHandler(int msg_type) {
19 | WrouterMessageHandler::Instance()->RemoveHandler(msg_type);
20 | }
21 |
22 | void WrouterRegisterMessageRequestType(int msg_type, int request_type) {
23 | WrouterMessageHandler::Instance()->AddRequestType(msg_type, request_type);
24 | }
25 |
26 | void WrouterUnregisterMessageRequestType(int msg_type) {
27 | WrouterMessageHandler::Instance()->RemoveRequestType(msg_type);
28 | }
29 |
30 | int WrouterGetRequestType(int msg_type) {
31 | return WrouterMessageHandler::Instance()->GetRequestType(msg_type);
32 | }
33 |
34 | void WrouterSelfHandleMessage(transport::protobuf::RoutingMessage& message, base::xpacket_t& packet) {
35 | return WrouterMessageHandler::Instance()->HandleMessage(message, packet);
36 | }
37 |
38 | } // namespace wrouter
39 |
40 | } // namespace top
41 |
--------------------------------------------------------------------------------
/multi_routing/small_net_cache.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #pragma once
6 |
7 | #include
8 | #include
9 | #include
10 | #include
11 |
12 | #include "xpbase/base/xip_parser.h"
13 | #include "xpbase/base/top_timer.h"
14 |
15 |
16 | namespace top {
17 |
18 | namespace wrouter {
19 |
20 | typedef struct NetNode {
21 | std::string m_account;
22 | std::string m_public_key;
23 | base::XipParser m_xip;
24 | std::chrono::steady_clock::time_point time_point;
25 | } NetNode;
26 |
27 | class SmallNetNodes {
28 | public:
29 | static SmallNetNodes* Instance();
30 | bool Init();
31 |
32 | uint32_t AddNode(NetNode node);
33 | bool FindNode(const std::string& account, NetNode& Fnode);
34 | bool FindNode(uint32_t index, NetNode& Fnode, uint64_t service_type);
35 | bool FindRandomNode(NetNode& Fnode, uint64_t service_type);
36 | bool FindAllNode(std::vector& node_vec, uint64_t service_type);
37 | void do_clear_and_reset();
38 |
39 | private:
40 | SmallNetNodes();
41 | ~SmallNetNodes();
42 |
43 | private:
44 | std::mutex net_nodes_cache_map_mutex_;
45 | // key is service_type
46 | std::unordered_map> net_nodes_cache_map_;
47 | std::shared_ptr clear_timer_{nullptr};
48 | };
49 |
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/root_message_handler.cc:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #include "xwrouter/root/root_message_handler.h"
6 |
7 | #include "xpbase/base/top_log.h"
8 | #include "xkad/routing_table/routing_table.h"
9 | #include "xwrouter/register_routing_table.h"
10 | #include "xwrouter/register_message_handler.h"
11 | #include "xwrouter/wrouter_utils/wrouter_utils.h"
12 |
13 | namespace top {
14 |
15 | namespace wrouter {
16 |
17 | RootMessageHandler::RootMessageHandler() {
18 | WrouterRegisterMessageHandler(kRootMessage, [this](
19 | transport::protobuf::RoutingMessage& message,
20 | base::xpacket_t& packet) {
21 | HandleMessage(message, packet);
22 | });
23 | }
24 |
25 | RootMessageHandler::~RootMessageHandler() {
26 | WrouterUnregisterMessageHandler(kRootMessage);
27 | }
28 |
29 | void RootMessageHandler::HandleMessage(
30 | transport::protobuf::RoutingMessage& message,
31 | base::xpacket_t& packet) {
32 | auto routing_table = GetRoutingTable(
33 | message.des_service_type(),
34 | message.has_is_root() && message.is_root());
35 | if (!routing_table) {
36 | TOP_ERROR("service type[%llu] has not register routing table.",
37 | message.des_service_type());
38 | return;
39 | }
40 | routing_table->HandleMessage(message, packet);
41 | }
42 |
43 | } // namespace wrouter
44 |
45 | } // namespace top
46 |
--------------------------------------------------------------------------------
/message_handler/rumor_message_handler.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #pragma once
6 | #include
7 | #include
8 |
9 | #include "xwrouter/register_message_handler.h"
10 | #include "xkad/routing_table/routing_utils.h"
11 | #include "xtransport/transport_message_register.h"
12 | #include "xpbase/base/manager_template.h"
13 | #include "xkad/gossip/rumor_def.h"
14 | #include "xkad/gossip/rumor_filter.h"
15 |
16 | namespace top {
17 | namespace gossip {
18 |
19 | class RumorMessageHandler : public ManagerTemplate {
20 | public:
21 | static RumorMessageHandler* Instance();
22 | void AddHandle(uint32_t message_type, transport::HandlerProc proc);
23 | void RemoveHandle(int32_t message_type);
24 | void SetMaxHopNum(const int32_t);
25 | private:
26 | RumorMessageHandler() :
27 | max_hop_num_(kDefautHopCount) {}
28 | ~RumorMessageHandler() {}
29 | bool CheckMessage(
30 | int32_t,
31 | const transport::protobuf::RoutingMessage&) const;
32 | bool IsHopExpired(
33 | const int32_t) const;
34 | void HandleMessage(
35 | transport::protobuf::RoutingMessage&,
36 | base::xpacket_t&);
37 | std::atomic max_hop_num_;
38 | transport::MessageManagerIntf* message_manager_{transport::MessageManagerIntf::Instance()};
39 | };
40 | typedef std::shared_ptr RumorMessageHandlerSptr;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/demo/main.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #pragma once
6 |
7 | #include
8 | #include
9 | #include // NOLINT
10 |
11 | #include "xpbase/base/line_parser.h"
12 | #include "xpbase/base/top_utils.h"
13 | #include "xpbase/base/top_timer.h"
14 | #include "xpbase/base/top_log.h"
15 | #include "xpbase/base/args_parser.h"
16 | #include "xpbase/base/top_config.h"
17 | #include "xpbase/base/check_cast.h"
18 | #include "xpbase/base/endpoint_util.h"
19 | #include "xpbase/base/kad_key/platform_kadmlia_key.h"
20 | #include "xpbase/base/redis_client.h"
21 | #include "xtransport/udp_transport/udp_transport.h"
22 | #include "xkad/routing_table/routing_table.h"
23 | #include "xkad/routing_table/routing_utils.h"
24 | #include "commands.h"
25 | #include "xkad/routing_table/local_node_info.h"
26 | #include "xwrouter/register_routing_table.h"
27 | #include "xwrouter/root/root_routing_manager.h"
28 | #include "xwrouter/multi_routing/small_net_cache.h"
29 | #include "xwrouter/root/root_routing.h"
30 | #include "xtransport/message_manager/multi_message_handler.h"
31 | #include "xkad/nat_detect/nat_manager_intf.h"
32 | #include "xwrouter/xwrouter.h"
33 | #include "cpp_redis/core/client.hpp"
34 | #include "xgossip/include/block_sync_manager.h"
35 | #include "xtransport/udp_config.h"
36 | #include "xstobject/xledger_db.h"
37 | #include "xwrouter/multi_routing/small_net_cache.h"
38 | #include "demo_routing.h"
39 |
40 | namespace top {
41 |
42 | static const std::string kKadmliaKeyDbKey = "KADMLIA_KEY_DB_KEY";
43 |
44 | /*
45 | int KadKey_GetFromDb(
46 | base::KadmliaKeyPtr& kadkey,
47 | std::shared_ptr db_face,
48 | const std::string& db_field);
49 |
50 | int KadKey_StoreInDb(
51 | base::KadmliaKeyPtr& kadkey,
52 | std::shared_ptr db_face,
53 | const std::string& db_field);
54 | */
55 |
56 | } // namespace top
57 |
--------------------------------------------------------------------------------
/register_routing_table.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #pragma once
6 |
7 | #include
8 | #include
9 | #include
10 |
11 | #include "xkad/proto/kadmlia.pb.h"
12 | #include "xkad/routing_table/routing_utils.h"
13 | #include "xbase/xpacket.h"
14 | #include "xpbase/base/kad_key/get_kadmlia_key.h"
15 | #include "xwrouter/root/root_routing.h"
16 |
17 | namespace top {
18 |
19 | namespace kadmlia {
20 | class RoutingTable;
21 | class LocalNodeInfo;
22 | class CallbackManager;
23 | struct NodeInfo;
24 |
25 | typedef std::shared_ptr NodeInfoPtr;
26 | }
27 |
28 | namespace transport {
29 | class UdpTransport;
30 | }
31 |
32 | namespace wrouter {
33 |
34 | class RootRoutingManager;
35 |
36 | void RegisterRoutingTable(uint64_t type, std::shared_ptr routing_table);
37 | void UnregisterRoutingTable(uint64_t type);
38 | void UnregisterAllRoutingTable();
39 | std::shared_ptr GetRoutingTable(const uint64_t& type, bool root = false);
40 | std::shared_ptr GetRoutingTable(const std::string& routing_id, bool root = false);
41 | void SetRootRoutingManager(std::shared_ptr root_manager_ptr);
42 | void GetAllRegisterType(std::vector& vec_type);
43 | bool CheckTypeExist(uint64_t type);
44 | std::shared_ptr GetSmartRoutingTable(uint64_t type);
45 | uint64_t TryGetSmartRoutingTable(uint64_t type);
46 | bool SetCacheServiceType(uint64_t service_type);
47 | bool GetServiceBootstrapRootNetwork(
48 | uint64_t service_type,
49 | std::set>& boot_endpoints);
50 |
51 | int NetworkExists(
52 | base::KadmliaKeyPtr& kad_key_ptr,
53 | std::set>& endpoints);
54 | int GetSameNetworkPublicEndpoints(
55 | base::KadmliaKeyPtr& kad_key_ptr,
56 | std::set>& boot_endpoints);
57 | int GetSameNetworkNodes(
58 | base::KadmliaKeyPtr& kad_key_ptr,
59 | std::vector& ret_nodes);
60 |
61 | } // namespace wrouter
62 |
63 | } // namespace top
64 |
--------------------------------------------------------------------------------
/root/root_routing_manager.h:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2017-2019 Telos Foundation & contributors
2 | // Distributed under the MIT software license, see the accompanying
3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 |
5 | #pragma once
6 |
7 | #include