2012-06-26 15:52:01 -04:00
2012-08-06 17:00:38 -04:00
/*
* libretroshare / src / gxs : rsgxsdata . cc
*
* Gxs Data types used to specific services
*
* Copyright 2012 - 2012 by Christopher Evi - Parker , 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 " .
*
*/
2012-06-26 15:52:01 -04:00
# include "rsgxsdata.h"
# include "serialiser/rsbaseserial.h"
2012-08-19 18:15:37 -04:00
# include "serialiser/rstlvbase.h"
2012-06-26 15:52:01 -04:00
RsGxsGrpMetaData : : RsGxsGrpMetaData ( )
{
2012-09-30 10:21:17 -04:00
clear ( ) ;
2012-06-26 15:52:01 -04:00
}
2015-12-17 00:08:08 -05:00
uint32_t RsGxsGrpMetaData : : serial_size ( uint32_t api_version )
2012-06-26 15:52:01 -04:00
{
uint32_t s = 8 ; // header size
2014-03-17 16:56:06 -04:00
s + = mGroupId . serial_size ( ) ;
s + = mOrigGrpId . serial_size ( ) ;
2012-06-26 15:52:01 -04:00
s + = GetTlvStringSize ( mGroupName ) ;
s + = 4 ;
s + = 4 ;
2014-03-17 16:56:06 -04:00
s + = mAuthorId . serial_size ( ) ;
2012-08-18 06:01:35 -04:00
s + = GetTlvStringSize ( mServiceString ) ;
2012-10-01 16:57:56 -04:00
s + = signSet . TlvSize ( ) ;
2012-06-26 15:52:01 -04:00
s + = keys . TlvSize ( ) ;
2015-10-27 22:41:29 -04:00
s + = 4 ; // for mCircleType
2014-03-17 16:56:06 -04:00
s + = mCircleId . serial_size ( ) ;
2015-10-27 22:41:29 -04:00
s + = 4 ; // mAuthenFlag
s + = mParentGrpId . serial_size ( ) ; // mParentGroupId
2015-12-17 00:08:08 -05:00
if ( api_version = = RS_GXS_GRP_META_DATA_VERSION_ID_0002 )
s + = 4 ; // mSignFlag
else if ( api_version ! = RS_GXS_GRP_META_DATA_VERSION_ID_0001 )
std : : cerr < < " (EE) wrong/unknown API version " < < api_version < < " requested in RsGxsGrpMetaData::serial_size() " < < std : : endl ;
2012-06-26 15:52:01 -04:00
return s ;
}
2012-06-28 17:22:55 -04:00
void RsGxsGrpMetaData : : clear ( ) {
mGroupId . clear ( ) ;
mOrigGrpId . clear ( ) ;
mGroupName . clear ( ) ;
mGroupFlags = 0 ;
2014-11-26 15:07:43 -05:00
mPublishTs = 0 ;
mSignFlags = 0 ;
mAuthorId . clear ( ) ;
2012-12-02 14:40:17 -05:00
mCircleId . clear ( ) ;
mCircleType = 0 ;
2014-11-26 15:07:43 -05:00
signSet . TlvClear ( ) ;
keys . TlvClear ( ) ;
mServiceString . clear ( ) ;
2013-06-04 17:00:43 -04:00
mAuthenFlags = 0 ;
2014-01-19 18:38:48 -05:00
mParentGrpId . clear ( ) ;
2014-11-26 15:07:43 -05:00
mSubscribeFlags = 0 ;
mPop = 0 ;
2014-12-16 01:54:15 -05:00
mVisibleMsgCount = 0 ;
2014-11-26 15:07:43 -05:00
mGroupStatus = 0 ;
mLastPost = 0 ;
2014-02-22 19:08:11 -05:00
mReputationCutOff = 0 ;
2014-11-26 15:07:43 -05:00
mGrpSize = 0 ;
2012-10-01 16:57:56 -04:00
2014-11-26 15:07:43 -05:00
mGroupStatus = 0 ;
mRecvTS = 0 ;
mOriginator . clear ( ) ;
mInternalCircle . clear ( ) ;
mHash . clear ( ) ;
2012-06-28 17:22:55 -04:00
}
2015-12-17 00:08:08 -05:00
bool RsGxsGrpMetaData : : serialise ( void * data , uint32_t & pktsize , uint32_t api_version )
2012-06-26 15:52:01 -04:00
{
2015-12-17 00:08:08 -05:00
uint32_t tlvsize = serial_size ( api_version ) ;
2012-06-26 15:52:01 -04:00
uint32_t offset = 0 ;
if ( pktsize < tlvsize )
return false ; /* not enough space */
pktsize = tlvsize ;
bool ok = true ;
2015-12-17 00:08:08 -05:00
ok & = setRsItemHeader ( data , tlvsize , api_version , tlvsize ) ;
2012-06-26 15:52:01 -04:00
# ifdef GXS_DEBUG
std : : cerr < < " RsGxsGrpMetaData serialise() " < < std : : endl ;
std : : cerr < < " RsGxsGrpMetaData serialise(): Header: " < < ok < < std : : endl ;
std : : cerr < < " RsGxsGrpMetaData serialise(): Size: " < < tlvsize < < std : : endl ;
# endif
/* skip header */
offset + = 8 ;
2014-03-17 16:56:06 -04:00
ok & = mGroupId . serialise ( data , tlvsize , offset ) ;
ok & = mOrigGrpId . serialise ( data , tlvsize , offset ) ;
ok & = mParentGrpId . serialise ( data , tlvsize , offset ) ;
2012-06-26 15:52:01 -04:00
ok & = SetTlvString ( data , tlvsize , & offset , 0 , mGroupName ) ;
ok & = setRawUInt32 ( data , tlvsize , & offset , mGroupFlags ) ;
ok & = setRawUInt32 ( data , tlvsize , & offset , mPublishTs ) ;
2012-12-02 14:40:17 -05:00
ok & = setRawUInt32 ( data , tlvsize , & offset , mCircleType ) ;
2015-10-27 20:15:50 -04:00
ok & = setRawUInt32 ( data , tlvsize , & offset , mAuthenFlags ) ;
2014-03-17 16:56:06 -04:00
ok & = mAuthorId . serialise ( data , tlvsize , offset ) ;
2012-08-18 06:01:35 -04:00
ok & = SetTlvString ( data , tlvsize , & offset , 0 , mServiceString ) ;
2014-03-17 16:56:06 -04:00
ok & = mCircleId . serialise ( data , tlvsize , offset ) ;
2013-06-04 17:00:43 -04:00
2012-10-01 16:57:56 -04:00
ok & = signSet . SetTlv ( data , tlvsize , & offset ) ;
2012-06-26 15:52:01 -04:00
ok & = keys . SetTlv ( data , tlvsize , & offset ) ;
2012-10-01 16:57:56 -04:00
2015-12-17 00:08:08 -05:00
if ( api_version = = RS_GXS_GRP_META_DATA_VERSION_ID_0002 )
ok & = setRawUInt32 ( data , tlvsize , & offset , mSignFlags ) ; // new in API v2. Was previously missing. Kept in the end for backward compatibility
2012-06-26 15:52:01 -04:00
return ok ;
}
bool RsGxsGrpMetaData : : deserialise ( void * data , uint32_t & pktsize )
{
uint32_t offset = 8 ; // skip the header
uint32_t rssize = getRsItemSize ( data ) ;
bool ok = true ;
2012-06-28 17:22:55 -04:00
ok & = rssize = = pktsize ;
2012-06-26 15:52:01 -04:00
if ( ! ok ) return false ;
2014-03-17 16:56:06 -04:00
ok & = mGroupId . deserialise ( data , pktsize , offset ) ;
ok & = mOrigGrpId . deserialise ( data , pktsize , offset ) ;
ok & = mParentGrpId . deserialise ( data , pktsize , offset ) ;
2012-06-28 17:22:55 -04:00
ok & = GetTlvString ( data , pktsize , & offset , 0 , mGroupName ) ;
2012-06-26 15:52:01 -04:00
ok & = getRawUInt32 ( data , pktsize , & offset , & mGroupFlags ) ;
ok & = getRawUInt32 ( data , pktsize , & offset , & mPublishTs ) ;
2012-12-02 14:40:17 -05:00
ok & = getRawUInt32 ( data , pktsize , & offset , & mCircleType ) ;
2015-10-27 20:15:50 -04:00
ok & = getRawUInt32 ( data , pktsize , & offset , & mAuthenFlags ) ;
2014-03-17 16:56:06 -04:00
ok & = mAuthorId . deserialise ( data , pktsize , offset ) ;
2012-08-18 06:01:35 -04:00
ok & = GetTlvString ( data , pktsize , & offset , 0 , mServiceString ) ;
2014-03-17 16:56:06 -04:00
ok & = mCircleId . deserialise ( data , pktsize , offset ) ;
2012-10-01 16:57:56 -04:00
ok & = signSet . GetTlv ( data , pktsize , & offset ) ;
2012-06-28 17:22:55 -04:00
ok & = keys . GetTlv ( data , pktsize , & offset ) ;
2015-10-27 20:15:50 -04:00
switch ( getRsItemId ( data ) )
{
case RS_GXS_GRP_META_DATA_VERSION_ID_0002 : ok & = getRawUInt32 ( data , pktsize , & offset , & mSignFlags ) ; // current API
break ;
case RS_GXS_GRP_META_DATA_VERSION_ID_0001 : mSignFlags = 0 ; // old API. Do not leave this uninitialised!
break ;
2012-10-01 16:57:56 -04:00
2015-10-27 20:15:50 -04:00
default :
std : : cerr < < " (EE) RsGxsGrpMetaData::deserialise(): ERROR: unknown API version " < < std : : hex < < getRsItemId ( data ) < < std : : dec < < std : : endl ;
}
if ( offset ! = pktsize )
{
std : : cerr < < " (EE) RsGxsGrpMetaData::deserialise(): ERROR: unmatched size " < < offset < < " , expected: " < < pktsize < < std : : dec < < std : : endl ;
return false ;
}
2015-04-09 15:55:56 -04:00
# ifdef DROP_NON_CANONICAL_ITEMS
if ( mGroupName . length ( ) > RsGxsGrpMetaData : : MAX_ALLOWED_STRING_SIZE )
{
std : : cerr < < " WARNING: Deserialised group with mGroupName.length() = " < < mGroupName . length ( ) < < " . This is not allowed. This item will be dropped. " < < std : : endl ;
return false ;
}
if ( mServiceString . length ( ) > RsGxsGrpMetaData : : MAX_ALLOWED_STRING_SIZE )
{
std : : cerr < < " WARNING: Deserialised group with mServiceString.length() = " < < mGroupName . length ( ) < < " . This is not allowed. This item will be dropped. " < < std : : endl ;
return false ;
}
# endif
2012-06-26 15:52:01 -04:00
return ok ;
}
2013-02-07 18:04:16 -05:00
int RsGxsMsgMetaData : : refcount = 0 ;
2012-06-26 15:52:01 -04:00
2012-06-28 17:22:55 -04:00
RsGxsMsgMetaData : : RsGxsMsgMetaData ( ) {
2013-02-07 18:04:16 -05:00
//std::cout << "\nrefcount++ : " << ++refcount << std::endl;
return ;
}
RsGxsMsgMetaData : : ~ RsGxsMsgMetaData ( ) {
//std::cout << "\nrefcount-- : " << --refcount << std::endl;
return ;
2012-06-28 17:22:55 -04:00
}
2012-06-26 15:52:01 -04:00
uint32_t RsGxsMsgMetaData : : serial_size ( )
{
uint32_t s = 8 ; // header size
2014-03-17 16:56:06 -04:00
s + = mGroupId . serial_size ( ) ;
s + = mMsgId . serial_size ( ) ;
s + = mThreadId . serial_size ( ) ;
s + = mParentId . serial_size ( ) ;
s + = mOrigMsgId . serial_size ( ) ;
s + = mAuthorId . serial_size ( ) ;
2012-06-26 15:52:01 -04:00
2012-10-01 16:57:56 -04:00
s + = signSet . TlvSize ( ) ;
2012-06-26 15:52:01 -04:00
s + = GetTlvStringSize ( mMsgName ) ;
2015-10-27 22:41:29 -04:00
s + = 4 ; // mPublishTS
s + = 4 ; // mMsgFlags
2012-06-26 15:52:01 -04:00
return s ;
}
2012-06-28 17:22:55 -04:00
void RsGxsMsgMetaData : : clear ( )
{
mGroupId . clear ( ) ;
mMsgId . clear ( ) ;
mThreadId . clear ( ) ;
mParentId . clear ( ) ;
mAuthorId . clear ( ) ;
mOrigMsgId . clear ( ) ;
mMsgName . clear ( ) ;
2012-08-18 06:01:35 -04:00
mServiceString . clear ( ) ;
2012-06-28 17:22:55 -04:00
2012-10-01 16:57:56 -04:00
signSet . TlvClear ( ) ;
2012-06-28 17:22:55 -04:00
mPublishTs = 0 ;
mMsgFlags = 0 ;
mMsgStatus = 0 ;
mChildTs = 0 ;
2013-11-23 18:39:55 -05:00
recvTS = 0 ;
2012-06-28 17:22:55 -04:00
}
2012-06-26 15:52:01 -04:00
bool RsGxsMsgMetaData : : serialise ( void * data , uint32_t * size )
{
uint32_t tlvsize = serial_size ( ) ;
uint32_t offset = 0 ;
if ( * size < tlvsize )
return false ; /* not enough space */
* size = tlvsize ;
bool ok = true ;
2015-12-17 00:08:08 -05:00
ok & = setRsItemHeader ( data , tlvsize , RS_GXS_MSG_META_DATA_VERSION_ID_0002 , tlvsize ) ;
2012-06-26 15:52:01 -04:00
# ifdef GXS_DEBUG
std : : cerr < < " RsGxsGrpMetaData serialise() " < < std : : endl ;
std : : cerr < < " RsGxsGrpMetaData serialise(): Header: " < < ok < < std : : endl ;
std : : cerr < < " RsGxsGrpMetaData serialise(): Size: " < < tlvsize < < std : : endl ;
# endif
/* skip header */
offset + = 8 ;
2014-03-17 16:56:06 -04:00
ok & = mGroupId . serialise ( data , * size , offset ) ;
ok & = mMsgId . serialise ( data , * size , offset ) ;
ok & = mThreadId . serialise ( data , * size , offset ) ;
ok & = mParentId . serialise ( data , * size , offset ) ;
ok & = mOrigMsgId . serialise ( data , * size , offset ) ;
ok & = mAuthorId . serialise ( data , * size , offset ) ;
2012-06-26 15:52:01 -04:00
2012-10-01 16:57:56 -04:00
ok & = signSet . SetTlv ( data , * size , & offset ) ;
2012-06-26 15:52:01 -04:00
ok & = SetTlvString ( data , * size , & offset , 0 , mMsgName ) ;
ok & = setRawUInt32 ( data , * size , & offset , mPublishTs ) ;
ok & = setRawUInt32 ( data , * size , & offset , mMsgFlags ) ;
return ok ;
}
bool RsGxsMsgMetaData : : deserialise ( void * data , uint32_t * size )
{
uint32_t offset = 8 ; // skip the header
uint32_t rssize = getRsItemSize ( data ) ;
bool ok = true ;
2012-06-28 17:22:55 -04:00
ok & = rssize = = * size ;
2012-06-26 15:52:01 -04:00
if ( ! ok ) return false ;
2014-03-17 16:56:06 -04:00
ok & = mGroupId . deserialise ( data , * size , offset ) ;
ok & = mMsgId . deserialise ( data , * size , offset ) ;
ok & = mThreadId . deserialise ( data , * size , offset ) ;
ok & = mParentId . deserialise ( data , * size , offset ) ;
ok & = mOrigMsgId . deserialise ( data , * size , offset ) ;
ok & = mAuthorId . deserialise ( data , * size , offset ) ;
2012-06-26 15:52:01 -04:00
2012-10-01 16:57:56 -04:00
ok & = signSet . GetTlv ( data , * size , & offset ) ;
2012-06-26 15:52:01 -04:00
ok & = GetTlvString ( data , * size , & offset , 0 , mMsgName ) ;
uint32_t t ;
ok & = getRawUInt32 ( data , * size , & offset , & t ) ;
mPublishTs = t ;
ok & = getRawUInt32 ( data , * size , & offset , & mMsgFlags ) ;
return ok ;
}
2012-07-26 17:48:54 -04:00
void RsGxsGrpMetaData : : operator = ( const RsGroupMetaData & rMeta )
{
2014-12-16 01:54:15 -05:00
this - > mAuthorId = rMeta . mAuthorId ;
this - > mGroupFlags = rMeta . mGroupFlags ;
this - > mGroupId = rMeta . mGroupId ;
this - > mGroupStatus = rMeta . mGroupStatus ;
this - > mLastPost = rMeta . mLastPost ;
this - > mVisibleMsgCount = rMeta . mVisibleMsgCount ;
this - > mPop = rMeta . mPop ;
this - > mPublishTs = rMeta . mPublishTs ;
this - > mSubscribeFlags = rMeta . mSubscribeFlags ;
this - > mGroupName = rMeta . mGroupName ;
this - > mServiceString = rMeta . mServiceString ;
2012-08-26 10:02:47 -04:00
this - > mSignFlags = rMeta . mSignFlags ;
2012-12-02 14:40:17 -05:00
this - > mCircleId = rMeta . mCircleId ;
this - > mCircleType = rMeta . mCircleType ;
this - > mInternalCircle = rMeta . mInternalCircle ;
this - > mOriginator = rMeta . mOriginator ;
2013-06-04 17:00:43 -04:00
this - > mAuthenFlags = rMeta . mAuthenFlags ;
2014-01-19 18:38:48 -05:00
//std::cout << "rMeta.mParentGrpId= " <<rMeta.mParentGrpId<<"\n";
this - > mParentGrpId = rMeta . mParentGrpId ;
2012-07-26 17:48:54 -04:00
}
void RsGxsMsgMetaData : : operator = ( const RsMsgMetaData & rMeta )
{
2014-12-16 01:54:15 -05:00
this - > mAuthorId = rMeta . mAuthorId ;
this - > mChildTs = rMeta . mChildTs ;
this - > mGroupId = rMeta . mGroupId ;
this - > mMsgFlags = rMeta . mMsgFlags ;
this - > mMsgId = rMeta . mMsgId ;
this - > mMsgName = rMeta . mMsgName ;
this - > mMsgStatus = rMeta . mMsgStatus ;
this - > mOrigMsgId = rMeta . mOrigMsgId ;
this - > mParentId = rMeta . mParentId ;
this - > mPublishTs = rMeta . mPublishTs ;
this - > mThreadId = rMeta . mThreadId ;
this - > mServiceString = rMeta . mServiceString ;
2012-08-19 18:15:37 -04:00
}
2012-07-26 17:48:54 -04:00