2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* libretroshare/src/serialiser: rsdiscitems.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/rsbaseserial.h"
|
|
|
|
|
|
|
|
#include "serialiser/rsserviceids.h"
|
|
|
|
#include "serialiser/rsdiscitems.h"
|
|
|
|
|
|
|
|
#include "serialiser/rstlvbase.h"
|
|
|
|
#include "serialiser/rstlvtypes.h"
|
|
|
|
|
2008-03-31 10:06:59 -04:00
|
|
|
/***
|
2007-12-11 19:54:42 -05:00
|
|
|
#define RSSERIAL_DEBUG 1
|
2008-03-31 10:06:59 -04:00
|
|
|
***/
|
|
|
|
|
2009-05-25 07:38:47 -04:00
|
|
|
#define RSSERIAL_DEBUG 1
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
uint32_t RsDiscSerialiser::size(RsItem *i)
|
|
|
|
{
|
2009-06-23 17:36:02 -04:00
|
|
|
RsDiscOwnItem *rdi;
|
2007-12-11 19:54:42 -05:00
|
|
|
RsDiscReply *rdr;
|
2009-05-25 07:38:47 -04:00
|
|
|
RsDiscIssuer *rds;
|
2009-06-28 16:57:02 -04:00
|
|
|
RsDiscVersion *rdv;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
/* do reply first - as it is derived from Item */
|
|
|
|
if (NULL != (rdr = dynamic_cast<RsDiscReply *>(i)))
|
|
|
|
{
|
|
|
|
return sizeReply(rdr);
|
|
|
|
}
|
2009-05-25 07:38:47 -04:00
|
|
|
else if (NULL != (rds = dynamic_cast<RsDiscIssuer *>(i)))
|
|
|
|
{
|
|
|
|
return sizeIssuer(rds);
|
|
|
|
}
|
2009-06-23 17:36:02 -04:00
|
|
|
else if (NULL != (rdi = dynamic_cast<RsDiscOwnItem *>(i)))
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
|
|
|
return sizeItem(rdi);
|
|
|
|
}
|
2009-06-28 16:57:02 -04:00
|
|
|
else if (NULL != (rdv = dynamic_cast<RsDiscVersion *>(i)))
|
|
|
|
{
|
|
|
|
return sizeVersion(rdv);
|
|
|
|
}
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
|
|
|
bool RsDiscSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize)
|
|
|
|
{
|
2009-06-23 17:36:02 -04:00
|
|
|
RsDiscOwnItem *rdi;
|
2007-12-11 19:54:42 -05:00
|
|
|
RsDiscReply *rdr;
|
2009-05-25 07:38:47 -04:00
|
|
|
RsDiscIssuer *rds;
|
2009-06-28 16:57:02 -04:00
|
|
|
RsDiscVersion *rdv;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
/* do reply first - as it is derived from Item */
|
|
|
|
if (NULL != (rdr = dynamic_cast<RsDiscReply *>(i)))
|
|
|
|
{
|
|
|
|
return serialiseReply(rdr, data, pktsize);
|
|
|
|
}
|
2009-05-25 07:38:47 -04:00
|
|
|
else if (NULL != (rds = dynamic_cast<RsDiscIssuer *>(i)))
|
|
|
|
{
|
|
|
|
return serialiseIssuer(rds, data, pktsize);
|
|
|
|
}
|
2009-06-23 17:36:02 -04:00
|
|
|
else if (NULL != (rdi = dynamic_cast<RsDiscOwnItem *>(i)))
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
|
|
|
return serialiseItem(rdi, data, pktsize);
|
|
|
|
}
|
2009-06-28 16:57:02 -04:00
|
|
|
else if (NULL != (rdv = dynamic_cast<RsDiscVersion *>(i)))
|
|
|
|
{
|
|
|
|
return serialiseVersion(rdv, data, pktsize);
|
|
|
|
}
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsItem *RsDiscSerialiser::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_DISC != getRsItemService(rstype)))
|
|
|
|
{
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialise() Wrong Type" << std::endl;
|
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(getRsItemSubType(rstype))
|
|
|
|
{
|
|
|
|
case RS_PKT_SUBTYPE_DISC_REPLY:
|
|
|
|
return deserialiseReply(data, pktsize);
|
|
|
|
break;
|
2009-06-23 17:36:02 -04:00
|
|
|
case RS_PKT_SUBTYPE_DISC_OWN:
|
|
|
|
return deserialiseOwnItem(data, pktsize);
|
2007-12-11 19:54:42 -05:00
|
|
|
break;
|
2009-05-25 07:38:47 -04:00
|
|
|
case RS_PKT_SUBTYPE_DISC_ISSUER:
|
|
|
|
return deserialiseIssuer(data, pktsize);
|
|
|
|
break;
|
2009-06-28 16:57:02 -04:00
|
|
|
case RS_PKT_SUBTYPE_DISC_VERSION:
|
|
|
|
return deserialiseVersion(data, pktsize);
|
|
|
|
break;
|
2007-12-11 19:54:42 -05:00
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2009-06-23 17:36:02 -04:00
|
|
|
RsDiscOwnItem::~RsDiscOwnItem()
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-23 17:36:02 -04:00
|
|
|
void RsDiscOwnItem::clear()
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
|
|
|
memset(&laddr, 0, sizeof(laddr));
|
|
|
|
memset(&saddr, 0, sizeof(laddr));
|
2008-02-03 01:29:02 -05:00
|
|
|
contact_tf = 0;
|
2007-12-11 19:54:42 -05:00
|
|
|
discFlags = 0;
|
|
|
|
}
|
|
|
|
|
2009-06-23 17:36:02 -04:00
|
|
|
std::ostream &RsDiscOwnItem::print(std::ostream &out, uint16_t indent)
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
2009-06-23 17:36:02 -04:00
|
|
|
printRsItemBase(out, "RsDiscOwnItem", indent);
|
2007-12-11 19:54:42 -05:00
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "Local Address: " << inet_ntoa(laddr.sin_addr);
|
|
|
|
out << " Port: " << ntohs(laddr.sin_port) << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "Server Address: " << inet_ntoa(saddr.sin_addr);
|
|
|
|
out << " Port: " << ntohs(saddr.sin_port) << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2008-02-03 01:29:02 -05:00
|
|
|
out << "Contact TimeFrame: " << contact_tf;
|
|
|
|
out << std::endl;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "DiscFlags: " << discFlags << std::endl;
|
|
|
|
|
2009-06-23 17:36:02 -04:00
|
|
|
printRsItemEnd(out, "RsDiscOwnItem", indent);
|
2007-12-11 19:54:42 -05:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-23 17:36:02 -04:00
|
|
|
uint32_t RsDiscSerialiser::sizeItem(RsDiscOwnItem *item)
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
|
|
|
uint32_t s = 8; /* header */
|
|
|
|
s += GetTlvIpAddrPortV4Size(); /* laddr */
|
|
|
|
s += GetTlvIpAddrPortV4Size(); /* saddr */
|
2008-02-03 01:29:02 -05:00
|
|
|
s += 2; /* contact_tf */
|
2007-12-11 19:54:42 -05:00
|
|
|
s += 4; /* discFlags */
|
2009-12-18 12:51:08 -05:00
|
|
|
s += 4; /* ipaddress list size */
|
2007-12-11 19:54:42 -05:00
|
|
|
|
2009-11-11 11:49:11 -05:00
|
|
|
//add the size of the ip list
|
|
|
|
int ipListSize = item->ipAddressList.size();
|
|
|
|
s += ipListSize * GetTlvIpAddrPortV4Size();
|
|
|
|
s += ipListSize * 8; //size of an uint64
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
2009-06-23 17:36:02 -04:00
|
|
|
bool RsDiscSerialiser::serialiseItem(RsDiscOwnItem *item, void *data, uint32_t *pktsize)
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseItem() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseItem() Size: " << tlvsize << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
2009-06-28 16:57:02 -04:00
|
|
|
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset,
|
2007-12-11 19:54:42 -05:00
|
|
|
TLV_TYPE_IPV4_LOCAL, &(item->laddr));
|
2009-12-17 17:22:27 -05:00
|
|
|
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset,
|
2008-02-07 11:18:34 -05:00
|
|
|
TLV_TYPE_IPV4_REMOTE, &(item->saddr));
|
2008-02-03 01:29:02 -05:00
|
|
|
ok &= setRawUInt16(data, tlvsize, &offset, item->contact_tf);
|
2007-12-11 19:54:42 -05:00
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->discFlags);
|
|
|
|
|
2009-12-18 12:51:08 -05:00
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->ipAddressList.size());
|
|
|
|
|
|
|
|
//store the ip list
|
|
|
|
std::list<IpAddressTimed>::iterator ipListIt;
|
|
|
|
for (ipListIt = item->ipAddressList.begin(); ipListIt!=(item->ipAddressList.end()); ipListIt++) {
|
|
|
|
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_REMOTE, &(ipListIt->ipAddr));
|
|
|
|
ok &= setRawUInt64(data, tlvsize, &offset, ipListIt->seenTime);
|
|
|
|
}
|
2009-11-11 11:49:11 -05:00
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseItem() Size Error! " << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2009-06-23 17:36:02 -04:00
|
|
|
RsDiscOwnItem *RsDiscSerialiser::deserialiseOwnItem(void *data, uint32_t *pktsize)
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
|
|
|
/* get the type and size */
|
|
|
|
uint32_t rstype = getRsItemId(data);
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
|
|
|
(RS_SERVICE_TYPE_DISC != getRsItemService(rstype)) ||
|
2009-06-23 17:36:02 -04:00
|
|
|
(RS_PKT_SUBTYPE_DISC_OWN != getRsItemSubType(rstype)))
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseItem() Wrong Type" << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseItem() Not Enough Space" << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
return NULL; /* not enough data */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the packet length */
|
|
|
|
*pktsize = rssize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
/* ready to load */
|
2009-06-23 17:36:02 -04:00
|
|
|
RsDiscOwnItem *item = new RsDiscOwnItem();
|
2007-12-11 19:54:42 -05:00
|
|
|
item->clear();
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* get mandatory parts first */
|
2009-06-28 16:57:02 -04:00
|
|
|
ok &= GetTlvIpAddrPortV4(data, rssize, &offset,
|
2007-12-11 19:54:42 -05:00
|
|
|
TLV_TYPE_IPV4_LOCAL, &(item->laddr));
|
2009-06-28 16:57:02 -04:00
|
|
|
ok &= GetTlvIpAddrPortV4(data, rssize, &offset,
|
2008-02-07 11:18:34 -05:00
|
|
|
TLV_TYPE_IPV4_REMOTE, &(item->saddr));
|
2008-02-03 01:29:02 -05:00
|
|
|
ok &= getRawUInt16(data, rssize, &offset, &(item->contact_tf));
|
2007-12-11 19:54:42 -05:00
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &(item->discFlags));
|
|
|
|
|
2009-12-18 12:51:08 -05:00
|
|
|
uint32_t listSize;
|
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &listSize);
|
|
|
|
|
2009-11-11 11:49:11 -05:00
|
|
|
//get the ip adress list
|
2009-12-18 12:51:08 -05:00
|
|
|
uint32_t count = 0;
|
|
|
|
std::list<IpAddressTimed> ipTimedList;
|
|
|
|
while (offset < rssize && count < listSize) {
|
|
|
|
count++;
|
|
|
|
IpAddressTimed ipTimed;
|
|
|
|
ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_REMOTE, &ipTimed.ipAddr);
|
|
|
|
uint64_t time;
|
|
|
|
ok &= getRawUInt64(data, rssize, &offset, &time);
|
|
|
|
ipTimed.seenTime = time;
|
|
|
|
ipTimedList.push_back(ipTimed);
|
|
|
|
}
|
|
|
|
item->ipAddressList = ipTimedList;
|
2009-11-11 11:49:11 -05:00
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
if (offset != rssize)
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseItem() offset != rssize" << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
/* error */
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseItem() ok = false" << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
RsDiscReply::~RsDiscReply()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsDiscReply::clear()
|
|
|
|
{
|
2009-11-11 11:43:51 -05:00
|
|
|
memset(¤tladdr, 0, sizeof(currentladdr));
|
|
|
|
memset(¤tsaddr, 0, sizeof(currentladdr));
|
2008-02-03 01:29:02 -05:00
|
|
|
contact_tf = 0;
|
2007-12-11 19:54:42 -05:00
|
|
|
discFlags = 0;
|
2008-02-03 01:29:02 -05:00
|
|
|
aboutId.clear();
|
2007-12-11 19:54:42 -05:00
|
|
|
certDER.TlvClear();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &RsDiscReply::print(std::ostream &out, uint16_t indent)
|
|
|
|
{
|
|
|
|
printRsItemBase(out, "RsDiscReply", indent);
|
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2009-11-11 11:43:51 -05:00
|
|
|
out << "Local Address: " << inet_ntoa(currentladdr.sin_addr);
|
|
|
|
out << " Port: " << ntohs(currentladdr.sin_port) << std::endl;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2009-11-11 11:43:51 -05:00
|
|
|
out << "Server Address: " << inet_ntoa(currentsaddr.sin_addr);
|
|
|
|
out << " Port: " << ntohs(currentsaddr.sin_port) << std::endl;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2008-02-03 01:29:02 -05:00
|
|
|
out << "Contact TimeFrame: " << contact_tf;
|
|
|
|
out << std::endl;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "DiscFlags: " << discFlags << std::endl;
|
|
|
|
|
2008-02-03 01:29:02 -05:00
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "AboutId: " << aboutId << std::endl;
|
2007-12-11 19:54:42 -05:00
|
|
|
certDER.print(out, int_Indent);
|
|
|
|
|
|
|
|
printRsItemEnd(out, "RsDiscReply", indent);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t RsDiscSerialiser::sizeReply(RsDiscReply *item)
|
|
|
|
{
|
|
|
|
uint32_t s = 8; /* header */
|
|
|
|
s += GetTlvIpAddrPortV4Size(); /* laddr */
|
|
|
|
s += GetTlvIpAddrPortV4Size(); /* saddr */
|
|
|
|
s += 2; /* connect_tr */
|
|
|
|
s += 4; /* discFlags */
|
2008-02-03 01:29:02 -05:00
|
|
|
s += GetTlvStringSize(item->aboutId);
|
2007-12-11 19:54:42 -05:00
|
|
|
s += item->certDER.TlvSize();
|
2009-12-18 12:51:08 -05:00
|
|
|
s += 4; /* ipaddress list size */
|
2007-12-11 19:54:42 -05:00
|
|
|
|
2009-11-11 11:49:11 -05:00
|
|
|
//add the size of the ip list
|
|
|
|
int ipListSize = item->ipAddressList.size();
|
|
|
|
s += ipListSize * GetTlvIpAddrPortV4Size();
|
|
|
|
s += ipListSize * 8; //size of an uint64
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
|
|
|
bool RsDiscSerialiser::serialiseReply(RsDiscReply *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
uint32_t tlvsize = sizeReply(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);
|
|
|
|
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() Size: " << tlvsize << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
2009-11-11 11:43:51 -05:00
|
|
|
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_LOCAL, &(item->currentladdr));
|
|
|
|
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_REMOTE, &(item->currentsaddr));
|
2008-02-03 01:29:02 -05:00
|
|
|
ok &= setRawUInt16(data, tlvsize, &offset, item->contact_tf);
|
2007-12-11 19:54:42 -05:00
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->discFlags);
|
|
|
|
|
2009-02-22 12:36:39 -05:00
|
|
|
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->aboutId);
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
ok &= item->certDER.SetTlv(data, tlvsize, &offset);
|
|
|
|
|
2009-12-18 12:51:08 -05:00
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, item->ipAddressList.size());
|
2009-11-11 11:49:11 -05:00
|
|
|
|
2009-12-18 12:51:08 -05:00
|
|
|
//store the ip list
|
|
|
|
std::list<IpAddressTimed>::iterator ipListIt;
|
|
|
|
for (ipListIt = item->ipAddressList.begin(); ipListIt!=(item->ipAddressList.end()); ipListIt++) {
|
2009-11-11 11:49:11 -05:00
|
|
|
ok &= SetTlvIpAddrPortV4(data, tlvsize, &offset, TLV_TYPE_IPV4_REMOTE, &(ipListIt->ipAddr));
|
|
|
|
ok &= setRawUInt64(data, tlvsize, &offset, ipListIt->seenTime);
|
|
|
|
}
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() Size Error! " << std::endl;
|
2009-04-22 19:29:16 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsDiscReply *RsDiscSerialiser::deserialiseReply(void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
/* get the type and size */
|
|
|
|
uint32_t rstype = getRsItemId(data);
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
|
|
|
(RS_SERVICE_TYPE_DISC != getRsItemService(rstype)) ||
|
|
|
|
(RS_PKT_SUBTYPE_DISC_REPLY != getRsItemSubType(rstype)))
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseReply() Wrong Type" << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseReply() pktsize != rssize" << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
return NULL; /* not enough data */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the packet length */
|
|
|
|
*pktsize = rssize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
/* ready to load */
|
|
|
|
RsDiscReply *item = new RsDiscReply();
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* get mandatory parts first */
|
2009-06-28 16:57:02 -04:00
|
|
|
ok &= GetTlvIpAddrPortV4(data, rssize, &offset,
|
2009-11-11 11:43:51 -05:00
|
|
|
TLV_TYPE_IPV4_LOCAL, &(item->currentladdr));
|
2009-06-28 16:57:02 -04:00
|
|
|
ok &= GetTlvIpAddrPortV4(data, rssize, &offset,
|
2009-11-11 11:43:51 -05:00
|
|
|
TLV_TYPE_IPV4_REMOTE, &(item->currentsaddr));
|
2008-02-03 01:29:02 -05:00
|
|
|
ok &= getRawUInt16(data, rssize, &offset, &(item->contact_tf));
|
2007-12-11 19:54:42 -05:00
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &(item->discFlags));
|
2008-02-03 01:29:02 -05:00
|
|
|
|
2009-06-28 16:57:02 -04:00
|
|
|
ok &= GetTlvString(data, rssize, &offset,
|
2008-02-03 01:29:02 -05:00
|
|
|
TLV_TYPE_STR_PEERID, item->aboutId);
|
2007-12-11 19:54:42 -05:00
|
|
|
ok &= item->certDER.GetTlv(data, rssize, &offset);
|
|
|
|
|
2009-12-18 12:51:08 -05:00
|
|
|
uint32_t listSize;
|
|
|
|
ok &= getRawUInt32(data, rssize, &offset, &listSize);
|
|
|
|
|
|
|
|
//get the ip adress list
|
|
|
|
uint32_t count = 0;
|
|
|
|
std::list<IpAddressTimed> ipTimedList;
|
|
|
|
while (offset < rssize && count < listSize) {
|
2009-11-11 11:49:11 -05:00
|
|
|
IpAddressTimed ipTimed;
|
|
|
|
ok &= GetTlvIpAddrPortV4(data, rssize, &offset, TLV_TYPE_IPV4_REMOTE, &ipTimed.ipAddr);
|
|
|
|
uint64_t time;
|
|
|
|
ok &= getRawUInt64(data, rssize, &offset, &time);
|
|
|
|
ipTimed.seenTime = time;
|
|
|
|
ipTimedList.push_back(ipTimed);
|
|
|
|
}
|
|
|
|
item->ipAddressList = ipTimedList;
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
if (offset != rssize)
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseReply() offset != rssize" << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
/* error */
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2007-12-11 19:54:42 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseReply() ok = false" << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-05-25 07:38:47 -04:00
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
RsDiscIssuer::~RsDiscIssuer()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RsDiscIssuer::clear()
|
|
|
|
{
|
|
|
|
issuerCert = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &RsDiscIssuer::print(std::ostream &out, uint16_t indent)
|
|
|
|
{
|
|
|
|
printRsItemBase(out, "RsDiscIssuer", indent);
|
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "Cert String: " << issuerCert << std::endl;
|
|
|
|
|
|
|
|
printRsItemEnd(out, "RsDiscIssuer", indent);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t RsDiscSerialiser::sizeIssuer(RsDiscIssuer *item)
|
|
|
|
{
|
|
|
|
uint32_t s = 8; /* header */
|
|
|
|
s += 4; /* size in RawString() */
|
|
|
|
s += item->issuerCert.length();
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
|
|
|
bool RsDiscSerialiser::serialiseIssuer(RsDiscIssuer *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
uint32_t tlvsize = sizeIssuer(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);
|
|
|
|
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2009-05-25 07:38:47 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseIssuer() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseIssuer() Size: " << tlvsize << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
ok &= setRawString(data, tlvsize, &offset, item->issuerCert);
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2009-05-25 07:38:47 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseIssuer() Size Error! " << std::endl;
|
|
|
|
std::cerr << "Offset: " << offset << " tlvsize: " << tlvsize << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsDiscIssuer *RsDiscSerialiser::deserialiseIssuer(void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
/* get the type and size */
|
|
|
|
uint32_t rstype = getRsItemId(data);
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
|
|
|
(RS_SERVICE_TYPE_DISC != getRsItemService(rstype)) ||
|
|
|
|
(RS_PKT_SUBTYPE_DISC_ISSUER != getRsItemSubType(rstype)))
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2009-05-25 07:38:47 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseIssuer() Wrong Type" << std::endl;
|
|
|
|
#endif
|
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2009-05-25 07:38:47 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseIssuer() pktsize != rssize" << std::endl;
|
|
|
|
std::cerr << "Pktsize: " << *pktsize << " Rssize: " << rssize << std::endl;
|
|
|
|
#endif
|
|
|
|
return NULL; /* not enough data */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the packet length */
|
|
|
|
*pktsize = rssize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
/* ready to load */
|
|
|
|
RsDiscIssuer *item = new RsDiscIssuer();
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* get mandatory parts first */
|
|
|
|
ok &= getRawString(data, rssize, &offset, item->issuerCert);
|
|
|
|
|
|
|
|
if (offset != rssize)
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2009-05-25 07:38:47 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseIssuer() offset != rssize" << std::endl;
|
|
|
|
std::cerr << "Offset: " << offset << " Rssize: " << rssize << std::endl;
|
|
|
|
#endif
|
|
|
|
/* error */
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
2009-06-28 16:57:02 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
2009-05-25 07:38:47 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseIssuer() ok = false" << std::endl;
|
|
|
|
#endif
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-06-28 16:57:02 -04:00
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
RsDiscVersion::~RsDiscVersion()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
void RsDiscVersion::clear()
|
|
|
|
{
|
|
|
|
version = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &RsDiscVersion::print(std::ostream &out, uint16_t indent)
|
|
|
|
{
|
|
|
|
printRsItemBase(out, "RsDiscVersion", indent);
|
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "Version String: " << version << std::endl;
|
|
|
|
|
|
|
|
printRsItemEnd(out, "RsDiscVersion", indent);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RsDiscSerialiser::sizeVersion(RsDiscVersion *item)
|
|
|
|
{
|
2009-09-16 15:42:00 -04:00
|
|
|
uint32_t s = 8; /* header */
|
2009-09-19 13:36:29 -04:00
|
|
|
s += GetTlvStringSize(item->version);
|
2009-06-28 16:57:02 -04:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
|
|
|
bool RsDiscSerialiser::serialiseVersion(RsDiscVersion *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
uint32_t tlvsize = sizeVersion(item);
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (*pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
*pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data, *pktsize, item->PacketId(), *pktsize);
|
|
|
|
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseVersion() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseVersion() Size: " << tlvsize << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
2009-09-19 13:36:29 -04:00
|
|
|
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, item->version);
|
2009-06-28 16:57:02 -04:00
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseVersion() Size Error! " << std::endl;
|
|
|
|
std::cerr << "Offset: " << offset << " tlvsize: " << tlvsize << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsDiscVersion *RsDiscSerialiser::deserialiseVersion(void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
/* get the type and size */
|
|
|
|
uint32_t rstype = getRsItemId(data);
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
|
|
|
(RS_SERVICE_TYPE_DISC != getRsItemService(rstype)) ||
|
|
|
|
(RS_PKT_SUBTYPE_DISC_VERSION != getRsItemSubType(rstype)))
|
|
|
|
{
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseVersion() Wrong Type" << std::endl;
|
|
|
|
#endif
|
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
{
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseVersion() pktsize != rssize" << std::endl;
|
|
|
|
std::cerr << "Pktsize: " << *pktsize << " Rssize: " << rssize << std::endl;
|
|
|
|
#endif
|
|
|
|
return NULL; /* not enough data */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the packet length */
|
|
|
|
*pktsize = rssize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
/* ready to load */
|
|
|
|
RsDiscVersion *item = new RsDiscVersion();
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
2009-09-19 13:36:29 -04:00
|
|
|
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VALUE, item->version);
|
2009-06-28 16:57:02 -04:00
|
|
|
|
|
|
|
if (offset != rssize)
|
|
|
|
{
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseVersion() offset != rssize" << std::endl;
|
|
|
|
std::cerr << "Offset: " << offset << " Rssize: " << rssize << std::endl;
|
|
|
|
#endif
|
|
|
|
/* error */
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseVersion() ok = false" << std::endl;
|
|
|
|
#endif
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
/*************************************************************************/
|