Added the basic filter code to remove dht spies. (bdfilter)

Added initialisation to bdconnection



git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-peernet@4344 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2011-06-27 15:30:49 +00:00
parent 1ba11dd30d
commit 4d1ee57340
4 changed files with 237 additions and 11 deletions

View File

@ -1674,9 +1674,13 @@ std::ostream &operator<<(std::ostream &out, const bdProxyTuple &t)
bdConnection::bdConnection()
{
///* Connection State, and TimeStamp of Update */
//int mState;
//time_t mLastEvent;
/* DUMMY INITIALISATION FOR ALL DATA - DON"T THINK IT MATTERS
* But keeps Valgrind happy
*/
/* Connection State, and TimeStamp of Update */
int mState = 0;
time_t mLastEvent = 0;
//
///* Addresses of Start/Proxy/End Nodes */
//bdId mSrcId;
@ -1686,24 +1690,24 @@ bdConnection::bdConnection()
///* Where we are in the connection,
//* and what connection mode.
//*/
//int mPoint;
//int mMode;
int mPoint = 0;
int mMode = 0;
//
///* must have ip:ports of connection ends (if proxied) */
//bdId mSrcConnAddr;
//bdId mDestConnAddr;
//
//int mBandwidth;
int mBandwidth = 0;
//
///* START/ACK Finishing ****/
//time_t mLastStart; /* timer for retries */
//int mRetryCount; /* retry counter */
time_t mLastStart = 0; /* timer for retries */
int mRetryCount = 0; /* retry counter */
//
//bool mSrcAck;
//bool mDestAck;
bool mSrcAck = false;
bool mDestAck = false;
//
//// Completion TS.
//time_t mCompletedTS;
time_t mCompletedTS = 0;
}
/* heavy check, used to check for alternative connections, coming from other direction

View File

@ -0,0 +1,146 @@
/*
* bitdht/bdfilter.cc
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
#include "bitdht/bdfilter.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
/**
* #define DEBUG_FILTER 1
**/
bdFilter::bdFilter(const bdNodeId *ownId, std::list<bdFilteredPeer> &startList,
uint32_t filterFlags, bdDhtFunctions *fns)
{
/* */
mOwnId = *ownId;
mFns = fns;
time_t now = time(NULL);
std::list<bdFilteredPeer>::iterator it;
for(it = startList.begin(); it != startList.end(); it++)
{
mFiltered.push_back(*it);
}
mFilterFlags = filterFlags;
}
bool bdFilter::filtered(std::list<bdFilteredPeer> &answer)
{
answer = mFiltered;
return (answer.size() > 0);
}
int bdFilter::checkPeer(const bdId *id, uint32_t mode)
{
bool add = false;
uint32_t flags = 0;
if ((mFilterFlags & BITDHT_FILTER_REASON_OWNID) &&
isOwnIdWithoutBitDhtFlags(id, mode))
{
add = true;
flags |= BITDHT_FILTER_REASON_OWNID;
}
if (add)
{
bool isNew = addPeerToFilter(id, flags);
if (isNew)
{
return 1;
}
}
return 0;
}
int bdFilter::addPeerToFilter(const bdId *id, uint32_t flags)
{
std::list<bdFilteredPeer>::iterator it;
bool found = false;
for(it = mFiltered.begin(); it != mFiltered.end(); it++)
{
if (id->addr.sin_addr.s_addr == it->mAddr.sin_addr.s_addr)
{
found = true;
it->mLastSeen = time(NULL);
it->mFilterFlags |= flags;
break;
}
}
if (!found)
{
time_t now = time(NULL);
bdFilteredPeer fp;
fp.mAddr = id->addr;
fp.mAddr.sin_port = 0;
fp.mFilterFlags = flags;
fp.mFilterTS = now;
fp.mLastSeen = now;
mFiltered.push_back(fp);
}
return found;
}
bool bdFilter::isOwnIdWithoutBitDhtFlags(const bdId *id, uint32_t peerFlags)
{
if (peerFlags & BITDHT_PEER_STATUS_RECV_PONG)
{
if (peerFlags & BITDHT_PEER_STATUS_DHT_ENGINE)
{
/* okay! */
return false;
}
/* now check distance */
bdMetric dist;
mFns->bdDistance(&mOwnId, &(id->id), &dist);
int bucket = mFns->bdBucketDistance(&dist);
/* if they match us... kill it */
if (bucket == 0)
{
return true;
}
}
return false;
}

View File

@ -0,0 +1,74 @@
#ifndef BITDHT_FILTER_H
#define BITDHT_FILTER_H
/*
* bitdht/bdfilter.h
*
* BitDHT: An Flexible DHT library.
*
* Copyright 2010 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 3 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "bitdht@lunamutt.com".
*
*/
/* This class is used to detect bad and filter them peers
*
*/
#include "bitdht/bdiface.h"
/* Query result flags are in bdiface.h */
#define BITDHT_FILTER_REASON_OWNID 0x0001
class bdFilteredPeer
{
public:
struct sockaddr_in mAddr;
uint32_t mFilterFlags; /* reasons why we are filtering */
time_t mFilterTS;
time_t mLastSeen;
};
class bdFilter
{
public:
bdFilter(const bdNodeId *ownid, std::list<bdFilteredPeer> &initialFilters,
uint32_t filterFlags, bdDhtFunctions *fns);
// get the answer.
bool filtered(std::list<bdFilteredPeer> &answer);
int checkPeer(const bdId *id, uint32_t peerFlags);
private:
int addPeerToFilter(const bdId *id, uint32_t flags);
bool isOwnIdWithoutBitDhtFlags(const bdId *id, uint32_t peerFlags);
// searching for
bdNodeId mOwnId;
uint32_t mFilterFlags;
std::list<bdFilteredPeer> mFiltered;
bdDhtFunctions *mFns;
};
#endif

View File

@ -105,6 +105,7 @@ HEADERS += \
udp/udpstack.h \
udp/udpbitdht.h \
bitdht/bdconnection.h \
bitdht/bdfilter.h \
SOURCES += \
bitdht/bencode.c \
@ -125,5 +126,6 @@ SOURCES += \
udp/udpstack.cc \
udp/udpbitdht.cc \
bitdht/bdconnection.cc \
bitdht/bdfilter.cc \