moved dsdv code to unused directory

This commit is contained in:
csoler 2017-04-26 14:31:15 +02:00
parent 508a2e2864
commit c31396a010
6 changed files with 7 additions and 7 deletions

View file

@ -0,0 +1,914 @@
/*
* libretroshare/src/services/p3dsdv.h
*
* Network-Wide Routing Service.
*
* Copyright 2011 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 2 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 "retroshare@lunamutt.com".
*
*/
#include <list>
#include <string>
#include <time.h>
//#include "serialiser/rsdsdvitems.h"
#include "services/p3dsdv.h"
#include "pqi/p3linkmgr.h"
#include "util/rsrandom.h"
#include <openssl/sha.h>
/****
* #define DEBUG_DSDV 1
****/
#define DEBUG_DSDV 1
/* DEFINE INTERFACE POINTER! */
RsDsdv *rsDsdv = NULL;
/*****
* Routing Services are provided for any service or peer that wants to access / receive info
* over the internal network.
*
* This is going to be based loosely on Ben's Algorithm...
* Each Service / Peer is identified by a HASH.
* This HASH is disguised by a temporary ANON-CHUNK.
* DSDVID = Sha1(ANONCHUNK + HASH).
*
* Each peer can advertise as many Services as they want.
* The Anon-chunk should be rotated regularly to hide it well.
* period to be defined.
*
*
* Once this Routing table has been established, Routes can be created - in a similar manner to turtle paths.
* (Send Path request (Path Id + Origin + Destination))... path is established.
*
* Then we can do Onion Routing etc, on top of this.
*
****/
p3Dsdv::p3Dsdv(p3ServiceControl *sc)
:p3Service(), /* p3Config(CONFIG_TYPE_DSDV), */ mDsdvMtx("p3Dsdv"), mServiceCtrl(sc)
{
addSerialType(new RsDsdvSerialiser());
mSentTablesTime = 0;
mSentIncrementTime = 0;
}
const std::string DSDV_APP_NAME = "dsdv";
const uint16_t DSDV_APP_MAJOR_VERSION = 1;
const uint16_t DSDV_APP_MINOR_VERSION = 0;
const uint16_t DSDV_MIN_MAJOR_VERSION = 1;
const uint16_t DSDV_MIN_MINOR_VERSION = 0;
RsServiceInfo p3Dsdv::getServiceInfo()
{
return RsServiceInfo(RS_SERVICE_TYPE_DSDV,
DSDV_APP_NAME,
DSDV_APP_MAJOR_VERSION,
DSDV_APP_MINOR_VERSION,
DSDV_MIN_MAJOR_VERSION,
DSDV_MIN_MINOR_VERSION);
}
int p3Dsdv::tick()
{
processIncoming();
sendTables();
return 0;
}
int p3Dsdv::status()
{
return 1;
}
#define DSDV_BROADCAST_PERIOD (60*10) // 10 Minutes.
#define DSDV_MIN_INCREMENT_PERIOD 5
#define DSDV_DISCARD_PERIOD (DSDV_BROADCAST_PERIOD * 2)
int p3Dsdv::sendTables()
{
time_t now = time(NULL);
time_t tt, it;
bool updateRequired = false;
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
tt = mSentTablesTime;
it = mSentIncrementTime;
updateRequired = mSignificantChanges;
}
if (now - tt > DSDV_BROADCAST_PERIOD)
{
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::sendTables() Broadcast Time";
std::cerr << std::endl;
#endif
selectStableRoutes();
clearOldRoutes();
generateRoutingTables(false);
printDsdvTable(std::cerr);
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
mSentTablesTime = now;
mSentIncrementTime = now;
return true ;
}
/* otherwise send incremental changes */
if ((updateRequired) && (now - it > DSDV_MIN_INCREMENT_PERIOD))
{
selectStableRoutes();
generateRoutingTables(true);
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
mSentIncrementTime = now;
}
return true;
}
#define RSDSDV_SEQ_INCREMENT 2
void p3Dsdv::advanceLocalSequenceNumbers()
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
time_t now = time(NULL);
std::map<std::string, RsDsdvTableEntry>::iterator it;
for(it = mTable.begin(); it != mTable.end(); ++it)
{
RsDsdvTableEntry &v = (it->second);
if (v.mOwnSource)
{
v.mStableRoute.mSequence += RSDSDV_SEQ_INCREMENT;
v.mStableRoute.mReceived = now;
}
}
}
void p3Dsdv::clearSignificantChangesFlags()
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
std::map<std::string, RsDsdvTableEntry>::iterator it;
for(it = mTable.begin(); it != mTable.end(); ++it)
{
RsDsdvTableEntry &v = (it->second);
if (v.mFlags & RSDSDV_FLAGS_SIGNIFICANT_CHANGE)
{
v.mFlags &= ~RSDSDV_FLAGS_SIGNIFICANT_CHANGE;
}
}
mSignificantChanges = false;
}
int p3Dsdv::generateRoutingTables(bool incremental)
{
/* we ping our peers */
/* who is online? */
std::set<RsPeerId> idList;
mServiceCtrl->getPeersConnected(getServiceInfo().mServiceType, idList);
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::generateRoutingTables(" << incremental << ")";
std::cerr << std::endl;
#endif
if (!incremental)
{
/* now clear significant flag */
advanceLocalSequenceNumbers();
}
/* prepare packets */
std::set<RsPeerId>::iterator it;
for(it = idList.begin(); it != idList.end(); ++it)
{
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::generateRoutingTables() For: " << *it;
std::cerr << std::endl;
#endif
generateRoutingTable(*it, incremental);
}
/* now clear significant flag */
clearSignificantChangesFlags();
return 1;
}
int p3Dsdv::generateRoutingTable(const RsPeerId &peerId, bool incremental)
{
RsDsdvRouteItem *dsdv = new RsDsdvRouteItem();
dsdv->PeerId(peerId);
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
std::map<std::string, RsDsdvTableEntry>::iterator it;
for(it = mTable.begin(); it != mTable.end(); ++it)
{
RsDsdvTableEntry &v = (it->second);
/* discard/ignore criterion */
if (!v.mIsStable)
{
continue;
}
if (v.mStableRoute.mDistance >= RSDSDV_MAX_DISTANCE)
{
continue;
}
if (incremental)
{
if (v.mFlags & RSDSDV_FLAGS_SIGNIFICANT_CHANGE)
{
// Done elsewhere.
//v.mFlags &= ~SIGNIFICANT_CHANGE;
}
else
{
/* ignore non-significant changes */
continue;
}
}
RsTlvDsdvEntry entry;
entry.endPoint.idType = v.mDest.mIdType;
entry.endPoint.anonChunk = v.mDest.mAnonChunk;
entry.endPoint.serviceId = v.mDest.mHash;
entry.sequence = v.mStableRoute.mSequence;
entry.distance = v.mStableRoute.mDistance;
//dsdv->routes.entries.push_back(entry);
dsdv->routes.mList.push_back(entry);
//if (dsdv->routes.entries.size() > RSDSDV_MAX_ROUTE_TABLE)
if (dsdv->routes.mList.size() > RSDSDV_MAX_ROUTE_TABLE)
{
sendItem(dsdv);
dsdv = new RsDsdvRouteItem();
dsdv->PeerId(peerId);
}
}
sendItem(dsdv);
return 1;
}
/****************************************************************************
****************************************************************************/
int p3Dsdv::processIncoming()
{
/* for each packet - pass to specific handler */
RsItem *item = NULL;
while(NULL != (item = recvItem()))
{
switch(item->PacketSubType())
{
default:
break;
case RS_PKT_SUBTYPE_DSDV_ROUTE:
{
handleDSDV((RsDsdvRouteItem *) item);
}
break;
}
/* clean up */
delete item;
}
return true ;
}
int p3Dsdv::handleDSDV(RsDsdvRouteItem *dsdv)
{
/* iterate over the entries */
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
time_t now = time(NULL);
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::handleDSDV() Received Pkt from: " << dsdv->PeerId();
std::cerr << std::endl;
//dsdv->print(std::cerr);
//std::cerr << std::endl;
#endif
std::list<RsTlvDsdvEntry>::iterator it;
//for(it = dsdv->routes.entries.begin(); it != dsdv->routes.entries.end(); ++it)
for(it = dsdv->routes.mList.begin(); it != dsdv->routes.mList.end(); ++it)
{
/* check for existing */
RsTlvDsdvEntry &entry = *it;
uint32_t realDistance = entry.distance + 1; // metric.
/* find the entry */
std::map<std::string, RsDsdvTableEntry>::iterator tit;
tit = mTable.find(entry.endPoint.serviceId);
if (tit == mTable.end())
{
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::handleDSDV() Adding Entry for New ServiceId: ";
std::cerr << entry.endPoint.serviceId;
std::cerr << std::endl;
#endif
/* new entry! */
RsDsdvTableEntry v;
v.mDest.mIdType = entry.endPoint.idType;
v.mDest.mAnonChunk = entry.endPoint.anonChunk;
v.mDest.mHash = entry.endPoint.serviceId;
/* add as a possible route */
RsDsdvRoute newRoute;
newRoute.mNextHop = dsdv->PeerId();
newRoute.mReceived = now;
newRoute.mSequence = entry.sequence;
newRoute.mDistance = realDistance;
newRoute.mValidSince = now;
v.mAllRoutes[dsdv->PeerId()] = newRoute;
v.mIsStable = false;
v.mFlags = RSDSDV_FLAGS_NEW_ROUTE;
v.mOwnSource = false;
v.mMatched = false;
// store in table.
mTable[v.mDest.mHash] = v;
}
else
{
RsDsdvTableEntry &v = tit->second;
if (v.mOwnSource)
{
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::handleDSDV() Ignoring OwnSource Entry:";
std::cerr << entry.endPoint.serviceId;
std::cerr << std::endl;
#endif
continue; // Ignore if we are source.
}
/* look for this in mAllRoutes */
std::map<RsPeerId, RsDsdvRoute>::iterator rit;
rit = v.mAllRoutes.find(dsdv->PeerId());
if (rit == v.mAllRoutes.end())
{
/* add a new entry in */
RsDsdvRoute newRoute;
newRoute.mNextHop = dsdv->PeerId();
newRoute.mReceived = now;
newRoute.mSequence = entry.sequence;
newRoute.mDistance = realDistance;
newRoute.mValidSince = now;
v.mAllRoutes[dsdv->PeerId()] = newRoute;
/* if we've just added it in - can't be stable one */
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::handleDSDV() Adding NewRoute Entry:";
std::cerr << entry.endPoint.serviceId;
std::cerr << std::endl;
#endif
}
else
{
if (rit->second.mSequence >= entry.sequence)
{
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::handleDSDV() Ignoring OLDSEQ Entry:";
std::cerr << entry.endPoint.serviceId;
std::cerr << std::endl;
#endif
/* ignore same/old sequence number??? */
continue;
}
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::handleDSDV() Updating Entry:";
std::cerr << entry.endPoint.serviceId;
std::cerr << std::endl;
#endif
/* update seq,dist,etc */
if (rit->second.mSequence + 2 < entry.sequence)
{
/* skipped a sequence number - reset timer */
rit->second.mValidSince = now;
}
//rit->second.mNextHop; // unchanged.
rit->second.mReceived = now;
rit->second.mSequence = entry.sequence;
rit->second.mDistance = realDistance;
/* if consistent route... maintain */
if ((v.mIsStable) &&
(rit->second.mNextHop == v.mStableRoute.mNextHop))
{
v.mStableRoute = rit->second;
}
else
{
/* otherwise we need to wait - see if we get new update */
}
}
}
}
return 1;
}
int p3Dsdv::selectStableRoutes()
{
/* iterate over the entries */
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
time_t now = time(NULL);
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::selectStableRoutes()";
std::cerr << std::endl;
#endif
/* find the entry */
std::map<std::string, RsDsdvTableEntry>::iterator tit;
for(tit = mTable.begin(); tit != mTable.end(); ++tit)
{
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::selectStableRoutes() For Entry: ";
std::cerr << tit->second;
std::cerr << std::endl;
#endif
if (tit->second.mOwnSource)
{
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::selectStableRoutes() OwnSource... Ignoring";
std::cerr << std::endl;
#endif
continue; // Ignore if we are source.
}
std::map<RsPeerId, RsDsdvRoute>::iterator rit;
uint32_t newest = 0;
RsPeerId newestId;
uint32_t closest = RSDSDV_MAX_DISTANCE + 1;
RsPeerId closestId;
time_t closestAge = 0;
/* find newest sequence number */
for(rit = tit->second.mAllRoutes.begin();
rit != tit->second.mAllRoutes.end(); ++rit)
{
if ((now - rit->second.mReceived <= DSDV_DISCARD_PERIOD) &&
(rit->second.mSequence >= newest))
{
newest = rit->second.mSequence;
newestId = rit->first;
/* also becomes default for closest (later) */
closest = rit->second.mDistance;
closestId = rit->first;
closestAge = now - rit->second.mValidSince;
}
}
if (closest >= RSDSDV_MAX_DISTANCE + 1)
{
#ifdef DEBUG_DSDV
std::cerr << "\tNo Suitable Route";
std::cerr << std::endl;
#endif
tit->second.mIsStable = false;
continue;
}
uint32_t currseq = newest - (newest % 2); // remove 'kill'=ODD Seq.
#ifdef DEBUG_DSDV
std::cerr << "\t Newest Seq: " << newest << " from: " << newestId;
std::cerr << std::endl;
#endif
/* find closest distance - with valid seq & max valid time */
for(rit = tit->second.mAllRoutes.begin();
rit != tit->second.mAllRoutes.end(); ++rit)
{
/* Maximum difference in Sequence number is 2*DISTANCE
* Otherwise it must be old.
*/
if (rit->second.mSequence + rit->second.mDistance * 2 < currseq)
{
#ifdef DEBUG_DSDV
std::cerr << "\t\tIgnoring OLD SEQ Entry: " << rit->first;
std::cerr << std::endl;
#endif
continue; // ignore.
}
/* if we haven't received an update in ages - old */
if (now - rit->second.mReceived > DSDV_DISCARD_PERIOD)
{
#ifdef DEBUG_DSDV
std::cerr << "\t\tIgnoring OLD TIME Entry: " << rit->first;
std::cerr << std::endl;
#endif
continue; // ignore.
}
if (rit->second.mDistance < closest)
{
closest = rit->second.mDistance;
closestId = rit->first;
closestAge = now - rit->second.mValidSince;
#ifdef DEBUG_DSDV
std::cerr << "\t\tUpdating to Closer Entry: " << rit->first;
std::cerr << std::endl;
#endif
}
else if ((rit->second.mDistance == closest) &&
(closestAge < now - rit->second.mValidSince))
{
/* have a more stable (older) one */
closest = rit->second.mDistance;
closestId = rit->first;
closestAge = now - rit->second.mValidSince;
#ifdef DEBUG_DSDV
std::cerr << "\t\tUpdating to Stabler Entry: " << rit->first;
std::cerr << std::endl;
#endif
}
else
{
#ifdef DEBUG_DSDV
std::cerr << "\t\tIgnoring Distant Entry: " << rit->first;
std::cerr << std::endl;
#endif
}
}
tit->second.mIsStable = true;
rit = tit->second.mAllRoutes.find(closestId);
tit->second.mStableRoute = rit->second;
tit->second.mFlags &= ~RSDSDV_FLAGS_NEW_ROUTE;
#ifdef DEBUG_DSDV
std::cerr << "\tStable Route: " << tit->second.mStableRoute;
std::cerr << std::endl;
#endif
}
return 1;
}
int p3Dsdv::clearOldRoutes()
{
/* iterate over the entries */
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::clearOldRoutes()";
std::cerr << std::endl;
#endif
/* find the entry */
std::map<std::string, RsDsdvTableEntry>::iterator it, it2;
for(it = mTable.begin(); it != mTable.end(); ++it)
{
if (it->second.mOwnSource)
{
continue;
}
if (it->second.mIsStable)
{
continue;
}
if (it->second.mFlags & RSDSDV_FLAGS_NEW_ROUTE)
{
continue;
}
/* backstep iterator for loop, and delete original */
it2 = it;
it--;
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::clearOldRoutes() Deleting OLD ServiceId: " << it2->first;
std::cerr << std::endl;
#endif
mTable.erase(it2);
}
return 1;
}
/*************** pqiMonitor callback ***********************/
void p3Dsdv::statusChange(const std::list<pqiServicePeer> &plist)
{
std::list<pqiServicePeer>::const_iterator it;
for(it = plist.begin(); it != plist.end(); ++it)
{
/* only care about disconnected / not friends cases */
if ( 1 )
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
}
}
}
int p3Dsdv::addTestService()
{
RsDsdvId testId;
int rndhash1[SHA_DIGEST_LENGTH / 4];
int rndhash2[SHA_DIGEST_LENGTH / 4];
std::string realHash;
std::string seedHash;
for(int i = 0; i < SHA_DIGEST_LENGTH / 4; i++)
{
rndhash1[i] = RSRandom::random_u32();
rndhash2[i] = RSRandom::random_u32();
}
for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
{
rs_sprintf_append(realHash, "%02x", (uint32_t) ((uint8_t *) rndhash1)[i]);
rs_sprintf_append(seedHash, "%02x", (uint32_t) ((uint8_t *) rndhash2)[i]);
}
uint8_t sha_hash[SHA_DIGEST_LENGTH];
memset(sha_hash,0,SHA_DIGEST_LENGTH*sizeof(uint8_t)) ;
SHA_CTX *sha_ctx = new SHA_CTX;
SHA1_Init(sha_ctx);
SHA1_Update(sha_ctx, realHash.c_str(), realHash.length());
SHA1_Update(sha_ctx, seedHash.c_str(), seedHash.length());
SHA1_Final(sha_hash, sha_ctx);
delete sha_ctx;
for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
{
rs_sprintf_append(testId.mHash, "%02x", (uint32_t) (sha_hash)[i]);
}
testId.mIdType = RSDSDV_IDTYPE_TEST;
testId.mAnonChunk = seedHash;
addDsdvId(&testId, realHash);
return 1;
}
int p3Dsdv::addDsdvId(RsDsdvId *id, std::string realHash)
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::addDsdvId() ID: " << *id << " RealHash: " << realHash;
std::cerr << std::endl;
#endif
time_t now = time(NULL);
/* check for duplicate */
std::map<std::string, RsDsdvTableEntry>::iterator it;
it = mTable.find(id->mHash);
if (it != mTable.end())
{
/* error */
std::cerr << "p3Dsdv::addDsdvId() ERROR Duplicate ID";
std::cerr << std::endl;
return 0;
}
/* new entry! */
RsDsdvTableEntry v;
v.mDest = *id;
v.mStableRoute.mNextHop = mServiceCtrl->getOwnId();
v.mStableRoute.mReceived = now;
v.mStableRoute.mValidSince = now;
v.mStableRoute.mSequence = 0;
v.mStableRoute.mDistance = 0;
v.mIsStable = true;
v.mFlags = RSDSDV_FLAGS_OWN_SERVICE;
v.mOwnSource = true;
v.mMatched = true;
v.mMatchedHash = realHash;
// store in table.
mTable[v.mDest.mHash] = v;
return 1;
}
int p3Dsdv::dropDsdvId(RsDsdvId *id)
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::dropDsdvId() ID: " << *id;
std::cerr << std::endl;
#endif
/* This should send out an infinity packet... and flag for deletion */
std::map<std::string, RsDsdvTableEntry>::iterator it;
it = mTable.find(id->mHash);
if (it == mTable.end())
{
/* error */
std::cerr << "p3Dsdv::addDsdvId() ERROR Unknown ID";
std::cerr << std::endl;
return 0;
}
mTable.erase(it);
return 1;
}
int p3Dsdv::printDsdvTable(std::ostream &out)
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
/* iterate over the entries */
std::map<std::string, RsDsdvTableEntry>::iterator it;
for(it = mTable.begin(); it != mTable.end(); ++it)
{
RsDsdvTableEntry &v = it->second;
out << v;
out << std::endl;
}
return 1;
}
/*****************************************/
uint32_t p3Dsdv::getLocalServices(std::list<std::string> &hashes)
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
/* iterate over the entries */
std::map<std::string, RsDsdvTableEntry>::iterator it;
for(it = mTable.begin(); it != mTable.end(); ++it)
{
if (it->second.mOwnSource)
{
hashes.push_back(it->first);
}
}
return 1;
}
uint32_t p3Dsdv::getAllServices(std::list<std::string> &hashes)
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
/* iterate over the entries */
std::map<std::string, RsDsdvTableEntry>::iterator it;
for(it = mTable.begin(); it != mTable.end(); ++it)
{
hashes.push_back(it->first);
}
return 1;
}
int p3Dsdv::getDsdvEntry(const std::string &hash, RsDsdvTableEntry &entry)
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
/* iterate over the entries */
std::map<std::string, RsDsdvTableEntry>::iterator it;
it = mTable.find(hash);
if (it == mTable.end())
{
return 0;
}
entry = it->second;
return 1;
}
std::ostream &operator<<(std::ostream &out, const RsDsdvId &id)
{
out << "[Type: " << id.mIdType << " AMZ: " << id.mAnonChunk << " THASH: " << id.mHash;
out << "]";
return out;
}
std::ostream &operator<<(std::ostream &out, const RsDsdvRoute &route)
{
time_t now = time(NULL);
out << "< Seq: " << route.mSequence << " Dist: " << route.mDistance;
out << " NextHop: " << route.mNextHop;
out << " recvd: " << now-route.mReceived;
out << " validSince: " << now-route.mValidSince;
out << " >";
return out;
}
std::ostream &operator<<(std::ostream &out, const RsDsdvTableEntry &entry)
{
out << "DSDV Route for: " << entry.mDest << std::endl;
if (entry.mIsStable)
{
out << "\tStable: " << entry.mStableRoute << std::endl;
}
else
{
out << "\tNo Stable Route" << std::endl;
}
out << "\tOwnSource: " << entry.mOwnSource;
out << " Flags: " << entry.mFlags;
if (entry.mMatched)
{
out << " Matched: " << entry.mMatchedHash;
}
else
{
out << " Non Matched";
}
out << std::endl;
if (entry.mAllRoutes.size() > 0)
{
out << "\tAll Routes:" << std::endl;
}
std::map<RsPeerId, RsDsdvRoute>::const_iterator it;
for(it = entry.mAllRoutes.begin(); it != entry.mAllRoutes.end(); ++it)
{
out << "\t\t" << it->second << std::endl;
}
return out;
}

