Added first part of p3banlist.

This is a service to exchange bad IP addresses within Retroshare network.
 


git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-dhtmods@4684 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2011-11-23 16:40:32 +00:00
parent 7b0a33c318
commit f01d06c806
9 changed files with 1189 additions and 2 deletions

View file

@ -0,0 +1,203 @@
/*
* libretroshare/src/serialiser: rsbanlist.cc
*
* 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".
*
*/
#include "serialiser/rsbaseserial.h"
#include "serialiser/rsbanlistitems.h"
#include "serialiser/rstlvbanlist.h"
/***
#define RSSERIAL_DEBUG 1
***/
#include <iostream>
/*************************************************************************/
RsBanListItem::~RsBanListItem()
{
return;
}
void RsBanListItem::clear()
{
peerList.TlvClear();
}
std::ostream &RsBanListItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsBanListItem", indent);
uint16_t int_Indent = indent + 2;
peerList.print(out, int_Indent);
printRsItemEnd(out, "RsBanListItem", indent);
return out;
}
uint32_t RsBanListSerialiser::sizeList(RsBanListItem *item)
{
uint32_t s = 8; /* header */
s += item->peerList.TlvSize();
return s;
}
/* serialise the data to the buffer */
bool RsBanListSerialiser::serialiseList(RsBanListItem *item, void *data, uint32_t *pktsize)
{
uint32_t tlvsize = sizeList(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->peerList.SetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
ok = false;
#ifdef RSSERIAL_DEBUG
std::cerr << "RsDsdvSerialiser::serialiseRoute() Size Error! " << std::endl;
#endif
}
return ok;
}
RsBanListItem *RsBanListSerialiser::deserialiseList(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_BANLIST != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_BANLIST_ITEM != 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 */
RsBanListItem *item = new RsBanListItem();
item->clear();
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= item->peerList.GetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
/* error */
delete item;
return NULL;
}
if (!ok)
{
delete item;
return NULL;
}
return item;
}
/*************************************************************************/
uint32_t RsBanListSerialiser::size(RsItem *i)
{
RsBanListItem *dri;
if (NULL != (dri = dynamic_cast<RsBanListItem *>(i)))
{
return sizeList(dri);
}
return 0;
}
bool RsBanListSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize)
{
RsBanListItem *dri;
if (NULL != (dri = dynamic_cast<RsBanListItem *>(i)))
{
return serialiseList(dri, data, pktsize);
}
return false;
}
RsItem *RsBanListSerialiser::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_BANLIST != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_BANLIST_ITEM:
return deserialiseList(data, pktsize);
break;
default:
return NULL;
break;
}
}
/*************************************************************************/

View file

@ -0,0 +1,82 @@
#ifndef RS_BANLIST_ITEMS_H
#define RS_BANLIST_ITEMS_H
/*
* libretroshare/src/serialiser: rsbanlistitems.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".
*
*/
#include <map>
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvtypes.h"
#include "serialiser/rstlvbanlist.h"
#define RS_PKT_SUBTYPE_BANLIST_ITEM 0x01
/**************************************************************************/
class RsBanListItem: public RsItem
{
public:
RsBanListItem()
:RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST,
RS_PKT_SUBTYPE_BANLIST_ITEM)
{ return; }
virtual ~RsBanListItem();
virtual void clear();
std::ostream &print(std::ostream &out, uint16_t indent = 0);
RsTlvBanList peerList;
};
class RsBanListSerialiser: public RsSerialType
{
public:
RsBanListSerialiser()
:RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST)
{ return; }
virtual ~RsBanListSerialiser()
{ 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 sizeList(RsBanListItem *);
virtual bool serialiseList (RsBanListItem *item, void *data, uint32_t *size);
virtual RsBanListItem *deserialiseList(void *data, uint32_t *size);
};
/**************************************************************************/
#endif /* RS_BANLIST_ITEMS_H */

View file

@ -95,6 +95,9 @@ const uint16_t RS_SERVICE_TYPE_PHOTO = 0xf040;
/* DSDV Testing at the moment - Service Only */
const uint16_t RS_SERVICE_TYPE_DSDV = 0xf050;
/* BanList Testing at the moment - Service Only */
const uint16_t RS_SERVICE_TYPE_BANLIST = 0xf060;
/* Games/External Apps - Service Only */
const uint16_t RS_SERVICE_TYPE_GAME_LAUNCHER = 0xf200;
const uint16_t RS_SERVICE_TYPE_PORT = 0xf201;

View file

