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
|
2008-02-26 13:55:16 -05:00
|
|
|
#define PQI_MAX_PORT 50000
|
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.
|
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()
|
|
|
|
:bw_in(0), bw_out(0), bwMax_in(0), bwMax_out(0) { return; }
|
|
|
|
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)
|
|
|
|
bwMax_in = val;
|
|
|
|
else
|
|
|
|
bwMax_out = val;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
public:
|
2007-12-11 20:29:14 -05:00
|
|
|
PQInterface(std::string id) :peerId(id) { return; }
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual ~PQInterface() { return; }
|
|
|
|
|
2010-09-18 15:09:11 -04:00
|
|
|
/*!
|
|
|
|
* allows user to send RsItems to a particular facility (file, network)
|
|
|
|
*/
|
2007-12-11 20:29:14 -05:00
|
|
|
virtual int SendItem(RsItem *) = 0;
|
2010-09-18 15:09:11 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Retrieve RsItem from a facility
|
|
|
|
*/
|
2007-12-11 20:29:14 -05:00
|
|
|
virtual RsItem *GetItem() = 0;
|
2007-11-14 22:18:48 -05:00
|
|
|
|
2010-04-15 07:39:54 -04:00
|
|
|
/**
|
|
|
|
* also there are tick + person id functions.
|
|
|
|
*/
|
2007-11-14 22:18:48 -05:00
|
|
|
virtual int tick() { return 0; }
|
|
|
|
virtual int status() { return 0; }
|
2007-12-11 20:29:14 -05:00
|
|
|
virtual std::string PeerId() { return peerId; }
|
2007-11-14 22:18:48 -05:00
|
|
|
|
|
|
|
// the callback from NetInterface Connection Events.
|
2010-06-26 08:31:24 -04:00
|
|
|
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:
|
|
|
|
|
|
|
|
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;
|
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;
|
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
|
|
|
|
|