Corrected two harmful bugs in tunnel connexion code:

- added a destructor to RsTunnelDataItem, to remove a memory leak on all data packets.
- apparently the code would allow data items of zero size, hence calling malloc(0), which
  has undetermined behavior.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3191 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2010-06-22 21:05:56 +00:00
parent 13671d03ea
commit 5045e720b2
3 changed files with 229 additions and 204 deletions

View File

@ -82,6 +82,12 @@ RsItem *RsTunnelSerialiser::deserialise(void *data, uint32_t *size)
} }
} }
RsTunnelDataItem::~RsTunnelDataItem()
{
if(encoded_data != NULL)
free(encoded_data) ;
}
bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize) bool RsTunnelDataItem::serialize(void *data,uint32_t& pktsize)
{ {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
@ -109,25 +115,27 @@ 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, encoded_data_len) ; ok &= setRawUInt32(data, tlvsize, &offset, encoded_data_len) ;
memcpy((void*)((unsigned char*)data+offset),encoded_data,encoded_data_len) ;
if(encoded_data != NULL)
memcpy((void*)((unsigned char*)data+offset),encoded_data,encoded_data_len) ;
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::serialise() (offset + encoded_data_len) " << (offset + encoded_data_len) << std::endl; std::cerr << "RsTunnelDataItem::serialise() (offset + encoded_data_len) " << (offset + encoded_data_len) << std::endl;
std::cerr << "RsTunnelDataItem::serialise() tlvsize " << tlvsize << std::endl; std::cerr << "RsTunnelDataItem::serialise() tlvsize " << tlvsize << std::endl;
#endif #endif
if ((offset + encoded_data_len) != tlvsize ) if ((offset + encoded_data_len) != tlvsize )
{ {
ok = false; ok = false;
std::cerr << "RsTunnelDataItem::serialiseTransfer() Size Error! " << std::endl; std::cerr << "RsTunnelDataItem::serialiseTransfer() Size Error! " << std::endl;
} }
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "RsTunnelDataItem::serialize() packet size inside serialised data : " << getRsItemSize(data) << std::endl ; std::cerr << "RsTunnelDataItem::serialize() packet size inside serialised data : " << getRsItemSize(data) << std::endl ;
#endif #endif
return ok; return ok;
} }
@ -215,11 +223,16 @@ RsTunnelDataItem *RsTunnelDataItem::deserialise(void *data,uint32_t pktsize)
ok &= getRawUInt32(data, pktsize, &offset, &item->encoded_data_len) ; ok &= getRawUInt32(data, pktsize, &offset, &item->encoded_data_len) ;
if(!ok) // return early to avoid calling malloc with 0 size if(!ok) // return early to avoid calling malloc with invalid size
return NULL ; return NULL ;
item->encoded_data = (void*)malloc(item->encoded_data_len) ; if(item->encoded_data_len > 0)
memcpy(item->encoded_data, (void*)((unsigned char*)data+offset), item->encoded_data_len); {
item->encoded_data = (void*)malloc(item->encoded_data_len) ;
memcpy(item->encoded_data, (void*)((unsigned char*)data+offset), item->encoded_data_len);
}
else
item->encoded_data = NULL ;
if ((offset + item->encoded_data_len) != rssize) if ((offset + item->encoded_data_len) != rssize)
{ {

View File

@ -47,6 +47,8 @@ class RsTunnelItem: public RsItem
public: public:
RsTunnelItem(uint8_t tunnel_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_TUNNEL,tunnel_subtype) {} RsTunnelItem(uint8_t tunnel_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_TUNNEL,tunnel_subtype) {}
virtual ~RsTunnelItem() {}
virtual bool serialize(void *data,uint32_t& size) = 0 ; // Isn't it better that items can serialize themselves ? 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 uint32_t serial_size() { return 0;}
@ -56,8 +58,13 @@ class RsTunnelItem: public RsItem
class RsTunnelDataItem: public RsTunnelItem class RsTunnelDataItem: public RsTunnelItem
{ {
public: public:
RsTunnelDataItem() : RsTunnelItem(RS_TUNNEL_SUBTYPE_DATA) {} RsTunnelDataItem() : RsTunnelItem(RS_TUNNEL_SUBTYPE_DATA)
RsTunnelDataItem(void *data,uint32_t size) ; // deserialization {
encoded_data = NULL ; // To comply with the destructor below.
}
virtual ~RsTunnelDataItem() ;
static RsTunnelDataItem *deserialise(void *data,uint32_t size) ; // deserialization
uint32_t encoded_data_len; uint32_t encoded_data_len;
void *encoded_data; void *encoded_data;
@ -76,7 +83,9 @@ class RsTunnelHandshakeItem: public RsTunnelItem
{ {
public: public:
RsTunnelHandshakeItem() : RsTunnelItem(RS_TUNNEL_SUBTYPE_HANDSHAKE) {} RsTunnelHandshakeItem() : RsTunnelItem(RS_TUNNEL_SUBTYPE_HANDSHAKE) {}
RsTunnelHandshakeItem(void *data,uint32_t size) ; // deserialization virtual ~RsTunnelHandshakeItem() {}
static RsTunnelHandshakeItem *deserialise(void *data,uint32_t size) ; // deserialization
std::string sourcePeerId ; std::string sourcePeerId ;
std::string relayPeerId ; std::string relayPeerId ;

View File

@ -44,13 +44,13 @@
#define TUNNEL_HANDSHAKE_ACK 2 #define TUNNEL_HANDSHAKE_ACK 2
#define TUNNEL_HANDSHAKE_REFUSE 0 #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)
{ {
RsStackMutex stack(mTunnelMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mTunnelMtx); /********** STACK LOCKED MTX ******/
ownId = mConnMgr->getOwnId(); ownId = mConnMgr->getOwnId();
std::cerr << "ownId : " << mConnMgr->getOwnId() << std::endl; std::cerr << "ownId : " << mConnMgr->getOwnId() << std::endl;
addSerialType(new RsTunnelSerialiser()); addSerialType(new RsTunnelSerialiser());
return; return;
@ -69,39 +69,39 @@ int p3tunnel::handleIncoming()
RsItem *item = NULL; RsItem *item = NULL;
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
//std::cerr << "p3tunnel::handleIncoming() called." << std::endl; //std::cerr << "p3tunnel::handleIncoming() called." << std::endl;
#endif #endif
int nhandled = 0; int nhandled = 0;
// While messages read // While messages read
while(NULL != (item = recvItem())) while(NULL != (item = recvItem()))
{ {
if (!mConnMgr->getTunnelConnection()) { if (!mConnMgr->getTunnelConnection()) {
//no tunnel allowed, just drop the packet //no tunnel allowed, just drop the packet
continue; continue;
} }
RsTunnelDataItem *tdi = NULL; RsTunnelDataItem *tdi = NULL;
RsTunnelHandshakeItem *thi = 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);
std::cerr << out.str(); std::cerr << out.str();
#endif #endif
} }
if (NULL != (tdi = dynamic_cast<RsTunnelDataItem *> (item))) { if (NULL != (tdi = dynamic_cast<RsTunnelDataItem *> (item))) {
recvTunnelData(tdi); recvTunnelData(tdi);
nhandled++; nhandled++;
} else if (NULL != (thi = dynamic_cast<RsTunnelHandshakeItem *> (item))) { } else if (NULL != (thi = dynamic_cast<RsTunnelHandshakeItem *> (item))) {
recvTunnelHandshake(thi); recvTunnelHandshake(thi);
nhandled++; nhandled++;
} }
delete item; delete item;
} }
return nhandled; return nhandled;
} }
@ -110,74 +110,77 @@ 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(relayPeerId, ownId,relayPeerId, destPeerId, data, data_length);
} }
void p3tunnel::sendTunnelDataPrivate(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;
} }
RsStackMutex stack(mTunnelMtx); /********** STACK LOCKED MTX ******/ RsStackMutex stack(mTunnelMtx); /********** STACK LOCKED MTX ******/
// 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->encoded_data_len = data_length; rdi->encoded_data_len = data_length;
rdi->encoded_data = (void*)malloc(data_length); if(data_length > 0)
memcpy(rdi->encoded_data, data, data_length); {
rdi->encoded_data = (void*)malloc(data_length);
memcpy(rdi->encoded_data, data, data_length);
}
rdi->PeerId(to); rdi->PeerId(to);
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::sendTunnelDataPrivate() data_length : "<< data_length << std::endl; std::cerr << "p3tunnel::sendTunnelDataPrivate() data_length : "<< data_length << std::endl;
#endif #endif
/* send msg */ /* send msg */
sendItem(rdi); sendItem(rdi);
} }
void p3tunnel::pingTunnelConnection(std::string relayPeerId, std::string destPeerId) { void p3tunnel::pingTunnelConnection(std::string relayPeerId, std::string destPeerId) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::pingTunnelConnection() sending ping with relay id : " << relayPeerId << std::endl; std::cerr << "p3tunnel::pingTunnelConnection() sending ping with relay id : " << relayPeerId << std::endl;
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(relayPeerId, ownId, relayPeerId, destPeerId, NULL, 0); this->sendTunnelDataPrivate(relayPeerId, ownId, relayPeerId, destPeerId, NULL, 0);
} }
void p3tunnel::initiateHandshake(std::string relayPeerId, std::string destPeerId) { void p3tunnel::initiateHandshake(std::string relayPeerId, std::string destPeerId) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::initiateHandshake() initiating handshake with relay id : " << relayPeerId << std::endl; std::cerr << "p3tunnel::initiateHandshake() initiating handshake with relay id : " << relayPeerId << std::endl;
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
// Construct a message // Construct a message
RsTunnelHandshakeItem *rhi = new RsTunnelHandshakeItem(); RsTunnelHandshakeItem *rhi = new RsTunnelHandshakeItem();
rhi->destPeerId = destPeerId; rhi->destPeerId = destPeerId;
rhi->sourcePeerId = ownId; rhi->sourcePeerId = ownId;
rhi->relayPeerId = relayPeerId; rhi->relayPeerId = relayPeerId;
rhi->connection_accepted = TUNNEL_HANDSHAKE_INIT; rhi->connection_accepted = TUNNEL_HANDSHAKE_INIT;
rhi->sslCertPEM = AuthSSL::getAuthSSL()->SaveOwnCertificateToString(); rhi->sslCertPEM = AuthSSL::getAuthSSL()->SaveOwnCertificateToString();
rhi->PeerId(relayPeerId); rhi->PeerId(relayPeerId);
/* send msg */ /* send msg */
sendItem(rhi); sendItem(rhi);
} }
/*************************************************************************************/ /*************************************************************************************/
@ -185,164 +188,164 @@ void p3tunnel::initiateHandshake(std::string relayPeerId, std::string destPeerId
/*************************************************************************************/ /*************************************************************************************/
void p3tunnel::recvTunnelHandshake(RsTunnelHandshakeItem *item) void p3tunnel::recvTunnelHandshake(RsTunnelHandshakeItem *item)
{ {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() From: " << item->PeerId() << std::endl; std::cerr << "p3tunnel::recvTunnelHandshake() From: " << item->PeerId() << std::endl;
#endif #endif
RsPeerDetails pd; RsPeerDetails pd;
if (!AuthSSL::getAuthSSL()->LoadDetailsFromStringCert(item->sslCertPEM, pd)) { if (!AuthSSL::getAuthSSL()->LoadDetailsFromStringCert(item->sslCertPEM, pd)) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() cert is not valid. This might be a intrusion attempt." << std::endl; std::cerr << "p3tunnel::recvTunnelHandshake() cert is not valid. This might be a intrusion attempt." << std::endl;
#endif #endif
return; return;
} }
if (item->sourcePeerId != pd.id) { if (item->sourcePeerId != pd.id) {
#ifdef P3TUNNEL_DEBUG #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; std::cerr << "p3tunnel::recvTunnelHandshake() cert is not issued from the source id of the tunnel. This might be a intrusion attempt." << std::endl;
#endif #endif
return; return;
} }
//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) {
if (mConnMgr->isOnline(item->destPeerId)) { if (mConnMgr->isOnline(item->destPeerId)) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() relaying packet." << std::endl; std::cerr << "p3tunnel::recvTunnelHandshake() relaying packet." << std::endl;
#endif #endif
//relaying the handshake //relaying the handshake
RsTunnelHandshakeItem* forwardItem = new RsTunnelHandshakeItem(); RsTunnelHandshakeItem* forwardItem = new RsTunnelHandshakeItem();
forwardItem->sourcePeerId = item->sourcePeerId; forwardItem->sourcePeerId = item->sourcePeerId;
forwardItem->relayPeerId = item->relayPeerId; forwardItem->relayPeerId = item->relayPeerId;
forwardItem->destPeerId = item->destPeerId; forwardItem->destPeerId = item->destPeerId;
forwardItem->connection_accepted = item->connection_accepted; forwardItem->connection_accepted = item->connection_accepted;
forwardItem->sslCertPEM = item->sslCertPEM; forwardItem->sslCertPEM = item->sslCertPEM;
forwardItem->PeerId(item->destPeerId); forwardItem->PeerId(item->destPeerId);
sendItem(forwardItem); sendItem(forwardItem);
} else { } else {
//sending back refuse //sending back refuse
//not implemented //not implemented
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() not relaying packet because destination is offline." << std::endl; std::cerr << "p3tunnel::recvTunnelHandshake() not relaying packet because destination is offline." << std::endl;
#endif #endif
} }
} else if (item->PeerId() == item->relayPeerId && ownId == item->destPeerId) { } else if (item->PeerId() == item->relayPeerId && ownId == item->destPeerId) {
if (item->connection_accepted == TUNNEL_HANDSHAKE_INIT || item->connection_accepted == TUNNEL_HANDSHAKE_ACK) { if (item->connection_accepted == TUNNEL_HANDSHAKE_INIT || item->connection_accepted == TUNNEL_HANDSHAKE_ACK) {
//check if we accept connection //check if we accept connection
if (!mConnMgr->isFriend(pd.id)) { if (!mConnMgr->isFriend(pd.id)) {
//send back a refuse //send back a refuse
// not implemented // not implemented
} else { } else {
if (item->connection_accepted == TUNNEL_HANDSHAKE_INIT) { if (item->connection_accepted == TUNNEL_HANDSHAKE_INIT) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() sending back acknowledgement to " << item->sourcePeerId << std::endl; std::cerr << "p3tunnel::recvTunnelHandshake() sending back acknowledgement to " << item->sourcePeerId << std::endl;
#endif #endif
//send back acknowledgement //send back acknowledgement
RsTunnelHandshakeItem* ack = new RsTunnelHandshakeItem(); RsTunnelHandshakeItem* ack = new RsTunnelHandshakeItem();
ack->sourcePeerId = ownId; ack->sourcePeerId = ownId;
ack->relayPeerId = item->relayPeerId; ack->relayPeerId = item->relayPeerId;
ack->destPeerId = item->sourcePeerId; ack->destPeerId = item->sourcePeerId;
ack->connection_accepted = TUNNEL_HANDSHAKE_ACK; ack->connection_accepted = TUNNEL_HANDSHAKE_ACK;
ack->sslCertPEM = AuthSSL::getAuthSSL()->SaveOwnCertificateToString(); ack->sslCertPEM = AuthSSL::getAuthSSL()->SaveOwnCertificateToString();
ack->PeerId(item->relayPeerId); ack->PeerId(item->relayPeerId);
sendItem(ack); sendItem(ack);
} }
//open the local tunnel connection //open the local tunnel connection
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvTunnelHandshake() opening localy the tunnel connection emulation." << std::endl; std::cerr << "p3tunnel::recvTunnelHandshake() opening localy the tunnel connection emulation." << std::endl;
#endif #endif
pqiperson *pers = mPqiPersonGrp->getPeer(item->sourcePeerId); pqiperson *pers = mPqiPersonGrp->getPeer(item->sourcePeerId);
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni); pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
pqicon->IncommingHanshakePacket(item->relayPeerId); 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);
} }
} }
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->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->destPeerId)) { if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->destPeerId)) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting relay or dest peer. Aborting." << std::endl; std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting relay or dest peer. Aborting." << std::endl;
#endif #endif
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;
} }
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;
}
} }
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)) { if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->relayPeerId)) {
#ifdef P3TUNNEL_DEBUG #ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting relay or source peer. Aborting." << std::endl; std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting relay or source peer. Aborting." << std::endl;
#endif #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;
} }
pqiperson *pers = mPqiPersonGrp->getPeer(item->sourcePeerId); if (!mConnMgr->isOnline(item->relayPeerId)) {
if (pers == NULL) { #ifdef P3TUNNEL_DEBUG
#ifdef P3TUNNEL_DEBUG std::cerr << "p3tunnel::privateRecvTunnelDataDestination() relay peer is not connected, connection impossible. 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;
} }
//send the packet to the net emulation layer pqiperson *pers = mPqiPersonGrp->getPeer(item->sourcePeerId);
if (item->encoded_data_len == 0) { if (pers == NULL) {
#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() 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;
}
//send the packet to the net emulation layer
if (item->encoded_data_len == 0) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() receiving a ping packet, activating connection and sending back acknowlegment." << std::endl;
#endif
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni); pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
pqicon->IncommingPingPacket(); pqicon->IncommingPingPacket();
} 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);
} }
return; return;
} }