First pass at adding a DSDV system to libretroshare

- Added Routing Data Types.
	- Completed Basic Logic
	- Added Interface for GUI display.

Lots to do:
	- Handle edge cases (drop / add )
	- Test on the network.
	- Partner service, which actually uses system.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-dhtmods@4678 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2011-11-21 07:49:38 +00:00
parent e679b57940
commit 7f30b19f2a
10 changed files with 1415 additions and 2 deletions

View File

@ -152,6 +152,7 @@ PUBLIC_HEADERS = retroshare/rsblogs.h \
retroshare/rsturtle.h \
retroshare/rstypes.h \
retroshare/rsdht.h \
retroshare/rsdsdv.h \
retroshare/rsconfig.h
HEADERS += plugins/pluginmanager.h \
@ -422,6 +423,8 @@ HEADERS += serialiser/rsbaseitems.h \
serialiser/rstlvkvwide.h \
serialiser/rstlvtypes.h \
serialiser/rstlvutil.h \
serialiser/rstlvdsdv.h \
serialiser/rsdsdvitems.h \
serialiser/rstunnelitems.h
HEADERS += services/p3channels.h \
@ -434,6 +437,7 @@ HEADERS += services/p3channels.h \
services/p3photoservice.h \
services/p3service.h \
services/p3statusservice.h \
services/p3dsdv.h \
services/p3tunnel.h
HEADERS += distrib/p3distrib.h \
@ -555,6 +559,8 @@ SOURCES += serialiser/rsbaseitems.cc \
serialiser/rstlvkvwide.cc \
serialiser/rstlvtypes.cc \
serialiser/rstlvutil.cc \
serialiser/rstlvdsdv.cc \
serialiser/rsdsdvitems.cc \
serialiser/rstunnelitems.cc
SOURCES += services/p3channels.cc \
@ -565,7 +571,9 @@ SOURCES += services/p3channels.cc \
services/p3msgservice.cc \
services/p3photoservice.cc \
services/p3service.cc \
services/p3statusservice.cc
services/p3statusservice.cc \
services/p3dsdv.cc
# removed because getPeer() doesn t exist services/p3tunnel.cc
SOURCES += distrib/p3distrib.cc \

View File

@ -0,0 +1,102 @@
#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_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:
std::string mNextHop;
uint32_t mSequence;
uint32_t mDistance;
time_t mReceived;
};
class RsDsdvTableEntry
{
public:
RsDsdvId mDest;
RsDsdvRoute mStableRoute;
RsDsdvRoute mBestRoute;
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,173 @@
/*
* 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::sizeItem(RsDsdvRouteItem *item)
{
uint32_t s = 8; /* header */
s += item->routes.TlvSize();
return s;
}
/* serialise the data to the buffer */
bool RsDsdvSerialiser::serialiseItem(RsDsdvRouteItem *item, void *data, uint32_t *pktsize)
{
uint32_t tlvsize = sizeItem(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::serialiseItem() Header: " << ok << std::endl;
std::cerr << "RsDsdvSerialiser::serialiseItem() 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::serialiseItem() Size Error! " << std::endl;
#endif
}
return ok;
}
RsDsdvRouteItem *RsDsdvSerialiser::deserialiseItem(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;
}
uint32_t RsDsdvSerialiser::size(RsItem *item)
{
return sizeItem((RsDsdvRouteItem *) item);
}
bool RsDsdvSerialiser::serialise(RsItem *item, void *data, uint32_t *pktsize)
{
return serialiseItem((RsDsdvRouteItem *) item, data, pktsize);
}
RsItem *RsDsdvSerialiser::deserialise(void *data, uint32_t *pktsize)
{
return deserialiseItem(data, pktsize);
}
/*************************************************************************/

View File

