• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_error_code.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 
PackUsbLinkTypeMsg(cJSON * msg,const AppInfo * appInfo)27 static int32_t PackUsbLinkTypeMsg(cJSON *msg, const AppInfo *appInfo)
28 {
29     if (appInfo->linkType == LANE_USB) {
30         if (!AddNumberToJsonObject(msg, "LANE_LINK_TYPE", appInfo->linkType)) {
31             TRANS_LOGE(TRANS_SVC, "add usb linkType failed");
32             return SOFTBUS_CREATE_JSON_ERR;
33         }
34     }
35     return SOFTBUS_OK;
36 }
37 
TransAuthChannelMsgPack(cJSON * msg,const AppInfo * appInfo)38 int32_t TransAuthChannelMsgPack(cJSON *msg, const AppInfo *appInfo)
39 {
40     if (appInfo == NULL || msg == NULL) {
41         return SOFTBUS_INVALID_PARAM;
42     }
43     if (appInfo->reqId[0] == '\0') {
44         int32_t ret = GenerateRandomStr((char *)(appInfo->reqId), REQ_ID_SIZE_MAX);
45         if (ret != SOFTBUS_OK) {
46             TRANS_LOGE(TRANS_SVC, "GenerateRandomStr fail");
47             return ret;
48         }
49     }
50     if (!AddNumberToJsonObject(msg, "CODE", CODE_OPEN_AUTH_MSG_CHANNEL) ||
51         !AddStringToJsonObject(msg, "DEVICE_ID", appInfo->myData.deviceId) ||
52         !AddStringToJsonObject(msg, "PEER_NETWORK_ID", appInfo->peerNetWorkId) ||
53         !AddStringToJsonObject(msg, "PKG_NAME", appInfo->myData.pkgName) ||
54         !AddStringToJsonObject(msg, "SRC_BUS_NAME", appInfo->myData.sessionName) ||
55         !AddStringToJsonObject(msg, "DST_BUS_NAME", appInfo->peerData.sessionName) ||
56         !AddStringToJsonObject(msg, "REQ_ID", appInfo->reqId) ||
57         !AddNumberToJsonObject(msg, "MTU_SIZE", (int32_t)appInfo->myData.dataConfig) ||
58         !AddNumberToJsonObject(msg, "API_VERSION", (int32_t)appInfo->myData.apiVersion) ||
59         !AddNumberToJsonObject(msg, "ROUTE_TYPE", (int32_t)appInfo->routeType)) {
60         TRANS_LOGE(TRANS_SVC, "failed");
61         return SOFTBUS_CREATE_JSON_ERR;
62     }
63     char compatibleAddr[IP_LEN] = { 0 };
64     if (appInfo->linkType == LANE_HML_RAW) {
65         if (!AddNumberToJsonObject(msg, "LANE_LINK_TYPE", appInfo->linkType) ||
66             !AddStringToJsonObject(msg, "LOCAL_HML_RAW_IP", compatibleAddr) ||
67             !AddStringToJsonObject(msg, "PEER_HML_RAW_IP", compatibleAddr)) {
68             TRANS_LOGE(TRANS_SVC, "add linkType and ip failed");
69             return SOFTBUS_CREATE_JSON_ERR;
70         }
71     }
72     return PackUsbLinkTypeMsg(msg, appInfo);
73 }
74 
TransAuthChannelMsgUnpack(const char * msg,AppInfo * appInfo,int32_t len)75 int32_t TransAuthChannelMsgUnpack(const char *msg, AppInfo *appInfo, int32_t len)
76 {
77     if (msg == NULL || appInfo == NULL) {
78         return SOFTBUS_INVALID_PARAM;
79     }
80 
81     cJSON *obj = cJSON_ParseWithLength(msg, len);
82     if (obj == NULL) {
83         TRANS_LOGE(TRANS_SVC, "parse json failed.");
84         return SOFTBUS_PARSE_JSON_ERR;
85     }
86     int32_t errcode;
87     if (GetJsonObjectInt32Item(obj, "ERR_CODE", &errcode)) {
88         TRANS_LOGE(TRANS_SVC, "peer failed: errcode=%{public}d.", errcode);
89         cJSON_Delete(obj);
90         return errcode;
91     }
92     if (!GetJsonObjectStringItem(obj, "DEVICE_ID", appInfo->peerData.deviceId, DEVICE_ID_SIZE_MAX) ||
93         !GetJsonObjectStringItem(obj, "PKG_NAME", appInfo->peerData.pkgName, PKG_NAME_SIZE_MAX) ||
94         !GetJsonObjectStringItem(obj, "SRC_BUS_NAME", appInfo->peerData.sessionName, SESSION_NAME_SIZE_MAX) ||
95         !GetJsonObjectStringItem(obj, "DST_BUS_NAME", appInfo->myData.sessionName, SESSION_NAME_SIZE_MAX) ||
96         !GetJsonObjectStringItem(obj, "REQ_ID", appInfo->reqId, REQ_ID_SIZE_MAX)) {
97         cJSON_Delete(obj);
98         TRANS_LOGE(TRANS_SVC, "get json object failed.");
99         return SOFTBUS_PARSE_JSON_ERR;
100     }
101     if (!GetJsonObjectStringItem(obj, "PEER_NETWORK_ID", appInfo->peerNetWorkId, DEVICE_ID_SIZE_MAX)) {
102         TRANS_LOGW(TRANS_SVC, "peerNetWorkId is null.");
103     }
104     if (!GetJsonObjectNumberItem(obj, "MTU_SIZE", (int32_t *)&(appInfo->peerData.dataConfig))) {
105         TRANS_LOGW(TRANS_SVC, "peer dataconfig is null.");
106     }
107     if (!GetJsonObjectNumberItem(obj, "ROUTE_TYPE", (int32_t *)&(appInfo->routeType))) {
108         TRANS_LOGW(TRANS_SVC, "routeType is null.");
109     }
110     if (!GetJsonObjectNumberItem(obj, "API_VERSION", (int32_t *)&appInfo->myData.apiVersion)) {
111         TRANS_LOGW(TRANS_SVC, "apiVersion is null.");
112     }
113     if (!GetJsonObjectNumberItem(obj, "LANE_LINK_TYPE", (int32_t *)&(appInfo->linkType)) ||
114         (appInfo->linkType != LANE_HML_RAW)) {
115         TRANS_LOGW(TRANS_SVC, "get linkType failed");
116     }
117     cJSON_Delete(obj);
118     return SOFTBUS_OK;
119 }
120 
TransAuthChannelErrorPack(int32_t errcode,const char * errMsg,char * cJsonStr,int32_t maxLen)121 int32_t TransAuthChannelErrorPack(int32_t errcode, const char *errMsg, char *cJsonStr, int32_t maxLen)
122 {
123     if (errMsg == NULL || cJsonStr == NULL) {
124         return SOFTBUS_INVALID_PARAM;
125     }
126 
127     cJSON *obj = cJSON_CreateObject();
128     if (obj == NULL) {
129         return SOFTBUS_MALLOC_ERR;
130     }
131     if (!AddNumberToJsonObject(obj, "CODE", CODE_OPEN_AUTH_MSG_CHANNEL) ||
132         !AddNumberToJsonObject(obj, "ERR_CODE", errcode) ||
133         !AddStringToJsonObject(obj, "ERR_DESC", errMsg)) {
134         cJSON_Delete(obj);
135         TRANS_LOGE(TRANS_SVC, "add json object failed.");
136         return SOFTBUS_CREATE_JSON_ERR;
137     }
138     char *data = cJSON_PrintUnformatted(obj);
139     cJSON_Delete(obj);
140     if (data == NULL) {
141         TRANS_LOGE(TRANS_SVC, "convert json to string failed.");
142         return SOFTBUS_CREATE_JSON_ERR;
143     }
144     if (memcpy_s(cJsonStr, maxLen, data, strlen(data)) != EOK) {
145         cJSON_free(data);
146         return SOFTBUS_MEM_ERR;
147     }
148     cJSON_free(data);
149     return SOFTBUS_OK;
150 }
151