2018-05-22 16:03:11 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/grouter: grouteritems.h *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2013 by Cyril Soler <csoler@users.sourceforge.net> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
2018-05-28 16:03:39 -04:00
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
2018-05-22 16:03:11 -04:00
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 *
|
2018-05-28 16:03:39 -04:00
|
|
|
* GNU Lesser General Public License for more details. *
|
2018-05-22 16:03:11 -04:00
|
|
|
* *
|
2018-05-28 16:03:39 -04:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
2018-05-22 16:03:11 -04:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2013-11-02 10:35:33 -04:00
|
|
|
#pragma once
|
|
|
|
|
2016-01-12 21:10:11 -05:00
|
|
|
#include "util/rsmemory.h"
|
|
|
|
|
2013-11-02 10:35:33 -04:00
|
|
|
#include "serialiser/rsserial.h"
|
2015-01-11 17:18:28 -05:00
|
|
|
#include "serialiser/rstlvkeys.h"
|
2017-04-18 15:11:37 -04:00
|
|
|
#include "rsitems/rsserviceids.h"
|
2014-03-17 16:56:06 -04:00
|
|
|
#include "retroshare/rstypes.h"
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2014-03-29 10:18:05 -04:00
|
|
|
#include "retroshare/rsgrouter.h"
|
2015-04-09 17:34:50 -04:00
|
|
|
#include "groutermatrix.h"
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2015-10-25 18:07:17 -04:00
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_PUBLISH_KEY = 0x01 ; // used to publish a key
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_ACK_deprecated = 0x03 ; // don't use!
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT_deprecated = 0x04 ; // don't use!
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA_deprecated = 0x05 ; // don't use!
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA_deprecated2 = 0x06 ; // don't use!
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_DATA = 0x07 ; // used to send data to a destination (Signed by source)
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT = 0x08 ; // long-distance acknowledgement of data received
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK = 0x10 ; // chunk of data. Used internally.
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN = 0x11 ; // acknowledge for finished transaction. Not necessary, but increases fiability.
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES = 0x80 ; // item to save matrix clues
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST = 0x82 ; // item to save friend lists
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO = 0x93 ; //
|
|
|
|
const uint8_t RS_PKT_SUBTYPE_GROUTER_MATRIX_TRACK = 0x94 ; // item to save matrix track info
|
2013-12-27 15:06:47 -05:00
|
|
|
|
2015-04-12 17:43:52 -04:00
|
|
|
const uint8_t QOS_PRIORITY_RS_GROUTER = 4 ; // relevant for items that travel through friends
|
2013-11-02 10:35:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************************/
|
|
|
|
/* Basic GRouter Item Class */
|
|
|
|
/***********************************************************************************/
|
|
|
|
|
|
|
|
class RsGRouterItem: public RsItem
|
|
|
|
{
|
|
|
|
public:
|
2017-07-21 08:01:20 -04:00
|
|
|
explicit RsGRouterItem(uint8_t grouter_subtype) : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_GROUTER,grouter_subtype) {}
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2015-04-09 17:34:50 -04:00
|
|
|
virtual ~RsGRouterItem() {}
|
|
|
|
|
2013-11-02 10:35:33 -04:00
|
|
|
virtual void clear() = 0 ;
|
|
|
|
};
|
|
|
|
|
2014-01-03 17:41:20 -05:00
|
|
|
/***********************************************************************************/
|
|
|
|
/* Helper base classes */
|
|
|
|
/***********************************************************************************/
|
|
|
|
|
|
|
|
class RsGRouterNonCopyableObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RsGRouterNonCopyableObject() {}
|
2015-01-22 09:33:19 -05:00
|
|
|
protected:
|
2014-01-03 17:41:20 -05:00
|
|
|
RsGRouterNonCopyableObject(const RsGRouterNonCopyableObject&) {}
|
|
|
|
RsGRouterNonCopyableObject operator=(const RsGRouterNonCopyableObject&) { return *this ;}
|
|
|
|
};
|
|
|
|
|
2013-11-02 10:35:33 -04:00
|
|
|
/***********************************************************************************/
|
|
|
|
/* Specific packets */
|
|
|
|
/***********************************************************************************/
|
|
|
|
|
2015-01-20 08:04:10 -05:00
|
|
|
// This abstract item class encapsulates 2 types of signed items. All have signature, destination key
|
|
|
|
// and routing ID. Sub-items are responsible for providing the serialised data to be signed for
|
|
|
|
// both signing and checking.
|
|
|
|
|
2015-01-19 08:53:36 -05:00
|
|
|
class RsGRouterAbstractMsgItem: public RsGRouterItem
|
2013-11-02 10:35:33 -04:00
|
|
|
{
|
2015-01-20 08:04:10 -05:00
|
|
|
public:
|
2017-07-21 08:01:20 -04:00
|
|
|
explicit RsGRouterAbstractMsgItem(uint8_t pkt_subtype) : RsGRouterItem(pkt_subtype), flags(0) {}
|
2015-04-09 17:34:50 -04:00
|
|
|
virtual ~RsGRouterAbstractMsgItem() {}
|
2015-01-19 08:53:36 -05:00
|
|
|
|
|
|
|
GRouterMsgPropagationId routing_id ;
|
2015-01-20 08:04:10 -05:00
|
|
|
GRouterKeyId destination_key ;
|
2015-04-09 17:34:50 -04:00
|
|
|
GRouterServiceId service_id ;
|
2015-01-20 08:04:10 -05:00
|
|
|
RsTlvKeySignature signature ; // signs mid+destination_key+state
|
2017-04-25 15:21:46 -04:00
|
|
|
uint32_t flags ; // packet was delivered, not delivered, bounced, etc
|
2015-01-19 08:53:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class RsGRouterGenericDataItem: public RsGRouterAbstractMsgItem, public RsGRouterNonCopyableObject
|
|
|
|
{
|
|
|
|
public:
|
2017-07-21 08:01:20 -04:00
|
|
|
RsGRouterGenericDataItem() : RsGRouterAbstractMsgItem(RS_PKT_SUBTYPE_GROUTER_DATA), data_size(0), data_bytes(NULL), duplication_factor(0) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
|
2015-01-11 17:18:28 -05:00
|
|
|
virtual ~RsGRouterGenericDataItem() { clear() ; }
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2015-01-11 17:18:28 -05:00
|
|
|
virtual void clear()
|
|
|
|
{
|
|
|
|
free(data_bytes);
|
|
|
|
data_bytes=NULL;
|
|
|
|
}
|
2017-04-25 15:21:46 -04:00
|
|
|
|
2017-04-26 05:40:46 -04:00
|
|
|
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2015-01-11 17:18:28 -05:00
|
|
|
RsGRouterGenericDataItem *duplicate() const ;
|
2013-12-22 12:04:13 -05:00
|
|
|
|
2015-01-11 17:18:28 -05:00
|
|
|
// packet data
|
|
|
|
//
|
|
|
|
uint32_t data_size ;
|
|
|
|
uint8_t *data_bytes;
|
2016-01-30 20:27:56 -05:00
|
|
|
uint32_t duplication_factor ; // number of duplicates allowed. Should be capped at each de-serialise operation!
|
2013-11-02 10:35:33 -04:00
|
|
|
};
|
|
|
|
|
2015-01-19 08:53:36 -05:00
|
|
|
class RsGRouterSignedReceiptItem: public RsGRouterAbstractMsgItem
|
2013-11-02 10:35:33 -04:00
|
|
|
{
|
2015-01-11 17:18:28 -05:00
|
|
|
public:
|
2015-01-19 08:53:36 -05:00
|
|
|
RsGRouterSignedReceiptItem() : RsGRouterAbstractMsgItem(RS_PKT_SUBTYPE_GROUTER_SIGNED_RECEIPT) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual ~RsGRouterSignedReceiptItem() {}
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2015-01-11 17:18:28 -05:00
|
|
|
virtual void clear() {}
|
2017-04-26 05:40:46 -04:00
|
|
|
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
|
2013-11-02 10:35:33 -04:00
|
|
|
|
2015-01-22 09:33:19 -05:00
|
|
|
RsGRouterSignedReceiptItem *duplicate() const ;
|
|
|
|
|
2015-01-11 17:18:28 -05:00
|
|
|
// packet data
|
|
|
|
//
|
2017-04-25 15:21:46 -04:00
|
|
|
Sha1CheckSum data_hash ; // avoids an attacker to re-use a given signed receipt. This is the hash of the enceypted data.
|
2013-11-02 10:35:33 -04:00
|
|
|
};
|
|
|
|
|
2015-01-15 15:33:24 -05:00
|
|
|
// Low-level data items
|
|
|
|
|
2015-04-09 17:34:50 -04:00
|
|
|
class RsGRouterTransactionItem: public RsGRouterItem
|
2015-01-15 15:33:24 -05:00
|
|
|
{
|
|
|
|
public:
|
2017-07-21 08:01:20 -04:00
|
|
|
explicit RsGRouterTransactionItem(uint8_t pkt_subtype) : RsGRouterItem(pkt_subtype) {}
|
2015-04-09 17:34:50 -04:00
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual ~RsGRouterTransactionItem() {}
|
2015-04-09 17:34:50 -04:00
|
|
|
|
|
|
|
virtual void clear() =0;
|
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual RsGRouterTransactionItem *duplicate() const = 0 ;
|
2015-04-09 17:34:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class RsGRouterTransactionChunkItem: public RsGRouterTransactionItem, public RsGRouterNonCopyableObject
|
|
|
|
{
|
2017-04-25 15:21:46 -04:00
|
|
|
public:
|
2017-07-21 08:01:20 -04:00
|
|
|
RsGRouterTransactionChunkItem() : RsGRouterTransactionItem(RS_PKT_SUBTYPE_GROUTER_TRANSACTION_CHUNK), chunk_start(0), chunk_size(0), total_size(0), chunk_data(NULL) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
|
2015-04-09 17:34:50 -04:00
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual ~RsGRouterTransactionChunkItem() { free(chunk_data) ; }
|
2015-01-15 15:33:24 -05:00
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual void clear() {}
|
2017-04-26 05:40:46 -04:00
|
|
|
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
|
2015-01-15 15:33:24 -05:00
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual RsGRouterTransactionItem *duplicate() const
|
|
|
|
{
|
|
|
|
RsGRouterTransactionChunkItem *item = new RsGRouterTransactionChunkItem ;
|
|
|
|
*item = *this ; // copy all fields
|
|
|
|
item->chunk_data = (uint8_t*)rs_malloc(chunk_size) ; // deep copy memory chunk
|
|
|
|
|
|
|
|
if(item->chunk_data == NULL)
|
|
|
|
return NULL ;
|
|
|
|
|
|
|
|
memcpy(item->chunk_data,chunk_data,chunk_size) ;
|
|
|
|
return item ;
|
|
|
|
}
|
|
|
|
|
|
|
|
GRouterMsgPropagationId propagation_id ;
|
|
|
|
uint32_t chunk_start ;
|
|
|
|
uint32_t chunk_size ;
|
|
|
|
uint32_t total_size ;
|
|
|
|
uint8_t *chunk_data ;
|
2015-01-15 15:33:24 -05:00
|
|
|
};
|
2015-04-09 17:34:50 -04:00
|
|
|
class RsGRouterTransactionAcknItem: public RsGRouterTransactionItem
|
2015-01-19 08:53:36 -05:00
|
|
|
{
|
2017-04-25 15:21:46 -04:00
|
|
|
public:
|
|
|
|
RsGRouterTransactionAcknItem() : RsGRouterTransactionItem(RS_PKT_SUBTYPE_GROUTER_TRANSACTION_ACKN) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER) ; }
|
|
|
|
virtual ~RsGRouterTransactionAcknItem() {}
|
2015-01-19 08:53:36 -05:00
|
|
|
|
2017-04-26 05:40:46 -04:00
|
|
|
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
|
2015-01-19 08:53:36 -05:00
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual void clear() {}
|
2015-01-19 08:53:36 -05:00
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual RsGRouterTransactionItem *duplicate() const { return new RsGRouterTransactionAcknItem(*this) ; }
|
2015-04-09 17:34:50 -04:00
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
GRouterMsgPropagationId propagation_id ;
|
2015-01-19 08:53:36 -05:00
|
|
|
};
|
2015-01-15 15:33:24 -05:00
|
|
|
|
2013-12-27 15:06:47 -05:00
|
|
|
// Items for saving the routing matrix information.
|
|
|
|
|
|
|
|
class RsGRouterMatrixCluesItem: public RsGRouterItem
|
|
|
|
{
|
|
|
|
public:
|
2015-04-09 17:34:50 -04:00
|
|
|
RsGRouterMatrixCluesItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_MATRIX_CLUES)
|
2013-12-27 15:06:47 -05:00
|
|
|
{ setPriorityLevel(0) ; } // this item is never sent through the network
|
|
|
|
|
|
|
|
virtual void clear() {}
|
2017-04-26 05:40:46 -04:00
|
|
|
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
|
2013-12-27 15:06:47 -05:00
|
|
|
|
|
|
|
// packet data
|
|
|
|
//
|
|
|
|
GRouterKeyId destination_key ;
|
|
|
|
std::list<RoutingMatrixHitEntry> clues ;
|
|
|
|
};
|
|
|
|
|
2015-10-25 18:07:17 -04:00
|
|
|
class RsGRouterMatrixTrackItem: public RsGRouterItem
|
|
|
|
{
|
|
|
|
public:
|
2017-07-21 08:01:20 -04:00
|
|
|
RsGRouterMatrixTrackItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_MATRIX_TRACK), time_stamp(0)
|
2015-10-25 18:07:17 -04:00
|
|
|
{ setPriorityLevel(0) ; } // this item is never sent through the network
|
|
|
|
|
2017-04-26 05:40:46 -04:00
|
|
|
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
|
2015-10-25 18:07:17 -04:00
|
|
|
|
|
|
|
virtual void clear() {}
|
|
|
|
|
|
|
|
// packet data
|
|
|
|
//
|
|
|
|
RsGxsMessageId message_id ;
|
2017-04-25 15:21:46 -04:00
|
|
|
RsPeerId provider_id ;
|
2018-10-06 19:34:05 -04:00
|
|
|
rstime_t time_stamp ;
|
2015-10-25 18:07:17 -04:00
|
|
|
};
|
2013-12-27 15:06:47 -05:00
|
|
|
class RsGRouterMatrixFriendListItem: public RsGRouterItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RsGRouterMatrixFriendListItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_FRIENDS_LIST)
|
2015-01-11 17:18:28 -05:00
|
|
|
{ setPriorityLevel(0) ; } // this item is never sent through the network
|
2013-12-27 15:06:47 -05:00
|
|
|
|
2017-04-26 05:40:46 -04:00
|
|
|
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual void clear() {}
|
2013-12-27 15:06:47 -05:00
|
|
|
|
|
|
|
// packet data
|
|
|
|
//
|
2014-03-17 16:56:06 -04:00
|
|
|
std::vector<RsPeerId> reverse_friend_indices ;
|
2013-12-27 15:06:47 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class RsGRouterRoutingInfoItem: public RsGRouterItem, public GRouterRoutingInfo, public RsGRouterNonCopyableObject
|
|
|
|
{
|
|
|
|
public:
|
2015-04-15 16:48:30 -04:00
|
|
|
RsGRouterRoutingInfoItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_ROUTING_INFO)
|
2013-12-27 15:06:47 -05:00
|
|
|
{ setPriorityLevel(0) ; } // this item is never sent through the network
|
|
|
|
|
|
|
|
virtual ~RsGRouterRoutingInfoItem() { clear() ; }
|
|
|
|
|
2017-04-26 05:40:46 -04:00
|
|
|
virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx);
|
2013-12-27 15:06:47 -05:00
|
|
|
|
|
|
|
virtual void clear()
|
|
|
|
{
|
2015-01-22 09:33:19 -05:00
|
|
|
if(data_item != NULL) delete data_item ;
|
|
|
|
if(receipt_item != NULL) delete receipt_item ;
|
|
|
|
|
|
|
|
data_item = NULL ;
|
|
|
|
receipt_item = NULL ;
|
|
|
|
}
|
2013-12-27 15:06:47 -05:00
|
|
|
};
|
|
|
|
|
2013-11-02 10:35:33 -04:00
|
|
|
/***********************************************************************************/
|
|
|
|
/* Serialisation */
|
|
|
|
/***********************************************************************************/
|
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
class RsGRouterSerialiser: public RsServiceSerializer
|
2013-11-02 10:35:33 -04:00
|
|
|
{
|
2015-01-19 08:53:36 -05:00
|
|
|
public:
|
2017-07-21 08:01:20 -04:00
|
|
|
explicit RsGRouterSerialiser(SerializationFlags flags = SERIALIZATION_FLAG_NONE) : RsServiceSerializer(RS_SERVICE_TYPE_GROUTER,RsGenericSerializer::FORMAT_BINARY,flags) {}
|
2015-01-19 08:53:36 -05:00
|
|
|
|
2017-04-25 15:21:46 -04:00
|
|
|
virtual RsItem *create_item(uint16_t service,uint8_t subtype) const ;
|
2013-11-02 10:35:33 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|