@ -0,0 +1,79 @@
#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 "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvdsdv.h"
/**************************************************************************/
#define RS_PKT_SUBTYPE_DSDV_ROUTE 0x01
class RsDsdvRouteItem: public RsItem
{
public:
RsDsdvRouteItem()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_DSDV,
RS_PKT_SUBTYPE_DSDV_ROUTE)
{ return; }
virtual ~RsDsdvRouteItem();
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsTlvDsdvEntrySet routes;
};
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 sizeItem(RsDsdvRouteItem *);
virtual bool serialiseItem (RsDsdvRouteItem *item, void *data, uint32_t *size);
virtual RsDsdvRouteItem *deserialiseItem(void *data, uint32_t *size);
};
/**************************************************************************/
#endif /* RS_DSDV_ITEMS_H */

View File

@ -92,6 +92,8 @@ const uint16_t RS_SERVICE_TYPE_PROXY = 0xf030;
/* Photo - Cache Only */
const uint16_t RS_SERVICE_TYPE_PHOTO = 0xf040;
/* DSDV Testing at the moment - Service Only */
const uint16_t RS_SERVICE_TYPE_DSDV = 0xf050;
/* Games/External Apps - Service Only */
const uint16_t RS_SERVICE_TYPE_GAME_LAUNCHER = 0xf200;

View File

@ -210,7 +210,8 @@ const uint16_t TLV_TYPE_IMAGE = 0x1060;
const uint16_t TLV_TYPE_ADDRESS_INFO = 0x1070;
const uint16_t TLV_TYPE_ADDRESS_SET = 0x1071;
const uint16_t TLV_TYPE_DSDV_ENTRY = 0x1080;
const uint16_t TLV_TYPE_DSDV_ENTRY_SET= 0x1081;

View File

