Use retrocompatible iterators

Explain why shuffling the local addresses list before looking for
 a viable local is a bad idea.
This commit is contained in:
Gioacchino Mazzurco 2018-02-21 04:24:12 +01:00
parent 0c99975800
commit 2044a2c848
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
3 changed files with 28 additions and 12 deletions

View File

@ -25,6 +25,7 @@
*/ */
#include <time.h> #include <time.h>
#include <vector>
#include "pqi/p3netmgr.h" #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 /* 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 * address is a flawed assumption, this should be redesigned as soon as
* possible. It will require complete reenginering of the network layer * possible. It will require complete reenginering of the network layer
* code. * code. */
*/
std::list<sockaddr_storage> addrs; std::vector<sockaddr_storage> addrs;
if (getLocalAddresses(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) && if( sockaddr_storage_isValidNet(addr) &&
!sockaddr_storage_isLoopbackNet(addr) && !sockaddr_storage_isLoopbackNet(addr) &&
!sockaddr_storage_isLinkLocalNet(addr)) !sockaddr_storage_isLinkLocalNet(addr))
@ -1035,10 +1048,11 @@ bool p3NetMgrIMPL::checkNetAddress()
} }
} }
// If no satisfactory local address has been found yet relax and /* If no satisfactory local address has been found yet relax and
// accept also link local addresses * accept also link local addresses */
if(!validAddr) for (auto&& addr : addrs) if(!validAddr) for (auto it = addrs.begin(); it!=addrs.end(); ++it)
{ {
sockaddr_storage& addr(*it);
if( sockaddr_storage_isValidNet(addr) && if( sockaddr_storage_isValidNet(addr) &&
!sockaddr_storage_isLoopbackNet(addr) ) !sockaddr_storage_isLoopbackNet(addr) )
{ {

View File

@ -3,7 +3,8 @@
* *
* 3P/PQI network interface for RetroShare. * 3P/PQI network interface for RetroShare.
* *
* Copyright 2004-2006 by Robert Fernie. * Copyright (C) 2004-2006 Robert Fernie.
* Copyright (C) 2015-2018 Gioacchino Mazzurco <gio@eigenlab.org>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public * 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 <net/if.h> # include <net/if.h>
#endif // WINDOWS_SYS #endif // WINDOWS_SYS
bool getLocalAddresses(std::list<sockaddr_storage> & addrs) bool getLocalAddresses(std::vector<sockaddr_storage>& addrs)
{ {
addrs.clear(); addrs.clear();

View File

@ -3,7 +3,8 @@
* *
* 3P/PQI network interface for RetroShare. * 3P/PQI network interface for RetroShare.
* *
* Copyright 2004-2006 by Robert Fernie. * Copyright (C) 2004-2006 Robert Fernie.
* Copyright (C) 2015-2018 Gioacchino Mazzurco <gio@eigenlab.org>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public * modify it under the terms of the GNU Library General Public
@ -24,10 +25,10 @@
*/ */
#ifndef MRK_PQI_NETWORKING_HEADER #ifndef MRK_PQI_NETWORKING_HEADER
#define MRK_PQI_NETWORKING_HEADER #define MRK_PQI_NETWORKING_HEADER
#include <vector>
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ /********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS #ifndef WINDOWS_SYS
@ -95,7 +96,7 @@ void showSocketError(std::string &out);
std::string socket_errorType(int err); std::string socket_errorType(int err);
bool getLocalAddresses(std::list<struct sockaddr_storage> & addrs); bool getLocalAddresses(std::vector<sockaddr_storage> & addrs);
/* universal socket interface */ /* universal socket interface */