• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "trans_auth_message.h"
17 
18 #include "securec.h"
19 #include "softbus_conn_interface.h"
20 #include "softbus_def.h"
21 #include "softbus_errcode.h"
22 #include "softbus_log.h"
23 #include "softbus_utils.h"
24 
25 #define CODE_OPEN_AUTH_MSG_CHANNEL 4
26 
TransAuthChannelMsgPack(cJSON * msg,const AppInfo * appInfo)27 NO_SANITIZE("cfi") int32_t TransAuthChannelMsgPack(cJSON *msg, const AppInfo *appInfo)
28 {
29     if (appInfo == NULL || msg == NULL) {
30         return SOFTBUS_INVALID_PARAM;
31     }
32     if (appInfo->reqId[0] == '\0') {
33         if (GenerateRandomStr((char *)(appInfo->reqId), REQ_ID_SIZE_MAX) != SOFTBUS_OK) {
34             return SOFTBUS_ERR;
35         }
36     }
37     if (!AddNumberToJsonObject(msg, "CODE", CODE_OPEN_AUTH_MSG_CHANNEL) ||
38         !AddStringToJsonObject(msg, "DEVICE_ID", appInfo->myData.deviceId) ||
39         !AddStringToJsonObject(msg, "PKG_NAME", appInfo->myData.pkgName) ||
40         !AddStringToJsonObject(msg, "SRC_BUS_NAME", appInfo->myData.sessionName) ||
41         !AddStringToJsonObject(msg, "DST_BUS_NAME", appInfo->peerData.sessionName) ||
42         !AddStringToJsonObject(msg, "REQ_ID", appInfo->reqId) ||
43         !AddNumberToJsonObject(msg, "MTU_SIZE", appInfo->myData.dataConfig)) {
44         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransAuthChannelMsgPack failed");
45         return SOFTBUS_PARSE_JSON_ERR;
46     }
47     return SOFTBUS_OK;
48 }
49 
TransAuthChannelMsgUnpack(const char * msg,AppInfo * appInfo,int32_t len)50 NO_SANITIZE("cfi") int32_t TransAuthChannelMsgUnpack(const char *msg, AppInfo *appInfo, int32_t len)
51 {
52     if (msg == NULL || appInfo == NULL) {
53         return SOFTBUS_INVALID_PARAM;
54     }
55 
56     cJSON *obj = cJSON_ParseWithLength(msg, len);
57     if (obj == NULL) {
58         return SOFTBUS_PARSE_JSON_ERR;
59     }
60     int32_t errcode;
61     if (GetJsonObjectNumberItem(obj, "ERR_CODE", &errcode)) {
62         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "unpack errcode:%d", errcode);
63         cJSON_Delete(obj);
64         return SOFTBUS_ERR;
65     }
66     if (!GetJsonObjectStringItem(obj, "DEVICE_ID", appInfo->peerData.deviceId, DEVICE_ID_SIZE_MAX) ||
67         !GetJsonObjectStringItem(obj, "PKG_NAME", appInfo->peerData.pkgName, PKG_NAME_SIZE_MAX) ||
68         !GetJsonObjectStringItem(obj, "SRC_BUS_NAME", appInfo->peerData.sessionName, SESSION_NAME_SIZE_MAX) ||
69         !GetJsonObjectStringItem(obj, "DST_BUS_NAME", appInfo->myData.sessionName, SESSION_NAME_SIZE_MAX) ||
70         !GetJsonObjectStringItem(obj, "REQ_ID", appInfo->reqId, REQ_ID_SIZE_MAX)) {
71         cJSON_Delete(obj);
72         return SOFTBUS_PARSE_JSON_ERR;
73     }
74     if (!GetJsonObjectNumberItem(obj, "MTU_SIZE", (int32_t *)&(appInfo->peerData.dataConfig))) {
75         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "peer dataconfig is null.");
76     }
77     cJSON_Delete(obj);
78     return SOFTBUS_OK;
79 }
80 
TransAuthChannelErrorPack(int32_t errcode,const char * errMsg,char * cJsonStr,int32_t maxLen)81 NO_SANITIZE("cfi") int32_t TransAuthChannelErrorPack(int32_t errcode, const char *errMsg, char *cJsonStr,
82     int32_t maxLen)
83 {
84     (void)maxLen;
85     if (errMsg == NULL || cJsonStr == NULL) {
86         return SOFTBUS_INVALID_PARAM;
87     }
88 
89     cJSON *obj = cJSON_CreateObject();
90     if (obj == NULL) {
91         return SOFTBUS_MALLOC_ERR;
92     }
93     if (!AddNumberToJsonObject(obj, "CODE", CODE_OPEN_AUTH_MSG_CHANNEL) ||
94         !AddNumberToJsonObject(obj, "ERR_CODE", errcode) ||
95         !AddStringToJsonObject(obj, "ERR_DESC", errMsg)) {
96         cJSON_Delete(obj);
97         return SOFTBUS_PARSE_JSON_ERR;
98     }
99     char *data = cJSON_PrintUnformatted(obj);
100     if (data == NULL) {
101         cJSON_Delete(obj);
102         return SOFTBUS_PARSE_JSON_ERR;
103     }
104     if (memcpy_s(cJsonStr, ERR_MSG_MAX_LEN, data, strlen(data)) != EOK) {
105         cJSON_Delete(obj);
106         cJSON_free(data);
107         return SOFTBUS_MEM_ERR;
108     }
109     cJSON_Delete(obj);
110     cJSON_free(data);
111     return SOFTBUS_OK;
112 }