From 04da34a56243428ca5becd2eb08270798196ef8c Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Thu, 3 Mar 2022 09:41:54 +0800 Subject: [PATCH 1/7] =?UTF-8?q?OpenHarmonySA=E4=B8=AD=E7=BB=A7=E6=94=BB?= =?UTF-8?q?=E5=87=BB=E9=97=AE=E9=A2=98=E6=8E=92=E6=9F=A5=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=20Signed-off-by:=20zuojiangjiang=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../include/ikvstore_client_death_observer.h | 3 +-- .../src/ikvstore_client_death_observer.cpp | 6 ------ .../distributeddataservice/app/src/kvstore_data_service.cpp | 4 ++-- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_client_death_observer.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_client_death_observer.h index 15a83f10d..ee6d91a4a 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_client_death_observer.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_client_death_observer.h @@ -30,8 +30,7 @@ public: class KvStoreClientDeathObserverStub : public IRemoteStub { public: - virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, - MessageParcel &reply, MessageOption &option) override; + }; class KvStoreClientDeathObserverProxy : public IRemoteProxy { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_client_death_observer.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_client_death_observer.cpp index 4299647b2..2c6616444 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_client_death_observer.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_client_death_observer.cpp @@ -20,11 +20,5 @@ namespace DistributedKv { KvStoreClientDeathObserverProxy::KvStoreClientDeathObserverProxy(const sptr &impl) : IRemoteProxy(impl) {} - -int32_t KvStoreClientDeathObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, - MessageParcel &reply, MessageOption &option) -{ - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); -} } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 57ec67c97..7733dfee8 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -737,9 +737,9 @@ Status KvStoreDataService::AppExit(const AppId &appId, pid_t uid) ZLOGI("map size: %zu.", clientDeathObserverMap_.size()); } - std::string trueAppId = CheckerManager::GetInstance().GetAppId(appId.appId, uid); + std::string trueAppId = CheckerManager::GetInstance().GetAppId(appIdTmp.appId, uid); if (trueAppId.empty()) { - ZLOGW("check appId:%{public}s uid:%{public}d failed.", appId.appId.c_str(), uid); + ZLOGW("check appId:%{public}s uid:%{public}d failed.", appIdTmp.appId.c_str(), uid); return Status::PERMISSION_DENIED; } const std::string userId = AccountDelegate::GetInstance()->GetCurrentAccountId(appIdTmp.appId); -- Gitee From 280154bc7e8890457cad49efdbb0afb2e3b0eda4 Mon Sep 17 00:00:00 2001 From: illybyy Date: Mon, 7 Mar 2022 22:09:24 +0800 Subject: [PATCH 2/7] increase retry count and do error process while empty device Signed-off-by: illybyy --- .../app/src/kvstore_data_service.cpp | 66 +++++++++++++------ .../app/src/kvstore_data_service.h | 2 + 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 7733dfee8..684d1160b 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -186,26 +186,16 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId { DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); ZLOGI("begin."); - if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType() - || options.kvStoreType == KvStoreType::MULTI_VERSION) { - ZLOGE("invalid argument type."); - return Status::INVALID_ARGUMENT; - } - KVSTORE_ACCOUNT_EVENT_PROCESSING_CHECKER(Status::SYSTEM_ACCOUNT_EVENT_PROCESSING); KvStoreParam param; - param.bundleName = appId.appId; - param.storeId = storeId.storeId; - const int32_t uid = IPCSkeleton::GetCallingUid(); - param.trueAppId = CheckerManager::GetInstance().GetAppId(appId.appId, uid); - if (param.trueAppId.empty()) { - ZLOGW("appId:%{public}s, uid:%{public}d, PERMISSION_DENIED", appId.appId.c_str(), uid); - return Status::PERMISSION_DENIED; + Status status = FillStoreParam(options, appId, storeId, param); + if (status != Status::SUCCESS) { + callback(nullptr); + return status; } - param.userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(uid); SecretKeyPara keyPara; - Status status = KvStoreDataService::GetSecretKey(options, param, keyPara); + status = KvStoreDataService::GetSecretKey(options, param, keyPara); if (status != Status::SUCCESS) { callback(nullptr); return status; @@ -223,14 +213,21 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId it = result.first; } sptr store; - param.status = (it->second).GetKvStore(options, param.bundleName, param.storeId, uid, keyPara.secretKey, store); + param.status = + (it->second).GetKvStore(options, param.bundleName, param.storeId, param.uid, keyPara.secretKey, store); if (keyPara.outdated) { KvStoreMetaManager::GetInstance().ReKey(param.userId, param.bundleName, param.storeId, KvStoreAppManager::ConvertPathType(param.uid, param.bundleName, options.securityLevel), store); } if (param.status == Status::SUCCESS) { + status = UpdateMetaData(options, param, keyPara.metaKey, it->second); + if (status != Status::SUCCESS) { + ZLOGE("failed to write meta"); + callback(nullptr); + return status; + } callback(std::move(store)); - return UpdateMetaData(options, param, keyPara.metaKey, it->second); + return status; } param.status = GetSingleKvStoreFailDo(options, param, keyPara, it->second, store); @@ -238,6 +235,28 @@ Status KvStoreDataService::GetSingleKvStore(const Options &options, const AppId return param.status; } +Status KvStoreDataService::FillStoreParam( + const Options &options, const AppId &appId, const StoreId &storeId, KvStoreParam ¶m) +{ + if (!appId.IsValid() || !storeId.IsValid() || !options.IsValidType() + || options.kvStoreType == KvStoreType::MULTI_VERSION) { + ZLOGE("invalid argument type."); + return Status::INVALID_ARGUMENT; + } + param.bundleName = appId.appId; + param.storeId = storeId.storeId; + param.uid = IPCSkeleton::GetCallingUid(); + param.trueAppId = CheckerManager::GetInstance().GetAppId(appId.appId, param.uid); + ZLOGI("%{public}s, %{public}s", param.trueAppId.c_str(), param.bundleName.c_str()); + if (param.trueAppId.empty()) { + ZLOGW("appId:%{public}s, uid:%{public}d, PERMISSION_DENIED", appId.appId.c_str(), param.uid); + return PERMISSION_DENIED; + } + + param.userId = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(param.uid); + return SUCCESS; +} + Status KvStoreDataService::GetSecretKey(const Options &options, const KvStoreParam &kvParas, SecretKeyPara &secretKeyParas) { @@ -323,12 +342,17 @@ Status KvStoreDataService::RecoverSecretKey(const Status &alreadyCreated, bool & Status KvStoreDataService::UpdateMetaData(const Options &options, const KvStoreParam &kvParas, const std::vector &metaKey, KvStoreUserManager &kvStoreUserManager) { + auto localDeviceId = DeviceKvStoreImpl::GetLocalDeviceId(); + if (localDeviceId.empty()) { + ZLOGE("failed to get local device id"); + return Status::ERROR; + } KvStoreMetaData metaData; metaData.appId = kvParas.trueAppId; metaData.appType = "harmony"; metaData.bundleName = kvParas.bundleName; metaData.deviceAccountId = kvParas.userId; - metaData.deviceId = DeviceKvStoreImpl::GetLocalDeviceId(); + metaData.deviceId = localDeviceId; metaData.isAutoSync = options.autoSync; metaData.isBackup = options.backup; metaData.isEncrypt = options.encrypt; @@ -816,12 +840,12 @@ void KvStoreDataService::OnStart() void KvStoreDataService::StartService() { static constexpr int32_t RETRY_TIMES = 10; - static constexpr int32_t RETRY_INTERVAL = 500; // unit is ms + static constexpr int32_t RETRY_INTERVAL = 500 * 1000; // unit is ms for (BlockInteger retry(RETRY_INTERVAL); retry < RETRY_TIMES; ++retry) { if (!DeviceKvStoreImpl::GetLocalDeviceId().empty()) { break; } - ZLOGE("GetLocalDeviceId failed, reties: %{public}d", static_cast(retry)); + ZLOGE("GetLocalDeviceId failed, retry count:%{public}d", static_cast(retry)); } // register this to ServiceManager. @@ -960,7 +984,7 @@ bool KvStoreDataService::CheckPermissions(const std::string &userId, const std:: return true; } bool ret = PermissionValidator::CheckSyncPermission(userId, appId, metaData.uid); - ZLOGD("checking sync permission ret:%d.", ret); + ZLOGD("checking sync permission ret:%{public}d.", ret); return ret; } diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index f62715443..766935805 100755 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -167,6 +167,8 @@ private: bool CheckOptions(const Options &options, const std::vector &metaKey) const; void CreateRdbService(); + static Status FillStoreParam( + const Options &options, const AppId &appId, const StoreId &storeId, KvStoreParam ¶m); static constexpr int TEN_SEC = 10; -- Gitee From f13f8cb98c2fa9bfe3b62e41c7d2a8600a2e88e7 Mon Sep 17 00:00:00 2001 From: illybyy Date: Thu, 10 Mar 2022 16:43:27 +0800 Subject: [PATCH 3/7] swap byteorder Signed-off-by: illybyy --- .../route_head_handler_impl.cpp | 50 +++++++++++-------- .../session_manager/route_head_handler_impl.h | 6 +-- .../include/utils/endian_converter.h | 38 ++++++++++++++ 3 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 services/distributeddataservice/framework/include/utils/endian_converter.h 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 f763d20d3..c5f3eb1ae 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 @@ -18,6 +18,7 @@ #include "auth/auth_delegate.h" #include "device_kvstore_impl.h" +#include "utils/endian_converter.h" #include "kvstore_meta_manager.h" #include "log_print.h" #include "securec.h" @@ -125,10 +126,10 @@ bool RouteHeadHandlerImpl::PackDataHead(uint8_t *data, uint32_t totalLen) return false; } RouteHead *head = reinterpret_cast(ptr); - head->magic = RouteHead::MAGIC_NUMBER; - head->version = RouteHead::VERSION; - head->checkSum = 0; - head->dataLen = static_cast(totalLen - sizeof(RouteHead)); + head->magic = htons(RouteHead::MAGIC_NUMBER); + head->version = htons(RouteHead::VERSION); + head->checkSum = htonll(0); + head->dataLen = htonl(totalLen - sizeof(RouteHead)); return true; } @@ -149,15 +150,15 @@ bool RouteHeadHandlerImpl::PackDataBody(uint8_t *data, uint32_t totalLen) ptr += sizeof(SessionDevicePair); SessionUserPair *userPair = reinterpret_cast(ptr); - userPair->sourceUserId = session_.sourceUserId; + userPair->sourceUserId = htonl(session_.sourceUserId); userPair->targetUserCount = session_.targetUserIds.size(); for (size_t i = 0; i < session_.targetUserIds.size(); ++i) { - *(userPair->targetUserIds + i) = session_.targetUserIds[i]; + *(userPair->targetUserIds + i) = htonl(session_.targetUserIds[i]); } ptr += (sizeof(SessionUserPair) + session_.targetUserIds.size() * sizeof(int)); SessionAppId *appPair = reinterpret_cast(ptr); - appPair->len = data + totalLen - ptr; // left size + appPair->len = htonl(data + totalLen - ptr); // left size ret = strcpy_s(appPair->appId, data + totalLen - ptr, session_.appId.c_str()); if (ret != 0) { ZLOGE("strcpy for app id failed"); @@ -196,30 +197,34 @@ bool RouteHeadHandlerImpl::UnPackData(const uint8_t *data, uint32_t totalLen, ui return false; } unpackedSize = 0; - const RouteHead *head = UnPackHeadHead(data, totalLen); - if (head != nullptr && head->version == RouteHead::VERSION) { + RouteHead head = { 0 }; + bool result = UnPackHeadHead(data, totalLen, head); + if (result && head.version == RouteHead::VERSION) { auto isOk = UnPackHeadBody(data + sizeof(RouteHead), totalLen - sizeof(RouteHead)); if (isOk) { - unpackedSize = sizeof(RouteHead) + head->dataLen; + unpackedSize = sizeof(RouteHead) + head.dataLen; } return isOk; } return false; } -const RouteHead *RouteHeadHandlerImpl::UnPackHeadHead(const uint8_t *data, uint32_t totalLen) +bool RouteHeadHandlerImpl::UnPackHeadHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead) { - const uint8_t *ptr = data; - const RouteHead *head = reinterpret_cast(ptr); - if (head->magic != RouteHead::MAGIC_NUMBER) { + const RouteHead *head = reinterpret_cast(data); + routeHead.magic = ntohs(head->magic); + routeHead.version = ntohs(head->version); + routeHead.checkSum = ntohll(head->checkSum); + routeHead.dataLen = ntohl(head->dataLen); + if (routeHead.magic != RouteHead::MAGIC_NUMBER) { ZLOGW("not route head data"); - return nullptr; + return false; } - if (head->dataLen + sizeof(RouteHead) > totalLen) { + if (routeHead.dataLen + sizeof(RouteHead) > totalLen) { ZLOGE("invalid route head len"); - return nullptr; + return false; } - return head; + return true; } bool RouteHeadHandlerImpl::UnPackHeadBody(const uint8_t *data, uint32_t totalLen) @@ -242,14 +247,14 @@ bool RouteHeadHandlerImpl::UnPackHeadBody(const uint8_t *data, uint32_t totalLen return false; } const SessionUserPair *userPair = reinterpret_cast(ptr); - session_.sourceUserId = userPair->sourceUserId; + session_.sourceUserId = ntohl(userPair->sourceUserId); if (leftSize < sizeof(SessionUserPair) + userPair->targetUserCount * sizeof(int)) { ZLOGE("failed to parse user pair, target user"); return false; } for (int i = 0; i < userPair->targetUserCount; ++i) { - session_.targetUserIds.push_back(*(userPair->targetUserIds + i)); + session_.targetUserIds.push_back(ntohl(*(userPair->targetUserIds + i))); } ptr += sizeof(SessionUserPair) + userPair->targetUserCount * sizeof(int); @@ -258,12 +263,13 @@ bool RouteHeadHandlerImpl::UnPackHeadBody(const uint8_t *data, uint32_t totalLen return false; } const SessionAppId *appId = reinterpret_cast(ptr); + auto appIdLen = ntohl(appId->len); - if (leftSize < sizeof(SessionAppId) + appId->len) { + if (leftSize < sizeof(SessionAppId) + appIdLen) { ZLOGE("failed to parse app id"); return false; } - session_.appId.append(appId->appId, appId->len); + session_.appId.append(appId->appId, appIdLen); 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 395e4d336..ea0253edb 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 @@ -44,9 +44,9 @@ struct SessionDevicePair { }; struct SessionUserPair { - int sourceUserId; + uint32_t sourceUserId; uint8_t targetUserCount; - int targetUserIds[0]; + uint32_t targetUserIds[0]; }; struct SessionAppId { @@ -69,7 +69,7 @@ private: bool PackDataHead(uint8_t *data, uint32_t totalLen); bool PackDataBody(uint8_t *data, uint32_t totalLen); bool UnPackData(const uint8_t *data, uint32_t totalLen, uint32_t &unpackedSize); - const RouteHead *UnPackHeadHead(const uint8_t *data, uint32_t totalLen); + bool UnPackHeadHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead); bool UnPackHeadBody(const uint8_t *data, uint32_t totalLen); std::string userId_; diff --git a/services/distributeddataservice/framework/include/utils/endian_converter.h b/services/distributeddataservice/framework/include/utils/endian_converter.h new file mode 100644 index 000000000..df7c8542a --- /dev/null +++ b/services/distributeddataservice/framework/include/utils/endian_converter.h @@ -0,0 +1,38 @@ +/* +* Copyright (c) 2022 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H +#define DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H + +#include +#include + +static inline uint64_t htonll(const uint64_t value) +{ +#if __BYTE_ORDER == __LITTLE_ENDIAN + constexpr const uint32_t UINT32_SIZE = 32; + constexpr const uint64_t UINT64_MASK_HIGH = 0xFFFFFFFF00000000U; + constexpr const uint64_t UINT64_MASK_LOW = 0x00000000FFFFFFFFU; + return ((((uint64_t)htonl(value)) << UINT32_SIZE) & UINT64_MASK_HIGH) + | (htonl(value >> UINT32_SIZE) & UINT64_MASK_LOW); +#endif + return value; +} + +static inline uint64_t ntohll(const uint64_t value) +{ + return htonll(value); +} +#endif // DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H -- Gitee From 589e3793c6ad7cd75b77de6959c4bb91512734cb Mon Sep 17 00:00:00 2001 From: illybyy Date: Thu, 10 Mar 2022 16:49:17 +0800 Subject: [PATCH 4/7] rename function Signed-off-by: illybyy --- .../app/src/session_manager/route_head_handler_impl.cpp | 8 ++++---- .../app/src/session_manager/route_head_handler_impl.h | 4 ++-- 2 files changed, 6 insertions(+), 6 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 c5f3eb1ae..262713329 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 @@ -198,9 +198,9 @@ bool RouteHeadHandlerImpl::UnPackData(const uint8_t *data, uint32_t totalLen, ui } unpackedSize = 0; RouteHead head = { 0 }; - bool result = UnPackHeadHead(data, totalLen, head); + bool result = UnPackDataHead(data, totalLen, head); if (result && head.version == RouteHead::VERSION) { - auto isOk = UnPackHeadBody(data + sizeof(RouteHead), totalLen - sizeof(RouteHead)); + auto isOk = UnPackDataBody(data + sizeof(RouteHead), totalLen - sizeof(RouteHead)); if (isOk) { unpackedSize = sizeof(RouteHead) + head.dataLen; } @@ -209,7 +209,7 @@ bool RouteHeadHandlerImpl::UnPackData(const uint8_t *data, uint32_t totalLen, ui return false; } -bool RouteHeadHandlerImpl::UnPackHeadHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead) +bool RouteHeadHandlerImpl::UnPackDataHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead) { const RouteHead *head = reinterpret_cast(data); routeHead.magic = ntohs(head->magic); @@ -227,7 +227,7 @@ bool RouteHeadHandlerImpl::UnPackHeadHead(const uint8_t *data, uint32_t totalLen return true; } -bool RouteHeadHandlerImpl::UnPackHeadBody(const uint8_t *data, uint32_t totalLen) +bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen) { const uint8_t *ptr = data; uint32_t leftSize = totalLen; 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 ea0253edb..3deb9ed9e 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 @@ -69,8 +69,8 @@ private: bool PackDataHead(uint8_t *data, uint32_t totalLen); bool PackDataBody(uint8_t *data, uint32_t totalLen); bool UnPackData(const uint8_t *data, uint32_t totalLen, uint32_t &unpackedSize); - bool UnPackHeadHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead); - bool UnPackHeadBody(const uint8_t *data, uint32_t totalLen); + bool UnPackDataHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead); + bool UnPackDataBody(const uint8_t *data, uint32_t totalLen); std::string userId_; std::string appId_; -- Gitee From 66955d7a869d015611e29187cf98cef9826b22c3 Mon Sep 17 00:00:00 2001 From: illybyy Date: Thu, 10 Mar 2022 17:53:25 +0800 Subject: [PATCH 5/7] fix code check Signed-off-by: illybyy --- .../app/src/session_manager/route_head_handler_impl.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 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 262713329..3349a41d4 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 @@ -18,11 +18,11 @@ #include "auth/auth_delegate.h" #include "device_kvstore_impl.h" -#include "utils/endian_converter.h" #include "kvstore_meta_manager.h" #include "log_print.h" #include "securec.h" #include "upgrade_manager.h" +#include "utils/endian_converter.h" namespace OHOS::DistributedData { using namespace OHOS::DistributedKv; @@ -264,7 +264,6 @@ bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen } const SessionAppId *appId = reinterpret_cast(ptr); auto appIdLen = ntohl(appId->len); - if (leftSize < sizeof(SessionAppId) + appIdLen) { ZLOGE("failed to parse app id"); return false; -- Gitee From 6373f009951b59bc1f9556c70b8e2b9b90e79c36 Mon Sep 17 00:00:00 2001 From: illybyy Date: Fri, 11 Mar 2022 12:03:45 +0800 Subject: [PATCH 6/7] use endian.h Signed-off-by: illybyy --- .../route_head_handler_impl.cpp | 31 +++++++-------- .../app/src/session_manager/session_manager.h | 6 +-- .../include/metadata/user_meta_data.h | 2 +- .../include/utils/endian_converter.h | 38 ++++++++++++------- 4 files changed, 45 insertions(+), 32 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 3349a41d4..0a9311458 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 @@ -126,10 +126,10 @@ bool RouteHeadHandlerImpl::PackDataHead(uint8_t *data, uint32_t totalLen) return false; } RouteHead *head = reinterpret_cast(ptr); - head->magic = htons(RouteHead::MAGIC_NUMBER); - head->version = htons(RouteHead::VERSION); - head->checkSum = htonll(0); - head->dataLen = htonl(totalLen - sizeof(RouteHead)); + head->magic = htobe(RouteHead::MAGIC_NUMBER); + head->version = htobe(RouteHead::VERSION); + head->checkSum = htobe(uint64_t(0)); + head->dataLen = htobe(uint32_t(totalLen - sizeof(RouteHead))); return true; } @@ -150,16 +150,17 @@ bool RouteHeadHandlerImpl::PackDataBody(uint8_t *data, uint32_t totalLen) ptr += sizeof(SessionDevicePair); SessionUserPair *userPair = reinterpret_cast(ptr); - userPair->sourceUserId = htonl(session_.sourceUserId); + userPair->sourceUserId = htobe(session_.sourceUserId); userPair->targetUserCount = session_.targetUserIds.size(); for (size_t i = 0; i < session_.targetUserIds.size(); ++i) { - *(userPair->targetUserIds + i) = htonl(session_.targetUserIds[i]); + *(userPair->targetUserIds + i) = htobe(session_.targetUserIds[i]); } ptr += (sizeof(SessionUserPair) + session_.targetUserIds.size() * sizeof(int)); SessionAppId *appPair = reinterpret_cast(ptr); - appPair->len = htonl(data + totalLen - ptr); // left size - ret = strcpy_s(appPair->appId, data + totalLen - ptr, session_.appId.c_str()); + uint32_t appLen = data + totalLen - ptr; + appPair->len = htobe(appLen); // left size + ret = strcpy_s(appPair->appId, appLen, session_.appId.c_str()); if (ret != 0) { ZLOGE("strcpy for app id failed"); return false; @@ -212,10 +213,10 @@ bool RouteHeadHandlerImpl::UnPackData(const uint8_t *data, uint32_t totalLen, ui bool RouteHeadHandlerImpl::UnPackDataHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead) { const RouteHead *head = reinterpret_cast(data); - routeHead.magic = ntohs(head->magic); - routeHead.version = ntohs(head->version); - routeHead.checkSum = ntohll(head->checkSum); - routeHead.dataLen = ntohl(head->dataLen); + routeHead.magic = betoh(head->magic); + routeHead.version = betoh(head->version); + routeHead.checkSum = betoh(head->checkSum); + routeHead.dataLen = betoh(head->dataLen); if (routeHead.magic != RouteHead::MAGIC_NUMBER) { ZLOGW("not route head data"); return false; @@ -247,14 +248,14 @@ bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen return false; } const SessionUserPair *userPair = reinterpret_cast(ptr); - session_.sourceUserId = ntohl(userPair->sourceUserId); + session_.sourceUserId = betoh(userPair->sourceUserId); if (leftSize < sizeof(SessionUserPair) + userPair->targetUserCount * sizeof(int)) { ZLOGE("failed to parse user pair, target user"); return false; } for (int i = 0; i < userPair->targetUserCount; ++i) { - session_.targetUserIds.push_back(ntohl(*(userPair->targetUserIds + i))); + session_.targetUserIds.push_back(betoh(*(userPair->targetUserIds + i))); } ptr += sizeof(SessionUserPair) + userPair->targetUserCount * sizeof(int); @@ -263,7 +264,7 @@ bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen return false; } const SessionAppId *appId = reinterpret_cast(ptr); - auto appIdLen = ntohl(appId->len); + auto appIdLen = betoh(appId->len); if (leftSize < sizeof(SessionAppId) + appIdLen) { ZLOGE("failed to parse app id"); return false; diff --git a/services/distributeddataservice/app/src/session_manager/session_manager.h b/services/distributeddataservice/app/src/session_manager/session_manager.h index 355539738..17909f7de 100644 --- a/services/distributeddataservice/app/src/session_manager/session_manager.h +++ b/services/distributeddataservice/app/src/session_manager/session_manager.h @@ -24,7 +24,7 @@ namespace OHOS::DistributedData { struct SessionPoint { std::string deviceId; - int userId; + uint32_t userId; std::string appId; }; @@ -32,8 +32,8 @@ class Session : public Serializable { public: std::string sourceDeviceId; std::string targetDeviceId; - int32_t sourceUserId; - std::vector targetUserIds; + uint32_t sourceUserId; + std::vector targetUserIds; std::string appId; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; diff --git a/services/distributeddataservice/framework/include/metadata/user_meta_data.h b/services/distributeddataservice/framework/include/metadata/user_meta_data.h index 7ed2b61a2..09353c657 100644 --- a/services/distributeddataservice/framework/include/metadata/user_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/user_meta_data.h @@ -22,7 +22,7 @@ namespace OHOS::DistributedData { class API_EXPORT UserStatus final : public Serializable { public: - int id; + int32_t id; bool isActive; API_EXPORT UserStatus() = default; API_EXPORT ~UserStatus() = default; diff --git a/services/distributeddataservice/framework/include/utils/endian_converter.h b/services/distributeddataservice/framework/include/utils/endian_converter.h index df7c8542a..67e8329b7 100644 --- a/services/distributeddataservice/framework/include/utils/endian_converter.h +++ b/services/distributeddataservice/framework/include/utils/endian_converter.h @@ -16,23 +16,35 @@ #ifndef DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H #define DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H -#include -#include +#include -static inline uint64_t htonll(const uint64_t value) +inline uint16_t htobe(uint16_t value) { -#if __BYTE_ORDER == __LITTLE_ENDIAN - constexpr const uint32_t UINT32_SIZE = 32; - constexpr const uint64_t UINT64_MASK_HIGH = 0xFFFFFFFF00000000U; - constexpr const uint64_t UINT64_MASK_LOW = 0x00000000FFFFFFFFU; - return ((((uint64_t)htonl(value)) << UINT32_SIZE) & UINT64_MASK_HIGH) - | (htonl(value >> UINT32_SIZE) & UINT64_MASK_LOW); -#endif - return value; + return htobe16(value); } -static inline uint64_t ntohll(const uint64_t value) +inline uint16_t betoh(uint16_t value) { - return htonll(value); + return be16toh(value); +} + +inline uint32_t htobe(uint32_t value) +{ + return htobe32(value); +} + +inline uint32_t betoh(uint32_t value) +{ + return be32toh(value); +} + +inline uint64_t htobe(uint64_t value) +{ + return htobe64(value); +} + +inline uint64_t betoh(uint64_t value) +{ + return be64toh(value); } #endif // DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H -- Gitee From f39dabc6de8918f04be114b0c967fbbb08edb349 Mon Sep 17 00:00:00 2001 From: illybyy Date: Fri, 11 Mar 2022 17:51:30 +0800 Subject: [PATCH 7/7] use little endian Signed-off-by: illybyy --- .../route_head_handler_impl.cpp | 28 +++++++++---------- .../include/utils/endian_converter.h | 27 ++++++++++-------- 2 files changed, 29 insertions(+), 26 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 0a9311458..2158affa1 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 @@ -126,10 +126,10 @@ bool RouteHeadHandlerImpl::PackDataHead(uint8_t *data, uint32_t totalLen) return false; } RouteHead *head = reinterpret_cast(ptr); - head->magic = htobe(RouteHead::MAGIC_NUMBER); - head->version = htobe(RouteHead::VERSION); - head->checkSum = htobe(uint64_t(0)); - head->dataLen = htobe(uint32_t(totalLen - sizeof(RouteHead))); + head->magic = HostToNet(RouteHead::MAGIC_NUMBER); + head->version = HostToNet(RouteHead::VERSION); + head->checkSum = HostToNet(uint64_t(0)); + head->dataLen = HostToNet(uint32_t(totalLen - sizeof(RouteHead))); return true; } @@ -150,16 +150,16 @@ bool RouteHeadHandlerImpl::PackDataBody(uint8_t *data, uint32_t totalLen) ptr += sizeof(SessionDevicePair); SessionUserPair *userPair = reinterpret_cast(ptr); - userPair->sourceUserId = htobe(session_.sourceUserId); + userPair->sourceUserId = HostToNet(session_.sourceUserId); userPair->targetUserCount = session_.targetUserIds.size(); for (size_t i = 0; i < session_.targetUserIds.size(); ++i) { - *(userPair->targetUserIds + i) = htobe(session_.targetUserIds[i]); + *(userPair->targetUserIds + i) = HostToNet(session_.targetUserIds[i]); } ptr += (sizeof(SessionUserPair) + session_.targetUserIds.size() * sizeof(int)); SessionAppId *appPair = reinterpret_cast(ptr); uint32_t appLen = data + totalLen - ptr; - appPair->len = htobe(appLen); // left size + appPair->len = HostToNet(appLen); // left size ret = strcpy_s(appPair->appId, appLen, session_.appId.c_str()); if (ret != 0) { ZLOGE("strcpy for app id failed"); @@ -213,10 +213,10 @@ bool RouteHeadHandlerImpl::UnPackData(const uint8_t *data, uint32_t totalLen, ui bool RouteHeadHandlerImpl::UnPackDataHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead) { const RouteHead *head = reinterpret_cast(data); - routeHead.magic = betoh(head->magic); - routeHead.version = betoh(head->version); - routeHead.checkSum = betoh(head->checkSum); - routeHead.dataLen = betoh(head->dataLen); + routeHead.magic = NetToHost(head->magic); + routeHead.version = NetToHost(head->version); + routeHead.checkSum = NetToHost(head->checkSum); + routeHead.dataLen = NetToHost(head->dataLen); if (routeHead.magic != RouteHead::MAGIC_NUMBER) { ZLOGW("not route head data"); return false; @@ -248,14 +248,14 @@ bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen return false; } const SessionUserPair *userPair = reinterpret_cast(ptr); - session_.sourceUserId = betoh(userPair->sourceUserId); + session_.sourceUserId = NetToHost(userPair->sourceUserId); if (leftSize < sizeof(SessionUserPair) + userPair->targetUserCount * sizeof(int)) { ZLOGE("failed to parse user pair, target user"); return false; } for (int i = 0; i < userPair->targetUserCount; ++i) { - session_.targetUserIds.push_back(betoh(*(userPair->targetUserIds + i))); + session_.targetUserIds.push_back(NetToHost(*(userPair->targetUserIds + i))); } ptr += sizeof(SessionUserPair) + userPair->targetUserCount * sizeof(int); @@ -264,7 +264,7 @@ bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen return false; } const SessionAppId *appId = reinterpret_cast(ptr); - auto appIdLen = betoh(appId->len); + auto appIdLen = NetToHost(appId->len); if (leftSize < sizeof(SessionAppId) + appIdLen) { ZLOGE("failed to parse app id"); return false; diff --git a/services/distributeddataservice/framework/include/utils/endian_converter.h b/services/distributeddataservice/framework/include/utils/endian_converter.h index 67e8329b7..faf4c0a58 100644 --- a/services/distributeddataservice/framework/include/utils/endian_converter.h +++ b/services/distributeddataservice/framework/include/utils/endian_converter.h @@ -18,33 +18,36 @@ #include -inline uint16_t htobe(uint16_t value) +namespace OHOS::DistributedData { +// use little endian byteorder by default +static inline uint16_t HostToNet(uint16_t value) { - return htobe16(value); + return htole16(value); } -inline uint16_t betoh(uint16_t value) +static inline uint16_t NetToHost(uint16_t value) { - return be16toh(value); + return le16toh(value); } -inline uint32_t htobe(uint32_t value) +static inline uint32_t HostToNet(uint32_t value) { - return htobe32(value); + return htole32(value); } -inline uint32_t betoh(uint32_t value) +static inline uint32_t NetToHost(uint32_t value) { - return be32toh(value); + return le32toh(value); } -inline uint64_t htobe(uint64_t value) +static inline uint64_t HostToNet(uint64_t value) { - return htobe64(value); + return htole64(value); } -inline uint64_t betoh(uint64_t value) +static inline uint64_t NetToHost(uint64_t value) { - return be64toh(value); + return le64toh(value); } +} // namespace OHOS::DistributedData #endif // DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H -- Gitee