Add the tunneling connection. Warning, encryption is not implemented yet for tunnel connection, use only for testing.

Redesign the udp connection
Remove the rsiface duplicates from retroshare-gui.
Add some randomness in timings for connect manager

Merge branch 'connectionTunneling'

Conflicts:
	libretroshare/src/libretroshare.pro
	libretroshare/src/pqi/p3connmgr.cc
	retroshare-gui/src/RetroShare.pro
	retroshare-gui/src/rsiface/rsfiles.h
	retroshare-gui/src/rsiface/rstypes.h

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1867 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
joss17 2009-12-13 21:59:26 +00:00
parent 1d567a7faa
commit fdb3673ce2
56 changed files with 2420 additions and 3394 deletions

View file

@ -682,14 +682,21 @@ uint32_t RsServiceSerialiser::size(RsItem *i)
/* serialise the data to the buffer */
bool RsServiceSerialiser::serialise(RsItem *i, void *data, uint32_t *pktsize)
{
RsRawItem *item = dynamic_cast<RsRawItem *>(i);
RsRawItem *item = dynamic_cast<RsRawItem *>(i);
if (!item)
{
return false;
}
#ifdef RSSERIAL_DEBUG
std::cerr << "RsServiceSerialiser::serialise() serializing raw item. pktsize : " << *pktsize;
#endif
uint32_t tlvsize = item->getRawLength();
uint32_t offset = 0;
#ifdef RSSERIAL_DEBUG
std::cerr << "tlvsize : " << tlvsize << std::endl;
#endif
uint32_t offset = 0;
if (*pktsize < tlvsize)
return false; /* not enough space */

View file

@ -164,7 +164,7 @@ class RsRawItem: public RsItem
{
public:
RsRawItem(uint32_t t, uint32_t size)
:RsItem(t), len(size) { data = malloc(len); }
:RsItem(t), len(size) { data = malloc(len);}
virtual ~RsRawItem()
{

View file

@ -46,6 +46,7 @@ const uint16_t RS_SERVICE_TYPE_DISC = 0x0011;
const uint16_t RS_SERVICE_TYPE_CHAT = 0x0012;
const uint16_t RS_SERVICE_TYPE_MSG = 0x0013;
const uint16_t RS_SERVICE_TYPE_TURTLE = 0x0014;
const uint16_t RS_SERVICE_TYPE_TUNNEL = 0x0015;
/* Combined Cache/Service ids */

View file

@ -0,0 +1,179 @@
#ifndef WINDOWS_SYS
#include <stdexcept>
#endif
#include <iostream>
#include "rstunnelitems.h"
// -----------------------------------------------------------------------------------//
// -------------------------------- Serialization. --------------------------------- //
// -----------------------------------------------------------------------------------//
//
//
// ---------------------------------- Packet sizes -----------------------------------//
//
uint32_t RsTunnelDataItem::serial_size()
{
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::serial_size() called." << std::endl ;
#endif
uint32_t s = 0 ;
s += 8 ; // header
s += GetTlvStringSize(sourcePeerId) ;
s += GetTlvStringSize(relayPeerId) ;
s += GetTlvStringSize(destPeerId) ;
s += 4 ; //connection_accept
s += 4 ; //encoded_data_len
s += encoded_data_len;
return s ;
}
//
// ---------------------------------- Serialization ----------------------------------//
//
RsItem *RsTunnelSerialiser::deserialise(void *data, uint32_t *size)
{
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::deserialise() called." << std::endl ;
#endif
// look what we have...
/* get the type */
uint32_t rstype = getRsItemId(data);
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel: deserialising packet: " << std::endl ;
#endif
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_TUNNEL != getRsItemService(rstype)))
{
#ifdef P3TUNNEL_DEBUG
std::cerr << " Wrong type !!" << std::endl ;
#endif
return NULL; /* wrong type */
}
#ifndef WINDOWS_SYS
try
{
#endif
switch(getRsItemSubType(rstype))
{
case RS_TUNNEL_SUBTYPE_DATA : return new RsTunnelDataItem(data,*size) ;
default:
std::cerr << "Unknown packet type in Rstunnel!" << std::endl ;
return NULL ;
}
#ifndef WINDOWS_SYS
}
catch(std::exception& e)
{
std::cerr << "Exception raised: " << e.what() << std::endl ;
return NULL ;
}
#endif
}
bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
{
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::serialize() called." << std::endl ;
#endif
uint32_t tlvsize = serial_size();
uint32_t offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
bool ok = true;
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::serialize() tlvsize : " << tlvsize << std::endl ;
#endif
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, sourcePeerId);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, relayPeerId);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, destPeerId);
ok &= setRawUInt32(data, tlvsize, &offset, connection_accepted);
ok &= setRawUInt32(data, tlvsize, &offset, encoded_data_len) ;
memcpy((void*)((unsigned char*)data+offset),encoded_data,encoded_data_len) ;
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::serialise() (offset + encoded_data_len) " << (offset + encoded_data_len) << std::endl;
std::cerr << "RsTunnelDataItem::serialise() tlvsize " << tlvsize << std::endl;
#endif
if ((offset + encoded_data_len) != tlvsize )
{
ok = false;
#ifdef RSSERIAL_DEBUG
std::cerr << "RsTunnelDataItem::serialiseTransfer() Size Error! " << std::endl;
#endif
}
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::serialize() packet size inside serialised data : " << getRsItemSize(data) << std::endl ;
#endif
return ok;
}
//deserialize in constructor
RsTunnelDataItem::RsTunnelDataItem(void *data,uint32_t pktsize)
: RsTunnelItem(RS_TUNNEL_SUBTYPE_DATA)
{
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem constructor called : deserializing packet." << std::endl ;
#endif
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(data);
bool ok = true ;
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem constructor rssize : " << rssize << std::endl ;
#endif
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, sourcePeerId);
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, relayPeerId);
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, destPeerId);
ok &= getRawUInt32(data, pktsize, &offset, &connection_accepted);
ok &= getRawUInt32(data, pktsize, &offset, &encoded_data_len) ;
encoded_data = (void*)malloc(encoded_data_len) ;
memcpy(encoded_data, (void*)((unsigned char*)data+offset), encoded_data_len);
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
#else
if ((offset + encoded_data_len) != rssize)
throw std::runtime_error("Size error while deserializing.") ;
if (!ok)
throw std::runtime_error("Unknown error while deserializing.") ;
#endif
}
std::ostream& RsTunnelDataItem::print(std::ostream& o, uint16_t)
{
o << "RsTunnelDataItem :" << std::endl ;
o << " sourcePeerId : " << sourcePeerId << std::endl ;
o << " relayPeerId : " << relayPeerId << std::endl ;
o << " destPeerId : " << destPeerId << std::endl ;
o << " connection_accepted : " << connection_accepted << std::endl ;
o << " encoded_data_len : " << encoded_data_len << std::endl ;
if (encoded_data_len != 0 ) {
o << "getRsItemSize(encoded_data)" << getRsItemSize(encoded_data) << std::endl ;
}
return o ;
}