@ -0,0 +1,309 @@
/*
* 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 "rstlvbanlist.h"
#include "rstlvbase.h"
#include "rstlvtypes.h"
#include "rsbaseserial.h"
#include "util/rsprint.h"
#include <ostream>
#include <sstream>
#include <iomanip>
#include <iostream>
/************************************* RsTlvBanListEntry ************************************/
RsTlvBanListEntry::RsTlvBanListEntry()
:RsTlvItem(), level(0), reason(0), age(0)
{
return;
}
void RsTlvBanListEntry::TlvClear()
{
sockaddr_clear(&addr);
level = 0;
reason = 0;
age = 0;
}
uint32_t RsTlvBanListEntry::TlvSize()
{
uint32_t s = TLV_HEADER_SIZE;
s += GetTlvIpAddrPortV4Size();
s += 4; // level;
s += 4; // reason;
s += 4; // age;
return s;
}
bool RsTlvBanListEntry::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_BAN_ENTRY, tlvsize);
ok &= SetTlvIpAddrPortV4(data, tlvend, offset, TLV_TYPE_IPV4_REMOTE, &addr);
ok &= setRawUInt32(data, tlvend, offset, level);
ok &= setRawUInt32(data, tlvend, offset, reason);
ok &= setRawUInt32(data, tlvend, offset, age);
return ok;
}
bool RsTlvBanListEntry::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_BAN_ENTRY) /* check type */
return false;
bool ok = true;
/* ready to load */
TlvClear();
/* skip the header */
(*offset) += TLV_HEADER_SIZE;
ok &= GetTlvIpAddrPortV4(data, tlvend, offset, TLV_TYPE_IPV4_REMOTE, &addr);
ok &= getRawUInt32(data, tlvend, offset, &(level));
ok &= getRawUInt32(data, tlvend, offset, &(reason));
ok &= getRawUInt32(data, tlvend, offset, &(age));
/***************************************************************************
* 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 << "RsTlvBanListEntry::GetTlv() Warning extra bytes at end of item";
std::cerr << std::endl;
#endif
*offset = tlvend;
}
return ok;
}
std::ostream &RsTlvBanListEntry::print(std::ostream &out, uint16_t indent)
{
printBase(out, "RsTlvBanListEntry", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "addr:" << rs_inet_ntoa(addr.sin_addr) << ":" << htons(addr.sin_port);
out << std::endl;
printIndent(out, int_Indent);
out << "level:" << level;
out << std::endl;
printIndent(out, int_Indent);
out << "reason:" << reason;
out << std::endl;
printIndent(out, int_Indent);
out << "age:" << age;
out << std::endl;
printEnd(out, "RsTlvBanListEntry", indent);
return out;
}
/************************************* RsTlvBanList ************************************/
void RsTlvBanList::TlvClear()
{
entries.clear();
}
uint32_t RsTlvBanList::TlvSize()
{
uint32_t s = TLV_HEADER_SIZE; /* header */
std::list<RsTlvBanListEntry>::iterator it;
if(!entries.empty())
{
for(it = entries.begin(); it != entries.end() ; ++it)
s += it->TlvSize();
}
return s;
}
bool RsTlvBanList::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_BAN_LIST , tlvsize);
if(!entries.empty())
{
std::list<RsTlvBanListEntry>::iterator it;
for(it = entries.begin(); it != entries.end() ; ++it)
ok &= it->SetTlv(data, size, offset);
}
return ok;
}
bool RsTlvBanList::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_BAN_LIST) /* 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_BAN_ENTRY:
{
RsTlvBanListEntry 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 << "RsTlvBanList::GetTlv() Warning extra bytes at end of item";
std::cerr << std::endl;
#endif
*offset = tlvend;
}
return ok;
}
// prints out contents of RsTlvBanList
std::ostream &RsTlvBanList::print(std::ostream &out, uint16_t indent)
{
printBase(out, "RsTlvBanList", indent);
uint16_t int_Indent = indent + 2;
std::list<RsTlvBanListEntry>::iterator it;
for(it = entries.begin(); it != entries.end() ; ++it)
it->print(out, int_Indent);
printEnd(out, "RsTlvBanList", indent);
return out;
}
/************************************* RsTlvBanList ************************************/

View file

@ -0,0 +1,72 @@
#ifndef RS_TLV_BANLIST_TYPES_H
#define RS_TLV_BANLIST_TYPES_H
/*
* libretroshare/src/serialiser: rstlvbanlist.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 RsTlvBanListEntry: public RsTlvItem
{
public:
RsTlvBanListEntry();
virtual ~RsTlvBanListEntry() { 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);
struct sockaddr_in addr;
uint32_t level;
uint32_t reason;
uint32_t age;
};
class RsTlvBanList: public RsTlvItem
{
public:
RsTlvBanList();
virtual ~RsTlvBanList() { 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<RsTlvBanListEntry> entries;
};
#endif

View file

@ -216,7 +216,8 @@ const uint16_t TLV_TYPE_DSDV_ENDPOINT = 0x1080;
const uint16_t TLV_TYPE_DSDV_ENTRY = 0x1081;
const uint16_t TLV_TYPE_DSDV_ENTRY_SET= 0x1082;
const uint16_t TLV_TYPE_BAN_ENTRY = 0x1090;
const uint16_t TLV_TYPE_BAN_LIST = 0x1091;
const uint32_t RSTLV_IMAGE_TYPE_PNG = 0x0001;