1 /*
2 * Copyright (c) 2022 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 #include "trans_tcp_direct_json.h"
16
17 #include "softbus_def.h"
18 #include "softbus_errcode.h"
19 #include "softbus_json_utils.h"
20 #include "softbus_proxychannel_message.h"
21 #include "trans_log.h"
22 #include "trans_tcp_direct_p2p.h"
23
24 #define MSG_CODE "CODE"
25 #define P2P_IP "P2P_IP"
26 #define P2P_PORT "P2P_PORT"
27 #define PEER_IP "PEER_IP"
28 #define ERR_CODE "ERR_CODE"
29 #define ERR_DESC "ERR_DESC"
30
VerifyP2pPackError(int32_t code,int32_t errCode,const char * errDesc)31 char *VerifyP2pPackError(int32_t code, int32_t errCode, const char *errDesc)
32 {
33 if (errDesc == NULL) {
34 TRANS_LOGE(TRANS_CTRL, "param invalid");
35 return NULL;
36 }
37 cJSON *json = cJSON_CreateObject();
38 if (json == NULL) {
39 TRANS_LOGE(TRANS_CTRL, "create object failed");
40 return NULL;
41 }
42 if (!AddNumberToJsonObject(json, MSG_CODE, code) ||
43 !AddNumberToJsonObject(json, ERR_CODE, errCode) ||
44 !AddStringToJsonObject(json, ERR_DESC, errDesc)) {
45 cJSON_Delete(json);
46 return NULL;
47 }
48 char *data = cJSON_PrintUnformatted(json);
49 cJSON_Delete(json);
50 return data;
51 }
52
VerifyP2pPack(const char * myIp,int32_t myPort,const char * peerIp)53 char *VerifyP2pPack(const char *myIp, int32_t myPort, const char *peerIp)
54 {
55 if (myIp == NULL || myPort <= 0) {
56 TRANS_LOGE(TRANS_CTRL, "param invalid");
57 return NULL;
58 }
59 cJSON *json = cJSON_CreateObject();
60 if (json == NULL) {
61 TRANS_LOGE(TRANS_CTRL, "create object failed");
62 return NULL;
63 }
64 if (peerIp != NULL) {
65 AddStringToJsonObject(json, PEER_IP, peerIp);
66 }
67 if (!AddNumberToJsonObject(json, MSG_CODE, CODE_VERIFY_P2P) ||
68 !AddStringToJsonObject(json, P2P_IP, myIp) ||
69 !AddNumberToJsonObject(json, P2P_PORT, myPort)) {
70 cJSON_Delete(json);
71 TRANS_LOGE(TRANS_CTRL, "add json object failed");
72 return NULL;
73 }
74 char *data = cJSON_PrintUnformatted(json);
75 cJSON_Delete(json);
76 return data;
77 }
78
VerifyP2pUnPack(const cJSON * json,char * ip,uint32_t ipLen,int32_t * port)79 int32_t VerifyP2pUnPack(const cJSON *json, char *ip, uint32_t ipLen, int32_t *port)
80 {
81 if (json == NULL || ip == NULL || port == NULL) {
82 TRANS_LOGE(TRANS_CTRL, "param invalid");
83 return SOFTBUS_INVALID_PARAM;
84 }
85 int32_t errCode = 0;
86 if (GetJsonObjectInt32Item(json, ERR_CODE, &errCode)) {
87 TRANS_LOGE(TRANS_CTRL, "peer proc failed: errCode=%{public}d", errCode);
88 return errCode;
89 }
90 if (!GetJsonObjectNumberItem(json, P2P_PORT, port) ||
91 !GetJsonObjectStringItem(json, P2P_IP, ip, ipLen)) {
92 TRANS_LOGE(TRANS_INIT, "VerifyP2pUnPack get obj fail");
93 return SOFTBUS_PARSE_JSON_ERR;
94 }
95 return SOFTBUS_OK;
96 }
97