From 44d0cbe2959b52cbcd0be7ad98aba798d3c12f83 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 12 Apr 2019 21:29:49 +0200 Subject: [PATCH] finished fast track turtle items for distant chat --- libretroshare/src/gxstunnel/p3gxstunnel.cc | 51 +++++++++++++++++----- libretroshare/src/gxstunnel/p3gxstunnel.h | 11 ++++- retroshare.pri | 10 ++++- 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/libretroshare/src/gxstunnel/p3gxstunnel.cc b/libretroshare/src/gxstunnel/p3gxstunnel.cc index a66170270..d5e3b3a90 100644 --- a/libretroshare/src/gxstunnel/p3gxstunnel.cc +++ b/libretroshare/src/gxstunnel/p3gxstunnel.cc @@ -770,7 +770,11 @@ void p3GxsTunnelService::receiveTurtleData(const RsTurtleGenericTunnelItem *gite // This function encrypts the given data and adds a MAC and an IV into a serialised memory chunk that is then sent through the tunnel. +#ifndef V07_NON_BACKWARD_COMPATIBLE_CHANGE_004 bool p3GxsTunnelService::handleEncryptedData(const uint8_t *data_bytes,uint32_t data_size,const TurtleFileHash& hash,const RsPeerId& virtual_peer_id,bool accepts_fast_items) +#else +bool p3GxsTunnelService::handleEncryptedData(const uint8_t *data_bytes,uint32_t data_size,const TurtleFileHash& hash,const RsPeerId& virtual_peer_id) +#endif { #ifdef DEBUG_GXS_TUNNEL std::cerr << "p3GxsTunnelService::handleEncryptedDataItem()" << std::endl; @@ -813,9 +817,15 @@ bool p3GxsTunnelService::handleEncryptedData(const uint8_t *data_bytes,uint32_t std::cerr << "(EE) no tunnel data for tunnel ID=" << tunnel_id << ". This is a bug." << std::endl; return false ; } - +#ifndef V07_NON_BACKWARD_COMPATIBLE_CHANGE_004 if(accepts_fast_items) - it2->second.accepts_fast_turtle_items = accepts_fast_items; + { + if(!it2->second.accepts_fast_turtle_items) + std::cerr << "(II) received probe for Fast track turtle items for tunnel VPID " << it2->second.virtual_peer_id << ": switching to Fast items mode." << std::endl; + + it2->second.accepts_fast_turtle_items = true; + } +#endif memcpy(aes_key,it2->second.aes_key,GXS_TUNNEL_AES_KEY_SIZE) ; @@ -1342,27 +1352,48 @@ bool p3GxsTunnelService::locked_sendEncryptedTunnelData(RsGxsTunnelItem *item) // We send the item through the turtle tunnel. We do that using the new 'fast' item type if the tunnel accepts it, and using the old slow one otherwise. // Still if the tunnel hasn't been probed, we duplicate the packet using the new fast item format. The packet being received twice on the other side will // be discarded with a warning. + // Note that because the data is deleted by sendTurtleData(), we need to send all packets at the end, and properly duplicate the data when needed. + +#ifdef V07_NON_BACKWARD_COMPATIBLE_CHANGE_004 + RsTurtleGenericFastDataItem *gitem = new RsTurtleGenericFastDataItem; + gitem->data_bytes = data_bytes; + gitem->data_size = data_size ; +#else + RsTurtleGenericDataItem *gitem = NULL; + RsTurtleGenericFastDataItem *gitem2 = NULL; if(!it->second.accepts_fast_turtle_items) { - RsTurtleGenericDataItem *gitem = new RsTurtleGenericDataItem ; + std::cerr << "Sending old format (slow) item for packet IV=" << std::hex << IV << std::dec << " in tunnel VPID=" << it->second.virtual_peer_id << std::endl; + gitem = new RsTurtleGenericDataItem ; gitem->data_bytes = data_bytes; gitem->data_size = data_size ; - - mTurtle->sendTurtleData(virtual_peer_id,gitem) ; } if(it->second.accepts_fast_turtle_items || !it->second.already_probed_for_fast_items) { - RsTurtleGenericFastDataItem *gitem = new RsTurtleGenericFastDataItem ; -#warning we should duplicate the data here, otherwise it's gonna crash - gitem->data_bytes = data_bytes; - gitem->data_size = data_size ; + std::cerr << "Sending new format (fast) item for packet IV=" << std::hex << IV << std::dec << " in tunnel VPID=" << it->second.virtual_peer_id << std::endl; + gitem2 = new RsTurtleGenericFastDataItem ; + + if(gitem != NULL) // duplicate the data because it was already sent in gitem. + { + gitem2->data_bytes = rs_malloc(data_size); + gitem2->data_size = data_size ; + memcpy(gitem2->data_bytes,data_bytes,data_size); + } + else + { + gitem2->data_bytes = data_bytes; + gitem2->data_size = data_size ; + } it->second.already_probed_for_fast_items = true; - mTurtle->sendTurtleData(virtual_peer_id,gitem) ; + } + if(gitem2) mTurtle->sendTurtleData(virtual_peer_id,gitem2) ; +#endif + if(gitem) mTurtle->sendTurtleData(virtual_peer_id,gitem) ; return true ; } diff --git a/libretroshare/src/gxstunnel/p3gxstunnel.h b/libretroshare/src/gxstunnel/p3gxstunnel.h index 98b68f161..00bd6d65a 100644 --- a/libretroshare/src/gxstunnel/p3gxstunnel.h +++ b/libretroshare/src/gxstunnel/p3gxstunnel.h @@ -147,7 +147,10 @@ private: class GxsTunnelPeerInfo { public: - GxsTunnelPeerInfo() : last_contact(0), last_keep_alive_sent(0), status(0), direction(0),accepts_fast_turtle_items(false) + GxsTunnelPeerInfo() : last_contact(0), last_keep_alive_sent(0), status(0), direction(0) +#ifndef V07_NON_BACKWARD_COMPATIBLE_CHANGE_004 + ,accepts_fast_turtle_items(false) +#endif { memset(aes_key, 0, GXS_TUNNEL_AES_KEY_SIZE); @@ -170,8 +173,10 @@ private: std::map received_data_prints ; // list of recently received messages, to avoid duplicates. Kept for 20 mins at most. uint32_t total_sent ; // total data sent to this peer uint32_t total_received ; // total data received by this peer +#ifndef V07_NON_BACKWARD_COMPATIBLE_CHANGE_004 bool accepts_fast_turtle_items; // does the tunnel accept RsTurtleGenericFastDataItem type? bool already_probed_for_fast_items; // has the tunnel been probed already? If not, a fast item will be sent +#endif }; class GxsTunnelDHInfo @@ -243,7 +248,11 @@ private: bool locked_sendEncryptedTunnelData(RsGxsTunnelItem *item) ; bool locked_sendClearTunnelData(RsGxsTunnelDHPublicKeyItem *item); // this limits the usage to DH items. Others should be encrypted! +#ifndef V07_NON_BACKWARD_COMPATIBLE_CHANGE_004 bool handleEncryptedData(const uint8_t *data_bytes, uint32_t data_size, const TurtleFileHash& hash, const RsPeerId& virtual_peer_id, bool accepts_fast_items) ; +#else + bool handleEncryptedData(const uint8_t *data_bytes, uint32_t data_size, const TurtleFileHash& hash, const RsPeerId& virtual_peer_id) ; +#endif // local data diff --git a/retroshare.pri b/retroshare.pri index 7f687d766..2f2dac230 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -230,11 +230,18 @@ isEmpty(RS_UPNP_LIB):RS_UPNP_LIB = upnp ixml threadutil # # V07_NON_BACKWARD_COMPATIBLE_CHANGE_003: # -# What: Do not hash PGP certificate twice when signing +# What: Do not hash PGP certificate twice when signing # # Why: hasing twice is not per se a security issue, but it makes it harder to change the settings for hashing. # # Backward compat: patched peers cannot connect to non patched peers older than Nov 2017. +# +# V07_NON_BACKWARD_COMPATIBLE_CHANGE_004: +# +# What: Do not probe that GXS tunnels accept fast items. Just assume they do. +# Why: Avoids sending probe packets +# BackwardCompat: old RS before Mai 2019 will not be able to distant chat. +# ########################################################################################################################################################### #CONFIG += rs_v07_changes @@ -242,6 +249,7 @@ rs_v07_changes { DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_001 DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_002 DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_003 + DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_004 DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_UNNAMED }