2013-06-04 17:00:43 -04:00
/*
* libretroshare / src / gxs : rsgxnetutils . cc
*
* Helper objects for the operation rsgxsnetservice
*
* Copyright 2012 - 2013 by Christopher Evi - Parker
*
* 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 " .
*
*/
# include "rsgxsnetutils.h"
2014-03-28 23:57:44 -04:00
# include "pqi/p3servicecontrol.h"
2014-04-27 09:14:07 -04:00
# include "pgp/pgpauxutils.h"
2013-06-04 17:00:43 -04:00
const time_t AuthorPending : : EXPIRY_PERIOD_OFFSET = 30 ; // 30 seconds
const int AuthorPending : : MSG_PEND = 1 ;
const int AuthorPending : : GRP_PEND = 2 ;
AuthorPending : : AuthorPending ( RsGixsReputation * rep , time_t timeStamp )
: mRep ( rep ) , mTimeStamp ( timeStamp ) {
}
AuthorPending : : ~ AuthorPending ( )
{
}
bool AuthorPending : : expired ( ) const
{
return time ( NULL ) > ( mTimeStamp + EXPIRY_PERIOD_OFFSET ) ;
}
2017-01-05 17:07:59 -05:00
bool AuthorPending : : getAuthorRep ( GixsReputation & rep , const RsGxsId & authorId , const RsPeerId & peerId )
2013-06-04 17:00:43 -04:00
{
if ( mRep - > haveReputation ( authorId ) )
{
return mRep - > getReputation ( authorId , rep ) ;
}
2014-03-17 16:56:06 -04:00
std : : list < RsPeerId > peers ;
2013-11-04 09:09:32 -05:00
peers . push_back ( peerId ) ;
mRep - > loadReputation ( authorId , peers ) ;
2013-06-04 17:00:43 -04:00
return false ;
}
MsgAuthEntry : : MsgAuthEntry ( )
: mPassedVetting ( false ) { }
GrpAuthEntry : : GrpAuthEntry ( )
: mPassedVetting ( false ) { }
2014-03-17 16:56:06 -04:00
MsgRespPending : : MsgRespPending ( RsGixsReputation * rep , const RsPeerId & peerId , const MsgAuthorV & msgAuthV , int cutOff )
2013-06-04 17:00:43 -04:00
: AuthorPending ( rep , time ( NULL ) ) , mPeerId ( peerId ) , mMsgAuthV ( msgAuthV ) , mCutOff ( cutOff )
{
}
2014-03-17 16:56:06 -04:00
GrpRespPending : : GrpRespPending ( RsGixsReputation * rep , const RsPeerId & peerId , const GrpAuthorV & grpAuthV , int cutOff )
2013-06-04 17:00:43 -04:00
: AuthorPending ( rep , time ( NULL ) ) , mPeerId ( peerId ) , mGrpAuthV ( grpAuthV ) , mCutOff ( cutOff )
{
}
int MsgRespPending : : getType ( ) const
{
return MSG_PEND ;
}
bool MsgRespPending : : accepted ( )
{
MsgAuthorV : : iterator cit = mMsgAuthV . begin ( ) ;
MsgAuthorV : : size_type count = 0 ;
2014-10-24 18:07:26 -04:00
for ( ; cit ! = mMsgAuthV . end ( ) ; + + cit )
2013-06-04 17:00:43 -04:00
{
MsgAuthEntry & entry = * cit ;
if ( ! entry . mPassedVetting )
{
GixsReputation rep ;
2013-11-04 09:09:32 -05:00
if ( getAuthorRep ( rep , entry . mAuthorId , mPeerId ) )
2013-06-04 17:00:43 -04:00
{
2015-10-12 10:41:48 -04:00
if ( rep . score > = mCutOff )
2013-06-04 17:00:43 -04:00
{
entry . mPassedVetting = true ;
count + + ;
}
}
} else
{
count + + ;
}
}
return count = = mMsgAuthV . size ( ) ;
}
int GrpRespPending : : getType ( ) const
{
return GRP_PEND ;
}
bool GrpRespPending : : accepted ( )
{
GrpAuthorV : : iterator cit = mGrpAuthV . begin ( ) ;
GrpAuthorV : : size_type count = 0 ;
2014-10-24 18:07:26 -04:00
for ( ; cit ! = mGrpAuthV . end ( ) ; + + cit )
2013-06-04 17:00:43 -04:00
{
GrpAuthEntry & entry = * cit ;
if ( ! entry . mPassedVetting )
{
GixsReputation rep ;
2013-11-04 09:09:32 -05:00
if ( getAuthorRep ( rep , entry . mAuthorId , mPeerId ) )
2013-06-04 17:00:43 -04:00
{
2015-10-12 10:41:48 -04:00
if ( rep . score > = mCutOff )
2013-06-04 17:00:43 -04:00
{
entry . mPassedVetting = true ;
count + + ;
}
}
} else
{
count + + ;
}
}
return count = = mGrpAuthV . size ( ) ;
}
/** NxsTransaction definition **/
const uint8_t NxsTransaction : : FLAG_STATE_STARTING = 0x0001 ; // when
const uint8_t NxsTransaction : : FLAG_STATE_RECEIVING = 0x0002 ; // begin receiving items for incoming trans
const uint8_t NxsTransaction : : FLAG_STATE_SENDING = 0x0004 ; // begin sending items for outgoing trans
const uint8_t NxsTransaction : : FLAG_STATE_COMPLETED = 0x008 ;
const uint8_t NxsTransaction : : FLAG_STATE_FAILED = 0x0010 ;
const uint8_t NxsTransaction : : FLAG_STATE_WAITING_CONFIRM = 0x0020 ;
NxsTransaction : : NxsTransaction ( )
: mFlag ( 0 ) , mTimeOut ( 0 ) , mTransaction ( NULL ) {
}
NxsTransaction : : ~ NxsTransaction ( ) {
std : : list < RsNxsItem * > : : iterator lit = mItems . begin ( ) ;
2014-10-24 18:07:26 -04:00
for ( ; lit ! = mItems . end ( ) ; + + lit )
2013-06-04 17:00:43 -04:00
{
delete * lit ;
* lit = NULL ;
}
if ( mTransaction )
delete mTransaction ;
mTransaction = NULL ;
}
/* Net Manager */
2014-03-28 23:57:44 -04:00
RsNxsNetMgrImpl : : RsNxsNetMgrImpl ( p3ServiceControl * sc )
: mServiceCtrl ( sc )
2013-06-04 17:00:43 -04:00
{
}
2014-03-17 16:56:06 -04:00
const RsPeerId & RsNxsNetMgrImpl : : getOwnId ( )
2013-06-04 17:00:43 -04:00
{
2014-03-28 23:57:44 -04:00
return mServiceCtrl - > getOwnId ( ) ;
2013-06-04 17:00:43 -04:00
}
2014-03-28 23:57:44 -04:00
void RsNxsNetMgrImpl : : getOnlineList ( const uint32_t serviceId , std : : set < RsPeerId > & ssl_peers )
2013-06-04 17:00:43 -04:00
{
2014-03-28 23:57:44 -04:00
mServiceCtrl - > getPeersConnected ( serviceId , ssl_peers ) ;
2013-06-04 17:00:43 -04:00
}
const time_t GrpCircleVetting : : EXPIRY_PERIOD_OFFSET = 5 ; // 10 seconds
const int GrpCircleVetting : : GRP_ID_PEND = 1 ;
const int GrpCircleVetting : : GRP_ITEM_PEND = 2 ;
const int GrpCircleVetting : : MSG_ID_SEND_PEND = 3 ;
const int GrpCircleVetting : : MSG_ID_RECV_PEND = 3 ;
2014-05-01 14:50:07 -04:00
GrpIdCircleVet : : GrpIdCircleVet ( const RsGxsGroupId & grpId , const RsGxsCircleId & circleId , const RsGxsId & authId )
: mGroupId ( grpId ) , mCircleId ( circleId ) , mAuthorId ( authId ) , mCleared ( false ) { }
2013-06-04 17:00:43 -04:00
2014-04-27 09:14:07 -04:00
GrpCircleVetting : : GrpCircleVetting ( RsGcxs * const circles , PgpAuxUtils * pgpUtils )
2014-05-01 14:50:07 -04:00
: mCircles ( circles ) , mPgpUtils ( pgpUtils ) , mTimeStamp ( time ( NULL ) ) { }
2013-06-04 17:00:43 -04:00
GrpCircleVetting : : ~ GrpCircleVetting ( ) { }
bool GrpCircleVetting : : expired ( )
{
return time ( NULL ) > ( mTimeStamp + EXPIRY_PERIOD_OFFSET ) ;
}
2016-02-20 17:53:03 -05:00
bool GrpCircleVetting : : canSend ( const SSLIdType & peerId , const RsGxsCircleId & circleId , bool & should_encrypt )
2013-06-04 17:00:43 -04:00
{
if ( mCircles - > isLoaded ( circleId ) )
{
2014-04-27 09:14:07 -04:00
const RsPgpId & pgpId = mPgpUtils - > getPGPId ( peerId ) ;
2016-02-20 17:53:03 -05:00
return mCircles - > canSend ( circleId , pgpId , should_encrypt ) ;
2013-06-04 17:00:43 -04:00
}
mCircles - > loadCircle ( circleId ) ;
return false ;
}
GrpCircleIdRequestVetting : : GrpCircleIdRequestVetting (
2014-04-27 09:14:07 -04:00
RsGcxs * const circles ,
PgpAuxUtils * pgpUtils ,
std : : vector < GrpIdCircleVet > grpCircleV , const RsPeerId & peerId )
: GrpCircleVetting ( circles , pgpUtils ) , mGrpCircleV ( grpCircleV ) , mPeerId ( peerId ) { }
2013-06-04 17:00:43 -04:00
bool GrpCircleIdRequestVetting : : cleared ( )
{
2015-06-16 14:54:25 -04:00
std : : vector < GrpIdCircleVet > : : size_type i , count = 0 ;
2014-10-24 18:07:26 -04:00
for ( i = 0 ; i < mGrpCircleV . size ( ) ; + + i )
2013-06-04 17:00:43 -04:00
{
GrpIdCircleVet & gic = mGrpCircleV [ i ] ;
if ( ! gic . mCleared )
{
2016-02-20 17:53:03 -05:00
if ( canSend ( mPeerId , gic . mCircleId , gic . mShouldEncrypt ) )
2013-06-04 17:00:43 -04:00
{
gic . mCleared = true ;
count + + ;
}
}
else
{
count + + ;
}
}
return count = = mGrpCircleV . size ( ) ;
}
int GrpCircleIdRequestVetting : : getType ( ) const
{
return GRP_ID_PEND ;
}
MsgIdCircleVet : : MsgIdCircleVet ( const RsGxsMessageId & msgId ,
2014-03-17 16:56:06 -04:00
const RsGxsId & authorId )
2013-06-04 17:00:43 -04:00
: mMsgId ( msgId ) , mAuthorId ( authorId ) {
}
2014-04-27 09:14:07 -04:00
MsgCircleIdsRequestVetting : : MsgCircleIdsRequestVetting ( RsGcxs * const circles ,
PgpAuxUtils * pgpUtils ,
2013-06-04 17:00:43 -04:00
std : : vector < MsgIdCircleVet > msgs , const RsGxsGroupId & grpId ,
2014-03-17 16:56:06 -04:00
const RsPeerId & peerId , const RsGxsCircleId & circleId )
2014-04-27 09:14:07 -04:00
: GrpCircleVetting ( circles , pgpUtils ) , mMsgs ( msgs ) , mGrpId ( grpId ) , mPeerId ( peerId ) , mCircleId ( circleId ) { }
2013-06-04 17:00:43 -04:00
bool MsgCircleIdsRequestVetting : : cleared ( )
{
2016-03-23 20:54:45 -04:00
if ( ! mCircles - > isLoaded ( mCircleId ) )
{
mCircles - > loadCircle ( mCircleId ) ;
return false ;
}
for ( uint32_t i = 0 ; i < mMsgs . size ( ) ; )
2016-05-18 21:13:54 -04:00
if ( ! mCircles - > isRecipient ( mCircleId , mGrpId , mMsgs [ i ] . mAuthorId ) )
2016-03-23 20:54:45 -04:00
{
std : : cerr < < " (WW) MsgCircleIdsRequestVetting::cleared() filtering out message " < < mMsgs [ i ] . mMsgId < < " because it's signed by author " < < mMsgs [ i ] . mAuthorId < < " which is not in circle " < < mCircleId < < std : : endl ;
mMsgs [ i ] = mMsgs [ mMsgs . size ( ) - 1 ] ;
mMsgs . pop_back ( ) ;
}
else
+ + i ;
RsPgpId pgpId = mPgpUtils - > getPGPId ( mPeerId ) ;
bool can_send_res = mCircles - > canSend ( mCircleId , pgpId , mShouldEncrypt ) ;
if ( mShouldEncrypt ) // that means the circle is external
return true ;
else
return can_send_res ;
2013-06-04 17:00:43 -04:00
}
int MsgCircleIdsRequestVetting : : getType ( ) const
{
return MSG_ID_SEND_PEND ;
}
2014-04-26 19:48:33 -04:00