reimplemented the tunnel handshake

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@2428 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
joss17 2010-02-25 22:44:04 +00:00
parent 47b5f62aeb
commit 3d33f5d729
7 changed files with 373 additions and 192 deletions

View File

@ -436,6 +436,10 @@ pqiconnect *pqiperson::getKid(uint32_t type)
{ {
std::map<uint32_t, pqiconnect *>::iterator it; std::map<uint32_t, pqiconnect *>::iterator it;
if (kids.empty()) {
return NULL;
}
it = kids.find(type); it = kids.find(type);
if (it == kids.end()) if (it == kids.end())
{ {

View File

@ -45,8 +45,8 @@ const int pqisslzone = 37714;
#define TUNNEL_WAITING_NOT 0 #define TUNNEL_WAITING_NOT 0
#define TUNNEL_WAITING_DELAY 1 #define TUNNEL_WAITING_DELAY 1
#define TUNNEL_WAITING_SPAM_PING 2 #define TUNNEL_WAITING_SPAM_HANDSHAKE 2
#define TUNNEL_WAITING_PING_RETURN 3 #define TUNNEL_WAITING_RETURN_HANDSHAKE 3
#define TUNNEL_PASSIVE 0x00 #define TUNNEL_PASSIVE 0x00
@ -301,7 +301,7 @@ int pqissltunnel::tick()
if (waiting > 0) if (waiting > 0)
{ {
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::tick() Continuing Connection Attempt!" << std::endl; //std::cerr << "pqissltunnel::tick() Continuing Connection Attempt!" << std::endl;
#endif #endif
ConnectAttempt(); ConnectAttempt();
return 1; return 1;
@ -314,42 +314,41 @@ int pqissltunnel::tick()
/********** Internals of Tunnel Connection ****************************/ /********** Internals of Tunnel Connection ****************************/
int pqissltunnel::ConnectAttempt() int pqissltunnel::ConnectAttempt()
{ {
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::ConnectAttempt() called." << std::endl; //std::cerr << "pqissltunnel::ConnectAttempt() called." << std::endl;
#endif #endif
switch(waiting) switch(waiting)
{ {
case TUNNEL_WAITING_NOT: case TUNNEL_WAITING_NOT:
active = true; /* we're starting this one */ active = true; /* we're starting this one */
#ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::ConnectAttempt() STATE = Not Waiting." << std::endl; std::cerr << "pqissltunnel::ConnectAttempt() STATE = Not Waiting." << std::endl;
#endif
case TUNNEL_WAITING_DELAY: case TUNNEL_WAITING_DELAY:
std::cerr << "pqissltunnel::ConnectAttempt() STATE = Waiting Delay, starting connection" << std::endl;
if ((time(NULL) - mConnectTS) > TUNNEL_START_CONNECTION_DELAY) { if ((time(NULL) - mConnectTS) > TUNNEL_START_CONNECTION_DELAY) {
waiting = TUNNEL_WAITING_SPAM_PING; waiting = TUNNEL_WAITING_SPAM_HANDSHAKE;
} }
break; break;
case TUNNEL_WAITING_SPAM_PING: case TUNNEL_WAITING_SPAM_HANDSHAKE:
#ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::ConnectAttempt() STATE = Waiting for spamming handshake." << std::endl;
#endif
std::cerr << "pqissltunnel::ConnectAttempt() STATE = Waiting for spamming ping." << std::endl; spam_handshake();
waiting = TUNNEL_WAITING_RETURN_HANDSHAKE;
Spam_Ping();
waiting = TUNNEL_WAITING_PING_RETURN;
break; break;
case TUNNEL_WAITING_PING_RETURN: case TUNNEL_WAITING_RETURN_HANDSHAKE:
if ((time(NULL) - mConnectTS) < TUNNEL_PING_TIMEOUT) { if ((time(NULL) - mConnectTS) < TUNNEL_PING_TIMEOUT) {
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::ConnectAttempt() STATE = Waiting for ping reply." << std::endl; //std::cerr << "pqissltunnel::ConnectAttempt() STATE = Waiting for handshake reply." << std::endl;
#endif #endif
} else { } else {
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::ConnectAttempt() no ping reply during imparing time. Connection failed." << std::endl; std::cerr << "pqissltunnel::ConnectAttempt() no handshake reply during imparing time. Connection failed." << std::endl;
#endif #endif
waiting = TUNNEL_WAITING_NOT; waiting = TUNNEL_WAITING_NOT;
active = false; active = false;
@ -373,29 +372,27 @@ int pqissltunnel::ConnectAttempt()
return -1; return -1;
} }
void pqissltunnel::Spam_Ping() void pqissltunnel::spam_handshake()
{ {
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::Spam_Ping() starting to spam ping tunnel packet." << std::endl; std::cerr << "pqissltunnel::spam_handshake() starting to spam handshake tunnel packet." << std::endl;
#endif #endif
std::list<std::string> peers; std::list<std::string> peers;
mConnMgr->getOnlineList(peers); mConnMgr->getOnlineList(peers);
std::list<std::string>::iterator it = peers.begin(); std::list<std::string>::iterator it = peers.begin();
while (it != peers.end()) { while (it != peers.end()) {
//send a ping to the destination through the relay //send a handshake to the destination through the relay
if (*it != parent()->PeerId()) { if (*it != parent()->PeerId()) {
std::cerr << "sending ping with relay id : " << *it << std::endl; mP3tunnel->initiateHandshake(*it, parent()->PeerId());
mP3tunnel->pingTunnelConnection(*it, parent()->PeerId());
} }
++it; ++it;
} }
} }
void pqissltunnel::addIncomingPacket(void* encoded_data, int encoded_data_length) { void pqissltunnel::addIncomingPacket(void* encoded_data, int encoded_data_length) {
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::addIncomingPacket() called." << std::endl; std::cerr << "pqissltunnel::addIncomingPacket() called." << std::endl;
std::cerr << "pqissltunnel::addIncomingPacket() getRsItemSize(encoded_data) : " << getRsItemSize(encoded_data) << std::endl; #endif
#endif
last_packet_time = time(NULL); last_packet_time = time(NULL);
data_with_length data_w_l; data_with_length data_w_l;
@ -403,25 +400,30 @@ void pqissltunnel::addIncomingPacket(void* encoded_data, int encoded_data_length
memcpy(data_w_l.data, encoded_data, encoded_data_length); memcpy(data_w_l.data, encoded_data, encoded_data_length);
data_w_l.length = encoded_data_length; data_w_l.length = encoded_data_length;
data_packet_queue.push_front(data_w_l); data_packet_queue.push_front(data_w_l);
#ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::addIncomingPacket() getRsItemSize(data_w_l.data) : " << getRsItemSize(data_w_l.data) << std::endl;
#endif
} }
void pqissltunnel::IncommingPingPacket(std::string incRelayPeerId) { void pqissltunnel::IncommingPingPacket() {
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::IncommingPingPacket() called with incRelayPeerId : " << incRelayPeerId << std::endl; std::cerr << "pqissltunnel::IncommingPingPacket() called" << std::endl;
#endif #endif
last_packet_time = time(NULL);
}
void pqissltunnel::IncommingHanshakePacket(std::string incRelayPeerId) {
#ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::IncommingHanshakePacket() called with incRelayPeerId : " << incRelayPeerId << std::endl;
#endif
if ((time(NULL) - resetTime) <= TUNNEL_TIMEOUT_AFTER_RESET) { if ((time(NULL) - resetTime) <= TUNNEL_TIMEOUT_AFTER_RESET) {
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::IncommingPingPacket() a reset occured, don't activate the connection." << std::endl; std::cerr << "pqissltunnel::IncommingHanshakePacket() a reset occured, don't activate the connection." << std::endl;
#endif #endif
return; return;
} }
last_packet_time = time(NULL); last_packet_time = time(NULL);
std::string message = "pqissltunnel::IncommingPingPacket() mConnMgr->isOnline(parent()->PeerId() : "; std::string message = "pqissltunnel::IncommingHanshakePacket() mConnMgr->isOnline(parent()->PeerId() : ";
if (mConnMgr->isOnline(parent()->PeerId())) { if (mConnMgr->isOnline(parent()->PeerId())) {
message += "true"; message += "true";
} else { } else {
@ -442,14 +444,13 @@ void pqissltunnel::IncommingPingPacket(std::string incRelayPeerId) {
if (parent()) if (parent())
{ {
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::IncommingPingPacket() Notify the pqiperson.... (Both Connect/Receive)" << parent()->PeerId() <<std::endl; std::cerr << "pqissltunnel::IncommingHanshakePacket() Notify the pqiperson.... (Both Connect/Receive)" << parent()->PeerId() <<std::endl;
#endif #endif
rslog(RSL_DEBUG_BASIC, pqisslzone, "pqissltunnel::IncommingPingPacket() Notify the pqiperson.... (Both Connect/Receive)"); rslog(RSL_DEBUG_BASIC, pqisslzone, "pqissltunnel::IncommingHanshakePacket() Notify the pqiperson.... (Both Connect/Receive)");
parent() -> notifyEvent(this, NET_CONNECT_SUCCESS); parent() -> notifyEvent(this, NET_CONNECT_SUCCESS);
} }
} }
/********** Implementation of BinInterface ************************** /********** Implementation of BinInterface **************************
* All the rest of the BinInterface. * All the rest of the BinInterface.
* *
@ -470,24 +471,14 @@ int pqissltunnel::senddata(void *data, int len)
int outlen = 0; int outlen = 0;
void * out; void * out;
if (!AuthSSL::getAuthSSL()->encrypt(out, outlen, data, len, parent()->PeerId())) { if (!AuthSSL::getAuthSSL()->encrypt(out, outlen, data, len, parent()->PeerId())) {
std::cerr << "pqissltunnel::readdata() problem while crypting packet, ignoring it." << std::endl; std::cerr << "pqissltunnel::senddata() problem while crypting packet, ignoring it." << std::endl;
return -1; return -1;
} }
std::cerr << "pqissltunnel::readdata() outlen : " << outlen << std::endl;
//create RsTunnelDataItem
RsTunnelDataItem *item = new RsTunnelDataItem();
item->destPeerId = parent()->PeerId();
item->relayPeerId = relayPeerId;
item->sourcePeerId = mConnMgr->getOwnId();
item->PeerId(relayPeerId);
item->connection_accepted = 1;
item->encoded_data_len = outlen;
item->encoded_data = out;
#ifdef DEBUG_PQISSL_TUNNEL #ifdef DEBUG_PQISSL_TUNNEL
std::cerr << "pqissltunnel::senddata() sending item (Putting it into queue)" << std::endl ; std::cerr << "pqissltunnel::senddata() sending item via p3tunnel" << std::endl ;
#endif #endif
mP3tunnel->sendItem(item); mP3tunnel->sendTunnelData(parent()->PeerId(), relayPeerId, out, outlen);
return len; return len;
} }

View File

@ -106,7 +106,8 @@ virtual bool bandwidthLimited() { return true ; } // replace by !sameLAN to avoi
//called by the p3tunnel service to add incoming packets that will be read by the read data function. //called by the p3tunnel service to add incoming packets that will be read by the read data function.
void addIncomingPacket(void* encoded_data, int data_length); void addIncomingPacket(void* encoded_data, int data_length);
void IncommingPingPacket(std::string incRelayPeerId); void IncommingPingPacket();
void IncommingHanshakePacket(std::string incRelayPeerId);
private: private:
//if no packet (last_time_packet_time) is received since PING_RECEIVE_TIME_OUT, let's assume the connection is broken //if no packet (last_time_packet_time) is received since PING_RECEIVE_TIME_OUT, let's assume the connection is broken
@ -119,7 +120,7 @@ private:
int last_ping_send_time; int last_ping_send_time;
int ConnectAttempt(); int ConnectAttempt();
void Spam_Ping(); void spam_handshake();
int waiting; int waiting;
bool active; bool active;
time_t resetTime; time_t resetTime;

View File

@ -24,7 +24,6 @@ uint32_t RsTunnelDataItem::serial_size()
s += GetTlvStringSize(sourcePeerId) ; s += GetTlvStringSize(sourcePeerId) ;
s += GetTlvStringSize(relayPeerId) ; s += GetTlvStringSize(relayPeerId) ;
s += GetTlvStringSize(destPeerId) ; s += GetTlvStringSize(destPeerId) ;
s += 4 ; //connection_accept
s += 4 ; //encoded_data_len s += 4 ; //encoded_data_len
s += encoded_data_len; s += encoded_data_len;
@ -32,13 +31,31 @@ uint32_t RsTunnelDataItem::serial_size()
return s ; return s ;
} }
uint32_t RsTunnelHandshakeItem::serial_size()
{
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelHandshakeItem::serial_size() called." << std::endl ;
#endif
uint32_t s = 0 ;
s += 8 ; // header
s += GetTlvStringSize(sourcePeerId) ;
s += GetTlvStringSize(relayPeerId) ;
s += GetTlvStringSize(destPeerId) ;
s += GetTlvStringSize(sslCertPEM) ;
s += 4 ; //connection_accept
return s ;
}
// //
// ---------------------------------- Serialization ----------------------------------// // ---------------------------------- Serialization ----------------------------------//
// //
RsItem *RsTunnelSerialiser::deserialise(void *data, uint32_t *size) RsItem *RsTunnelSerialiser::deserialise(void *data, uint32_t *size)
{ {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::deserialise() called." << std::endl ; std::cerr << "RsTunnelSerialiser::deserialise() called." << std::endl ;
#endif #endif
// look what we have... // look what we have...
@ -62,6 +79,7 @@ RsItem *RsTunnelSerialiser::deserialise(void *data, uint32_t *size)
switch(getRsItemSubType(rstype)) switch(getRsItemSubType(rstype))
{ {
case RS_TUNNEL_SUBTYPE_DATA : return new RsTunnelDataItem(data,*size) ; case RS_TUNNEL_SUBTYPE_DATA : return new RsTunnelDataItem(data,*size) ;
case RS_TUNNEL_SUBTYPE_HANDSHAKE : return new RsTunnelHandshakeItem(data,*size) ;
default: default:
std::cerr << "Unknown packet type in Rstunnel!" << std::endl ; std::cerr << "Unknown packet type in Rstunnel!" << std::endl ;
@ -105,8 +123,7 @@ bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
/* add mandatory parts first */ /* add mandatory parts first */
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, sourcePeerId); 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, relayPeerId);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, destPeerId); 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) ; ok &= setRawUInt32(data, tlvsize, &offset, encoded_data_len) ;
memcpy((void*)((unsigned char*)data+offset),encoded_data,encoded_data_len) ; memcpy((void*)((unsigned char*)data+offset),encoded_data,encoded_data_len) ;
@ -119,9 +136,9 @@ bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
if ((offset + encoded_data_len) != tlvsize ) if ((offset + encoded_data_len) != tlvsize )
{ {
ok = false; ok = false;
#ifdef RSSERIAL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::serialiseTransfer() Size Error! " << std::endl; std::cerr << "RsTunnelDataItem::serialiseTransfer() Size Error! " << std::endl;
#endif #endif
} }
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
@ -131,9 +148,50 @@ bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
return ok; return ok;
} }
bool RsTunnelHandshakeItem::serialize(void *data,uint32_t& pktsize)
{
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelHandshakeItem::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 << "RsTunnelHandshakeItem::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 &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_CERT_SSL, sslCertPEM);
ok &= setRawUInt32(data, tlvsize, &offset, connection_accepted);
if (offset != tlvsize )
{
ok = false;
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelHandshakeItem::serialiseTransfer() Size Error! " << std::endl;
#endif
}
return ok;
}
//deserialize in constructor //deserialize in constructor
RsTunnelDataItem::RsTunnelDataItem(void *data,uint32_t pktsize) RsTunnelDataItem::RsTunnelDataItem(void *data,uint32_t pktsize) : RsTunnelItem(RS_TUNNEL_SUBTYPE_DATA)
: RsTunnelItem(RS_TUNNEL_SUBTYPE_DATA)
{ {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem constructor called : deserializing packet." << std::endl ; std::cerr << "RsTunnelDataItem constructor called : deserializing packet." << std::endl ;
@ -148,8 +206,7 @@ RsTunnelDataItem::RsTunnelDataItem(void *data,uint32_t pktsize)
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, sourcePeerId); 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, relayPeerId);
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, destPeerId); 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) ; ok &= getRawUInt32(data, pktsize, &offset, &encoded_data_len) ;
encoded_data = (void*)malloc(encoded_data_len) ; encoded_data = (void*)malloc(encoded_data_len) ;
@ -164,16 +221,53 @@ RsTunnelDataItem::RsTunnelDataItem(void *data,uint32_t pktsize)
#endif #endif
} }
//deserialize in constructor
RsTunnelHandshakeItem::RsTunnelHandshakeItem(void *data,uint32_t pktsize) : RsTunnelItem(RS_TUNNEL_SUBTYPE_HANDSHAKE)
{
#ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelHandshakeItem 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 << "RsTunnelHandshakeItem 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 &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_CERT_SSL, sslCertPEM);
ok &= getRawUInt32(data, pktsize, &offset, &connection_accepted);
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
#else
if (offset != 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) std::ostream& RsTunnelDataItem::print(std::ostream& o, uint16_t)
{ {
o << "RsTunnelDataItem :" << std::endl ; o << "RsTunnelDataItem :" << std::endl ;
o << " sourcePeerId : " << sourcePeerId << std::endl ; o << " sourcePeerId : " << sourcePeerId << std::endl ;
o << " relayPeerId : " << relayPeerId << std::endl ; o << " relayPeerId : " << relayPeerId << std::endl ;
o << " destPeerId : " << destPeerId << std::endl ; o << " destPeerId : " << destPeerId << std::endl ;
o << " connection_accepted : " << connection_accepted << std::endl ;
o << " encoded_data_len : " << encoded_data_len << std::endl ; o << " encoded_data_len : " << encoded_data_len << std::endl ;
if (encoded_data_len != 0 ) { return o ;
o << "getRsItemSize(encoded_data)" << getRsItemSize(encoded_data) << std::endl ; }
}
std::ostream& RsTunnelHandshakeItem::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 << " sslCertPEM : " << sslCertPEM << std::endl ;
o << " connection_accepted : " << connection_accepted << std::endl ;
return o ; return o ;
} }

View File

@ -36,6 +36,7 @@
#include "serialiser/rstlvkeys.h" #include "serialiser/rstlvkeys.h"
const uint8_t RS_TUNNEL_SUBTYPE_DATA = 0x01 ; const uint8_t RS_TUNNEL_SUBTYPE_DATA = 0x01 ;
const uint8_t RS_TUNNEL_SUBTYPE_HANDSHAKE = 0x02 ;
/***********************************************************************************/ /***********************************************************************************/
/* Basic Tunnel Item Class */ /* Basic Tunnel Item Class */
@ -63,8 +64,7 @@ class RsTunnelDataItem: public RsTunnelItem
std::string sourcePeerId ; std::string sourcePeerId ;
std::string relayPeerId ; std::string relayPeerId ;
std::string destPeerId ; std::string destPeerId ;
uint32_t connection_accepted;
std::ostream& print(std::ostream& o, uint16_t) ; std::ostream& print(std::ostream& o, uint16_t) ;
@ -72,6 +72,24 @@ class RsTunnelDataItem: public RsTunnelItem
uint32_t serial_size() ; uint32_t serial_size() ;
}; };
class RsTunnelHandshakeItem: public RsTunnelItem
{
public:
RsTunnelHandshakeItem() : RsTunnelItem(RS_TUNNEL_SUBTYPE_HANDSHAKE) {}
RsTunnelHandshakeItem(void *data,uint32_t size) ; // deserialization
std::string sourcePeerId ;
std::string relayPeerId ;
std::string destPeerId ;
std::string sslCertPEM ;
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 class RsTunnelSerialiser: public RsSerialType
{ {
public: public:

View File

@ -40,6 +40,10 @@
#include "util/rsprint.h" #include "util/rsprint.h"
#include "util/rsversion.h" #include "util/rsversion.h"
#define TUNNEL_HANDSHAKE_INIT 1
#define TUNNEL_HANDSHAKE_ACK 2
#define TUNNEL_HANDSHAKE_REFUSE 0
p3tunnel::p3tunnel(p3ConnectMgr *cm, pqipersongrp *perGrp) p3tunnel::p3tunnel(p3ConnectMgr *cm, pqipersongrp *perGrp)
:p3Service(RS_SERVICE_TYPE_TUNNEL), mConnMgr(cm), mPqiPersonGrp(perGrp) :p3Service(RS_SERVICE_TYPE_TUNNEL), mConnMgr(cm), mPqiPersonGrp(perGrp)
{ {
@ -57,21 +61,11 @@ void p3tunnel::statusChange(const std::list<pqipeer> &plist) {
int p3tunnel::tick() int p3tunnel::tick()
{ {
if (!mConnMgr->getTunnelConnection()) {
//no tunnel allowed, just drop the packet
return -1;
}
return handleIncoming(); return handleIncoming();
} }
int p3tunnel::handleIncoming() int p3tunnel::handleIncoming()
{ {
if (!mConnMgr->getTunnelConnection()) {
//no tunnel allowed, just drop the packet
return -1;
}
RsItem *item = NULL; RsItem *item = NULL;
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
@ -81,28 +75,33 @@ int p3tunnel::handleIncoming()
int nhandled = 0; int nhandled = 0;
// While messages read // While messages read
while(NULL != (item = recvItem())) while(NULL != (item = recvItem()))
{ {
if (!mConnMgr->getTunnelConnection()) {
//no tunnel allowed, just drop the packet
continue;
}
RsTunnelDataItem *tdi = NULL; RsTunnelDataItem *tdi = NULL;
RsTunnelHandshakeItem *thi = NULL;
{ {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::ostringstream out; std::ostringstream out;
out << "p3tunnel::handleIncoming()"; out << "p3tunnel::handleIncoming()";
out << " Received Message!" << std::endl; out << " Received Message!" << std::endl;
item -> print(out); item -> print(out);
out << std::endl;
std::cerr << out.str(); std::cerr << out.str();
#endif #endif
} }
if (NULL != (tdi = dynamic_cast<RsTunnelDataItem *> (item))) { if (NULL != (tdi = dynamic_cast<RsTunnelDataItem *> (item))) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::handleIncoming() tdi->encoded_data_len : " << tdi->encoded_data_len << std::endl;
#endif
recvTunnelData(tdi); recvTunnelData(tdi);
nhandled++; nhandled++;
} } else if (NULL != (thi = dynamic_cast<RsTunnelHandshakeItem *> (item))) {
delete item; recvTunnelHandshake(thi);
nhandled++;
}
delete item;
} }
return nhandled; return nhandled;
} }
@ -110,13 +109,12 @@ int p3tunnel::handleIncoming()
/*************************************************************************************/ /*************************************************************************************/
/* Output Network Msgs */ /* Output Network Msgs */
/*************************************************************************************/ /*************************************************************************************/
void p3tunnel::sendTunnelData(std::string destPeerId, std::string relayPeerId, void *data, int data_length) void p3tunnel::sendTunnelData(std::string destPeerId, std::string relayPeerId, void *data, int data_length) {
{ sendTunnelDataPrivate(relayPeerId, ownId,relayPeerId, destPeerId, data, data_length);
sendTunnelDataPrivate(1, relayPeerId, ownId,relayPeerId, destPeerId, data, data_length);
} }
void p3tunnel::sendTunnelDataPrivate(int accept, std::string to, std::string sourcePeerId, std::string relayPeerId, std::string destPeerId, void *data, int data_length)
{ void p3tunnel::sendTunnelDataPrivate(std::string to, std::string sourcePeerId, std::string relayPeerId, std::string destPeerId, void *data, int data_length) {
if (!mConnMgr->getTunnelConnection()) { if (!mConnMgr->getTunnelConnection()) {
//no tunnel allowed, just drop the request //no tunnel allowed, just drop the request
return; return;
@ -126,32 +124,30 @@ void p3tunnel::sendTunnelDataPrivate(int accept, std::string to, std::string sou
// Then send message. // Then send message.
{ {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::ostringstream out; std::ostringstream out;
out << "p3tunnel::sendTunnelDataPrivate() Constructing a RsTunnelItem Message!" << std::endl; out << "p3tunnel::sendTunnelDataPrivate() Constructing a RsTunnelItem Message!" << std::endl;
out << "Sending to: " << to; out << "Sending to: " << to;
std::cerr << out.str() << std::endl; std::cerr << out.str() << std::endl;
#endif #endif
} }
// Construct a message // Construct a message
RsTunnelDataItem *rdi = new RsTunnelDataItem(); RsTunnelDataItem *rdi = new RsTunnelDataItem();
rdi->destPeerId = destPeerId; rdi->destPeerId = destPeerId;
rdi->sourcePeerId = sourcePeerId; rdi->sourcePeerId = sourcePeerId;
rdi->relayPeerId = relayPeerId; rdi->relayPeerId = relayPeerId;
rdi->connection_accepted = accept;
rdi->encoded_data_len = data_length; rdi->encoded_data_len = data_length;
rdi->encoded_data = (void*)malloc(data_length); rdi->encoded_data = (void*)malloc(data_length);
memcpy(rdi->encoded_data, data, data_length); memcpy(rdi->encoded_data, data, data_length);
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::sendTunnelDataPrivate() data_length : "<< data_length << std::endl;
#endif
rdi->PeerId(to); rdi->PeerId(to);
/* send msg */ #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::sendTunnelDataPrivate() data_length : "<< data_length << std::endl;
#endif
/* send msg */
sendItem(rdi); sendItem(rdi);
} }
@ -161,114 +157,190 @@ void p3tunnel::pingTunnelConnection(std::string relayPeerId, std::string destPee
std::cerr << "ownId : " << ownId << std::endl; std::cerr << "ownId : " << ownId << std::endl;
std::cerr << "destPeerId : " << destPeerId << std::endl; std::cerr << "destPeerId : " << destPeerId << std::endl;
#endif #endif
this->sendTunnelDataPrivate(1, relayPeerId, ownId, relayPeerId, destPeerId, NULL, 0); this->sendTunnelDataPrivate(relayPeerId, ownId, relayPeerId, destPeerId, NULL, 0);
}
void p3tunnel::initiateHandshake(std::string relayPeerId, std::string destPeerId) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::initiateHandshake() initiating handshake with relay id : " << relayPeerId << std::endl;
std::cerr << "ownId : " << ownId << std::endl;
std::cerr << "destPeerId : " << destPeerId << std::endl;
#endif
// Construct a message
RsTunnelHandshakeItem *rhi = new RsTunnelHandshakeItem();
rhi->destPeerId = destPeerId;
rhi->sourcePeerId = ownId;
rhi->relayPeerId = relayPeerId;
rhi->connection_accepted = TUNNEL_HANDSHAKE_INIT;
rhi->sslCertPEM = AuthSSL::getAuthSSL()->SaveOwnCertificateToString();
rhi->PeerId(relayPeerId);
/* send msg */
sendItem(rhi);
} }
/*************************************************************************************/ /*************************************************************************************/
/* Input Network Msgs */ /* Input Network Msgs */
/*************************************************************************************/ /*************************************************************************************/
void p3tunnel::recvTunnelHandshake(RsTunnelHandshakeItem *item)
{
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() From: " << item->PeerId() << std::endl;
#endif
RsPeerDetails pd;
if (!AuthSSL::getAuthSSL()->LoadDetailsFromStringCert(item->sslCertPEM, pd)) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() cert is not valid. This might be a intrusion attempt." << std::endl;
#endif
return;
}
if (item->sourcePeerId != pd.id) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() cert is not issued from the source id of the tunnel. This might be a intrusion attempt." << std::endl;
#endif
return;
}
//compare the peer id from the item sender to the ids in the item.
if (item->PeerId() == item->sourcePeerId && ownId == item->relayPeerId) {
if (mConnMgr->isOnline(item->destPeerId)) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() relaying packet." << std::endl;
#endif
//relaying the handshake
RsTunnelHandshakeItem* forwardItem = new RsTunnelHandshakeItem();
forwardItem->sourcePeerId = item->sourcePeerId;
forwardItem->relayPeerId = item->relayPeerId;
forwardItem->destPeerId = item->destPeerId;
forwardItem->connection_accepted = item->connection_accepted;
forwardItem->sslCertPEM = item->sslCertPEM;
forwardItem->PeerId(item->destPeerId);
sendItem(forwardItem);
} else {
//sending back refuse
//not implemented
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() not relaying packet because destination is offline." << std::endl;
#endif
}
} else if (item->PeerId() == item->relayPeerId && ownId == item->destPeerId) {
if (item->connection_accepted == TUNNEL_HANDSHAKE_INIT || item->connection_accepted == TUNNEL_HANDSHAKE_ACK) {
//check if we accept connection
if (!mConnMgr->isFriend(pd.id)) {
//send back a refuse
// not implemented
} else {
if (item->connection_accepted == TUNNEL_HANDSHAKE_INIT) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() sending back acknowledgement to " << item->sourcePeerId << std::endl;
#endif
//send back acknowledgement
RsTunnelHandshakeItem* ack = new RsTunnelHandshakeItem();
ack->sourcePeerId = ownId;
ack->relayPeerId = item->relayPeerId;
ack->destPeerId = item->sourcePeerId;
ack->connection_accepted = TUNNEL_HANDSHAKE_ACK;
ack->sslCertPEM = AuthSSL::getAuthSSL()->SaveOwnCertificateToString();
ack->PeerId(item->relayPeerId);
sendItem(ack);
}
//open the local tunnel connection
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() opening localy the tunnel connection emulation." << std::endl;
#endif
pqiperson *pers = mPqiPersonGrp->getPeer(item->sourcePeerId);
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
pqicon->IncommingHanshakePacket(item->relayPeerId);
}
}
}
}
void p3tunnel::recvTunnelData(RsTunnelDataItem *item) void p3tunnel::recvTunnelData(RsTunnelDataItem *item)
{ {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvPeerConnectRequest() From: " << item->PeerId() << std::endl; std::cerr << "p3tunnel::recvPeerConnectRequest() From: " << item->PeerId() << std::endl;
#endif #endif
//compare the peer id from the item sender to the ids in the item. //compare the peer id from the item sender to the ids in the item.
if (item->PeerId() == item->sourcePeerId && ownId == item->relayPeerId) { if (item->PeerId() == item->sourcePeerId && ownId == item->relayPeerId) {
privateRecvTunnelDataRelaying(item); privateRecvTunnelDataRelaying(item);
} else if (item->PeerId() == item->relayPeerId && ownId == item->destPeerId) { } else if (item->PeerId() == item->relayPeerId && ownId == item->destPeerId) {
privateRecvTunnelDataDestination(item); privateRecvTunnelDataDestination(item);
} else if (item->PeerId() == item->destPeerId && ownId == item->relayPeerId) { }
//it's a ping reply from a destination, I'm relaying. Just forward the packet to the source
if (item->connection_accepted && mConnMgr->isOnline(item->sourcePeerId)) {
sendTunnelDataPrivate(1, item->sourcePeerId, item->sourcePeerId, ownId, item->destPeerId, NULL, 0);
return;
}
} else if (item->PeerId() == item->relayPeerId && ownId == item->sourcePeerId) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() it's a ping reply. Let's see if the tunnel is accepted." << std::endl;
#endif
if (item->connection_accepted) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() tunnel is accepted. activate the pqissltunnel connection." << std::endl;
#endif
pqiperson *pers = mPqiPersonGrp->getPeer(item->destPeerId);
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
pqicon->IncommingPingPacket(item->relayPeerId);
} else {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() tunnel is not accepted." << std::endl;
#endif
return;
}
}
} }
void p3tunnel::privateRecvTunnelDataRelaying(RsTunnelDataItem *item) { void p3tunnel::privateRecvTunnelDataRelaying(RsTunnelDataItem *item) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() I am relaying, let's see if it's possible to send the packet to destination." << std::endl; std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() I am relaying, let's see if it's possible to send the packet to destination." << std::endl;
#endif #endif
if (mConnMgr->isOnline(item->destPeerId)) { if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->destPeerId)) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() I am relaying, relay the packet to destination." << std::endl; std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting relay or dest peer. Aborting." << std::endl;
#endif #endif
sendTunnelDataPrivate(1, item->destPeerId, item->sourcePeerId, ownId, item->destPeerId, item->encoded_data, item->encoded_data_len); return;
}
if (mConnMgr->isOnline(item->destPeerId)) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() I am relaying, relay the packet to destination." << std::endl;
#endif
sendTunnelDataPrivate(item->destPeerId, item->sourcePeerId, ownId, item->destPeerId, item->encoded_data, item->encoded_data_len);
return; return;
} else { }
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() destination peer is not online, send back the request with a deny" << std::endl;
#endif
sendTunnelDataPrivate(0, item->sourcePeerId, item->sourcePeerId, ownId, item->destPeerId, NULL, 0);
return;
}
} }
void p3tunnel::privateRecvTunnelDataDestination(RsTunnelDataItem *item) { void p3tunnel::privateRecvTunnelDataDestination(RsTunnelDataItem *item) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() I am the destination Id, let's make some checks and read the packet." << std::endl; std::cerr << "p3tunnel::privateRecvTunnelDataDestination() I am the destination Id, let's make some checks and read the packet." << std::endl;
#endif #endif
if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->relayPeerId)) {
#ifdef P3TUNNEL_DEBUG if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->relayPeerId)) {
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting rely or source peer. Aborting." << std::endl; #ifdef P3TUNNEL_DEBUG
#endif std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting relay or source peer. Aborting." << std::endl;
#endif
return;
}
if (!mConnMgr->isOnline(item->relayPeerId)) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() relay peer is not connected, connection impossible. Aborting." << std::endl;
#endif
return; return;
} }
//peer is online when connected through a tunnel so we should not drop the packet
// if (mConnMgr->isOnline(item->sourcePeerId)) {
//#ifdef P3TUNNEL_DEBUG
// std::cerr << "p3tunnel::privateRecvTunnelDataDestination() no need to make tunnel connection, source peer is online. Aborting." << std::endl;
//#endif
// return;
// }
if (!mConnMgr->isOnline(item->relayPeerId)) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() relay peer is not connected, connection impossible. Aborting." << std::endl;
#endif
return;
}
pqiperson *pers = mPqiPersonGrp->getPeer(item->sourcePeerId); pqiperson *pers = mPqiPersonGrp->getPeer(item->sourcePeerId);
if (pers == NULL) { if (pers == NULL) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() internal source pqiperson peer not found. Aborting." << std::endl; std::cerr << "p3tunnel::privateRecvTunnelDataDestination() tunnel connection not found. Aborting." << std::endl;
#endif #endif
return;
}
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
if (pqicon == NULL || !pqicon->isactive()) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() tunnel connection not found. Aborting." << std::endl;
#endif
return; return;
} }
//if data is empty, then it's a ping, send the packet to the net emulation layer //send the packet to the net emulation layer
if (item->encoded_data_len == 0) { if (item->encoded_data_len == 0) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() receiving a ping packet, activating connection and sending back acknowlegment." << std::endl; std::cerr << "p3tunnel::privateRecvTunnelDataDestination() receiving a ping packet, activating connection and sending back acknowlegment." << std::endl;
#endif #endif
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni); pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
pqicon->IncommingPingPacket(item->relayPeerId); pqicon->IncommingPingPacket();
//sendTunnelDataPrivate(1, item->relayPeerId, item->sourcePeerId, item->relayPeerId, ownId, NULL, 0);
} else { } else {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() receiving a data packet, transfer it to the pqissltunnel connection." << std::endl; std::cerr << "p3tunnel::privateRecvTunnelDataDestination() receiving a data packet, transfer it to the pqissltunnel connection." << std::endl;
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() getRsItemSize(item->encoded_data) : " << getRsItemSize(item->encoded_data) << std::endl; std::cerr << "p3tunnel::privateRecvTunnelDataDestination() getRsItemSize(item->encoded_data) : " << getRsItemSize(item->encoded_data) << std::endl;
#endif #endif
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni); pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
pqicon->addIncomingPacket(item->encoded_data, item->encoded_data_len); pqicon->addIncomingPacket(item->encoded_data, item->encoded_data_len);
} }

