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

@ -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