2009-05-26 17:42:45 -04:00
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
#include <stdexcept>
|
|
|
|
#endif
|
|
|
|
#include <iostream>
|
|
|
|
#include "turtletypes.h"
|
|
|
|
#include "rsturtleitem.h"
|
2013-04-06 05:21:01 -04:00
|
|
|
#include "turtleclientservice.h"
|
2009-05-26 17:42:45 -04:00
|
|
|
|
2010-07-21 19:14:10 -04:00
|
|
|
//#define P3TURTLE_DEBUG
|
2009-05-26 17:42:45 -04:00
|
|
|
// -----------------------------------------------------------------------------------//
|
|
|
|
// -------------------------------- Serialization. --------------------------------- //
|
|
|
|
// -----------------------------------------------------------------------------------//
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// ---------------------------------- Packet sizes -----------------------------------//
|
|
|
|
//
|
2009-08-25 08:04:43 -04:00
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
uint32_t RsTurtleStringSearchRequestItem::serial_size() const
|
2009-05-26 17:42:45 -04:00
|
|
|
{
|
|
|
|
uint32_t s = 0 ;
|
|
|
|
|
|
|
|
s += 8 ; // header
|
|
|
|
s += 4 ; // request_id
|
|
|
|
s += 2 ; // depth
|
2009-08-25 08:04:43 -04:00
|
|
|
s += GetTlvStringSize(match_string) ; // match_string
|
|
|
|
|
|
|
|
return s ;
|
|
|
|
}
|
2016-10-29 12:18:02 -04:00
|
|
|
uint32_t RsTurtleRegExpSearchRequestItem::serial_size() const
|
2009-08-25 08:04:43 -04:00
|
|
|
{
|
|
|
|
uint32_t s = 0 ;
|
|
|
|
|
|
|
|
s += 8 ; // header
|
|
|
|
s += 4 ; // request_id
|
|
|
|
s += 2 ; // depth
|
|
|
|
|
|
|
|
s += 4 ; // number of strings
|
|
|
|
|
2009-09-05 07:59:42 -04:00
|
|
|
for(unsigned int i=0;i<expr._strings.size();++i)
|
2009-08-25 08:04:43 -04:00
|
|
|
s += GetTlvStringSize(expr._strings[i]) ;
|
|
|
|
|
|
|
|
s += 4 ; // number of ints
|
|
|
|
s += 4 * expr._ints.size() ;
|
|
|
|
s += 4 ; // number of tokens
|
|
|
|
s += expr._tokens.size() ; // uint8_t has size 1
|
2009-05-26 17:42:45 -04:00
|
|
|
|
|
|
|
return s ;
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
uint32_t RsTurtleSearchResultItem::serial_size()const
|
2009-05-26 17:42:45 -04:00
|
|
|
{
|
|
|
|
uint32_t s = 0 ;
|
|
|
|
|
|
|
|
s += 8 ; // header
|
|
|
|
s += 4 ; // search request id
|
|
|
|
s += 2 ; // depth
|
|
|
|
s += 4 ; // number of results
|
|
|
|
|
|
|
|
for(std::list<TurtleFileInfo>::const_iterator it(result.begin());it!=result.end();++it)
|
|
|
|
{
|
|
|
|
s += 8 ; // file size
|
2014-03-17 16:56:06 -04:00
|
|
|
s += it->hash.serial_size(); // file hash
|
2009-05-26 17:42:45 -04:00
|
|
|
s += GetTlvStringSize(it->name) ; // file name
|
|
|
|
}
|
|
|
|
|
|
|
|
return s ;
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
uint32_t RsTurtleOpenTunnelItem::serial_size()const
|
2009-05-26 17:42:45 -04:00
|
|
|
{
|
|
|
|
uint32_t s = 0 ;
|
|
|
|
|
|
|
|
s += 8 ; // header
|
2014-03-17 16:56:06 -04:00
|
|
|
s += file_hash.serial_size() ; // file hash
|
2009-05-26 17:42:45 -04:00
|
|
|
s += 4 ; // tunnel request id
|
|
|
|
s += 4 ; // partial tunnel id
|
|
|
|
s += 2 ; // depth
|
|
|
|
|
|
|
|
return s ;
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
uint32_t RsTurtleTunnelOkItem::serial_size() const
|
2009-05-26 17:42:45 -04:00
|
|
|
{
|
|
|
|
uint32_t s = 0 ;
|
|
|
|
|
|
|
|
s += 8 ; // header
|
|
|
|
s += 4 ; // tunnel id
|
|
|
|
s += 4 ; // tunnel request id
|
|
|
|
|
|
|
|
return s ;
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
uint32_t RsTurtleGenericDataItem::serial_size() const
|
2013-04-06 09:58:27 -04:00
|
|
|
{
|
|
|
|
uint32_t s = 0 ;
|
|
|
|
|
|
|
|
s += 8 ; // header
|
|
|
|
s += 4 ; // tunnel id
|
|
|
|
s += 4 ; // data size
|
|
|
|
s += data_size ; // data
|
|
|
|
|
|
|
|
return s ;
|
|
|
|
}
|
2009-05-26 17:42:45 -04:00
|
|
|
//
|
|
|
|
// ---------------------------------- Serialization ----------------------------------//
|
|
|
|
//
|
|
|
|
RsItem *RsTurtleSerialiser::deserialise(void *data, uint32_t *size)
|
|
|
|
{
|
|
|
|
// look what we have...
|
|
|
|
|
|
|
|
/* get the type */
|
|
|
|
uint32_t rstype = getRsItemId(data);
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << "p3turtle: deserialising packet: " << std::endl ;
|
|
|
|
#endif
|
|
|
|
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_TURTLE != getRsItemService(rstype)))
|
|
|
|
{
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << " Wrong type !!" << std::endl ;
|
|
|
|
#endif
|
|
|
|
return NULL; /* wrong type */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
switch(getRsItemSubType(rstype))
|
|
|
|
{
|
2009-08-25 08:04:43 -04:00
|
|
|
case RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST : return new RsTurtleStringSearchRequestItem(data,*size) ;
|
|
|
|
case RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST : return new RsTurtleRegExpSearchRequestItem(data,*size) ;
|
|
|
|
case RS_TURTLE_SUBTYPE_SEARCH_RESULT : return new RsTurtleSearchResultItem(data,*size) ;
|
|
|
|
case RS_TURTLE_SUBTYPE_OPEN_TUNNEL : return new RsTurtleOpenTunnelItem(data,*size) ;
|
|
|
|
case RS_TURTLE_SUBTYPE_TUNNEL_OK : return new RsTurtleTunnelOkItem(data,*size) ;
|
2013-04-06 09:58:27 -04:00
|
|
|
case RS_TURTLE_SUBTYPE_GENERIC_DATA : return new RsTurtleGenericDataItem(data,*size) ;
|
2013-04-06 05:21:01 -04:00
|
|
|
|
2009-05-26 17:42:45 -04:00
|
|
|
default:
|
2013-04-06 05:21:01 -04:00
|
|
|
break ;
|
2009-05-26 17:42:45 -04:00
|
|
|
}
|
2013-04-06 05:21:01 -04:00
|
|
|
// now try all client services
|
|
|
|
//
|
|
|
|
RsItem *item = NULL ;
|
|
|
|
|
|
|
|
for(uint32_t i=0;i<_client_services.size();++i)
|
|
|
|
if((item = _client_services[i]->deserialiseItem(data,*size)) != NULL)
|
|
|
|
return item ;
|
|
|
|
|
|
|
|
std::cerr << "Unknown packet type in RsTurtle (not even handled by client services)!" << std::endl ;
|
|
|
|
return NULL ;
|
2009-05-26 17:42:45 -04:00
|
|
|
#ifndef WINDOWS_SYS
|
|
|
|
}
|
|
|
|
catch(std::exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << "Exception raised: " << e.what() << std::endl ;
|
|
|
|
return NULL ;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
bool RsTurtleStringSearchRequestItem::serialize(void *data,uint32_t& pktsize) const
|
2009-05-26 17:42:45 -04:00
|
|
|
{
|
|
|
|
uint32_t tlvsize = serial_size();
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
|
|
|
|
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, match_string);
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, request_id);
|
|
|
|
ok &= setRawUInt16(data, tlvsize, &offset, depth);
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsFileConfigSerialiser::serialiseTransfer() Size Error! " << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
bool RsTurtleRegExpSearchRequestItem::serialize(void *data,uint32_t& pktsize) const
|
2009-08-25 08:04:43 -04:00
|
|
|
{
|
|
|
|
uint32_t tlvsize = serial_size();
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << "RsTurtleSerialiser::serialising RegExp search packet (size=" << tlvsize << ")" << std::endl;
|
|
|
|
#endif
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, request_id);
|
|
|
|
ok &= setRawUInt16(data, tlvsize, &offset, depth);
|
|
|
|
|
|
|
|
// now serialize the regexp
|
|
|
|
ok &= setRawUInt32(data,tlvsize,&offset,expr._tokens.size()) ;
|
|
|
|
|
2009-09-05 07:59:42 -04:00
|
|
|
for(unsigned int i=0;i<expr._tokens.size();++i) ok &= setRawUInt8(data,tlvsize,&offset,expr._tokens[i]) ;
|
2009-08-25 08:04:43 -04:00
|
|
|
|
|
|
|
ok &= setRawUInt32(data,tlvsize,&offset,expr._ints.size()) ;
|
|
|
|
|
2009-09-05 07:59:42 -04:00
|
|
|
for(unsigned int i=0;i<expr._ints.size();++i) ok &= setRawUInt32(data,tlvsize,&offset,expr._ints[i]) ;
|
2009-08-25 08:04:43 -04:00
|
|
|
|
|
|
|
ok &= setRawUInt32(data,tlvsize,&offset,expr._strings.size()) ;
|
|
|
|
|
2009-09-05 07:59:42 -04:00
|
|
|
for(unsigned int i=0;i<expr._strings.size();++i) ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, expr._strings[i]);
|
2009-08-25 08:04:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << "RsTurtleSerialiser::serialiseTransfer() Size Error! (offset=" << offset << ", tlvsize=" << tlvsize << ")" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-06 05:21:01 -04:00
|
|
|
return ok;
|
2010-07-21 19:14:10 -04:00
|
|
|
}
|
2013-04-06 05:21:01 -04:00
|
|
|
|
|
|
|
RsTurtleStringSearchRequestItem::RsTurtleStringSearchRequestItem(void *data,uint32_t pktsize)
|
|
|
|
: RsTurtleSearchRequestItem(RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST)
|
2012-03-15 15:55:43 -04:00
|
|
|
{
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
2013-04-06 05:21:01 -04:00
|
|
|
std::cerr << " deserializibg packet. type = search request (string)" << std::endl ;
|
2012-03-15 15:55:43 -04:00
|
|
|
#endif
|
|
|
|
uint32_t offset = 8; // skip the header
|
2013-04-06 05:21:01 -04:00
|
|
|
uint32_t rssize = getRsItemSize(data);
|
2012-03-15 15:55:43 -04:00
|
|
|
bool ok = true ;
|
2013-04-06 05:21:01 -04:00
|
|
|
|
|
|
|
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, match_string); // file hash
|
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &request_id);
|
|
|
|
ok &= getRawUInt16(data, pktsize, &offset, &depth);
|
2012-03-15 15:55:43 -04:00
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
2013-04-06 05:21:01 -04:00
|
|
|
UNREFERENCED_LOCAL_VARIABLE(rssize);
|
2012-03-15 15:55:43 -04:00
|
|
|
#else
|
2013-04-06 05:21:01 -04:00
|
|
|
if (offset != rssize)
|
2012-03-15 15:55:43 -04:00
|
|
|
throw std::runtime_error("Size error while deserializing.") ;
|
|
|
|
if (!ok)
|
|
|
|
throw std::runtime_error("Unknown error while deserializing.") ;
|
|
|
|
#endif
|
|
|
|
}
|
2013-04-06 05:21:01 -04:00
|
|
|
|
|
|
|
RsTurtleRegExpSearchRequestItem::RsTurtleRegExpSearchRequestItem(void *data,uint32_t pktsize)
|
|
|
|
: RsTurtleSearchRequestItem(RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST)
|
2010-07-21 19:14:10 -04:00
|
|
|
{
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
2013-04-06 05:21:01 -04:00
|
|
|
std::cerr << " deserializibg packet. type = search request (regexp)" << std::endl ;
|
2010-07-21 19:14:10 -04:00
|
|
|
#endif
|
|
|
|
uint32_t offset = 8; // skip the header
|
2013-04-06 05:21:01 -04:00
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
bool ok = true ;
|
2010-07-21 19:14:10 -04:00
|
|
|
|
2013-04-06 05:21:01 -04:00
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &request_id);
|
|
|
|
ok &= getRawUInt16(data, pktsize, &offset, &depth);
|
2010-07-21 19:14:10 -04:00
|
|
|
|
2013-04-06 05:21:01 -04:00
|
|
|
// now serialize the regexp
|
|
|
|
uint32_t n =0 ;
|
|
|
|
ok &= getRawUInt32(data,pktsize,&offset,&n) ;
|
|
|
|
|
2016-10-22 17:48:19 -04:00
|
|
|
if(ok) expr._tokens.resize(n) ;
|
2013-04-06 05:21:01 -04:00
|
|
|
|
2016-01-11 20:40:57 -05:00
|
|
|
for(uint32_t i=0;i<n && ok;++i) ok &= getRawUInt8(data,pktsize,&offset,&expr._tokens[i]) ;
|
2013-04-06 05:21:01 -04:00
|
|
|
|
|
|
|
ok &= getRawUInt32(data,pktsize,&offset,&n) ;
|
2016-01-13 12:22:55 -05:00
|
|
|
|
2016-10-22 17:48:19 -04:00
|
|
|
if(ok) expr._ints.resize(n) ;
|
2013-04-06 05:21:01 -04:00
|
|
|
|
2016-01-11 20:40:57 -05:00
|
|
|
for(uint32_t i=0;i<n && ok;++i) ok &= getRawUInt32(data,pktsize,&offset,&expr._ints[i]) ;
|
2013-04-06 05:21:01 -04:00
|
|
|
|
2016-10-22 17:48:19 -04:00
|
|
|
ok &= getRawUInt32(data,pktsize,&offset,&n);
|
2016-01-13 12:22:55 -05:00
|
|
|
|
2016-10-22 17:48:19 -04:00
|
|
|
if (ok) expr._strings.resize(n);
|
2013-04-06 05:21:01 -04:00
|
|
|
|
2016-01-11 20:40:57 -05:00
|
|
|
for(uint32_t i=0;i<n && ok;++i) ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, expr._strings[i]);
|
2010-07-21 19:14:10 -04:00
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
2013-04-06 05:21:01 -04:00
|
|
|
UNREFERENCED_LOCAL_VARIABLE(rssize);
|
2010-07-21 19:14:10 -04:00
|
|
|
#else
|
2013-04-06 05:21:01 -04:00
|
|
|
if (offset != rssize)
|
2010-07-21 19:14:10 -04:00
|
|
|
throw std::runtime_error("Size error while deserializing.") ;
|
|
|
|
if (!ok)
|
|
|
|
throw std::runtime_error("Unknown error while deserializing.") ;
|
|
|
|
#endif
|
|
|
|
}
|
2013-04-06 05:21:01 -04:00
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
bool RsTurtleSearchResultItem::serialize(void *data,uint32_t& pktsize) const
|
2012-03-15 15:55:43 -04:00
|
|
|
{
|
2013-04-06 05:21:01 -04:00
|
|
|
uint32_t tlvsize = serial_size();
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
2012-03-15 15:55:43 -04:00
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
|
2013-04-06 05:21:01 -04:00
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, request_id);
|
|
|
|
ok &= setRawUInt16(data, tlvsize, &offset, depth);
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, result.size());
|
2010-07-21 19:14:10 -04:00
|
|
|
|
2013-04-06 05:21:01 -04:00
|
|
|
for(std::list<TurtleFileInfo>::const_iterator it(result.begin());it!=result.end();++it)
|
|
|
|
{
|
2014-03-17 16:56:06 -04:00
|
|
|
ok &= setRawUInt64(data, tlvsize, &offset, it->size); // file size
|
|
|
|
ok &= it->hash.serialise(data, tlvsize, offset); // file hash
|
2013-04-06 05:21:01 -04:00
|
|
|
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_NAME, it->name); // file name
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsFileConfigSerialiser::serialiseTransfer() Size Error! " << std::endl;
|
2012-03-15 15:55:43 -04:00
|
|
|
#endif
|
2013-04-06 05:21:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
2012-03-15 15:55:43 -04:00
|
|
|
}
|
2013-04-06 05:21:01 -04:00
|
|
|
|
2009-05-26 17:42:45 -04:00
|
|
|
RsTurtleSearchResultItem::RsTurtleSearchResultItem(void *data,uint32_t pktsize)
|
|
|
|
: RsTurtleItem(RS_TURTLE_SUBTYPE_SEARCH_RESULT)
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
setPriorityLevel(QOS_PRIORITY_RS_TURTLE_SEARCH_RESULT) ;
|
2009-05-26 17:42:45 -04:00
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << " type = search result" << std::endl ;
|
|
|
|
#endif
|
|
|
|
uint32_t offset = 8; // skip the header
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
|
|
|
|
bool ok = true ;
|
|
|
|
uint32_t s ;
|
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &request_id);
|
|
|
|
ok &= getRawUInt16(data, pktsize, &offset, &depth);
|
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &s) ;
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << " request_id=" << request_id << ", depth=" << depth << ", s=" << s << std::endl ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
result.clear() ;
|
|
|
|
|
|
|
|
for(int i=0;i<(int)s;++i)
|
|
|
|
{
|
|
|
|
TurtleFileInfo f ;
|
|
|
|
|
|
|
|
ok &= getRawUInt64(data, pktsize, &offset, &(f.size)); // file size
|
2014-03-17 16:56:06 -04:00
|
|
|
ok &= f.hash.deserialise(data, pktsize, offset); // file hash
|
2009-05-26 17:42:45 -04:00
|
|
|
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_NAME, f.name); // file name
|
|
|
|
|
|
|
|
result.push_back(f) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
2010-06-09 15:51:46 -04:00
|
|
|
UNREFERENCED_LOCAL_VARIABLE(rssize);
|
2009-05-26 17:42:45 -04:00
|
|
|
#else
|
|
|
|
if (offset != rssize)
|
|
|
|
throw std::runtime_error("Size error while deserializing.") ;
|
|
|
|
if (!ok)
|
|
|
|
throw std::runtime_error("Unknown error while deserializing.") ;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
bool RsTurtleOpenTunnelItem::serialize(void *data,uint32_t& pktsize) const
|
2009-05-26 17:42:45 -04:00
|
|
|
{
|
|
|
|
uint32_t tlvsize = serial_size();
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
ok &= file_hash.serialise(data, tlvsize, offset); // file hash
|
2009-05-26 17:42:45 -04:00
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, request_id);
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, partial_tunnel_id);
|
|
|
|
ok &= setRawUInt16(data, tlvsize, &offset, depth);
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsTurtleOpenTunnelItem::serialiseTransfer() Size Error! " << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsTurtleOpenTunnelItem::RsTurtleOpenTunnelItem(void *data,uint32_t pktsize)
|
|
|
|
: RsTurtleItem(RS_TURTLE_SUBTYPE_OPEN_TUNNEL)
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
setPriorityLevel(QOS_PRIORITY_RS_TURTLE_OPEN_TUNNEL) ;
|
2009-05-26 17:42:45 -04:00
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << " type = open tunnel" << std::endl ;
|
|
|
|
#endif
|
|
|
|
uint32_t offset = 8; // skip the header
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
|
|
|
|
bool ok = true ;
|
2014-03-17 16:56:06 -04:00
|
|
|
ok &= file_hash.deserialise(data, pktsize, offset); // file hash
|
2009-05-26 17:42:45 -04:00
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &request_id);
|
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &partial_tunnel_id) ;
|
|
|
|
ok &= getRawUInt16(data, pktsize, &offset, &depth);
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << " request_id=" << (void*)request_id << ", partial_id=" << (void*)partial_tunnel_id << ", depth=" << depth << ", hash=" << file_hash << std::endl ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
2010-06-09 15:51:46 -04:00
|
|
|
UNREFERENCED_LOCAL_VARIABLE(rssize);
|
2009-05-26 17:42:45 -04:00
|
|
|
#else
|
|
|
|
if (offset != rssize)
|
|
|
|
throw std::runtime_error("RsTurtleOpenTunnelItem::() error while deserializing.") ;
|
|
|
|
if (!ok)
|
|
|
|
throw std::runtime_error("RsTurtleOpenTunnelItem::() unknown error while deserializing.") ;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
bool RsTurtleTunnelOkItem::serialize(void *data,uint32_t& pktsize) const
|
2009-05-26 17:42:45 -04:00
|
|
|
{
|
|
|
|
uint32_t tlvsize = serial_size();
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, tunnel_id);
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, request_id);
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsTurtleTunnelOkItem::serialiseTransfer() Size Error! " << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsTurtleTunnelOkItem::RsTurtleTunnelOkItem(void *data,uint32_t pktsize)
|
|
|
|
: RsTurtleItem(RS_TURTLE_SUBTYPE_TUNNEL_OK)
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
setPriorityLevel(QOS_PRIORITY_RS_TURTLE_TUNNEL_OK) ;
|
2009-05-26 17:42:45 -04:00
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << " type = tunnel ok" << std::endl ;
|
|
|
|
#endif
|
|
|
|
uint32_t offset = 8; // skip the header
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
|
|
|
|
bool ok = true ;
|
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &tunnel_id) ;
|
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &request_id);
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << " request_id=" << (void*)request_id << ", tunnel_id=" << (void*)tunnel_id << std::endl ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
2010-06-09 15:51:46 -04:00
|
|
|
UNREFERENCED_LOCAL_VARIABLE(rssize);
|
2009-05-26 17:42:45 -04:00
|
|
|
#else
|
|
|
|
if (offset != rssize)
|
|
|
|
throw std::runtime_error("RsTurtleTunnelOkItem::() error while deserializing.") ;
|
|
|
|
if (!ok)
|
|
|
|
throw std::runtime_error("RsTurtleTunnelOkItem::() unknown error while deserializing.") ;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-06 09:58:27 -04:00
|
|
|
RsTurtleGenericDataItem::RsTurtleGenericDataItem(void *data,uint32_t pktsize)
|
|
|
|
: RsTurtleGenericTunnelItem(RS_TURTLE_SUBTYPE_GENERIC_DATA)
|
|
|
|
{
|
|
|
|
setPriorityLevel(QOS_PRIORITY_RS_TURTLE_GENERIC_DATA) ;
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << " type = tunnel ok" << std::endl ;
|
|
|
|
#endif
|
|
|
|
uint32_t offset = 8; // skip the header
|
|
|
|
uint32_t rssize = getRsItemSize(data);
|
|
|
|
|
2016-01-11 20:40:57 -05:00
|
|
|
if(rssize > pktsize)
|
|
|
|
throw std::runtime_error("RsTurtleTunnelOkItem::() wrong rssize (exceeds pktsize).") ;
|
|
|
|
|
2013-04-06 09:58:27 -04:00
|
|
|
/* add mandatory parts first */
|
|
|
|
|
|
|
|
bool ok = true ;
|
|
|
|
|
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &tunnel_id) ;
|
|
|
|
ok &= getRawUInt32(data, pktsize, &offset, &data_size);
|
|
|
|
#ifdef P3TURTLE_DEBUG
|
|
|
|
std::cerr << " request_id=" << (void*)request_id << ", tunnel_id=" << (void*)tunnel_id << std::endl ;
|
|
|
|
#endif
|
2016-01-11 20:40:57 -05:00
|
|
|
|
|
|
|
if(data_size > rssize || rssize - data_size < offset)
|
|
|
|
throw std::runtime_error("RsTurtleTunnelOkItem::() wrong data_size (exceeds rssize).") ;
|
|
|
|
|
2016-01-12 21:43:04 -05:00
|
|
|
data_bytes = rs_malloc(data_size) ;
|
2013-04-06 09:58:27 -04:00
|
|
|
|
|
|
|
if(data_bytes != NULL)
|
|
|
|
{
|
2013-08-21 17:36:33 -04:00
|
|
|
memcpy(data_bytes,(void *)((uint8_t *)data+offset),data_size) ;
|
2013-04-06 09:58:27 -04:00
|
|
|
offset += data_size ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
offset = 0 ; // generate an error
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
|
|
|
UNREFERENCED_LOCAL_VARIABLE(rssize);
|
|
|
|
#else
|
|
|
|
if (offset != rssize)
|
|
|
|
throw std::runtime_error("RsTurtleTunnelOkItem::() error while deserializing.") ;
|
|
|
|
if (!ok)
|
|
|
|
throw std::runtime_error("RsTurtleTunnelOkItem::() unknown error while deserializing.") ;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-29 12:18:02 -04:00
|
|
|
bool RsTurtleGenericDataItem::serialize(void *data,uint32_t& pktsize) const
|
2013-04-06 09:58:27 -04:00
|
|
|
{
|
|
|
|
uint32_t tlvsize = serial_size();
|
|
|
|
uint32_t offset = 0;
|
|
|
|
|
|
|
|
if (pktsize < tlvsize)
|
|
|
|
return false; /* not enough space */
|
|
|
|
|
|
|
|
pktsize = tlvsize;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
|
|
|
|
|
|
|
|
/* skip the header */
|
|
|
|
offset += 8;
|
|
|
|
|
|
|
|
/* add mandatory parts first */
|
|
|
|
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, tunnel_id);
|
|
|
|
ok &= setRawUInt32(data, tlvsize, &offset, data_size);
|
|
|
|
|
2013-08-21 17:36:33 -04:00
|
|
|
memcpy((void *)((uint8_t *)data+offset),data_bytes,data_size) ;
|
2013-04-06 09:58:27 -04:00
|
|
|
offset += data_size ;
|
|
|
|
|
|
|
|
if (offset != tlvsize)
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
#ifdef RSSERIAL_DEBUG
|
|
|
|
std::cerr << "RsTurtleTunnelOkItem::serialiseTransfer() Size Error! " << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
2009-05-26 17:42:45 -04:00
|
|
|
// -----------------------------------------------------------------------------------//
|
|
|
|
// ------------------------------------- IO --------------------------------------- //
|
|
|
|
// -----------------------------------------------------------------------------------//
|
|
|
|
//
|
2009-08-25 08:04:43 -04:00
|
|
|
std::ostream& RsTurtleStringSearchRequestItem::print(std::ostream& o, uint16_t)
|
2009-05-26 17:42:45 -04:00
|
|
|
{
|
2009-08-25 08:04:43 -04:00
|
|
|
o << "Search request (string):" << std::endl ;
|
2009-05-26 17:42:45 -04:00
|
|
|
o << " direct origin: \"" << PeerId() << "\"" << std::endl ;
|
|
|
|
o << " match string: \"" << match_string << "\"" << std::endl ;
|
2012-12-04 16:28:35 -05:00
|
|
|
o << " Req. Id: " << std::hex << request_id << std::dec << std::endl ;
|
2009-05-26 17:42:45 -04:00
|
|
|
o << " Depth : " << depth << std::endl ;
|
|
|
|
|
|
|
|
return o ;
|
|
|
|
}
|
2009-08-25 08:04:43 -04:00
|
|
|
std::ostream& RsTurtleRegExpSearchRequestItem::print(std::ostream& o, uint16_t)
|
|
|
|
{
|
|
|
|
o << "Search request (regexp):" << std::endl ;
|
|
|
|
o << " direct origin: \"" << PeerId() << "\"" << std::endl ;
|
2012-12-04 16:28:35 -05:00
|
|
|
o << " Req. Id: " << std::hex << request_id << std::dec << std::endl ;
|
2009-08-25 08:04:43 -04:00
|
|
|
o << " Depth : " << depth << std::endl ;
|
|
|
|
o << " RegExp: " << std::endl ;
|
2009-09-05 07:59:42 -04:00
|
|
|
o << " Toks: " ; for(unsigned int i=0;i<expr._tokens.size();++i) std::cout << (int)expr._tokens[i] << " " ; std::cout << std::endl ;
|
|
|
|
o << " Ints: " ; for(unsigned int i=0;i<expr._ints.size();++i) std::cout << (int)expr._ints[i] << " " ; std::cout << std::endl ;
|
|
|
|
o << " Strs: " ; for(unsigned int i=0;i<expr._strings.size();++i) std::cout << expr._strings[i] << " " ; std::cout << std::endl ;
|
2009-08-25 08:04:43 -04:00
|
|
|
|
|
|
|
return o ;
|
|
|
|
}
|
2009-05-26 17:42:45 -04:00
|
|
|
|
|
|
|
std::ostream& RsTurtleSearchResultItem::print(std::ostream& o, uint16_t)
|
|
|
|
{
|
|
|
|
o << "Search result:" << std::endl ;
|
|
|
|
|
|
|
|
o << " Peer id: " << PeerId() << std::endl ;
|
|
|
|
o << " Depth : " << depth << std::endl ;
|
2012-12-04 16:28:35 -05:00
|
|
|
o << " Req. Id: " << std::hex << request_id << std::dec << std::endl ;
|
2009-05-26 17:42:45 -04:00
|
|
|
o << " Files:" << std::endl ;
|
|
|
|
|
|
|
|
for(std::list<TurtleFileInfo>::const_iterator it(result.begin());it!=result.end();++it)
|
|
|
|
o << " " << it->hash << " " << it->size << " " << it->name << std::endl ;
|
|
|
|
|
|
|
|
return o ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& RsTurtleOpenTunnelItem::print(std::ostream& o, uint16_t)
|
|
|
|
{
|
|
|
|
o << "Open Tunnel:" << std::endl ;
|
|
|
|
|
|
|
|
o << " Peer id : " << PeerId() << std::endl ;
|
2012-12-04 16:28:35 -05:00
|
|
|
o << " Partial tId: " << std::hex << partial_tunnel_id << std::dec << std::endl ;
|
|
|
|
o << " Req. Id : " << std::hex << request_id << std::dec << std::endl ;
|
2009-05-26 17:42:45 -04:00
|
|
|
o << " Depth : " << depth << std::endl ;
|
|
|
|
o << " Hash : " << file_hash << std::endl ;
|
|
|
|
|
|
|
|
return o ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& RsTurtleTunnelOkItem::print(std::ostream& o, uint16_t)
|
|
|
|
{
|
|
|
|
o << "Tunnel Ok:" << std::endl ;
|
|
|
|
|
|
|
|
o << " Peer id : " << PeerId() << std::endl ;
|
2012-12-04 16:28:35 -05:00
|
|
|
o << " tunnel id : " << std::hex << tunnel_id << std::dec << std::endl ;
|
|
|
|
o << " Req. Id : " << std::hex << request_id << std::dec << std::endl ;
|
2009-05-26 17:42:45 -04:00
|
|
|
|
|
|
|
return o ;
|
|
|
|
}
|
|
|
|
|
2013-04-06 09:58:27 -04:00
|
|
|
std::ostream& RsTurtleGenericDataItem::print(std::ostream& o, uint16_t)
|
|
|
|
{
|
|
|
|
o << "Generic Data item:" << std::endl ;
|
|
|
|
|
|
|
|
o << " Peer id : " << PeerId() << std::endl ;
|
|
|
|
o << " data size : " << data_size << std::endl ;
|
|
|
|
o << " data bytes: " << std::hex << (void*)data_bytes << std::dec << std::endl ;
|
|
|
|
|
|
|
|
return o ;
|
|
|
|
}
|