2007-11-14 22:18:48 -05:00
|
|
|
/*
|
|
|
|
* "$Id: pqi_base.h,v 1.18 2007-05-05 16:10:05 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_BASE_ITEM_HEADER
|
|
|
|
#define PQI_BASE_ITEM_HEADER
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <functional>
|
|
|
|
#include <algorithm>
|
2008-03-26 11:35:09 -04:00
|
|
|
#include <inttypes.h>
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
#include "pqi/pqinetwork.h"
|
|
|
|
|
2007-12-11 20:29:14 -05:00
|
|
|
/*** Base DataTypes: ****/
|
|
|
|
#include "serialiser/rsserial.h"
|
2014-03-17 16:56:06 -04:00
|
|
|
#include "retroshare/rstypes.h"
|
2007-12-11 20:29:14 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2014-01-17 21:32:55 -05:00
|
|
|
#define PQI_MIN_PORT 10 // TO ALLOW USERS TO HAVE PORT 80! - was 1024
|
2015-05-04 05:05:38 -04:00
|
|
|
#define PQI_MIN_PORT_RNG 1024
|
2011-07-23 16:07:06 -04:00
|
|
|
#define PQI_MAX_PORT 65535
|
2007-11-14 22:18:48 -05:00
|
|
|
#define PQI_DEFAULT_PORT 7812
|
|
|
|
|
|
|
|
int getPQIsearchId();
|
|
|
|
int fixme(char *str, int n);
|
|
|
|
|
2013-09-10 15:31:44 -04:00
|
|
|
class RsPeerCryptoParams ;
|
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
//! controlling data rates
|
|
|
|
/*!
|
|
|
|
* For controlling data rates.
|
2012-01-20 10:13:50 -05:00
|
|
|
* #define DEBUG_RATECAP 1
|
2007-11-14 22:18:48 -05:00
|
|
|
*/
|
2012-06-21 19:23:46 -04:00
|
|
|
|
|
|
|
class RsBwRates
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RsBwRates()
|
2012-06-21 21:35:32 -04:00
|
|
|
:mRateIn(0), mRateOut(0), mMaxRateIn(0), mMaxRateOut(0), mQueueIn(0), mQueueOut(0) {return;}
|
2012-06-21 19:23:46 -04:00
|
|
|
float mRateIn;
|
|
|
|
float mRateOut;
|
|
|
|
float mMaxRateIn;
|
|
|
|
float mMaxRateOut;
|
2012-06-21 21:35:32 -04:00
|
|
|
int mQueueIn;
|
|
|
|
int mQueueOut;
|
2012-06-21 19:23:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
class RateInterface
|
|
|
|
{
|
2010-04-15 07:39:54 -04:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
public:
|
|
|
|
|
|
|
|
RateInterface()
|
2012-01-08 13:40:09 -05:00
|
|
|
:bw_in(0), bw_out(0), bwMax_in(0), bwMax_out(0),
|
|
|
|
bwCapEnabled(false), bwCap_in(0), bwCap_out(0) { return; }
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual ~RateInterface() { return; }
|
|
|
|
|
2012-06-21 19:23:46 -04:00
|
|
|
virtual void getRates(RsBwRates &rates)
|
|
|
|
{
|
|
|
|
rates.mRateIn = bw_in;
|
|
|
|
rates.mRateOut = bw_out;
|
|
|
|
rates.mMaxRateIn = bwMax_in;
|
|
|
|
rates.mMaxRateOut = bwMax_out;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-14 19:39:19 -05:00
|
|
|
virtual int gatherOutQueueStatistics(std::vector<uint32_t>& /*per_service_count*/,std::vector<uint32_t>& /*per_priority_count*/) { return 0;}
|
2012-06-21 21:35:32 -04:00
|
|
|
virtual int getQueueSize(bool /* in */) { return 0;}
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual float getRate(bool in)
|
|
|
|
{
|
|
|
|
if (in)
|
|
|
|
return bw_in;
|
|
|
|
return bw_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual float getMaxRate(bool in)
|
|
|
|
{
|
|
|
|
if (in)
|
|
|
|
return bwMax_in;
|
|
|
|
return bwMax_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setMaxRate(bool in, float val)
|
|
|
|
{
|
|
|
|
if (in)
|
2012-01-08 13:40:09 -05:00
|
|
|
{
|
2007-11-14 22:18:48 -05:00
|
|
|
bwMax_in = val;
|
2012-01-08 13:40:09 -05:00
|
|
|
if (bwCapEnabled)
|
|
|
|
{
|
|
|
|
if (bwMax_in > bwCap_in)
|
|
|
|
{
|
|
|
|
bwMax_in = bwCap_in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-14 22:18:48 -05:00
|
|
|
else
|
2012-01-08 13:40:09 -05:00
|
|
|
{
|
2007-11-14 22:18:48 -05:00
|
|
|
bwMax_out = val;
|
2012-01-08 13:40:09 -05:00
|
|
|
if (bwCapEnabled)
|
|
|
|
{
|
|
|
|
if (bwMax_out > bwCap_out)
|
|
|
|
{
|
|
|
|
bwMax_out = bwCap_out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-08 13:40:09 -05:00
|
|
|
|
|
|
|
virtual void setRateCap(float val_in, float val_out)
|
|
|
|
{
|
|
|
|
if ((val_in == 0) && (val_out == 0))
|
|
|
|
{
|
2012-01-20 10:13:50 -05:00
|
|
|
#ifdef DEBUG_RATECAP
|
2012-01-08 13:40:09 -05:00
|
|
|
std::cerr << "RateInterface::setRateCap() Now disabled" << std::endl;
|
2012-01-20 10:13:50 -05:00
|
|
|
#endif
|
2012-01-08 13:40:09 -05:00
|
|
|
bwCapEnabled = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-20 10:13:50 -05:00
|
|
|
#ifdef DEBUG_RATECAP
|
2012-01-08 13:40:09 -05:00
|
|
|
std::cerr << "RateInterface::setRateCap() Enabled ";
|
2012-01-08 17:15:19 -05:00
|
|
|
std::cerr << "in: " << bwCap_in << " out: " << bwCap_out << std::endl;
|
2012-01-20 10:13:50 -05:00
|
|
|
#endif
|
2012-01-08 13:40:09 -05:00
|
|
|
bwCapEnabled = true;
|
|
|
|
bwCap_in = val_in;
|
|
|
|
bwCap_out = val_out;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
protected:
|
|
|
|
|
|
|
|
void setRate(bool in, float val)
|
|
|
|
{
|
|
|
|
if (in)
|
|
|
|
bw_in = val;
|
|
|
|
else
|
|
|
|
bw_out = val;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
float bw_in, bw_out, bwMax_in, bwMax_out;
|
2012-01-08 13:40:09 -05:00
|
|
|
bool bwCapEnabled;
|
|
|
|
float bwCap_in, bwCap_out;
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class NetInterface;
|
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
//! The basic exchange interface.
|
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* This inherits the RateInterface, as Bandwidth control
|
|
|
|
* is critical to a networked application.
|
|
|
|
**/
|
2007-11-14 22:18:48 -05:00
|
|
|
class PQInterface: public RateInterface
|
|
|
|
{
|
2011-09-04 16:01:30 -04:00
|
|
|
public:
|
2014-03-17 16:56:06 -04:00
|
|
|
PQInterface(const RsPeerId &id) :peerId(id) { return; }
|
2011-09-04 16:01:30 -04:00
|
|
|
virtual ~PQInterface() { return; }
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* allows user to send RsItems to a particular facility (file, network)
|
|
|
|
*/
|
|
|
|
virtual int SendItem(RsItem *) = 0;
|
|
|
|
|
|
|
|
// this function is overloaded in classes that need the serilized size to be returned.
|
|
|
|
virtual int SendItem(RsItem *item,uint32_t& size)
|
|
|
|
{
|
|
|
|
size = 0 ;
|
2012-09-17 16:19:02 -04:00
|
|
|
|
|
|
|
static bool already=false ;
|
|
|
|
if(!already)
|
|
|
|
{
|
|
|
|
std::cerr << "Warning: PQInterface::SendItem(RsItem*,uint32_t&) calledbut not overloaded! Serialized size will not be returned." << std::endl;
|
|
|
|
already=true ;
|
|
|
|
}
|
2011-09-04 16:01:30 -04:00
|
|
|
return SendItem(item) ;
|
|
|
|
}
|
|
|
|
|
2013-09-10 15:31:44 -04:00
|
|
|
virtual bool getCryptoParams(RsPeerCryptoParams&) { return false ;}
|
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
/*!
|
|
|
|
* Retrieve RsItem from a facility
|
|
|
|
*/
|
|
|
|
virtual RsItem *GetItem() = 0;
|
2014-01-17 21:32:55 -05:00
|
|
|
virtual bool RecvItem(RsItem * /*item*/ ) { return false; } /* alternative for for GetItem(), when we want to push */
|
2011-09-04 16:01:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* also there are tick + person id functions.
|
|
|
|
*/
|
|
|
|
virtual int tick() { return 0; }
|
|
|
|
virtual int status() { return 0; }
|
2014-03-17 16:56:06 -04:00
|
|
|
virtual const RsPeerId& PeerId() { return peerId; }
|
2011-09-04 16:01:30 -04:00
|
|
|
|
|
|
|
// the callback from NetInterface Connection Events.
|
2013-10-01 06:11:34 -04:00
|
|
|
virtual int notifyEvent(NetInterface *ni, int event, const struct sockaddr_storage &remote_peer_address)
|
2011-09-04 16:01:30 -04:00
|
|
|
{
|
|
|
|
(void) ni; /* remove unused parameter warnings */
|
|
|
|
(void) event; /* remove unused parameter warnings */
|
2013-10-01 06:11:34 -04:00
|
|
|
(void) remote_peer_address;
|
2011-09-04 16:01:30 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
RsPeerId peerId;
|
2007-11-14 22:18:48 -05:00
|
|
|
};
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
/**** Consts for pqiperson -> placed here for NetBinDummy usage() */
|
|
|
|
|
|
|
|
const uint32_t PQI_CONNECT_TCP = 0x0001;
|
|
|
|
const uint32_t PQI_CONNECT_UDP = 0x0002;
|
2013-09-03 09:35:39 -04:00
|
|
|
const uint32_t PQI_CONNECT_HIDDEN_TCP = 0x0004;
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
#define BIN_FLAGS_NO_CLOSE 0x0001
|
|
|
|
#define BIN_FLAGS_READABLE 0x0002
|
|
|
|
#define BIN_FLAGS_WRITEABLE 0x0004
|
2007-12-11 20:29:14 -05:00
|
|
|
#define BIN_FLAGS_NO_DELETE 0x0008
|
2008-01-25 01:36:40 -05:00
|
|
|
#define BIN_FLAGS_HASH_DATA 0x0010
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
/*!
|
|
|
|
* This defines the binary interface used by Network/loopback/file
|
|
|
|
* interfaces
|
|
|
|
* e.g. sending binary data to file or to memory or even through a socket
|
|
|
|
*/
|
2007-11-14 22:18:48 -05:00
|
|
|
class BinInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BinInterface() { return; }
|
|
|
|
virtual ~BinInterface() { return; }
|
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
/**
|
|
|
|
* To be called loop, for updating state
|
|
|
|
*/
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual int tick() = 0;
|
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
/**
|
|
|
|
* Sends data to a prescribed location (implementation dependent)
|
|
|
|
*@param data what will be sent
|
|
|
|
*@param len the size of data pointed to in memory
|
|
|
|
*/
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual int senddata(void *data, int len) = 0;
|
2010-04-15 07:39:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* reads data from a prescribed location (implementation dependent)
|
|
|
|
*@param data what will be sent
|
|
|
|
*@param len the size of data pointed to in memory
|
|
|
|
*/
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual int readdata(void *data, int len) = 0;
|
2010-04-15 07:39:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is more particular the case of the sending data through a socket (internet)
|
2013-10-01 06:11:34 -04:00
|
|
|
* moretoread and candsend, take a microsec timeout argument.
|
|
|
|
*
|
2010-04-15 07:39:54 -04:00
|
|
|
*/
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual int netstatus() = 0;
|
|
|
|
virtual int isactive() = 0;
|
2013-10-01 06:11:34 -04:00
|
|
|
virtual bool moretoread(uint32_t usec) = 0;
|
|
|
|
virtual bool cansend(uint32_t usec) = 0;
|
2008-02-05 08:45:04 -05:00
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
/**
|
|
|
|
* method for streamer to shutdown bininterface
|
|
|
|
**/
|
2008-03-31 10:06:59 -04:00
|
|
|
virtual int close() = 0;
|
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
/**
|
|
|
|
* If hashing data
|
|
|
|
**/
|
2014-03-17 16:56:06 -04:00
|
|
|
virtual RsFileHash gethash() = 0;
|
2010-04-15 07:39:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of bytes read/sent
|
|
|
|
*/
|
2008-02-05 08:45:04 -05:00
|
|
|
virtual uint64_t bytecount() { return 0; }
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
/**
|
|
|
|
* used by pqistreamer to limit transfers
|
|
|
|
**/
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual bool bandwidthLimited() { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const int NET_CONNECT_RECEIVED = 1;
|
|
|
|
static const int NET_CONNECT_SUCCESS = 2;
|
|
|
|
static const int NET_CONNECT_UNREACHABLE = 3;
|
|
|
|
static const int NET_CONNECT_FIREWALLED = 4;
|
|
|
|
static const int NET_CONNECT_FAILED = 5;
|
|
|
|
|
2008-03-26 11:35:09 -04:00
|
|
|
static const uint32_t NET_PARAM_CONNECT_DELAY = 1;
|
|
|
|
static const uint32_t NET_PARAM_CONNECT_PERIOD = 2;
|
|
|
|
static const uint32_t NET_PARAM_CONNECT_TIMEOUT = 3;
|
2011-07-13 18:23:40 -04:00
|
|
|
static const uint32_t NET_PARAM_CONNECT_FLAGS = 4;
|
2011-07-14 10:56:33 -04:00
|
|
|
static const uint32_t NET_PARAM_CONNECT_BANDWIDTH = 5;
|
|
|
|
|
|
|
|
static const uint32_t NET_PARAM_CONNECT_PROXY = 6;
|
|
|
|
static const uint32_t NET_PARAM_CONNECT_SOURCE = 7;
|
2008-02-26 21:32:20 -05:00
|
|
|
|
2013-09-03 09:35:39 -04:00
|
|
|
static const uint32_t NET_PARAM_CONNECT_DOMAIN_ADDRESS = 8;
|
|
|
|
static const uint32_t NET_PARAM_CONNECT_REMOTE_PORT = 9;
|
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* ******************** Network INTERFACE ***************************
|
|
|
|
* This defines the Network interface used by sockets/SSL/XPGP
|
|
|
|
* interfaces
|
|
|
|
*
|
|
|
|
* NetInterface: very pure interface, so no tick....
|
|
|
|
*
|
|
|
|
* It is passed a pointer to a PQInterface *parent,
|
|
|
|
* this is used to notify the system of Connect/Disconnect Events.
|
|
|
|
*
|
|
|
|
* Below are the Events for callback.
|
|
|
|
**/
|
2007-11-14 22:18:48 -05:00
|
|
|
class NetInterface
|
|
|
|
{
|
|
|
|
public:
|
2010-04-15 07:39:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param p_in used to notify system of connect/disconnect events
|
|
|
|
*/
|
2014-03-17 16:56:06 -04:00
|
|
|
NetInterface(PQInterface *p_in, const RsPeerId& id)
|
2007-12-11 20:29:14 -05:00
|
|
|
:p(p_in), peerId(id) { return; }
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
virtual ~NetInterface()
|
|
|
|
{ return; }
|
|
|
|
|
2013-09-13 10:35:19 -04:00
|
|
|
virtual int connect(const struct sockaddr_storage &raddr) = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual int listen() = 0;
|
|
|
|
virtual int stoplistening() = 0;
|
|
|
|
virtual int disconnect() = 0;
|
|
|
|
virtual int reset() = 0;
|
2014-03-17 16:56:06 -04:00
|
|
|
virtual const RsPeerId& PeerId() { return peerId; }
|
2013-09-13 10:35:19 -04:00
|
|
|
virtual int getConnectAddress(struct sockaddr_storage &raddr) = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-03-26 11:35:09 -04:00
|
|
|
virtual bool connect_parameter(uint32_t type, uint32_t value) = 0;
|
2013-09-03 09:35:39 -04:00
|
|
|
virtual bool connect_parameter(uint32_t /* type */ , const std::string & /* value */ ) { return false; } // not generally used.
|
2013-09-13 10:35:19 -04:00
|
|
|
virtual bool connect_additional_address(uint32_t /*type*/, const struct sockaddr_storage & /*addr*/) { return false; } // only needed by udp.
|
2008-02-26 21:32:20 -05:00
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
protected:
|
|
|
|
PQInterface *parent() { return p; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
PQInterface *p;
|
2014-03-17 16:56:06 -04:00
|
|
|
RsPeerId peerId;
|
2007-11-14 22:18:48 -05:00
|
|
|
};
|
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
//! network binary interface (abstract)
|
|
|
|
/**
|
|
|
|
* Should be used for networking and connecting to addresses
|
|
|
|
*
|
|
|
|
* @see pqissl
|
|
|
|
* @see NetInterface
|
|
|
|
* @see BinInterface
|
|
|
|
**/
|
2007-11-14 22:18:48 -05:00
|
|
|
class NetBinInterface: public NetInterface, public BinInterface
|
|
|
|
{
|
|
|
|
public:
|
2014-03-17 16:56:06 -04:00
|
|
|
NetBinInterface(PQInterface *parent, const RsPeerId& id)
|
2007-12-11 20:29:14 -05:00
|
|
|
:NetInterface(parent, id)
|
2007-11-14 22:18:48 -05:00
|
|
|
{ return; }
|
|
|
|
virtual ~NetBinInterface() { return; }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define CHAN_SIGN_SIZE 16
|
2008-02-07 11:18:34 -05:00
|
|
|
#define CERTSIGNLEN 16 /* actual byte length of signature */
|
|
|
|
#define PQI_PEERID_LENGTH 32 /* When expanded into a string */
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
#endif // PQI_BASE_ITEM_HEADER
|
|
|
|
|