From 6faf467e9f6394bd0fded50cf3061bb8eba0ee2b Mon Sep 17 00:00:00 2001 From: yanhui Date: Sat, 19 Apr 2025 18:12:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?session=E4=B8=AD=E5=A2=9E=E5=8A=A0storeId?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yanhui Change-Id: I2879464abe18be43d0b4ab5e2f9c9183234ef648 --- .../route_head_handler_impl.cpp | 48 ++++++++++++++++--- .../session_manager/route_head_handler_impl.h | 8 ++++ .../src/session_manager/session_manager.cpp | 3 ++ .../app/src/session_manager/session_manager.h | 1 + 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index f52cde722..787729e59 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -113,7 +113,8 @@ DistributedDB::DBStatus RouteHeadHandlerImpl::GetHeadDataSize(uint32_t &headSize return DistributedDB::DB_ERROR; } size_t expectSize = sizeof(RouteHead) + sizeof(SessionDevicePair) + sizeof(SessionUserPair) - + session_.targetUserIds.size() * sizeof(int) + sizeof(SessionAppId) + session_.appId.size(); + + session_.targetUserIds.size() * sizeof(int) + sizeof(SessionAppId) + session_.appId.size() + + sizeof(SessionStoreId) + session_.storeId.size(); // align message uint width headSize = GET_ALIGNED_SIZE(expectSize, ALIGN_WIDTH); @@ -194,16 +195,24 @@ bool RouteHeadHandlerImpl::PackDataBody(uint8_t *data, uint32_t totalLen) ptr += (sizeof(SessionUserPair) + session_.targetUserIds.size() * sizeof(int)); SessionAppId *appPair = reinterpret_cast(ptr); - ptr += sizeof(SessionAppId); - - uint8_t *end = data + totalLen; uint32_t appIdSize = session_.appId.size(); - ret = memcpy_s(appPair->appId, end - ptr, session_.appId.data(), appIdSize); + appPair->len = HostToNet(appIdSize); + ret = strcpy_s(appPair->appId, SessionAppId::MAX_APP_ID_LEN, session_.appId.c_str()); if (ret != 0) { ZLOGE("strcpy for app id failed, error:%{public}d", errno); return false; } - appPair->len = HostToNet(appIdSize); + ptr += (sizeof(SessionAppId) + appIdSize); + + uint8_t *end = data + totalLen; + SessionStoreId *storePair = reinterpret_cast(ptr); + uint32_t storeIdSize = session_.storeId.size(); + ret = memcpy_s(storePair->storeId, end - ptr, session_.storeId.data(), storeIdSize); + if (ret != 0) { + ZLOGE("strcpy for store id failed, error:%{public}d", errno); + return false; + } + storePair->len = HostToNet(storeIdSize); return true; } @@ -222,6 +231,9 @@ bool RouteHeadHandlerImpl::ParseHeadDataLen(const uint8_t *data, uint32_t totalL std::string RouteHeadHandlerImpl::ParseStoreId(const std::string &deviceId, const std::string &label) { + if (!session_.storeId.empty()) { + return session_.storeId; + } std::vector metaData; auto prefix = StoreMetaData::GetPrefix({ deviceId }); if (!MetaDataManager::GetInstance().LoadMeta(prefix, metaData)) { @@ -255,7 +267,7 @@ bool RouteHeadHandlerImpl::ParseHeadDataUser(const uint8_t *data, uint32_t total metaData.deviceId = session_.targetDeviceId; metaData.user = DEFAULT_USERID; metaData.bundleName = session_.appId; - metaData.storeId = storeId; + metaData.storeId = std::move(storeId); if (!MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData)) { int foregroundUserId = 0; AccountDelegate::GetInstance()->QueryForegroundUserId(foregroundUserId); @@ -369,6 +381,28 @@ bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen return false; } session_.appId.append(appId->appId, appIdLen); + leftSize -= (sizeof(SessionAppId) + appIdLen); + if (leftSize > 0) { + ptr += (sizeof(SessionAppId) + appIdLen); + return UnPackStoreId(ptr, leftSize); + } + return true; +} + +bool RouteHeadHandlerImpl::UnPackStoreId(const uint8_t *data, uint32_t leftSize) +{ + if (leftSize < sizeof(SessionStoreId)) { + ZLOGE("failed to parse store id, leftSize:%{public}d.", leftSize); + return false; + } + const uint8_t *ptr = data; + const SessionStoreId *storeId = reinterpret_cast(ptr); + auto storeIdLen = NetToHost(storeId->len); + if (leftSize - sizeof(SessionStoreId) < storeIdLen) { + ZLOGE("failed to parse store id, storeIdLen:%{public}d, leftSize:%{public}d.", storeIdLen, leftSize); + return false; + } + session_.storeId = std::string(storeId->storeId, storeIdLen); return true; } } // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h index f23360dd7..163d958c2 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h @@ -50,9 +50,16 @@ struct SessionUserPair { }; struct SessionAppId { + static constexpr int32_t MAX_APP_ID_LEN = 256; uint32_t len; char appId[0]; }; + +struct SessionStoreId { + static constexpr int32_t MAX_STORE_ID_LEN = 256; + uint32_t len; + char storeId[0]; +}; #pragma pack() class RouteHeadHandlerImpl : public DistributedData::RouteHeadHandler { @@ -74,6 +81,7 @@ private: bool UnPackDataHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead); bool UnPackDataBody(const uint8_t *data, uint32_t totalLen); std::string ParseStoreId(const std::string &deviceId, const std::string &label); + bool UnPackStoreId(const uint8_t *data, uint32_t leftSize); std::string userId_; std::string appId_; diff --git a/services/distributeddataservice/app/src/session_manager/session_manager.cpp b/services/distributeddataservice/app/src/session_manager/session_manager.cpp index 4b5d6eaae..1ef5c834a 100644 --- a/services/distributeddataservice/app/src/session_manager/session_manager.cpp +++ b/services/distributeddataservice/app/src/session_manager/session_manager.cpp @@ -44,6 +44,7 @@ Session SessionManager::GetSession(const SessionPoint &local, const std::string ZLOGD("begin. peer device:%{public}s", Anonymous::Change(targetDeviceId).c_str()); Session session; session.appId = local.appId; + session.storeId = local.storeId; session.sourceUserId = local.userId; session.sourceDeviceId = local.deviceId; session.targetDeviceId = targetDeviceId; @@ -163,6 +164,7 @@ bool Session::Marshal(json &node) const ret = SetValue(node[GET_NAME(sourceUserId)], sourceUserId) && ret; ret = SetValue(node[GET_NAME(targetUserIds)], targetUserIds) && ret; ret = SetValue(node[GET_NAME(appId)], appId) && ret; + ret = SetValue(node[GET_NAME(storeId)], storeId) && ret; return ret; } @@ -174,6 +176,7 @@ bool Session::Unmarshal(const json &node) ret = GetValue(node, GET_NAME(sourceUserId), sourceUserId) && ret; ret = GetValue(node, GET_NAME(targetUserIds), targetUserIds) && ret; ret = GetValue(node, GET_NAME(appId), appId) && ret; + ret = GetValue(node, GET_NAME(storeId), storeId) && ret; return ret; } } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/app/src/session_manager/session_manager.h b/services/distributeddataservice/app/src/session_manager/session_manager.h index ca83970bc..c72c1bef1 100644 --- a/services/distributeddataservice/app/src/session_manager/session_manager.h +++ b/services/distributeddataservice/app/src/session_manager/session_manager.h @@ -39,6 +39,7 @@ public: uint32_t sourceUserId; std::vector targetUserIds; std::string appId; + std::string storeId; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; inline bool IsValid() -- Gitee From 41ed1549c70eb81bc4cb45459bf552df2308666f Mon Sep 17 00:00:00 2001 From: yanhui Date: Thu, 24 Apr 2025 20:35:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=94=B9=E7=94=A8appIdSize?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yanhui Change-Id: Iaa10858b1fd80e77da3910f66284580a08b7302d --- .../app/src/session_manager/route_head_handler_impl.cpp | 2 +- .../app/src/session_manager/route_head_handler_impl.h | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index 787729e59..d7feaea8e 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -197,7 +197,7 @@ bool RouteHeadHandlerImpl::PackDataBody(uint8_t *data, uint32_t totalLen) SessionAppId *appPair = reinterpret_cast(ptr); uint32_t appIdSize = session_.appId.size(); appPair->len = HostToNet(appIdSize); - ret = strcpy_s(appPair->appId, SessionAppId::MAX_APP_ID_LEN, session_.appId.c_str()); + ret = strcpy_s(appPair->appId, appIdSize, session_.appId.c_str()); if (ret != 0) { ZLOGE("strcpy for app id failed, error:%{public}d", errno); return false; diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h index 163d958c2..11583b7b1 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h @@ -50,13 +50,11 @@ struct SessionUserPair { }; struct SessionAppId { - static constexpr int32_t MAX_APP_ID_LEN = 256; uint32_t len; char appId[0]; }; struct SessionStoreId { - static constexpr int32_t MAX_STORE_ID_LEN = 256; uint32_t len; char storeId[0]; }; -- Gitee