diff --git a/baselib/msglib/BUILD.gn b/baselib/msglib/BUILD.gn index d890c3fa44f3c0375776061e68380f6bef1fd034..f0fe37354d204b856d812fe13ec6b93ca44a28d8 100644 --- a/baselib/msglib/BUILD.gn +++ b/baselib/msglib/BUILD.gn @@ -26,7 +26,10 @@ ohos_static_library("messenger_static") { "src/utils", ] - sources = [ "src/common/messenger.c" ] + sources = [ + "src/common/messenger.c", + "src/utils/messenger_utils.c", + ] deps = [ "../utils:utils_static" ] diff --git a/baselib/msglib/src/lite/messenger_device_session_manager.c b/baselib/msglib/src/lite/messenger_device_session_manager.c index cadde9360b8ff7971d89be58372101e4099fa6a8..bb6a2157256298f431336d299210805ac11f599f 100644 --- a/baselib/msglib/src/lite/messenger_device_session_manager.c +++ b/baselib/msglib/src/lite/messenger_device_session_manager.c @@ -47,12 +47,6 @@ typedef struct DeviceSessionManager { Mutex mutex; } DeviceSessionManager; -typedef struct QueueMsgData { - DeviceIdentify srcIdentity; - uint32_t msgLen; - uint8_t msgData[1]; -} QueueMsgData; - typedef struct PendingMsgData { ListNode link; uint32_t transNo; @@ -121,26 +115,12 @@ static void OnSessionMessageReceived(const DeviceIdentify *devId, const uint8_t SECURITY_LOG_ERROR("messageReceiver is null"); return; } - uint32_t queueDataLen = sizeof(QueueMsgData) + msgLen; - QueueMsgData *queueData = MALLOC(queueDataLen); + uint32_t queueDataLen = 0; + QueueMsgData *queueData = CreateQueueMsgData(devId, msg, msgLen, &queueDataLen); if (queueData == NULL) { - SECURITY_LOG_ERROR("malloc result null"); - return; - } - uint32_t ret = (uint32_t)memcpy_s(&queueData->srcIdentity, sizeof(DeviceIdentify), devId, sizeof(DeviceIdentify)); - if (ret != EOK) { - SECURITY_LOG_ERROR("memcpy failed"); - FREE(queueData); - return; - } - ret = (uint32_t)memcpy_s(queueData->msgData, msgLen, msg, msgLen); - if (ret != EOK) { - SECURITY_LOG_ERROR("memcpy failed"); - FREE(queueData); return; } - queueData->msgLen = msgLen; - ret = QueueWork(queue, ProcessSessionMessageReceived, (uint8_t *)queueData, queueDataLen); + uint32_t ret = QueueWork(queue, ProcessSessionMessageReceived, (uint8_t *)queueData, queueDataLen); if (ret != WORK_QUEUE_OK) { SECURITY_LOG_ERROR("QueueWork failed, ret is %{public}u", ret); FREE(queueData); diff --git a/baselib/msglib/src/standard/messenger_device_socket_manager.c b/baselib/msglib/src/standard/messenger_device_socket_manager.c index a7248b42e95d5574d0bffe5d98545653f77ac637..ee2f87e42b59a2fef120ec6ae7ecc14165477c6b 100644 --- a/baselib/msglib/src/standard/messenger_device_socket_manager.c +++ b/baselib/msglib/src/standard/messenger_device_socket_manager.c @@ -48,12 +48,6 @@ typedef struct DeviceSocketManager { Mutex mutex; } DeviceSocketManager; -typedef struct QueueMsgData { - DeviceIdentify srcIdentity; - uint32_t msgLen; - uint8_t msgData[1]; -} QueueMsgData; - typedef struct PendingMsgData { ListNode link; uint32_t transNo; @@ -130,26 +124,12 @@ static void OnSocketMessageReceived(const DeviceIdentify *devId, const uint8_t * SECURITY_LOG_ERROR("messageReceiver is null"); return; } - uint32_t queueDataLen = sizeof(QueueMsgData) + msgLen; - QueueMsgData *queueData = MALLOC(queueDataLen); + uint32_t queueDataLen = 0; + QueueMsgData *queueData = CreateQueueMsgData(devId, msg, msgLen, &queueDataLen); if (queueData == NULL) { - SECURITY_LOG_ERROR("malloc result null"); - return; - } - uint32_t ret = (uint32_t)memcpy_s(&queueData->srcIdentity, sizeof(DeviceIdentify), devId, sizeof(DeviceIdentify)); - if (ret != EOK) { - SECURITY_LOG_ERROR("memcpy failed"); - FREE(queueData); - return; - } - ret = (uint32_t)memcpy_s(queueData->msgData, msgLen, msg, msgLen); - if (ret != EOK) { - SECURITY_LOG_ERROR("memcpy failed"); - FREE(queueData); return; } - queueData->msgLen = msgLen; - ret = QueueWork(queue, ProcessSocketMessageReceived, (uint8_t *)queueData, queueDataLen); + uint32_t ret = QueueWork(queue, ProcessSocketMessageReceived, (uint8_t *)queueData, queueDataLen); if (ret != WORK_QUEUE_OK) { SECURITY_LOG_ERROR("QueueWork failed, ret is %{public}u", ret); FREE(queueData); diff --git a/baselib/msglib/src/utils/messenger_utils.c b/baselib/msglib/src/utils/messenger_utils.c new file mode 100644 index 0000000000000000000000000000000000000000..b15d9b53eafffac879c97ca31ceff79af1266bc2 --- /dev/null +++ b/baselib/msglib/src/utils/messenger_utils.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 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. + */ + +#include + +#include "securec.h" + +#include "messenger_utils.h" +#include "utils_log.h" +#include "utils_mem.h" + +QueueMsgData *CreateQueueMsgData(const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen, + uint32_t *queueDataLen) +{ + if (devId == NULL || msg == NULL || msgLen == 0 || queueDataLen == NULL) { + return NULL; + } + + uint32_t dataLen = sizeof(QueueMsgData) + msgLen; + QueueMsgData *queueData = MALLOC(dataLen); + if (queueData == NULL) { + SECURITY_LOG_ERROR("malloc result null"); + return NULL; + } + uint32_t ret = (uint32_t)memcpy_s(&queueData->srcIdentity, sizeof(DeviceIdentify), devId, sizeof(DeviceIdentify)); + if (ret != EOK) { + SECURITY_LOG_ERROR("memcpy failed"); + FREE(queueData); + return NULL; + } + ret = (uint32_t)memcpy_s(queueData->msgData, msgLen, msg, msgLen); + if (ret != EOK) { + SECURITY_LOG_ERROR("memcpy failed"); + FREE(queueData); + return NULL; + } + queueData->msgLen = msgLen; + *queueDataLen = dataLen; + + return queueData; +} diff --git a/baselib/msglib/src/utils/messenger_utils.h b/baselib/msglib/src/utils/messenger_utils.h index 9aed0f0cd5f44802b398f4e36792b9779cc58cae..32ceae327ef20aa5c76f9012a64398d6049ed58a 100644 --- a/baselib/msglib/src/utils/messenger_utils.h +++ b/baselib/msglib/src/utils/messenger_utils.h @@ -31,6 +31,12 @@ extern "C" { #define MAX_TRY_TIMES 512 +typedef struct QueueMsgData { + DeviceIdentify srcIdentity; + uint32_t msgLen; + uint8_t msgData[1]; +} QueueMsgData; + static inline uint32_t MaskDeviceIdentity(const char *deviceId, uint32_t length) { #define MASK_LEN 4U @@ -75,6 +81,9 @@ static inline bool IsSameDevice(const DeviceIdentify *left, const DeviceIdentify return true; } +QueueMsgData *CreateQueueMsgData(const DeviceIdentify *devId, const uint8_t *msg, uint32_t msgLen, + uint32_t *queueDataLen); + #ifdef __cplusplus } #endif