View file

@ -0,0 +1,123 @@
/*
* libretroshare/src/services/p3dsdv.h
*
* Network-Wide Routing Service.
*
* Copyright 2011 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 2 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 "retroshare@lunamutt.com".
*
*/
#ifndef SERVICE_RSDSDV_HEADER
#define SERVICE_RSDSDV_HEADER
#include <list>
#include <string>
#include "serialiser/rsdsdvitems.h"
#include "services/p3service.h"
#include "pqi/p3cfgmgr.h"
#include "pqi/pqimonitor.h"
#include "retroshare/rsdsdv.h"
class p3ServiceControl;
#define RSDSDV_MAX_DISTANCE 3
#define RSDSDV_MAX_SEND_TABLE 100
//!The RS DSDV service.
/**
*
* Finds RS wide paths to Services and Peers.
*/
class p3Dsdv: public RsDsdv, public p3Service /* , public p3Config */, public pqiServiceMonitor
{
public:
p3Dsdv(p3ServiceControl *cm);
virtual RsServiceInfo getServiceInfo();
/*** internal librs interface ****/
int addDsdvId(RsDsdvId *id, std::string realHash);
int dropDsdvId(RsDsdvId *id);
int printDsdvTable(std::ostream &out);
int addTestService();
private:
int sendTables();
void advanceLocalSequenceNumbers();
void clearSignificantChangesFlags();
int generateRoutingTables(bool incremental);
int generateRoutingTable(const RsPeerId &peerId, bool incremental);
int processIncoming();
int handleDSDV(RsDsdvRouteItem *dsdv);
int selectStableRoutes();
int clearOldRoutes();
public:
/***** overloaded from rsDsdv *****/
virtual uint32_t getLocalServices(std::list<std::string> &hashes);
virtual uint32_t getAllServices(std::list<std::string> &hashes);
virtual int getDsdvEntry(const std::string &hash, RsDsdvTableEntry &entry);
/***** overloaded from p3Service *****/
/*!
* Process stuff.
*/
virtual int tick();
virtual int status();
/*************** pqiMonitor callback ***********************/
virtual void statusChange(const std::list<pqiServicePeer> &plist);
/************* from p3Config *******************/
//virtual RsSerialiser *setupSerialiser() ;
//virtual bool saveList(bool& cleanup, std::list<RsItem*>&) ;
//virtual void saveDone();
//virtual bool loadList(std::list<RsItem*>& load) ;
private:
RsMutex mDsdvMtx;
std::map<std::string, RsDsdvTableEntry> mTable;
time_t mSentTablesTime;
time_t mSentIncrementTime;
bool mSignificantChanges;
p3ServiceControl *mServiceCtrl;
};
#endif // SERVICE_RSDSDV_HEADER

