2018-05-28 16:03:39 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/pqi: pqiipset.cc *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2007-2008 by Robert Fernie <retroshare@lunamutt.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2010-07-05 07:37:07 -04:00
|
|
|
#include <time.h>
|
2010-07-04 06:15:56 -04:00
|
|
|
#include "pqi/pqiipset.h"
|
2012-04-14 18:38:24 -04:00
|
|
|
#include "util/rsstring.h"
|
2010-07-04 06:15:56 -04:00
|
|
|
|
|
|
|
bool pqiIpAddress::sameAddress(const pqiIpAddress &a) const
|
|
|
|
{
|
2013-09-13 10:35:19 -04:00
|
|
|
return sockaddr_storage_same(mAddr, a.mAddr);
|
2010-07-04 06:15:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool pqiIpAddress::validAddress() const
|
|
|
|
{
|
|
|
|
/* filter for unlikely addresses */
|
2013-09-13 10:35:19 -04:00
|
|
|
if(sockaddr_storage_isLoopbackNet(mAddr))
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddress::validAddress() ip parameter is loopback: disgarding." << std::endl ;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-13 10:35:19 -04:00
|
|
|
if(sockaddr_storage_isnull(mAddr))
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddress::validAddress() ip parameter is 0.0.0.0/1, or port is 0, ignoring." << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool pqiIpAddrList::updateIpAddressList(const pqiIpAddress &addr)
|
|
|
|
{
|
|
|
|
std::list<pqiIpAddress>::iterator it;
|
|
|
|
bool add = false;
|
|
|
|
bool newAddr = true;
|
|
|
|
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (mAddrs.size() < MAX_ADDRESS_LIST_SIZE)
|
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList() small list: Add";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
add = true;
|
|
|
|
}
|
2010-07-04 11:16:52 -04:00
|
|
|
else if (mAddrs.back().mSeenTime < addr.mSeenTime)
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList() oldAddr: Add";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
add = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!add) || (!addr.validAddress()))
|
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList() not Add or !valid.. fail";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = mAddrs.begin(); it != mAddrs.end(); ++it)
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
if (it->sameAddress(addr))
|
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList() found duplicate";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
if (it->mSeenTime > addr.mSeenTime)
|
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList() orig better, returning";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
/* already better -> quit */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList() deleting orig";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
it = mAddrs.erase(it);
|
|
|
|
newAddr = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-04 11:16:52 -04:00
|
|
|
// ordered by decreaseing time. (newest at front)
|
2010-07-04 06:15:56 -04:00
|
|
|
bool added = false;
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = mAddrs.begin(); it != mAddrs.end(); ++it)
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
2010-07-04 11:16:52 -04:00
|
|
|
if (it->mSeenTime < addr.mSeenTime)
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList() added orig SeenTime: " << it->mSeenTime << " new SeenTime: " << addr.mSeenTime;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
added = true;
|
|
|
|
mAddrs.insert(it, addr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!added)
|
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList() pushing to back";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
mAddrs.push_back(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pop if necessary */
|
2010-07-04 11:16:52 -04:00
|
|
|
while (mAddrs.size() > MAX_ADDRESS_LIST_SIZE)
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
2010-07-04 11:16:52 -04:00
|
|
|
std::cerr << "pqiIpAddrList::updateIpAddressList() popping back";
|
2010-07-04 06:15:56 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2010-07-04 11:16:52 -04:00
|
|
|
mAddrs.pop_back();
|
2010-07-04 06:15:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return newAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pqiIpAddrList::extractFromTlv(const RsTlvIpAddrSet &tlvAddrs)
|
|
|
|
{
|
|
|
|
std::list<RsTlvIpAddressInfo>::const_iterator it;
|
|
|
|
|
2014-03-29 11:34:37 -04:00
|
|
|
//for(it = tlvAddrs.addrs.begin(); it != tlvAddrs.addrs.end() ; ++it)
|
|
|
|
for(it = tlvAddrs.mList.begin(); it != tlvAddrs.mList.end() ; ++it)
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
pqiIpAddress addr;
|
2013-09-13 10:35:19 -04:00
|
|
|
addr.mAddr = it->addr.addr;
|
2010-07-04 06:15:56 -04:00
|
|
|
addr.mSeenTime = it->seenTime;
|
|
|
|
addr.mSrc = it->source;
|
|
|
|
|
|
|
|
mAddrs.push_back(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 04:09:59 -04:00
|
|
|
void pqiIpAddrList::loadTlv(RsTlvIpAddrSet &tlvAddrs) const
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
2013-09-28 04:09:59 -04:00
|
|
|
std::list<pqiIpAddress>::const_iterator it;
|
2010-07-04 06:15:56 -04:00
|
|
|
|
|
|
|
for(it = mAddrs.begin(); it != mAddrs.end() ; ++it)
|
|
|
|
{
|
|
|
|
RsTlvIpAddressInfo addr;
|
2013-09-13 10:35:19 -04:00
|
|
|
addr.addr.addr = it->mAddr;
|
2010-07-04 06:15:56 -04:00
|
|
|
addr.seenTime = it->mSeenTime;
|
|
|
|
addr.source = it->mSrc;
|
|
|
|
|
2014-03-29 11:34:37 -04:00
|
|
|
//tlvAddrs.addrs.push_back(addr);
|
|
|
|
tlvAddrs.mList.push_back(addr);
|
2010-07-04 06:15:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
void pqiIpAddrList::printIpAddressList(std::string &out) const
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
std::list<pqiIpAddress>::const_iterator it;
|
|
|
|
time_t now = time(NULL);
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = mAddrs.begin(); it != mAddrs.end(); ++it)
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
2013-09-13 10:35:19 -04:00
|
|
|
out += sockaddr_storage_tostring(it->mAddr);
|
|
|
|
rs_sprintf_append(out, "( %ld old)\n", now - it->mSeenTime);
|
2010-07-04 06:15:56 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool pqiIpAddrSet::updateLocalAddrs(const pqiIpAddress &addr)
|
|
|
|
{
|
|
|
|
return mLocal.updateIpAddressList(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pqiIpAddrSet::updateExtAddrs(const pqiIpAddress &addr)
|
|
|
|
{
|
2010-07-04 09:19:09 -04:00
|
|
|
return mExt.updateIpAddressList(addr);
|
2010-07-04 06:15:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pqiIpAddrSet::updateAddrs(const pqiIpAddrSet &addrs)
|
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrSet::updateAddrs()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool newAddrs = false;
|
|
|
|
std::list<pqiIpAddress>::const_iterator it;
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = addrs.mLocal.mAddrs.begin(); it != addrs.mLocal.mAddrs.end(); ++it)
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
if (mLocal.updateIpAddressList(*it))
|
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrSet::updateAddrs() Updated Local Addr";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
newAddrs = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = addrs.mExt.mAddrs.begin(); it != addrs.mExt.mAddrs.end(); ++it)
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
|
|
|
if (mExt.updateIpAddressList(*it))
|
|
|
|
{
|
|
|
|
#ifdef IPADDR_DEBUG
|
|
|
|
std::cerr << "pqiIpAddrSet::updateAddrs() Updated Ext Addr";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
newAddrs = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newAddrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
void pqiIpAddrSet::printAddrs(std::string &out) const
|
2010-07-04 06:15:56 -04:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
out += "Local Addresses: ";
|
2010-07-04 06:15:56 -04:00
|
|
|
mLocal.printIpAddressList(out);
|
2012-04-14 18:38:24 -04:00
|
|
|
out += "\nExt Addresses: ";
|
2010-07-04 06:15:56 -04:00
|
|
|
mExt.printIpAddressList(out);
|
2012-04-14 18:38:24 -04:00
|
|
|
out += "\n";
|
2010-07-04 06:15:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|