1 /*
2  * Copyright (c) 2021-2023 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_def.h"
20 #include "softbus_errcode.h"
21 #include "softbus_utils.h"
22 #include "trans_log.h"
23 #include "lnn_lane_interface.h"
24 
25 #define CODE_OPEN_AUTH_MSG_CHANNEL 4
26 
TransAuthChannelMsgPack(cJSON * msg,const AppInfo * appInfo)27 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         int32_t ret = GenerateRandomStr((char *)(appInfo->reqId), REQ_ID_SIZE_MAX);
34         if (ret != SOFTBUS_OK) {
35             TRANS_LOGE(TRANS_SVC, "GenerateRandomStr fail");
36             return ret;
37         }
38     }
39     if (!AddNumberToJsonObject(msg, "CODE", CODE_OPEN_AUTH_MSG_CHANNEL) ||
40         !AddStringToJsonObject(msg, "DEVICE_ID", appInfo->myData.deviceId) ||
41         !AddStringToJsonObject(msg, "PEER_NETWORK_ID", appInfo->peerNetWorkId) ||
42         !AddStringToJsonObject(msg, "PKG_NAME", appInfo->myData.pkgName) ||
43         !AddStringToJsonObject(msg, "SRC_BUS_NAME", appInfo->myData.sessionName) ||
44         !AddStringToJsonObject(msg, "DST_BUS_NAME", appInfo->peerData.sessionName) ||
45         !AddStringToJsonObject(msg, "REQ_ID", appInfo->reqId) ||
46         !AddNumberToJsonObject(msg, "MTU_SIZE", (int32_t)appInfo->myData.dataConfig) ||
47         !AddNumberToJsonObject(msg, "ROUTE_TYPE", (int32_t)appInfo->routeType)) {
48         TRANS_LOGE(TRANS_SVC, "failed");
49         return SOFTBUS_CREATE_JSON_ERR;
50     }
51     if (appInfo->linkType == LANE_HML_RAW) {
52         if (!AddNumberToJsonObject(msg, "LANE_LINK_TYPE", appInfo->linkType) ||
53             !AddStringToJsonObject(msg, "LOCAL_HML_RAW_IP", appInfo->myData.addr) ||
54             !AddStringToJsonObject(msg, "PEER_HML_RAW_IP", appInfo->peerData.addr)) {
55             TRANS_LOGE(TRANS_SVC, "add linkType and ip failed");
56             return SOFTBUS_PARSE_JSON_ERR;
57         }
58     }
59     return SOFTBUS_OK;
60 }
61 
TransAuthChannelMsgUnpack(const char * msg,AppInfo * appInfo,int32_t len)62 int32_t TransAuthChannelMsgUnpack(const char *msg, AppInfo *appInfo, int32_t len)
63 {
64     if (msg == NULL || appInfo == NULL) {
65         return SOFTBUS_INVALID_PARAM;
66     }
67 
68     cJSON *obj = cJSON_ParseWithLength(msg, len);
69     if (obj == NULL) {
70         TRANS_LOGE(TRANS_SVC, "parse json failed.");
71         return SOFTBUS_PARSE_JSON_ERR;
72     }
73     int32_t errcode;
74     if (GetJsonObjectInt32Item(obj, "ERR_CODE", &errcode)) {
75         TRANS_LOGE(TRANS_SVC, "peer failed: errcode=%{public}d.", errcode);
76         cJSON_Delete(obj);
77         return errcode;
78     }
79     if (!GetJsonObjectStringItem(obj, "DEVICE_ID", appInfo->peerData.deviceId, DEVICE_ID_SIZE_MAX) ||
80         !GetJsonObjectStringItem(obj, "PKG_NAME", appInfo->peerData.pkgName, PKG_NAME_SIZE_MAX) ||
81         !GetJsonObjectStringItem(obj, "SRC_BUS_NAME", appInfo->peerData.sessionName, SESSION_NAME_SIZE_MAX) ||
82         !GetJsonObjectStringItem(obj, "DST_BUS_NAME", appInfo->myData.sessionName, SESSION_NAME_SIZE_MAX) ||
83         !GetJsonObjectStringItem(obj, "REQ_ID", appInfo->reqId, REQ_ID_SIZE_MAX)) {
84         cJSON_Delete(obj);
85         TRANS_LOGE(TRANS_SVC, "get json object failed.");
86         return SOFTBUS_PARSE_JSON_ERR;
87     }
88     if (!GetJsonObjectStringItem(obj, "PEER_NETWORK_ID", appInfo->peerNetWorkId, DEVICE_ID_SIZE_MAX)) {
89         TRANS_LOGW(TRANS_SVC, "peerNetWorkId is null.");
90     }
91     if (!GetJsonObjectNumberItem(obj, "MTU_SIZE", (int32_t *)&(appInfo->peerData.dataConfig))) {
92         TRANS_LOGW(TRANS_SVC, "peer dataconfig is null.");
93     }
94     if (!GetJsonObjectNumberItem(obj, "ROUTE_TYPE", (int32_t *)&(appInfo->routeType))) {
95         TRANS_LOGW(TRANS_SVC, "routeType is null.");
96     }
97     if (GetJsonObjectNumberItem(obj, "LANE_LINK_TYPE", (int32_t *)&(appInfo->linkType))
98         && (appInfo->linkType == LANE_HML_RAW)) {
99         if (!GetJsonObjectStringItem(obj, "LOCAL_HML_RAW_IP", appInfo->peerData.addr, IP_LEN) ||
100             !GetJsonObjectStringItem(obj, "PEER_HML_RAW_IP", appInfo->myData.addr, IP_LEN)) {
101             TRANS_LOGE(TRANS_SVC, "get linkType and ip failed");
102             cJSON_Delete(obj);
103             return SOFTBUS_PARSE_JSON_ERR;
104         }
105     }
106     cJSON_Delete(obj);
107     return SOFTBUS_OK;
108 }
109 
TransAuthChannelErrorPack(int32_t errcode,const char * errMsg,char * cJsonStr,int32_t maxLen)110 int32_t TransAuthChannelErrorPack(int32_t errcode, const char *errMsg, char *cJsonStr, int32_t maxLen)
111 {
112     if (errMsg == NULL || cJsonStr == NULL) {
113         return SOFTBUS_INVALID_PARAM;
114     }
115 
116     cJSON *obj = cJSON_CreateObject();
117     if (obj == NULL) {
118         return SOFTBUS_MALLOC_ERR;
119     }
120     if (!AddNumberToJsonObject(obj, "CODE", CODE_OPEN_AUTH_MSG_CHANNEL) ||
121         !AddNumberToJsonObject(obj, "ERR_CODE", errcode) ||
122         !AddStringToJsonObject(obj, "ERR_DESC", errMsg)) {
123         cJSON_Delete(obj);
124         TRANS_LOGE(TRANS_SVC, "add json object failed.");
125         return SOFTBUS_CREATE_JSON_ERR;
126     }
127     char *data = cJSON_PrintUnformatted(obj);
128     cJSON_Delete(obj);
129     if (data == NULL) {
130         TRANS_LOGE(TRANS_SVC, "convert json to string failed.");
131         return SOFTBUS_CREATE_JSON_ERR;
132     }
133     if (memcpy_s(cJsonStr, maxLen, data, strlen(data)) != EOK) {
134         cJSON_free(data);
135         return SOFTBUS_MEM_ERR;
136     }
137     cJSON_free(data);
138     return SOFTBUS_OK;
139 }
140