@ -0,0 +1,319 @@
/*
* libretroshare/src/serialiser: rstlvtypes.cc
*
* RetroShare Serialiser.
*
* Copyright 2007-2008 by Robert Fernie, Chris Parker
*
* 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 "rstlvdsdv.h"
#include "rstlvbase.h"
#include "rstlvtypes.h"
#include "rsbaseserial.h"
#include "util/rsprint.h"
#include <ostream>
#include <sstream>
#include <iomanip>
#include <iostream>
/************************************* RsTlvDsdvEntry ************************************/
RsTlvDsdvEntry::RsTlvDsdvEntry()
:RsTlvItem(), idType(0), sequence(0), distance(0)
{
return;
}
void RsTlvDsdvEntry::TlvClear()
{
idType = 0;
anonChunk.clear();
serviceId.clear();
sequence = 0;
distance = 0;
}
uint32_t RsTlvDsdvEntry::TlvSize()
{
uint32_t s = TLV_HEADER_SIZE; /* header + 4 + str + str + 4 + 4 */
s += 4; // idType;
s += GetTlvStringSize(anonChunk);
s += GetTlvStringSize(serviceId);
s += 4; // sequence;
s += 4; // distance;
return s;
}
bool RsTlvDsdvEntry::SetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */
{
/* must check sizes */
uint32_t tlvsize = TlvSize();
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend)
return false; /* not enough space */
bool ok = true;
/* start at data[offset] */
/* add mandatory parts first */
ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_ADDRESS_INFO, tlvsize);
ok &= setRawUInt32(data, tlvend, offset, idType);
ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_GENID, anonChunk);
ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_HASH_SHA1, serviceId);
ok &= setRawUInt32(data, tlvend, offset, sequence);
ok &= setRawUInt32(data, tlvend, offset, distance);
return ok;
}
bool RsTlvDsdvEntry::GetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */
{
if (size < *offset + TLV_HEADER_SIZE)
return false;
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend) /* check size */
return false; /* not enough space */
if (tlvtype != TLV_TYPE_DSDV_ENTRY) /* check type */
return false;
bool ok = true;
/* ready to load */
TlvClear();
/* skip the header */
(*offset) += TLV_HEADER_SIZE;
ok &= getRawUInt32(data, tlvend, offset, &(idType));
ok &= GetTlvString(data, tlvend, offset, TLV_TYPE_STR_GENID, anonChunk);
ok &= GetTlvString(data, tlvend, offset, TLV_TYPE_STR_HASH_SHA1, serviceId);
ok &= getRawUInt32(data, tlvend, offset, &(sequence));
ok &= getRawUInt32(data, tlvend, offset, &(distance));
/***************************************************************************
* NB: extra components could be added (for future expansion of the type).
* or be present (if this code is reading an extended version).
*
* We must chew up the extra characters to conform with TLV specifications
***************************************************************************/
if (*offset != tlvend)
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvDsdvEntry::GetTlv() Warning extra bytes at end of item";
std::cerr << std::endl;
#endif
*offset = tlvend;
}
return ok;
}
std::ostream &RsTlvDsdvEntry::print(std::ostream &out, uint16_t indent)
{
printBase(out, "RsTlvDsdvEntry", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "idType:" << idType;
out << std::endl;
printIndent(out, int_Indent);
out << "AnonChunk:" << anonChunk;
out << std::endl;
printIndent(out, int_Indent);
out << "ServiceId:" << serviceId;
out << std::endl;
printIndent(out, int_Indent);
out << "Sequence:" << sequence;
out << std::endl;
printIndent(out, int_Indent);
out << "Distance:" << distance;
out << std::endl;
printEnd(out, "RsTlvDsdvEntry", indent);
return out;
}
/************************************* RsTlvDsdvEntrySet ************************************/
void RsTlvDsdvEntrySet::TlvClear()
{
entries.clear();
}
uint32_t RsTlvDsdvEntrySet::TlvSize()
{
uint32_t s = TLV_HEADER_SIZE; /* header */
std::list<RsTlvDsdvEntry>::iterator it;
if(!entries.empty())
{
for(it = entries.begin(); it != entries.end() ; ++it)
s += it->TlvSize();
}
return s;
}
bool RsTlvDsdvEntrySet::SetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */
{
/* must check sizes */
uint32_t tlvsize = TlvSize();
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend)
return false; /* not enough space */
bool ok = true;
/* start at data[offset] */
ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_DSDV_ENTRY_SET , tlvsize);
if(!entries.empty())
{
std::list<RsTlvDsdvEntry>::iterator it;
for(it = entries.begin(); it != entries.end() ; ++it)
ok &= it->SetTlv(data, size, offset);
}
return ok;
}
bool RsTlvDsdvEntrySet::GetTlv(void *data, uint32_t size, uint32_t *offset) /* serialise */
{
if (size < *offset + TLV_HEADER_SIZE)
return false;
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend) /* check size */
return false; /* not enough space */
if (tlvtype != TLV_TYPE_DSDV_ENTRY_SET) /* check type */
return false;
bool ok = true;
/* ready to load */
TlvClear();
/* skip the header */
(*offset) += TLV_HEADER_SIZE;
/* while there is TLV */
while((*offset) + 2 < tlvend)
{
/* get the next type */
uint16_t tlvsubtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
switch(tlvsubtype)
{
case TLV_TYPE_DSDV_ENTRY:
{
RsTlvDsdvEntry entry;
ok &= entry.GetTlv(data, size, offset);
if (ok)
{
entries.push_back(entry);
}
}
break;
default:
ok &= SkipUnknownTlv(data, tlvend, offset);
break;
}
if (!ok)
break;
}
/***************************************************************************
* NB: extra components could be added (for future expansion of the type).
* or be present (if this code is reading an extended version).
*
* We must chew up the extra characters to conform with TLV specifications
***************************************************************************/
if (*offset != tlvend)
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvDsdvEntrySet::GetTlv() Warning extra bytes at end of item";
std::cerr << std::endl;
#endif
*offset = tlvend;
}
return ok;
}
// prints out contents of RsTlvDsdvEntrySet
std::ostream &RsTlvDsdvEntrySet::print(std::ostream &out, uint16_t indent)
{
printBase(out, "RsTlvDsdvEntrySet", indent);
uint16_t int_Indent = indent + 2;
std::list<RsTlvDsdvEntry>::iterator it;
for(it = entries.begin(); it != entries.end() ; ++it)
it->print(out, int_Indent);
printEnd(out, "RsTlvDsdvEntrySet", indent);
return out;
}
/************************************* RsTlvIpAddressInfo ************************************/

