2013-11-02 10:35:33 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/services: p3grouter.h
|
|
|
|
*
|
|
|
|
* Services for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2013 by Cyril Soler
|
|
|
|
*
|
|
|
|
* 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 "csoler@users.sourceforge.net".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
2014-04-13 15:27:50 -04:00
|
|
|
#include <fstream>
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2014-03-29 10:18:05 -04:00
|
|
|
#include "retroshare/rsgrouter.h"
|
|
|
|
#include "retroshare/rstypes.h"
|
|
|
|
|
2013-11-02 10:35:33 -04:00
|
|
|
#include "services/p3service.h"
|
|
|
|
#include "pqi/p3cfgmgr.h"
|
|
|
|
|
|
|
|
#include "groutertypes.h"
|
|
|
|
#include "groutermatrix.h"
|
|
|
|
#include "grouteritems.h"
|
2014-03-29 10:18:05 -04:00
|
|
|
//#include "groutercache.h"
|
2013-11-02 10:35:33 -04:00
|
|
|
|
|
|
|
// To be put in pqi/p3cfgmgr.h
|
|
|
|
//
|
|
|
|
static const uint32_t CONFIG_TYPE_GROUTER = 0x0016 ;
|
|
|
|
|
|
|
|
class p3LinkMgr ;
|
2013-12-27 15:06:47 -05:00
|
|
|
class RsGRouterPublishKeyItem ;
|
|
|
|
class RsGRouterACKItem ;
|
2013-12-22 12:04:13 -05:00
|
|
|
|
2013-11-02 10:35:33 -04:00
|
|
|
class p3GRouter: public RsGRouter, public p3Service, public p3Config
|
|
|
|
{
|
|
|
|
public:
|
2014-03-29 19:03:04 -04:00
|
|
|
p3GRouter(p3ServiceControl *sc,p3LinkMgr *lm) ;
|
2013-11-02 10:35:33 -04:00
|
|
|
|
|
|
|
//===================================================//
|
|
|
|
// Router clients business //
|
|
|
|
//===================================================//
|
|
|
|
|
|
|
|
// This method allows to associate client ids (that are saved to disk) to client objects deriving
|
|
|
|
// from GRouterClientService. The various services are responsible for regstering themselves to the
|
|
|
|
// global router, with consistent ids. The services are stored in a map, and arriving objects are
|
|
|
|
// passed on the correct service depending on the client id of the key they are reaching.
|
|
|
|
//
|
|
|
|
bool registerClientService(const GRouterServiceId& id,GRouterClientService *service) ;
|
|
|
|
|
2014-01-03 17:41:20 -05:00
|
|
|
// Use this method to register/unregister a key that the global router will
|
2013-11-02 10:35:33 -04:00
|
|
|
// forward in the network, so that is can be a possible destination for
|
|
|
|
// global messages.
|
|
|
|
//
|
2014-01-30 17:14:37 -05:00
|
|
|
// key : The key that is published
|
|
|
|
// fingerp : Fingerprint of the key to encrypt the data.
|
|
|
|
// desc_str : Any fixed length string (< 20 characters) to descript the address in words.
|
2013-11-02 10:35:33 -04:00
|
|
|
// client_id: id of the client service to send the traffic to.
|
|
|
|
// To obtain a client id, the service must register using the previous method.
|
|
|
|
//
|
2014-01-03 17:41:20 -05:00
|
|
|
// Unregistering a key might not have an instantaneous effect, so the client is responsible for
|
|
|
|
// discarding traffic that might later come for this key.
|
|
|
|
//
|
2014-03-29 10:18:05 -04:00
|
|
|
bool registerKey(const GRouterKeyId& key, const GRouterServiceId& client_id,const std::string& description_string) ;
|
2014-01-03 17:41:20 -05:00
|
|
|
bool unregisterKey(const GRouterKeyId& key) ;
|
2013-11-02 10:35:33 -04:00
|
|
|
|
|
|
|
//===================================================//
|
|
|
|
// Client/server request services //
|
|
|
|
//===================================================//
|
|
|
|
|
|
|
|
// Sends an item to the given destination. The router takes ownership of
|
2014-04-19 12:02:11 -04:00
|
|
|
// the memory. That means item_data will be erase on return. The returned id should be
|
|
|
|
// remembered by the client, so that he knows when the data has been received.
|
2014-04-21 08:39:30 -04:00
|
|
|
// The client id is supplied so that the client can be notified when the data has been received.
|
2013-11-02 10:35:33 -04:00
|
|
|
//
|
2014-04-21 08:39:30 -04:00
|
|
|
void sendData(const GRouterKeyId& destination,const GRouterServiceId& client_id, RsGRouterGenericDataItem *item,GRouterMsgPropagationId& id) ;
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2013-12-22 12:04:13 -05:00
|
|
|
// Sends an ACK to the origin of the msg. This is used to notify for
|
|
|
|
// unfound route, or message correctly received, depending on the particular situation.
|
|
|
|
//
|
2014-03-17 16:56:06 -04:00
|
|
|
void sendACK(const RsPeerId& peer,GRouterMsgPropagationId mid, uint32_t flags) ;
|
2013-12-22 12:04:13 -05:00
|
|
|
|
2013-11-02 10:35:33 -04:00
|
|
|
//===================================================//
|
|
|
|
// Interface with RsGRouter //
|
|
|
|
//===================================================//
|
|
|
|
|
|
|
|
// debug info from routing matrix
|
|
|
|
// - list of known key ids
|
|
|
|
// - list of clues/time_stamp for each key.
|
|
|
|
// - real time routing probabilities
|
|
|
|
//
|
2014-03-29 10:18:05 -04:00
|
|
|
virtual bool getRoutingMatrixInfo(GRouterRoutingMatrixInfo& info) ;
|
2013-11-02 10:35:33 -04:00
|
|
|
|
|
|
|
// debug info from routing cache
|
|
|
|
// - Cache Items
|
|
|
|
// * which message ids
|
|
|
|
// * directions
|
|
|
|
// * timestamp
|
|
|
|
// * message type
|
|
|
|
// - Cache state (memory size, etc)
|
|
|
|
//
|
2014-03-29 10:18:05 -04:00
|
|
|
virtual bool getRoutingCacheInfo(std::vector<GRouterRoutingCacheInfo>& info) ;
|
|
|
|
|
|
|
|
//===================================================//
|
|
|
|
// Derived from p3Service //
|
|
|
|
//===================================================//
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2014-03-29 10:18:05 -04:00
|
|
|
virtual RsServiceInfo getServiceInfo()
|
|
|
|
{
|
|
|
|
return RsServiceInfo(RS_SERVICE_TYPE_GROUTER,
|
|
|
|
SERVICE_INFO_APP_NAME,
|
|
|
|
SERVICE_INFO_APP_MAJOR_VERSION,
|
|
|
|
SERVICE_INFO_APP_MINOR_VERSION,
|
|
|
|
SERVICE_INFO_MIN_MAJOR_VERSION,
|
|
|
|
SERVICE_INFO_MIN_MINOR_VERSION) ;
|
|
|
|
}
|
2014-04-13 15:27:50 -04:00
|
|
|
|
|
|
|
void setDebugEnabled(bool b) { _debug_enabled = b ; }
|
2013-11-05 16:15:26 -05:00
|
|
|
protected:
|
2013-11-02 10:35:33 -04:00
|
|
|
//===================================================//
|
|
|
|
// Routing method handling //
|
|
|
|
//===================================================//
|
|
|
|
|
|
|
|
// Calls
|
|
|
|
// - autoWash()
|
|
|
|
// - packet handling methods
|
|
|
|
// - matrix updates
|
|
|
|
//
|
|
|
|
virtual int tick() ;
|
|
|
|
|
2014-03-29 10:18:05 -04:00
|
|
|
static const std::string SERVICE_INFO_APP_NAME ;
|
|
|
|
static const uint16_t SERVICE_INFO_APP_MAJOR_VERSION = 1;
|
|
|
|
static const uint16_t SERVICE_INFO_APP_MINOR_VERSION = 0;
|
|
|
|
static const uint16_t SERVICE_INFO_MIN_MAJOR_VERSION = 1;
|
|
|
|
static const uint16_t SERVICE_INFO_MIN_MINOR_VERSION = 0;
|
|
|
|
|
2013-11-05 16:15:26 -05:00
|
|
|
private:
|
2014-04-13 15:27:50 -04:00
|
|
|
class nullstream: public std::ostream {};
|
|
|
|
|
|
|
|
std::ostream& grouter_debug() const
|
|
|
|
{
|
|
|
|
static nullstream null ;
|
|
|
|
|
|
|
|
return _debug_enabled?(std::cerr):null;
|
|
|
|
}
|
|
|
|
|
2013-11-02 10:35:33 -04:00
|
|
|
void autoWash() ;
|
2013-12-22 12:04:13 -05:00
|
|
|
void routePendingObjects() ;
|
2013-11-02 10:35:33 -04:00
|
|
|
void handleIncoming() ;
|
2013-11-05 16:15:26 -05:00
|
|
|
void debugDump() ;
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2014-03-29 10:18:05 -04:00
|
|
|
// utility functions
|
|
|
|
//
|
2014-04-11 17:56:10 -04:00
|
|
|
static uint32_t computeBranchingFactor(const std::vector<RsPeerId>& friends,uint32_t dist) ;
|
2014-04-13 15:27:50 -04:00
|
|
|
std::set<uint32_t> computeRoutingFriends(const std::vector<RsPeerId>& friends,const std::vector<float>& probas,uint32_t N) ;
|
2014-04-12 15:58:45 -04:00
|
|
|
static float computeMatrixContribution(float base,uint32_t time_shift,float probability) ;
|
2014-04-15 18:10:53 -04:00
|
|
|
static time_t computeNextTimeDelay(time_t duration) ;
|
2014-04-07 17:36:43 -04:00
|
|
|
|
2014-04-21 08:39:30 -04:00
|
|
|
void locked_notifyClientAcknowledged(const GRouterMsgPropagationId& msg_id,const GRouterServiceId& service_id) const ;
|
2014-04-19 12:02:11 -04:00
|
|
|
|
2014-04-07 17:36:43 -04:00
|
|
|
uint32_t computeRandomDistanceIncrement(const RsPeerId& pid,const GRouterKeyId& destination_id) ;
|
2014-03-29 10:18:05 -04:00
|
|
|
|
2013-11-02 10:35:33 -04:00
|
|
|
//===================================================//
|
|
|
|
// p3Config methods //
|
|
|
|
//===================================================//
|
|
|
|
|
|
|
|
// Load/save the routing info, the pending items in transit, and the config variables.
|
|
|
|
//
|
|
|
|
virtual bool loadList(std::list<RsItem*>& items) ;
|
2013-12-27 15:06:47 -05:00
|
|
|
virtual bool saveList(bool& cleanup,std::list<RsItem*>& items) ;
|
2013-11-02 10:35:33 -04:00
|
|
|
|
|
|
|
virtual RsSerialiser *setupSerialiser() ;
|
|
|
|
|
|
|
|
//===================================================//
|
|
|
|
// Debug methods //
|
|
|
|
//===================================================//
|
|
|
|
|
|
|
|
// Prints the internal state of the router, for debug purpose.
|
|
|
|
//
|
|
|
|
void debug_dump() ;
|
|
|
|
|
|
|
|
// Stores the routing info
|
|
|
|
// - list of known key ids
|
|
|
|
// - list of clues/time_stamp for each key.
|
|
|
|
// - real time routing probabilities
|
|
|
|
//
|
|
|
|
GRouterMatrix _routing_matrix ;
|
|
|
|
|
|
|
|
// Stores the routing events.
|
|
|
|
// - ongoing requests, waiting for return ACK
|
2013-12-22 12:04:13 -05:00
|
|
|
// - pending items
|
|
|
|
// Both a stored in 2 different lists, to allow a more efficient handling.
|
2013-11-02 10:35:33 -04:00
|
|
|
//
|
2013-12-22 12:04:13 -05:00
|
|
|
std::map<GRouterMsgPropagationId, GRouterRoutingInfo> _pending_messages;// pending messages
|
2013-11-02 10:35:33 -04:00
|
|
|
|
|
|
|
// Stores the keys which identify the router's node. For each key, a structure holds:
|
|
|
|
// - the client service
|
|
|
|
// - flags
|
|
|
|
// - usage time stamps
|
|
|
|
//
|
|
|
|
std::map<GRouterKeyId, GRouterPublishedKeyInfo> _owned_key_ids ;
|
|
|
|
|
|
|
|
// Registered services. These are known to the different peers with a common id,
|
|
|
|
// so it's important to keep consistency here. This map is volatile, and re-created at each startup of
|
|
|
|
// the software, when newly created services register themselves.
|
|
|
|
|
|
|
|
std::map<GRouterServiceId,GRouterClientService *> _registered_services ;
|
|
|
|
|
|
|
|
// Data handling ethods
|
|
|
|
//
|
|
|
|
void handleRecvDataItem(RsGRouterGenericDataItem *item);
|
|
|
|
void handleRecvACKItem(RsGRouterACKItem *item);
|
|
|
|
|
|
|
|
// Pointers to other RS objects
|
|
|
|
//
|
2014-03-29 19:03:04 -04:00
|
|
|
p3ServiceControl *mServiceControl ;
|
|
|
|
p3LinkMgr *mLinkMgr ;
|
2013-11-02 10:35:33 -04:00
|
|
|
|
|
|
|
// Multi-thread protection mutex.
|
|
|
|
//
|
|
|
|
RsMutex grMtx ;
|
2013-12-28 16:47:15 -05:00
|
|
|
|
|
|
|
// config update/save variables
|
|
|
|
bool _changed ;
|
2014-04-13 15:27:50 -04:00
|
|
|
bool _debug_enabled ;
|
2014-04-07 17:36:43 -04:00
|
|
|
|
|
|
|
time_t _last_autowash_time ;
|
2014-04-11 17:56:10 -04:00
|
|
|
time_t _last_matrix_update_time ;
|
2014-04-07 17:36:43 -04:00
|
|
|
time_t _last_debug_output_time ;
|
|
|
|
time_t _last_config_changed ;
|
|
|
|
|
|
|
|
uint64_t _random_salt ;
|
2013-11-02 10:35:33 -04:00
|
|
|
};
|
|
|
|
|
2014-04-13 15:27:50 -04:00
|
|
|
template<typename T> p3GRouter::nullstream& operator<<(p3GRouter::nullstream& ns,const T&) { return ns ; }
|