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