View file

@ -0,0 +1,107 @@
#ifndef RETROSHARE_DSDV_INTERFACE_H
#define RETROSHARE_DSDV_INTERFACE_H
/*
* libretroshare/src/rsiface: rsdsdv.h
*
* RetroShare C++ Interface.
*
* Copyright 2011-2011 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 2 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 "retroshare@lunamutt.com".
*
*/
#include <inttypes.h>
#include <string>
#include <list>
/* The Main Interface Class - for information about your Peers */
class RsDsdv;
extern RsDsdv *rsDsdv;
#define RSDSDV_IDTYPE_PEER 0x0001
#define RSDSDV_IDTYPE_SERVICE 0x0002
#define RSDSDV_IDTYPE_TEST 0x0100
#define RSDSDV_FLAGS_SIGNIFICANT_CHANGE 0x0001
#define RSDSDV_FLAGS_STABLE_ROUTE 0x0002
#define RSDSDV_FLAGS_NEW_ROUTE 0x0004
#define RSDSDV_FLAGS_OWN_SERVICE 0x0008
class RsDsdvId
{
public:
uint32_t mIdType;
std::string mAnonChunk;
std::string mHash;
};
class RsDsdvRoute
{
public:
RsPeerId mNextHop;
uint32_t mSequence;
uint32_t mDistance;
time_t mReceived;
time_t mValidSince;
};
class RsDsdvTableEntry
{
public:
RsDsdvId mDest;
bool mIsStable;
RsDsdvRoute mStableRoute;
//RsDsdvRoute mFreshestRoute;
std::map<RsPeerId, RsDsdvRoute> mAllRoutes;
uint32_t mFlags;
/* if we've matched it to someone */
std::string mMatchedHash;
bool mMatched;
bool mOwnSource;
};
std::ostream &operator<<(std::ostream &out, const RsDsdvId &id);
std::ostream &operator<<(std::ostream &out, const RsDsdvRoute &route);
std::ostream &operator<<(std::ostream &out, const RsDsdvTableEntry &entry);
class RsDsdv
{
public:
RsDsdv() { return; }
virtual ~RsDsdv() { return; }
virtual uint32_t getLocalServices(std::list<std::string> &hashes) = 0;
virtual uint32_t getAllServices(std::list<std::string> &hashes) = 0;
virtual int getDsdvEntry(const std::string &hash, RsDsdvTableEntry &entry) = 0;
};
#endif