View File

@ -0,0 +1,73 @@
#ifndef RS_TLV_DSDV_TYPES_H
#define RS_TLV_DSDV_TYPES_H
/*
* libretroshare/src/serialiser: rstlvdsdv.h
*
* RetroShare Serialiser.
*
* 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".
*
*/
/*******************************************************************
* These are the Compound TLV structures that must be (un)packed.
******************************************************************/
#include <map>
#include "serialiser/rstlvtypes.h"
#include "util/rsnet.h"
#define RSDSDV_MAX_ROUTE_TABLE 1000
class RsTlvDsdvEntry: public RsTlvItem
{
public:
RsTlvDsdvEntry();
virtual ~RsTlvDsdvEntry() { return; }
virtual uint32_t TlvSize();
virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */
virtual std::ostream &print(std::ostream &out, uint16_t indent);
uint32_t idType;
std::string anonChunk;
std::string serviceId;
uint32_t sequence;
uint32_t distance;
};
class RsTlvDsdvEntrySet: public RsTlvItem
{
public:
RsTlvDsdvEntrySet();
virtual ~RsTlvDsdvEntrySet() { return; }
virtual uint32_t TlvSize();
virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset); /* serialise */
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset); /* deserialise */
virtual std::ostream &print(std::ostream &out, uint16_t indent);
std::list<RsTlvDsdvEntry> entries;
};
#endif

View File

