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

@ -40,6 +40,10 @@
#include "util/rsprint.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)
:p3Service(RS_SERVICE_TYPE_TUNNEL), mConnMgr(cm), mPqiPersonGrp(perGrp)
{
@ -57,21 +61,11 @@ void p3tunnel::statusChange(const std::list<pqipeer> &plist) {
int p3tunnel::tick()
{
if (!mConnMgr->getTunnelConnection()) {
//no tunnel allowed, just drop the packet
return -1;
}
return handleIncoming();
}
int p3tunnel::handleIncoming()
{
if (!mConnMgr->getTunnelConnection()) {
//no tunnel allowed, just drop the packet
return -1;
}
RsItem *item = NULL;
#ifdef P3TUNNEL_DEBUG
@ -81,28 +75,33 @@ int p3tunnel::handleIncoming()
int nhandled = 0;
// While messages read
while(NULL != (item = recvItem()))
{
{
if (!mConnMgr->getTunnelConnection()) {
//no tunnel allowed, just drop the packet
continue;
}
RsTunnelDataItem *tdi = NULL;
RsTunnelHandshakeItem *thi = NULL;
{
#ifdef P3TUNNEL_DEBUG
#ifdef P3TUNNEL_DEBUG
std::ostringstream out;
out << "p3tunnel::handleIncoming()";
out << " Received Message!" << std::endl;
item -> print(out);
out << std::endl;
std::cerr << out.str();
#endif
#endif
}
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);
nhandled++;
}
delete item;
} else if (NULL != (thi = dynamic_cast<RsTunnelHandshakeItem *> (item))) {
recvTunnelHandshake(thi);
nhandled++;
}
delete item;
}
return nhandled;
}
@ -110,13 +109,12 @@ int p3tunnel::handleIncoming()
/*************************************************************************************/
/* Output Network Msgs */
/*************************************************************************************/
void p3tunnel::sendTunnelData(std::string destPeerId, std::string relayPeerId, void *data, int data_length)
{
sendTunnelDataPrivate(1, relayPeerId, ownId,relayPeerId, destPeerId, data, data_length);
void p3tunnel::sendTunnelData(std::string destPeerId, std::string relayPeerId, void *data, int data_length) {
sendTunnelDataPrivate(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()) {
//no tunnel allowed, just drop the request
return;
@ -126,32 +124,30 @@ void p3tunnel::sendTunnelDataPrivate(int accept, std::string to, std::string sou
// Then send message.
{
#ifdef P3TUNNEL_DEBUG
std::ostringstream out;
out << "p3tunnel::sendTunnelDataPrivate() Constructing a RsTunnelItem Message!" << std::endl;
out << "Sending to: " << to;
std::cerr << out.str() << std::endl;
#endif
#ifdef P3TUNNEL_DEBUG
std::ostringstream out;
out << "p3tunnel::sendTunnelDataPrivate() Constructing a RsTunnelItem Message!" << std::endl;
out << "Sending to: " << to;
std::cerr << out.str() << std::endl;
#endif
}
// Construct a message
RsTunnelDataItem *rdi = new RsTunnelDataItem();
rdi->destPeerId = destPeerId;
rdi->sourcePeerId = sourcePeerId;
rdi->relayPeerId = relayPeerId;
rdi->connection_accepted = accept;
rdi->relayPeerId = relayPeerId;
rdi->encoded_data_len = data_length;
rdi->encoded_data = (void*)malloc(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);
/* send msg */
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::sendTunnelDataPrivate() data_length : "<< data_length << std::endl;
#endif
/* send msg */
sendItem(rdi);
}
@ -161,114 +157,190 @@ void p3tunnel::pingTunnelConnection(std::string relayPeerId, std::string destPee
std::cerr << "ownId : " << ownId << std::endl;
std::cerr << "destPeerId : " << destPeerId << std::endl;
#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 */
/*************************************************************************************/
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)
{
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvPeerConnectRequest() From: " << item->PeerId() << std::endl;
#endif
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::recvPeerConnectRequest() From: " << item->PeerId() << std::endl;
#endif
//compare the peer id from the item sender to the ids in the item.
if (item->PeerId() == item->sourcePeerId && ownId == item->relayPeerId) {
privateRecvTunnelDataRelaying(item);
} else if (item->PeerId() == item->relayPeerId && ownId == item->destPeerId) {
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) {
#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;
#endif
if (mConnMgr->isOnline(item->destPeerId)) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataRelaying() I am relaying, relay the packet to destination." << std::endl;
#endif
sendTunnelDataPrivate(1, item->destPeerId, item->sourcePeerId, ownId, item->destPeerId, item->encoded_data, item->encoded_data_len);
#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;
#endif
if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->destPeerId)) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting relay or dest peer. Aborting." << std::endl;
#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;
} 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) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() I am the destination Id, let's make some checks and read the packet." << std::endl;
#endif
if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->relayPeerId)) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() not trusting rely or source peer. Aborting." << std::endl;
#endif
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() I am the destination Id, let's make some checks and read the packet." << std::endl;
#endif
if (!mConnMgr->isFriend(item->sourcePeerId) || !mConnMgr->isFriend(item->relayPeerId)) {
#ifdef P3TUNNEL_DEBUG
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;
}
//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);
if (pers == NULL) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() internal source pqiperson peer not found. Aborting." << std::endl;
#endif
if (pers == NULL) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() tunnel connection not found. Aborting." << std::endl;
#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;
}
//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) {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() receiving a ping packet, activating connection and sending back acknowlegment." << std::endl;
#endif
#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);
pqicon->IncommingPingPacket(item->relayPeerId);
//sendTunnelDataPrivate(1, item->relayPeerId, item->sourcePeerId, item->relayPeerId, ownId, NULL, 0);
pqicon->IncommingPingPacket();
} else {
#ifdef P3TUNNEL_DEBUG
std::cerr << "p3tunnel::privateRecvTunnelDataDestination() receiving a data packet, transfer it to the pqissltunnel connection." << std::endl;
#ifdef P3TUNNEL_DEBUG
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;
#endif
#endif
pqissltunnel *pqicon = (pqissltunnel *)(((pqiconnect *) pers->getKid(PQI_CONNECT_TUNNEL))->ni);
pqicon->addIncomingPacket(item->encoded_data, item->encoded_data_len);
}