From 2044a2c8480917451515dd598185ddd168000026 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Wed, 21 Feb 2018 04:24:12 +0100 Subject: [PATCH] Use retrocompatible iterators Explain why shuffling the local addresses list before looking for a viable local is a bad idea. --- libretroshare/src/pqi/p3netmgr.cc | 28 +++++++++++++++++++++------- libretroshare/src/pqi/pqinetwork.cc | 5 +++-- libretroshare/src/pqi/pqinetwork.h | 7 ++++--- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index a7b76823f..2a73e109b 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -25,6 +25,7 @@ */ #include +#include #include "pqi/p3netmgr.h" @@ -1018,13 +1019,25 @@ bool p3NetMgrIMPL::checkNetAddress() /* TODO: Sat Oct 24 15:51:24 CEST 2015 The fact of having just one local * address is a flawed assumption, this should be redesigned as soon as * possible. It will require complete reenginering of the network layer - * code. - */ - std::list addrs; + * code. */ + + std::vector addrs; if (getLocalAddresses(addrs)) { - for (auto&& addr : addrs) + + /* To work around one address limitation, to shuffle the list of + * local addresses in the hope that with enough time every local + * address is advertised to trusted nodes so they may try to + * connect to all of them including the most convenient if a local + * connection exists, is a bad idea. It would cause net reset every + * time a different local address is selected, potentially breaking + * broader RS assumptions. + */ + //std::random_shuffle(addrs.begin(), addrs.end()); + + for (auto it = addrs.begin(); it!=addrs.end(); ++it) { + sockaddr_storage& addr(*it); if( sockaddr_storage_isValidNet(addr) && !sockaddr_storage_isLoopbackNet(addr) && !sockaddr_storage_isLinkLocalNet(addr)) @@ -1035,10 +1048,11 @@ bool p3NetMgrIMPL::checkNetAddress() } } - // If no satisfactory local address has been found yet relax and - // accept also link local addresses - if(!validAddr) for (auto&& addr : addrs) + /* If no satisfactory local address has been found yet relax and + * accept also link local addresses */ + if(!validAddr) for (auto it = addrs.begin(); it!=addrs.end(); ++it) { + sockaddr_storage& addr(*it); if( sockaddr_storage_isValidNet(addr) && !sockaddr_storage_isLoopbackNet(addr) ) { diff --git a/libretroshare/src/pqi/pqinetwork.cc b/libretroshare/src/pqi/pqinetwork.cc index f15692e06..c302123a4 100644 --- a/libretroshare/src/pqi/pqinetwork.cc +++ b/libretroshare/src/pqi/pqinetwork.cc @@ -3,7 +3,8 @@ * * 3P/PQI network interface for RetroShare. * - * Copyright 2004-2006 by Robert Fernie. + * Copyright (C) 2004-2006 Robert Fernie. + * Copyright (C) 2015-2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -284,7 +285,7 @@ int inet_aton(const char *name, struct in_addr *addr) # include #endif // WINDOWS_SYS -bool getLocalAddresses(std::list & addrs) +bool getLocalAddresses(std::vector& addrs) { addrs.clear(); diff --git a/libretroshare/src/pqi/pqinetwork.h b/libretroshare/src/pqi/pqinetwork.h index 7cb7f93cf..21d8ee81c 100644 --- a/libretroshare/src/pqi/pqinetwork.h +++ b/libretroshare/src/pqi/pqinetwork.h @@ -3,7 +3,8 @@ * * 3P/PQI network interface for RetroShare. * - * Copyright 2004-2006 by Robert Fernie. + * Copyright (C) 2004-2006 Robert Fernie. + * Copyright (C) 2015-2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -24,10 +25,10 @@ */ - #ifndef MRK_PQI_NETWORKING_HEADER #define MRK_PQI_NETWORKING_HEADER +#include /********************************** WINDOWS/UNIX SPECIFIC PART ******************/ #ifndef WINDOWS_SYS @@ -95,7 +96,7 @@ void showSocketError(std::string &out); std::string socket_errorType(int err); -bool getLocalAddresses(std::list & addrs); +bool getLocalAddresses(std::vector & addrs); /* universal socket interface */