@ -0,0 +1,539 @@
/*
* 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 "serialiser/rsdsdvitems.h"
#include "services/p3dsdv.h"
#include "pqi/p3linkmgr.h"
/****
* #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(p3LinkMgr *lm)
:p3Service(RS_SERVICE_TYPE_DSDV), /* p3Config(CONFIG_TYPE_DSDV), */ mDsdvMtx("p3Dsdv"), mLinkMgr(lm)
{
addSerialType(new RsDsdvSerialiser());
mSentTablesTime = 0;
mSentIncrementTime = 0;
}
int p3Dsdv::tick()
{
processIncoming();
sendTables();
return 0;
}
int p3Dsdv::status()
{
return 1;
}
#define DSDV_BROADCAST_PERIOD 60
#define DSDV_MIN_INCREMENT_PERIOD 5
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)
{
generateRoutingTables(false);
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
mSentTablesTime = now;
mSentIncrementTime = now;
return true ;
}
/* otherwise send incremental changes */
if ((updateRequired) && (now - it > DSDV_MIN_INCREMENT_PERIOD))
{
generateRoutingTables(true);
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
mSentIncrementTime = now;
}
return true;
}
#define RSDSDV_SEQ_INCREMENT 2
void p3Dsdv::advanceLocalSequenceNumbers()
{
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.mOwnSource)
{
v.mStableRoute.mSequence += RSDSDV_SEQ_INCREMENT;
v.mBestRoute.mSequence = v.mStableRoute.mSequence;
}
}
}
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::list<std::string> idList;
mLinkMgr->getOnlineList(idList);
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::generateRoutingTables(" << incremental << ")";
std::cerr << std::endl;
#endif
if (!incremental)
{
/* now clear significant flag */
advanceLocalSequenceNumbers();
}
/* prepare packets */
std::list<std::string>::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();
}
int p3Dsdv::generateRoutingTable(const std::string &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.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.idType = v.mDest.mIdType;
entry.anonChunk = v.mDest.mAnonChunk;
entry.serviceId = v.mDest.mHash;
entry.sequence = v.mStableRoute.mSequence;
entry.distance = v.mStableRoute.mDistance;
dsdv->routes.entries.push_back(entry);
if (dsdv->routes.entries.size() > RSDSDV_MAX_ROUTE_TABLE)
{
sendItem(dsdv);
dsdv = new RsDsdvRouteItem();
dsdv->PeerId(peerId);
}
}
sendItem(dsdv);
}
/****************************************************************************
****************************************************************************/
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);
std::list<RsTlvDsdvEntry>::iterator it;
for(it = dsdv->routes.entries.begin(); it != dsdv->routes.entries.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.serviceId);
if (tit == mTable.end())
{
/* new entry! */
RsDsdvTableEntry v;
v.mDest.mIdType = entry.idType;
v.mDest.mAnonChunk = entry.anonChunk;
v.mDest.mHash = entry.serviceId;
v.mBestRoute.mNextHop = dsdv->PeerId();
v.mBestRoute.mReceived = now;
v.mBestRoute.mSequence = entry.sequence;
v.mBestRoute.mDistance = realDistance;
v.mStableRoute = v.mBestRoute; // same as new.
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;
/* update best entry */
if ((entry.sequence >= v.mBestRoute.mSequence) || (realDistance < v.mBestRoute.mDistance))
{
v.mBestRoute.mNextHop = dsdv->PeerId();
v.mBestRoute.mReceived = now;
v.mBestRoute.mSequence = entry.sequence;
v.mBestRoute.mDistance = realDistance;
/* if consistent route... maintain */
if (v.mBestRoute.mNextHop == v.mStableRoute.mNextHop)
{
v.mStableRoute = v.mBestRoute;
v.mFlags |= RSDSDV_FLAGS_STABLE_ROUTE;
}
else
{
/* otherwise we need to wait - see if we get new update */
}
}
}
}
}
/*************** pqiMonitor callback ***********************/
void p3Dsdv::statusChange(const std::list<pqipeer> &plist)
{
std::list<pqipeer>::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::addDsdvId(RsDsdvId *id, std::string realHash)
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
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.mBestRoute.mNextHop = mLinkMgr->getOwnId();
v.mBestRoute.mReceived = now;
v.mBestRoute.mSequence = 0;
v.mBestRoute.mDistance = 0;
v.mStableRoute = v.mBestRoute; // same as new.
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 *******/
/* 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.mDest;
out << " BR: " << v.mBestRoute;
out << " SR: " << v.mBestRoute;
out << " Flags: " << v.mFlags;
out << " Own: " << v.mOwnSource;
if (v.mMatched)
{
out << " RH: " << v.mMatchedHash;
}
else
{
out << " Unknown";
}
out << std::endl;
}
}
/*****************************************/
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)
{
out << "< Seq: " << route.mSequence << " Dist: " << route.mDistance;
out << " NextHop: " << route.mNextHop << " recvd: " << route.mReceived;
out << " >";
return out;
}
std::ostream &operator<<(std::ostream &out, const RsDsdvTableEntry &entry)
{
out << "DSDV Route for: " << entry.mDest << std::endl;
out << "Stable: " << entry.mStableRoute << std::endl;
out << "Best: " << entry.mBestRoute << std::endl;
out << "OwnSource: " << entry.mOwnSource;
out << " Flags: " << entry.mFlags << std::endl;
if (entry.mMatched)
{
out << "Matched: " << entry.mMatchedHash;
}
else
{
out << "Non Matched";
}
out << std::endl;
return out;
}

View File

@ -0,0 +1,117 @@
/*
* 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 p3LinkMgr;
#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 pqiMonitor
{
public:
p3Dsdv(p3LinkMgr *cm);
/*** internal librs interface ****/
int addDsdvId(RsDsdvId *id, std::string realHash);
int dropDsdvId(RsDsdvId *id);
int printDsdvTable(std::ostream &out);
private:
int sendTables();
void advanceLocalSequenceNumbers();
void clearSignificantChangesFlags();
int generateRoutingTables(bool incremental);
int generateRoutingTable(const std::string &peerId, bool incremental);
int processIncoming();
int handleDSDV(RsDsdvRouteItem *dsdv);
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<pqipeer> &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;
p3LinkMgr *mLinkMgr;
};
#endif // SERVICE_RSDSDV_HEADER