aboutsummaryrefslogtreecommitdiff
path: root/src/p2p/net_peerlist.h
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2017-05-27 11:35:54 +0100
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2017-05-27 11:35:54 +0100
commit072102cfd23c47d36f4d51875026339a767f22ae (patch)
tree3005737650989527f71b2c742696e6dfb0a2a7f6 /src/p2p/net_peerlist.h
parentMerge pull request #2015 (diff)
downloadmonero-072102cfd23c47d36f4d51875026339a767f22ae.tar.xz
abstracted nework addresses
All code which was using ip and port now uses a new IPv4 object, subclass of a new network_address class. This will allow easy addition of I2P addresses later (and also IPv6, etc). Both old style and new style peer lists are now sent in the P2P protocol, which is inefficient but allows peers using both codebases to talk to each other. This will be removed in the future. No other subclasses than IPv4 exist yet.
Diffstat (limited to 'src/p2p/net_peerlist.h')
-rw-r--r--src/p2p/net_peerlist.h78
1 files changed, 54 insertions, 24 deletions
diff --git a/src/p2p/net_peerlist.h b/src/p2p/net_peerlist.h
index de0f51cc3..a30a05422 100644
--- a/src/p2p/net_peerlist.h
+++ b/src/p2p/net_peerlist.h
@@ -54,7 +54,7 @@
#include "net_peerlist_boost_serialization.h"
-#define CURRENT_PEERLIST_STORAGE_ARCHIVE_VER 5
+#define CURRENT_PEERLIST_STORAGE_ARCHIVE_VER 6
namespace nodetool
{
@@ -78,14 +78,13 @@ namespace nodetool
bool append_with_peer_white(const peerlist_entry& pr);
bool append_with_peer_gray(const peerlist_entry& pr);
bool append_with_peer_anchor(const anchor_peerlist_entry& ple);
- bool set_peer_just_seen(peerid_type peer, uint32_t ip, uint32_t port);
- bool set_peer_just_seen(peerid_type peer, const net_address& addr);
+ bool set_peer_just_seen(peerid_type peer, const epee::net_utils::network_address& addr);
bool set_peer_unreachable(const peerlist_entry& pr);
- bool is_ip_allowed(uint32_t ip);
+ bool is_host_allowed(const epee::net_utils::network_address &address);
bool get_random_gray_peer(peerlist_entry& pe);
bool remove_from_peer_gray(const peerlist_entry& pe);
bool get_and_empty_anchor_peerlist(std::vector<anchor_peerlist_entry>& apl);
- bool remove_from_peer_anchor(const net_address& addr);
+ bool remove_from_peer_anchor(const epee::net_utils::network_address& addr);
private:
struct by_time{};
@@ -130,7 +129,7 @@ namespace nodetool
peerlist_entry,
boost::multi_index::indexed_by<
// access by peerlist_entry::net_adress
- boost::multi_index::ordered_unique<boost::multi_index::tag<by_addr>, boost::multi_index::member<peerlist_entry,net_address,&peerlist_entry::adr> >,
+ boost::multi_index::ordered_unique<boost::multi_index::tag<by_addr>, boost::multi_index::member<peerlist_entry,epee::net_utils::network_address,&peerlist_entry::adr> >,
// sort by peerlist_entry::last_seen<
boost::multi_index::ordered_non_unique<boost::multi_index::tag<by_time>, boost::multi_index::member<peerlist_entry,int64_t,&peerlist_entry::last_seen> >
>
@@ -142,7 +141,7 @@ namespace nodetool
// access by peerlist_entry::id<
boost::multi_index::ordered_unique<boost::multi_index::tag<by_id>, boost::multi_index::member<peerlist_entry,uint64_t,&peerlist_entry::id> >,
// access by peerlist_entry::net_adress
- boost::multi_index::ordered_unique<boost::multi_index::tag<by_addr>, boost::multi_index::member<peerlist_entry,net_address,&peerlist_entry::adr> >,
+ boost::multi_index::ordered_unique<boost::multi_index::tag<by_addr>, boost::multi_index::member<peerlist_entry,epee::net_utils::network_address,&peerlist_entry::adr> >,
// sort by peerlist_entry::last_seen<
boost::multi_index::ordered_non_unique<boost::multi_index::tag<by_time>, boost::multi_index::member<peerlist_entry,int64_t,&peerlist_entry::last_seen> >
>
@@ -152,16 +151,45 @@ namespace nodetool
anchor_peerlist_entry,
boost::multi_index::indexed_by<
// access by anchor_peerlist_entry::net_adress
- boost::multi_index::ordered_unique<boost::multi_index::tag<by_addr>, boost::multi_index::member<anchor_peerlist_entry,net_address,&anchor_peerlist_entry::adr> >,
+ boost::multi_index::ordered_unique<boost::multi_index::tag<by_addr>, boost::multi_index::member<anchor_peerlist_entry,epee::net_utils::network_address,&anchor_peerlist_entry::adr> >,
// sort by anchor_peerlist_entry::first_seen
boost::multi_index::ordered_non_unique<boost::multi_index::tag<by_time>, boost::multi_index::member<anchor_peerlist_entry,int64_t,&anchor_peerlist_entry::first_seen> >
>
> anchor_peers_indexed;
public:
+ template <class Archive, class List, class Element, class t_version_type>
+ void serialize_peers(Archive &a, List &list, Element ple, const t_version_type ver)
+ {
+ if (typename Archive::is_saving())
+ {
+ uint64_t size = list.size();
+ a & size;
+ for (auto p: list)
+ {
+ a & p;
+ }
+ }
+ else
+ {
+ uint64_t size;
+ a & size;
+ list.clear();
+ while (size--)
+ {
+ a & ple;
+ list.insert(ple);
+ }
+ }
+ }
+
template <class Archive, class t_version_type>
void serialize(Archive &a, const t_version_type ver)
{
+ // at v6, we drop existing peerlists, because annoying change
+ if (ver < 6)
+ return;
+
if(ver < 3)
return;
CRITICAL_REGION_LOCAL(m_peerlist_lock);
@@ -174,14 +202,25 @@ namespace nodetool
return;
}
+#if 0
+ // trouble loading more than one peer, can't find why
a & m_peers_white;
a & m_peers_gray;
+#else
+ serialize_peers(a, m_peers_white, peerlist_entry(), ver);
+ serialize_peers(a, m_peers_gray, peerlist_entry(), ver);
+#endif
if(ver < 5) {
return;
}
+#if 0
+ // trouble loading more than one peer, can't find why
a & m_peers_anchor;
+#else
+ serialize_peers(a, m_peers_anchor, anchor_peerlist_entry(), ver);
+#endif
}
private:
@@ -284,13 +323,13 @@ namespace nodetool
}
//--------------------------------------------------------------------------------------------------
inline
- bool peerlist_manager::is_ip_allowed(uint32_t ip)
+ bool peerlist_manager::is_host_allowed(const epee::net_utils::network_address &address)
{
//never allow loopback ip
- if(epee::net_utils::is_ip_loopback(ip))
+ if(address.is_loopback())
return false;
- if(!m_allow_local_ip && epee::net_utils::is_ip_local(ip))
+ if(!m_allow_local_ip && address.is_local())
return false;
return true;
@@ -336,16 +375,7 @@ namespace nodetool
}
//--------------------------------------------------------------------------------------------------
inline
- bool peerlist_manager::set_peer_just_seen(peerid_type peer, uint32_t ip, uint32_t port)
- {
- net_address addr;
- addr.ip = ip;
- addr.port = port;
- return set_peer_just_seen(peer, addr);
- }
- //--------------------------------------------------------------------------------------------------
- inline
- bool peerlist_manager::set_peer_just_seen(peerid_type peer, const net_address& addr)
+ bool peerlist_manager::set_peer_just_seen(peerid_type peer, const epee::net_utils::network_address& addr)
{
TRY_ENTRY();
CRITICAL_REGION_LOCAL(m_peerlist_lock);
@@ -362,7 +392,7 @@ namespace nodetool
bool peerlist_manager::append_with_peer_white(const peerlist_entry& ple)
{
TRY_ENTRY();
- if(!is_ip_allowed(ple.adr.ip))
+ if(!is_host_allowed(ple.adr))
return true;
CRITICAL_REGION_LOCAL(m_peerlist_lock);
@@ -392,7 +422,7 @@ namespace nodetool
bool peerlist_manager::append_with_peer_gray(const peerlist_entry& ple)
{
TRY_ENTRY();
- if(!is_ip_allowed(ple.adr.ip))
+ if(!is_host_allowed(ple.adr))
return true;
CRITICAL_REGION_LOCAL(m_peerlist_lock);
@@ -496,7 +526,7 @@ namespace nodetool
}
//--------------------------------------------------------------------------------------------------
inline
- bool peerlist_manager::remove_from_peer_anchor(const net_address& addr)
+ bool peerlist_manager::remove_from_peer_anchor(const epee::net_utils::network_address& addr)
{
TRY_ENTRY();