diff --git a/src/services/broadcastdiscoveryservice.cc b/src/services/broadcastdiscoveryservice.cc index 60869acb0..380cf8e5d 100644 --- a/src/services/broadcastdiscoveryservice.cc +++ b/src/services/broadcastdiscoveryservice.cc @@ -1,8 +1,8 @@ /******************************************************************************* * RetroShare Broadcast Domain Discovery * * * - * Copyright (C) 2019-2022 Gioacchino Mazzurco * - * Copyright (C) 2019-2022 Asociación Civil Altermundi * + * Copyright (C) 2019-2021 Gioacchino Mazzurco * + * Copyright (C) 2019-2021 Asociación Civil Altermundi * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * @@ -25,7 +25,6 @@ #include #include #include -#include #include "services/broadcastdiscoveryservice.h" #include "retroshare/rspeers.h" @@ -68,46 +67,14 @@ struct BroadcastDiscoveryPack : RsSerializable return bdp; } - /** - * @param[out] ec Optional storage for eventual error code, - * meaningful only on failure, if a nullptr is passed ther error is treated - * as fatal downstream, otherwise it is bubbled up to be treated upstream - */ - static std::unique_ptr fromSerializedString( - const std::string& st, - rs_view_ptr ec ) + static BroadcastDiscoveryPack fromSerializedString(const std::string& st) { - if(st.empty()) - { - if(!ec) - { - RS_FATAL("Attempteted from empty string ", std::errc::no_message); - print_stacktrace(); - exit(static_cast(std::errc::no_message)); - } - - *ec = std::errc::no_message; - return nullptr; - } - RsGenericSerializer::SerializeContext ctx( reinterpret_cast(const_cast(st.data())), static_cast(st.size()) ); - - auto bdp = std::make_unique(); - bdp->serial_process(RsGenericSerializer::DESERIALIZE, ctx); - if(ctx.mOk) return bdp; - - if(!ec) - { - RS_FATAL( "Attempteted from invalid string ", - std::errc::invalid_argument ); - print_stacktrace(); - exit(static_cast(std::errc::invalid_argument)); - } - - *ec = std::errc::invalid_argument; - return nullptr; + BroadcastDiscoveryPack bdp; + bdp.serial_process(RsGenericSerializer::DESERIALIZE, ctx); + return bdp; } std::string serializeToString() @@ -161,11 +128,7 @@ BroadcastDiscoveryService::getDiscoveredPeers() RS_STACK_MUTEX(mDiscoveredDataMutex); for(auto&& pp: mDiscoveredData) - { - /* Results must be clean at this point so let downstrem treat errors as - * fatal if something dirty gets here */ - ret.push_back(*createResult(pp.first, pp.second)); - } + ret.push_back(createResult(pp.first, pp.second)); return ret; } @@ -190,18 +153,8 @@ void BroadcastDiscoveryService::threadTick() mDiscoveredDataMutex.lock(); for(auto&& dEndpoint: currentEndpoints) { - /* Getting something invalid here from network is possible so treat - * it gracefully */ - std::error_condition errC; - if(!createResult(dEndpoint.ip_port(), dEndpoint.user_data(), &errC)) - { - RS_INFO( "Discovered peer: ", - UDC::IpPortToString(dEndpoint.ip_port()), - " with invalid data discarding it ", errC); - continue; - } - currentMap[dEndpoint.ip_port()] = dEndpoint.user_data(); + auto findIt = mDiscoveredData.find(dEndpoint.ip_port()); if( !dEndpoint.user_data().empty() && ( findIt == mDiscoveredData.end() || @@ -215,25 +168,29 @@ void BroadcastDiscoveryService::threadTick() { for (auto&& pp : updateMap) { - /* At this point all peers must be valid as we checked them - * before, so no need to check errors gracefully again */ - auto rbdr = createResult(pp.first, pp.second); + RsBroadcastDiscoveryResult rbdr = + createResult(pp.first, pp.second); - const bool isFriend = mRsPeers.isFriend(rbdr->mSslId); - if( isFriend && rbdr->mLocator.hasPort() && - !mRsPeers.isOnline(rbdr->mSslId) ) + const bool isFriend = mRsPeers.isFriend(rbdr.mSslId); + if( isFriend && rbdr.mLocator.hasPort() && + !mRsPeers.isOnline(rbdr.mSslId) ) { mRsPeers.setLocalAddress( - rbdr->mSslId, rbdr->mLocator.host(), - rbdr->mLocator.port() ); - mRsPeers.connectAttempt(rbdr->mSslId); + rbdr.mSslId, rbdr.mLocator.host(), + rbdr.mLocator.port() ); + mRsPeers.connectAttempt(rbdr.mSslId); } else if(!isFriend) { - auto ev = std::make_shared(); - ev->mDiscoveryEventType = RsBroadcastDiscoveryEventType::PEER_FOUND; - ev->mData = *rbdr; - rsEvents->postEvent(ev); + if(rsEvents) + { + auto ev = std::make_shared(); + + ev->mDiscoveryEventType = RsBroadcastDiscoveryEventType::PEER_FOUND; + ev->mData = rbdr; + + rsEvents->postEvent(ev); + } } } } @@ -243,33 +200,29 @@ void BroadcastDiscoveryService::threadTick() if( mUdcParameters.can_be_discovered() && !mRsPeers.isHiddenNode(mRsPeers.getOwnId()) ) updatePublishedData(); - /* This avoids waiting 5 secs when the thread should actually terminate - * (when RS closes). */ - for(uint32_t i=0;i<10;++i) - { - if(shouldStop()) return; - rstime::rs_usleep(500*1000); // sleep for 0.5 sec. - } + // This avoids waiting 5 secs when the thread should actually terminate (when RS closes). + for(uint32_t i=0;i<10;++i) + { + if(shouldStop()) + return; + rstime::rs_usleep(500*1000); // sleep for 0.5 sec. + } } -/*static*/ -std::unique_ptr -BroadcastDiscoveryService::createResult( - const UDC::IpPort& ipp, const std::string& uData, - rs_view_ptr ec ) +RsBroadcastDiscoveryResult BroadcastDiscoveryService::createResult( + const udpdiscovery::IpPort& ipp, const std::string& uData ) { - /* if ec is nullptr the error is treathed downstream otherwise upstream in - * any case should not be treated here */ - auto bdp = BroadcastDiscoveryPack::fromSerializedString(uData, ec); + BroadcastDiscoveryPack bdp = + BroadcastDiscoveryPack::fromSerializedString(uData); - auto rbdr = std::make_unique(); - rbdr->mPgpFingerprint = bdp->mPgpFingerprint; - rbdr->mSslId = bdp->mSslId; - rbdr->mProfileName = bdp->mProfileName; - rbdr->mLocator. + RsBroadcastDiscoveryResult rbdr; + rbdr.mPgpFingerprint = bdp.mPgpFingerprint; + rbdr.mSslId = bdp.mSslId; + rbdr.mProfileName = bdp.mProfileName; + rbdr.mLocator. setScheme("ipv4"). setHost(UDC::IpToString(ipp.ip())). - setPort(bdp->mLocalPort); + setPort(bdp.mLocalPort); return rbdr; } diff --git a/src/services/broadcastdiscoveryservice.h b/src/services/broadcastdiscoveryservice.h index f16b50958..f4204c324 100644 --- a/src/services/broadcastdiscoveryservice.h +++ b/src/services/broadcastdiscoveryservice.h @@ -1,8 +1,8 @@ /******************************************************************************* * RetroShare Broadcast Domain Discovery * * * - * Copyright (C) 2019-2022 Gioacchino Mazzurco * - * Copyright (C) 2019-2022 Asociación Civil Altermundi * + * Copyright (C) 2019-2021 Gioacchino Mazzurco * + * Copyright (C) 2019-2021 Asociación Civil Altermundi * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * @@ -23,13 +23,12 @@ #include #include #include +#include #include -#include #include #include "retroshare/rsbroadcastdiscovery.h" -#include "util/rsmemory.h" #include "util/rsthreads.h" #include "util/rsdebug.h" @@ -77,18 +76,8 @@ protected: RsPeers& mRsPeers; - /** - * @brief Create result object from data - * @param[in] ipp peer IP and port - * @param[in] uData serialized data associated to the peer - * @param[out] ec Optional storage for eventual error code, - * meaningful only on failure, if a nullptr is passed ther error is treated - * as fatal downstream, otherwise it bubble up to be treated upstream - * @return nullptr on failure, pointer to the generated result otherwise - */ - static std::unique_ptr createResult( - const UDC::IpPort& ipp, const std::string& uData, - rs_view_ptr ec = nullptr ); + RsBroadcastDiscoveryResult createResult( + const UDC::IpPort& ipp, const std::string& uData ); #ifdef __ANDROID__ struct AndroidMulticastLock diff --git a/src/util/rsmemory.h b/src/util/rsmemory.h index 7f7c961dd..8afc48cf7 100644 --- a/src/util/rsmemory.h +++ b/src/util/rsmemory.h @@ -120,9 +120,7 @@ static constexpr size_t SAFE_MEMALLOC_THRESHOLD = 1024*1024*1024; * `uint8_t* ptr = rs_malloc(40);` * @param[in] size number of bytes to allocate * @param[out] ec optional storage for error details. Value is meaningful only - * when nullptr is returned. If a nullptr is passed then errors are treated - * as fatal inside rs_malloc, otherwise they are bubbled up to be treated - * upstream + * whem nullptr is returned. * @return nullptr on error, pointer to the allocated chuck of memory on success */ template rs_owner_ptr rs_malloc( @@ -148,7 +146,6 @@ template rs_owner_ptr rs_malloc( { RS_ERR( "A chunk of size larger than ", SAFE_MEMALLOC_THRESHOLD, " was requested" ); - print_stacktrace(); exit(static_cast(std::errc::argument_out_of_domain)); }