2014-02-19 06:08:37 -05:00
/*
* libretroshare / src / services / p3gxsreputation . h
*
* Exchange list of Peers Reputations .
*
* Copyright 2014 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.1 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 SERVICE_RSGXSREPUTATION_HEADER
# define SERVICE_RSGXSREPUTATION_HEADER
# include <string>
# include <list>
# include <map>
# include <set>
2017-01-09 17:47:51 -05:00
static const uint32_t REPUTATION_IDENTITY_FLAG_UP_TO_DATE = 0x0100 ; // This flag means that the static info has been initialised from p3IdService. Normally such a call should happen once.
static const uint32_t REPUTATION_IDENTITY_FLAG_PGP_LINKED = 0x0001 ;
static const uint32_t REPUTATION_IDENTITY_FLAG_PGP_KNOWN = 0x0002 ;
2015-10-25 22:54:56 -04:00
2014-02-19 06:08:37 -05:00
# include "serialiser/rsgxsreputationitems.h"
# include "retroshare/rsidentity.h"
2015-10-04 23:14:49 -04:00
# include "retroshare/rsreputations.h"
2017-01-10 15:44:37 -05:00
# include "gxs/rsgixs.h"
2014-02-19 06:08:37 -05:00
# include "services/p3service.h"
class p3LinkMgr ;
class ReputationConfig
{
public :
ReputationConfig ( )
2015-06-19 08:04:48 -04:00
: mPeerId ( ) , mLatestUpdate ( 0 ) , mLastQuery ( 0 ) { return ; }
2014-02-19 06:08:37 -05:00
ReputationConfig ( const RsPeerId & peerId )
2015-06-19 08:04:48 -04:00
: mPeerId ( peerId ) , mLatestUpdate ( 0 ) , mLastQuery ( 0 ) { return ; }
2014-02-19 06:08:37 -05:00
RsPeerId mPeerId ;
time_t mLatestUpdate ;
time_t mLastQuery ;
} ;
2016-08-04 05:43:35 -04:00
struct BannedNodeInfo
{
time_t last_activity_TS ; // updated everytime a node or one of its former identities is required
std : : set < RsGxsId > known_identities ; // list of known identities from this node. This is kept for a while, and useful in order to avoid re-asking these keys.
} ;
2014-02-19 06:08:37 -05:00
class Reputation
{
public :
Reputation ( )
2017-01-08 05:14:18 -05:00
: mOwnOpinion ( RsReputations : : OPINION_NEUTRAL ) , mOwnOpinionTs ( 0 ) , mFriendAverage ( 1.0f ) , mReputationScore ( RsReputations : : OPINION_NEUTRAL ) , mIdentityFlags ( 0 ) { }
2015-10-25 23:45:33 -04:00
2016-06-02 08:42:32 -04:00
Reputation ( const RsGxsId & /*about*/ )
2017-01-08 05:14:18 -05:00
: mOwnOpinion ( RsReputations : : OPINION_NEUTRAL ) , mOwnOpinionTs ( 0 ) , mFriendAverage ( 1.0f ) , mReputationScore ( RsReputations : : OPINION_NEUTRAL ) , mIdentityFlags ( 0 ) { }
2014-02-19 06:08:37 -05:00
2015-10-25 23:45:33 -04:00
void updateReputation ( ) ;
2014-02-19 06:08:37 -05:00
2015-10-07 23:44:24 -04:00
std : : map < RsPeerId , RsReputations : : Opinion > mOpinions ;
2014-02-19 06:08:37 -05:00
int32_t mOwnOpinion ;
time_t mOwnOpinionTs ;
2016-12-23 11:52:02 -05:00
float mFriendAverage ;
uint32_t mFriendsPositive ; // number of positive vites from friends
uint32_t mFriendsNegative ; // number of negative vites from friends
float mReputationScore ;
RsPgpId mOwnerNode ;
2015-10-25 22:54:56 -04:00
2016-12-23 11:52:02 -05:00
uint32_t mIdentityFlags ;
2017-01-08 05:14:18 -05:00
time_t mLastUsedTS ; // last time the reputation was asked. Used to keep track of activity and clean up some reputation data.
2014-02-19 06:08:37 -05:00
} ;
//!The p3GxsReputation service.
/**
*
*
*/
2017-01-10 15:44:37 -05:00
class p3GxsReputation : public p3Service , public p3Config , public RsGixsReputation , public RsReputations /* , public pqiMonitor */
2014-02-19 06:08:37 -05:00
{
2016-07-25 15:45:49 -04:00
public :
p3GxsReputation ( p3LinkMgr * lm ) ;
virtual RsServiceInfo getServiceInfo ( ) ;
/***** Interface for RsReputations *****/
virtual bool setOwnOpinion ( const RsGxsId & key_id , const Opinion & op ) ;
2016-08-04 05:43:35 -04:00
virtual bool getReputationInfo ( const RsGxsId & id , const RsPgpId & ownerNode , ReputationInfo & info ) ;
virtual bool isIdentityBanned ( const RsGxsId & id ) ;
virtual bool isNodeBanned ( const RsPgpId & id ) ;
virtual void banNode ( const RsPgpId & id , bool b ) ;
2017-01-09 17:47:51 -05:00
virtual ReputationLevel overallReputationLevel ( const RsGxsId & id ) ;
2016-08-04 05:43:35 -04:00
2016-07-25 15:45:49 -04:00
virtual void setNodeAutoPositiveOpinionForContacts ( bool b ) ;
virtual bool nodeAutoPositiveOpinionForContacts ( ) ;
2016-12-28 12:58:49 -05:00
uint32_t thresholdForRemotelyNegativeReputation ( ) ;
uint32_t thresholdForRemotelyPositiveReputation ( ) ;
void setThresholdForRemotelyNegativeReputation ( uint32_t thresh ) ;
void setThresholdForRemotelyPositiveReputation ( uint32_t thresh ) ;
2016-07-25 15:45:49 -04:00
/***** overloaded from p3Service *****/
virtual int tick ( ) ;
virtual int status ( ) ;
/*!
* Interface stuff .
*/
/************* from p3Config *******************/
virtual RsSerialiser * setupSerialiser ( ) ;
virtual bool saveList ( bool & cleanup , std : : list < RsItem * > & ) ;
virtual void saveDone ( ) ;
virtual bool loadList ( std : : list < RsItem * > & load ) ;
2016-04-02 14:04:08 -04:00
private :
2014-02-19 06:08:37 -05:00
2016-07-25 15:45:49 -04:00
bool processIncoming ( ) ;
2014-02-19 06:08:37 -05:00
2016-07-25 15:45:49 -04:00
bool SendReputations ( RsGxsReputationRequestItem * request ) ;
bool RecvReputations ( RsGxsReputationUpdateItem * item ) ;
bool updateLatestUpdate ( RsPeerId peerid , time_t latest_update ) ;
2016-08-04 05:43:35 -04:00
void updateBannedNodesProxy ( ) ;
2016-07-25 15:45:49 -04:00
// internal update of data. Takes care of cleaning empty boxes.
void locked_updateOpinion ( const RsPeerId & from , const RsGxsId & about , RsReputations : : Opinion op ) ;
bool loadReputationSet ( RsGxsReputationSetItem * item , const std : : set < RsPeerId > & peerSet ) ;
2017-01-08 05:14:18 -05:00
bool loadReputationSet_deprecated3 ( RsGxsReputationSetItem_deprecated3 * item , const std : : set < RsPeerId > & peerSet ) ;
2016-07-25 15:45:49 -04:00
int sendPackets ( ) ;
void cleanup ( ) ;
void sendReputationRequests ( ) ;
int sendReputationRequest ( RsPeerId peerid ) ;
void debug_print ( ) ;
2017-01-09 17:47:51 -05:00
void updateStaticIdentityFlags ( ) ;
2016-07-25 15:45:49 -04:00
private :
RsMutex mReputationMtx ;
2017-01-08 16:00:54 -05:00
time_t mLastCleanUp ;
2016-07-25 15:45:49 -04:00
time_t mRequestTime ;
time_t mStoreTime ;
time_t mLastBannedNodesUpdate ;
2016-07-25 16:04:30 -04:00
time_t mLastIdentityFlagsUpdate ;
2016-07-25 15:45:49 -04:00
bool mReputationsUpdated ;
float mAutoBanIdentitiesLimit ;
bool mAutoSetPositiveOptionToContacts ;
p3LinkMgr * mLinkMgr ;
// Data for Reputation.
std : : map < RsPeerId , ReputationConfig > mConfig ;
std : : map < RsGxsId , Reputation > mReputations ;
std : : multimap < time_t , RsGxsId > mUpdated ;
// PGP Ids auto-banned. This is updated regularly.
2016-08-04 05:43:35 -04:00
std : : map < RsPgpId , BannedNodeInfo > mBannedPgpIds ;
std : : set < RsGxsId > mPerNodeBannedIdsProxy ;
bool mBannedNodesProxyNeedsUpdate ;
2016-12-28 12:58:49 -05:00
uint32_t mMinVotesForRemotelyPositive ;
uint32_t mMinVotesForRemotelyNegative ;
2017-01-09 17:47:51 -05:00
bool mChanged ; // slow version of IndicateConfigChanged();
time_t mLastReputationConfigSaved ;
2014-02-19 06:08:37 -05:00
} ;
# endif //SERVICE_RSGXSREPUTATION_HEADER