added proof of work algorithms to GRouter key items

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6984 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2014-01-03 22:41:20 +00:00
parent 469da4914f
commit 91291a9920
4 changed files with 145 additions and 11 deletions

View File

@ -33,11 +33,13 @@ bool RsGRouterItem::serialise_header(void *data,uint32_t& pktsize,uint32_t& tlvs
uint32_t RsGRouterPublishKeyItem::serial_size() const uint32_t RsGRouterPublishKeyItem::serial_size() const
{ {
uint32_t s = 8 ; // header uint32_t s = 8 ; // header
s += POW_PAYLOAD_SIZE ; // proof of work bytes
s += 4 ; // diffusion_id s += 4 ; // diffusion_id
s += 20 ; // sha1 for published_key s += 20 ; // sha1 for published_key
s += 4 ; // service id s += 4 ; // service id
s += 4 ; // randomized distance s += 4 ; // randomized distance
s += GetTlvStringSize(description_string) ; // description s += GetTlvStringSize(description_string) ; // description
s += PGP_KEY_FINGERPRINT_SIZE ; // fingerprint
return s ; return s ;
} }
@ -49,12 +51,16 @@ bool RsGRouterPublishKeyItem::serialise(void *data, uint32_t& pktsize) const
if(!serialise_header(data,pktsize,tlvsize,offset)) if(!serialise_header(data,pktsize,tlvsize,offset))
return false ; return false ;
memcpy(&((uint8_t*)data)[offset],pow_bytes,POW_PAYLOAD_SIZE) ;
offset += 8 ;
/* add mandatory parts first */ /* add mandatory parts first */
ok &= setRawUInt32(data, tlvsize, &offset, diffusion_id); ok &= setRawUInt32(data, tlvsize, &offset, diffusion_id);
ok &= setRawSha1(data, tlvsize, &offset, published_key); ok &= setRawSha1(data, tlvsize, &offset, published_key);
ok &= setRawUInt32(data, tlvsize, &offset, service_id); ok &= setRawUInt32(data, tlvsize, &offset, service_id);
ok &= setRawUFloat32(data, tlvsize, &offset, randomized_distance); ok &= setRawUFloat32(data, tlvsize, &offset, randomized_distance);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, description_string); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, description_string);
ok &= setRawPGPFingerprint(data, tlvsize, &offset, fingerprint);
if (offset != tlvsize) if (offset != tlvsize)
{ {
@ -65,6 +71,77 @@ bool RsGRouterPublishKeyItem::serialise(void *data, uint32_t& pktsize) const
return ok; return ok;
} }
/**********************************************************************************************/
/* PROOF OF WORK STUFF */
/**********************************************************************************************/
bool RsGRouterProofOfWorkObject::checkProofOfWork()
{
uint32_t size = serial_size() ;
unsigned char *mem = (unsigned char *)malloc(size) ;
if(mem == NULL)
{
std::cerr << "RsGRouterProofOfWorkObject: cannot allocate memory for " << size << " bytes." << std::endl;
return false ;
}
serialise(mem,size) ;
bool res = checkProofOfWork(mem,size) ;
free(mem) ;
return res ;
}
bool RsGRouterProofOfWorkObject::updateProofOfWork()
{
uint32_t size = serial_size() ;
unsigned char *mem = (unsigned char *)malloc(size) ;
if(mem == NULL)
{
std::cerr << "RsGRouterProofOfWorkObject: cannot allocate memory for " << size << " bytes." << std::endl;
return false ;
}
serialise(mem,size) ;
memset(mem,0,POW_PAYLOAD_SIZE) ; // init the payload
while(true)
{
if(checkProofOfWork(mem,size))
break ;
int k ;
for(k=0;k<POW_PAYLOAD_SIZE;++k)
{
++mem[k] ;
if(mem[k]!=0)
break ;
}
if(k == POW_PAYLOAD_SIZE)
return false ;
}
memcpy(pow_bytes,mem,POW_PAYLOAD_SIZE) ; // copy the good bytes.
free(mem) ;
return true ;
}
bool RsGRouterProofOfWorkObject::checkProofOfWork(unsigned char *mem,uint32_t size)
{
Sha1CheckSum sum = RsDirUtil::sha1sum(mem,size) ;
for(uint32_t i=0;i<(7+PROOF_OF_WORK_REQUESTED_BYTES)/4;++i)
for(int j=0;j<(PROOF_OF_WORK_REQUESTED_BYTES%4);++j)
if(sum.fourbytes[i] & (0xff << (8*(3-j))) != 0)
return false ;
return true ;
}
/**********************************************************************************************/ /**********************************************************************************************/
/* SERIALISER STUFF */ /* SERIALISER STUFF */
/**********************************************************************************************/ /**********************************************************************************************/
@ -102,11 +179,15 @@ RsGRouterItem *RsGRouterSerialiser::deserialise_RsGRouterPublishKeyItem(void *da
RsGRouterPublishKeyItem *item = new RsGRouterPublishKeyItem() ; RsGRouterPublishKeyItem *item = new RsGRouterPublishKeyItem() ;
memcpy(&((uint8_t*)data)[offset],item->pow_bytes,RsGRouterProofOfWorkObject::POW_PAYLOAD_SIZE) ;
offset += 8 ;
ok &= getRawUInt32(data, pktsize, &offset, &item->diffusion_id); // file hash ok &= getRawUInt32(data, pktsize, &offset, &item->diffusion_id); // file hash
ok &= getRawSha1(data, pktsize, &offset, item->published_key); ok &= getRawSha1(data, pktsize, &offset, item->published_key);
ok &= getRawUInt32(data, pktsize, &offset, &item->service_id); // file hash ok &= getRawUInt32(data, pktsize, &offset, &item->service_id); // file hash
ok &= getRawUFloat32(data, pktsize, &offset, item->randomized_distance); // file hash ok &= getRawUFloat32(data, pktsize, &offset, item->randomized_distance); // file hash
ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE,item->description_string); ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE,item->description_string);
ok &= getRawPGPFingerprint(data,pktsize,&offset,&item->fingerprint) ;
if (offset != rssize || !ok) if (offset != rssize || !ok)
{ {
@ -477,12 +558,14 @@ bool RsGRouterRoutingInfoItem::serialise(void *data,uint32_t& size) const
std::ostream& RsGRouterPublishKeyItem::print(std::ostream& o, uint16_t) std::ostream& RsGRouterPublishKeyItem::print(std::ostream& o, uint16_t)
{ {
o << "GRouterPublishKeyItem:" << std::endl ; o << "GRouterPublishKeyItem:" << std::endl ;
o << " POW bytes : \""<< PGPIdType(pow_bytes).toStdString() << "\"" << std::endl ;
o << " direct origin: \""<< PeerId() << "\"" << std::endl ; o << " direct origin: \""<< PeerId() << "\"" << std::endl ;
o << " Key: " << published_key.toStdString() << std::endl ; o << " Key: " << published_key.toStdString() << std::endl ;
o << " Req. Id: " << std::hex << diffusion_id << std::dec << std::endl ; o << " Req. Id: " << std::hex << diffusion_id << std::dec << std::endl ;
o << " Srv. Id: " << std::hex << service_id << std::dec << std::endl ; o << " Srv. Id: " << std::hex << service_id << std::dec << std::endl ;
o << " Distance: " << randomized_distance << std::endl ; o << " Distance: " << randomized_distance << std::endl ;
o << " Description: " << description_string << std::endl ; o << " Description: " << description_string << std::endl ;
o << " Fingerprint: " << fingerprint.toStdString() << std::endl ;
return o ; return o ;
} }

View File

@ -26,6 +26,7 @@
#pragma once #pragma once
#include "serialiser/rsserial.h" #include "serialiser/rsserial.h"
#include "util/rsid.h"
#include "rsgrouter.h" #include "rsgrouter.h"
#include "p3grouter.h" #include "p3grouter.h"
@ -69,11 +70,44 @@ class RsGRouterItem: public RsItem
bool serialise_header(void *data, uint32_t& pktsize, uint32_t& tlvsize, uint32_t& offset) const; bool serialise_header(void *data, uint32_t& pktsize, uint32_t& tlvsize, uint32_t& offset) const;
}; };
/***********************************************************************************/
/* Helper base classes */
/***********************************************************************************/
class RsGRouterNonCopyableObject
{
public:
RsGRouterNonCopyableObject() {}
private:
RsGRouterNonCopyableObject(const RsGRouterNonCopyableObject&) {}
RsGRouterNonCopyableObject operator=(const RsGRouterNonCopyableObject&) { return *this ;}
};
class RsGRouterProofOfWorkObject
{
public:
RsGRouterProofOfWorkObject() {}
virtual bool serialise(void *data,uint32_t& size) const =0;
virtual uint32_t serial_size() const =0;
virtual bool checkProofOfWork() ; // checks that the serialized object hashes down to a hash beginning with LEADING_BYTES_SIZE zeroes
virtual bool updateProofOfWork() ; // computes the pow_bytes so that the hash starts with LEADING_BYTES_SIZE zeroes.
static bool checkProofOfWork(unsigned char *mem,uint32_t size) ;
static const int POW_PAYLOAD_SIZE = 8 ;
static const int PROOF_OF_WORK_REQUESTED_BYTES = 4 ;
unsigned char pow_bytes[POW_PAYLOAD_SIZE] ; // 8 bytes to put at the beginning of the serialized packet, so that
// the hash starts with a fixed number of zeroes.
};
/***********************************************************************************/ /***********************************************************************************/
/* Specific packets */ /* Specific packets */
/***********************************************************************************/ /***********************************************************************************/
class RsGRouterPublishKeyItem: public RsGRouterItem class RsGRouterPublishKeyItem: public RsGRouterItem, public RsGRouterProofOfWorkObject
{ {
public: public:
RsGRouterPublishKeyItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_PUBLISH_KEY) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER_PUBLISH_KEY) ; } RsGRouterPublishKeyItem() : RsGRouterItem(RS_PKT_SUBTYPE_GROUTER_PUBLISH_KEY) { setPriorityLevel(QOS_PRIORITY_RS_GROUTER_PUBLISH_KEY) ; }
@ -91,15 +125,8 @@ class RsGRouterPublishKeyItem: public RsGRouterItem
uint32_t service_id ; uint32_t service_id ;
float randomized_distance ; float randomized_distance ;
std::string description_string ; std::string description_string ;
}; PGPFingerprintType fingerprint ;
class RsGRouterNonCopyableObject
{
public:
RsGRouterNonCopyableObject() {}
private:
RsGRouterNonCopyableObject(const RsGRouterNonCopyableObject&) {}
RsGRouterNonCopyableObject operator=(const RsGRouterNonCopyableObject&) { return *this ;}
}; };
class RsGRouterGenericDataItem: public RsGRouterItem, public RsGRouterNonCopyableObject class RsGRouterGenericDataItem: public RsGRouterItem, public RsGRouterNonCopyableObject

