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"
|
|
|
|
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
#define PQI_MIN_PORT 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);
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
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; }
|
|
|
|
|
|
|
|
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:
|
|
|
|
PQInterface(const std::string &id) :peerId(id) { return; }
|
|
|
|
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 ;
|
|
|
|
std::cerr << "Warning: PQInterface::SendItem(RsItem*,uint32_t&) calledbut not overloaded! Serialized size will not be returned." << std::endl;
|
|
|
|
return SendItem(item) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Retrieve RsItem from a facility
|
|
|
|
*/
|
|
|
|
virtual RsItem *GetItem() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* also there are tick + person id functions.
|
|
|
|
*/
|
|
|
|
virtual int tick() { return 0; }
|
|
|
|
virtual int status() { return 0; }
|
|
|
|
virtual std::string PeerId() { return peerId; }
|
|
|
|
|
|
|
|
// the callback from NetInterface Connection Events.
|
|
|
|
virtual int notifyEvent(NetInterface *ni, int event)
|
|
|
|
{
|
|
|
|
(void) ni; /* remove unused parameter warnings */
|
|
|
|
(void) event; /* remove unused parameter warnings */
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-11 20:29:14 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-09-04 16:01:30 -04:00
|
|
|
std::string 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;
|
2009-12-13 16:59:26 -05:00
|
|
|
const uint32_t PQI_CONNECT_TUNNEL = 0x0003;
|
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)
|
|
|
|
*/
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual int netstatus() = 0;
|
|
|
|
virtual int isactive() = 0;
|
|
|
|
virtual bool moretoread() = 0;
|
|
|
|
virtual bool cansend() = 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
|
|
|
|
**/
|
2008-01-25 01:36:40 -05:00
|
|
|
virtual std::string 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
|
|
|
|
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
|
|
|
|
*/
|
2007-12-11 20:29:14 -05:00
|
|
|
NetInterface(PQInterface *p_in, std::string id)
|
|
|
|
:p(p_in), peerId(id) { return; }
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
virtual ~NetInterface()
|
|
|
|
{ return; }
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
virtual int connect(struct sockaddr_in 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;
|
2007-12-11 20:29:14 -05:00
|
|
|
virtual std::string PeerId() { return peerId; }
|
2009-12-14 13:11:19 -05:00
|
|
|
virtual int getConnectAddress(struct sockaddr_in &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;
|
2011-08-12 09:42:30 -04:00
|
|
|
virtual bool connect_additional_address(uint32_t /*type*/, struct sockaddr_in */*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;
|
2007-12-11 20:29:14 -05:00
|
|
|
std::string 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:
|
2007-12-11 20:29:14 -05:00
|
|
|
NetBinInterface(PQInterface *parent, std::string id)
|
|
|
|
: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
|
|
|
|
|