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
|
|
|
/***
|
2011-07-29 11:17:39 -04:00
|
|
|
* #define RSSERIAL_DEBUG 1
|
|
|
|
* #define RSSERIAL_ERROR_DEBUG 1
|
|
|
|
***/
|
|
|
|
|
|
|
|
#define RSSERIAL_ERROR_DEBUG 1
|
2008-03-31 10:06:59 -04:00
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
uint32_t RsDiscSerialiser::size(RsItem *i)
|
|
|
|
{
|
2010-01-19 14:15:22 -05:00
|
|
|
RsDiscAskInfo *inf;
|
|
|
|
RsDiscReply *rdr;
|
2009-06-28 16:57:02 -04:00
|
|
|
RsDiscVersion *rdv;
|
2009-12-22 05:41:19 -05:00
|
|
|
RsDiscHeartbeat *rdt;
|
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);
|
|
|
|
}
|
2010-01-19 14:15:22 -05:00
|
|
|
else if (NULL != (inf = dynamic_cast<RsDiscAskInfo *>(i)))
|
2009-05-25 07:38:47 -04:00
|
|
|
{
|
2010-01-19 14:15:22 -05:00
|
|
|
return sizeAskInfo(inf);
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
2009-06-28 16:57:02 -04:00
|
|
|
else if (NULL != (rdv = dynamic_cast<RsDiscVersion *>(i)))
|
|
|
|
{
|
|
|
|
return sizeVersion(rdv);
|
|
|
|
}
|
2009-12-22 05:41:19 -05:00
|
|
|
else if (NULL != (rdt = dynamic_cast<RsDiscHeartbeat *>(i)))
|
|
|
|
{
|
|
|
|
return sizeHeartbeat(rdt);
|
|
|
|
}
|
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)
|
|
|
|
{
|
2010-01-19 14:15:22 -05:00
|
|
|
RsDiscAskInfo *inf;
|
|
|
|
RsDiscReply *rdr;
|
2009-06-28 16:57:02 -04:00
|
|
|
RsDiscVersion *rdv;
|
2009-12-22 05:41:19 -05:00
|
|
|
RsDiscHeartbeat *rdt;
|
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);
|
|
|
|
}
|
2010-01-19 14:15:22 -05:00
|
|
|
else if (NULL != (inf = dynamic_cast<RsDiscAskInfo *>(i)))
|
2009-05-25 07:38:47 -04:00
|
|
|
{
|
2010-01-19 14:15:22 -05:00
|
|
|
return serialiseAskInfo(inf, data, pktsize);
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
2009-06-28 16:57:02 -04:00
|
|
|
else if (NULL != (rdv = dynamic_cast<RsDiscVersion *>(i)))
|
|
|
|
{
|
|
|
|
return serialiseVersion(rdv, data, pktsize);
|
|
|
|
}
|
2009-12-22 05:41:19 -05:00
|
|
|
else if (NULL != (rdt = dynamic_cast<RsDiscHeartbeat *>(i)))
|
|
|
|
{
|
|
|
|
return serialiseHeartbeat(rdt, 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;
|
2010-01-19 14:15:22 -05:00
|
|
|
case RS_PKT_SUBTYPE_DISC_ASK_INFO:
|
|
|
|
return deserialiseAskInfo(data, pktsize);
|
2009-05-25 07:38:47 -04:00
|
|
|
break;
|
2009-06-28 16:57:02 -04:00
|
|
|
case RS_PKT_SUBTYPE_DISC_VERSION:
|
|
|
|
return deserialiseVersion(data, pktsize);
|
|
|
|
break;
|
2009-12-22 05:41:19 -05:00
|
|
|
case RS_PKT_SUBTYPE_DISC_HEARTBEAT:
|
|
|
|
return deserialiseHeartbeat(data, pktsize);
|
|
|
|
break;
|
|
|
|
default:
|
2007-12-11 19:54:42 -05:00
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2010-01-19 14:15:22 -05:00
|
|
|
RsDiscAskInfo::~RsDiscAskInfo()
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-19 14:15:22 -05:00
|
|
|
void RsDiscAskInfo::clear()
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
2010-01-19 14:15:22 -05:00
|
|
|
gpg_id.clear();
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
|
|
|
|
2010-01-19 14:15:22 -05:00
|
|
|
std::ostream &RsDiscAskInfo::print(std::ostream &out, uint16_t indent)
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
2010-01-19 14:15:22 -05:00
|
|
|
printRsItemBase(out, "RsDiscAskInfo", indent);
|
2007-12-11 19:54:42 -05:00
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
2010-01-19 14:15:22 -05:00
|
|
|
out << "gpg_id: " << gpg_id << std::endl;
|
2009-12-18 12:51:23 -05:00
|
|
|
|
2010-01-19 14:17:21 -05:00
|
|
|
printRsItemEnd(out, "RsDiscAskInfo", indent);
|
2007-12-11 19:54:42 -05:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-19 14:15:22 -05:00
|
|
|
uint32_t RsDiscSerialiser::sizeAskInfo(RsDiscAskInfo *item)
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
|
|
|
uint32_t s = 8; /* header */
|
2010-01-19 14:15:22 -05:00
|
|
|
s += GetTlvStringSize(item->gpg_id);
|
2007-12-11 19:54:42 -05:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
2010-01-19 14:15:22 -05:00
|
|
|
bool RsDiscSerialiser::serialiseAskInfo(RsDiscAskInfo *item, void *data, uint32_t *pktsize)
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
2010-01-19 14:15:22 -05:00
|
|
|
uint32_t tlvsize = sizeAskInfo(item);
|
2007-12-11 19:54:42 -05:00
|
|
|
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
|
2010-01-19 14:15:22 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseAskInfo() Header: " << ok << std::endl;
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseAskInfo() Size: " << tlvsize << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
|
2009-12-18 12:51:23 -05:00
|
|
|
/* skip the header */
|
2007-12-11 19:54:42 -05:00
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
2010-01-19 14:15:22 -05:00
|
|
|
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->gpg_id);
|
2007-12-11 19:54:42 -05:00
|
|
|
|
2010-01-19 14:15:22 -05:00
|
|
|
if (offset != tlvsize) {
|
2007-12-11 19:54:42 -05:00
|
|
|
ok = false;
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2010-01-19 14:15:22 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseAskInfo() Size Error! " << std::endl;
|
2008-03-31 10:06:59 -04:00
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2010-01-19 14:15:22 -05:00
|
|
|
RsDiscAskInfo *RsDiscSerialiser::deserialiseAskInfo(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)) ||
|
2010-01-19 14:15:22 -05:00
|
|
|
(RS_PKT_SUBTYPE_DISC_ASK_INFO != getRsItemSubType(rstype)))
|
2007-12-11 19:54:42 -05:00
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2010-01-19 14:15:22 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseAskInfo() 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 */
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2010-01-19 14:15:22 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseAskInfo() 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 */
|
2010-05-19 16:08:58 -04:00
|
|
|
RsDiscAskInfo *item = new RsDiscAskInfo();
|
2007-12-11 19:54:42 -05:00
|
|
|
item->clear();
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* get mandatory parts first */
|
2010-01-19 14:15:22 -05:00
|
|
|
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PEERID, item->gpg_id);
|
2009-11-11 11:49:11 -05:00
|
|
|
|
2010-01-19 14:15:22 -05:00
|
|
|
if (offset != rssize) {
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2010-01-19 14:15:22 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseAskInfo() 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;
|
|
|
|
}
|
|
|
|
|
2010-01-19 14:15:22 -05:00
|
|
|
if (!ok) {
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2010-01-19 14:15:22 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseAskInfo() 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()
|
|
|
|
{
|
2008-02-03 01:29:02 -05:00
|
|
|
aboutId.clear();
|
2010-01-13 16:22:52 -05:00
|
|
|
certGPG.clear();
|
2010-01-13 16:33:54 -05:00
|
|
|
rsPeerList.clear();
|
2007-12-11 19:54:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &RsDiscReply::print(std::ostream &out, uint16_t indent)
|
|
|
|
{
|
|
|
|
printRsItemBase(out, "RsDiscReply", indent);
|
|
|
|
uint16_t int_Indent = indent + 2;
|
|
|
|
|
2010-01-13 16:22:52 -05:00
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "AboutId: " << aboutId << std::endl;
|
|
|
|
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
out << "certGPG: " << certGPG << std::endl;
|
|
|
|
|
2009-12-18 12:51:23 -05:00
|
|
|
printIndent(out, int_Indent);
|
2010-01-13 16:33:54 -05:00
|
|
|
out << "RsDiscReply::print() RsPeerNetItem list : " << std::endl;
|
|
|
|
for (std::list<RsPeerNetItem>::iterator pitemIt = rsPeerList.begin(); pitemIt!=(rsPeerList.end()); pitemIt++) {
|
|
|
|
printIndent(out, int_Indent);
|
|
|
|
pitemIt->print(std::cerr, indent);
|
2009-12-18 12:51:23 -05:00
|
|
|
}
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
printRsItemEnd(out, "RsDiscReply", indent);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t RsDiscSerialiser::sizeReply(RsDiscReply *item)
|
|
|
|
{
|
|
|
|
uint32_t s = 8; /* header */
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::sizeReply() Header Size: " << s << std::endl;
|
|
|
|
#endif
|
2008-02-03 01:29:02 -05:00
|
|
|
s += GetTlvStringSize(item->aboutId);
|
2011-07-29 11:17:39 -04:00
|
|
|
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::sizeReply() +AboutId Size: " << s << std::endl;
|
|
|
|
#endif
|
2010-05-19 16:08:58 -04:00
|
|
|
s += GetTlvStringSize(item->certGPG);
|
2007-12-11 19:54:42 -05:00
|
|
|
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::sizeReply() +certGPG Size: " << s << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2010-05-19 16:08:58 -04:00
|
|
|
RsPeerConfigSerialiser rss ;
|
|
|
|
|
|
|
|
for (std::list<RsPeerNetItem>::iterator it = item->rsPeerList.begin(); it != item->rsPeerList.end(); it++)
|
2011-07-29 11:17:39 -04:00
|
|
|
{
|
2010-05-19 16:08:58 -04:00
|
|
|
s += rss.size(&(*it)) ;
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::sizeReply() +RsPeerNetItem Size: " << s << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::sizeReply() Total Size: " << s << std::endl;
|
|
|
|
#endif
|
2009-11-11 11:49:11 -05:00
|
|
|
|
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;
|
|
|
|
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() tlvsize: " << tlvsize;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
if (*pktsize < tlvsize)
|
2011-07-29 11:17:39 -04:00
|
|
|
{
|
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() ERROR not enough space" << std::endl;
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() ERROR *pktsize: " << *pktsize << " tlvsize: " << tlvsize;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
return false; /* not enough space */
|
2011-07-29 11:17:39 -04:00
|
|
|
}
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
*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;
|
2010-01-13 16:22:52 -05:00
|
|
|
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-02-22 12:36:39 -05:00
|
|
|
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->aboutId);
|
2010-05-19 16:08:58 -04:00
|
|
|
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_CERT_GPG, item->certGPG);
|
|
|
|
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() Offset After Strings: " << offset << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2010-05-19 16:08:58 -04:00
|
|
|
//store the ip list
|
|
|
|
RsPeerConfigSerialiser rss ;
|
|
|
|
std::list<RsPeerNetItem>::iterator pitemIt;
|
|
|
|
|
|
|
|
for (pitemIt = item->rsPeerList.begin(); pitemIt!=(item->rsPeerList.end()) && ok; ++pitemIt)
|
|
|
|
{
|
2010-06-10 09:42:49 -04:00
|
|
|
uint32_t size = *pktsize - offset ;//~(uint32_t)0; // we must be conservative otherwise the serialiser returns false !!
|
2010-06-07 18:23:28 -04:00
|
|
|
|
2010-05-19 16:08:58 -04:00
|
|
|
ok &= rss.serialise(&(*pitemIt), (void *) (((char *) data) + offset), &size);
|
2010-06-07 18:23:28 -04:00
|
|
|
|
|
|
|
// The size has been updated to its exact value.
|
2010-05-19 16:08:58 -04:00
|
|
|
offset += size;
|
2011-07-29 11:17:39 -04:00
|
|
|
|
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() RsPeerNetItem ok?: " << ok << std::endl;
|
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() Offset After RsPeerNetItem: " << offset << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2009-11-11 11:49:11 -05:00
|
|
|
}
|
|
|
|
|
2010-05-19 16:08:58 -04:00
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
2007-12-11 19:54:42 -05:00
|
|
|
ok = false;
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2010-06-07 18:23:28 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseReply() Size Error: " << tlvsize << " != " << offset << 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;
|
|
|
|
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseReply() Pkt Type: " << std::hex << rstype << std::dec;
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseReply() Pkt Size: " << rssize << std::endl;
|
|
|
|
#endif
|
2007-12-11 19:54:42 -05:00
|
|
|
|
|
|
|
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
|
|
|
|
(RS_SERVICE_TYPE_DISC != getRsItemService(rstype)) ||
|
|
|
|
(RS_PKT_SUBTYPE_DISC_REPLY != getRsItemSubType(rstype)))
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_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 */
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_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 */
|
2010-05-19 16:08:58 -04:00
|
|
|
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PEERID, item->aboutId);
|
|
|
|
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_CERT_GPG, item->certGPG);
|
|
|
|
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseReply() offset after Strings: " << offset << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2010-05-19 16:08:58 -04:00
|
|
|
//get the peernet address list
|
|
|
|
RsPeerConfigSerialiser rss ;
|
|
|
|
|
|
|
|
while (offset < rssize)
|
|
|
|
{
|
|
|
|
uint32_t peerNetSize = rssize - offset ;
|
|
|
|
|
|
|
|
RsPeerNetItem *rsPeerNetItem = (RsPeerNetItem*)rss.deserialise((void *) (((char *) data) + offset), &peerNetSize);
|
2011-07-29 11:17:39 -04:00
|
|
|
|
2010-05-19 16:08:58 -04:00
|
|
|
offset += peerNetSize;
|
|
|
|
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseReply() offset aft PeerNetItem: " << offset << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2010-05-19 16:08:58 -04:00
|
|
|
if(rsPeerNetItem == NULL)
|
2011-07-29 11:17:39 -04:00
|
|
|
{
|
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseReply() ERROR deserialise PeerNetItem Failed" << std::endl;
|
|
|
|
#endif
|
2010-05-19 16:08:58 -04:00
|
|
|
break ;
|
2011-07-29 11:17:39 -04:00
|
|
|
}
|
2010-05-19 16:08:58 -04:00
|
|
|
|
|
|
|
item->rsPeerList.push_back(*rsPeerNetItem);
|
|
|
|
delete rsPeerNetItem ;
|
2009-11-11 11:49:11 -05:00
|
|
|
}
|
2007-12-11 19:54:42 -05:00
|
|
|
|
2010-05-19 16:08:58 -04:00
|
|
|
if (offset != rssize)
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_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;
|
|
|
|
}
|
|
|
|
|
2010-05-19 16:08:58 -04:00
|
|
|
if (!ok)
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_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-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
|
2011-07-29 11:17:39 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseVersion() Header: " << ok << std::endl;
|
2009-06-28 16:57:02 -04:00
|
|
|
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;
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-06-28 16:57:02 -04:00
|
|
|
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)))
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-06-28 16:57:02 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseVersion() Wrong Type" << std::endl;
|
|
|
|
#endif
|
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-06-28 16:57:02 -04:00
|
|
|
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)
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-06-28 16:57:02 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseVersion() offset != rssize" << std::endl;
|
|
|
|
std::cerr << "Offset: " << offset << " Rssize: " << rssize << std::endl;
|
|
|
|
#endif
|
|
|
|
/* error */
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-06-28 16:57:02 -04:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseVersion() ok = false" << std::endl;
|
|
|
|
#endif
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-22 05:41:19 -05:00
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
RsDiscHeartbeat::~RsDiscHeartbeat()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
void RsDiscHeartbeat::clear()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &RsDiscHeartbeat::print(std::ostream &out, uint16_t indent)
|
|
|
|
{
|
|
|
|
printRsItemBase(out, "RsDiscHeartbeat", indent);
|
|
|
|
printRsItemEnd(out, "RsDiscHeartbeat", indent);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t RsDiscSerialiser::sizeHeartbeat(RsDiscHeartbeat *item)
|
|
|
|
{
|
|
|
|
uint32_t s = 8; /* header */
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serialise the data to the buffer */
|
|
|
|
bool RsDiscSerialiser::serialiseHeartbeat(RsDiscHeartbeat *item, void *data, uint32_t *pktsize)
|
|
|
|
{
|
|
|
|
uint32_t tlvsize = sizeHeartbeat(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
|
2010-02-07 18:01:46 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseHeartbeat() Header: " << ok << std::endl;
|
2009-12-22 05:41:19 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseHeartbeat() Size: " << tlvsize << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-12-22 05:41:19 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::serialiseHeartbeat() Size Error! " << std::endl;
|
|
|
|
std::cerr << "Offset: " << offset << " tlvsize: " << tlvsize << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsDiscHeartbeat *RsDiscSerialiser::deserialiseHeartbeat(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_HEARTBEAT != getRsItemSubType(rstype)))
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-12-22 05:41:19 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseHeartbeat() Wrong Type" << std::endl;
|
|
|
|
#endif
|
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pktsize < rssize) /* check size */
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-12-22 05:41:19 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseHeartbeat() 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 */
|
|
|
|
RsDiscHeartbeat *item = new RsDiscHeartbeat();
|
|
|
|
item->clear();
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
if (offset != rssize)
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-12-22 05:41:19 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseHeartbeat() offset != rssize" << std::endl;
|
|
|
|
std::cerr << "Offset: " << offset << " Rssize: " << rssize << std::endl;
|
|
|
|
#endif
|
|
|
|
/* error */
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
2011-07-29 11:17:39 -04:00
|
|
|
#ifdef RSSERIAL_ERROR_DEBUG
|
2009-12-22 05:41:19 -05:00
|
|
|
std::cerr << "RsDiscSerialiser::deserialiseHeartbeat() ok = false" << std::endl;
|
|
|
|
#endif
|
|
|
|
delete item;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-11 19:54:42 -05:00
|
|
|
/*************************************************************************/
|