View File

@ -52,12 +52,12 @@ int tick();
void sendTunnelData(std::string destPeerId, std::string relayPeerId, void *data, int data_length); void sendTunnelData(std::string destPeerId, std::string relayPeerId, void *data, int data_length);
//to establish a connection, send a ping (just a empty data packet)
void pingTunnelConnection(std::string relayPeerId, std::string destPeerId); void pingTunnelConnection(std::string relayPeerId, std::string destPeerId);
void initiateHandshake(std::string relayPeerId, std::string destPeerId);
private: private:
void sendTunnelDataPrivate(int connection_accept, std::string to, std::string sourcePeerId, std::string relayPeerId, std::string destPeerId, void *data, int data_length); void sendTunnelDataPrivate(std::string to, std::string sourcePeerId, std::string relayPeerId, std::string destPeerId, void *data, int data_length);
void privateRecvTunnelDataRelaying(RsTunnelDataItem *item); //invoked when I am relaying void privateRecvTunnelDataRelaying(RsTunnelDataItem *item); //invoked when I am relaying
void privateRecvTunnelDataDestination(RsTunnelDataItem *item); //invoked when I am the destination of the tunnel void privateRecvTunnelDataDestination(RsTunnelDataItem *item); //invoked when I am the destination of the tunnel
@ -65,6 +65,7 @@ void privateRecvTunnelDataDestination(RsTunnelDataItem *item); //invoked when I
/* Network Input */ /* Network Input */
int handleIncoming(); int handleIncoming();
void recvTunnelData(RsTunnelDataItem *item); void recvTunnelData(RsTunnelDataItem *item);
void recvTunnelHandshake(RsTunnelHandshakeItem *item);
private: private: