From 91291a992074de6302d44196461e89ac61dcc403 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 3 Jan 2014 22:41:20 +0000 Subject: [PATCH] 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 --- libretroshare/src/grouter/grouteritems.cc | 83 +++++++++++++++++++++++ libretroshare/src/grouter/grouteritems.h | 45 +++++++++--- libretroshare/src/grouter/p3grouter.cc | 22 +++++- libretroshare/src/grouter/p3grouter.h | 6 +- 4 files changed, 145 insertions(+), 11 deletions(-) diff --git a/libretroshare/src/grouter/grouteritems.cc b/libretroshare/src/grouter/grouteritems.cc index 565d0ef77..3174ac756 100644 --- a/libretroshare/src/grouter/grouteritems.cc +++ b/libretroshare/src/grouter/grouteritems.cc @@ -33,11 +33,13 @@ bool RsGRouterItem::serialise_header(void *data,uint32_t& pktsize,uint32_t& tlvs uint32_t RsGRouterPublishKeyItem::serial_size() const { uint32_t s = 8 ; // header + s += POW_PAYLOAD_SIZE ; // proof of work bytes s += 4 ; // diffusion_id s += 20 ; // sha1 for published_key s += 4 ; // service id s += 4 ; // randomized distance s += GetTlvStringSize(description_string) ; // description + s += PGP_KEY_FINGERPRINT_SIZE ; // fingerprint return s ; } @@ -49,12 +51,16 @@ bool RsGRouterPublishKeyItem::serialise(void *data, uint32_t& pktsize) const if(!serialise_header(data,pktsize,tlvsize,offset)) return false ; + memcpy(&((uint8_t*)data)[offset],pow_bytes,POW_PAYLOAD_SIZE) ; + offset += 8 ; + /* add mandatory parts first */ ok &= setRawUInt32(data, tlvsize, &offset, diffusion_id); ok &= setRawSha1(data, tlvsize, &offset, published_key); ok &= setRawUInt32(data, tlvsize, &offset, service_id); ok &= setRawUFloat32(data, tlvsize, &offset, randomized_distance); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, description_string); + ok &= setRawPGPFingerprint(data, tlvsize, &offset, fingerprint); if (offset != tlvsize) { @@ -65,6 +71,77 @@ bool RsGRouterPublishKeyItem::serialise(void *data, uint32_t& pktsize) const 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;kpow_bytes,RsGRouterProofOfWorkObject::POW_PAYLOAD_SIZE) ; + offset += 8 ; + ok &= getRawUInt32(data, pktsize, &offset, &item->diffusion_id); // file hash ok &= getRawSha1(data, pktsize, &offset, item->published_key); ok &= getRawUInt32(data, pktsize, &offset, &item->service_id); // 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 &= getRawPGPFingerprint(data,pktsize,&offset,&item->fingerprint) ; 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) { o << "GRouterPublishKeyItem:" << std::endl ; + o << " POW bytes : \""<< PGPIdType(pow_bytes).toStdString() << "\"" << std::endl ; o << " direct origin: \""<< PeerId() << "\"" << std::endl ; o << " Key: " << published_key.toStdString() << 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 << " Distance: " << randomized_distance << std::endl ; o << " Description: " << description_string << std::endl ; + o << " Fingerprint: " << fingerprint.toStdString() << std::endl ; return o ; } diff --git a/libretroshare/src/grouter/grouteritems.h b/libretroshare/src/grouter/grouteritems.h index 498cde6eb..461b54099 100644 --- a/libretroshare/src/grouter/grouteritems.h +++ b/libretroshare/src/grouter/grouteritems.h @@ -26,6 +26,7 @@ #pragma once #include "serialiser/rsserial.h" +#include "util/rsid.h" #include "rsgrouter.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; }; +/***********************************************************************************/ +/* 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 */ /***********************************************************************************/ -class RsGRouterPublishKeyItem: public RsGRouterItem +class RsGRouterPublishKeyItem: public RsGRouterItem, public RsGRouterProofOfWorkObject { public: 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 ; float randomized_distance ; 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 diff --git a/libretroshare/src/grouter/p3grouter.cc b/libretroshare/src/grouter/p3grouter.cc index 0c4479992..227718c9f 100644 --- a/libretroshare/src/grouter/p3grouter.cc +++ b/libretroshare/src/grouter/p3grouter.cc @@ -361,7 +361,6 @@ void p3GRouter::locked_forwardKey(const RsGRouterPublishKeyItem& item) else 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) { RsStackMutex mtx(grMtx) ; @@ -381,6 +380,27 @@ bool p3GRouter::registerKey(const GRouterKeyId& key,const GRouterServiceId& clie return true ; } +bool p3GRouter::unregisterKey(const GRouterKeyId& key) +{ + RsStackMutex mtx(grMtx) ; + + std::map::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() { diff --git a/libretroshare/src/grouter/p3grouter.h b/libretroshare/src/grouter/p3grouter.h index a52a3b2cd..92aa23593 100644 --- a/libretroshare/src/grouter/p3grouter.h +++ b/libretroshare/src/grouter/p3grouter.h @@ -62,7 +62,7 @@ class p3GRouter: public RsGRouter, public p3Service, public p3Config // 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 // 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. // 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 unregisterKey(const GRouterKeyId& key) ; //===================================================// // Client/server request services //