View file

@ -0,0 +1,352 @@
/*
* libretroshare/src/serialiser: rsgameitems.cc
*
* RetroShare Serialiser.
*
* Copyright 2007-2008 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 2 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 "retroshare@lunamutt.com".
*
*/
#include "serialiser/rsbaseserial.h"
#include "serialiser/rsdsdvitems.h"
#include "serialiser/rstlvdsdv.h"
/***
#define RSSERIAL_DEBUG 1
***/
#include <iostream>
/*************************************************************************/
RsDsdvRouteItem::~RsDsdvRouteItem()
{
return;
}
void RsDsdvRouteItem::clear()
{
routes.TlvClear();
}
std::ostream &RsDsdvRouteItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsDsdvRouteItem", indent);
uint16_t int_Indent = indent + 2;
routes.print(out, int_Indent);
printRsItemEnd(out, "RsDsdvRouteItem", indent);
return out;
}
uint32_t RsDsdvSerialiser::sizeRoute(RsDsdvRouteItem *item)
{
uint32_t s = 8; /* header */
s += item->routes.TlvSize();
return s;
}
/* serialise the data to the buffer */
bool RsDsdvSerialiser::serialiseRoute(RsDsdvRouteItem *item, void *data, uint32_t *pktsize)
{
uint32_t tlvsize = sizeRoute(item);
uint32_t offset = 0;
if (*pktsize < tlvsize)
return false; /* not enough space */
*pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDsdvSerialiser::serialiseRoute() Header: " << ok << std::endl;
std::cerr << "RsDsdvSerialiser::serialiseRoute() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= item->routes.SetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
ok = false;
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDsdvSerialiser::serialiseRoute() Size Error! " << std::endl;
#endif
}
return ok;
}
RsDsdvRouteItem *RsDsdvSerialiser::deserialiseRoute(void *data, uint32_t *pktsize)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t tlvsize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_DSDV != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_DSDV_ROUTE != getRsItemSubType(rstype)))
{
return NULL; /* wrong type */
}
if (*pktsize < tlvsize) /* check size */
return NULL; /* not enough data */
/* set the packet length */
*pktsize = tlvsize;
bool ok = true;
/* ready to load */
RsDsdvRouteItem *item = new RsDsdvRouteItem();
item->clear();
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= item->routes.GetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
/* error */
delete item;
return NULL;
}
if (!ok)
{
delete item;
return NULL;
}
return item;
}
/*************************************************************************/
RsDsdvDataItem::~RsDsdvDataItem()
{
return;
}
void RsDsdvDataItem::clear()
{
src.TlvClear();
dest.TlvClear();
ttl = 0;
data.TlvClear();
}
std::ostream &RsDsdvDataItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsDsdvDataItem", indent);
uint16_t int_Indent = indent + 2;
src.print(out, int_Indent);
dest.print(out, int_Indent);
printIndent(out, int_Indent);
out << "TTL: " << ttl << std::endl;
data.print(out, int_Indent);
printRsItemEnd(out, "RsDsdvDataItem", indent);
return out;
}
uint32_t RsDsdvSerialiser::sizeData(RsDsdvDataItem *item)
{
uint32_t s = 8; /* header */
s += item->src.TlvSize();
s += item->dest.TlvSize();
s += 4;
s += item->data.TlvSize();
return s;
}
/* serialise the data to the buffer */
bool RsDsdvSerialiser::serialiseData(RsDsdvDataItem *item, void *data, uint32_t *pktsize)
{
uint32_t tlvsize = sizeData(item);
uint32_t offset = 0;
if (*pktsize < tlvsize)
return false; /* not enough space */
*pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, item->PacketId(), tlvsize);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDsdvSerialiser::serialiseData() Header: " << ok << std::endl;
std::cerr << "RsDsdvSerialiser::serialiseData() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= item->src.SetTlv(data, tlvsize, &offset);
ok &= item->dest.SetTlv(data, tlvsize, &offset);
ok &= setRawUInt32(data, tlvsize, &offset, item->ttl);
ok &= item->data.SetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
ok = false;
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDsdvSerialiser::serialiseData() Size Error! " << std::endl;
#endif
}
return ok;
}
RsDsdvDataItem *RsDsdvSerialiser::deserialiseData(void *data, uint32_t *pktsize)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t tlvsize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_DSDV != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_DSDV_DATA != getRsItemSubType(rstype)))
{
return NULL; /* wrong type */
}
if (*pktsize < tlvsize) /* check size */
return NULL; /* not enough data */
/* set the packet length */
*pktsize = tlvsize;
bool ok = true;
/* ready to load */
RsDsdvDataItem *item = new RsDsdvDataItem();
item->clear();
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= item->src.GetTlv(data, tlvsize, &offset);
ok &= item->dest.GetTlv(data, tlvsize, &offset);
ok &= getRawUInt32(data, tlvsize, &offset, &(item->ttl));
ok &= item->data.GetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
/* error */
delete item;
return NULL;
}
if (!ok)
{
delete item;
return NULL;
}
return item;
}
/*************************************************************************/
uint32_t RsDsdvSerialiser::size(RsItem *i)
{
RsDsdvRouteItem *dri;
RsDsdvDataItem *ddi;
if (NULL != (dri = dynamic_cast<RsDsdvRouteItem *>(i)))
{
return sizeRoute(dri);
}
if (NULL != (ddi = dynamic_cast<RsDsdvDataItem *>(i)))
{
return sizeData(ddi);
}
return 0;
}
bool RsDsdvSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize)
{
RsDsdvRouteItem *dri;
RsDsdvDataItem *ddi;
if (NULL != (dri = dynamic_cast<RsDsdvRouteItem *>(i)))
{
return serialiseRoute(dri, data, pktsize);
}
if (NULL != (ddi = dynamic_cast<RsDsdvDataItem *>(i)))
{
return serialiseData(ddi, data, pktsize);
}
return false;
}
RsItem *RsDsdvSerialiser::deserialise(void *data, uint32_t *pktsize)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(RS_SERVICE_TYPE_DSDV != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_DSDV_ROUTE:
return deserialiseRoute(data, pktsize);
break;
case RS_PKT_SUBTYPE_DSDV_DATA:
return deserialiseData(data, pktsize);
break;
default:
return NULL;
break;
}
}
/*************************************************************************/