View file

@ -0,0 +1,105 @@
#ifndef RS_TUNNEL_ITEMS_H
#define RS_TUNNEL_ITEMS_H
/*
* libretroshare/src/serialiser: rschannelitems.h
*
* 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 <map>
#include "serialiser/rstlvbase.h"
#include "serialiser/rsbaseserial.h"
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvtypes.h"
#include "serialiser/rstlvkeys.h"
const uint8_t RS_TUNNEL_SUBTYPE_DATA = 0x01 ;
/***********************************************************************************/
/* Basic Tunnel Item Class */
/***********************************************************************************/
class RsTunnelItem: public RsItem
{
public:
RsTunnelItem(uint8_t tunnel_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_TUNNEL,tunnel_subtype) {}
virtual bool serialize(void *data,uint32_t& size) = 0 ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() { return 0;}
virtual void clear() {}
};
class RsTunnelDataItem: public RsTunnelItem
{
public:
RsTunnelDataItem() : RsTunnelItem(RS_TUNNEL_SUBTYPE_DATA) {}
RsTunnelDataItem(void *data,uint32_t size) ; // deserialization
uint32_t encoded_data_len;
void *encoded_data;
std::string sourcePeerId ;
std::string relayPeerId ;
std::string destPeerId ;
uint32_t connection_accepted;
std::ostream& print(std::ostream& o, uint16_t) ;
bool serialize(void *data,uint32_t& size) ;
uint32_t serial_size() ;
};
class RsTunnelSerialiser: public RsSerialType
{
public:
RsTunnelSerialiser() : RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_TUNNEL) {}
virtual uint32_t size(RsItem *item)
{
RsTunnelItem * rst;
if (NULL != (rst = dynamic_cast<RsTunnelItem *>(item)))
{
return rst->serial_size() ;
} else {
std::cerr << "RsTunnelSerialiser::size() problem, not a RsTunnelItem." << std::endl;
}
return 0;
}
virtual bool serialise(RsItem *item, void *data, uint32_t *size)
{
RsTunnelItem * rst;
if (NULL != (rst = dynamic_cast<RsTunnelItem *>(item)))
{
return rst->serialize(data,*size) ;
} else {
std::cerr << "RsTunnelSerialiser::serialise() problem, not a RsTunnelItem." << std::endl;
}
return false;
}
virtual RsItem *deserialise (void *data, uint32_t *size) ;
};
#endif /* RS_TUNNEL_ITEMS_H */