diff --git a/libretroshare/src/pqi/Makefile b/libretroshare/src/pqi/Makefile index ca252b644..e4256f436 100644 --- a/libretroshare/src/pqi/Makefile +++ b/libretroshare/src/pqi/Makefile @@ -5,13 +5,11 @@ include ../make.opt # All the executables that should be generated. EXECS = # test_p3items -BASE_OBJ = pqi_base.o pqi.o pqipacket.o \ +BASE_OBJ = pqi_base.o \ pqidebug.o pqisecurity.o pqinetwork.o \ - pqistreamer.o pqibin.o pqimon.o - -TUNN_OBJ = pqitunnel.o pqitunneltst.o -LOOP_OBJ = pqiloopback.o p3loopback.o -DISC_OBJ = discItem.o p3disc.o + pqibin.o pqimon.o \ + +LOOP_OBJ = pqiloopback.o ifdef PQI_USE_XPGP SSL_OBJ = xpgpcert.o @@ -20,39 +18,52 @@ else SSL_OBJ = sslcert.o endif -SSL_OBJ += pqissl.o pqiarchive.o pqissllistener.o - -GRP_OBJ = pqiperson.o pqihandler.o pqipersongrp.o +SSL_OBJ += pqissl.o pqissllistener.o +GRP_OBJ = pqiperson.o pqihandler.o pqiservice.o pqipersongrp.o +STREAM_OBJ = pqistreamer.o pqiarchive.o +OLD_OBJ = pqipacket.o p3loopback.o pqi.o +TUNN_OBJ = pqitunnel.o pqitunneltst.o +DISC_OBJ = discItem.o p3disc.o UDP_OBJ = pqistunner.o pqiudpproxy.o pqissludp.o pqitunnelproxyudp.o PRXY_OBJ = pqiproxy.o pqitunnelproxy.o CHAN_OBJ = p3channel.o pqichannel.o SN_OBJ = pqisupernode.o -OBJ = $(BASE_OBJ) $(TUNN_OBJ) $(LOOP_OBJ) $(DISC_OBJ) \ - $(GRP_OBJ) $(SSL_OBJ) $(CHAN_OBJ) $(PRXY_OBJ) $(UDP_OBJ) +OBJ = $(BASE_OBJ) $(LOOP_OBJ) \ + $(GRP_OBJ) $(SSL_OBJ) $(STREAM_OBJ) + +# Disabling this part of pqi core - until we have the basic +# items replaced by the new serialiser..... +# $(TUNN_OBJ) $(DISC_OBJ) \ +# $(CHAN_OBJ) $(PRXY_OBJ) $(UDP_OBJ) # Linux only parts. ifeq ($(OS),Linux) - OBJ += $(SN_OBJ) +# OBJ += $(SN_OBJ) endif -BASE_HDR = pqi_base.h pqi.h pqi_data.h pqipacket.h pqisecurity.h pqinetwork.h \ +BASE_HDR = pqi_base.h pqisecurity.h pqinetwork.h \ pqidebug.h pqistreamer.h pqiarchive.h pqiindic.h pqibin.h -TUNN_HDR = pqitunnel.h pqitunneltst.h -LOOP_HDR = p3loopback.h pqiloopback.h -DISC_HDR = discItem.h p3disc.h +LOOP_HDR = pqiloopback.h SSL_HDR = pqissl.h pqissllistener.h xpgpcert.h -GPR_HDR = pqihandler.h pqiperson.h pqipersongrp.h +GPR_HDR = pqihandler.h pqiperson.h pqiservice.h pqipersongrp.h +STREAM_HDR = pqistreamer.h pqiarchive.h + +OLD_HDR = pqipacket.h pqi_data.h p3loopback.h pqi.h +TUNN_HDR = pqitunnel.h pqitunneltst.h +DISC_HDR = discItem.h p3disc.h UDP_HDR = pqistunner.h pqiudpproxy.h pqissludp.h pqitunnelproxyudp.h PRXY_HDR = pqitunnelproxy.h pqiproxy.h CHAN_HDR = pqichannel.h p3channel.h SN_HDR = pqisupernode.h -HDR = $(BASE_HDR) $(TUNN_HDR) $(LOOP_HDR) $(DISC_HDR) \ - $(GRP_HDR) $(SSL_HDR) $(CHAN_HDR) $(PRXY_HDR) $(UDP_HDR) +HDR = $(BASE_HDR) $(LOOP_HDR) $(GRP_HDR) $(SSL_HDR) $(STREAM_HDR) + +#$(DISC_HDR) $(TUNN_HDR) +#$(CHAN_HDR) $(PRXY_HDR) $(UDP_HDR) LIBPQIA = $(LIBDIR)/libpqi.a LIBPQISO = $(LIBDIR)/libpqi.so diff --git a/libretroshare/src/pqi/discItem.cc b/libretroshare/src/pqi/discItem.cc deleted file mode 100644 index 6aaaadf29..000000000 --- a/libretroshare/src/pqi/discItem.cc +++ /dev/null @@ -1,353 +0,0 @@ -/* - * "$Id: discItem.cc,v 1.8 2007-02-18 21:46:49 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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 "pqi/discItem.h" - -static const int pqidizone = 44863; - -#include "pqi/pqidebug.h" -#include - -PQTunnel *createDiscItems(void *d, int n) -{ - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "createDiscItems()"); - - // check the discType of DiscItem; - if ( ((int *) d)[0] == 0) - { - return new DiscItem(); - } - return new DiscReplyItem(); -} - -DiscItem::DiscItem() - :PQTunnel(PQI_TUNNEL_DISC_ITEM_TYPE), discType(0), discFlags(0) -{ - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscItem::DiscItem()"); - - return; -} - -DiscItem *DiscItem::clone() -{ - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscItem::clone()"); - - DiscItem *di = new DiscItem(); - di -> copy(this); - return di; -} - -void DiscItem::copy(const DiscItem *di) -{ - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscItem::copy()"); - - PQTunnel::copy(di); - - discType = di -> discType; - saddr = di -> saddr; - laddr = di -> laddr; - receive_tr = di -> receive_tr; - connect_tr = di -> connect_tr; - discFlags = di -> discFlags; -} - // Overloaded from PQTunnel. -const int DiscItem::getSize() const -{ - return 3 * 4 + 2 * sizeof(struct sockaddr_in); -} - -int DiscItem::out(void *data, const int size) const -{ - { - std::ostringstream out; - out << "DiscItem::out() Data: " << data; - out << " Size: " << size << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidizone, out.str()); - } - - if (size < DiscItem::getSize()) - { - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscItem::out() Packet Too Small!"); - return -1; - } - - char *dta = (char *) data; - ((int *) dta)[0] = discType; - dta += 4; - ((struct sockaddr_in *) dta)[0] = saddr; - dta += sizeof(sockaddr_in); - ((struct sockaddr_in *) dta)[0] = laddr; - dta += sizeof(sockaddr_in); - - // version 1.... - // : 4 bytes for receive_tr - // : 4 bytes for connect_tr - //((int *) dta)[0] = receive_tr; - //dta += 4; - //((int *) dta)[0] = connect_tr; - //dta += 4; - - // version 2.... - // : 2 bytes for receive_tf - // : 2 bytes for connect_tf - // : 4 bytes for cert->flags. - ((unsigned short *) dta)[0] = htons(receive_tr); - dta += 2; - ((unsigned short *) dta)[0] = htons(connect_tr); - dta += 2; - ((unsigned long *) dta)[0] = htonl(discFlags); - dta += 4; - - return DiscItem::getSize(); -} - -int DiscItem::in(const void *data, const int size) -{ - { - std::ostringstream out; - out << "DiscItem::in() Data: " << data; - out << " Size: " << size << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidizone, out.str()); - } - - if (size < DiscItem::getSize()) - { - std::ostringstream out; - out << "DiscItem::in() Pkt to small"; - pqioutput(PQL_DEBUG_BASIC, pqidizone, out.str()); - return -1; - } - - char *dta = (char *) data; - discType = ((int *) dta)[0]; - dta += 4; - saddr = ((struct sockaddr_in *) dta)[0]; - dta += sizeof(sockaddr_in); - laddr = ((struct sockaddr_in *) dta)[0]; - dta += sizeof(sockaddr_in); - - // version 1.... - // : 4 bytes for receive_tr - // : 4 bytes for connect_tr - //receive_tf = ((int *) dta)[0]; - //dta += 4; - //connect_tf = ((int *) dta)[0]; - //dta += 4; - - // version 2.... - // : 2 bytes for receive_tr - // : 2 bytes for connect_tr - // : 4 bytes for cert->flags. - receive_tr = ntohs(((unsigned short *) dta)[0]); - dta += 2; - connect_tr = ntohs(((unsigned short *) dta)[0]); - dta += 2; - discFlags = ntohl(((unsigned long *) dta)[0]); - dta += 4; - - return getSize(); -} - -std::ostream &DiscItem::print(std::ostream &out) -{ - out << "-------- DiscItem" << std::endl; - PQItem::print(out); - - out << "Peer Details" << std::endl; - out << "Local Address: " << inet_ntoa(laddr.sin_addr); - out << " Port: " << ntohs(laddr.sin_port) << std::endl; - - out << "Server Address: " << inet_ntoa(saddr.sin_addr); - out << " Port: " << ntohs(saddr.sin_port) << std::endl; - out << "[R/C]_TF: " << receive_tr; - out << "/" << connect_tr << std::endl; - out << "discFlags: " << discFlags << std::endl; - - return out << "--------" << std::endl; -} - - -DiscReplyItem::DiscReplyItem() - :DiscItem(), certDER(NULL), certLen(0) -{ - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscReplyItem::DiscReplyItem()"); - - discType = 1; - return; -} - -DiscReplyItem::~DiscReplyItem() -{ - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscReplyItem::~DiscReplyItem()"); - - if (certDER != NULL) - { - free(certDER); - certDER = NULL; - certLen = 0; - } -} - -DiscReplyItem *DiscReplyItem::clone() -{ - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscReplyItem::clone()"); - - DiscReplyItem *di = new DiscReplyItem(); - di -> copy(this); - return di; -} - -void DiscReplyItem::copy(const DiscReplyItem *di) -{ - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscReplyItem::copy()"); - - DiscItem::copy(di); - - - if (di -> certDER != NULL) - { - certLen = di -> certLen; - certDER = (unsigned char *) malloc(certLen); - memcpy(certDER, di -> certDER, certLen); - } - else - { - certLen = 0; - certDER = NULL; - } - - return; -} - - // Overloaded from PQTunnel. -const int DiscReplyItem::getSize() const -{ - return DiscItem::getSize() + 4 + certLen; -} - -int DiscReplyItem::out(void *data, const int maxsize) const -{ - { - std::ostringstream out; - out << "DiscReplyItem::out() Data: " << data; - out << " Size: " << maxsize << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidizone, out.str()); - } - - if (maxsize < getSize()) - { - std::ostringstream out; - out << "DiscReplyItem::out() Not Enough space"; - pqioutput(PQL_DEBUG_BASIC, pqidizone, out.str()); - - return -1; - } - - int basesize = DiscItem::getSize(); - // put out the DiscItem Part. - DiscItem::out(data, basesize); - char *loc = ((char *) data) + basesize; - ((int *) loc)[0] = certLen; - loc += 4; - memcpy(loc, certDER, certLen); - return getSize(); -} - -int DiscReplyItem::in(const void *data, const int size) -{ - { - std::ostringstream out; - out << "DiscReplyItem::in() Data: " << data; - out << " Size: " << size << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidizone, out.str()); - } - - // harder. - int basesize = DiscItem::getSize(); - if (basesize + 4 > size) - { - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscReplyItem::in() Not enough space for Base Class!"); - return -1; // cant be us. - } - - // read in the DiscItem part. - DiscItem::in(data, DiscItem::getSize()); - char *loc = ((char *) data) + basesize; - int certsize = ((int *) loc)[0]; - // check the size again. - if (basesize + 4 + certsize != size) - { - pqioutput(PQL_DEBUG_BASIC, pqidizone, - "DiscReplyItem::in() Not enough space for Certificate"); - return -1; - } - loc += 4; - // allocate memory for certDER.... - if ((certLen != certsize) && (!certLen)) - { - if (certDER) - free(certDER); - } - certLen = certsize; - if (!certDER) - { - certDER = (unsigned char *) malloc(certsize); - } - - memcpy(certDER, loc, certLen); - return getSize(); -} - -std::ostream &DiscReplyItem::print(std::ostream &out) -{ - out << "---------------- DiscReplyItem" << std::endl; - out << "A Friend of a Friend:" << std::endl; - DiscItem::print(out); - - if (certDER != NULL) - { - out << "CertLen: " << certLen << std::endl; - } - else - { - out << "No Certificate" << std::endl; - } - - return out << "----------------" << std::endl; -} - diff --git a/libretroshare/src/pqi/discItem.h b/libretroshare/src/pqi/discItem.h deleted file mode 100644 index 8f81b1412..000000000 --- a/libretroshare/src/pqi/discItem.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * "$Id: discItem.h,v 1.6 2007-02-18 21:46:49 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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". - * - */ - - - -#ifndef MRK_PQI_DISCITEM_H -#define MRK_PQI_DISCITEM_H - -#include "pqi/pqitunnel.h" - -#define PQI_TUNNEL_DISC_ITEM_TYPE 121 - -PQTunnel *createDiscItems(void *d, int n); - -class DiscItem: public PQTunnel -{ - protected: - DiscItem(int st); - public: - DiscItem(); -virtual DiscItem *clone(); -void copy(const DiscItem *di); -virtual std::ostream &print(std::ostream &out); - - // Overloaded from PQTunnel. -virtual const int getSize() const; -virtual int out(void *data, const int size) const; -virtual int in(const void *data, const int size); - - int discType; // specifies subtypes. - - struct sockaddr_in laddr; - struct sockaddr_in saddr; - - // time frame of recent connections. - unsigned short connect_tr; - unsigned short receive_tr; - // flags... - unsigned long discFlags; -}; - -class DiscReplyItem: public DiscItem -{ - public: - - DiscReplyItem(); -virtual ~DiscReplyItem(); -virtual DiscReplyItem *clone(); -void copy(const DiscReplyItem *di); -virtual std::ostream &print(std::ostream &out); - - // Overloaded from PQTunnel. -virtual const int getSize() const; -virtual int out(void *data, const int size) const; -virtual int in(const void *data, const int size); - - - unsigned char *certDER; - int certLen; -}; - -#define AUTODISC_RDI_SUBTYPE_PING 1 -#define AUTODISC_RDI_SUBTYPE_RPLY 2 - -#define AUTODISC_LDI_SUBTYPE_PING 1 -#define AUTODISC_LDI_SUBTYPE_RPLY 2 - -#endif // MRK_PQI_DISCITEM_H diff --git a/libretroshare/src/pqi/p3disc.cc b/libretroshare/src/pqi/p3disc.cc deleted file mode 100644 index e5a6fd3e2..000000000 --- a/libretroshare/src/pqi/p3disc.cc +++ /dev/null @@ -1,2276 +0,0 @@ -/* - * "$Id: p3disc.cc,v 1.23 2007-02-18 21:46:49 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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 "pqi/p3disc.h" - -// for active local cert stuff. -#include "pqi/pqissl.h" - -#include -#include -#include - -const int PQI_DISC_ITEM_TYPE = 0x69532; - -#include -#include "pqi/pqidebug.h" - -const int pqidisczone = 2482; - - -static int updateAutoServer(autoserver *as, DiscItem *di); -static int convertTDeltaToTRange(double tdelta); -static int convertTRangeToTDelta(int trange); -static int updateCertAvailabilityFlags(cert *c, unsigned long discFlags); -static unsigned long determineCertAvailabilityFlags(cert *c); - -// Operating System specific includes. -#include "pqi/pqinetwork.h" - -p3disc::p3disc(sslroot *r) - :PQTunnelService(PQI_TUNNEL_DISC_ITEM_TYPE), sroot(r) -{ - ldata = NULL; - ldlenmax = 1024; - - local_disc = false; //true; - remote_disc = true; - - // set last check to current time, this prevents queued. - // messages at the start! (actually shouldn't matter - as they aren't connected). - ts_lastcheck = time(NULL); // 0; - - // configure... - load_configuration(); - localSetup(); - - return; -} - -p3disc::~p3disc() -{ - return; -} - -// The PQTunnelService interface. -int p3disc::receive(PQTunnel *i) -{ - inpkts.push_back(i); - - std::ostringstream out; - out << "p3disc::receive() InQueue:" << inpkts.size() << std::endl; - out << "incoming packet: " << std::endl; - i -> print(out); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - - return 1; -} - -PQTunnel *p3disc::send() -{ - std::ostringstream out; - out << "p3disc::send() To Send:" << outpkts.size() << std::endl; - - if (outpkts.size() < 1) - { - out << "No Packet" << std::endl; - pqioutput(PQL_DEBUG_ALL, pqidisczone, out.str()); - return NULL; - } - PQTunnel *pkt = (PQTunnel *) outpkts.front(); - outpkts.pop_front(); - - out << "outgoing packet: " << std::endl; - pkt -> print(out); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - - return pkt; -} - -PQTunnel *p3disc::getObj() -{ - return new DiscItem(); -} - - -int p3disc::tick() -{ - pqioutput(PQL_DEBUG_ALL, pqidisczone, - "p3disc::tick()"); - - if (local_disc) - { - if (ts_nextlp == 0) - { - pqioutput(PQL_DEBUG_ALL, pqidisczone, - "Local Discovery On!"); - localPing(baddr); - localListen(); - } - } - else - { - pqioutput(PQL_DEBUG_ALL, pqidisczone, - "Local Discovery Off!"); - } - - // ten minute counter. - if (--ts_nextlp < 0) - { - ts_nextlp = 600; - } - - if (ts_nextlp % 300 == 0) - idServers(); - - - /* remote discovery can run infrequently.... - * this is a good idea, as it ensures that - * multiple Pings aren't sent to neighbours.... - * - * only run every 5 seconds. - */ - - if (ts_nextlp % 5 != 0) - { - return 1; - } - - // important bit - int nr = handleReplies(); // discards packets if not running. - if (remote_disc) - { - pqioutput(PQL_DEBUG_ALL, pqidisczone, - "Remote Discovery On!"); - newRequests(); - if ((sroot -> collectedCerts()) || (nr > 0)) - { - distillData(); - } - } - else - { - pqioutput(PQL_DEBUG_ALL, pqidisczone, - "Remote Discovery Off!"); - } - return 1; -} - -static int local_disc_def_port = 7770; -static int local_disc_secondary_port = 7870; - -int p3disc::setLocalAddress(struct sockaddr_in srvaddr) -{ - saddr = srvaddr; - return 1; -} - - -int p3disc::determineLocalNetAddr() -{ - // laddr filled in by load_configuration. - laddr.sin_port = htons(local_disc_def_port); - -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ -#ifndef WINDOWS_SYS // ie UNIX - // broadcast address. - baddr.sin_family = AF_INET; - inet_aton("0.0.0.0", &(baddr.sin_addr)); - baddr.sin_port = htons(local_disc_def_port); -#else // WIN - - baddr. sin_family = AF_INET; - - // So as recommended on this site.. will use |+& to calc it. - unsigned long netmask = inet_addr("255.255.255.0"); - unsigned long netaddr = saddr.sin_addr.s_addr & netmask; - baddr.sin_addr.s_addr = netaddr | (~netmask); - - // direct works! - //baddr.sin_addr.s_addr = inet_addr("10.0.0.59"); - //baddr.sin_addr.s_addr = inet_addr("127.0.0.1"); - // broadcast! - baddr.sin_addr.s_addr = INADDR_BROADCAST; - baddr.sin_port = htons(local_disc_def_port); - - - - - -#endif -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ - - { - std::ostringstream out; - out << "p3disc::determineLocalNetAddr() baddr: "; - out << inet_ntoa(baddr.sin_addr) << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - return 1; -} - -int p3disc::setupLocalPacket(int type, struct sockaddr_in *home, - struct sockaddr_in *server) -{ - if (ldata == NULL) - { - ldata = malloc(ldlenmax); - } - - // setup packet. - // 8 bytes - tag - ((char *) ldata)[0] = 'P'; - ((char *) ldata)[1] = 'Q'; - ((char *) ldata)[2] = 'I'; - ((char *) ldata)[3] = 'L'; - if (type == AUTODISC_LDI_SUBTYPE_PING) - { - ((char *) ldata)[4] = 'D'; - ((char *) ldata)[5] = 'I'; - ((char *) ldata)[6] = 'S'; - ((char *) ldata)[7] = 'C'; - } - else - { - ((char *) ldata)[4] = 'R'; - ((char *) ldata)[5] = 'P'; - ((char *) ldata)[6] = 'L'; - ((char *) ldata)[7] = 'Y'; - } - - // sockaddr copy. - ldlen = 8; - for(unsigned int i = 0; i < sizeof(*home); i++) - { - ((char *) ldata)[ldlen + i] = ((char *) home)[i]; - } - - ldlen += sizeof(*home); - for(unsigned int i = 0; i < sizeof(*server); i++) - { - ((char *) ldata)[ldlen + i] = ((char *) server)[i]; - } - ldlen += sizeof(*server); - - return 1; -} - - - - - -int p3disc::localSetup() -{ - - if (!local_disc) - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::localSetup() Warning local_disc OFF!"); - return -1; - } - - //First we must attempt to open the default socket - determineLocalNetAddr(); - - int err = 0; - - lsock = socket(PF_INET, SOCK_DGRAM, 0); -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ -#ifndef WINDOWS_SYS // ie UNIX - - if (lsock < 0) - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::localSetup() Cannot open UDP socket!"); - local_disc = false; - return -1; - } - - err = fcntl(lsock, F_SETFL, O_NONBLOCK); - if (err < 0) - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::localSetup() Error: Cannot make socket NON-Blocking: "); - - local_disc = false; - return -1; - } - - int on = 1; - if(0 != (err =setsockopt(lsock, SOL_SOCKET, SO_BROADCAST,(void *) &on, sizeof(on)))) - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::localSetup() Error: Cannot make socket Broadcast: "); - local_disc = false; - return -1; - } - else - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::localSetup() Broadcast Flag Set!"); - } - -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ -#else //WINDOWS_SYS - - if (lsock == INVALID_SOCKET) - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::localSetup() Cannot open UDP socket!"); - local_disc = false; - return -1; - } - - unsigned long int on = 1; - if (0 != (err = ioctlsocket(lsock, FIONBIO, &on))) - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::localSetup() Error: Cannot make socket NON-Blocking: "); - local_disc = false; - return -1; - } - - on = 1; - if(0 != (err=setsockopt(lsock, SOL_SOCKET, SO_BROADCAST,(char *) &on, sizeof(on)))) - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::localSetup() Error: Cannot make socket Broadcast: "); - - local_disc = false; - return -1; - } - else - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::localSetup() Broadcast Flag Set!"); - } - -#endif -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ - - { - std::ostringstream out; - out << "p3disc::localSetup()" << std::endl; - out << "\tSetup Family: " << laddr.sin_family; - out << std::endl; - out << "\tSetup Address: " << inet_ntoa(laddr.sin_addr); - out << std::endl; - out << "\tSetup Port: " << ntohs(laddr.sin_port) << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - if (0 != (err = bind(lsock, (struct sockaddr *) &laddr, sizeof(laddr)))) - { - std::ostringstream out; - out << "p3disc::localSetup()"; - out << " Cannot Bind to Default Address!" << std::endl; - showSocketError(out); - out << std::endl; - out << " Trying Secondary Address." << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - - } - else - { - // ifsucessful then call localPing - // set ts to -1 and don't worry about outgoing until - // we receive a packet - std::ostringstream out; - out << "p3disc::localSetup()" << std::endl; - out << " Bound to Address." << std::endl; - out << "\tSetup Address: " << inet_ntoa(laddr.sin_addr); - out << std::endl; - out << "\tSetup Port: " << ntohs(laddr.sin_port) << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - - ts_nextlp = -1; - ts_nextlp = 10; - localPing(baddr); - return 1; - } - - laddr.sin_port = htons(local_disc_secondary_port); - if (0 != (err = bind(lsock, (struct sockaddr *) &laddr, sizeof(laddr)))) - { - std::ostringstream out; - out << "p3disc::localSetup()"; - out << " Cannot Bind to Secondary Address!" << std::endl; - showSocketError(out); - out << std::endl; - out << " Giving Up!" << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - local_disc = false; - return -1; - } - else - { - std::ostringstream out; - out << "p3disc::localSetup()" << std::endl; - out << " Bound to Secondary Address." << std::endl; - out << "\tSetup Address: " << inet_ntoa(laddr.sin_addr); - out << std::endl; - out << "\tSetup Port: " << ntohs(laddr.sin_port) << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - - ts_nextlp = 10; - localPing(baddr); - return 1; - } - - // else we open a random port and set the timer - // ie - don't bind to a port..... - ts_nextlp = 10; // ping every 10 minutes. - localPing(baddr); - return 1; -} - -int p3disc::localPing(struct sockaddr_in reply_to) -{ - //This function sends a meessage out containing both cert - // and server address, as well as the ping address (if not standard) - - // so we send a packet out to that address - // (most likely broadcast address). - - // setup up the data for connection. - setupLocalPacket(AUTODISC_LDI_SUBTYPE_PING,&laddr, &saddr); - - // Cast to char for windows benefit. - int len = sendto(lsock, (char *) ldata, ldlen, 0, (struct sockaddr *) &reply_to, sizeof(reply_to)); - if (len != ldlen) - { - std::ostringstream out; - out << "p3disc::localPing()"; - out << " Failed to send Packet." << std::endl; - out << "Sent (" << len << "/" << ldlen; - out << std::endl; - out << "Addr:" << inet_ntoa(reply_to.sin_addr) << std::endl; - out << "Port:" << ntohs(reply_to.sin_port) << std::endl; - out << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - else - { - std::ostringstream out; - out << "p3disc::localPing() Success!" << std::endl; - out << "Sent To Addr:" << inet_ntoa(reply_to.sin_addr) << std::endl; - out << "Sent To Port:" << ntohs(reply_to.sin_port) << std::endl; - out << "Message Size: " << len << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - return 1; -} - -int p3disc::localReply(struct sockaddr_in reply_to) -{ - //This function sends a meessage out containing both cert - // and server address, as well as the ping address (if not standard) - - // so we send a packet out to that address - // (most likely broadcast address). - - // setup up the data for connection. - setupLocalPacket(AUTODISC_LDI_SUBTYPE_RPLY,&laddr, &saddr); - - // Cast to char for windows benefit. - int len = sendto(lsock, (char *) ldata, ldlen, 0, (struct sockaddr *) &reply_to, sizeof(reply_to)); - if (len != ldlen) - { - std::ostringstream out; - out << "p3disc::localPing()"; - out << " Failed to send Packet." << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - return 1; -} - - -int p3disc::localListen() -{ - //This function listens to the ping address. - //For each reply, store the result in the structure and mark as local - struct sockaddr_in addr; - struct sockaddr_in neighbour; - struct sockaddr_in server; - socklen_t alen = sizeof(addr); - int nlen = sizeof(neighbour); - int len; - int size = 0; - - while(0 < (size = recvfrom(lsock, (char *) ldata, ldlen, 0, - (struct sockaddr *) &addr, &alen))) - { - std::ostringstream out; - out << "Recved Message" << std::endl; - out << "From Addr:" << inet_ntoa(addr.sin_addr) << std::endl; - out << "From Port:" << ntohs(addr.sin_port) << std::endl; - out << "Message Size: " << size << std::endl; - - for(int i = 0; i < 8; i++) - { - out << ((char *) ldata)[i]; - } - out << std::endl; - - len = 8; - // sockaddr copy. - for(int i = 0; i < nlen; i++) - { - ((char *) &neighbour)[i] = ((char *) ldata)[len + i]; - } - len += nlen; - for(int i = 0; i < nlen; i++) - { - ((char *) &server)[i] = ((char *) ldata)[len + i]; - } - len += nlen; - - - out << "Neighbour Addr:" << inet_ntoa(neighbour.sin_addr) << std::endl; - out << "Neighbour Port:" << ntohs(neighbour.sin_port) << std::endl; - out << "Server Addr:" << inet_ntoa(server.sin_addr) << std::endl; - out << "Server Port:" << ntohs(server.sin_port) << std::endl; - - if ((laddr.sin_addr.s_addr == neighbour.sin_addr.s_addr) && - (laddr.sin_port == neighbour.sin_port)) - { - // Then We Sent it!!!! - // ignore.. - out << "Found Self! Addr - " << inet_ntoa(neighbour.sin_addr); - out << ":" << ntohs(neighbour.sin_port) << std::endl; - } - else - { - if ('D' == (((char *) ldata)[4])) // Then Ping. - { - // reply. - localReply(neighbour); - } - - addLocalNeighbour(&neighbour, &server); - } - - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ -#ifndef WINDOWS_SYS // ie UNIX - - if ((size < 0) && (errno != EAGAIN)) - { - std::ostringstream out; - out << "Error Recieving Message" << std::endl; - out << "Errno: " << errno << std::endl; - out << socket_errorType(errno) << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ -#else // WINDOWS_SYS - - if (size == SOCKET_ERROR) - { - int err = WSAGetLastError(); - if (err != WSAEWOULDBLOCK) - { - std::ostringstream out; - out << "Error Recieving Message" << std::endl; - out << "WSE: " << err << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - } - -#endif -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ - - return 1; -} - -// This needs to be fixed up.... -// Local dicsovery disabled for the moment.... - -int p3disc::addLocalNeighbour(struct sockaddr_in *n, struct sockaddr_in *s) -{ - std::list::iterator it; - for(it = neighbours.begin(); it != neighbours.end(); it++) - { - // if the server address matches one already! - // make sure its flags as local and return. - if (0 == memcmp((char *) &((*it) -> server_addr), (char *) s, sizeof(*s))) - { - (*it) -> local = true; - return 1; - } - } - // else add it in! - autoneighbour *ln = new autoneighbour(); - ln -> server_addr = (*s); - ln -> local = true; - ln -> id = NULL; // null cert - - std::string nname = "Local Neighbour ("; - nname += inet_ntoa(ln -> server_addr.sin_addr); - nname += ")"; - - //ln -> id -> Name(nname); - - // now we call the dummy connect... - // this will only be done once per local neighbour. - // connectForExchange(ln -> server_addr); - - neighbours.push_back(ln); - // update dicovered. - distillData(); - return 1; -} - - -// Code Fragment that might be useful when local is patched up.... -/**************************************************** -int p3disc::distillLocalData() -{ - // This transforms the autoneighbour tree into - // a list of certificates with the best guess settings. - - discovered.clear(); - - pqioutput(PQL_DEBUG_BASIC, pqidisczone, "p3disc::distillData()"); - - std::list::iterator it; - std::list::iterator it2; - - // Now check for local -> remote duplicates.... - for(it = neighbours.begin(); it != neighbours.end();) - { - cert *c = (cert *) ((*it) -> id); - if (((*it) -> local) && (c == NULL)) - { - // potentially a duplicate. - bool found = false; - for(it2 = neighbours.begin(); it2 != neighbours.end(); it2++) - { - // if address is the same -> remove first version. - if ((it != it2) && (0 == memcmp((char *) &((*it) -> addr), - (char *) &((*it2) -> addr), - sizeof(struct sockaddr)))) - { - (*it2) -> local = true; - found = true; - } - } - if (found == true) - { - // remove the certless local. - it = neighbours.erase(it); - } - else - { - it++; - } - } - else - { - it++; - } - } - -******************************************/ - - -int p3disc::idServers() -{ - std::list::iterator it; - std::list::iterator nit; - int cts = time(NULL); - - std::ostringstream out; - out << "::::AutoDiscovery Neighbours::::" << std::endl; - for(it = neighbours.begin(); it != neighbours.end(); it++) - { - if ((*it) -> local) - { - out << "Local Neighbour: "; - } - else - { - out << "Friend of a friend: "; - } - cert *c = (cert *) ((*it) -> id); - - if (c != NULL) - { - if (c -> certificate != NULL) - { - out << c -> certificate -> name; - } - else - { - out << c -> Name(); - } - } - else - { - out << "UnIdentified"; - } - - out << std::endl; - out << "BG LocalAddr: "; - out << inet_ntoa((*it) -> local_addr.sin_addr); - out << ":" << ntohs((*it) -> local_addr.sin_port) << std::endl; - out << "BG Server: "; - out << inet_ntoa((*it) -> server_addr.sin_addr); - out << ":" << ntohs((*it) -> server_addr.sin_port) << std::endl; - out << " Listen TR: "; - if (((*it) -> listen) && ((*it) -> l_ts)) - { - out << cts - (*it) -> l_ts << " sec ago"; - } - else - { - out << "Never"; - } - out << " "; - - out << "Connect TR: "; - if (((*it) -> connect) && ((*it) -> c_ts)) - { - out << cts - (*it) -> c_ts << " sec ago"; - } - else - { - out << "Never"; - } - - if ((*it) -> active) - { - out << " Active!!!"; - } - out << std::endl; - - out << " -->DiscFlags: 0x" << std::hex << (*it)->discFlags; - out << std::dec << std::endl; - - for(nit = ((*it) -> neighbour_of).begin(); - nit != ((*it) -> neighbour_of).end(); nit++) - { - out << "\tConnected via: "; - if ((*nit) -> id != NULL) - { - out << ((*nit) ->id) -> Name() << "("; - out << inet_ntoa(((*nit) -> id) -> lastaddr.sin_addr); - out << ":" << ntohs(((*nit) -> id) -> lastaddr.sin_port); - out << ")"; - } - out << std::endl; - out << "\t\tServer: "; - out << inet_ntoa((*nit) -> server_addr.sin_addr); - out <<":"<< ntohs((*nit) -> server_addr.sin_port); - out << std::endl; - out << "\t\tLocalAddr: "; - out << inet_ntoa((*nit) -> local_addr.sin_addr); - out <<":"<< ntohs((*nit) -> local_addr.sin_port); - - out << std::endl; - if ((*nit) -> listen) - { - out << "\t\tListen TR:"; - out << cts - (*nit) -> l_ts << " sec ago"; - } - else - { - out << "\t\tNever Received!"; - } - out << std::endl; - if ((*nit) -> connect) - { - out << "\t\tConnect TR:"; - out << cts - (*nit) -> c_ts << " sec ago"; - } - else - { - out << "\t\tNever Connected!"; - } - out << std::endl; - out << "\t\tDiscFlags: 0x" << std::hex << (*nit)->discFlags; - out << std::dec << std::endl; - } - } - pqioutput(PQL_WARNING, pqidisczone, out.str()); - return 1; -} - - - - -int p3disc::newRequests() -{ - // Check the timestamp against the list of certs. - // If any are newer and currently active, then - // send out Discovery Request. - // This initiates the p3disc procedure. - - if (!remote_disc) - { - pqioutput(PQL_DEBUG_ALL, pqidisczone, - "p3disc::newRequests() Remote Discovery is turned off"); - return -1; - } - - pqioutput(PQL_DEBUG_ALL, pqidisczone, - "p3disc::newRequests() checkin for new neighbours"); - - // Perform operation on the cert list. - std::list::iterator it; - // Temp variable - std::list &certlist = sroot -> getCertList(); - - { - std::ostringstream out; - out << "Checking CertList!" << std::endl; - out << "last_check: " << ts_lastcheck; - out << " time(): " << time(NULL); - pqioutput(PQL_DEBUG_ALL, pqidisczone, out.str()); - } - - for(it = certlist.begin(); it != certlist.end(); it++) - { - { - std::ostringstream out; - out << "Cert: " << (*it) -> Name(); - out << " lc_ts: " << (*it) -> lc_timestamp; - out << " lr_ts: " << (*it) -> lr_timestamp; - pqioutput(PQL_DEBUG_ALL, pqidisczone, out.str()); - } - - // This should be Connected(), rather than Accepted(). - // should reply with all Accepted(), but only send to all connected(). - // if (((*it) -> Accepted()) && - // - // need >= to ensure that it will happen, - // about 1 in 5 chance of multiple newRequests if called every 5 secs. - // can live with this. (else switch to fractional seconds). - - if (((*it) -> Connected()) && - (((*it) -> lc_timestamp >= ts_lastcheck) - || ((*it) -> lr_timestamp >= ts_lastcheck))) - { - - // also must not have already sent message. - // (unless reconnection?) - // actually - this should occur, even if last - // exchange not complete. - // reconnect - -/***************************************************************************** - * No more need for ad_init silliness.... - * - //if (ad_init.end() == - // find(ad_init.begin(),ad_init.end(),*it)) - // infact - we need the opposite behaviour. - // remove if in the init list. - - std::list::iterator it2; - if (ad_init.end() != - (it2 = find(ad_init.begin(),ad_init.end(),*it))) - { - ad_init.erase(it2); - } - * - * - * - ****************************************************************************/ - - - { - // Then send message. - { - std::ostringstream out; - out << "p3disc::newRequests()"; - out << "Constructing a Message!" << std::endl; - out << "Sending to: " << (*it) -> Name(); - out << " lc_ts: " << (*it) -> lc_timestamp; - out << " lr_ts: " << (*it) -> lr_timestamp; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - // Construct a message - DiscItem *di = new DiscItem(); - - // get our details..... - cert *own = sroot -> getOwnCert(); - - // Fill the message - di -> cid = (*it) -> cid; - di -> laddr = own -> localaddr; - di -> saddr = own -> serveraddr; - - // if we are firewalled..... (and no forwarding...) - // set received as impossible. - if (own -> Firewalled() && (!(own -> Forwarded()))) - di -> receive_tr = 0; /* invalid */ - else - di -> receive_tr = 1; /* zero time */ - - di -> connect_tr = 1; /* zero time */ - di -> discFlags = determineCertAvailabilityFlags(own); - - // Send off message - outpkts.push_back(di); - //p3i -> SendOtherPQItem(di); - -/***************************************************************************** - * No more need for ad_init silliness.... - // push onto init list. - ad_init.push_back(*it); - * - ****************************************************************************/ - - // Finally we should also advertise the - // new connection to our neighbours???? - // SHOULD DO - NOT YET. - - } - } - } - ts_lastcheck = time(NULL); - return 1; -} - -bool isDiscItem(PQItem *item) -{ - if (item -> type == PQI_ITEM_TYPE_AUTODISCITEM) - return true; - return false; -} - -int p3disc::handleReplies() -{ - DiscItem *di = NULL; - pqioutput(PQL_DEBUG_ALL, pqidisczone, "p3disc::handleReplies()"); - - // if off discard item. - if (!remote_disc) - { - //while(NULL != (di = (DiscItem *) p3i -> SelectOtherPQItem(isDiscItem))) - while(inpkts.size() > 0) - { - std::ostringstream out; - out << "p3disc::handleReplies()"; - out << " Deleting - Cos RemoteDisc Off!" << std::endl; - - di = (DiscItem *) inpkts.front(); - inpkts.pop_front(); - - di -> print(out); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - - delete di; - } - return 0; - } - - int nhandled = 0; - // While messages read - //while(NULL != (di = (DiscItem *) p3i -> SelectOtherPQItem(isDiscItem))) - while(inpkts.size() > 0) - { - PQItem *item = inpkts.front(); - inpkts.pop_front(); - - DiscItem *di = NULL; - DiscReplyItem *dri = NULL; - - if (NULL == (di = dynamic_cast (item))) - { - std::ostringstream out; - out << "p3disc::handleReplies()"; - out << "Deleting Non DiscItem Msg" << std::endl; - item -> print(out); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - - // delete and continue to next loop. - delete item; - - continue; - } - nhandled++; - - { - std::ostringstream out; - out << "p3disc::handleReplies()"; - out << " Received Message!" << std::endl; - di -> print(out); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - - // if discovery reply then respondif haven't already. - if (NULL != (dri = dynamic_cast (di))) - { - -/********************************************************************************* - * Shouldn't Reply to a Reply - end up with silly traffic..... - * only reply to a ping.... - * - std::list::iterator it; - if (ad_init.end() != (it = find(ad_init.begin(), ad_init.end(), - (cert *) (dri -> p)))) - { - // Should send our answer! - sendDiscoveryReply((cert *) dri -> p); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "After Reply to Reply"); - // remove from reply list. - ad_init.erase(it); - } - * - * - ********************************************************************************/ - - // add to data tree. - handleDiscoveryData(dri); - } - else if (di -> discType == 0) - { - handleDiscoveryPing(di); - sendDiscoveryReply((cert *) di -> p); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "After Reply to Ping"); - - } - delete di; - } - return nhandled; -} - -int p3disc::sendDiscoveryReply(cert *p) -{ - if (!remote_disc) - return -1; - - // So to send a discovery reply .... we need to.... - // 1) generate a list of our neighbours..... - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::sendDiscoveryReply() Generating Messages!"); - - std::list::iterator it; - // Temp variable - std::list &certlist = sroot -> getCertList(); - int good_certs = 0; - int cts = time(NULL); - - for(it = certlist.begin(); it != certlist.end(); it++) - { - // if accepted and has connected (soon) - if ((*it) -> Accepted()) - { - good_certs++; - - { - std::ostringstream out; - out << "p3disc::sendDiscoveryReply()"; - out << " Found Neighbour Cert!" << std::endl; - out << "Encoding: "<<(*it)->Name() << std::endl; - out << "Encoding(2): "<<(*it)->certificate->name << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - // Construct a message - DiscReplyItem *di = new DiscReplyItem(); - - // Fill the message - // Set Target as input cert. - di -> cid = p -> cid; - - // set the server address. - di -> laddr = (*it) -> localaddr; - di -> saddr = (*it) -> serveraddr; - - // set the timeframe since last connection. - if ((*it) -> lr_timestamp <= 0) - { - di -> receive_tr = 0; - } - else - { - di -> receive_tr = convertTDeltaToTRange(cts - (*it) -> lr_timestamp); - } - - if ((*it) -> lc_timestamp <= 0) - { - di -> connect_tr = 0; - } - else - { - di -> connect_tr = convertTDeltaToTRange(cts - (*it) -> lc_timestamp); - } - di -> discFlags = determineCertAvailabilityFlags(*it); - - // irrelevent - di -> p = NULL; - - // actually ned to copy certificate to array - // for proper cert stuff. - -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - int len = i2d_XPGP((*it) -> certificate, &(di -> certDER)); -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - int len = i2d_X509((*it) -> certificate, &(di -> certDER)); -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - if (len > 0) - { - di -> certLen = len; - std::ostringstream out; - out << "Cert Encoded(" << len << ")" << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - else - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, "Failed to Encode Cert"); - di -> certLen = 0; - } - - // Send off message - outpkts.push_back(di); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, "Sent DI Message"); - } - else - { - std::ostringstream out; - out << "p3disc::sendDiscoveryReply()"; - out << "Not Sending Cert: " << std::endl; - out << (*it) -> Name() << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - } - - { - std::ostringstream out; - out << "p3disc::sendDiscoveryReply()"; - out << "Found " << good_certs << " Certs" << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - return 1; -} - - -int p3disc::handleDiscoveryPing(DiscItem *di) -{ - std::list::iterator it; - - // as already connected.... certificate available. - cert *c = (cert *) di -> p; - - if (c == NULL) - return -1; - - { - std::ostringstream out; - out << "p3disc::handleDiscoveryPing()" << std::endl; - di -> print(out); - out << "RECEIVED Self Describing DiscItem!"; - out << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - // The first check is whether this packet came from - // the cert in the reply. - - // Local address is always right! - // No User control anyway! - c -> localaddr = di -> laddr; - - // The Rest of this should only be set - // if we are in autoconnect mode..... - // else should be done manually. - // ----> Think we should do this always (is disc from then) - /**************************** - if (!(c -> Manual())) - ****************************/ - { - - // if the connect addr isn't valid. - if (!isValidNet(&(c -> lastaddr.sin_addr))) - { - // set it all - c -> serveraddr = di -> saddr; - pqioutput(PQL_WARNING, pqidisczone, - "lastaddr !Valid -> serveraddr=di->saddr"); - } - // if the connect addr == dispkt.local - else if (0 == inaddr_cmp(c -> lastaddr, di -> laddr)) - { - // set it all - c -> serveraddr = di -> saddr; - c -> Local(true); - pqioutput(PQL_WARNING, pqidisczone, - "lastaddr=di->laddr -> Local & serveraddr=di->saddr"); - } - else if (0 == inaddr_cmp(c -> lastaddr, di -> saddr)) - { - pqioutput(PQL_WARNING, pqidisczone, - "lastaddr=di->saddr -> !Local & serveraddr=di->saddr"); - c -> serveraddr = di -> saddr; - c -> Local(false); - } - else - { - pqioutput(PQL_WARNING, pqidisczone, - "lastaddr!=(di->laddr|di->saddr) -> !Local,serveraddr left"); - c -> Local(false); - } - - updateCertAvailabilityFlags(c, di->discFlags); - - } - /**************************** - else - { - pqioutput(PQL_WARNING, pqidisczone, - "peer is Manual -> leaving server settings"); - if (0 == inaddr_cmp(c -> lastaddr, di -> laddr)) - { - pqioutput(PQL_WARNING, pqidisczone, - "c->lastaddr=di->laddr -> local"); - c -> Local(true); - } - else - { - pqioutput(PQL_WARNING, pqidisczone, - "c->lastaddr!=di->laddr -> !local"); - c -> Local(false); - } - - } - ****************************/ - - // Now add it into the system. - // check if it exists already...... - for(it = neighbours.begin(); it != neighbours.end(); it++) - { - cert *c2 = (cert *) (*it) -> id; -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - if ((c2 != NULL) && (0 == XPGP_cmp( - c -> certificate, c2 -> certificate))) -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - if ((c2 != NULL) && (0 == X509_cmp( - c -> certificate, c2 -> certificate))) -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - { - // matching.... - // update it....; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, "Updating Certificate (AN)!"); - - (*it)-> local = c -> Local(); - updateAutoServer((*it), di); - - /* now look through the neighbours_of */ - std::list::iterator nit; - for(nit = ((*it) -> neighbour_of).begin(); - nit != ((*it) -> neighbour_of).end(); nit++) - { - - /* check if we already have a autoserver.... */ - if ((*it)->id == (*nit)->id) - { - /* we already have one */ - // update it....; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "Updating Certificate (AS)!"); - - updateAutoServer(*nit, di); - return 0; - } - } - - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "Adding Certificate (AS)!"); - - /* if we get here, we need to add an autoserver */ - autoserver *as = new autoserver(); - as -> id = c; - updateAutoServer(as, di); - (*it) -> neighbour_of.push_back(as); - - return 1; - } - } - - // if get here must add a autoneighbour + an autoserver. - - autoneighbour *an = new autoneighbour(); - an -> id = c; - an -> local = c -> Local(); - updateAutoServer(an, di); - - // add autoserver to an. - autoserver *as = new autoserver(); - as -> id = c; - updateAutoServer(as, di); - - an -> neighbour_of.push_back(as); - neighbours.push_back(an); - - return 1; -} - - -int p3disc::handleDiscoveryData(DiscReplyItem *di) -{ - std::list::iterator it; - - { - std::ostringstream out; - out << "p3disc::handleDiscoveryData()" << std::endl; - di -> print(out); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - /* WIN/LINUX Difference. - */ -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ -#ifndef WINDOWS_SYS // ie UNIX - const unsigned char *certptr = di -> certDER; -#else - unsigned char *certptr = di -> certDER; -#endif -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ - - - // load up the certificate..... -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - - XPGP *tmp = NULL; - XPGP *xpgp = d2i_XPGP(&tmp, (unsigned char **) &certptr, di -> certLen); - if (xpgp == NULL) - return -1; - { - std::ostringstream out; - out << "p3disc::handleDiscoveryData()" << std::endl; - out << "certificate name: " << xpgp -> name << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - // if a duplicate with ad/or sslroot; - cert *c = sroot -> makeCertificateXPGP(xpgp); - if (c == NULL) - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "Failed to Create Certificate"); - // delete the cert. - XPGP_free(xpgp); - return -1; - } - - -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - - X509 *tmp = NULL; - X509 *x509 = d2i_X509(&tmp, &certptr, di -> certLen); - if (x509 == NULL) - return -1; - { - std::ostringstream out; - out << "p3disc::handleDiscoveryData()" << std::endl; - out << "certificate name: " << x509 -> name << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - // if a duplicate with ad/or sslroot; - cert *c = sroot -> makeCertificate(x509); - if (c == NULL) - { - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "Failed to Create Certificate"); - // delete the cert. - X509_free(x509); - return -1; - } - -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - - - // So have new/existing cert; - // check if it exists already...... - for(it = neighbours.begin(); it != neighbours.end(); it++) - { - cert *c2 = (cert *) (*it) -> id; - -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - if ((c2 != NULL) && (0 == XPGP_cmp( - c -> certificate, c2 -> certificate))) -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - if ((c2 != NULL) && (0 == X509_cmp( - c -> certificate, c2 -> certificate))) -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - { - // matching.... check neighbours of.... - // for the source of the message. - - std::list::iterator nit; - for(nit = ((*it) -> neighbour_of).begin(); - nit != ((*it) -> neighbour_of).end(); nit++) - { -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - if (0 == XPGP_cmp( -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - if (0 == X509_cmp( -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - ((cert *) (*nit) -> id) -> certificate, - ((cert *) (di -> p)) -> certificate)) - { - - // update it....; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "Updating Certificate!"); - - updateAutoServer(*nit, di); - - return 0; - } - } - - // if we get to here - add neighbour of info. - autoserver *as = new autoserver(); - as -> id = (cert *) (di -> p); - - // add in some more ....as -> addr = (di -> ); - - updateAutoServer(as, di); - - (*it) -> neighbour_of.push_back(as); - - return 1; - } - } - - // if get here must add a autoneighbour + autoserver. - - { - std::ostringstream out; - out << "p3disc::handleDiscoveryData()" << std::endl; - out << "Adding New AutoNeighbour:" << c -> Name() << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - autoneighbour *an = new autoneighbour(); - an -> id = c; - // initial guess. - an -> local_addr = di -> laddr; - an -> server_addr = di -> saddr; - an -> local = false; - - autoserver *as = new autoserver(); - as -> id = (cert *) di -> p; - - updateAutoServer(as, di); - - an -> neighbour_of.push_back(as); - - neighbours.push_back(an); - - return 1; -} - -int p3disc::collectCerts() -{ - - // First get any extras from the CollectedCerts Queue. - // if the cert matches an existing one.... update + discard - // else add in.... - - std::list::iterator it; - std::list::iterator it2; - - cert *nc; - while(NULL != (nc = sroot -> getCollectedCert())) - { - // check for matching certs. - bool found = false; - - { - std::ostringstream out; - out << "p3disc::collectCert: " << std::endl; - out << "Name: " << nc -> Name() << std::endl; - out << "CN: " << nc -> certificate -> name << std::endl; - - out << " From: "; - out << inet_ntoa(nc -> lastaddr.sin_addr); - out << ":" << ntohs(nc -> lastaddr.sin_port) << std::endl; - out << " Local: "; - out << inet_ntoa(nc -> localaddr.sin_addr); - out << ":" << ntohs(nc -> localaddr.sin_port) << std::endl; - out << " Server: "; - out << inet_ntoa(nc -> serveraddr.sin_addr); - out << ":" << ntohs(nc -> serveraddr.sin_port) << std::endl; - out << " Listen TS:"; - out << nc -> lr_timestamp << " "; - out << "Connect TR:"; - out << nc -> lc_timestamp << std::endl; - out << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - } - - - for(it = neighbours.begin(); (!found) && (it != neighbours.end()); it++) - { - cert *c = (cert *) ((*it) -> id); -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - if ((c != NULL) && - (0 == XPGP_cmp(c -> certificate, nc -> certificate))) -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - if ((c != NULL) && - (0 == X509_cmp(c -> certificate, nc -> certificate))) -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - { - /* addresses handled already .... - * by sslroot. (more intelligent decisions). - * update timestamps so we don't overwrite - * the uptodate cert data. - */ - - found = true; - if ((nc -> lc_timestamp > 0) && - ((unsigned) nc -> lc_timestamp > (*it) -> c_ts)) - { - // connect.... timestamp - (*it) -> connect = true; - (*it) -> c_ts = nc -> lc_timestamp; - // don't make this decision here. - //(*it) -> server_addr = nc -> lastaddr; - } - - if ((nc -> lr_timestamp > 0) && - ((unsigned) nc -> lr_timestamp > (*it) -> l_ts)) - { - // received.... timestamp - (*it) -> listen = true; - (*it) -> l_ts = nc -> lr_timestamp; - } - - if ((c != nc) || - (c -> certificate != nc -> certificate)) - { - std::ostringstream out; - out << "Warning Dup/Diff Mem "; - out << " Found in p3Disc!"; - out << std::endl; - pqioutput(PQL_ALERT, pqidisczone, out.str()); - exit(1); - } - } - } - if (!found) - { - // add into the list..... - autoneighbour *an = new autoneighbour(); - an -> id = nc; - - // initial guess. - an -> local_addr = nc -> localaddr; - an -> server_addr = nc -> serveraddr; - an -> local = false; - - if (nc -> lc_timestamp > 0) - { - an -> c_ts = nc -> lc_timestamp; - an -> connect = true; - } - else - { - an -> c_ts = 0; - an -> connect = false; - } - - if (nc -> lr_timestamp > 0) - { - an -> l_ts = nc -> lr_timestamp; - an -> listen = true; - } - else - { - an -> l_ts = 0; - an -> listen = false; - } - - neighbours.push_back(an); - } - } - return 1; -} - -int p3disc::distillData() -{ - // This transforms the autoneighbour tree into - // a list of certificates with the best guess settings. - - // get any extra. from sslroot. - collectCerts(); - - discovered.clear(); - - std::ostringstream out; - out << "p3disc::distillData()" << std::endl; - - std::list::iterator it; - std::list::iterator it2; - std::list::iterator nit; - cert *own = sroot -> getOwnCert(); - - for(it = neighbours.begin(); it != neighbours.end(); it++) - { - /* for the moment this is going to be a simplistic - * (and non-fault tolerent design).... - * we will take the most up-to-date values.... from the friends of neighbours. - * - * if these are more up-to-date than both the - * (1) neighbour (*it) and - * (2) the actual certificate and - * (3) we are not connected... then - * - * (a) we update the addresses and timestamps on the neighbour. - * (b) addresses on the certificate. - * - * Therefore - * cert has (1) our connect times, (2) best guess server. - * neighbour has uptodate times/servers from last distill. - * - * NOTE this requires a better algorithm. - * - */ - - unsigned int mr_connect = 0; - unsigned int mr_listen = 0; - - unsigned int mr_both = 0; /* connect or receive */ - /* three fields below match most recent (of either) */ - struct sockaddr_in mr_server; - struct sockaddr_in mr_local; - unsigned int mr_flags = 0; - - /* if we find a neighbour_of, which is the same cert. - * then we have the definitive answer already - * (and it has been installed) - */ - - bool haveDefinitive = false; - - cert *c = (cert *) (*it) -> id; - for(nit = ((*it) -> neighbour_of).begin(); - nit != ((*it) -> neighbour_of).end(); nit++) - { - out << "\tDistill Connected via: "; - if ((*nit) -> id != NULL) - { - out << ((*nit) ->id) -> Name(); - } - out << std::endl; - out << "\t\tServer: "; - out << inet_ntoa((*nit)->server_addr.sin_addr); - out << ":" << ntohs((*nit)->server_addr.sin_port); - out << std::endl; - if ((*nit)->id == (*it)->id) - { - haveDefinitive = true; - out << "\t\tIs Definitive Answer!"; - out << std::endl; - } - - if ((*nit) -> listen) - { - if ((*nit)->l_ts > mr_listen) - { - mr_listen = (*nit)->l_ts; - if (mr_listen > mr_both) - { - mr_both = mr_listen; - mr_server = (*nit) -> server_addr; - mr_local = (*nit) -> local_addr; - mr_flags = (*nit) -> discFlags; - } - } - } - - if ((*nit) -> connect) - { - if ((*nit) -> c_ts > mr_connect) - { - mr_connect = (*nit)->c_ts; - if (mr_connect > mr_both) - { - mr_both = mr_connect; - mr_server = (*nit) -> server_addr; - mr_local = (*nit) -> local_addr; - mr_flags = (*nit) -> discFlags; - } - } - } - } - - if ((c == own) || (haveDefinitive)) - { - out << c -> Name(); - out << ": Is Own or Definitive: no Update..."; - out << std::endl; - - discovered.push_back(c); - continue; - } - - if ((mr_both > (*it)-> c_ts) && (mr_both > (*it)-> l_ts)) - { - (*it) -> server_addr = mr_server; - (*it) -> local_addr = mr_local; - (*it) -> discFlags = mr_flags; - - } - - /* now we can check against (*it) */ - if ((!(*it)->listen) || ((*it)-> l_ts < mr_listen)) - { - (*it) -> listen = true; - (*it)-> l_ts = mr_listen; - } - - if ((!(*it)->connect) || ((*it)-> c_ts < mr_connect)) - { - (*it) -> connect = true; - (*it)-> c_ts = mr_connect; - } - - /* XXX fixme ***/ - // Finally we can update the certificate, if auto - // is selected.... or not in use. - if (!(c -> Connected())) - { - out << "Checking: " << c -> Name() << std::endl; - - // if empty local - if (0 == inaddr_cmp(c -> localaddr, INADDR_ANY)) - { - out << "\tUpdating NULL Local Addr:" << std::endl; - out << "\t\tOld: "; - out << inet_ntoa(c->localaddr.sin_addr); - out << ":" << ntohs(c->localaddr.sin_port); - c -> localaddr = (*it) -> local_addr; - out << "\t\tNew: "; - out << inet_ntoa(c->localaddr.sin_addr); - out << ":" << ntohs(c->localaddr.sin_port); - } - - // if empty server ..... - if (0 == inaddr_cmp(c -> serveraddr, INADDR_ANY)) - { - out << "\tUpdating NULL Serv Addr:" << std::endl; - out << "\t\tOld: "; - out << inet_ntoa(c->serveraddr.sin_addr); - out << ":" << ntohs(c->serveraddr.sin_port); - c -> serveraddr = (*it) -> server_addr; - out << "\t\tNew: "; - out << inet_ntoa(c->serveraddr.sin_addr); - out << ":" << ntohs(c->serveraddr.sin_port); - } - // if local (second as should catch empty) - else if ((0 == inaddr_cmp((*it) -> server_addr, - c -> localaddr))) - //&& (inaddr_local(c -> localaddr)) - { - out << "\tMaking Local..." << std::endl; - c -> Local(true); - } - - // Finally the key update .... - // check only against the latest data.... - - if (mr_both) - { - // - unsigned int cert_both = c -> lc_timestamp; - if (cert_both < (unsigned) c -> lr_timestamp) - { - cert_both = c -> lr_timestamp; - } - - int log_delta = -1; /* invalid log */ - if (mr_both > cert_both) - { - log_delta = (int) log10((double) (mr_both - cert_both)); - } - - /* if a peer has connected more recently than us */ - if (log_delta > 3) // or > 10000 (secs), or ~3 hours. - { - out << "\tUpdating OLD Addresses:" << std::endl; - out << "\t\tOld Local: "; - out << inet_ntoa(c->serveraddr.sin_addr); - out << ":" << ntohs(c->serveraddr.sin_port); - out << std::endl; - out << "\t\tOld Server: "; - out << inet_ntoa(c->serveraddr.sin_addr); - out << ":" << ntohs(c->serveraddr.sin_port); - out << std::endl; - if (c->Firewalled()) - { - out << "\t\tFireWalled/"; - } - else - { - out << "\t\tNot FireWalled/"; - } - if (c->Forwarded()) - { - out << "Forwarded"; - } - else - { - out << "Not Forwarded"; - } - out << std::endl; - - if (0!=inaddr_cmp(mr_server, INADDR_ANY)) - { - c -> serveraddr = mr_server; - } - - if (0!=inaddr_cmp(mr_local, INADDR_ANY)) - { - c -> localaddr = mr_local; - } - - updateCertAvailabilityFlags(c, mr_flags); - - out << "\t\tNew: "; - out << inet_ntoa(c->serveraddr.sin_addr); - out << ":" << ntohs(c->serveraddr.sin_port); - out << "\t\tNew Local: "; - out << inet_ntoa(c->serveraddr.sin_addr); - out << ":" << ntohs(c->serveraddr.sin_port); - out << std::endl; - out << "\t\tNew Server: "; - out << inet_ntoa(c->serveraddr.sin_addr); - out << ":" << ntohs(c->serveraddr.sin_port); - out << std::endl; - if (c->Firewalled()) - { - out << "\t\tFireWalled/"; - } - else - { - out << "\t\tNot FireWalled/"; - } - if (c->Forwarded()) - { - out << "Forwarded"; - } - else - { - out << "Not Forwarded"; - } - out << std::endl; - } - } - } - discovered.push_back(c); - } - pqioutput(PQL_DEBUG_BASIC, pqidisczone, out.str()); - idServers(); - return 1; -} - -std::list &p3disc::getDiscovered() -{ - return discovered; -} - -static const std::string pqi_adflags("PQI_ADFLAGS"); - -int p3disc::save_configuration() -{ - if (sroot == NULL) - return -1; - - std::string localflags; - if (local_disc) - localflags += "L"; - if (remote_disc) - localflags += "R"; - sroot -> setSetting(pqi_adflags, localflags); - return 1; -} - - -// load configuration from sslcert -> owncert() -// instead of from the configuration files. - -int p3disc::load_configuration() -{ - unsigned int i = 0; - - if (sroot == NULL) - return -1; - - Person *p = sroot -> getOwnCert(); - if (p == NULL) - return -1; - laddr = p -> localaddr; - //laddr.sin_family = AF_INET; - - saddr = p -> serveraddr; - local_firewalled = p -> Firewalled(); - local_forwarded = p -> Forwarded(); - - std::string localflags = sroot -> getSetting(pqi_adflags); - // initially drop out gracefully. - if (localflags.length() == 0) - return 1; - if (i < localflags.length()) - if (local_disc = ('L' == localflags[i])) - i++; - if (i < localflags.length()) - if (remote_disc = ('R' == localflags[i])) - i++; - // temp turn on! - local_disc = false; // true; - remote_disc = true; - return 1; -} - - -std::list p3disc::potentialproxy(cert *target) -{ - std::list certs; - // search the discovery tree for proxies for target. - - std::list::iterator it; - std::list::iterator nit; - - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::potentialproxy()"); - - for(it = neighbours.begin(); it != neighbours.end(); it++) - { - cert *c = (cert *) (*it) -> id; - if (c == target) - { - // found target. - for(nit = ((*it) -> neighbour_of).begin(); - nit != ((*it) -> neighbour_of).end(); nit++) - { - /* can't use target as proxy */ - cert *pp = (cert *) (*nit)->id; - if ((pp -> Connected()) && (target != pp)) - { - std::ostringstream out; - out << "Potential Proxy: "; - out << pp -> Name(); - certs.push_back(pp); - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - out.str()); - } - } - - return certs; - } - } - - pqioutput(PQL_DEBUG_BASIC, pqidisczone, - "p3disc::potentialproxy() No proxies found"); - // empty list. - return certs; -} - -std::list p3disc::requestStunServers() -{ - - /* loop through all the possibilities - * - * find the ones which aren't firewalled. - * - * get their addresses. - */ - cert *own = sroot -> getOwnCert(); - - std::list stunList; - - std::list::iterator it; - for(it = neighbours.begin(); it != neighbours.end(); it++) - { - cert *c = (cert *) (*it) -> id; - - /* if flags are correct, and the address looks - * valid. - */ - -/* switch on Local Stun for testing */ -/* - * #define STUN_ALLOW_LOCAL_NET 1 - */ - - -#ifdef STUN_ALLOW_LOCAL_NET - bool isExtern = true; -#else - bool isExtern = (!c->Firewalled()) || - (c->Firewalled() && c->Forwarded()); -#endif - - if (isExtern) - { - // second level of checks. - // if we will connect, and haven't -> they are probably - // offline. - if (c->Accepted() && (!c->Connected())) - { - std::ostringstream out; - out << "Offline Friend: "; - out << c -> Name(); - out << " not available for Stun"; - pqioutput(PQL_DEBUG_ALERT, pqidisczone, out.str()); - isExtern = false; - } - - // and address looks good. - // - // and not in our subnet (external to us) - } - - - if (isExtern) - { - std::ostringstream out; - out << "Potential Stun Server: "; - out << c -> Name(); - out << std::endl; - out << " ServerAddr: " << inet_ntoa(c->serveraddr.sin_addr); - out << " : " << ntohs(c->serveraddr.sin_port); - out << std::endl; - out << " LocalAddr: " << inet_ntoa(c->localaddr.sin_addr); - out << " : " << ntohs(c->localaddr.sin_port); - out << std::endl; - -#ifdef STUN_ALLOW_LOCAL_NET - if (isValidNet(&(c->serveraddr.sin_addr)) && - (!sameNet(&(own->serveraddr.sin_addr), &(c->serveraddr.sin_addr)))) -#else - if ((isValidNet(&(c->serveraddr.sin_addr))) && - (!isPrivateNet(&(c->serveraddr.sin_addr))) && - (!sameNet(&(own->localaddr.sin_addr), &(c->serveraddr.sin_addr))) && - (!sameNet(&(own->serveraddr.sin_addr), &(c->serveraddr.sin_addr)))) -#endif - { - out << " -- Chose Server Address"; - out << std::endl; - stunList.push_back(c->serveraddr); - } -#ifdef STUN_ALLOW_LOCAL_NET - else if (isValidNet(&(c->localaddr.sin_addr))) -#else - else if ((!c->Firewalled()) && - (isValidNet(&(c->localaddr.sin_addr))) && - (!isPrivateNet(&(c->localaddr.sin_addr))) && - (!sameNet(&(own->localaddr.sin_addr), &(c->localaddr.sin_addr)))) -#endif - { - out << " -- Chose Local Address"; - out << std::endl; - stunList.push_back(c->localaddr); - } - else - { - out << "<=> Invalid / Private Addresses"; - out << std::endl; - } - pqioutput(PQL_DEBUG_ALERT, pqidisczone, out.str()); - } - else - { - std::ostringstream out; - out << "Non-Stun Neighbour: "; - out << c -> Name(); - pqioutput(PQL_DEBUG_ALERT, pqidisczone, out.str()); - } - } - - return stunList; -} - - - - - -// tdelta -> trange. -// -inf...<0 0 (invalid) -// 0.. <9 1 -// 9...<99 2 -// 99...<999 3 -// 999...<9999 4 -// etc... - -int convertTDeltaToTRange(double tdelta) -{ - if (tdelta < 0) - return 0; - int trange = 1 + (int) log10(tdelta + 1.0); - return trange; - -} - -// trange -> tdelta -// -inf...0 -1 (invalid) -// 1 8 -// 2 98 -// 3 998 -// 4 9998 -// etc... - -int convertTRangeToTDelta(int trange) -{ - if (trange <= 0) - return -1; - - return (int) (pow(10.0, trange) - 1.5); // (int) xxx98.5 -> xxx98 -} - -// fn which updates: connect, c_ts, -// listen, l_ts, -// local_addr, server_addr, -// and discFlags. -int updateAutoServer(autoserver *as, DiscItem *di) -{ - int cts = time(NULL); - - - as->listen = (di->receive_tr != 0); - as->connect= (di->connect_tr != 0); - - /* convert [r|c]_tf to timestamps.... - * - * Conversion to a _tf.... - * - * - * */ - if (as->listen) - { - as->l_ts = cts - convertTRangeToTDelta(di->receive_tr); - } - - if (as->connect) - { - as->c_ts = cts - convertTRangeToTDelta(di->connect_tr); - - } - as->local_addr = di->laddr; - as->server_addr = di->saddr; - as->discFlags = di->discFlags; - - return 1; -} - - -static const int PQI_DISC_FLAGS_FIREWALLED = 0x0001; -static const int PQI_DISC_FLAGS_FORWARDED = 0x0002; -static const int PQI_DISC_FLAGS_LOCAL = 0x0004; - -int updateCertAvailabilityFlags(cert *c, unsigned long discFlags) -{ - if (c) - { - c->Firewalled(discFlags & PQI_DISC_FLAGS_FIREWALLED); - c->Forwarded(discFlags & PQI_DISC_FLAGS_FORWARDED); - - if (discFlags & PQI_DISC_FLAGS_FIREWALLED) - { - pqioutput(PQL_WARNING, pqidisczone, - "updateCertAvailabilityFlags() Setting Firewalled Flag = true"); - } - else - { - pqioutput(PQL_WARNING, pqidisczone, - "updateCertAvailabilityFlags() Setting Firewalled Flag = false"); - } - - if (discFlags & PQI_DISC_FLAGS_FORWARDED) - { - pqioutput(PQL_WARNING, pqidisczone, - "updateCertAvailabilityFlags() Setting Forwarded Flag = true"); - } - else - { - pqioutput(PQL_WARNING, pqidisczone, - "updateCertAvailabilityFlags() Setting Forwarded Flag = false"); - } - - return 1; - } - return 0; -} - - -unsigned long determineCertAvailabilityFlags(cert *c) -{ - unsigned long flags = 0; - if (c->Firewalled()) - { - flags |= PQI_DISC_FLAGS_FIREWALLED; - } - - if (c->Forwarded()) - { - flags |= PQI_DISC_FLAGS_FORWARDED; - } - - if (c->Local()) - { - flags |= PQI_DISC_FLAGS_LOCAL; - } - - return flags; -} - diff --git a/libretroshare/src/pqi/p3disc.h b/libretroshare/src/pqi/p3disc.h deleted file mode 100644 index 9ca8ca538..000000000 --- a/libretroshare/src/pqi/p3disc.h +++ /dev/null @@ -1,186 +0,0 @@ -/* - * "$Id: p3disc.h,v 1.11 2007-02-18 21:46:49 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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". - * - */ - - - -#ifndef MRK_PQI_AUTODISC_H -#define MRK_PQI_AUTODISC_H - - -// The AutoDiscovery Class - -#include -#include - -// system specific network headers -#include "pqi/pqinetwork.h" - -#include "pqi/pqi.h" - -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - -#include "pqi/xpgpcert.h" - -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - -#include "pqi/sslcert.h" - -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - - -#include "pqi/discItem.h" -#include "pqi/pqitunnel.h" - -class autoserver -{ - public: - autoserver() - :id(NULL), ca(NULL), connect(false), c_ts(0), - listen(false), l_ts(0), discFlags(0) { return;} - - Person *id; - Person *ca; - bool connect; - unsigned int c_ts; // this is connect_tf converted to timestamp, 0 invalid. - - bool listen; - unsigned int l_ts; // this is receive_tf converted to timestamp, 0 invalid. - - struct sockaddr_in local_addr; - struct sockaddr_in server_addr; - unsigned long discFlags; -}; - - -class autoneighbour: public autoserver -{ - public: - autoneighbour() - :autoserver(), local(false), active(false) {} - - bool local; - bool active; // meaning in ssl's list. - std::list neighbour_of; - -}; - - -class p3disc: public PQTunnelService -{ - public: - bool local_disc; - bool remote_disc; - //sslroot *sslbase; - - p3disc(sslroot *r); -virtual ~p3disc(); - - // PQTunnelService functions. -virtual int receive(PQTunnel *); -virtual PQTunnel *send(); -virtual PQTunnel *getObj(); - -virtual int tick(); - - // doesn't use the init functions. -virtual int receive(PQTunnelInit *i) { delete i; return 1; } -virtual PQTunnelInit *sendInit() { return NULL; } -virtual PQTunnelInit *getObjInit() { return NULL; } - - // End of PQTunnelService functions. - // - // For Proxy Information. -std::list requestStunServers(); -std::list potentialproxy(cert *target); - - // load and save configuration to sslroot. - int save_configuration(); - int load_configuration(); - - int ts_lastcheck; - - int idServers(); - - // Handle Local Discovery. - int localListen(); - int localSetup(); - - int lsock; // local discovery socket. - struct sockaddr_in laddr; // local addr - struct sockaddr_in baddr; // local broadcast addr. - struct sockaddr_in saddr; // pqi ssl server addr. - - // bonus configuration flags. - bool local_firewalled; - bool local_forwarded; - - - // local message construction/destruction. - void *ldata; - int ldlen; - int ldlenmax; - - - bool std_port; // if we have bound to default. - int ts_nextlp; // -1 for never (if on default) - - // helper functions. - int setLocalAddress(struct sockaddr_in srvaddr); - int determineLocalNetAddr(); - int setupLocalPacket(int type, struct sockaddr_in *home, - struct sockaddr_in *server); - int localPing(struct sockaddr_in); - int localReply(struct sockaddr_in); - int addLocalNeighbour(struct sockaddr_in*, struct sockaddr_in*); - - // remote discovery function. - int newRequests(); - int handleReplies(); - - int handleDiscoveryData(DiscReplyItem *di); - int handleDiscoveryPing(DiscItem *di); - int sendDiscoveryReply(cert *); - int collectCerts(); - int distillData(); - - //cert *checkDuplicateX509(X509 *x509); - std::list &getDiscovered(); - - // Main Storage - std::list neighbours; - std::list ad_init; - - std::list discovered; - sslroot *sroot; - - // Storage for incoming/outgoing. - std::list inpkts; - std::list outpkts; -}; - -#endif // MRK_PQI_AUTODISC_H diff --git a/libretroshare/src/pqi/p3loopback.cc b/libretroshare/src/pqi/p3loopback.cc deleted file mode 100644 index 08c3d34e1..000000000 --- a/libretroshare/src/pqi/p3loopback.cc +++ /dev/null @@ -1,341 +0,0 @@ -/* - * "$Id: p3loopback.cc,v 1.3 2007-02-18 21:46:49 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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 "pqi/pqi.h" -#include "pqi/p3loopback.h" - -// Test Interface (LoopBack) - - -p3loopback::~p3loopback() -{ - // should clean up lists ... - // delete all random search items... - // fix later.. - return; -} - -int p3loopback::Search(SearchItem *si) -{ - // no more copy - just use. - - // tag as new. - si -> flags = 1; - - // add to requested searches. - searches[si -> sid] = si; - - return 1; -} - -int p3loopback::CancelSearch(SearchItem *item) -{ - std::map::iterator it; - std::list::iterator it2; - - unsigned int sid = item -> sid; - - // Quick Sanity Check. - it = searches.find(sid); - if (it == searches.end()) - { - std::cerr << "p3loopback::CancelSearch(" << sid; - std::cerr << ") Search Not Found!" << std::endl; - return -2; - } - - // found search delete. - SearchItem *osi = it -> second; - searches.erase(it); - - // find anything matching in the list. - for(it2 = searchResults.begin(); it2 != searchResults.end();) - { - if ((*it2) -> sid == sid) // erase - { - PQFileItem *osr = (*it2); - it2 = searchResults.erase(it2); - delete osr; - } - else - { - it2++; - } - } - - cancelledSearches.push_back(osi); - // clean up input. - delete item; - return 1; -} - - -PQFileItem * p3loopback::GetSearchResult() -{ - std::list::iterator it; - - it = searchResults.begin(); - if (it == searchResults.end()) // empty list - { - return NULL; - } - PQFileItem *osr = (*it); - searchResults.erase(it); - - // set as local... - osr -> flags |= PQI_ITEM_FLAG_LOCAL; - return osr; -} - -int p3loopback::GetFile(PQFileItem *item, std::ostream &in) -{ - // save a stream .... - // send on the request. - //fixme("p3loopback::GetFile()", 1); - return 1; -} - -int p3loopback::CancelFile(PQFileItem *item) -{ - // close down stream (if we created it) - // send on request. - //fixme("p3loopback::CancelFile()", 1); - return 1; -} - - -// Remote Requests. - -SearchItem *p3loopback::RequestedSearch() -{ - std::map::iterator it; - SearchItem *ns; - - for(it = searches.begin(); it != searches.end(); it++) - { - // check if new... - if ((it -> second) -> flags == 1) - { - // generate new sid number. - int rsid = getPQIsearchId(); - int sid = (it -> second) -> sid; - - // setup translation stuff. - sid2rsid[sid] = rsid; - - ns = new SearchItem(); - ns -> copy(it -> second); - (it -> second) -> flags = 0; - - ns -> sid = rsid; - - return ns; - } - } - return NULL; -} - - - -SearchItem *p3loopback::CancelledSearch() -{ - std::list::iterator it; - std::map::iterator mit; - - it = cancelledSearches.begin(); - if (it == cancelledSearches.end()) // empty list - { - return NULL; - } - // else copy into structure. - SearchItem *ncs = (*it); - cancelledSearches.erase(it); - - // get from mapping. - mit = sid2rsid.find(ncs -> sid); - - if (mit == sid2rsid.end()) // error - { - std::cerr << "Error No Mapping (Cancelling the Cancel)!"; - std::cerr << "But Still deleting item"; - std::cerr << std::endl; - delete ncs; - return NULL; - } - else - { - ncs -> sid = mit -> second; - sid2rsid.erase(mit); - } - - return ncs; -} - -int p3loopback::SendSearchResult(PQFileItem *item) -{ - //std::list >::iterator it; - std::map::iterator it; - - int rsid = item -> sid; - int sid = 0; - - // get from mapping. - for(it = sid2rsid.begin(); ((it != sid2rsid.end()) && (sid == 0)); it++) - { - if (rsid == (it -> second)) - { - sid = it -> first; - } - } - - // if no mapping. - if (sid == 0) - { - std::cerr << "SendSearchResult::Error No Mapping (Removing Item)!"; - std::cerr << std::endl; - delete item; - return 0; - } - - // have mapping.... translate. - item -> sid = sid; - - // stick on the queue. - searchResults.push_back(item); - - return 1; -} - -PQFileItem *p3loopback::RequestedFile() -{ - // check for searches with new tag... - // if none return 0; - // copy data from first. - - // mark old. - //fixme("p3loopback::RequestedFile()", 1); - return NULL; -} - - -PQFileItem *p3loopback::CancelledFile() -{ - // check for cancelled files. - // if none return 0; - // copy data from first. - - // remove/cleanup - //fixme("p3loopback::CancelledFile()", 1); - return NULL; -} - -int p3loopback::SendFile(PQFileItem *, std::istream &out) -{ - // get data... - // attach streams..... - // - //fixme("p3loopback::SendFile()", 1); - return 1; -} - - -// -//// control interface. -//int p3loopback::RequestInfo(InfoItem *item) -//{ -// switch(item -> querytype) -// { -// case PQI_II_QUERYTYPE_TAG: -// item -> answer = "<++Local++>"; -// return 1; -// break; -// -// case PQI_II_QUERYTYPE_CLASS: -// item -> answer = "p3loopback"; -// return 1; -// break; -// -// default: -// return 0; -// break; -// } -// return -1; -// -// return 0; -//} -// -//int p3loopback::doCommand(CommandItem *) -//{ -// //fixme("p3loopback::doCommand()", 1); -// return 0; -//} -// - -// // chat interface. -int p3loopback::SendMsg(ChatItem *item) -{ - //fixme("p3loopback::SendMsg()", 1); - return 0; -} - -ChatItem *p3loopback::GetMsg() -{ - //fixme("p3loopback::GetMsg()", 1); - return NULL; -} - -int p3loopback::SendOtherPQItem(PQItem *item) -{ - other.push_back(item); - return 1; -} - -PQItem *p3loopback::GetOtherPQItem() -{ - if (other.size() != 0) - { - PQItem *i = other.front(); - other.pop_front(); - return i; - } - return NULL; -} - - -// // PQI interface. -int p3loopback::tick() -{ - //fixme("p3loopback::tick()", 1); - return 0; -} - -int p3loopback::status() -{ - //fixme("p3loopback::status()", 1); - return 0; -} diff --git a/libretroshare/src/pqi/p3loopback.h b/libretroshare/src/pqi/p3loopback.h deleted file mode 100644 index d06df6bc0..000000000 --- a/libretroshare/src/pqi/p3loopback.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * "$Id: p3loopback.h,v 1.4 2007-02-18 21:46:49 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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". - * - */ - - - -#ifndef MRK_P3_LOOPBACK_HEADER -#define MRK_P3_LOOPBACK_HEADER - - -// The standard data types and the search interface. -#include "pqi/pqi.h" - -#include -#include - - -// Test Interface (LoopBack) -// public interface .... includes the whole p3interface. -class p3loopback: public P3Interface -{ -public: -virtual ~p3loopback(); - -// search Interface. -virtual int Search(SearchItem *item); -virtual int CancelSearch(SearchItem *item); -virtual PQFileItem *GetSearchResult(); - -virtual int GetFile(PQFileItem *item, std::ostream &in); -virtual int CancelFile(PQFileItem *); - -virtual SearchItem *RequestedSearch(); -virtual SearchItem *CancelledSearch(); -virtual int SendSearchResult(PQFileItem *item); - -virtual PQFileItem *RequestedFile(); -virtual PQFileItem *CancelledFile(); -virtual int SendFile(PQFileItem *, std::istream &out); - -// control interface. -//virtual int RequestInfo(InfoItem *); -//virtual int doCommand(CommandItem *); - -// chat interface. -virtual int SendMsg(ChatItem *item); -virtual ChatItem *GetMsg(); - -// PQI interface. -virtual int tick(); -virtual int status(); - -virtual int SendOtherPQItem(PQItem *); -virtual PQItem *GetOtherPQItem(); - - private: - std::list searchResults; - std::map searches; - std::map files; - std::list cancelledSearches; - std::list cancelledFiles; - std::list other; - - // remote mapping - std::map sid2rsid; -}; - - - -#endif //MRK_P3_LOOPBACK_HEADER diff --git a/libretroshare/src/pqi/p3supernode.cc b/libretroshare/src/pqi/p3supernode.cc deleted file mode 100644 index ab6c6f744..000000000 --- a/libretroshare/src/pqi/p3supernode.cc +++ /dev/null @@ -1,317 +0,0 @@ -/* - * "$Id: p3supernode.cc,v 1.3 2007-02-18 21:46:49 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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 "pqi/pqisupernode.h" - -#include -#include - -// Includes for directory creation. -#include -#include -#include -// Conflicts with FLTK def - hope they are the same. -//#include - -// for blocking signals -#include - -#include "pqi/pqidebug.h" - -void load_check_basedir(); - -// Global Pointers for the callback functions. - -// initial configuration bootstrapping... -static const std::string config_init_file = "default_cert.txt"; -static const std::string config_file = "config.rs"; -static const std::string cert_dir = "friends"; -static const std::string key_dir = "keys"; -static const std::string ca_file = "cacerts.pem"; - -static std::string config_basedir; -static std::string load_cert; -static std::string load_key; -static std::string load_cacert; - -void load_check_basedir(); - -static const char dirSeperator = '/'; // For unix. - -// standard start for unix. -int main(int argc, char **argv) -{ - // setup debugging for desired zones. - setOutputLevel(PQL_ALERT); // to show the others. - setZoneLevel(PQL_ALERT, 38422); // pqipacket. - setZoneLevel(PQL_ALERT, 96184); // pqinetwork; - setZoneLevel(PQL_ALERT, 82371); // pqiperson. - setZoneLevel(PQL_ALERT, 60478); // pqitunnel. - setZoneLevel(PQL_ALERT, 34283); // pqihandler. - setZoneLevel(PQL_ALERT, 44863); // discItems. - setZoneLevel(PQL_DEBUG_BASIC, 2482); // p3disc - setZoneLevel(PQL_ALERT, 1728); // proxy - setZoneLevel(PQL_ALERT, 1211); // sslroot. - setZoneLevel(PQL_ALERT, 37714); // pqissl. - setZoneLevel(PQL_ALERT, 8221); // pqistreamer. - setZoneLevel(PQL_ALERT, 354); // pqipersongrp. - - std::list dirs; - - short port = 7812; // default port. - bool forceLocalAddr = false; - - char inet[256] = "127.0.0.1"; - char passwd[256] = ""; - char logfname[1024] = ""; - - int c; - bool havePasswd = false; - bool haveLogFile = false; - - while((c = getopt(argc, argv,"i:p:c:s:w:l:")) != -1) - { - switch (c) - { - case 'l': - strncpy(logfname, optarg, 1024); - std::cerr << "LogFile (" << logfname; - std::cerr << ") Selected" << std::endl; - haveLogFile = true; - break; - case 'w': - strncpy(passwd, optarg, 256); - std::cerr << "Password Specified(" << passwd; - std::cerr << ") Selected" << std::endl; - havePasswd = true; - break; - case 'i': - strncpy(inet, optarg, 256); - std::cerr << "New Inet Addr(" << inet; - std::cerr << ") Selected" << std::endl; - forceLocalAddr = true; - break; - case 'p': - port = atoi(optarg); - std::cerr << "New Listening Port(" << port; - std::cerr << ") Selected" << std::endl; - break; - case 'c': - config_basedir = optarg; - std::cerr << "New Base Config Dir("; - std::cerr << config_basedir; - std::cerr << ") Selected" << std::endl; - break; - case 's': - dirs.push_back(std::string(optarg)); - std::cerr << "Adding Search Directory (" << optarg; - std::cerr << ")" << std::endl; - break; - default: - std::cerr << "Unknown Option!"; - exit(1); - } - } - - // Can only log under linux. - // set the debug file. - if (haveLogFile) - { - setDebugFile(logfname); - } - - // first check config directories, and set bootstrap values. - load_check_basedir(); - - // SWITCH off the SIGPIPE - kills process on Linux. - struct sigaction sigact; - sigact.sa_handler = SIG_IGN; - sigact.sa_flags = 0; - - if (0 == sigaction(SIGPIPE, &sigact, NULL)) - { - std::cerr << "RetroShare:: Successfully Installed"; - std::cerr << "the SIGPIPE Block" << std::endl; - } - else - { - std::cerr << "RetroShare:: Failed to Install"; - std::cerr << "the SIGPIPE Block" << std::endl; - } - - if (!havePasswd) - { - std::cerr << "main() - Fatal Error....." << std::endl; - std::cerr << "Missing Passwd!" << std::endl; - std::cerr << std::endl; - exit(1); - } - - /* determine the cert/key parameters.... - */ - load_check_basedir(); - - bool sslroot_ok = false; - sslroot *sr = getSSLRoot(); - -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - if (0 < sr -> initssl(load_cert.c_str(), load_key.c_str(), passwd)) - { - sslroot_ok = true; - } -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - if (0 < sr -> initssl(load_cert.c_str(), load_key.c_str(), - load_cacert.c_str(), passwd)) - { - sslroot_ok = true; - } -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - if (!sslroot_ok) - { - std::cerr << "main() - Fatal Error....." << std::endl; - std::cerr << "Invalid Certificate configuration!" << std::endl; - std::cerr << std::endl; - exit(1); - } - - // wait for this window to be finished.... - if(!(sr -> active())) - { - std::cerr << "main() - Fatal Error....." << std::endl; - std::cerr << "SSLRoot Failed to start" << std::endl; - std::cerr << std::endl; - exit(1); - } - - // set the directories for full configuration load. - sr -> setConfigDirs(config_basedir.c_str(), cert_dir.c_str()); - sr -> loadCertificates(config_file.c_str()); - sr -> checkNetAddress(); - - SecurityPolicy *none = secpolicy_create(); - - if (forceLocalAddr) - { - struct sockaddr_in laddr; - - laddr.sin_family = AF_INET; - laddr.sin_port = htons(port); - - // universal - laddr.sin_addr.s_addr = inet_addr(inet); - // unix specific - //inet_aton(inet, &(laddr.sin_addr)); - - cert *own = sr -> getOwnCert(); - if (own != NULL) - { - own -> localaddr = laddr; - } - } - - -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - /* can only do this in XPGP */ - sr -> superNodeMode(); -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - - pqisupernode psn(none, sr); - psn.load_config(); - - psn.run(); - - return 1; -} - - -void load_check_basedir() -{ - // get the default configuration location. - - if (config_basedir == "") - { - std::cerr << "load_check_basedir() - Fatal Error....." << std::endl; - std::cerr << "Missing config_basedir" << std::endl; - std::cerr << std::endl; - exit(1); - } - - std::string key_subdir = config_basedir + dirSeperator; - key_subdir += key_dir; - - // will catch bad files later. - - // Check for config file. - std::string initfile = config_basedir + dirSeperator; - initfile += config_init_file; - - /* setup the ca cert file */ - load_cacert = key_dir + dirSeperator; - load_cacert += ca_file; - - // open and read in the lines. - FILE *ifd = fopen(initfile.c_str(), "r"); - char path[1024]; - int i; - - if (ifd != NULL) - { - if (NULL != fgets(path, 1024, ifd)) - { - for(i = 0; (path[i] != '\0') && (path[i] != '\n'); i++); - path[i] = '\0'; - load_cert = path; - } - if (NULL != fgets(path, 1024, ifd)) - { - for(i = 0; (path[i] != '\0') && (path[i] != '\n'); i++); - path[i] = '\0'; - load_key = path; - } - fclose(ifd); - } - else - { - std::cerr << "load_check_basedir() - Fatal Error....." << std::endl; - std::cerr << "Cannot open initfile." << std::endl; - std::cerr << std::endl; - exit(1); - } - - return; -} - - - - diff --git a/libretroshare/src/pqi/pqi.cc b/libretroshare/src/pqi/pqi.cc deleted file mode 100644 index ce6cf7db9..000000000 --- a/libretroshare/src/pqi/pqi.cc +++ /dev/null @@ -1,440 +0,0 @@ -/* - * "$Id: pqi.cc,v 1.8 2007-03-21 18:45:41 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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 "pqi/pqi.h" -#include "pqi/pqi_data.h" - -#include - -// local functions. -//int pqiroute_setshift(PQItem *item, int chan); -//int pqiroute_getshift(PQItem *item); -//int pqicid_clear(ChanId *cid); -//int pqicid_copy(const ChanId *cid, ChanId *newcid); - -// Helper functions for the PQInterface. - -int fixme(char *str, int n) -{ - for(int i = 0; i < n; i++) - { - std::cerr << "FIXME:: " << str << std::endl; - } - return 1; -} - -SearchItem::SearchItem() - :PQItem(PQI_ITEM_TYPE_SEARCHITEM, PQI_SI_SUBTYPE_SEARCH), - datatype(PQI_SI_DATATYPE_TERMS) - -{ - //std::cerr << "Creating SearchItem()" << std::endl; - return; -} - -SearchItem::SearchItem(int st) - :PQItem(PQI_ITEM_TYPE_SEARCHITEM, st), datatype(PQI_SI_DATATYPE_TERMS) -{ - //std::cerr << "Creating SearchItem()" << std::endl; - return; -} - -SearchItem::~SearchItem() -{ - //std::cerr << "Deleting SearchItem()" << std::endl; - return; -} - - -// These need to be recursive. -SearchItem *SearchItem::clone() -{ - SearchItem *nsi = new SearchItem(); - nsi -> copy(this); - - return nsi; -} - -void SearchItem::copy(const SearchItem *si) -{ - // copy below. - PQItem::copy(si); - - // do elements of searchItem. - - datatype = si -> datatype; - data = si -> data; -} - -std::ostream &SearchItem::print(std::ostream &out) -{ - out << "---- SearchItem" << std::endl; - PQItem::print(out); - - out << "Search Type: " << datatype << std::endl; - out << "Search: " << data << std::endl; - return out << "----" << std::endl; -} - - - ChatItem::ChatItem() - :PQItem(PQI_ITEM_TYPE_CHATITEM, PQI_MI_SUBTYPE_CHAT) -{ - //std::cerr << "Creating ChatItem()" << std::endl; - return; -} - - ChatItem::ChatItem(int st) - :PQItem(PQI_ITEM_TYPE_CHATITEM, st) -{ - //std::cerr << "Creating ChatItem()" << std::endl; - return; -} - - -ChatItem::~ChatItem() -{ - //std::cerr << "Deleting ChatItem()" << std::endl; - return; -} - - -MsgItem::MsgItem() - :ChatItem(PQI_MI_SUBTYPE_MSG), msgflags(0), sendTime(0) -{ - //std::cerr << "Creating MsgItem()" << std::endl; - return; -} - - -MsgItem::~MsgItem() -{ - //std::cerr << "Deleting MsgItem()" << std::endl; - return; -} - - -ChatItem *ChatItem::clone() -{ - ChatItem *nsi = new ChatItem(); - nsi -> copy(this); - - return nsi; -} - -void ChatItem::copy(const ChatItem *si) -{ - // copy below. - PQItem::copy(si); - - msg = si -> msg; - - return; -} - - -std::ostream &ChatItem::print(std::ostream &out) -{ - out << "---- ChatItem" << std::endl; - PQItem::print(out); - - out << "Msg: " << msg; - out << std::endl << "----"; - return out << std::endl; -} - - -MsgItem *MsgItem::clone() -{ - MsgItem *nsi = new MsgItem(); - nsi -> copy(this); - - return nsi; -} - -void MsgItem::copy(const MsgItem *mi) -{ - // copy below. - ChatItem::copy(mi); - - msgflags = mi -> msgflags; - sendTime = mi -> sendTime; - title = mi -> title; - header = mi -> header; - files = mi -> files; - -} - -std::ostream &MsgItem::print(std::ostream &out) -{ - out << "-------- MsgItem" << std::endl; - ChatItem::print(out); - - out << "MsgFlags: " << msgflags; - out << std::endl; - out << "SendTime: " << sendTime; - out << std::endl; - out << "Title: " << title; - out << std::endl; - out << "Header: " << header; - out << std::endl; - out << "NoFiles: " << files.size(); - out << std::endl; - std::list::iterator it; - for(it = files.begin(); it != files.end(); it++) - { - out << "File: " << it->name; - out << " Size: " << it->size; - out << " Hash: " << it->hash; - out << std::endl; - } - out << std::endl << "--------"; - return out << std::endl; -} - - -//void FileItem::fillsi(SearchItem *si) -//{ -// // first copy lowlevel stuff. -// si -> PQItem::copyIDs(this); -// if (hash.length() > 5) -// { -// // use hash. -// si -> data = hash; -// si -> datatype = PQI_SI_DATATYPE_HASH; -// } -// else -// { -// si -> data = name; -// si -> datatype = PQI_SI_DATATYPE_NAME; -// } -// return; -//} - - -// This uses some helper functions for -// string matching... these -// functions are located at the end -// of this file. - -int PQFileItem::match(std::string term) -{ - std::string lowerterm = strtolower(term); - - std::string haystack = strtolower(path); - if (strstr(haystack.c_str(), lowerterm.c_str()) != NULL) - { - return 1; - } - - haystack = strtolower(name); - if (strstr(haystack.c_str(), lowerterm.c_str()) != NULL) - { - return 1; - } - return 0; -} - - - - -MediaItem::MediaItem() -{ - //std::cerr << "Creating MediaItem()" << std::endl; - return; -} - -MediaItem::~MediaItem() -{ - //std::cerr << "Deleting MediaItem()" << std::endl; - return; -} - -MediaItem *MediaItem::clone() -{ - MediaItem *nmi = new MediaItem(); - nmi -> copy(this); - return nmi; -} - - -void MediaItem::copy(const MediaItem *src) -{ - // copy fileitem stuff. - PQFileItem::copy(src); - - mimetype = src -> mimetype; - title = src -> title; - author = src -> author; - collection = src -> collection; - genre = src -> genre; - len = src -> len; - - return; -} - -int MediaItem::match(std::string term) -{ - std::string lowerterm = strtolower(term); - - std::string haystack = strtolower(title); - if (strstr(haystack.c_str(), lowerterm.c_str()) != NULL) - { - return 1; - } - - haystack = strtolower(author); - if (strstr(haystack.c_str(), lowerterm.c_str()) != NULL) - { - return 1; - } - - haystack = strtolower(collection); - if (strstr(haystack.c_str(), lowerterm.c_str()) != NULL) - { - return 1; - } - haystack = strtolower(genre); - if (strstr(haystack.c_str(), lowerterm.c_str()) != NULL) - { - return 1; - } - - return PQFileItem::match(term); -} - - -/*********************** STRING HELPERS *************************/ - -std::string strtolower(std::string in) -{ - std::string out = in; - for(int i = 0; i < (signed) out.length(); i++) - { - if (isupper(out[i])) - { - out[i] += 'a' - 'A'; - } - } - return out; -} - -std::string fileextension(std::string in) -{ - std::string out; - bool found = false; - for(int i = (signed) in.length() -1; (i >= 0) && (found == false); i--) - { - if (in[i] == '.') - { - found = true; - for(int j = i+1; j < (signed) in.length(); j++) - { - out += in[j]; - } - } - } - return strtolower(out); -} - - -std::list createtermlist(std::string in) -{ - std::list terms; - char str[1024]; - strncpy(str, in.c_str(), 1024); - str[1023] = '\0'; - - int len = strlen(str); - int i; - - int sword = 0; - for(i = 0; (i < len - 1) && (str[i] != '\0'); i++) - { - if (isspace(str[i])) - { - str[i] = '\0'; - if (i - sword > 0) - { - terms.push_back(std::string(&(str[sword]))); - //std::cerr << "Found Search Term: (" << &(str[sword]); - //std::cerr << ")" << std::endl; - } - sword = i+1; - } - } - // do the final term. - if (i - sword > 0) - { - terms.push_back(std::string(&(str[sword]))); - //std::cerr << "Found Search Term: (" << &(str[sword]); - //std::cerr << ")" << std::endl; - } - return terms; -} - - - - - - - -int match(PQFileItem *fi, SearchItem *si) -{ - if (si -> datatype == PQI_SI_DATATYPE_HASH) - { - if (0 == strcmp((fi -> hash).c_str(), (si -> data).c_str())) - return 1; - return 0; - } - else if (si -> datatype == PQI_SI_DATATYPE_NAME) - { - if (0 == strcmp((fi -> name).c_str(), (si -> data).c_str())) - return 1; - return 0; - } - else // try it as terms.... - { - std::list terms = createtermlist(si -> data); - std::list::iterator it; - if (terms.size() == 0) - { - return 0; - } - - for(it = terms.begin(); it != terms.end(); it++) - { - if (0 == fi -> match(*it)) - { - return 0; - } - } - return 1; - } -} - diff --git a/libretroshare/src/pqi/pqi.h b/libretroshare/src/pqi/pqi.h index 8f54e616a..ed39d2d34 100644 --- a/libretroshare/src/pqi/pqi.h +++ b/libretroshare/src/pqi/pqi.h @@ -1,9 +1,9 @@ /* - * "$Id: pqi.h,v 1.8 2007-03-31 09:41:32 rmf24 Exp $" + * libretroshare/src/pqi pqi.h * * 3P/PQI network interface for RetroShare. * - * Copyright 2004-2006 by Robert Fernie. + * Copyright 2004-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 @@ -24,20 +24,22 @@ */ +#ifndef PQI_TOP_HEADER +#define PQI_TOP_HEADER -#ifndef MRK_P3_PUBLIC_INTERFACE_HEADER -#define MRK_P3_PUBLIC_INTERFACE_HEADER +/* This just includes the standard headers required. + */ -// This is the overall include, for public usage -// + extension of the p3 interface. -#include "pqi/pqi_data.h" +#include "pqi/pqi_base.h" +#include "pqi/pqinetwork.h" +#include "serialiser/rsserial.h" +#include "serialiser/rsbaseitems.h" #include #include #include - /********************** SEARCH INTERFACE ***************************/ // this is an interface.... so should be // classified as virtual = 0; @@ -49,54 +51,24 @@ public: virtual ~SearchInterface() { return; } - // OutGoing Searches... + // Cache Requests +virtual int SearchSpecific(RsCacheRequest *) = 0; +virtual RsCacheRequest *RequestedSearch() = 0; -virtual int Search(SearchItem *) = 0; // Search All people... -virtual int SearchSpecific(SearchItem *) = 0; // Search One person only... -virtual int CancelSearch(SearchItem *) = 0; // no longer used? -virtual PQFileItem *GetSearchResult() = 0; - - - // Incoming Searches... -virtual SearchItem *RequestedSearch() = 0; -virtual SearchItem *CancelledSearch() = 0; -virtual int SendSearchResult(PQFileItem *item) = 0; + // Cache Results +virtual int SendSearchResult(RsCacheItem *item) = 0; +virtual RsCacheItem *GetSearchResult() = 0; // FileTransfer. -virtual PQFileItem *GetFileItem() = 0; -virtual int SendFileItem(PQFileItem *) = 0; +virtual RsFileRequest *GetFileRequest() = 0; +virtual int SendFileRequest(RsFileRequest *) = 0; + +virtual RsFileData *GetFileData() = 0; +virtual int SendFileData(RsFileData *) = 0; }; -class ChatInterface -{ -public: - ChatInterface() {return; }; -virtual ~ChatInterface() {return; }; - -virtual int SendMsg(ChatItem *item) = 0; -virtual int SendGlobalMsg(ChatItem *ns) = 0; -virtual ChatItem *GetMsg() = 0; - -}; - -// This can potentially be moved to where the -// discovery code is!!!! - -class HostItem: public PQItem { }; - -class DiscoveryInterface -{ - public: -virtual ~DiscoveryInterface() { return; } - // Discovery Request. -virtual int DiscoveryRequest(PQItem *) = 0; // item is just for direction. -virtual HostItem *DiscoveryResult() = 0; -}; - - -class P3Interface: public SearchInterface, - public ChatInterface +class P3Interface: public SearchInterface { public: P3Interface() {return; } @@ -105,10 +77,10 @@ virtual ~P3Interface() {return; } virtual int tick() { return 1; } virtual int status() { return 1; } -virtual int SendOtherPQItem(PQItem *) = 0; -virtual PQItem *GetOtherPQItem() = 0; -virtual PQItem *SelectOtherPQItem(bool (*tst)(PQItem *)) = 0; +virtual int SendRsRawItem(RsRawItem *) = 0; +virtual RsRawItem *GetRsRawItem() = 0; }; -#endif //MRK_PQI_BASE_HEADER +#endif // PQI_TOP_HEADER + diff --git a/libretroshare/src/pqi/pqi_base.cc b/libretroshare/src/pqi/pqi_base.cc index ed8db1a32..69291c094 100644 --- a/libretroshare/src/pqi/pqi_base.cc +++ b/libretroshare/src/pqi/pqi_base.cc @@ -48,384 +48,6 @@ int getPQIsearchId() return next_search_id++; } -// SI Operations. -PQItem::PQItem() - :sid(0), type(0), subtype(0), p(0) -{ - //std::cerr << "Creating PQItem(0,0)@" << std::endl; - //std::cerr << (void *) this << std::endl; - pqicid_clear(&cid); - return; -} - -PQItem::~PQItem() -{ - //std::cerr << "Deleting PQItem(" << type << ","; - //std::cerr << subtype << ")@" << (void *) this << std::endl; - return; -} - -PQItem::PQItem(int t, int st) - :sid(0), type(t), subtype(st), flags(0), p(0) -{ - //std::cerr << "Creating PQItem(" << t << "," << st << ")@"; - //std::cerr << (void *) this << std::endl; - pqicid_clear(&cid); - return; -} - -PQItem *PQItem::clone() -{ - PQItem *npqi = new PQItem(); - npqi -> copy(this); - - return npqi; -} - -void PQItem::copy(const PQItem *pqi) -{ - sid = pqi -> sid; - pqicid_copy(&(pqi -> cid), &(cid)); - type = pqi -> type; - subtype = pqi -> subtype; - flags = pqi -> flags; - p = pqi -> p; - return; -} - -void PQItem::copyIDs(const PQItem *pqi) -{ - sid = pqi -> sid; - pqicid_copy(&(pqi -> cid), &(cid)); - // Person ref should also be part of the id. - p = pqi -> p; - return; -} - - -std::ostream &PQItem::print(std::ostream &out) -{ - out << "-- PQItem" << std::endl; - out << "CID [" << cid.route[0]; - for(int i = 0; i < 10; i++) - out << ":" << cid.route[i]; - out << "] SID: " << sid << std::endl; - out << "Type: " << type << "/" << subtype; - out << " Flags: " << flags << std::endl; - out << " Source: "; - if (p == NULL) - { - out << "*Unknown*" << std::endl; - } - else - { - out << p -> Name() << std::endl; - } - return out << "--" << std::endl; -} - - -int PQItem::cidpop() -{ - return pqiroute_getshift(&cid); -} - -void PQItem::cidpush(int id) -{ - pqiroute_setshift(&cid, id); - return; -} - -PQFileItem::PQFileItem() - :PQItem(PQI_ITEM_TYPE_FILEITEM, PQI_FI_SUBTYPE_GENERAL), - reqtype(PQIFILE_STD) -{ - //std::cerr << "Creating PQFileItem()" << std::endl; - return; -} - -PQFileItem::PQFileItem(int subtype) - :PQItem(PQI_ITEM_TYPE_FILEITEM, subtype), - reqtype(PQIFILE_STD) -{ - //std::cerr << "Creating PQFileItem()" << std::endl; - return; -} - -PQFileItem::~PQFileItem() -{ - //std::cerr << "Deleting PQFileItem()" << std::endl; - return; -} - - -PQFileItem *PQFileItem::clone() -{ - PQFileItem *nfi = new PQFileItem(); - nfi -> copy(this); - return nfi; -} - - -void PQFileItem::copy(const PQFileItem *src) -{ - PQItem::copy(src); - - hash = src -> hash; - name = src -> name; - path = src -> path; - ext = src -> ext; - - size = src -> size; - fileoffset = src -> fileoffset; - chunksize = src -> chunksize; - - exttype = src -> exttype; - reqtype = src -> reqtype; - return; -} - -std::ostream &PQFileItem::print(std::ostream &out) -{ - out << "-------- PQFileItem" << std::endl; - PQItem::print(out); - - out << "Name: " << name << std::endl; - out << "Hash: " << hash << std::endl; - out << "Path: " << path << std::endl; - out << "Ext: " << ext << std::endl; - out << "Size: " << size << std::endl; - out << "FileOffset: " << fileoffset << std::endl; - out << "ChunkSize: " << chunksize << std::endl; - out << "Ext/Req Types: " << exttype << "/" << reqtype << std::endl; - return out << "--------" << std::endl; -} - - -PQFileData::PQFileData() - :PQFileItem(PQI_FI_SUBTYPE_DATA), - datalen(0), data(NULL), fileflags(0) -{ - //std::cerr << "Creating PQFileData()" << std::endl; - flags = 0; - return; -} - - -PQFileData::~PQFileData() -{ - if (data != NULL) - { - free(data); - } - //std::cerr << "Deleting PQFileData()" << std::endl; - return; -} - -PQFileData *PQFileData::clone() -{ - PQFileData *nfd = new PQFileData(); - nfd -> copy(this); - - return nfd; -} - -void PQFileData::copy(const PQFileData *fd) -{ - // copy below. - PQItem::copy(fd); - - if (data != NULL) - { - free(data); - } - data = malloc(fd -> datalen); - memcpy(data, fd -> data, fd -> datalen); - datalen = fd -> datalen; - fileflags = fd -> fileflags; - return; -} - -std::ostream &PQFileData::print(std::ostream &out) -{ - out << "-------- PQFileData" << std::endl; - PQItem::print(out); - - out << "DataLen: " << datalen << std::endl; - out << "FileFlags: " << fileflags << std::endl; - return out << "--------" << std::endl; -} - - -int PQFileData::writedata(std::ostream &out) -{ - //std::cerr << "PQFileData::writedata() len: " << datalen << std::endl; - if (data != NULL) - { - out.write((char *) data, datalen); - if (out.bad()) - { - //std::cerr << "PQFileData::writedata() Bad ostream!" << std::endl; - return -1; - } - return 1; - } - return 1; -} - - -int PQFileData::readdata(std::istream &in, int maxbytes) -{ - if (data != NULL) - { - free(data); - } - data = malloc(maxbytes); - in.read((char *) data, maxbytes); - - int read = in.gcount(); - - // if read < maxread then finished? - if (read < maxbytes) - { - // check if finished. - if (in.eof()) - { - isend(); - } - else - { - //std::cerr << "pqi_makeFilePkt() Only Read: "; - //std::cerr << read << "/" << maxbytes << " Why?"; - //std::cerr << std::endl; - } - // adjust the space. - void *tmpdata = data; - data = malloc(read); - memcpy(data, tmpdata, read); - free(tmpdata); - } - datalen = read; - return 1; -} - -int PQFileData::endstream() -{ - if (fileflags & PQI_FD_FLAG_ENDSTREAM) - return 1; - return 0; -} - - -void PQFileData::isend() -{ - fileflags |= PQI_FD_FLAG_ENDSTREAM; - return; -} - -FileTransferItem::FileTransferItem() - :PQFileItem(PQI_FI_SUBTYPE_TRANSFER), - transferred(0), state(FT_STATE_OKAY), - crate(0), trate(0), in(false), ltransfer(0), lrate(0) -{ - //std::cerr << "Creating FileTransferItem()" << std::endl; - return; -} - - -FileTransferItem::~FileTransferItem() -{ - //std::cerr << "Deleting FileTransferItem()" << std::endl; - return; -} - -FileTransferItem *FileTransferItem::clone() -{ - FileTransferItem *nfd = new FileTransferItem(); - nfd -> copy(this); - - return nfd; -} - -void FileTransferItem::copy(const FileTransferItem *fd) -{ - // copy below. - PQFileItem::copy(fd); - - transferred = fd -> transferred; - state = fd -> state; - crate = fd -> crate; - trate = fd -> trate; - in = fd -> in; - ltransfer = fd -> ltransfer; - lrate = fd -> lrate; - return; -} - -std::ostream &FileTransferItem::print(std::ostream &out) -{ - out << "-------- FileTransferItem" << std::endl; - PQFileItem::print(out); - - if (in == true) - { - out << "Incoming File....."; - } - else - { - out << "Outgoing File....."; - } - out << "Transferred: " << transferred << std::endl; - out << "Transfer State: " << state << std::endl; - out << "Current/Total Rate: " << crate << "/" << trate << std::endl; - out << "LTransfer/LRate: " << ltransfer << "/" << lrate << std::endl; - return out << "--------" << std::endl; -} - - -RateCntrlItem::RateCntrlItem() - :PQFileItem(PQI_FI_SUBTYPE_RATE), - total(0), individual(0) -{ - //std::cerr << "Creating RateCntrlItem()" << std::endl; - return; -} - - -RateCntrlItem::~RateCntrlItem() -{ - //std::cerr << "Deleting RateCntrlItem()" << std::endl; - return; -} - -RateCntrlItem *RateCntrlItem::clone() -{ - RateCntrlItem *nfd = new RateCntrlItem(); - nfd -> copy(this); - - return nfd; -} - -void RateCntrlItem::copy(const RateCntrlItem *fd) -{ - // copy below. - PQFileItem::copy(fd); - - total = fd -> total; - individual = fd -> individual; - return; -} - -std::ostream &RateCntrlItem::print(std::ostream &out) -{ - out << "-------- RateCntrlItem" << std::endl; - PQFileItem::print(out); - - out << "Total Rate: " << total << std::endl; - out << "Individual Rate: " << individual << std::endl; - return out << "--------" << std::endl; -} - // CHANID Operations. int pqicid_clear(ChanId *cid) diff --git a/libretroshare/src/pqi/pqi_base.h b/libretroshare/src/pqi/pqi_base.h index 560679fb7..eef8d6755 100644 --- a/libretroshare/src/pqi/pqi_base.h +++ b/libretroshare/src/pqi/pqi_base.h @@ -36,6 +36,9 @@ #include "pqi/pqinetwork.h" +/*** Base DataTypes: ****/ +#include "serialiser/rsserial.h" + #define PQI_MIN_PORT 1024 #define PQI_MAX_PORT 16000 @@ -61,7 +64,7 @@ int pqicid_cmp(const ChanId *cid1, ChanId *cid2); // So the basic pqi interface defines // // 1) Person -> reference object -// 2) PQItem +// 2) RsItem // 3) PQInterface // which defines: // - send/recieve objs @@ -80,7 +83,7 @@ int pqicid_cmp(const ChanId *cid1, ChanId *cid2); // Base for all Security/Id/Certificate schemes. // The list of these must be maintained seperately -// from the PQItems.... +// from the RsItems.... // Person - includes @@ -198,197 +201,6 @@ private: }; -class PQItem -{ - // include serialisation (Boost) functions... - - // private copy constructor... prevent copying... - PQItem(const PQItem &) {return; } -public: - PQItem(); - PQItem(int t, int st); // initialise with subtypes. -virtual ~PQItem(); - - // other type of serialise.... -virtual std::ostream &print(std::ostream &out); - - // copy functions. -virtual PQItem *clone(); -void copy(const PQItem *pqi); -void copyIDs(const PQItem *pqi); - - // stack operations -> could be converted to STL stack. -int cidpop(); -void cidpush(int id); - - // data - ChanId cid; - SearchId sid; - - int type; - int subtype; - - int flags; - - Person *p; // This is shallow copied inside to itself, - // Over serialisation - becomes a name.... -}; - - -// Some Functional Objects for operations on PQItems - -class PQItem_MatchSid -//: public unary_function -{ - unsigned long sid; - - public: - explicit PQItem_MatchSid(const unsigned long s) : sid(s) {} - - bool operator()(const PQItem *pqi) const - { - if (pqi == NULL) - return false; - return (pqi -> sid == sid); - } -}; - - -class PQItem_MatchType -//: public unary_function -{ - int type, subtype; - - public: - explicit PQItem_MatchType(const int t, const int s = 0) - : type(t), subtype(s) {} - - bool operator()(const PQItem *pqi) const - { - if (pqi == NULL) - return false; - if (type == pqi -> type) - { - if (subtype == 0) - return true; - return (pqi -> subtype == subtype); - } - return false; - } -}; - - -#define PQIFILE_STD 0x0001 -#define PQIFILE_DIRLIST_FILE 0x0002 -#define PQIFILE_DIRLIST_DIR 0x0004 - - -class PQFileItem: public PQItem -{ - // private copy constructor... prevent copying... - PQFileItem(const PQFileItem &) {return; } -public: - PQFileItem(); - PQFileItem(int st); // initialise with subtypes. -virtual ~PQFileItem(); - - // copy functions. -virtual PQFileItem *clone(); -void copy(const PQFileItem *src); - -std::ostream &print(std::ostream &out); -virtual int match(std::string term); - - std::string hash; - std::string name; - std::string path; - std::string ext; - - - int size; // total size of file. - long fileoffset; // used for file requests. - long chunksize; // used for file requests. - - int reqtype; - int exttype; - -}; - - -class PQFileData: public PQFileItem -{ -public: - PQFileData(); -virtual ~PQFileData(); -virtual PQFileData *clone(); -void copy(const PQFileData *fd); - -virtual std::ostream &print(std::ostream &out); - -int writedata(std::ostream &out); -int readdata(std::istream &in, int maxbytes); -int endstream(); - -void isend(); - - // data - int datalen; - void *data; - - int fileflags; -}; - -// And finally the Control/Info Interface. - - -class FileTransferItem: public PQFileItem -{ - // private copy constructor... prevent copying... - // FileTransferItem(const FileTransferItem &) {return; } -public: - FileTransferItem(); -virtual ~FileTransferItem(); - -virtual FileTransferItem *clone(); -void copy(const FileTransferItem *ft); - -virtual std::ostream &print(std::ostream &out); - - int transferred; - int state; - float crate; - float trate; - bool in; - - int ltransfer; - float lrate; -}; - -#define FT_STATE_FAILED 0 -#define FT_STATE_OKAY 1 -#define FT_STATE_COMPLETE 2 - - - -class RateCntrlItem: public PQFileItem -{ -public: - RateCntrlItem(); -virtual ~RateCntrlItem(); - -virtual RateCntrlItem *clone(); -void copy(const RateCntrlItem *ft); - -virtual std::ostream &print(std::ostream &out); - - float total; - float individual; -}; - - - - - class RateInterface { // controlling data rates. @@ -437,9 +249,6 @@ float bw_in, bw_out, bwMax_in, bwMax_out; }; - - - /*********************** PQI INTERFACE ******************************\ * The basic exchange interface. * This inherits the RateInterface, as Bandwidth control @@ -451,19 +260,23 @@ class NetInterface; class PQInterface: public RateInterface { public: - PQInterface() { return; } + PQInterface(std::string id) :peerId(id) { return; } virtual ~PQInterface() { return; } -virtual int SendItem(PQItem *) = 0; -virtual PQItem *GetItem() = 0; +virtual int SendItem(RsItem *) = 0; +virtual RsItem *GetItem() = 0; // also there are tick + person id functions. virtual int tick() { return 0; } virtual int status() { return 0; } -virtual Person *getContact() { return NULL; } +virtual std::string PeerId() { return peerId; } // the callback from NetInterface Connection Events. virtual int notifyEvent(NetInterface *ni, int event) { return 0; } + + private: + + std::string peerId; }; /********************** Binary INTERFACE **************************** @@ -474,6 +287,7 @@ virtual int notifyEvent(NetInterface *ni, int event) { return 0; } #define BIN_FLAGS_NO_CLOSE 0x0001 #define BIN_FLAGS_READABLE 0x0002 #define BIN_FLAGS_WRITEABLE 0x0004 +#define BIN_FLAGS_NO_DELETE 0x0008 class BinInterface { @@ -515,8 +329,8 @@ static const int NET_CONNECT_FAILED = 5; class NetInterface { public: - NetInterface(PQInterface *p_in, Person *person_in) - :p(p_in), person(person_in) { return; } + NetInterface(PQInterface *p_in, std::string id) + :p(p_in), peerId(id) { return; } virtual ~NetInterface() { return; } @@ -528,28 +342,29 @@ virtual int listen() = 0; virtual int stoplistening() = 0; virtual int disconnect() = 0; virtual int reset() = 0; -virtual Person *getContact() { return person; } +virtual std::string PeerId() { return peerId; } protected: PQInterface *parent() { return p; } private: PQInterface *p; - Person *person; + std::string peerId; }; class NetBinInterface: public NetInterface, public BinInterface { public: - NetBinInterface(PQInterface *parent, Person *person) - :NetInterface(parent, person) + NetBinInterface(PQInterface *parent, std::string id) + :NetInterface(parent, id) { return; } virtual ~NetBinInterface() { return; } }; #define CHAN_SIGN_SIZE 16 #define CERTSIGNLEN 16 +#define PQI_PEERID_LENGTH 16 class certsign { diff --git a/libretroshare/src/pqi/pqi_data.h b/libretroshare/src/pqi/pqi_data.h deleted file mode 100644 index 45758d090..000000000 --- a/libretroshare/src/pqi/pqi_data.h +++ /dev/null @@ -1,169 +0,0 @@ -/* - * "$Id: pqi_data.h,v 1.8 2007-04-07 08:40:55 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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". - * - */ - - - -#ifndef PQI_ITEM_HEADER -#define PQI_ITEM_HEADER - -#include "pqi/pqi_base.h" - -#include -#include -#include - -// Definitions of some of the commonly used classes. -// These will all be made streamable! via Boost C++ - -class SearchItem: public PQItem -{ -public: - SearchItem(); - SearchItem(int st); -virtual ~SearchItem(); -virtual SearchItem *clone(); - -void copy(const SearchItem *si); -virtual std::ostream &print(std::ostream &out); - - // data - int datatype; - std::string data; - - int siflags; -}; - - -int match(PQFileItem *fi, SearchItem *si); - -class MediaItem: public PQFileItem -{ -public: - MediaItem(); -virtual ~MediaItem(); -virtual MediaItem *clone(); - -void copy(const MediaItem *src); -int match(std::string term); - - // data. -std::string mimetype; - -std::string title; -std::string author; -std::string collection; -std::string genre; - -int len; - -}; - -class ChatItem: public PQItem -{ -public: - ChatItem(); - ChatItem(int st); -virtual ~ChatItem(); -virtual ChatItem *clone(); -void copy(const ChatItem *si); -virtual std::ostream &print(std::ostream &out); - - // data -std::string msg; - // Timestamp (Not transmitted currently!). - // Stamped on arrival. - int epoch; -}; - - -class MsgFileItem -{ - public: - std::string name; /* and dir! */ - std::string hash; - unsigned long size; -}; - - -class MsgItem: public ChatItem -{ -public: - MsgItem(); -virtual ~MsgItem(); - -virtual MsgItem *clone(); -void copy(const MsgItem *mi); -std::ostream &print(std::ostream &out); - - // Note this breaks the old client.... -// Old data..... -//int mode; -//std::string recommendname; -//std::string recommendhash; -//std::string channel; -//int recommendsize; -//int datalen; -//void *data; - - // Flags - unsigned long msgflags; - unsigned long sendTime; - - std::string title; - std::string header; - - std::list files; -}; - - -std::string strtolower(std::string in); -std::string fileextension(std::string in); -std::list createtermlist(std::string in); - - -// IDS - -const int PQI_SI_SUBTYPE_SEARCH = 1; -const int PQI_SI_SUBTYPE_CANCEL = 2; -const int PQI_SI_SUBTYPE_FILEREQ = 3; -const int PQI_SI_SUBTYPE_FILECANCEL = 4; - -const int PQI_SI_DATATYPE_TERMS = 1; -const int PQI_SI_DATATYPE_HASH = 2; -const int PQI_SI_DATATYPE_NAME = 3; -const int PQI_SI_DATATYPE_DIR = 4; - -const int PQI_MI_SUBTYPE_CHAT = 1; -const int PQI_MI_SUBTYPE_MSG = 2; - - -/* not really used here, but at higher layers */ -const unsigned long PQI_MI_FLAGS_OUTGOING = 0x0001; -const unsigned long PQI_MI_FLAGS_PENDING = 0x0002; -const unsigned long PQI_MI_FLAGS_DRAFT = 0x0004; -const unsigned long PQI_MI_FLAGS_NEW = 0x0010; - -#endif // PQI_ITEM_HEADER - diff --git a/libretroshare/src/pqi/pqiarchive.cc b/libretroshare/src/pqi/pqiarchive.cc index 21f0ae333..bc64f3af3 100644 --- a/libretroshare/src/pqi/pqiarchive.cc +++ b/libretroshare/src/pqi/pqiarchive.cc @@ -33,7 +33,7 @@ /******************************************************************* * pqiarchive provides an archive stream. - * This stores PQItem + Person Reference + Timestamp, + * This stores RsItem + Person Reference + Timestamp, * * and allows Objects to be replayed or restored, * independently of the rest of the pqi system. @@ -41,7 +41,7 @@ */ #include "pqi/pqiarchive.h" -#include "pqi/pqipacket.h" +#include "serialiser/rsserial.h" #include #include @@ -60,10 +60,13 @@ struct pqiarchive_header const int PQIARCHIVE_TYPE_PQITEM = 0x0001; -pqiarchive::pqiarchive(BinInterface *bio_in, int bio_flags_in, sslroot *s) - :bio(bio_in), bio_flags(bio_flags_in), - nextPkt(NULL), nextPktTS(0), firstPktTS(0), initTS(0), - sslr(s) +/* PeerId of PQInterface is not important ... as we are archiving + * packets from other people... + */ + +pqiarchive::pqiarchive(RsSerialiser *rss, BinInterface *bio_in, int bio_flags_in) + :PQInterface(""), rsSerialiser(rss), bio(bio_in), bio_flags(bio_flags_in), + nextPkt(NULL), nextPktTS(0), firstPktTS(0), initTS(0) { { std::ostringstream out; @@ -112,14 +115,14 @@ pqiarchive::~pqiarchive() if (nextPkt) { - pqipkt_delete(nextPkt); + delete nextPkt; } return; } // Get/Send Items. -int pqiarchive::SendItem(PQItem *si) +int pqiarchive::SendItem(RsItem *si) { { std::ostringstream out; @@ -132,7 +135,8 @@ int pqiarchive::SendItem(PQItem *si) if (!(bio_flags & BIN_FLAGS_WRITEABLE)) { - delete si; + if (!(bio_flags & BIN_FLAGS_NO_DELETE)) + delete si; return -1; } @@ -140,7 +144,7 @@ int pqiarchive::SendItem(PQItem *si) return ret; /* 0 - failure, 1 - success*/ } -PQItem *pqiarchive::GetItem() +RsItem *pqiarchive::GetItem() { { std::ostringstream out; @@ -198,7 +202,7 @@ PQItem *pqiarchive::GetItem() initTS = time(NULL); firstPktTS = nextPktTS; } - PQItem *outPkt = nextPkt; + RsItem *outPkt = nextPkt; nextPkt = NULL; if (outPkt != NULL) @@ -237,7 +241,7 @@ int pqiarchive::status() // /**************** HANDLE OUTGOING TRANSLATION + TRANSMISSION ******/ -int pqiarchive::writePkt(PQItem *pqi) +int pqiarchive::writePkt(RsItem *pqi) { { std::ostringstream out; @@ -245,8 +249,9 @@ int pqiarchive::writePkt(PQItem *pqi) pqioutput(PQL_DEBUG_ALL, pqiarchivezone, out.str()); } - void *ptr = pqipkt_makepkt(pqi); - if (NULL == ptr) + uint32_t pktsize = rsSerialiser->size(pqi); + void *ptr = malloc(pktsize); + if (!(rsSerialiser->serialise(pqi, ptr, &pktsize))) { std::ostringstream out; out << "pqiarchive::writePkt() Null Pkt generated!"; @@ -255,13 +260,30 @@ int pqiarchive::writePkt(PQItem *pqi) pqi -> print(out); pqioutput(PQL_ALERT, pqiarchivezone, out.str()); - pqipkt_delete(ptr); - delete pqi; + free(ptr); + if (!(bio_flags & BIN_FLAGS_NO_DELETE)) + delete pqi; return 0; } /* extract the extra details */ - int len = pqipkt_rawlen(ptr); + uint32_t len = getRsItemSize(ptr); + if (len != pktsize) + { + std::ostringstream out; + out << "pqiarchive::writePkt() Length MisMatch: len: " << len; + out << " != pktsize: " << pktsize; + out << std::endl; + out << "Caused By: " << std::endl; + pqi -> print(out); + pqioutput(PQL_ALERT, pqiarchivezone, out.str()); + + free(ptr); + if (!(bio_flags & BIN_FLAGS_NO_DELETE)) + delete pqi; + return 0; + } + if (!(bio->cansend())) { @@ -272,36 +294,36 @@ int pqiarchive::writePkt(PQItem *pqi) pqi -> print(out); pqioutput(PQL_ALERT, pqiarchivezone, out.str()); - pqipkt_delete(ptr); - delete pqi; + free(ptr); + if (!(bio_flags & BIN_FLAGS_NO_DELETE)) + delete pqi; return 0; } + // using the peerid from the item. + if (pqi->PeerId().length() != PQI_PEERID_LENGTH) + { + std::ostringstream out; + out << "pqiarchive::writePkt() Invalid peerId Length!"; + out << std::endl; + out << "Caused By: " << std::endl; + pqi -> print(out); + pqioutput(PQL_ALERT, pqiarchivezone, out.str()); + + free(ptr); + if (!(bio_flags & BIN_FLAGS_NO_DELETE)) + delete pqi; + return 0; + } + std::ostringstream out; out << "Writing Pkt Header" << std::endl; struct pqiarchive_header hdr; hdr.type = PQIARCHIVE_TYPE_PQITEM; hdr.length = len; hdr.ts = time(NULL); - certsign sig; - - // NB: At the moment, the messages we have sent don't have - // pqi->p set, and so they will be discarded...... - if (!sslr -> getcertsign((cert *) (pqi -> p), sig)) - { - out << "pqiarchive::writePkt() cannot get certsign!"; - out << std::endl; - out << "Caused By: " << std::endl; - pqi -> print(out); - pqioutput(PQL_ALERT, pqiarchivezone, out.str()); - - pqipkt_delete(ptr); - delete pqi; - return 0; - } - - memcpy(hdr.personSig, sig.data, CERTSIGNLEN); + memcpy(hdr.personSig, pqi->PeerId().c_str(), PQI_PEERID_LENGTH); // write packet header. if (sizeof(hdr) != bio->senddata(&hdr, sizeof(hdr))) @@ -310,8 +332,9 @@ int pqiarchive::writePkt(PQItem *pqi) out << std::endl; pqioutput(PQL_ALERT, pqiarchivezone, out.str()); - pqipkt_delete(ptr); - delete pqi; + free(ptr); + if (!(bio_flags & BIN_FLAGS_NO_DELETE)) + delete pqi; return 0; } @@ -325,8 +348,9 @@ int pqiarchive::writePkt(PQItem *pqi) out << std::endl; pqioutput(PQL_ALERT, pqiarchivezone, out.str()); - pqipkt_delete(ptr); - delete pqi; + free(ptr); + if (!(bio_flags & BIN_FLAGS_NO_DELETE)) + delete pqi; return 0; } @@ -334,8 +358,9 @@ int pqiarchive::writePkt(PQItem *pqi) out << " Success!" << std::endl; pqioutput(PQL_DEBUG_BASIC, pqiarchivezone, out.str()); - pqipkt_delete(ptr); - delete pqi; + free(ptr); + if (!(bio_flags & BIN_FLAGS_NO_DELETE)) + delete pqi; return 1; } @@ -345,7 +370,7 @@ int pqiarchive::writePkt(PQItem *pqi) * */ -int pqiarchive::readPkt(PQItem **item_out, long *ts_out) +int pqiarchive::readPkt(RsItem **item_out, long *ts_out) { { std::ostringstream out; @@ -362,11 +387,11 @@ int pqiarchive::readPkt(PQItem **item_out, long *ts_out) struct pqiarchive_header hdr; // enough space to read any packet. - int maxlen = pqipkt_maxsize(); + int maxlen = getRsPktMaxSize(); void *block = malloc(maxlen); // initial read size: basic packet. - int blen = pqipkt_basesize(); + int blen = getRsPktBaseSize(); int tmplen; @@ -394,7 +419,7 @@ int pqiarchive::readPkt(PQItem **item_out, long *ts_out) } // workout how much more to read. - int extralen = pqipkt_rawlen(block) - blen; + int extralen = getRsItemSize(block) - blen; if (extralen > 0) { void *extradata = (void *) (((char *) block) + blen); @@ -415,15 +440,11 @@ int pqiarchive::readPkt(PQItem **item_out, long *ts_out) // create packet, based on header. std::cerr << "Read Data Block -> Incoming Pkt("; std::cerr << blen + extralen << ")" << std::endl; - int readbytes = extralen + blen; - - PQItem *item = NULL; - if (pqipkt_check(block, readbytes)) - { - item = pqipkt_create(block); - } + uint32_t readbytes = extralen + blen; + RsItem *item = rsSerialiser->deserialise(block, &readbytes); free(block); + if (item == NULL) { pqioutput(PQL_ALERT, pqiarchivezone, @@ -431,39 +452,20 @@ int pqiarchive::readPkt(PQItem **item_out, long *ts_out) return 0; } - /* get the PersonSign out of the header */ - certsign sig; - memcpy(sig.data, hdr.personSig, CERTSIGNLEN); - item -> p = sslr -> findcertsign(sig); - /* if we can't identify the person it is from, - * then we need to discard it!..... (happens - * when they've been removed from our friend list.... + /* Cannot detect bad ids here anymore.... + * but removed dependancy on the sslroot! */ - if (item->p==NULL) - { - std::ostringstream out; - out << "pqiarchive::readPkt() Couldn't translate certsign"; - out << " into cert for pkt:" << std::endl; - item->print(out); - out << "Discarding!"; - pqioutput(PQL_ALERT, pqiarchivezone, out.str()); - delete item; - item = NULL; - return 0; - } - else + std::string peerId = ""; + for(int i = 0; i < PQI_PEERID_LENGTH; i++) { - /* copy cid ... to ensure outgoing - * msgs work properly - */ - item->cid = item->p->cid; + peerId += hdr.personSig[i]; } + item->PeerId(peerId); *item_out = item; *ts_out = hdr.ts; - /* cid/sid will be wrong */ return 1; } diff --git a/libretroshare/src/pqi/pqiarchive.h b/libretroshare/src/pqi/pqiarchive.h index 81a455011..1ba6cdcb1 100644 --- a/libretroshare/src/pqi/pqiarchive.h +++ b/libretroshare/src/pqi/pqiarchive.h @@ -28,23 +28,13 @@ #ifndef MRK_PQI_ARCHIVE_STREAMER_HEADER #define MRK_PQI_ARCHIVE_STREAMER_HEADER -// Only dependent on the base stuff. -#include "pqi/pqi_base.h" - -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) -#include "pqi/xpgpcert.h" -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ -#include "pqi/sslcert.h" -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ +#include "pqi/pqi.h" #include /******************************************************************* * pqiarchive provides an archive stream. - * This stores PQItem + Person Reference + Timestamp, + * This stores RsItem + Person Reference + Timestamp, * * and allows Objects to be replayed or restored, * independently of the rest of the pqi system. @@ -54,31 +44,32 @@ class pqiarchive: PQInterface { public: - pqiarchive(BinInterface *bio_in, int bio_flagsin, sslroot *); + pqiarchive(RsSerialiser *rss, BinInterface *bio_in, int bio_flagsin); virtual ~pqiarchive(); // PQInterface -virtual int SendItem(PQItem *); -virtual PQItem *GetItem(); +virtual int SendItem(RsItem *); +virtual RsItem *GetItem(); virtual int tick(); virtual int status(); virtual void setRealTime(bool r) { realTime = r; } private: -int writePkt(PQItem *item); -int readPkt(PQItem **item_out, long *ts); +int writePkt(RsItem *item); +int readPkt(RsItem **item_out, long *ts); + // Serialiser + RsSerialiser *rsSerialiser; // Binary Interface for IO, initialisated at startup. BinInterface *bio; unsigned int bio_flags; // only BIN_NO_CLOSE at the moment. // Temp Storage for transient data..... - PQItem *nextPkt; + RsItem *nextPkt; long nextPktTS; /* timestamp associated with nextPkt */ long firstPktTS; /* timestamp associated with first read Pkt */ long initTS; /* clock timestamp at first read */ - sslroot *sslr; bool realTime; }; diff --git a/libretroshare/src/pqi/pqihandler.cc b/libretroshare/src/pqi/pqihandler.cc index 266b38104..93842a2fd 100644 --- a/libretroshare/src/pqi/pqihandler.cc +++ b/libretroshare/src/pqi/pqihandler.cc @@ -59,7 +59,7 @@ pqihandler::pqihandler(SecurityPolicy *Global) int pqihandler::tick() { // tick all interfaces... - std::map::iterator it; + std::map::iterator it; int moreToTick = 0; for(it = mods.begin(); it != mods.end(); it++) { @@ -81,7 +81,7 @@ int pqihandler::tick() int pqihandler::status() { - std::map::iterator it; + std::map::iterator it; { // for output std::ostringstream out; @@ -109,45 +109,35 @@ int pqihandler::status() -int pqihandler::AddSearchModule(SearchModule *mod, int chanid) +bool pqihandler::AddSearchModule(SearchModule *mod) { - // if chan id set, then use.... - // otherwise find empty channel. - - int realchanid = -1; - std::map::iterator it; - - - if ((chanid > 0) && (mods.find(chanid) == mods.end())) + // if peerid used -> error. + std::map::iterator it; + if (mod->peerid != mod->pqi->PeerId()) { - // okay id. - realchanid = chanid; - + // ERROR! + std::ostringstream out; + out << "ERROR peerid != PeerId!" << std::endl; + pqioutput(PQL_ALERT, pqihandlerzone, out.str()); + return false; } - else - { - // find empty chan. - for(int i = 1; (i < 1000) && (realchanid == -1); i++) - { - if (mods.find(i) == mods.end()) - { - realchanid = i; - } - } - if (realchanid > 0) - { - std::ostringstream out; - out << "Allocated Chan Id: " << realchanid << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); - } - else - { - std::ostringstream out; - out << "Unable to Allocate Channel!" << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); - return -1; - } + if (mod->peerid == "") + { + // ERROR! + std::ostringstream out; + out << "ERROR peerid == NULL" << std::endl; + pqioutput(PQL_ALERT, pqihandlerzone, out.str()); + return false; + } + + if (mods.find(mod->peerid) != mods.end()) + { + // ERROR! + std::ostringstream out; + out << "ERROR PeerId Module already exists!" << std::endl; + pqioutput(PQL_ALERT, pqihandlerzone, out.str()); + return false; } // check security. @@ -161,35 +151,26 @@ int pqihandler::AddSearchModule(SearchModule *mod, int chanid) secpolicy_limit(globsec, mod -> sp); // store. - mods[realchanid] = mod; - mod -> smi = realchanid; - - // experimental ... pushcid. - Person *p = (mod -> pqi) -> getContact(); - if (p != NULL) - { - p -> cidpush(realchanid); - } - - return realchanid; + mods[mod->peerid] = mod; + return true; } -int pqihandler::RemoveSearchModule(SearchModule *mod) +bool pqihandler::RemoveSearchModule(SearchModule *mod) { - std::map::iterator it; + std::map::iterator it; for(it = mods.begin(); it != mods.end(); it++) { if (mod == it -> second) { mods.erase(it); - return 1; + return true; } } - return -1; + return false; } // dummy output check -int pqihandler::checkOutgoingPQItem(PQItem *item, int global) +int pqihandler::checkOutgoingRsItem(RsItem *item, int global) { pqioutput(PQL_WARNING, pqihandlerzone, "pqihandler::checkOutgoingPQItem() NULL fn"); @@ -199,176 +180,115 @@ int pqihandler::checkOutgoingPQItem(PQItem *item, int global) // generalised output -int pqihandler::HandlePQItem(PQItem *item, int allowglobal) +int pqihandler::HandleRsItem(RsItem *item, int allowglobal) { - std::map::iterator it; + std::map::iterator it; pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "pqihandler::HandlePQItem()"); + "pqihandler::HandleRsItem()"); - - if ((!allowglobal) && (!checkOutgoingPQItem(item, allowglobal))) + /* simplified to no global! */ + if (allowglobal) { + /* error */ std::ostringstream out; - out << "pqihandler::HandlePQItem() checkOutgoingPQItem"; - out << " Failed on item: " << std::endl; - item -> print(out); - - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); + out << "pqihandler::HandleSearchItem()"; + out << " Cannot send out Global RsItem"; + pqioutput(PQL_ALERT, pqihandlerzone, out.str()); delete item; return -1; } - // need to get channel id. (and remove from stack) - int chan = item -> cidpop(); - if (chan > 0) + if (!checkOutgoingRsItem(item, allowglobal)) { - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "pqihandler::HandlePQItem() Sending to One Channel"); + std::ostringstream out; + out << "pqihandler::HandleRsItem() checkOutgoingPQItem"; + out << " Failed on item: " << std::endl; + item -> print(out); + + pqioutput(PQL_ALERT, pqihandlerzone, out.str()); + delete item; + return -1; + } + + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, + "pqihandler::HandleRsItem() Sending to One Channel"); - // find module. - if ((it = mods.find(chan)) == mods.end()) - { - std::ostringstream out; - out << "pqihandler::HandlePQItem() Invalid chan!"; - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); + // find module. + if ((it = mods.find(item->PeerId())) == mods.end()) + { + std::ostringstream out; + out << "pqihandler::HandleRsItem() Invalid chan!"; + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); - delete item; - return -1; - } + delete item; + return -1; + } - // check security... is output allowed. - if(0 < secpolicy_check((it -> second) -> sp, 0, PQI_OUTGOING)) - { - std::ostringstream out; - out << "pqihandler::HandlePQItem() sending to chan:"; - out << it -> first << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); + // check security... is output allowed. + if(0 < secpolicy_check((it -> second) -> sp, 0, PQI_OUTGOING)) + { + std::ostringstream out; + out << "pqihandler::HandleRsItem() sending to chan:"; + out << it -> first << std::endl; + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); - // if yes send on item. - ((it -> second) -> pqi) -> SendItem(item); - return 1; - } - else - { - std::ostringstream out; - out << "pqihandler::HandlePQItem()"; - out << " Sec not approved"; - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); - - delete item; - return -1; - } + // if yes send on item. + ((it -> second) -> pqi) -> SendItem(item); + return 1; } else { - if (allowglobal <= 0) - { - std::ostringstream out; - out << "pqihandler::HandleSearchItem()"; - out << " invalid routing for non-global searchitem"; - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); + std::ostringstream out; + out << "pqihandler::HandleRsItem()"; + out << " Sec not approved"; + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); - return -1; - } - - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "pqihandler::HandlePQItem() Sending to All Channels"); - - // loop through all channels. - // for each one. - - // find module - // if yes send. - for(it = mods.begin(); it != mods.end(); it++) - { - - // check security... is output allowed. - if(0 < secpolicy_check((it -> second) -> sp, 0, PQI_OUTGOING)) - { - std::ostringstream out; - out << "pqihandler::HandlePQItem()"; - out << "Sending to chan:" << it -> first; - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); - - // if yes send on item. - ((it -> second) -> pqi) -> SendItem(item -> clone()); - } - else - { - std::ostringstream out; - out << "pqihandler::HandlePQItem()"; - out << " List Send - Not in this channel!"; - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); - - } - } - - // now we can clean up! delete item; - return 1; + return -1; } // if successfully sent to at least one. return 1; } - - -// 4 very similar outputs..... -int pqihandler::Search(SearchItem *ns) +int pqihandler::SearchSpecific(RsCacheRequest *ns) { - return HandlePQItem(ns, 1); + return HandleRsItem(ns, 0); } -/* This is called when we only want it to go to 1 person.... */ -int pqihandler::SearchSpecific(SearchItem *ns) +int pqihandler::SendSearchResult(RsCacheItem *ns) { - return HandlePQItem(ns, 0); + return HandleRsItem(ns, 0); } -int pqihandler::CancelSearch(SearchItem *ns) +int pqihandler::SendFileRequest(RsFileRequest *ns) { - return HandlePQItem(ns, 1); + return HandleRsItem(ns, 0); } -int pqihandler::SendFileItem(PQFileItem *ns) +int pqihandler::SendFileData(RsFileData *ns) { - return HandlePQItem(ns, 0); + return HandleRsItem(ns, 0); } -int pqihandler::SendSearchResult(PQFileItem *ns) -{ - return HandlePQItem(ns, 0); -} - -int pqihandler::SendMsg(ChatItem *ns) -{ - /* switch from 1 -> 0 for specific directed Msg */ - return HandlePQItem(ns, 0); -} - -int pqihandler::SendGlobalMsg(ChatItem *ns) -{ - return HandlePQItem(ns, 1); -} - -int pqihandler::SendOtherPQItem(PQItem *ns) +int pqihandler::SendRsRawItem(RsRawItem *ns) { pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "pqihandler::SendOtherPQItem()"); - return HandlePQItem(ns, 1); + "pqihandler::SendRsRawItem()"); + return HandleRsItem(ns, 0); } + // inputs. This is a very basic // system that is completely biased and slow... // someone please fix. int pqihandler::GetItems() { - std::map::iterator it; + std::map::iterator it; - PQItem *item; + RsItem *item; int count = 0; // loop through modules.... @@ -391,9 +311,14 @@ int pqihandler::GetItems() pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, out.str()); - // got one!.... - // update routing. - item -> cidpush(mod -> smi); + if (item->PeerId() != (mod->pqi)->PeerId()) + { + /* ERROR */ + pqioutput(PQL_ALERT, + pqihandlerzone, "ERROR PeerIds dont match!"); + item->PeerId(mod->pqi->PeerId()); + } + SortnStoreItem(item); count++; } @@ -424,150 +349,162 @@ int pqihandler::GetItems() -void pqihandler::SortnStoreItem(PQItem *item) +void pqihandler::SortnStoreItem(RsItem *item) { - // some template comparors for sorting incoming. - const static PQItem_MatchType match_fileitem(PQI_ITEM_TYPE_FILEITEM, - 0); - const static PQItem_MatchType match_result(PQI_ITEM_TYPE_FILEITEM, - PQI_FI_SUBTYPE_GENERAL); + /* get class type / subtype out of the item */ + uint8_t vers = item -> PacketVersion(); + uint8_t cls = item -> PacketClass(); + uint8_t type = item -> PacketType(); + uint8_t subtype = item -> PacketSubType(); - const static PQItem_MatchType match_endsrch(PQI_ITEM_TYPE_SEARCHITEM, - PQI_SI_SUBTYPE_CANCEL); - const static PQItem_MatchType match_reqsrch(PQI_ITEM_TYPE_SEARCHITEM, - PQI_SI_SUBTYPE_SEARCH); - - const static PQItem_MatchType match_info(PQI_ITEM_TYPE_INFOITEM, 0); - const static PQItem_MatchType match_chat(PQI_ITEM_TYPE_CHATITEM, 0); - - if (match_fileitem(item)) + /* whole Version reserved for SERVICES/CACHES */ + if (vers == RS_PKT_VERSION_SERVICE) { - if (match_result(item)) - { - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "SortnStore -> Result"); - - in_result.push_back(item); + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, + "SortnStore -> Service"); + in_service.push_back(item); + item = NULL; + return; + } + + if (vers != RS_PKT_VERSION1) + { + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, + "SortnStore -> Invalid VERSION! Deleting!"); + delete item; + item = NULL; + return; + } + + switch(cls) + { + case RS_PKT_CLASS_BASE: + switch(type) + { + case RS_PKT_TYPE_CACHE: + switch(subtype) + { + case RS_PKT_SUBTYPE_CACHE_REQUEST: + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, + "SortnStore -> Cache Request"); + in_search.push_back(item); + item = NULL; + break; + + case RS_PKT_SUBTYPE_CACHE_ITEM: + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, + "SortnStore -> Cache Result"); + in_result.push_back(item); + item = NULL; + break; + + default: + break; /* no match! */ } - else - { - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "SortnStore -> FileItem"); - in_file.push_back(item); - } - } - else if (match_endsrch(item)) - { - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "SortnStore -> Cancel Search"); + break; + + case RS_PKT_TYPE_FILE: + switch(subtype) + { + case RS_PKT_SUBTYPE_FI_REQUEST: + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, + "SortnStore -> File Request"); + in_request.push_back(item); + item = NULL; + break; + + case RS_PKT_SUBTYPE_FI_DATA: + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, + "SortnStore -> File Data"); + in_data.push_back(item); + item = NULL; + break; + + default: + break; /* no match! */ + } + break; + + default: + break; /* no match! */ + } + break; + + default: + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, + "SortnStore -> Unknown"); + break; - in_endsrch.push_back(item); } - else if (match_reqsrch(item)) + + if (item) { - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "SortnStore -> Search Request"); - in_reqsrch.push_back(item); - } - else if (match_chat(item)) - { - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "SortnStore -> Chat"); - in_chat.push_back(item); - } - else if (match_info(item)) - { - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "SortnStore -> Info"); - in_info.push_back(item); - } - else - { - pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, - "SortnStore -> Other"); - in_other.push_back(item); + pqioutput(PQL_DEBUG_BASIC, pqihandlerzone, + "SortnStore -> Deleting Unsorted Item"); + delete item; } + return; } // much like the input stuff. -PQFileItem *pqihandler::GetSearchResult() +RsCacheItem *pqihandler::GetSearchResult() { if (in_result.size() != 0) { - PQFileItem *fi = (PQFileItem *) in_result.front(); + RsCacheItem *fi = dynamic_cast(in_result.front()); + if (!fi) { delete in_result.front(); } in_result.pop_front(); return fi; } return NULL; } -SearchItem *pqihandler::RequestedSearch() +RsCacheRequest *pqihandler::RequestedSearch() { - if (in_reqsrch.size() != 0) + if (in_search.size() != 0) { - SearchItem *si = (SearchItem *) in_reqsrch.front(); - in_reqsrch.pop_front(); - return si; - } - return NULL; -} - -SearchItem * pqihandler::CancelledSearch() -{ - if (in_endsrch.size() != 0) - { - SearchItem *si = (SearchItem *) in_endsrch.front(); - in_endsrch.pop_front(); - return si; - } - return NULL; -} - -PQFileItem *pqihandler::GetFileItem() -{ - if (in_file.size() != 0) - { - PQFileItem *fi = (PQFileItem *) in_file.front(); - in_file.pop_front(); + RsCacheRequest *fi = dynamic_cast(in_search.front()); + if (!fi) { delete in_search.front(); } + in_search.pop_front(); return fi; } return NULL; } -// Chat Interface -ChatItem *pqihandler::GetMsg() +RsFileRequest *pqihandler::GetFileRequest() { - if (in_chat.size() != 0) + if (in_request.size() != 0) { - ChatItem *ci = (ChatItem *) in_chat.front(); - in_chat.pop_front(); - return ci; + RsFileRequest *fi = dynamic_cast(in_request.front()); + if (!fi) { delete in_request.front(); } + in_request.pop_front(); + return fi; } return NULL; } -PQItem *pqihandler::GetOtherPQItem() +RsFileData *pqihandler::GetFileData() { - if (in_other.size() != 0) + if (in_data.size() != 0) { - PQItem *pqi = in_other.front(); - in_other.pop_front(); - return pqi; + RsFileData *fi = dynamic_cast(in_data.front()); + if (!fi) { delete in_data.front(); } + in_data.pop_front(); + return fi; } return NULL; } -PQItem *pqihandler::SelectOtherPQItem(bool (*tst)(PQItem *)) +RsRawItem *pqihandler::GetRsRawItem() { - std::list::iterator it; - it = find_if(in_other.begin(), in_other.end(), *tst); - if (it != in_other.end()) + if (in_service.size() != 0) { - PQItem *pqi = (*it); - in_other.erase(it); - return pqi; + RsRawItem *fi = dynamic_cast(in_service.front()); + if (!fi) { delete in_service.front(); } + in_service.pop_front(); + return fi; } return NULL; } @@ -577,7 +514,7 @@ static const float MIN_RATE = 0.01; // 10 B/s // internal fn to send updates int pqihandler::UpdateRates() { - std::map::iterator it; + std::map::iterator it; int num_sm = mods.size(); float avail_in = getMaxRate(true); diff --git a/libretroshare/src/pqi/pqihandler.h b/libretroshare/src/pqi/pqihandler.h index 68df37b53..483603bb3 100644 --- a/libretroshare/src/pqi/pqihandler.h +++ b/libretroshare/src/pqi/pqihandler.h @@ -50,7 +50,7 @@ class SearchModule { public: - int smi; // id. + std::string peerid; PQInterface *pqi; SecurityPolicy *sp; }; @@ -62,39 +62,30 @@ class pqihandler: public P3Interface { public: pqihandler(SecurityPolicy *Global); -int AddSearchModule(SearchModule *mod, int chanid = -1); -int RemoveSearchModule(SearchModule *mod); +bool AddSearchModule(SearchModule *mod); +bool RemoveSearchModule(SearchModule *mod); // P3Interface. -// 4 very similar outputs..... -virtual int Search(SearchItem *ns); -virtual int SearchSpecific(SearchItem *ns); /* search one person only */ -virtual int CancelSearch(SearchItem *ns); /* no longer used? */ -virtual int SendSearchResult(PQFileItem *); +virtual int SearchSpecific(RsCacheRequest *ns); +virtual int SendSearchResult(RsCacheItem *); // inputs. -virtual PQFileItem * GetSearchResult(); -virtual SearchItem * RequestedSearch(); -virtual SearchItem * CancelledSearch(); +virtual RsCacheRequest * RequestedSearch(); +virtual RsCacheItem * GetSearchResult(); // file i/o -virtual int SendFileItem(PQFileItem *ns); -virtual PQFileItem * GetFileItem(); - -// Chat Interface -virtual int SendMsg(ChatItem *item); -virtual int SendGlobalMsg(ChatItem *ns); /* needed until chat complete */ - -virtual ChatItem *GetMsg(); +virtual int SendFileRequest(RsFileRequest *ns); +virtual int SendFileData(RsFileData *ns); +virtual RsFileRequest * GetFileRequest(); +virtual RsFileData * GetFileData(); // Rest of P3Interface virtual int tick(); virtual int status(); -// Control Interface -virtual int SendOtherPQItem(PQItem *); -virtual PQItem *GetOtherPQItem(); -virtual PQItem *SelectOtherPQItem(bool (*tst)(PQItem *)); +// Service Data Interface +virtual int SendRsRawItem(RsRawItem *); +virtual RsRawItem *GetRsRawItem(); // rate control. void setMaxIndivRate(bool in, float val); @@ -106,19 +97,19 @@ float getMaxRate(bool in); /* check to be overloaded by those that can * generates warnings otherwise */ -virtual int checkOutgoingPQItem(PQItem *item, int global); +virtual int checkOutgoingRsItem(RsItem *item, int global); -int HandlePQItem(PQItem *ns, int allowglobal); +int HandleRsItem(RsItem *ns, int allowglobal); int GetItems(); -void SortnStoreItem(PQItem *item); +void SortnStoreItem(RsItem *item); - std::map mods; + std::map mods; SecurityPolicy *globsec; // Temporary storage... - std::list in_result, in_endsrch, in_reqsrch, in_reqfile, - in_file, in_chat, in_info, in_host, in_other; + std::list in_result, in_search, + in_request, in_data, in_service; private: diff --git a/libretroshare/src/pqi/pqiloopback.cc b/libretroshare/src/pqi/pqiloopback.cc index 0057d15b0..45a9e71be 100644 --- a/libretroshare/src/pqi/pqiloopback.cc +++ b/libretroshare/src/pqi/pqiloopback.cc @@ -45,7 +45,8 @@ // Test Interface (LoopBack) -pqiloopback::pqiloopback() +pqiloopback::pqiloopback(std::string id) + :PQInterface(id) { //std::cerr << "pqiloopback construction()" << std::endl; // rates are irrelevant..... no B/W consumed. @@ -62,19 +63,20 @@ pqiloopback::~pqiloopback() return; } -int pqiloopback::SendItem(PQItem *i) +int pqiloopback::SendItem(RsItem *i) { - i -> p = getContact(); - i -> flags |= PQI_ITEM_FLAG_LOCAL; + // contact Id should be correct already! + //i -> p = getContact(); + //i -> flags |= PQI_ITEM_FLAG_LOCAL; objs.push_back(i); return 1; } -PQItem * pqiloopback::GetItem() +RsItem * pqiloopback::GetItem() { if (objs.size() > 0) { - PQItem *pqi = objs.front(); + RsItem *pqi = objs.front(); objs.pop_front(); // breaks the module, through ssl dependance. // but necessary for running system.. @@ -84,25 +86,6 @@ PQItem * pqiloopback::GetItem() return NULL; } -int pqiloopback::GetFile(PQFileItem *item, std::ostream &in) -{ - return 1; -} - -int pqiloopback::SendFile(PQFileItem *, std::istream &out) -{ - return 1; -} - -Person *pqiloopback::getContact() -{ - sslroot *root = getSSLRoot(); - cert *c = root -> getOwnCert(); - return c; -} - - - // // PQI interface. int pqiloopback::tick() { diff --git a/libretroshare/src/pqi/pqiloopback.h b/libretroshare/src/pqi/pqiloopback.h index ee91f6336..fb1b9b6d7 100644 --- a/libretroshare/src/pqi/pqiloopback.h +++ b/libretroshare/src/pqi/pqiloopback.h @@ -42,26 +42,20 @@ class pqiloopback: public PQInterface { public: - pqiloopback(); + pqiloopback(std::string id); virtual ~pqiloopback(); // search Interface. -virtual int SendItem(PQItem *item); -virtual PQItem *GetItem(); - -virtual int GetFile(PQFileItem *item, std::ostream &in); -virtual int SendFile(PQFileItem *item, std::istream &out); +virtual int SendItem(RsItem *item); +virtual RsItem *GetItem(); // PQI interface. virtual int tick(); virtual int status(); -virtual Person *getContact(); - +virtual int notifyEvent(NetInterface *ni, int event) { return 0; } /* Not used */ private: - std::list objs; + std::list objs; }; - - #endif //MRK_PQI_LOOPBACK_HEADER diff --git a/libretroshare/src/pqi/pqinetwork.h b/libretroshare/src/pqi/pqinetwork.h index 5c565dd2d..b87d92260 100644 --- a/libretroshare/src/pqi/pqinetwork.h +++ b/libretroshare/src/pqi/pqinetwork.h @@ -40,6 +40,7 @@ //socket blocking/options. #include +#include #else @@ -135,6 +136,13 @@ int unix_getsockopt_error(int sockfd, int *err); int WinToUnixError(int error); #endif +/***** From http://www.codeproject.com/KB/cpp/endianness.aspx + +#define ntohll(x) (((int64_t)(ntohl((int32_t)((x << 32) >> 32))) << 32) | + (uint32_t) ntohl(((int32_t)(x >> 32)))) +#define htonll(x) ntohll(x) + +*****/ #endif diff --git a/libretroshare/src/pqi/pqipacket.cc b/libretroshare/src/pqi/pqipacket.cc deleted file mode 100644 index 011592577..000000000 --- a/libretroshare/src/pqi/pqipacket.cc +++ /dev/null @@ -1,1783 +0,0 @@ -/* - * "$Id: pqipacket.cc,v 1.11 2007-03-21 18:45:41 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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 "pqi/pqipacket.h" - -// get -#include "pqi/pqi.h" - -// discItem functions... -//#include "discItem.h" - -#include "pqi/pqitunnel.h" - - -#include "pqi/pqidebug.h" -#include -const int pqipktzone = 38422; - - -void pqipkt_print(pqipkt *pkt); - -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ -#ifndef WINDOWS_SYS // ie UNIX - - // NOTHING -#else -// typedefs to generate the types... -// this must be checked!!! - -typedef unsigned short uint16_t; -typedef unsigned int uint32_t; - -#endif -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ - - -// internal functions. -// PQI Functions. -void pqipkt_setPQITag(pqipkt *pkt); -void pqipkt_setPQIType(void *pkt, int type); -int pqipkt_getPQIType(void *pkt); -void pqipkt_setPQISubType(void *pkt, int subtype); -int pqipkt_getPQISubType(void *pkt); -void pqipkt_setPQILen(void *pkt, int len); -int pqipkt_getPQILen(void *pkt); -void pqipkt_setPQIsid(void *pkt, int sid); -int pqipkt_getPQIsid(void *pkt); -void pqipkt_setPQIFlags(void *pkt, int flags); -int pqipkt_getPQIFlags(void *pkt); - -// SI Functions. -pqipkt *pqipkt_makesipkt(SearchItem *si); -SearchItem *pqipkt_makesiobj(pqipkt *pkt); -void pqipkt_setSIdatatype(void *pkt, int datatype); -int pqipkt_getSIdatatype(void *pkt); -void pqipkt_setSIdata(void *pkt, const char *in); -std::string pqipkt_getSIdata(void *pkt); - -// FI Functions. -pqipkt *pqipkt_makefipkt(PQFileItem *fi); -PQFileItem *pqipkt_makefiobj(pqipkt *pkt); -void pqipkt_setFIhash(void *pkt, const char *in); -std::string pqipkt_getFIhash(void *pkt); -void pqipkt_setFIname(void *pkt, const char *in); -std::string pqipkt_getFIname(void *pkt); -void pqipkt_setFIext(void *pkt, const char *in); -std::string pqipkt_getFIext(void *pkt); -void pqipkt_setFIlen(void *pkt, int len); -int pqipkt_getFIlen(void *pkt); - -void pqipkt_setFIoffset(void *pkt, int offset); -int pqipkt_getFIoffset(void *pkt); -void pqipkt_setFIchunksize(void *pkt, int size); -int pqipkt_getFIchunksize(void *pkt); - -void pqipkt_setFIreqtype(void *pkt, int in); -int pqipkt_getFIreqtype(void *pkt); -void pqipkt_setFIexttype(void *pkt, int in); -int pqipkt_getFIexttype(void *pkt); -void pqipkt_setFIpath(void *pkt, const char *in); -std::string pqipkt_getFIpath(void *pkt); - -// (S)MI Functions. -pqipkt *pqipkt_makecipkt(ChatItem *ci); -ChatItem *pqipkt_makeciobj(pqipkt *pkt); -void pqipkt_setMIuint32(void *pkt, int offset, unsigned long val); -unsigned long pqipkt_getMIuint32(void *pkt, int offset); - -void pqipkt_setMIstring(void *pkt, int offset, std::string name); -std::string pqipkt_setMIstring(void *pkt, int offset, int len); - -// FD Functions. -pqipkt *pqipkt_makefdpkt(PQFileData *fd); -PQFileData *pqipkt_makefdobj(pqipkt *pkt); - -void pqipkt_setFDlen(void *pkt, int len); -int pqipkt_getFDlen(void *pkt); -void pqipkt_setFDflags(void *pkt, int flags); -int pqipkt_getFDflags(void *pkt); -void pqipkt_setFDdata(void *pkt, void *data, int datalen); -void pqipkt_getFDdata(void *pkt, void *data, int datalen); - -static std::map types_fn; -static std::map types_fninit; - - -int registerTunnelType(int subtype, PQTunnel *(*fn)(void *d, int n)) -{ - // check it don't exist already. - std::map::iterator it; - it = types_fn.find(subtype); - if (it != types_fn.end()) - return -1; - - types_fn[subtype] = fn; - return 1; -} - -PQTunnel *createTunnelType(int subtype, void *d, int n) -{ - // check it don't exist already. - std::map::iterator it; - it = types_fn.find(subtype); - if (it != types_fn.end()) - { - std::ostringstream out; - out << "Created Tunnel Class:" << std::endl; - - PQTunnel *pkt = (it -> second)(d, n); - // build the packet. - pkt -> in(d, n); - - pkt -> print(out); - out << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - return pkt; - } - - { - std::ostringstream out; - for(it = types_fn.begin(); it != types_fn.end(); it++) - { - out << "TYPE_FN IDX: " << it -> first << std::endl; - } - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "createTunnelType() returning Null"); - return NULL; -} - - -int registerTunnelInitType(int subtype, PQTunnelInit *(*fn)(void *d, int n)) -{ - // check it don't exist already. - std::map::iterator it; - it = types_fninit.find(subtype); - if (it != types_fninit.end()) - return -1; - - types_fninit[subtype] = fn; - return 1; -} - -PQTunnelInit *createTunnelInitType(int subtype, void *d, int n) -{ - // check it don't exist already. - std::map::iterator it; - it = types_fninit.find(subtype); - if (it != types_fninit.end()) - { - std::ostringstream out; - out << "Created Init Tunnel Class:" << std::endl; - - PQTunnelInit *pkt = (it -> second)(d, n); - // build the packet. - pkt -> in(d, n); - - pkt -> print(out); - out << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - return pkt; - } - - { - std::ostringstream out; - for(it = types_fninit.begin(); it != types_fninit.end(); it++) - { - out << "TYPE_FN IDX: " << it -> first << std::endl; - } - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "createTunnelInitType() returning Null"); - return NULL; -} - - -// Tunnel Functions. -pqipkt *pqipkt_maketunnelpkt(PQTunnel *item); -PQTunnel *pqipkt_maketunnelobj(pqipkt *pkt); - -// Init Tunnel -pqipkt *pqipkt_maketunnelinitpkt(PQTunnelInit *item); -PQTunnelInit *pqipkt_maketunnelinitobj(pqipkt *pkt); - -void pqipkt_setTunnellen(void *pkt, int len); -int pqipkt_getTunnellen(void *pkt); -void *pqipkt_getTunneldata(void *pkt); - - - -int pqipkt_basesize() -{ - //fixme("pqipkt_basesize()", 5); - return 276; -} - - int pqipkt_maxsize() -{ - //fixme("pqipkt_maxsize()", 5); - return 16 * 1024; -} - - -// determine properties. (possible from incomplete pkt). -int pqipkt_rawlen(pqipkt *pkt) -{ - return pqipkt_getPQILen(pkt); -} - -bool pqipkt_check(pqipkt *pkt, int size) -{ - std::ostringstream out; - out << "pqipkt_check() RawLen: " << pqipkt_rawlen(pkt); - out << " is ?=? to size: " << size; - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - - return (pqipkt_rawlen(pkt) <= size); -} - - -PQItem *pqipkt_create(pqipkt *pkt) -{ - PQItem *pqi = NULL; - - { - std::ostringstream out; - out << "pqipkt_create() Making PQItem Based on Type:"; - out << pqipkt_getPQIType(pkt); - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - //pqipkt_print(pkt); - - switch(pqipkt_getPQIType(pkt)) - { - case PQI_ITEM_TYPE_SEARCHITEM: - pqi = pqipkt_makesiobj(pkt); - break; - - case PQI_ITEM_TYPE_FILEITEM: - pqi = pqipkt_makefiobj(pkt); - break; - - - case PQI_ITEM_TYPE_CHATITEM: - pqi = pqipkt_makeciobj(pkt); - break; - - case PQI_ITEM_TYPE_TUNNELITEM: - pqi = pqipkt_maketunnelobj(pkt); - break; - - case PQI_ITEM_TYPE_TUNNELINITITEM: - pqi = pqipkt_maketunnelinitobj(pkt); - break; - - - //case PQI_ITEM_TYPE_AUTODISCITEM: - // pqi = pqipkt_makeadobj(pkt); - // break; - - case PQI_ITEM_TYPE_INFOITEM: // fall through as none - case PQI_ITEM_TYPE_COMMANDITEM: // of these should happen. - default: - { - std::ostringstream out; - out << "pqipkt_create() UNKNOWN TYPE :"; - out << pqipkt_getPQIType(pkt); - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - return NULL; - break; - } - if (pqi != NULL) - { - //pqi -> print(std::cerr); - } - else - { - pqioutput(PQL_ALERT, pqipktzone, - "pqipkt_create() Bad Packet"); - } - return pqi; -} - - -// Outgoing Pkts.... (These are raw packets....(a chunk of data)) -pqipkt *pqipkt_makepkt(PQItem *pqi) -{ - pqipkt *pkt = NULL; - { - std::ostringstream out; - out << "pqipkt_makepkt() Making pqipkt Based on Type:"; - out << pqi -> type << std::endl; - pqi -> print(out); - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - switch(pqi -> type) - { - case PQI_ITEM_TYPE_SEARCHITEM: - pkt = pqipkt_makesipkt((SearchItem *) pqi); - break; - - case PQI_ITEM_TYPE_FILEITEM: - pkt = pqipkt_makefipkt((PQFileItem *) pqi); - break; - - case PQI_ITEM_TYPE_CHATITEM: - pkt = pqipkt_makecipkt((ChatItem *) pqi); - break; - - case PQI_ITEM_TYPE_TUNNELITEM: - pkt = pqipkt_maketunnelpkt((PQTunnel *) pqi); - break; - - case PQI_ITEM_TYPE_TUNNELINITITEM: - pkt = pqipkt_maketunnelinitpkt((PQTunnelInit *) pqi); - break; - - //case PQI_ITEM_TYPE_AUTODISCITEM: - // pkt = pqipkt_makeadpkt((DiscItem *) pqi); - // return pkt; - // break; - - case PQI_ITEM_TYPE_INFOITEM: // fall through as none - case PQI_ITEM_TYPE_COMMANDITEM: // of these should happen. - default: - { - std::ostringstream out; - out << "pqipkt_makepkt() UNKNOWN TYPE :"; - out << pqi->type << "/" << pqi->subtype; - pqioutput(PQL_ALERT, pqipktzone, out.str()); - } - return NULL; - break; - } - - if (pkt != NULL) - { - //pqipkt_print(pkt); - } - else - { - std::ostringstream out; - out << "pqipkt_makepkt() NULL pkt"; - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - return pkt; -} - -// delete the raw packet (allocated using above). -void pqipkt_delete(pqipkt *pkt) -{ - free(pkt); - return; -} - - -// delete the raw packet (allocated using above). -void pqipkt_print(pqipkt *pkt) -{ - int len = pqipkt_getPQILen(pkt); - std::ostringstream out; - - out << "pqipkt_print(pkt): Type("; - out << pqipkt_getPQIType(pkt) << "/"; - out << pqipkt_getPQISubType(pkt) << ") - len:"; - out << len << std::endl; - for(int i = 0; i < len; i++) - { - if ((i > 0) && (i % 20 == 0)) - out << std::endl; - - out << std::hex; - int val = (int) ((unsigned char *) pkt)[i]; - if (val < 16) - out << "0" << val << " "; - else - out << val << " "; - - } - out << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - - return; -} - - - - - - - - - - -/************ PQI PACKET DESCRIPTION ****************** - * - * VERY BASIC at the moment. - * - * 4 bytes -> PQI Tag + Version. - * 2 bytes -> PKT_TYPE - * 2 bytes -> PKT_SUBTYPE - * 4 bytes -> PKT_LEN - * 4 bytes -> PKT_SID - * 4 bytes -> PKT_FLAGS - * - * Total (20) bytes. - **************************************************/ - - -void pqipkt_setPQITag(pqipkt *pkt) -{ - char *str = (char *) pkt; - strncpy(str, "PQ11", 4); - return; -} - -void pqipkt_setPQIType(void *pkt, int type) -{ - uint16_t *ptype = (uint16_t *) &(((char *) pkt)[4]); - ptype[0] = htons(type); - return; -} - - -int pqipkt_getPQIType(void *pkt) -{ - uint16_t *ptype = (uint16_t *) &(((char *) pkt)[4]); - return ntohs(ptype[0]); -} - -void pqipkt_setPQISubType(void *pkt, int subtype) -{ - uint16_t *ptype = (uint16_t *) &(((char *) pkt)[6]); - ptype[0] = htons(subtype); - return; -} - -int pqipkt_getPQISubType(void *pkt) -{ - uint16_t *ptype = (uint16_t *) &(((char *) pkt)[6]); - return ntohs(ptype[0]); -} - -void pqipkt_setPQILen(void *pkt, int len) -{ - uint32_t *ptype = (uint32_t *) &(((char *) pkt)[8]); - ptype[0] = htonl(len); - return; -} - -int pqipkt_getPQILen(void *pkt) -{ - uint32_t *ptype = (uint32_t *) &(((char *) pkt)[8]); - return ntohl(ptype[0]); -} - - -void pqipkt_setPQIsid(void *pkt, int sid) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[12]); - loc[0] = htonl(sid); - return; -} - - -int pqipkt_getPQIsid(void *pkt) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[12]); - return ntohl(loc[0]); -} - - -void pqipkt_setPQIFlags(void *pkt, int flags) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[16]); - loc[0] = htonl(flags); - return; -} - -int pqipkt_getPQIFlags(void *pkt) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[16]); - return ntohl(loc[0]); -} - - -// end of the PQI data fields. - - -/************ SI PACKET DESCRIPTION ****************** - * - * PQI + - * - * 2 bytes -> SI_DATATYPE - * 256 bytes -> DATA - * - * Total (20 + 2 + 256 = 278) bytes. - **************************************************/ - -// Outgoing Pkts.... (These are raw packets....(a chunk of data)) -pqipkt *pqipkt_makesipkt(SearchItem *si) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_makesipkt"); - - pqipkt *pkt = (pqipkt *) malloc(278); - pqipkt_setPQITag(pkt); - pqipkt_setPQIType(pkt, PQI_ITEM_TYPE_SEARCHITEM); - pqipkt_setPQISubType(pkt, si -> subtype); - pqipkt_setPQILen(pkt, 278); - pqipkt_setPQIFlags(pkt, si -> flags); - pqipkt_setPQIsid(pkt, si -> sid); - - pqipkt_setSIdatatype(pkt, si -> datatype); - pqipkt_setSIdata(pkt, (si -> data).c_str()); - - return pkt; -} - -SearchItem *pqipkt_makesiobj(pqipkt *pkt) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_makesiobj"); - - // only one type... - SearchItem *si = new SearchItem(); - //pqipkt_getPQITag(pkt); - si -> type = PQI_ITEM_TYPE_SEARCHITEM; - si -> subtype = pqipkt_getPQISubType(pkt); - si -> flags = pqipkt_getPQIFlags(pkt); - si -> sid = pqipkt_getPQIsid(pkt); - - si -> datatype = pqipkt_getSIdatatype(pkt); - si -> data = pqipkt_getSIdata(pkt); - return si; -} - -void pqipkt_setSIdatatype(void *pkt, int datatype) -{ - uint16_t *ptype = (uint16_t *) &(((char *) pkt)[20]); - ptype[0] = htons(datatype); - return; -} - -int pqipkt_getSIdatatype(void *pkt) -{ - uint16_t *ptype = (uint16_t *) &(((char *) pkt)[20]); - return ntohs(ptype[0]); -} - -void pqipkt_setSIdata(void *pkt, const char *in) -{ - char *str = &(((char *) pkt)[22]); - strncpy(str, in, 256); -} - - -std::string pqipkt_getSIdata(void *pkt) -{ - char *str = &(((char *) pkt)[22]); - str[255] = '\0'; - return std::string(str); -} - - -/************ FI PACKET DESCRIPTION ****************** - * - * PQI + - * 256 bytes -> hash; - * 256 bytes -> name; - * 16 bytes -> ext; - * 4 bytes -> len; - * 4 bytes -> offset; // data req - * 4 bytes -> chunk; // data req - * 4 bytes -> reqtype; (now needed) - * 4 bytes -> exttype; - * 512 bytes -> path; - * - * Total (20 + 512 + 512 + 16 + 4*5 = 1080) bytes. - **************************************************/ - - -pqipkt *pqipkt_makefipkt(PQFileItem *fi) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_makefipkt"); - - if (fi -> subtype == PQI_FI_SUBTYPE_DATA) - { - return pqipkt_makefdpkt( (PQFileData *) fi); - } - - int len = 1080; - pqipkt *pkt = (pqipkt *) malloc(len); - pqipkt_setPQITag(pkt); - pqipkt_setPQIType(pkt, PQI_ITEM_TYPE_FILEITEM); - pqipkt_setPQISubType(pkt, fi -> subtype); - pqipkt_setPQILen(pkt, len); - pqipkt_setPQIFlags(pkt, fi -> flags); - pqipkt_setPQIsid(pkt, fi -> sid); - - pqipkt_setFIhash(pkt, (fi -> hash).c_str()); - pqipkt_setFIname(pkt, (fi -> name).c_str()); - - pqipkt_setFIext(pkt, (fi -> ext).c_str()); - pqipkt_setFIlen(pkt, fi -> size); - - pqipkt_setFIoffset(pkt, fi -> fileoffset); - pqipkt_setFIchunksize(pkt, fi -> chunksize); - - pqipkt_setFIreqtype(pkt, fi -> reqtype); - pqipkt_setFIexttype(pkt, fi -> exttype); - pqipkt_setFIpath(pkt, (fi -> path).c_str()); - - return pkt; -} - -PQFileItem *pqipkt_makefiobj(pqipkt *pkt) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_makefiobj"); - - if (pqipkt_getPQISubType(pkt) == PQI_FI_SUBTYPE_DATA) - { - return pqipkt_makefdobj(pkt); - } - - // only one type... - PQFileItem *fi = new PQFileItem(); - //pqipkt_getPQITag(pkt); - fi -> type = PQI_ITEM_TYPE_FILEITEM; - fi -> subtype = pqipkt_getPQISubType(pkt); - fi -> flags = pqipkt_getPQIFlags(pkt); - fi -> sid = pqipkt_getPQIsid(pkt); - - fi -> hash = pqipkt_getFIhash(pkt); - fi -> name = pqipkt_getFIname(pkt); - fi -> ext = pqipkt_getFIext(pkt); - fi -> size = pqipkt_getFIlen(pkt); - - fi -> fileoffset = pqipkt_getFIoffset(pkt); - fi -> chunksize = pqipkt_getFIchunksize(pkt); - - fi -> reqtype = pqipkt_getFIreqtype(pkt); - fi -> exttype = pqipkt_getFIexttype(pkt); - - fi -> path = pqipkt_getFIpath(pkt); - - return fi; -} - -void pqipkt_setFIhash(void *pkt, const char *in) -{ - char *str = &(((char *) pkt)[20]); - strncpy(str, in, 256); -} - -std::string pqipkt_getFIhash(void *pkt) -{ - char *str = &(((char *) pkt)[20]); - str[255] = '\0'; - return std::string(str); -} - -void pqipkt_setFIname(void *pkt, const char *in) -{ - char *str = &(((char *) pkt)[276]); - strncpy(str, in, 256); -} - -std::string pqipkt_getFIname(void *pkt) -{ - char *str = &(((char *) pkt)[276]); - str[255] = '\0'; - return std::string(str); -} - -void pqipkt_setFIext(void *pkt, const char *in) -{ - char *str = &(((char *) pkt)[532]); - strncpy(str, in, 16); -} - -std::string pqipkt_getFIext(void *pkt) -{ - char *str = &(((char *) pkt)[532]); - str[15] = '\0'; - return std::string(str); -} - -void pqipkt_setFIlen(void *pkt, int len) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[548]); - loc[0] = htonl(len); - return; -} - -int pqipkt_getFIlen(void *pkt) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[548]); - return ntohl(loc[0]); -} - - -void pqipkt_setFIoffset(void *pkt, int offset) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[552]); - loc[0] = htonl(offset); - return; -} - -int pqipkt_getFIoffset(void *pkt) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[552]); - return ntohl(loc[0]); -} - - -void pqipkt_setFIchunksize(void *pkt, int size) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[556]); - loc[0] = htonl(size); - return; -} - -int pqipkt_getFIchunksize(void *pkt) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[556]); - return ntohl(loc[0]); -} - - -void pqipkt_setFIreqtype(void *pkt, int len) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[560]); - loc[0] = htonl(len); - return; -} - -int pqipkt_getFIreqtype(void *pkt) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[560]); - return ntohl(loc[0]); -} - -void pqipkt_setFIexttype(void *pkt, int len) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[564]); - loc[0] = htonl(len); - return; -} - -int pqipkt_getFIexttype(void *pkt) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[564]); - return ntohl(loc[0]); -} - - -void pqipkt_setFIpath(void *pkt, const char *in) -{ - char *str = &(((char *) pkt)[568]); - strncpy(str, in, 512); -} - -std::string pqipkt_getFIpath(void *pkt) -{ - char *str = &(((char *) pkt)[568]); - str[511] = '\0'; - return std::string(str); -} - - -/************ SMI(Chat) PACKET DESCRIPTION ****************** - * OLD FORMATS>>>> - * - * PQI + - * 256 bytes -> msg; - * - * Total (20 + 256 = 276) bytes. - **************************************************/ -/************ MI PACKET DESCRIPTION ****************** - * - * SMI + - * 4 bytes -> datalen; - * 4 bytes -> flags; // if attachment - * 4 bytes -> size; // if attachment - * 256 bytes -> hash; - * 256 bytes -> name; - * 256 bytes -> channel name; - * n bytes -> msg. - * - * Total (276 + 4 + 4 + 4 + 768 + n) bytes. - * = 1056 + n - **************************************************/ - - -/************ SMI(Chat) PACKET DESCRIPTION ****************** - * - * PQI + - * 4 bytes -> msgsize - * n bytes -> msg; - * - * Total (20 + 4 + n = 24 + n) bytes. - **************************************************/ -/************ MI PACKET DESCRIPTION ****************** - * - * SMI + - * 4 bytes -> flags; - * 4 bytes -> sendTime; - * 4 bytes -> titleSize; - * n bytes -> title - * 4 bytes -> headerSize; - * m bytes -> header; - * 4 bytes -> noFiles; - * (per file) - * 4 bytes -> size; - * 4 bytes -> hashsize - * o bytes -> hash - * 4 bytes -> namesize - * p bytes -> name; - * - * Total (SMI + 4 + 4 + 4 + n + 4 + m + 4 + files) bytes. - **************************************************/ - -pqipkt *pqipkt_makecipkt(ChatItem *ci) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_makecipkt"); - - MsgItem *mi; - pqipkt *pkt; - int len; - int offset = 0; - - int baselen = 20 + 4 + ci->msg.size(); - - if ((mi = dynamic_cast(ci)) != NULL) - { - int extralen = 5 * 4; - extralen += mi->title.size(); - extralen += mi->header.size(); - - /* plus files */ - std::list::iterator it; - for(it = mi->files.begin(); it != mi->files.end(); it++) - { - extralen += 12; - extralen += it -> name.size(); - extralen += it -> hash.size(); - } - - len = baselen + extralen; - if (len > pqipkt_maxsize()) - { - /* cannot make packet! */ - return NULL; - } - if (len < pqipkt_basesize()) - { - len = pqipkt_basesize(); - } - - pkt = malloc(len); - - // fill it the base. - pqipkt_setPQITag(pkt); - pqipkt_setPQIType(pkt, PQI_ITEM_TYPE_CHATITEM); - pqipkt_setPQISubType(pkt, mi -> subtype); - pqipkt_setPQILen(pkt, len); - pqipkt_setPQIFlags(pkt, mi -> flags); - pqipkt_setPQIsid(pkt, mi -> sid); // id irrelevant. - - offset = 20; - // Msg - pqipkt_setMIuint32(pkt, offset, mi -> msg.size()); - offset += 4; - pqipkt_setMIstring(pkt, offset, mi -> msg); - offset += mi->msg.size(); - - // Actual Msg part.... - pqipkt_setMIuint32(pkt, offset, mi -> msgflags); - offset += 4; - pqipkt_setMIuint32(pkt, offset, mi -> sendTime); - offset += 4; - // Title - pqipkt_setMIuint32(pkt, offset, mi -> title.size()); - offset += 4; - pqipkt_setMIstring(pkt, offset, mi -> title); - offset += mi->title.size(); - // Header - pqipkt_setMIuint32(pkt, offset, mi -> header.size()); - offset += 4; - pqipkt_setMIstring(pkt, offset, mi -> header); - offset += mi->header.size(); - // nofiles. - pqipkt_setMIuint32(pkt, offset, mi -> files.size()); - offset += 4; - // Now pack those files.. - for(it = mi->files.begin(); it != mi->files.end(); it++) - { - // size. - pqipkt_setMIuint32(pkt, offset, it -> size); - offset += 4; - // hash - pqipkt_setMIuint32(pkt, offset, it -> hash.size()); - offset += 4; - pqipkt_setMIstring(pkt, offset, it -> hash); - offset += it->hash.size(); - // name - pqipkt_setMIuint32(pkt, offset, it -> name.size()); - offset += 4; - pqipkt_setMIstring(pkt, offset, it -> name); - offset += it->name.size(); - } - - if (offset != len) - { - /* error! */ - } - - return pkt; - } - // only chatitem. - len = baselen; - if (len < pqipkt_basesize()) - { - len = pqipkt_basesize(); - } - - pkt = malloc(len); - - pqipkt_setPQITag(pkt); - pqipkt_setPQIType(pkt, PQI_ITEM_TYPE_CHATITEM); - pqipkt_setPQISubType(pkt, ci -> subtype); - pqipkt_setPQILen(pkt, len); - pqipkt_setPQIFlags(pkt, ci -> flags); - pqipkt_setPQIsid(pkt, ci -> sid); // id irrelevant. - - // fill in the msg. - offset = 20; - // Msg - pqipkt_setMIuint32(pkt, offset, ci -> msg.size()); - offset += 4; - pqipkt_setMIstring(pkt, offset, ci -> msg); - offset += ci->msg.size(); - - return pkt; -} - - -ChatItem *pqipkt_makeciobj(pqipkt *pkt) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_makeciobj"); - - int len = pqipkt_getPQILen(pkt); - int offset = 0; - int namelen = 0; - int i; - - // switch based on the subtype; - if (pqipkt_getPQISubType(pkt) == PQI_MI_SUBTYPE_CHAT) - { - // only short pkt! - ChatItem *ci = new ChatItem(); - //pqipkt_getPQITag(pkt); - ci -> type = PQI_ITEM_TYPE_CHATITEM; - ci -> subtype = pqipkt_getPQISubType(pkt); - ci -> flags = pqipkt_getPQIFlags(pkt); - ci -> sid = pqipkt_getPQIsid(pkt); - - offset = 20; - namelen = pqipkt_getMIuint32(pkt, offset); - offset += 4; - - if (offset + namelen > len) - { - /* error */ - pqioutput(PQL_ALERT, pqipktzone, "pqipkt_makeciobj ALERT size error CI(1)"); - delete ci; - return NULL; - } - - ci -> msg = pqipkt_setMIstring(pkt, offset, namelen); - return ci; - } - - // else message - - MsgItem *mi = new MsgItem(); - //pqipkt_getPQITag(pkt); - mi -> type = PQI_ITEM_TYPE_CHATITEM; - mi -> subtype = pqipkt_getPQISubType(pkt); - mi -> flags = pqipkt_getPQIFlags(pkt); - mi -> sid = pqipkt_getPQIsid(pkt); - - bool pktOkay = true; - - offset = 20; - if (offset + 4 > len) pktOkay = false; - if (pktOkay) - { - namelen = pqipkt_getMIuint32(pkt, offset); - offset += 4; - } - - if (offset + namelen > len) pktOkay = false; - if (pktOkay) - { - mi -> msg = pqipkt_setMIstring(pkt, offset, namelen); - offset += namelen; - } - - /* now check the sizes */ - if (offset + 12 > len) pktOkay = false; - if (pktOkay) - { - // Actual Msg part.... - mi -> msgflags = pqipkt_getMIuint32(pkt, offset); - offset += 4; - mi -> sendTime = pqipkt_getMIuint32(pkt, offset); - offset += 4; - - // Title - namelen = pqipkt_getMIuint32(pkt, offset); - offset += 4; - } - - if (offset + namelen > len) pktOkay = false; - if (pktOkay) - { - mi -> title = pqipkt_setMIstring(pkt, offset, namelen); - offset += namelen; - } - - // Header - if (offset + 4 > len) pktOkay = false; - if (pktOkay) - { - namelen = pqipkt_getMIuint32(pkt, offset); - offset += 4; - } - - if (offset + namelen > len) pktOkay = false; - if (pktOkay) - { - mi -> header = pqipkt_setMIstring(pkt, offset, namelen); - offset += namelen; - } - - // nofiles. - if (offset + 4 > len) pktOkay = false; - int nofiles = 0; - if (pktOkay) - { - nofiles = pqipkt_getMIuint32(pkt, offset); - offset += 4; - } - - for(i = 0; i < nofiles; i++) - { - MsgFileItem mfi; - - if (offset + 8 > len) pktOkay = false; - if (pktOkay) - { - mfi.size = pqipkt_getMIuint32(pkt, offset); - offset += 4; - - // Hash - namelen = pqipkt_getMIuint32(pkt, offset); - offset += 4; - } - - if (offset + namelen > len) pktOkay = false; - if (pktOkay) - { - mfi.hash = pqipkt_setMIstring(pkt, offset, namelen); - offset += namelen; - } - - if (offset + 4 > len) pktOkay = false; - if (pktOkay) - { - // Name - namelen = pqipkt_getMIuint32(pkt, offset); - offset += 4; - } - - if (offset + namelen > len) pktOkay = false; - if (pktOkay) - { - mfi.name = pqipkt_setMIstring(pkt, offset, namelen); - offset += namelen; - - mi -> files.push_back(mfi); - } - } - if (pktOkay) - { - return mi; - } - else - { - /* error */ - pqioutput(PQL_ALERT, pqipktzone, "pqipkt_makeciobj ALERT size error MI(1)"); - - delete mi; - return NULL; - } -} - -void pqipkt_setMIuint32(void *pkt, int offset, unsigned long val) -{ - /* stick the value at the offset! */ - char *buf = (char *) pkt + offset; - *((unsigned long *) buf) = htonl(val); -} - -void pqipkt_setMIstring(void *pkt, int offset, std::string name) -{ - /* stick the value at the offset! */ - char *str = &(((char *) pkt)[offset]); - for(unsigned int i = 0; i < name.size(); i++) - { - str[i] = name[i]; - } -} - -unsigned long pqipkt_getMIuint32(void *pkt, int offset) -{ - /* stick the value at the offset! */ - char *buf = (char *) pkt + offset; - unsigned long val = ntohl(*((unsigned long *) buf)); - return val; -} - -std::string pqipkt_setMIstring(void *pkt, int offset, int len) -{ - /* stick the value at the offset! */ - char *str = &(((char *) pkt)[offset]); - std::string val; - for(int i = 0; i < len; i++) - { - val += str[i]; - } - return val; -} - -/************ FD PACKET DESCRIPTION ****************** - * - * PQI + - * (basic FI) - * 256 bytes -> hash; - * 256 bytes -> name; - * 16 bytes -> ext; - * 4 bytes -> len; - * 4 bytes -> offset; // data req - * 4 bytes -> chunk; // data req - * 4 bytes -> reqtype; (now needed) - * 4 bytes -> exttype; - * SubTotal (20 + 512 + 16 + 4*5 = 568) bytes. - * - * - * 4 bytes -> datalen; - * 4 bytes -> flags; - * n bytes -> data. - * - * Total (568 + 4 + 4 + n) bytes. - **************************************************/ - -pqipkt *pqipkt_makefdpkt(PQFileData *fd) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_makefdpkt"); - - pqipkt *pkt; - int len; - //is a MsgItem mi. - //calc length of the message. - len = 568 + 4 + 4 + fd -> datalen; - // but len also has a minimum length of 276 bytes.... - if (len < 276) - { - len = 276; - } - - pkt = malloc(len); - - // fill it in. - pqipkt_setPQITag(pkt); - pqipkt_setPQIType(pkt, PQI_ITEM_TYPE_FILEITEM); - pqipkt_setPQISubType(pkt, fd -> subtype); - pqipkt_setPQILen(pkt, len); - pqipkt_setPQIFlags(pkt, fd -> flags); - pqipkt_setPQIsid(pkt, fd -> sid); - - // Fi fields - pqipkt_setFIhash(pkt, (fd -> hash).c_str()); - pqipkt_setFIname(pkt, (fd -> name).c_str()); - pqipkt_setFIext(pkt, (fd -> ext).c_str()); - pqipkt_setFIlen(pkt, fd -> size); - pqipkt_setFIoffset(pkt, fd -> fileoffset); - pqipkt_setFIchunksize(pkt, fd -> chunksize); - pqipkt_setFIreqtype(pkt, fd -> reqtype); - pqipkt_setFIexttype(pkt, fd -> exttype); - - pqipkt_setFDlen(pkt, fd -> datalen); - pqipkt_setFDflags(pkt, fd -> fileflags); - pqipkt_setFDdata(pkt, (fd -> data), fd -> datalen); - - return pkt; -} - - -PQFileData *pqipkt_makefdobj(pqipkt *pkt) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_makefdobj"); - - PQFileData *fd = new PQFileData(); - //pqipkt_getPQITag(pkt); - fd -> type = PQI_ITEM_TYPE_FILEITEM; - fd -> subtype = pqipkt_getPQISubType(pkt); - fd -> flags = pqipkt_getPQIFlags(pkt); - fd -> sid = pqipkt_getPQIsid(pkt); - - // FI Stuff. - fd -> hash = pqipkt_getFIhash(pkt); - fd -> name = pqipkt_getFIname(pkt); - fd -> ext = pqipkt_getFIext(pkt); - fd -> size = pqipkt_getFIlen(pkt); - - fd -> fileoffset = pqipkt_getFIoffset(pkt); - fd -> chunksize = pqipkt_getFIchunksize(pkt); - - fd -> reqtype = pqipkt_getFIreqtype(pkt); - fd -> exttype = pqipkt_getFIexttype(pkt); - - // then the fd stuff. - fd -> datalen = pqipkt_getFDlen(pkt); - fd -> fileflags = pqipkt_getFDflags(pkt); - fd -> data = malloc(fd -> datalen); - pqipkt_getFDdata(pkt, fd -> data, fd -> datalen); - - return fd; -} - - -void pqipkt_setFDlen(void *pkt, int len) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[568]); - loc[0] = htonl(len); - return; -} - -int pqipkt_getFDlen(void *pkt) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[568]); - return ntohl(loc[0]); -} - - - -void pqipkt_setFDflags(void *pkt, int flags) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[572]); - loc[0] = htonl(flags); - return; -} - - -int pqipkt_getFDflags(void *pkt) -{ - uint32_t *loc = (uint32_t *) &(((char *) pkt)[572]); - return ntohl(loc[0]); -} - - -void pqipkt_setFDdata(void *pkt, void *data, int datalen) -{ - char *str = &(((char *) pkt)[576]); - memcpy(str, data, datalen); - if (datalen != pqipkt_getFDlen(pkt)) - { - pqioutput(PQL_ALERT, pqipktzone, - "pqipkt_setFDdata() len != mi.len"); - } -} - - -void pqipkt_getFDdata(void *pkt, void *data, int datalen) -{ - char *str = &(((char *) pkt)[576]); - memcpy(data, str, datalen); // copy to data. -} - - -// Tunnel Functions. -pqipkt *pqipkt_maketunnelpkt(PQTunnel *item) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_maketunnelpkt"); - - pqipkt *pkt; - int len; - len = 20 + 4 + item -> getSize(); - // but len also has a minimum length of 276 bytes.... - if (len < 276) - { - len = 276; - } - - { - std::ostringstream out; - out << "pqipkt_maketunnelpkt len:" << len; - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - pkt = malloc(len); - for(int i = 0; i < len; i++) - { - ((char *)pkt)[0] = 0; - } - - // fill it in. - pqipkt_setPQITag(pkt); - pqipkt_setPQIType(pkt, PQI_ITEM_TYPE_TUNNELITEM); - pqipkt_setPQISubType(pkt, item -> subtype); - pqipkt_setPQILen(pkt, len); - pqipkt_setPQIFlags(pkt, item -> flags); - pqipkt_setPQIsid(pkt, item -> sid); - - { - std::ostringstream out; - out << "pqipkt_maketunnelpkt datalen:" << item -> getSize(); - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - pqipkt_setTunnellen(pkt, item -> getSize()); - if (item -> getSize() == - item -> out(pqipkt_getTunneldata(pkt), item -> getSize())) - { - return pkt; - } - // else - free(pkt); - return NULL; -} - - -PQTunnel *pqipkt_maketunnelobj(pqipkt *pkt) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_maketunnelobj"); - - // Check the Tunnel Length... - int len = pqipkt_getPQILen(pkt); - if (len < (int) 24 + pqipkt_getTunnellen(pkt)) - { - std::ostringstream out; - out << "Invalid Tunnel Length - Discarding Incoming Packet"; - out << "Len: " << len << " < 24 + "; - out << pqipkt_getTunnellen(pkt); - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - - return NULL; - } - - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "creating a Tunnel Obj"); - - PQTunnel *nitem = createTunnelType(pqipkt_getPQISubType(pkt), - pqipkt_getTunneldata(pkt), pqipkt_getTunnellen(pkt)); - { - std::ostringstream out; - out << "Pkt Tunnel Incoming: st: "; - out << pqipkt_getPQISubType(pkt) << "DataLen: " << - out << pqipkt_getTunnellen(pkt) << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - - if (!nitem) - { - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_maketunnelobj returning Null"); - return NULL; - } - - nitem -> type = PQI_ITEM_TYPE_TUNNELITEM; - nitem -> subtype = pqipkt_getPQISubType(pkt); - nitem -> flags = pqipkt_getPQIFlags(pkt); - nitem -> sid = pqipkt_getPQIsid(pkt); - - nitem -> in(pqipkt_getTunneldata(pkt), pqipkt_getTunnellen(pkt)); - return nitem; -} - - - -void pqipkt_setTunnellen(void *pkt, int len) -{ - int *loc = (int *) &(((char *) pkt)[20]); - (*loc) = len; -} - -int pqipkt_getTunnellen(void *pkt) -{ - int *loc = (int *) &(((char *) pkt)[20]); - return (*loc); -} - -void *pqipkt_getTunneldata(void *pkt) -{ - void *loc = (void *) &(((char *) pkt)[24]); - return loc; -} - - -// Tunnel Functions. -pqipkt *pqipkt_maketunnelinitpkt(PQTunnelInit *item) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_maketunnelinitpkt"); - - pqipkt *pkt; - int len; - len = 20 + 4 + item -> getSize(); - // but len also has a minimum length of 276 bytes.... - if (len < 276) - { - len = 276; - } - - { - std::ostringstream out; - out << "pqipkt_maketunnelinitpkt len:" << len; - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - pkt = malloc(len); - for(int i = 0; i < len; i++) - { - ((char *)pkt)[0] = 0; - } - - // fill it in. - pqipkt_setPQITag(pkt); - pqipkt_setPQIType(pkt, PQI_ITEM_TYPE_TUNNELINITITEM); - pqipkt_setPQISubType(pkt, item -> subtype); - pqipkt_setPQILen(pkt, len); - pqipkt_setPQIFlags(pkt, item -> flags); - pqipkt_setPQIsid(pkt, item -> sid); - - { - std::ostringstream out; - out << "pqipkt_maketunnelinitpkt datalen:" << item -> getSize(); - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - pqipkt_setTunnellen(pkt, item -> getSize()); - if (item -> getSize() == - item -> out(pqipkt_getTunneldata(pkt), item -> getSize())) - { - return pkt; - } - // else - free(pkt); - return NULL; -} - - -PQTunnelInit *pqipkt_maketunnelinitobj(pqipkt *pkt) -{ - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_maketunnelinitobj"); - - // Check the Tunnel Length... - int len = pqipkt_getPQILen(pkt); - if (len < (int) 24 + pqipkt_getTunnellen(pkt)) - { - std::ostringstream out; - - out << "Invalid Tunnel Length - Discarding Incoming Packet" << std::endl; - out << "Len: " << len << " < 24 + "; - out << pqipkt_getTunnellen(pkt); - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - - return NULL; - } - - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "creating a Tunnel Init Obj"); - - PQTunnelInit *nitem = createTunnelInitType(pqipkt_getPQISubType(pkt), - pqipkt_getTunneldata(pkt), pqipkt_getTunnellen(pkt)); - { - std::ostringstream out; - out << "Pkt Init Tunnel Incoming: st: "; - out << pqipkt_getPQISubType(pkt) << "DataLen: " << - out << pqipkt_getTunnellen(pkt) << std::endl; - pqioutput(PQL_DEBUG_BASIC, pqipktzone, out.str()); - } - - - if (!nitem) - { - pqioutput(PQL_DEBUG_BASIC, pqipktzone, - "pqipkt_maketunnelinitobj returning Null"); - return NULL; - } - - nitem -> type = PQI_ITEM_TYPE_TUNNELINITITEM; - nitem -> subtype = pqipkt_getPQISubType(pkt); - nitem -> flags = pqipkt_getPQIFlags(pkt); - nitem -> sid = pqipkt_getPQIsid(pkt); - - nitem -> in(pqipkt_getTunneldata(pkt), pqipkt_getTunnellen(pkt)); - return nitem; -} - - -// ANOTHER TYPE DISCOVERY PACKETS... -// THIS HAS NOW BECOME A TUNNEL TYPE. -// -//void pqipkt_setADladdr(void *pkt, sockaddr_in addr); -//sockaddr_in pqipkt_getADladdr(void *pkt); -//void pqipkt_setADsaddr(void *pkt, sockaddr_in addr); -//sockaddr_in pqipkt_getADsaddr(void *pkt); -// -//int pqipkt_getADrecvTF(void *pkt); -//void pqipkt_setADrecvTF(void *pkt, int tf); -//int pqipkt_getADconnTF(void *pkt); -//void pqipkt_setADconnTF(void *pkt, int tf); -// -//void pqipkt_setADcertLen(void *pkt, int len); -//int pqipkt_getADcertLen(void *pkt); -//void pqipkt_getADcertDER(void *pkt, unsigned char *pktder, int len); -//void pqipkt_setADcertDER(void *pkt, unsigned char *pktder, int len); -// -// -//pqipkt *pqipkt_makeadpkt(DiscItem *ad) -//{ -// pqipkt *pkt; -// int len; -// -// if (ad -> subtype == AUTODISC_RDI_SUBTYPE_PING) -// { -// //len = 20 + 16 + 16 (addrs) + 4 + 4 (ints) -// len = 20 + 16 + 16 + 4 + 4; -// if (len < 276) -// len = 276; -// pkt = malloc(len); -// pqipkt_setPQITag(pkt); -// pqipkt_setPQIType(pkt, PQI_ITEM_TYPE_AUTODISCITEM); -// pqipkt_setPQISubType(pkt, ad -> subtype); -// pqipkt_setPQILen(pkt, len); -// pqipkt_setPQIFlags(pkt, ad -> flags); -// pqipkt_setPQIsid(pkt, ad -> sid); -// -// pqipkt_setADladdr(pkt, ad -> laddr); -// pqipkt_setADsaddr(pkt, ad -> saddr); -// pqipkt_setADrecvTF(pkt, ad -> receive_tf); -// pqipkt_setADconnTF(pkt, ad -> connect_tf); -// } -// else // RPLY -// { -// DiscReplyItem *dri = (DiscReplyItem *) ad; -// -// //len = 20 + 16 + 16 (addrs) + 4 + 4 (ints) + data; -// len = 20 + 16 + 16 + 4 + 4 + 4 + dri -> certLen; -// if (len < 276) -// len = 276; -// pkt = malloc(len); -// pqipkt_setPQITag(pkt); -// pqipkt_setPQIType(pkt, PQI_ITEM_TYPE_AUTODISCITEM); -// pqipkt_setPQISubType(pkt, ad -> subtype); -// pqipkt_setPQILen(pkt, len); -// pqipkt_setPQIFlags(pkt, ad -> flags); -// pqipkt_setPQIsid(pkt, ad -> sid); -// -// // connect details. -// pqipkt_setADladdr(pkt, dri -> laddr); -// pqipkt_setADsaddr(pkt, dri -> saddr); -// pqipkt_setADrecvTF(pkt, dri -> receive_tf); -// pqipkt_setADconnTF(pkt, dri -> connect_tf); -// -// // + certificate. -// pqipkt_setADcertLen(pkt, dri -> certLen); -// pqipkt_setADcertDER(pkt, dri -> certDER, dri -> certLen); -// -// } -// return pkt; -//} -// -// -//DiscItem *pqipkt_makeadobj(pqipkt *pkt) -//{ -// DiscItem *di; -// if (pqipkt_getPQISubType(pkt) == AUTODISC_RDI_SUBTYPE_PING) -// { -// DiscItem *fa = di = new DiscItem(); -// fa -> type = PQI_ITEM_TYPE_AUTODISCITEM; -// fa -> subtype = pqipkt_getPQISubType(pkt); -// fa -> flags = pqipkt_getPQIFlags(pkt); -// fa -> sid = pqipkt_getPQIsid(pkt); -// -// fa -> laddr = pqipkt_getADladdr(pkt); -// fa -> saddr = pqipkt_getADsaddr(pkt); -// fa -> receive_tf = pqipkt_getADrecvTF(pkt); -// fa -> connect_tf = pqipkt_getADconnTF(pkt); -// } -// else -// { -// DiscReplyItem *fa = new DiscReplyItem(); -// fa -> type = PQI_ITEM_TYPE_AUTODISCITEM; -// fa -> subtype = pqipkt_getPQISubType(pkt); -// fa -> flags = pqipkt_getPQIFlags(pkt); -// fa -> sid = pqipkt_getPQIsid(pkt); -// -// fa -> laddr = pqipkt_getADladdr(pkt); -// fa -> saddr = pqipkt_getADsaddr(pkt); -// fa -> receive_tf = pqipkt_getADrecvTF(pkt); -// fa -> connect_tf = pqipkt_getADconnTF(pkt); -// fa -> certLen = pqipkt_getADcertLen(pkt); -// fa -> certDER = (unsigned char *) malloc(fa -> certLen); -// pqipkt_getADcertDER(pkt, fa -> certDER, fa -> certLen); -// -// di = fa; -// } -// -// return di; -//} -// -// -//void pqipkt_setADladdr(void *pkt, struct sockaddr_in addr) -//{ -// struct sockaddr_in *loc = (struct sockaddr_in *) -// &(((char *) pkt)[20]); -// (*loc) = addr; -//} -// -//struct sockaddr_in pqipkt_getADladdr(void *pkt) -//{ -// struct sockaddr_in *loc = (struct sockaddr_in *) -// &(((char *) pkt)[20]); -// return (*loc); -//} -// -// -//void pqipkt_setADsaddr(void *pkt, struct sockaddr_in addr) -//{ -// struct sockaddr_in *loc = (struct sockaddr_in *) -// &(((char *) pkt)[36]); -// (*loc) = addr; -//} -// -//struct sockaddr_in pqipkt_getADsaddr(void *pkt) -//{ -// struct sockaddr_in *loc = (struct sockaddr_in *) -// &(((char *) pkt)[36]); -// return (*loc); -//} -// -// -//int pqipkt_getADrecvTF(void *pkt) -//{ -// uint32_t *loc = (uint32_t *) &(((char *) pkt)[52]); -// return ntohl(loc[0]); -//} -// -//void pqipkt_setADrecvTF(void *pkt, int tf) -//{ -// uint32_t *loc = (uint32_t *) &(((char *) pkt)[52]); -// loc[0] = htonl(tf); -// return; -//} -// -//int pqipkt_getADconnTF(void *pkt) -//{ -// uint32_t *loc = (uint32_t *) &(((char *) pkt)[56]); -// return ntohl(loc[0]); -//} -// -//void pqipkt_setADconnTF(void *pkt, int tf) -//{ -// uint32_t *loc = (uint32_t *) &(((char *) pkt)[56]); -// loc[0] = htonl(tf); -// return; -//} -// -//int pqipkt_getADcertLen(void *pkt) -//{ -// uint32_t *loc = (uint32_t *) &(((char *) pkt)[60]); -// return ntohl(loc[0]); -//} -// -//void pqipkt_setADcertLen(void *pkt, int len) -//{ -// uint32_t *loc = (uint32_t *) &(((char *) pkt)[60]); -// loc[0] = htonl(len); -// return; -//} -// -//void pqipkt_getADcertDER(void *pkt, unsigned char *pktder, int len) -//{ -// unsigned char *str = &(((unsigned char *) pkt)[64]); -// memcpy(pktder, str, len); -// if (len != pqipkt_getADcertLen(pkt)) -// { -// std::cerr << "pqipkt_getADcertDER() len != ad.len" << std::endl; -// } -//} -// -// -//void pqipkt_setADcertDER(void *pkt, unsigned char *pktder, int len) -//{ -// unsigned char *str = &(((unsigned char *) pkt)[64]); -// memcpy(str, pktder, len); -// if (len != pqipkt_getADcertLen(pkt)) -// { -// std::cerr << "pqipkt_setADcertDER() len != ad.len" << std::endl; -// } -//} -// -// -// diff --git a/libretroshare/src/pqi/pqipacket.h b/libretroshare/src/pqi/pqipacket.h deleted file mode 100644 index 195555003..000000000 --- a/libretroshare/src/pqi/pqipacket.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * "$Id: pqipacket.h,v 1.6 2007-02-18 21:46:49 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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". - * - */ - - - -#ifndef MRK_PQI_PACKET_HEADER -#define MRK_PQI_PACKET_HEADER - - -/* This is a set of functions for packaging - * the search queries/replies/data into packets - * - * Currently it makes very simple 1kb packets. - * - */ - -// The standard data types and the search interface. -#include "pqi/pqi.h" - -// defs for streams. -#include - -typedef void pqipkt; - -// Handle Incoming Pkts...... - -// constants -int pqipkt_basesize(); -int pqipkt_maxsize(); - -// determine properties. (possible from incomplete pkt). -int pqipkt_rawlen(pqipkt *); -bool pqipkt_check(pqipkt *pkt, int size); - -PQItem *pqipkt_create(pqipkt *block); - -// Outgoing Pkts.... (These are raw packets....(a chunk of data)) -pqipkt *pqipkt_makepkt(PQItem *pqi); - -// delete the raw packet (allocated using above). -void pqipkt_delete(pqipkt *); - -// print packet. -// -void pqipkt_print(pqipkt *pkt); - -// Type Extension methods for PQTunnel. -class PQTunnel; -class PQTunnelInit; - -int registerTunnelType(int subtype, PQTunnel *(*fn)(void *d, int n)); -PQTunnel *createTunnelType(int subtype, void *d, int n); - -int registerTunnelInitType(int subtype, PQTunnelInit *(*fn)(void *d, int n)); -PQTunnelInit *createTunnelInitType(int subtype, void *d, int n); - -#endif //MRK_PQI_PACKET_HEADER diff --git a/libretroshare/src/pqi/pqiperson.cc b/libretroshare/src/pqi/pqiperson.cc index 81ed9a720..095548d23 100644 --- a/libretroshare/src/pqi/pqiperson.cc +++ b/libretroshare/src/pqi/pqiperson.cc @@ -1,5 +1,5 @@ /* - * "$Id: pqiperson.cc,v 1.9 2007-02-18 21:46:49 rmf24 Exp $" + * libretroshare/src/pqi pqiperson.cc * * 3P/PQI network interface for RetroShare. * @@ -25,7 +25,6 @@ - #include "pqi/pqi.h" #include @@ -37,8 +36,8 @@ const int pqipersonzone = 82371; #include -pqiperson::pqiperson(cert *c) - :sslcert(c), active(false), activepqi(NULL), +pqiperson::pqiperson(cert *c, std::string id) + :PQInterface(id), sslcert(c), active(false), activepqi(NULL), inConnectAttempt(false), waittimes(0) { sroot = getSSLRoot(); @@ -77,7 +76,7 @@ pqiperson::~pqiperson() // The PQInterface interface. -int pqiperson::SendItem(PQItem *i) +int pqiperson::SendItem(RsItem *i) { std::ostringstream out; out << "pqiperson::SendItem()"; @@ -91,15 +90,13 @@ int pqiperson::SendItem(PQItem *i) out << " Not Active: Used to put in ToGo Store"; out << std::endl; out << " Now deleting..."; - // no longer storing in togo... - //togo.push_back(i); delete i; } pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str()); return 0; // queued. } -PQItem *pqiperson::GetItem() +RsItem *pqiperson::GetItem() { if (active) return activepqi -> GetItem(); @@ -184,27 +181,7 @@ int pqiperson::tick() } } } - // if active and togo!. - else if (togo.size() > 0) - { - while(togo.size() > 0) - { - PQItem *item = togo.front(); - togo.pop_front(); - std::ostringstream out; - out << "pqiperson::tick() Sending on Stored Item"; - out << " to activepqi @ " << (void *) activepqi; - out << std::endl; - item -> print(out); - out << std::endl; - - pqioutput(PQL_DEBUG_BASIC, pqipersonzone, out.str()); - - activepqi -> SendItem(item); - } - activeTick = 1; - } return activeTick; } diff --git a/libretroshare/src/pqi/pqiperson.h b/libretroshare/src/pqi/pqiperson.h index b82ed4d8d..403ad8f05 100644 --- a/libretroshare/src/pqi/pqiperson.h +++ b/libretroshare/src/pqi/pqiperson.h @@ -1,5 +1,5 @@ /* - * "$Id: pqiperson.h,v 1.7 2007-02-18 21:46:49 rmf24 Exp $" + * libretroshare/src/pqi pqiperson.h * * 3P/PQI network interface for RetroShare. * @@ -60,9 +60,9 @@ static const int CONNECT_FAILED = 5; class pqiconnect: public pqistreamer, public NetInterface { public: - pqiconnect(NetBinInterface *ni_in) - :pqistreamer(ni_in, 0), // pqistreamer will cleanup NetInterface. - NetInterface(NULL, NULL), // No need for callback. + pqiconnect(RsSerialiser *rss, NetBinInterface *ni_in) + :pqistreamer(rss, ni_in->PeerId(), ni_in, 0), // pqistreamer will cleanup NetInterface. + NetInterface(NULL, ni_in->PeerId()), // No need for callback. ni(ni_in) { if (!ni_in) @@ -84,15 +84,15 @@ virtual int reset() { return ni -> reset(); } virtual int disconnect() { return ni -> reset(); } // get the contact from the net side! -virtual Person *getContact() +virtual std::string PeerId() { if (ni) { - return ni->getContact(); + return ni->PeerId(); } else { - return PQInterface::getContact(); + return PQInterface::PeerId(); } } @@ -108,7 +108,7 @@ protected: class pqiperson: public PQInterface { public: - pqiperson(cert *c); + pqiperson(cert *c, std::string id); virtual ~pqiperson(); // must clean up children. // control of the connection. @@ -119,8 +119,8 @@ int autoconnect(bool); int addChildInterface(pqiconnect *pqi); // The PQInterface interface. -virtual int SendItem(PQItem *); -virtual PQItem *GetItem(); +virtual int SendItem(RsItem *); +virtual RsItem *GetItem(); virtual int status(); virtual int tick(); @@ -135,8 +135,6 @@ virtual void setMaxRate(bool in, float val); private: - // outgoing PQItems can be queued (nothing else). - std::list togo; std::list kids; cert *sslcert; bool active; diff --git a/libretroshare/src/pqi/pqipersongrp.cc b/libretroshare/src/pqi/pqipersongrp.cc index 638c6811d..dc4a07af6 100644 --- a/libretroshare/src/pqi/pqipersongrp.cc +++ b/libretroshare/src/pqi/pqipersongrp.cc @@ -3,7 +3,7 @@ * * 3P/PQI network interface for RetroShare. * - * Copyright 2004-2006 by Robert Fernie. + * Copyright 2004-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 @@ -28,23 +28,9 @@ #include "pqi/pqipersongrp.h" -/* -#include "pqi/pqiproxy.h" -#include "pqi/pqitunnelproxy.h" - */ - - -#include "pqi/p3disc.h" -#include "pqi/p3channel.h" - #include "pqi/pqissl.h" #include "pqi/pqissllistener.h" -/* - * #include "pqi/pqiudpproxy.h" - * #include "pqi/pqissludp.h" - */ - #ifdef PQI_USE_PROXY #include "pqi/pqiudpproxy.h" #include "pqi/pqissludp.h" @@ -57,25 +43,11 @@ const int pqipersongrpzone = 354; -// get the Tunnel and TunnelInit packets from the queue. -bool isTunnelItem(PQItem *item) -{ - if (item -> type == PQI_ITEM_TYPE_TUNNELITEM) - return true; - return false; -} - -bool isTunnelInitItem(PQItem *item) -{ - if (item -> type == PQI_ITEM_TYPE_TUNNELINITITEM) - return true; - return false; -} // handle the tunnel services. -int pqipersongrp::tickTunnelServer() +int pqipersongrp::tickServiceRecv() { - PQItem *pqi = NULL; + RsRawItem *pqi = NULL; int i = 0; { std::ostringstream out; @@ -83,16 +55,9 @@ int pqipersongrp::tickTunnelServer() pqioutput(PQL_DEBUG_ALL, pqipersongrpzone, out.str()); } - PQTunnelServer::tick(); + //p3ServiceServer::tick(); - while(NULL != (pqi = SelectOtherPQItem(isTunnelInitItem))) - { - ++i; - pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone, - "pqipersongrp::tickTunnelServer() Incoming TunnelInitItem"); - incoming(pqi); - } - while(NULL != (pqi = SelectOtherPQItem(isTunnelItem))) + while(NULL != (pqi = GetRsRawItem())) { ++i; pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone, @@ -100,13 +65,33 @@ int pqipersongrp::tickTunnelServer() incoming(pqi); } + if (0 < i) + { + return 1; + } + return 0; +} + +// handle the tunnel services. +int pqipersongrp::tickServiceSend() +{ + RsRawItem *pqi = NULL; + int i = 0; + { + std::ostringstream out; + out << "pqipersongrp::tickServiceSend()"; + pqioutput(PQL_DEBUG_ALL, pqipersongrpzone, out.str()); + } + + p3ServiceServer::tick(); + while(NULL != (pqi = outgoing())) { ++i; pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone, - "pqipersongrp::tickTunnelServer() OutGoing PQItem"); + "pqipersongrp::tickTunnelServer() OutGoing RsItem"); - SendOtherPQItem(pqi); + SendRsRawItem(pqi); } if (0 < i) { @@ -118,20 +103,16 @@ int pqipersongrp::tickTunnelServer() // inits pqipersongrp::pqipersongrp(SecurityPolicy *glob, sslroot *sr, unsigned long flags) - :pqihandler(glob), sslr(sr), p3d(NULL), + :pqihandler(glob), sslr(sr), #ifdef PQI_USE_PROXY p3p(NULL), #endif initFlags(flags) { // add a p3proxy & p3disc. - p3d = new p3disc(sr); #ifdef PQI_USE_PROXY p3p = new p3udpproxy(p3d); #endif -#ifdef PQI_USE_CHANNELS - p3c = new p3channel(sr); -#endif if (!(sr -> active())) { @@ -191,8 +172,6 @@ pqipersongrp::pqipersongrp(SecurityPolicy *glob, sslroot *sr, unsigned long flag //addService(new PQTStst()); //registerTunnelType(PQI_TUNNEL_TST_TYPE, createPQTStst); - addService(p3d); - registerTunnelType(PQI_TUNNEL_DISC_ITEM_TYPE, createDiscItems); #ifdef PQI_USE_PROXY addService(p3p); @@ -200,11 +179,6 @@ pqipersongrp::pqipersongrp(SecurityPolicy *glob, sslroot *sr, unsigned long flag registerTunnelInitType(PQI_TUNNEL_PROXY_TYPE, createPQTunnelProxyInit); #endif -#ifdef PQI_USE_CHANNELS - addService(p3c); - registerTunnelType(PQI_TUNNEL_CHANNEL_ITEM_TYPE, createChannelItems); -#endif - return; } @@ -221,9 +195,15 @@ int pqipersongrp::tick() #ifdef PQI_USE_PROXY pqiudpl->tick(); #endif - tickTunnelServer(); + int i = 0; - return pqihandler::tick(); + if (tickServiceSend()) i = 1; + + if (pqihandler::tick()) i = 1; /* does actual Send/Recv */ + + if (tickServiceRecv()) i = 1; + + return 1; } @@ -251,9 +231,28 @@ int pqipersongrp::cert_accept(cert *a) return -1; } - pqiperson *pqip = new pqiperson(a); + { + std::ostringstream out; + out << "pqipersongrp::cert_accept() PeerId: " << a->PeerId(); + pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone, out.str()); + } + + pqiperson *pqip = new pqiperson(a, a->PeerId()); pqissl *pqis = new pqissl(a, pqil, pqip); - pqiconnect *pqisc = new pqiconnect(pqis); + + /* construct the serialiser .... + * Needs: + * * FileItem + * * FileData + * * ServiceGeneric + */ + + RsSerialiser *rss = new RsSerialiser(); + rss->addSerialType(new RsFileItemSerialiser()); + rss->addSerialType(new RsCacheItemSerialiser()); + rss->addSerialType(new RsServiceSerialiser()); + + pqiconnect *pqisc = new pqiconnect(rss, pqis); pqip -> addChildInterface(pqisc); @@ -275,7 +274,7 @@ int pqipersongrp::cert_accept(cert *a) // attach to pqihandler SearchModule *sm = new SearchModule(); - sm -> smi = 2; + sm -> peerid = a->PeerId(); sm -> pqi = pqip; sm -> sp = secpolicy_create(); @@ -288,7 +287,7 @@ int pqipersongrp::cert_accept(cert *a) int pqipersongrp::cert_deny(cert *a) { - std::map::iterator it; + std::map::iterator it; SearchModule *mod; bool found = false; @@ -299,8 +298,8 @@ int pqipersongrp::cert_deny(cert *a) for(it = mods.begin(); (!found) && (it != mods.end());it++) { mod = it -> second; - if (a == (cert *) ((pqiperson *) - (mod -> pqi)) -> getContact()) + pqiperson *p = (pqiperson *) mod -> pqi; + if (a->PeerId() == p->PeerId()) { found = true; } @@ -321,7 +320,7 @@ int pqipersongrp::cert_deny(cert *a) int pqipersongrp::cert_auto(cert *a, bool b) { - std::map::iterator it; + std::map::iterator it; if (b) { cert_accept(a); @@ -330,7 +329,7 @@ int pqipersongrp::cert_auto(cert *a, bool b) { SearchModule *mod = it -> second; pqiperson *p = (pqiperson *) mod -> pqi; - if (a == (cert *) p -> getContact()) + if (a->PeerId() == p->PeerId()) { p -> autoconnect(b); return 1; @@ -407,74 +406,3 @@ int pqipersongrp::load_config() } - /* Overloaded PQItem Check */ -int pqipersongrp::checkOutgoingPQItem(PQItem *item, int global) -{ - /* check cid vs Person */ - if ((global) && (item->cid.route[0] == 0)) - { - /* allowed through as for all! */ - pqioutput(PQL_DEBUG_BASIC, pqipersongrpzone, - "pqipersongrp::checkOutgoingPQItem() Allowing global"); - return 1; - } - if ((!global) && (item->cid.route[0] == 0)) - { - /* not allowed for all! */ - std::ostringstream out; - out << "Bad CID on non-global traffic" << std::endl; - item -> print(out); - pqioutput(PQL_ALERT, pqipersongrpzone,out.str()); - return 0; - } - - if (item -> p == NULL) - { - pqioutput(PQL_ALERT, pqipersongrpzone, - "pqipersongrp::checkOutgoingPQItem() ERROR: NULL Person"); - - std::ostringstream out; - item -> print(out); - pqioutput(PQL_ALERT, pqipersongrpzone,out.str()); - - return 0; - } - - cert *c = (cert *) item -> p; - if (0 != pqicid_cmp(&(c -> cid), &(item -> cid))) - { - std::ostringstream out; - out << "pqipersongrp::checkOutgoingPQItem() c->cid != item->cid"; - out << std::endl; - out << "c -> CID [" << c->cid.route[0]; - for(int i = 0; i < 10; i++) - { - out << ":" << c->cid.route[i]; - } - out << "]" << std::endl; - - out << "item -> CID [" << item->cid.route[0]; - for(int i = 0; i < 10; i++) - { - out << ":" << item->cid.route[i]; - } - out << "]" << std::endl; - - item -> print(out); - - pqioutput(PQL_ALERT, pqipersongrpzone,out.str()); - pqicid_copy(&(c->cid), &(item->cid)); - } - - /* check the top one */ - - return 1; -} - - - - - - - - diff --git a/libretroshare/src/pqi/pqipersongrp.h b/libretroshare/src/pqi/pqipersongrp.h index 6ef39475e..a0d884998 100644 --- a/libretroshare/src/pqi/pqipersongrp.h +++ b/libretroshare/src/pqi/pqipersongrp.h @@ -30,7 +30,7 @@ #include "pqi/pqihandler.h" #include "pqi/pqiperson.h" -#include "pqi/pqitunnel.h" +#include "pqi/pqiservice.h" // So this is a specific implementation @@ -50,12 +50,17 @@ const unsigned long PQIPERSON_NO_SSLLISTENER = 0x0001; const unsigned long PQIPERSON_ALL_BW_LIMITED = 0x0010; -class p3disc; -class p3channel; +#ifdef PQI_USE_DISC + class p3disc; +#endif + +#ifdef PQI_USE_CHANNELS + class p3channel; +#endif class pqissllistener; -class pqipersongrp: public pqihandler, public PQTunnelServer +class pqipersongrp: public pqihandler, public p3ServiceServer { public: pqipersongrp(SecurityPolicy *, sslroot *sr, unsigned long flags); @@ -77,7 +82,9 @@ virtual int status(); // + SearchInterface which should automatically handle stuff // acess to services. +#ifdef PQI_USE_DISC p3disc *getP3Disc() { return p3d; } +#endif #ifdef PQI_USE_PROXY p3proxy *getP3Proxy() { return p3p; } @@ -88,22 +95,25 @@ virtual int status(); #endif protected: - /* Overloaded PQItem Check + /* Overloaded RsItem Check * checks item->cid vs Person */ -virtual int checkOutgoingPQItem(PQItem *item, int global); +virtual int checkOutgoingRsItem(RsItem *item, int global) { return 1; } private: // The tunnelserver operation. - int tickTunnelServer(); - + int tickServiceRecv(); + int tickServiceSend(); pqissllistener *pqil; sslroot *sslr; +#ifdef PQI_USE_DISC p3disc *p3d; +#endif + #ifdef PQI_USE_PROXY p3proxy *p3p; pqiudplistener *pqiudpl; diff --git a/libretroshare/src/pqi/pqisecurity.h b/libretroshare/src/pqi/pqisecurity.h index 71cac4ef6..7656927ad 100644 --- a/libretroshare/src/pqi/pqisecurity.h +++ b/libretroshare/src/pqi/pqisecurity.h @@ -28,12 +28,11 @@ #ifndef MRK_PQI_SECURITY_HEADER #define MRK_PQI_SECURITY_HEADER -#include "pqi/pqi.h" -#include "pqi/pqipacket.h" - #define PQI_INCOMING 2 #define PQI_OUTGOING 5 +#include + //structure. typedef struct sec_policy { diff --git a/libretroshare/src/pqi/pqiservice.cc b/libretroshare/src/pqi/pqiservice.cc new file mode 100644 index 000000000..98d8e3f05 --- /dev/null +++ b/libretroshare/src/pqi/pqiservice.cc @@ -0,0 +1,183 @@ +/* + * libretroshare/src/pqi pqiservice.cc + * + * 3P/PQI network interface for RetroShare. + * + * Copyright 2004-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 "pqi/pqiservice.h" +#include "pqi/pqidebug.h" +#include + +const int pqiservicezone = 60478; + +p3ServiceServer::p3ServiceServer() +{ + pqioutput(PQL_DEBUG_BASIC, pqiservicezone, + "p3ServiceServer::p3ServiceServer()"); + + rrit = services.begin(); + return; +} + +int p3ServiceServer::addService(pqiService *ts) +{ + pqioutput(PQL_DEBUG_BASIC, pqiservicezone, + "p3ServiceServer::addService()"); + + std::map::iterator it; + it = services.find(ts -> getType()); + if (it != services.end()) + { + // it exists already! + return -1; + } + + services[ts -> getType()] = ts; + rrit = services.begin(); + return 1; +} + +int p3ServiceServer::incoming(RsRawItem *item) +{ + pqioutput(PQL_DEBUG_BASIC, pqiservicezone, + "p3ServiceServer::incoming()"); + + { + std::ostringstream out; + out << "p3ServiceServer::incoming() PacketId: "; + out << std::hex << item -> PacketId() << std::endl; + out << "Looking for Service: "; + out << (item -> PacketId() & 0xffffff00) << std::dec << std::endl; + + out << "Item:" << std::endl; + item -> print(out); + out << std::endl; + pqioutput(PQL_DEBUG_BASIC, pqiservicezone, out.str()); + } + + std::map::iterator it; + it = services.find(item -> PacketId() & 0xffffff00); + if (it == services.end()) + { + pqioutput(PQL_DEBUG_BASIC, pqiservicezone, + "p3ServiceServer::incoming() Service: No Service - deleting"); + + // delete it. + delete item; + + // it exists already! + return -1; + } + + { + std::ostringstream out; + out << "p3ServiceServer::incoming() Sending to"; + out << it -> second << std::endl; + pqioutput(PQL_DEBUG_BASIC, pqiservicezone, out.str()); + + return (it->second) -> receive(item); + } + + delete item; + return -1; +} + + + +RsRawItem *p3ServiceServer::outgoing() +{ + pqioutput(PQL_DEBUG_ALL, pqiservicezone, + "p3ServiceServer::outgoing()"); + + if (rrit != services.end()) + { + rrit++; + } + else + { + rrit = services.begin(); + } + + std::map::iterator sit = rrit; + // run to the end. + RsRawItem *item; + + // run through to the end, + for(;rrit != services.end();rrit++) + { + if (NULL != (item = (rrit -> second) -> send())) + { + std::ostringstream out; + out << "p3ServiceServer::outgoing() Got Item From:"; + out << rrit -> second << std::endl; + + item -> print(out); + out << std::endl; + pqioutput(PQL_DEBUG_BASIC, pqiservicezone, out.str()); + return item; + } + } + + // from the beginning to where we started. + for(rrit = services.begin();rrit != sit; rrit++) + { + if (NULL != (item = (rrit -> second) -> send())) + { + std::ostringstream out; + out << "p3ServiceServer::outgoing() Got Item From:"; + out << rrit -> second << std::endl; + + item -> print(out); + out << std::endl; + pqioutput(PQL_DEBUG_BASIC, pqiservicezone, out.str()); + return item; + } + } + return NULL; +} + + + +int p3ServiceServer::tick() +{ + pqioutput(PQL_DEBUG_ALL, pqiservicezone, + "p3ServiceServer::tick()"); + + std::map::iterator it; + + // from the beginning to where we started. + for(it = services.begin();it != services.end(); it++) + { + std::ostringstream out; + out << "p3ServiceServer::service id:" << it -> first; + out << " -> Service: " << it -> second; + out << std::endl; + pqioutput(PQL_DEBUG_ALL, pqiservicezone, out.str()); + + // now we should actually tick the service. + (it -> second) -> tick(); + } + return 1; +} + + + diff --git a/libretroshare/src/pqi/pqiservice.h b/libretroshare/src/pqi/pqiservice.h new file mode 100644 index 000000000..21028d947 --- /dev/null +++ b/libretroshare/src/pqi/pqiservice.h @@ -0,0 +1,99 @@ +/* + * libretroshare/src/pqi pqiservice.h + * + * 3P/PQI network interface for RetroShare. + * + * Copyright 2004-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". + * + */ + + +#ifndef PQI_SERVICE_HEADER +#define PQI_SERVICE_HEADER + +#include "pqi/pqi_base.h" + +// PQI Service, is a generic lower layer on which services can run on. +// +// these packets get passed through the +// server, to a service that is registered. +// +// example services: +// proxytunnel. -> p3proxy. +// sockettunnel +// -> either broadcast (attach to +// open socket instead +// of broadcast address) +// -> or requested/signon. +// +// games, +// voice +// video +// +// +// DataType is defined in the serialiser directory. + +class RsRawItem; + +class pqiService +{ + protected: + + pqiService(uint32_t t) // our type of packets. + :type(t) { return; } + +virtual ~pqiService() { return; } + + public: + // +virtual int receive(RsRawItem *) = 0; +virtual RsRawItem * send() = 0; + +int getType() { return type; } + +virtual int tick() { return 0; } + + private: + int type; +}; + +#include + + +class p3ServiceServer +{ +public: + p3ServiceServer(); + +int addService(pqiService *); + +int incoming(RsRawItem *); +RsRawItem *outgoing(); + +int tick(); + +private: + +std::map services; +std::map::iterator rrit; + +}; + + +#endif // PQI_SERVICE_HEADER diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index 5dc667359..dcaca61c5 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -86,7 +86,8 @@ static const int PQISSL_SSL_CONNECT_TIMEOUT = 30; */ pqissl::pqissl(cert *c, pqissllistener *l, PQInterface *parent) - :NetBinInterface(parent, c), + //:NetBinInterface(parent, c), + :NetBinInterface(parent, parent->PeerId()), waiting(WAITING_NOT), active(false), certvalid(false), sslmode(PQISSL_ACTIVE), ssl_connection(NULL), sockfd(-1), sslcert(c), sslccr(NULL), pqil(l), // no init for remote_addr. @@ -98,6 +99,12 @@ pqissl::pqissl(cert *c, pqissllistener *l, PQInterface *parent) { sslccr = getSSLRoot(); + { + std::ostringstream out; + out << "pqissl for PeerId: " << PeerId(); + pqioutput(PQL_ALERT, pqisslzone, out.str()); + } + // check certificate /**************** PQI_USE_XPGP ******************/ #if defined(PQI_USE_XPGP) diff --git a/libretroshare/src/pqi/pqistreamer.cc b/libretroshare/src/pqi/pqistreamer.cc index b20a7d964..10f0192a3 100644 --- a/libretroshare/src/pqi/pqistreamer.cc +++ b/libretroshare/src/pqi/pqistreamer.cc @@ -27,7 +27,8 @@ #include "pqi/pqistreamer.h" -#include "pqi/pqipacket.h" +#include "serialiser/rsserial.h" +#include "serialiser/rsbaseitems.h" /***** For RsFileData *****/ #include #include @@ -36,10 +37,10 @@ const int pqistreamerzone = 8221; -const int PQISTREAM_ABS_MAX = 10000000; /* 10 MB/sec (actually per loop) */ +const int PQISTREAM_ABS_MAX = 100000000; /* 100 MB/sec (actually per loop) */ -pqistreamer::pqistreamer(BinInterface *bio_in, int bio_flags_in) - :bio(bio_in), bio_flags(bio_flags_in), +pqistreamer::pqistreamer(RsSerialiser *rss, std::string id, BinInterface *bio_in, int bio_flags_in) + :PQInterface(id), rsSerialiser(rss), bio(bio_in), bio_flags(bio_flags_in), pkt_wpending(NULL), totalRead(0), totalSent(0), currRead(0), currSent(0), @@ -48,7 +49,7 @@ pqistreamer::pqistreamer(BinInterface *bio_in, int bio_flags_in) avgLastUpdate = currReadTS = currSentTS = time(NULL); /* allocated once */ - pkt_rpend_size = pqipkt_maxsize(); + pkt_rpend_size = getRsPktMaxSize(); pkt_rpending = malloc(pkt_rpend_size); // 100 B/s (minimal) @@ -107,7 +108,7 @@ pqistreamer::~pqistreamer() { void *pkt = out_pkt.front(); out_pkt.pop_front(); - pqipkt_delete(pkt); + free(pkt); } // clean up outgoing (data packets) @@ -115,12 +116,12 @@ pqistreamer::~pqistreamer() { void *pkt = out_data.front(); out_data.pop_front(); - pqipkt_delete(pkt); + free(pkt); } if (pkt_wpending) { - pqipkt_delete(pkt_wpending); + free(pkt_wpending); pkt_wpending = NULL; } @@ -129,7 +130,7 @@ pqistreamer::~pqistreamer() // clean up outgoing. while(incoming.size() > 0) { - PQItem *i = incoming.front(); + RsItem *i = incoming.front(); incoming.pop_front(); delete i; } @@ -138,7 +139,7 @@ pqistreamer::~pqistreamer() // Get/Send Items. -int pqistreamer::SendItem(PQItem *si) +int pqistreamer::SendItem(RsItem *si) { { std::ostringstream out; @@ -150,7 +151,7 @@ int pqistreamer::SendItem(PQItem *si) return queue_outpqi(si); } -PQItem *pqistreamer::GetItem() +RsItem *pqistreamer::GetItem() { { std::ostringstream out; @@ -158,12 +159,12 @@ PQItem *pqistreamer::GetItem() pqioutput(PQL_DEBUG_ALL, pqistreamerzone, out.str()); } - std::list::iterator it; + std::list::iterator it; it = incoming.begin(); if (it == incoming.end()) { return NULL; } - PQItem *osr = (*it); + RsItem *osr = (*it); incoming.erase(it); return osr; } @@ -175,14 +176,7 @@ int pqistreamer::tick() std::ostringstream out; out << "pqistreamer::tick()"; out << std::endl; - if (getContact()) - { - out << getContact() -> Name() << ": currRead/Sent: " << currRead << "/" << currSent; - } - else - { - out << "Unknown Contact" << ": currRead/Sent: " << currRead << "/" << currSent; - } + out << PeerId() << ": currRead/Sent: " << currRead << "/" << currSent; out << std::endl; pqioutput(PQL_DEBUG_ALL, pqistreamerzone, out.str()); @@ -210,10 +204,8 @@ int pqistreamer::tick() std::ostringstream out; out << "pqistreamer::tick() Queued Data:"; - if (getContact()) - { - out << " for " << getContact() -> Name(); - } + out << " for " << PeerId(); + if (bio->isactive()) { out << " (active)"; @@ -227,7 +219,7 @@ int pqistreamer::tick() int total = 0; for(it = out_pkt.begin(); it != out_pkt.end(); it++) { - total += pqipkt_rawlen(*it); + total += getRsItemSize(*it); } out << "\t Out Packets [" << out_pkt.size() << "] => " << total; @@ -236,7 +228,7 @@ int pqistreamer::tick() total = 0; for(it = out_data.begin(); it != out_data.end(); it++) { - total += pqipkt_rawlen(*it); + total += getRsItemSize(*it); } out << "\t Out Data [" << out_data.size() << "] => " << total; @@ -277,7 +269,7 @@ int pqistreamer::status() // /**************** HANDLE OUTGOING TRANSLATION + TRANSMISSION ******/ -int pqistreamer::queue_outpqi(PQItem *pqi) +int pqistreamer::queue_outpqi(RsItem *pqi) { { std::ostringstream out; @@ -286,12 +278,13 @@ int pqistreamer::queue_outpqi(PQItem *pqi) } /* decide which type of packet it is */ - PQFileData *dta = dynamic_cast(pqi); + RsFileData *dta = dynamic_cast(pqi); bool isCntrl = (dta == NULL); - void *ptr = pqipkt_makepkt(pqi); - if (NULL != ptr) + uint32_t pktsize = rsSerialiser->size(pqi); + void *ptr = malloc(pktsize); + if (rsSerialiser->serialise(pqi, ptr, &pktsize)) { if (isCntrl) { @@ -304,6 +297,11 @@ int pqistreamer::queue_outpqi(PQItem *pqi) delete pqi; return 1; } + else + { + /* cleanup serialiser */ + free(ptr); + } std::ostringstream out; out << "pqistreamer::queue_outpqi() Null Pkt generated!"; @@ -316,7 +314,7 @@ int pqistreamer::queue_outpqi(PQItem *pqi) return 1; // keep error internal. } -int pqistreamer::handleincomingitem(PQItem *pqi) +int pqistreamer::handleincomingitem(RsItem *pqi) { { std::ostringstream out; @@ -325,7 +323,7 @@ int pqistreamer::handleincomingitem(PQItem *pqi) } // Use overloaded Contact function - pqi -> p = getContact(); + pqi -> PeerId(PeerId()); incoming.push_back(pqi); return 1; } @@ -351,7 +349,7 @@ int pqistreamer::handleoutgoing() /* if we are not active - clear anything in the queues. */ for(it = out_pkt.begin(); it != out_pkt.end(); ) { - pqipkt_delete(*it); + free(*it); it = out_pkt.erase(it); std::ostringstream out; @@ -360,7 +358,7 @@ int pqistreamer::handleoutgoing() } for(it = out_data.begin(); it != out_data.end(); ) { - pqipkt_delete(*it); + free(*it); it = out_data.erase(it); std::ostringstream out; @@ -371,7 +369,7 @@ int pqistreamer::handleoutgoing() /* also remove the pending packets */ if (pkt_wpending) { - pqipkt_delete(pkt_wpending); + free(pkt_wpending); pkt_wpending = NULL; } @@ -413,7 +411,7 @@ int pqistreamer::handleoutgoing() std::ostringstream out; out << "Sending Out Pkt!"; // write packet. - len = pqipkt_rawlen(pkt_wpending); + len = getRsItemSize(pkt_wpending); if (len != (ss = bio->senddata(pkt_wpending, len))) { out << "Problems with Send Data!"; @@ -429,7 +427,7 @@ int pqistreamer::handleoutgoing() out << " Success!" << std::endl; pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, out.str()); - pqipkt_delete(pkt_wpending); + free(pkt_wpending); pkt_wpending = NULL; sentbytes += len; @@ -465,10 +463,10 @@ int pqistreamer::handleincoming() void *block = pkt_rpending; // initial read size: basic packet. - int blen = pqipkt_basesize(); + int blen = getRsPktBaseSize(); int tmplen; - int pktlen; + uint32_t pktlen; int maxin = inAllowedBytes(); while((maxin > readbytes) && (bio->moretoread())) @@ -512,7 +510,7 @@ int pqistreamer::handleincoming() pktlen = tmplen; // workout how much more to read. - int extralen = pqipkt_rawlen(block) - blen; + int extralen = getRsItemSize(block) - blen; if (extralen > maxlen - blen) { pqioutput(PQL_ALERT, pqistreamerzone, "ERROR: Read Packet too Big!"); @@ -553,11 +551,7 @@ int pqistreamer::handleincoming() pqioutput(PQL_DEBUG_BASIC, pqistreamerzone, out.str()); } - PQItem *pkt = NULL; - if (pqipkt_check(block, pktlen)) - { - pkt = pqipkt_create(block); - } + RsItem *pkt = rsSerialiser->deserialise(block, &pktlen); if ((pkt != NULL) && (0 < handleincomingitem(pkt))) { @@ -589,7 +583,7 @@ float pqistreamer::outTimeSlice() pqioutput(PQL_DEBUG_ALL, pqistreamerzone, out.str()); } - fixme("pqistreamer::outTimeSlice()", 1); + //fixme("pqistreamer::outTimeSlice()", 1); return 1; } diff --git a/libretroshare/src/pqi/pqistreamer.h b/libretroshare/src/pqi/pqistreamer.h index 16a764f5d..af446c2e3 100644 --- a/libretroshare/src/pqi/pqistreamer.h +++ b/libretroshare/src/pqi/pqistreamer.h @@ -1,9 +1,9 @@ /* - * "$Id: pqistreamer.h,v 1.10 2007-02-18 21:46:50 rmf24 Exp $" + * libretroshare/src/pqi pqistreamer.h * * 3P/PQI network interface for RetroShare. * - * Copyright 2004-2006 by Robert Fernie. + * Copyright 2004-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 @@ -24,7 +24,6 @@ */ - #ifndef MRK_PQI_STREAMER_HEADER #define MRK_PQI_STREAMER_HEADER @@ -41,12 +40,12 @@ class pqistreamer: public PQInterface { public: - pqistreamer(BinInterface *bio_in, int bio_flagsin); + pqistreamer(RsSerialiser *rss, std::string peerid, BinInterface *bio_in, int bio_flagsin); virtual ~pqistreamer(); // PQInterface -virtual int SendItem(PQItem *); -virtual PQItem *GetItem(); +virtual int SendItem(RsItem *); +virtual RsItem *GetItem(); virtual int tick(); virtual int status(); @@ -55,8 +54,8 @@ virtual int status(); /* Implementation */ // to filter functions - detect filecancel/data and act! -int queue_outpqi( PQItem *i); -int handleincomingitem(PQItem *i); +int queue_outpqi( RsItem *i); +int handleincomingitem(RsItem *i); // ticked regularly (manages out queues and sending // via above interfaces. @@ -72,6 +71,8 @@ void outSentBytes(int ); int inAllowedBytes(); void inReadBytes(int ); + // RsSerialiser - determines which packets can be serialised. + RsSerialiser *rsSerialiser; // Binary Interface for IO, initialisated at startup. BinInterface *bio; unsigned int bio_flags; // only BIN_NO_CLOSE at the moment. @@ -83,7 +84,7 @@ void inReadBytes(int ); // Temp Storage for transient data..... std::list out_pkt; // Cntrl / Search / Results queue std::list out_data; // FileData - secondary queue. - std::list incoming; + std::list incoming; // data for network stats. int totalRead; diff --git a/libretroshare/src/pqi/pqisupernode.cc b/libretroshare/src/pqi/pqisupernode.cc deleted file mode 100644 index d90a8359f..000000000 --- a/libretroshare/src/pqi/pqisupernode.cc +++ /dev/null @@ -1,482 +0,0 @@ -/* - * "$Id: pqisupernode.cc,v 1.3 2007-02-18 21:46:50 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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 "pqi/pqisupernode.h" - -/* -#include "pqi/pqiproxy.h" -#include "pqi/pqitunnelproxy.h" - */ - - -#include "pqi/p3disc.h" -#include "pqi/p3channel.h" - -#include "pqi/pqissl.h" -#include "pqi/pqissllistener.h" - -/* - * #include "pqi/pqiudpproxy.h" - * #include "pqi/pqissludp.h" - */ - -#ifdef PQI_USE_PROXY - #include "pqi/pqiudpproxy.h" - #include "pqi/pqissludp.h" -#endif - -//#include "pqi/pqitunneltst.h" - -#include "pqi/pqidebug.h" -#include - -const int pqisupernodezone = 65904; - -// get the Tunnel and TunnelInit packets from the queue. -bool isTunnelItem(PQItem *item) -{ - if (item -> type == PQI_ITEM_TYPE_TUNNELITEM) - return true; - return false; -} - -bool isTunnelInitItem(PQItem *item) -{ - if (item -> type == PQI_ITEM_TYPE_TUNNELINITITEM) - return true; - return false; -} - -// handle the tunnel services. -int pqisupernode::tickTunnelServer() -{ - PQItem *pqi = NULL; - { - std::ostringstream out; - out << "pqisupernode::tickTunnelServer()"; - pqioutput(PQL_DEBUG_ALL, pqisupernodezone, out.str()); - } - - PQTunnelServer::tick(); - - while(NULL != (pqi = SelectOtherPQItem(isTunnelInitItem))) - { - pqioutput(PQL_DEBUG_BASIC, pqisupernodezone, - "pqisupernode::tickTunnelServer() Incoming TunnelInitItem"); - incoming(pqi); - } - while(NULL != (pqi = SelectOtherPQItem(isTunnelItem))) - { - pqioutput(PQL_DEBUG_BASIC, pqisupernodezone, - "pqisupernode::tickTunnelServer() Incoming TunnelItem"); - incoming(pqi); - } - - while(NULL != (pqi = outgoing())) - { - pqioutput(PQL_DEBUG_BASIC, pqisupernodezone, - "pqisupernode::tickTunnelServer() OutGoing PQItem"); - - SendOtherPQItem(pqi); - } - return 1; -} - - - // inits -pqisupernode::pqisupernode(SecurityPolicy *glob, sslroot *sr) - :pqihandler(glob), sslr(sr), p3d(NULL) -#ifdef PQI_USE_PROXY - ,p3p(NULL) -#endif -{ - // add a p3proxy & p3disc. - p3d = new p3disc(sr); -#ifdef PQI_USE_PROXY - p3p = new p3udpproxy(p3d); -#endif - - if (!(sr -> active())) - { - pqioutput(PQL_ALERT, pqisupernodezone, "sslroot not active... exiting!"); - exit(1); - } - - // make listen - Person *us = sr -> getOwnCert(); - if (us != NULL) - { - pqisslnode = new pqisslsupernode(us -> localaddr, this); -#ifdef PQI_USE_PROXY - pqiudpl = new pqiudplistener((p3udpproxy *) p3p, - us -> localaddr); -#endif - } - else - { - pqioutput(PQL_ALERT, pqisupernodezone, "No Us! what are we!"); - exit(1); - } - - addService(p3d); - registerTunnelType(PQI_TUNNEL_DISC_ITEM_TYPE, createDiscItems); - -#ifdef PQI_USE_PROXY - addService(p3p); - registerTunnelType(PQI_TUNNEL_PROXY_TYPE, createPQTunnelProxy); - registerTunnelInitType(PQI_TUNNEL_PROXY_TYPE, createPQTunnelProxyInit); -#endif - return; -} - - -int pqisupernode::run() -{ - while(1) - { - sleep(1); - tick(); - } - return 1; -} - -int pqisupernode::tick() -{ - pqisslnode -> tick(); -#ifdef PQI_USE_PROXY - pqiudpl->tick(); -#endif - tickTunnelServer(); - - return pqihandler::tick(); -} - - -int pqisupernode::status() -{ - pqisslnode -> status(); -#ifdef PQI_USE_PROXY - pqiudpl->status(); -#endif - return pqihandler::status(); -} - - // control the connections. - -int pqisupernode::cert_accept(cert *a) -{ - /* this function does nothing */ - pqioutput(PQL_ALERT, pqisupernodezone, - "pqisupernode::cert_accept() Null Fn."); - - return -1; -} - -int pqisupernode::cert_deny(cert *a) -{ - std::map::iterator it; - SearchModule *mod; - bool found = false; - - // if used find search module.... - if (a -> InUse()) - { - // find module. - for(it = mods.begin(); (!found) && (it != mods.end());it++) - { - mod = it -> second; - if (a == (cert *) ((pqiperson *) - (mod -> pqi)) -> getContact()) - { - found = true; - } - } - if (found) - { - RemoveSearchModule(mod); - secpolicy_delete(mod -> sp); - pqiperson *p = (pqiperson *) mod -> pqi; - p -> reset(); - delete p; - a -> InUse(false); - } - } - a -> Accepted(false); - return 1; -} - -int pqisupernode::cert_auto(cert *a, bool b) -{ - pqioutput(PQL_ALERT, pqisupernodezone, - "pqisupernode::cert_auto() Null Fn."); - return 1; -} - - -int pqisupernode::restart_listener() -{ - pqisslnode -> resetlisten(); - cert *own = sslr -> getOwnCert(); - pqisslnode -> setListenAddr(own -> localaddr); - pqisslnode -> setuplisten(); - -#ifdef PQI_USE_PROXY - pqiudpl -> resetlisten(); - pqiudpl -> setListenAddr(own -> localaddr); - pqiudpl -> setuplisten(); -#endif - return 1; -} - - -static const std::string pqih_ftr("PQIH_FTR"); - -int pqisupernode::save_config() -{ - char line[512]; - sprintf(line, "%f %f %f %f", getMaxRate(true), getMaxRate(false), - getMaxIndivRate(true), getMaxIndivRate(false)); - sslr -> setSetting(pqih_ftr, std::string(line)); - return 1; -} - - - -int pqisupernode::load_config() -{ - std::string line = sslr -> getSetting(pqih_ftr); - float mri, mro, miri, miro; - - if (4 == sscanf(line.c_str(), "%f %f %f %f", &mri, &mro, &miri, &miro)) - { - setMaxRate(true, mri); - setMaxRate(false, mro); - setMaxIndivRate(true, miri); - setMaxIndivRate(false, miro); - } - else - { - pqioutput(PQL_DEBUG_BASIC, pqisupernodezone, - "pqisupernode::load_config() Loading Default Rates!"); - - setMaxRate(true, 20.0); - setMaxRate(false, 20.0); - setMaxIndivRate(true, 5.0); - setMaxIndivRate(false, 5.0); - } - - return 1; -} - - - /* Overloaded PQItem Check */ -int pqisupernode::checkOutgoingPQItem(PQItem *item, int global) -{ - /* check cid vs Person */ - if ((global) && (item->cid.route[0] == 0)) - { - /* allowed through as for all! */ - pqioutput(PQL_DEBUG_BASIC, pqisupernodezone, - "pqisupernode::checkOutgoingPQItem() Allowing global"); - return 1; - } - - if (item -> p == NULL) - { - pqioutput(PQL_ALERT, pqisupernodezone, - "pqisupernode::checkOutgoingPQItem() ERROR: NULL Person"); - - std::ostringstream out; - item -> print(out); - pqioutput(PQL_ALERT, pqisupernodezone,out.str()); - - return 0; - } - - cert *c = (cert *) item -> p; - if (0 != pqicid_cmp(&(c -> cid), &(item -> cid))) - { - std::ostringstream out; - out << "pqisupernode::checkOutgoingPQItem() c->cid != item->cid"; - out << std::endl; - out << "c -> CID [" << c->cid.route[0]; - for(int i = 0; i < 10; i++) - { - out << ":" << c->cid.route[i]; - } - out << "]" << std::endl; - - out << "item -> CID [" << item->cid.route[0]; - for(int i = 0; i < 10; i++) - { - out << ":" << item->cid.route[i]; - } - out << "]" << std::endl; - - item -> print(out); - - pqioutput(PQL_ALERT, pqisupernodezone,out.str()); - pqicid_copy(&(c->cid), &(item->cid)); - } - - /* check the top one */ - - return 1; -} - -int pqisupernode::recvdConnection(int fd, SSL *in_connection, - cert *peer, struct sockaddr_in *raddr) -{ - /* so we need to accept the certificate */ - if (peer -> InUse()) - { - pqioutput(PQL_DEBUG_BASIC, pqisupernodezone, - "pqisupernode::recvdConnection() Cert in Use!"); - - return -1; - } - - pqiperson *pqip = new pqiperson(peer); - pqissl *pqis = new pqissl(peer, pqisslnode, pqip); - pqiconnect *pqisc = new pqiconnect(pqis); - pqip -> addChildInterface(pqisc); - - /* first set the certificate options off! */ - /* listening off, connecting off */ - // setup no behaviour. (no remote address) - // - - // add the certificate to sslroot... - sslr -> addUntrustedCertificate(peer); - - peer -> InUse(true); - peer -> Accepted(true); - peer -> WillListen(false); - peer -> WillConnect(false); - peer -> Manual(true); - - // attach to pqihandler - SearchModule *sm = new SearchModule(); - sm -> smi = 2; - sm -> pqi = pqip; - sm -> sp = secpolicy_create(); - - // reset it to start it working. - pqis -> reset(); - AddSearchModule(sm); // call to pqihandler.... - - pqis -> accept(in_connection, fd, *raddr); - - /* finally tell the system to listen - if the connection fails */ - peer -> WillListen(true); - - /* done! */ - return 1; -} - - -/************************ PQI SSL SUPER NODE **************************** - * - * This is the special listener, that accepts all connections. - * - */ - -pqisslsupernode::pqisslsupernode(struct sockaddr_in addr, pqisupernode *grp) - :pqissllistener(addr), psn(grp) -{ - return; -} - -pqisslsupernode::~pqisslsupernode() -{ - return; -} - - -int pqisslsupernode::completeConnection(int fd, SSL *ssl, struct sockaddr_in &remote_addr) -{ - /* first attempt a pqissllistener connect (ie if we are listening for it!) - * - * if this fails, try adding it! - */ - - if (0 < pqissllistener::completeConnection(fd,ssl, remote_addr)) - { - return 1; /* done! */ - } - - /* else ask the pqisupernode to create it! */ - - // Get the Peer Certificate.... -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - XPGP *peercert = SSL_get_peer_pgp_certificate(ssl); -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - X509 *peercert = SSL_get_peer_certificate(ssl); -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - - if (peercert == NULL) - { - pqioutput(PQL_WARNING, pqisupernodezone, - "pqisslsupernode::completeConnection() Peer Did Not Provide Cert!"); - return -1; - } - - - // save certificate... (and ip locations) - /* the first registerCertificate can fail, - * but this is in pqissllistener .... - * so this one should succeed. - */ -/**************** PQI_USE_XPGP ******************/ -#if defined(PQI_USE_XPGP) - cert *npc = sslccr -> registerCertificateXPGP(peercert, remote_addr, true); -#else /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - cert *npc = sslccr -> registerCertificate(peercert, remote_addr, true); -#endif /* X509 Certificates */ -/**************** PQI_USE_XPGP ******************/ - - if ((npc == NULL) || (npc -> Connected())) - { - std::ostringstream out; - out << "No Matching Certificate/Already Connected"; - out << " for Connection:" << inet_ntoa(remote_addr.sin_addr); - out << std::endl; - out << "Shutting it down!" << std::endl; - pqioutput(PQL_WARNING, pqisupernodezone, out.str()); - return -1; - } - - // hand off ssl conection. - psn -> recvdConnection(fd, ssl, npc, &remote_addr); - return 1; -} - diff --git a/libretroshare/src/pqi/pqisupernode.h b/libretroshare/src/pqi/pqisupernode.h deleted file mode 100644 index f63c647e7..000000000 --- a/libretroshare/src/pqi/pqisupernode.h +++ /dev/null @@ -1,166 +0,0 @@ -/* - * "$Id: pqisupernode.h,v 1.2 2007-02-18 21:46:50 rmf24 Exp $" - * - * 3P/PQI network interface for RetroShare. - * - * Copyright 2004-2006 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". - * - */ - - - -#ifndef MRK_PQI_SUPER_NODE_HEADER -#define MRK_PQI_SUPER_NODE_HEADER - -#include "pqi/pqihandler.h" -#include "pqi/pqiperson.h" -#include "pqi/pqitunnel.h" -#include "pqi/pqissllistener.h" - -/***************************************************************** - * So a super node, is a very special being, - * and used to assist in the setup of a retro share network. - * - * It role is to accept all connections, - * and allow discovery/proxying for facilitating connections. - * - * Therefore this interface includes - * both a p3disc + p3udpproxy, but - * doesn't allow any other traffic through.... - * - * each person will only include a pqissl (generated by the listener). - * - * The tricky part is allowing the pqissl to be - * generated on the fly, as the connections are made! - * - */ - - -class p3proxy; -class p3disc; - -class pqiudplistener; -class pqisslsupernode; - -class pqisupernode: public pqihandler, public PQTunnelServer -{ - public: - pqisupernode(SecurityPolicy *, sslroot *sr); - - // overloaded from pqihandler -> null fns - - virtual int Search(SearchItem *ns) { if (ns) delete ns; return 1; } - virtual int CancelSearch(SearchItem *ns) { if (ns) delete ns; return 1; } - virtual int SendSearchResult(PQFileItem *ns){ if (ns) delete ns; return 1; } - - // inputs. - virtual PQFileItem * GetSearchResult() { return NULL; } - virtual SearchItem * RequestedSearch() { return NULL; } - virtual SearchItem * CancelledSearch() { return NULL; } - - // file i/o - virtual int SendFileItem(PQFileItem *ns) { if (ns) delete ns; return 1; } - virtual PQFileItem * GetFileItem() { return NULL; } - - // Chat Interface - virtual int SendMsg(ChatItem *ns) { if (ns) delete ns; return 1; } - virtual ChatItem *GetMsg() { return NULL; } - - // end of overloaded from pqihandler -> null fns - - - // control the connections. -int cert_accept(cert *a); -int cert_deny(cert *a); -int cert_auto(cert *a, bool b); - -int restart_listener(); - -int save_config(); -int load_config(); - - // tick interfaces. -virtual int run(); -virtual int tick(); -virtual int status(); - - - // the pqisslsupernode, is derived from the pqissllistener, - // except it doesn't track who it should listen for. - // instead each connection is accepted blind, - // and passed to the pqisupernode. which can attach it - // to a pqissl, if desired.... - // - // The first basic version of this will accept up to a total - // limit...... -virtual int recvdConnection(int fd, SSL *in_connection, - cert *peer, struct sockaddr_in *raddr); - - - - // overloaded functions to disable various features of pqihandler - - // + SearchInterface which should automatically handle stuff - - // acess to services. - p3disc *getP3Disc() { return p3d; } - p3proxy *getP3Proxy() { return p3p; } - - protected: - /* Overloaded PQItem Check - * checks item->cid vs Person - */ -virtual int checkOutgoingPQItem(PQItem *item, int global); - - private: - - // The tunnelserver operation. - int tickTunnelServer(); - - pqisslsupernode *pqisslnode; - pqiudplistener *pqiudpl; - - sslroot *sslr; - - p3disc *p3d; - p3proxy *p3p; -}; - - - -class pqisslsupernode: public pqissllistener -{ - public: - - pqisslsupernode(struct sockaddr_in addr, pqisupernode *grp); -virtual ~pqisslsupernode(); - -//virtual int tick(); -//virtual int status(); - -virtual int completeConnection(int sockfd, SSL *in_connection, struct sockaddr_in &raddr); - - private: - - pqisupernode *psn; -}; - - - -#endif // MRK_PQI_SUPER_NODE_HEADER diff --git a/libretroshare/src/pqi/xpgpcert.cc b/libretroshare/src/pqi/xpgpcert.cc index 8b22f5040..710491874 100644 --- a/libretroshare/src/pqi/xpgpcert.cc +++ b/libretroshare/src/pqi/xpgpcert.cc @@ -27,7 +27,6 @@ #include "xpgpcert.h" -#include "pqipacket.h" #include "pqinetwork.h" diff --git a/libretroshare/src/pqi/xpgpcert.h b/libretroshare/src/pqi/xpgpcert.h index 45ec93a80..41bedf32f 100644 --- a/libretroshare/src/pqi/xpgpcert.h +++ b/libretroshare/src/pqi/xpgpcert.h @@ -73,9 +73,11 @@ virtual ~cert(); virtual std::string Signature(); std::string Hash(); void Hash(std::string); +std::string PeerId() { return Signature(); } XPGP *certificate; std::string hash; + std::string peerid; };