View file

@ -0,0 +1,109 @@
#ifndef RS_DSDV_ITEMS_H
#define RS_DSDV_ITEMS_H
/*
* libretroshare/src/serialiser: rsdsdvitems.h
*
* RetroShare Serialiser.
*
* Copyright 2011-2011 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 2 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 "retroshare@lunamutt.com".
*
*/
#include <map>
#include "rsitems/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvbase.h"
#include "serialiser/rstlvbinary.h"
#include "serialiser/rstlvdsdv.h"
/**************************************************************************/
#define RS_PKT_SUBTYPE_DSDV_ROUTE 0x01
#define RS_PKT_SUBTYPE_DSDV_DATA 0x02
class RsDsdvRouteItem: public RsItem
{
public:
RsDsdvRouteItem()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_DSDV,
RS_PKT_SUBTYPE_DSDV_ROUTE)
{
setPriorityLevel(QOS_PRIORITY_RS_DSDV_ROUTE);
return;
}
virtual ~RsDsdvRouteItem();
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsTlvDsdvEntrySet routes;
};
class RsDsdvDataItem: public RsItem
{
public:
RsDsdvDataItem()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_DSDV,
RS_PKT_SUBTYPE_DSDV_DATA), data(TLV_TYPE_BIN_GENERIC)
{
setPriorityLevel(QOS_PRIORITY_RS_DSDV_DATA);
return;
}
virtual ~RsDsdvDataItem();
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsTlvDsdvEndPoint src;
RsTlvDsdvEndPoint dest;
uint32_t ttl;
RsTlvBinaryData data;
};
class RsDsdvSerialiser: public RsSerialType
{
public:
RsDsdvSerialiser()
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_DSDV)
{ return; }
virtual ~RsDsdvSerialiser()
{ return; }
virtual uint32_t size(RsItem *);
virtual bool serialise (RsItem *item, void *data, uint32_t *size);
virtual RsItem * deserialise(void *data, uint32_t *size);
private:
virtual uint32_t sizeRoute(RsDsdvRouteItem *);
virtual bool serialiseRoute(RsDsdvRouteItem *item, void *data, uint32_t *size);
virtual RsDsdvRouteItem *deserialiseRoute(void *data, uint32_t *size);
virtual uint32_t sizeData(RsDsdvDataItem *);
virtual bool serialiseData(RsDsdvDataItem *item, void *data, uint32_t *size);
virtual RsDsdvDataItem *deserialiseData(void *data, uint32_t *size);
};
/**************************************************************************/
#endif /* RS_DSDV_ITEMS_H */