View File

@ -361,7 +361,6 @@ void p3GRouter::locked_forwardKey(const RsGRouterPublishKeyItem& item)
else else
std::cerr << " Not forwarding to source id " << item.PeerId() << std::endl; std::cerr << " Not forwarding to source id " << item.PeerId() << std::endl;
} }
bool p3GRouter::registerKey(const GRouterKeyId& key,const GRouterServiceId& client_id,const std::string& description) bool p3GRouter::registerKey(const GRouterKeyId& key,const GRouterServiceId& client_id,const std::string& description)
{ {
RsStackMutex mtx(grMtx) ; RsStackMutex mtx(grMtx) ;
@ -381,6 +380,27 @@ bool p3GRouter::registerKey(const GRouterKeyId& key,const GRouterServiceId& clie
return true ; return true ;
} }
bool p3GRouter::unregisterKey(const GRouterKeyId& key)
{
RsStackMutex mtx(grMtx) ;
std::map<GRouterKeyId,GRouterPublishedKeyInfo>::iterator it = _owned_key_ids.find(key) ;
if(it == _owned_key_ids.end())
{
std::cerr << "p3GRouter::unregisterKey(): key " << key.toStdString() << " not found." << std::endl;
return false ;
}
std::cerr << "p3GRouter::unregistered the following key: " << std::endl;
std::cerr << " Key id : " << key.toStdString() << std::endl;
std::cerr << " Client id : " << std::hex << it->second.service_id << std::dec << std::endl;
std::cerr << " Description : " << it->second.description_string << std::endl;
_owned_key_ids.erase(it) ;
return true ;
}
void p3GRouter::handleIncoming() void p3GRouter::handleIncoming()
{ {

View File

@ -62,7 +62,7 @@ class p3GRouter: public RsGRouter, public p3Service, public p3Config
// //
bool registerClientService(const GRouterServiceId& id,GRouterClientService *service) ; bool registerClientService(const GRouterServiceId& id,GRouterClientService *service) ;
// Use this method to register a new key that the global router will // Use this method to register/unregister a key that the global router will
// forward in the network, so that is can be a possible destination for // forward in the network, so that is can be a possible destination for
// global messages. // global messages.
// //
@ -70,7 +70,11 @@ class p3GRouter: public RsGRouter, public p3Service, public p3Config
// client_id: id of the client service to send the traffic to. // 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. // To obtain a client id, the service must register using the previous method.
// //
// Unregistering a key might not have an instantaneous effect, so the client is responsible for
// discarding traffic that might later come for this key.
//
bool registerKey(const GRouterKeyId& key,const GRouterServiceId& client_id,const std::string& description_string) ; bool registerKey(const GRouterKeyId& key,const GRouterServiceId& client_id,const std::string& description_string) ;
bool unregisterKey(const GRouterKeyId& key) ;
//===================================================// //===================================================//
// Client/server request services // // Client/server request services //