1 /*
2 * Copyright (c) 2022-2024 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_tcp_direct_p2p.h"
17
18 #include <securec.h>
19
20 #include "cJSON.h"
21
22 #include "auth_interface.h"
23 #include "lnn_lane_link.h"
24 #include "softbus_adapter_hitrace.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_adapter_socket.h"
27 #include "softbus_base_listener.h"
28 #include "softbus_conn_common.h"
29 #include "softbus_conn_interface.h"
30 #include "softbus_def.h"
31 #include "softbus_errcode.h"
32 #include "softbus_proxychannel_manager.h"
33 #include "softbus_proxychannel_pipeline.h"
34 #include "softbus_socket.h"
35 #include "trans_lane_pending_ctl.h"
36 #include "trans_log.h"
37 #include "trans_tcp_direct_json.h"
38 #include "trans_tcp_direct_listener.h"
39 #include "trans_tcp_direct_message.h"
40 #include "trans_tcp_direct_sessionconn.h"
41 #include "wifi_direct_manager.h"
42
43 #define ID_OFFSET (1)
44 #define P2P_VERIFY_REQUEST 0
45 #define P2P_VERIFY_REPLY 1
46
47 static int32_t g_p2pSessionPort = -1;
48 static char g_p2pSessionIp[IP_LEN] = {0};
49 static SoftBusMutex g_p2pLock;
50 static SoftBusList *g_hmlListenerList = NULL;
51
StartNewP2pListener(const char * ip,int32_t * port)52 static int32_t StartNewP2pListener(const char *ip, int32_t *port)
53 {
54 int32_t listenerPort;
55 LocalListenerInfo info;
56 info.type = CONNECT_P2P;
57 (void)memset_s(info.socketOption.addr, sizeof(info.socketOption.addr), 0, sizeof(info.socketOption.addr));
58 info.socketOption.port = *port;
59 info.socketOption.protocol = LNN_PROTOCOL_IP;
60 info.socketOption.moduleId = DIRECT_CHANNEL_SERVER_P2P;
61
62 if (strcpy_s(info.socketOption.addr, sizeof(info.socketOption.addr), ip) != EOK) {
63 TRANS_LOGE(TRANS_CTRL, "copy addr failed!");
64 return SOFTBUS_STRCPY_ERR;
65 }
66
67 listenerPort = TransTdcStartSessionListener(DIRECT_CHANNEL_SERVER_P2P, &info);
68 if (listenerPort < 0) {
69 TRANS_LOGE(TRANS_CTRL, "start listener fail");
70 return SOFTBUS_TRANS_TDC_START_SESSION_LISTENER_FAILED;
71 }
72 *port = listenerPort;
73 g_p2pSessionPort = *port;
74 return SOFTBUS_OK;
75 }
76
StartNewHmlListener(const char * ip,int32_t * port,ListenerModule * moudleType)77 static int32_t StartNewHmlListener(const char *ip, int32_t *port, ListenerModule *moudleType)
78 {
79 int32_t listenerPort = 0;
80 LocalListenerInfo info;
81 info.type = CONNECT_HML;
82 (void)memset_s(info.socketOption.addr, sizeof(info.socketOption.addr), 0, sizeof(info.socketOption.addr));
83 info.socketOption.port = *port;
84 info.socketOption.protocol = LNN_PROTOCOL_IP;
85 if (strcpy_s(info.socketOption.addr, sizeof(info.socketOption.addr), ip) != EOK) {
86 TRANS_LOGE(TRANS_CTRL, "copy addr failed!");
87 return SOFTBUS_STRCPY_ERR;
88 }
89 for (int32_t i = DIRECT_CHANNEL_SERVER_HML_START; i <= DIRECT_CHANNEL_SERVER_HML_END; i++) {
90 info.socketOption.moduleId = (ListenerModule)i;
91 listenerPort = TransTdcStartSessionListener((ListenerModule)i, &info);
92 if (listenerPort >= 0) {
93 *moudleType = (ListenerModule)i;
94 break;
95 }
96 }
97 if (listenerPort < 0) {
98 TRANS_LOGE(TRANS_CTRL, "listenerPort is invalid!");
99 return SOFTBUS_TRANS_TDC_START_SESSION_LISTENER_FAILED;
100 }
101 *port = listenerPort;
102 return SOFTBUS_OK;
103 }
104
DelHmlListenerByMoudle(ListenerModule type)105 static void DelHmlListenerByMoudle(ListenerModule type)
106 {
107 HmlListenerInfo *item = NULL;
108 HmlListenerInfo *nextItem = NULL;
109 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_hmlListenerList->list, HmlListenerInfo, node) {
110 if (item->moudleType == type) {
111 ListDelete(&item->node);
112 TRANS_LOGI(TRANS_CTRL,
113 "del hmlListener port=%{public}d, listenerModule=%{public}d",
114 item->myPort, (int32_t)item->moudleType);
115 SoftBusFree(item);
116 g_hmlListenerList->cnt--;
117 return;
118 }
119 }
120 }
121
StopHmlListener(ListenerModule module)122 void StopHmlListener(ListenerModule module)
123 {
124 if (SoftBusMutexLock(&g_hmlListenerList->lock) != SOFTBUS_OK) {
125 TRANS_LOGE(TRANS_CTRL, "lock fail");
126 return;
127 }
128 if (StopBaseListener(module) != SOFTBUS_OK) {
129 TRANS_LOGE(TRANS_CTRL, "StopHmlListener stop listener fail. module=%{public}d", module);
130 }
131 DelHmlListenerByMoudle(module);
132 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
133 }
134
StopP2pSessionListener()135 void StopP2pSessionListener()
136 {
137 if (g_p2pSessionPort > 0) {
138 if (StopBaseListener(DIRECT_CHANNEL_SERVER_P2P) != SOFTBUS_OK) {
139 TRANS_LOGE(TRANS_CTRL, "stop listener fail");
140 }
141 }
142
143 g_p2pSessionPort = -1;
144 g_p2pSessionIp[0] = '\0';
145 }
146
NotifyP2pSessionConnClear(ListNode * sessionConnList)147 static void NotifyP2pSessionConnClear(ListNode *sessionConnList)
148 {
149 if (sessionConnList == NULL) {
150 return;
151 }
152
153 SessionConn *item = NULL;
154 SessionConn *nextItem = NULL;
155
156 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, sessionConnList, SessionConn, node) {
157 (void)NotifyChannelOpenFailedBySessionConn(item, SOFTBUS_TRANS_NET_STATE_CHANGED);
158 TransSrvDelDataBufNode(item->channelId);
159 SoftBusFree(item);
160 }
161 TRANS_LOGI(TRANS_CTRL, "p2psession conn clear finished");
162 }
163
ClearP2pSessionConn(void)164 static void ClearP2pSessionConn(void)
165 {
166 SessionConn *item = NULL;
167 SessionConn *nextItem = NULL;
168
169 SoftBusList *sessionList = GetSessionConnList();
170 if (sessionList == NULL || GetSessionConnLock() != SOFTBUS_OK) {
171 return;
172 }
173
174 ListNode tempSessionConnList;
175 ListInit(&tempSessionConnList);
176 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &sessionList->list, SessionConn, node) {
177 if (item->status < TCP_DIRECT_CHANNEL_STATUS_CONNECTED && item->appInfo.routeType == WIFI_P2P) {
178 ListDelete(&item->node);
179 TRANS_LOGI(TRANS_CTRL,
180 "clear sessionConn pkgName=%{public}s, pid=%{public}d, status=%{public}u, channelId=%{public}d",
181 item->appInfo.myData.pkgName, item->appInfo.myData.pid, item->status, item->channelId);
182 sessionList->cnt--;
183 ListAdd(&tempSessionConnList, &item->node);
184 }
185 }
186 ReleaseSessionConnLock();
187
188 NotifyP2pSessionConnClear(&tempSessionConnList);
189 }
190
CreatHmlListenerList(void)191 static int32_t CreatHmlListenerList(void)
192 {
193 if (g_hmlListenerList == NULL) {
194 g_hmlListenerList = CreateSoftBusList();
195 if (g_hmlListenerList == NULL) {
196 TRANS_LOGE(TRANS_CTRL, "CreateSoftBusList fail");
197 return SOFTBUS_MALLOC_ERR;
198 }
199 }
200 return SOFTBUS_OK;
201 }
202
GetModuleByHmlIp(const char * ip)203 ListenerModule GetModuleByHmlIp(const char *ip)
204 {
205 HmlListenerInfo *item = NULL;
206 HmlListenerInfo *nextItem = NULL;
207 if (SoftBusMutexLock(&g_hmlListenerList->lock) != SOFTBUS_OK) {
208 TRANS_LOGE(TRANS_CTRL, "lock fail");
209 return UNUSE_BUTT;
210 }
211 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_hmlListenerList->list, HmlListenerInfo, node) {
212 if (strncmp(item->myIp, ip, IP_LEN) == 0) {
213 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
214 return item->moudleType;
215 }
216 }
217 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
218 return UNUSE_BUTT;
219 }
220
ClearHmlListenerByUuid(const char * peerUuid)221 void ClearHmlListenerByUuid(const char *peerUuid)
222 {
223 if (peerUuid == NULL) {
224 TRANS_LOGE(TRANS_CTRL, "peerUuid is null.");
225 return;
226 }
227 HmlListenerInfo *item = NULL;
228 HmlListenerInfo *nextItem = NULL;
229 if (SoftBusMutexLock(&g_hmlListenerList->lock) != SOFTBUS_OK) {
230 TRANS_LOGE(TRANS_CTRL, "lock fail");
231 return;
232 }
233 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_hmlListenerList->list, HmlListenerInfo, node) {
234 if (strncmp(item->peerUuid, peerUuid, UUID_BUF_LEN) == 0) {
235 int32_t module = item->moudleType; // item will free in StopHmlListener
236 StopHmlListener(item->moudleType);
237 TRANS_LOGI(TRANS_SVC, "StopHmlListener moudle=%{public}d succ", module);
238 }
239 }
240 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
241 return;
242 }
243
AnonymizeLogHmlListenerInfo(const char * ip,const char * peerUuid)244 static void AnonymizeLogHmlListenerInfo(const char *ip, const char *peerUuid)
245 {
246 char *tmpIp = NULL;
247 char *tmpUuid = NULL;
248 Anonymize(ip, &tmpIp);
249 Anonymize(peerUuid, &tmpUuid);
250 TRANS_LOGI(TRANS_CTRL,
251 "StartHmlListener: ip=%{public}s, peerUuid=%{public}s.", AnonymizeWrapper(tmpIp), AnonymizeWrapper(tmpUuid));
252 AnonymizeFree(tmpIp);
253 AnonymizeFree(tmpUuid);
254 }
255
StartHmlListener(const char * ip,int32_t * port,const char * peerUuid)256 static int32_t StartHmlListener(const char *ip, int32_t *port, const char *peerUuid)
257 {
258 AnonymizeLogHmlListenerInfo(ip, peerUuid);
259 if (g_hmlListenerList == NULL) {
260 TRANS_LOGE(TRANS_CTRL, "hmlListenerList not init");
261 return SOFTBUS_NO_INIT;
262 }
263 HmlListenerInfo *item = NULL;
264 HmlListenerInfo *nextItem = NULL;
265 if (SoftBusMutexLock(&g_hmlListenerList->lock) != SOFTBUS_OK) {
266 TRANS_LOGE(TRANS_CTRL, "lock fail");
267 return SOFTBUS_LOCK_ERR;
268 }
269 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_hmlListenerList->list, HmlListenerInfo, node) {
270 if (strncmp(item->myIp, ip, IP_LEN) == 0 && strncmp(item->peerUuid, peerUuid, UUID_BUF_LEN) == 0) {
271 *port = item->myPort;
272 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
273 TRANS_LOGI(TRANS_CTRL, "succ, port=%{public}d", *port);
274 return SOFTBUS_OK;
275 }
276 }
277 ListenerModule moudleType = UNUSE_BUTT;
278 int32_t ret = StartNewHmlListener(ip, port, &moudleType);
279 if (ret != SOFTBUS_OK) {
280 TRANS_LOGE(TRANS_CTRL, "create new listener fail");
281 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
282 return ret;
283 }
284 item = (HmlListenerInfo *)SoftBusCalloc(sizeof(HmlListenerInfo));
285 if (item == NULL) {
286 StopHmlListener(moudleType);
287 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
288 TRANS_LOGE(TRANS_CTRL, "HmlListenerInfo malloc fail");
289 return SOFTBUS_MALLOC_ERR;
290 }
291 item->myPort = *port;
292 item->moudleType = moudleType;
293 if (strncpy_s(item->myIp, IP_LEN, ip, IP_LEN) != EOK ||
294 strncpy_s(item->peerUuid, UUID_BUF_LEN, peerUuid, UUID_BUF_LEN) != EOK) {
295 TRANS_LOGE(TRANS_CTRL, "HmlListenerInfo copy ip or peer uuid failed.");
296 SoftBusFree(item);
297 StopHmlListener(moudleType);
298 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
299 return SOFTBUS_MEM_ERR;
300 }
301 ListAdd(&(g_hmlListenerList->list), &(item->node));
302 g_hmlListenerList->cnt++;
303 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
304 TRANS_LOGI(TRANS_CTRL, "StartHmlListener succ, port=%{public}d", *port);
305 return SOFTBUS_OK;
306 }
307
AnonymizeIp(const char * ip,char * sessionIp,int32_t port)308 static void AnonymizeIp(const char *ip, char *sessionIp, int32_t port)
309 {
310 char *temp = NULL;
311 char *anonyP2pIp = NULL;
312 Anonymize(ip, &temp);
313 Anonymize(sessionIp, &anonyP2pIp);
314 TRANS_LOGE(TRANS_CTRL, "param invalid g_p2pSessionPort=%{public}d, ip=%{public}s, g_p2pSessionIp=%{public}s",
315 port, temp, anonyP2pIp);
316 AnonymizeFree(temp);
317 AnonymizeFree(anonyP2pIp);
318 }
319
StartP2pListener(const char * ip,int32_t * port)320 static int32_t StartP2pListener(const char *ip, int32_t *port)
321 {
322 if (ip == NULL) {
323 TRANS_LOGE(TRANS_CTRL, "ip is null");
324 return SOFTBUS_INVALID_PARAM;
325 }
326 if (SoftBusMutexLock(&g_p2pLock) != SOFTBUS_OK) {
327 TRANS_LOGE(TRANS_CTRL, "lock failed");
328 return SOFTBUS_LOCK_ERR;
329 }
330 if (g_p2pSessionPort > 0 && strcmp(ip, g_p2pSessionIp) != 0) {
331 AnonymizeIp(ip, g_p2pSessionIp, g_p2pSessionPort);
332 ClearP2pSessionConn();
333 StopP2pSessionListener();
334 }
335 if (g_p2pSessionPort > 0) {
336 *port = g_p2pSessionPort;
337 TRANS_LOGI(TRANS_CTRL, "port=%{public}d", *port);
338 (void)SoftBusMutexUnlock(&g_p2pLock);
339 return SOFTBUS_OK;
340 }
341
342 int32_t ret = StartNewP2pListener(ip, port);
343 if (ret != SOFTBUS_OK) {
344 TRANS_LOGE(TRANS_CTRL, "start new listener fail");
345 (void)SoftBusMutexUnlock(&g_p2pLock);
346 return ret;
347 }
348
349 g_p2pSessionPort = *port;
350 if (strcpy_s(g_p2pSessionIp, sizeof(g_p2pSessionIp), ip) != EOK) {
351 TRANS_LOGE(TRANS_CTRL, "strcpy_s fail");
352 StopP2pSessionListener();
353 (void)SoftBusMutexUnlock(&g_p2pLock);
354 return SOFTBUS_STRCPY_ERR;
355 }
356 (void)SoftBusMutexUnlock(&g_p2pLock);
357 TRANS_LOGI(TRANS_CTRL, "end: port=%{public}d", *port);
358 return SOFTBUS_OK;
359 }
360
OnChannelOpenFail(int32_t channelId,int32_t errCode)361 static void OnChannelOpenFail(int32_t channelId, int32_t errCode)
362 {
363 TRANS_LOGW(TRANS_CTRL, "channelId=%{public}d", channelId);
364 NotifyChannelOpenFailed(channelId, errCode);
365 TransDelSessionConnById(channelId);
366 TransSrvDelDataBufNode(channelId);
367 TRANS_LOGW(TRANS_CTRL, "ok");
368 }
369
SendAuthData(AuthHandle authHandle,int32_t module,int32_t flag,int64_t seq,const char * data)370 static int32_t SendAuthData(AuthHandle authHandle, int32_t module, int32_t flag, int64_t seq, const char *data)
371 {
372 TRANS_LOGI(TRANS_CTRL,
373 "SendAuthData: authId=%{public}" PRId64 ", model=%{public}d, flag=%{public}d, seq=%{public}" PRId64,
374 authHandle.authId, module, flag, seq);
375 AuthTransData dataInfo = {
376 .module = module,
377 .flag = flag,
378 .seq = seq,
379 .len = strlen(data) + 1,
380 .data = (const uint8_t *)data,
381 };
382 int32_t ret = AuthPostTransData(authHandle, &dataInfo);
383 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "AuthPostTransData failed.");
384 return SOFTBUS_OK;
385 }
386
VerifyP2p(AuthHandle authHandle,const char * myIp,const char * peerIp,int32_t myPort,int64_t seq)387 static int32_t VerifyP2p(AuthHandle authHandle, const char *myIp, const char *peerIp, int32_t myPort, int64_t seq)
388 {
389 TRANS_LOGI(TRANS_CTRL, "authId=%{public}" PRId64 ", port=%{public}d", authHandle.authId, myPort);
390 char *msg = NULL;
391 int32_t ret;
392 msg = VerifyP2pPack(myIp, myPort, peerIp);
393 if (msg == NULL) {
394 TRANS_LOGE(TRANS_CTRL, "verifyp2p pack fail");
395 return SOFTBUS_PARSE_JSON_ERR;
396 }
397 ret = SendAuthData(authHandle, MODULE_P2P_LISTEN, MSG_FLAG_REQUEST, (int64_t)seq, msg);
398 cJSON_free(msg);
399 if (ret != SOFTBUS_OK) {
400 TRANS_LOGE(TRANS_CTRL, "VerifyP2p send auth data fail");
401 return ret;
402 }
403 return SOFTBUS_OK;
404 }
405
OnAuthConnOpened(uint32_t requestId,AuthHandle authHandle)406 static void OnAuthConnOpened(uint32_t requestId, AuthHandle authHandle)
407 {
408 TRANS_LOGI(TRANS_CTRL, "reqId=%{public}u, authId=%{public}" PRId64,
409 requestId, authHandle.authId);
410 if (authHandle.type < AUTH_LINK_TYPE_WIFI || authHandle.type >= AUTH_LINK_TYPE_MAX) {
411 TRANS_LOGE(TRANS_CTRL, "authHandle type error");
412 return;
413 }
414 int32_t channelId = INVALID_CHANNEL_ID;
415 SessionConn *conn = NULL;
416 char myDataAddr[IP_LEN] = {0};
417 char peerDataAddr[IP_LEN] = {0};
418 int32_t myDataPort = 0;
419 int64_t reqNum = 0;
420 if (GetSessionConnLock() != SOFTBUS_OK) {
421 goto EXIT_ERR;
422 }
423 conn = GetSessionConnByRequestId(requestId);
424 if (conn == NULL) {
425 TRANS_LOGE(TRANS_CTRL, "not find session");
426 ReleaseSessionConnLock();
427 goto EXIT_ERR;
428 }
429 channelId = conn->channelId;
430 conn->authHandle = authHandle;
431 conn->status = TCP_DIRECT_CHANNEL_STATUS_VERIFY_P2P;
432 if (strcpy_s(myDataAddr, sizeof(myDataAddr), conn->appInfo.myData.addr) != EOK ||
433 strcpy_s(peerDataAddr, sizeof(peerDataAddr), conn->appInfo.peerData.addr) != EOK) {
434 TRANS_LOGE(TRANS_CTRL, "strcpy failed.");
435 ReleaseSessionConnLock();
436 goto EXIT_ERR;
437 }
438 myDataPort = conn->appInfo.myData.port;
439 reqNum = conn->req;
440 ReleaseSessionConnLock();
441
442 if (VerifyP2p(authHandle, myDataAddr, peerDataAddr, myDataPort, reqNum) != SOFTBUS_OK) {
443 TRANS_LOGE(TRANS_CTRL, "verify p2p fail");
444 goto EXIT_ERR;
445 }
446 TRANS_LOGI(TRANS_CTRL, "ok");
447 return;
448 EXIT_ERR:
449 if (channelId != INVALID_CHANNEL_ID) {
450 OnChannelOpenFail(channelId, SOFTBUS_TRANS_OPEN_AUTH_CONN_FAILED);
451 }
452 }
453
OnAuthConnOpenFailed(uint32_t requestId,int32_t reason)454 static void OnAuthConnOpenFailed(uint32_t requestId, int32_t reason)
455 {
456 TRANS_LOGW(TRANS_CTRL, "OnAuthConnOpenFailed: reqId=%{public}u, reason=%{public}d", requestId, reason);
457 SessionConn *conn = NULL;
458 int32_t channelId;
459
460 if (GetSessionConnLock() != SOFTBUS_OK) {
461 TRANS_LOGE(TRANS_CTRL, "get session conn lock fail");
462 return;
463 }
464 conn = GetSessionConnByRequestId(requestId);
465 if (conn == NULL) {
466 ReleaseSessionConnLock();
467 TRANS_LOGE(TRANS_CTRL, "get session conn by requestid fail");
468 return;
469 }
470 channelId = conn->channelId;
471 ReleaseSessionConnLock();
472
473 (void)OnChannelOpenFail(channelId, SOFTBUS_TRANS_OPEN_AUTH_CONN_FAILED);
474 TRANS_LOGW(TRANS_CTRL, "ok");
475 }
476
OpenAuthConn(const char * uuid,uint32_t reqId,bool isMeta,ConnectType type)477 static int32_t OpenAuthConn(const char *uuid, uint32_t reqId, bool isMeta, ConnectType type)
478 {
479 TRANS_LOGI(TRANS_CTRL, "reqId=%{public}u", reqId);
480 AuthConnInfo auth;
481 (void)memset_s(&auth, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo));
482 AuthConnCallback cb;
483 (void)memset_s(&cb, sizeof(AuthConnCallback), 0, sizeof(AuthConnCallback));
484 int32_t ret = SOFTBUS_TRANS_OPEN_AUTH_CONN_FAILED;
485 if (type == CONNECT_HML) {
486 TRANS_LOGI(TRANS_CTRL, "get AuthConnInfo, linkType=%{public}d", type);
487 ret = AuthGetHmlConnInfo(uuid, &auth, isMeta);
488 }
489 if (ret != SOFTBUS_OK && type == CONNECT_P2P) {
490 TRANS_LOGI(TRANS_CTRL, "get AuthConnInfo, linkType=%{public}d", type);
491 ret = AuthGetP2pConnInfo(uuid, &auth, isMeta);
492 }
493 if (ret != SOFTBUS_OK) {
494 ret = AuthGetPreferConnInfo(uuid, &auth, isMeta);
495 }
496 cb.onConnOpened = OnAuthConnOpened;
497 cb.onConnOpenFailed = OnAuthConnOpenFailed;
498 if (AuthOpenConn(&auth, reqId, &cb, isMeta) != SOFTBUS_OK) {
499 TRANS_LOGE(TRANS_CTRL, "open auth conn fail");
500 return SOFTBUS_TRANS_OPEN_AUTH_CONN_FAILED;
501 }
502
503 TRANS_LOGI(TRANS_CTRL, "ok");
504 return SOFTBUS_OK;
505 }
506
SendVerifyP2pFailRsp(AuthHandle authHandle,int64_t seq,int32_t code,int32_t errCode,const char * errDesc,bool isAuthLink)507 static void SendVerifyP2pFailRsp(AuthHandle authHandle, int64_t seq,
508 int32_t code, int32_t errCode, const char *errDesc, bool isAuthLink)
509 {
510 char *reply = VerifyP2pPackError(code, errCode, errDesc);
511 TRANS_CHECK_AND_RETURN_LOGE(reply != NULL, TRANS_CTRL, "verifyP2p pack error");
512 if (isAuthLink) {
513 if (SendAuthData(authHandle, MODULE_P2P_LISTEN, MES_FLAG_REPLY, seq, reply) != SOFTBUS_OK) {
514 TRANS_LOGE(TRANS_CTRL, "send auth data fail");
515 }
516 } else {
517 uint32_t strLen = strlen(reply) + 1;
518 char *sendMsg = (char*)SoftBusCalloc(strLen + sizeof(int64_t) + sizeof(int64_t));
519 if (sendMsg == NULL) {
520 TRANS_LOGE(TRANS_CTRL, "softbuscalloc fail");
521 cJSON_free(reply);
522 return;
523 }
524 *(int64_t*)sendMsg = SoftBusHtoLll((uint64_t)P2P_VERIFY_REPLY);
525 *(int64_t*)(sendMsg + sizeof(int64_t)) = SoftBusHtoLll((uint64_t)seq);
526 if (strcpy_s(sendMsg + sizeof(int64_t) + sizeof(int64_t), strLen, reply) != EOK) {
527 cJSON_free(reply);
528 SoftBusFree(sendMsg);
529 return;
530 }
531 TransProxyPipelineSendMessage(authHandle.authId, (uint8_t *)sendMsg,
532 strLen + sizeof(int64_t) + sizeof(int64_t), MSG_TYPE_IP_PORT_EXCHANGE);
533 SoftBusFree(sendMsg);
534 }
535 cJSON_free(reply);
536 }
537
SendVerifyP2pRsp(AuthHandle authHandle,int32_t module,int32_t flag,int64_t seq,const char * reply,bool isAuthLink)538 static int32_t SendVerifyP2pRsp(AuthHandle authHandle, int32_t module, int32_t flag, int64_t seq,
539 const char *reply, bool isAuthLink)
540 {
541 int32_t ret = SOFTBUS_TRANS_VERIFY_P2P_FAILED;
542 if (isAuthLink) {
543 ret = SendAuthData(authHandle, module, flag, seq, reply);
544 if (ret != SOFTBUS_OK) {
545 TRANS_LOGE(TRANS_CTRL, "send auth data fail");
546 }
547 } else {
548 uint32_t strLen = strlen(reply) + 1;
549 char *sendMsg = (char *)SoftBusCalloc(strLen + sizeof(int64_t) + sizeof(int64_t));
550 TRANS_CHECK_AND_RETURN_RET_LOGE(sendMsg != NULL, SOFTBUS_MALLOC_ERR, TRANS_CTRL, "calloc sendMsg failed");
551 *(int64_t *)sendMsg = SoftBusHtoLll((uint64_t)P2P_VERIFY_REPLY);
552 *(int64_t *)(sendMsg + sizeof(int64_t)) = SoftBusHtoLll((uint64_t)seq);
553 if (strcpy_s(sendMsg + sizeof(int64_t) + sizeof(int64_t), strLen, reply) != EOK) {
554 SoftBusFree(sendMsg);
555 return SOFTBUS_STRCPY_ERR;
556 }
557 ret = TransProxyPipelineSendMessage(authHandle.authId, (uint8_t *)sendMsg,
558 strLen + sizeof(int64_t) + sizeof(int64_t), MSG_TYPE_IP_PORT_EXCHANGE);
559 if (ret != SOFTBUS_OK) {
560 TRANS_LOGE(TRANS_CTRL, "TransProxyPipelineSendMessage fail");
561 }
562 SoftBusFree(sendMsg);
563 }
564 return ret;
565 }
566
OutputAnonymizeIpAddress(const char * myIp,const char * peerIp)567 static void OutputAnonymizeIpAddress(const char *myIp, const char *peerIp)
568 {
569 char anonymizedMyIp[IP_LEN] = { 0 };
570 ConvertAnonymizeIpAddress(anonymizedMyIp, IP_LEN, myIp, IP_LEN);
571 char anonymizedPeerIp[IP_LEN] = { 0 };
572 ConvertAnonymizeIpAddress(anonymizedPeerIp, IP_LEN, peerIp, IP_LEN);
573 TRANS_LOGE(TRANS_CTRL, "StartListener failed, myIp=%{public}s peerIp=%{public}s", anonymizedMyIp, anonymizedPeerIp);
574 }
575
PackAndSendVerifyP2pRsp(const char * myIp,int32_t myPort,int64_t seq,bool isAuthLink,AuthHandle authHandle)576 static int32_t PackAndSendVerifyP2pRsp(const char *myIp, int32_t myPort, int64_t seq, bool isAuthLink,
577 AuthHandle authHandle)
578 {
579 int32_t ret = SOFTBUS_OK;
580 char *reply = VerifyP2pPack(myIp, myPort, NULL);
581 if (reply == NULL) {
582 SendVerifyP2pFailRsp(authHandle, seq, CODE_VERIFY_P2P, ret, "pack reply failed", isAuthLink);
583 return SOFTBUS_PARSE_JSON_ERR;
584 }
585 ret = SendVerifyP2pRsp(authHandle, MODULE_P2P_LISTEN, MES_FLAG_REPLY, seq, reply, isAuthLink);
586 cJSON_free(reply);
587 if (ret != SOFTBUS_OK) {
588 return ret;
589 }
590 return SOFTBUS_OK;
591 }
592
StartHmlListenerByUuid(AuthHandle authHandle,const char * myIp,int32_t * myPort)593 static int32_t StartHmlListenerByUuid(AuthHandle authHandle, const char *myIp, int32_t *myPort)
594 {
595 char peerUuid[UUID_BUF_LEN] = { 0 };
596 int32_t ret = SOFTBUS_OK;
597 if (authHandle.type == AUTH_LINK_TYPE_BLE) {
598 AuthHandle authHandleTmp = { 0 };
599 ret = TransProxyGetAuthId(authHandle.authId, &authHandleTmp);
600 if (ret == SOFTBUS_TRANS_NODE_NOT_FOUND) {
601 authHandleTmp.authId = authHandle.authId;
602 }
603 ret = AuthGetDeviceUuid(authHandleTmp.authId, peerUuid, UUID_BUF_LEN);
604 } else {
605 ret = AuthGetDeviceUuid(authHandle.authId, peerUuid, UUID_BUF_LEN);
606 }
607 if (ret != SOFTBUS_OK) {
608 TRANS_LOGE(TRANS_CTRL, "fail to get device uuid by authId=%{public}" PRId64, authHandle.authId);
609 return ret;
610 }
611 return StartHmlListener(myIp, myPort, peerUuid);
612 }
613
OnVerifyP2pRequest(AuthHandle authHandle,int64_t seq,const cJSON * json,bool isAuthLink)614 static int32_t OnVerifyP2pRequest(AuthHandle authHandle, int64_t seq, const cJSON *json, bool isAuthLink)
615 {
616 TRANS_LOGI(TRANS_CTRL, "authId=%{public}" PRId64 ", seq=%{public}" PRId64, authHandle.authId, seq);
617 int32_t peerPort = 0;
618 char peerIp[IP_LEN] = {0};
619 int32_t myPort = 0;
620 char myIp[IP_LEN] = {0};
621
622 int32_t ret = VerifyP2pUnPack(json, peerIp, IP_LEN, &peerPort);
623 if (ret != SOFTBUS_OK) {
624 SendVerifyP2pFailRsp(authHandle, seq, CODE_VERIFY_P2P, ret, "OnVerifyP2pRequest unpack fail", isAuthLink);
625 return ret;
626 }
627
628 struct WifiDirectManager *pManager = GetWifiDirectManager();
629 if (pManager != NULL && pManager->getLocalIpByRemoteIp != NULL) {
630 ret = pManager->getLocalIpByRemoteIp(peerIp, myIp, sizeof(myIp));
631 if (ret != SOFTBUS_OK) {
632 OutputAnonymizeIpAddress(myIp, peerIp);
633 TRANS_LOGE(TRANS_CTRL, "get Local Ip fail, ret = %{public}d", ret);
634 SendVerifyP2pFailRsp(authHandle, seq, CODE_VERIFY_P2P, ret, "get p2p ip fail", isAuthLink);
635 return SOFTBUS_TRANS_GET_P2P_INFO_FAILED;
636 }
637 } else {
638 SendVerifyP2pFailRsp(authHandle, seq, CODE_VERIFY_P2P, SOFTBUS_WIFI_DIRECT_INIT_FAILED,
639 "get wifidirectmanager or localip fail", isAuthLink);
640 return SOFTBUS_WIFI_DIRECT_INIT_FAILED;
641 }
642 if (IsHmlIpAddr(myIp)) {
643 ret = StartHmlListenerByUuid(authHandle, myIp, &myPort);
644 } else {
645 ret = StartP2pListener(myIp, &myPort);
646 }
647 if (ret != SOFTBUS_OK) {
648 OutputAnonymizeIpAddress(myIp, peerIp);
649 SendVerifyP2pFailRsp(authHandle, seq, CODE_VERIFY_P2P, ret, "invalid p2p port", isAuthLink);
650 return ret;
651 }
652 ret = PackAndSendVerifyP2pRsp(myIp, myPort, seq, isAuthLink, authHandle);
653 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "fail to send VerifyP2pRsp.");
654 LaneAddP2pAddressByIp(peerIp, peerPort);
655 return SOFTBUS_OK;
656 }
657
ConnectTcpDirectPeer(const char * addr,int port,const char * myIp)658 static int32_t ConnectTcpDirectPeer(const char *addr, int port, const char *myIp)
659 {
660 ConnectOption options;
661 if (IsHmlIpAddr(addr)) {
662 options.type = CONNECT_HML;
663 } else {
664 options.type = CONNECT_P2P;
665 }
666 (void)memset_s(options.socketOption.addr, sizeof(options.socketOption.addr), 0, sizeof(options.socketOption.addr));
667 options.socketOption.port = port;
668 options.socketOption.protocol = LNN_PROTOCOL_IP;
669 options.socketOption.moduleId = DIRECT_CHANNEL_CLIENT;
670
671 int32_t ret = strcpy_s(options.socketOption.addr, sizeof(options.socketOption.addr), addr);
672 if (ret != SOFTBUS_OK) {
673 TRANS_LOGE(TRANS_CTRL, "strcpy_s failed! ret=%{public}" PRId32, ret);
674 return SOFTBUS_STRCPY_ERR;
675 }
676
677 return ConnOpenClientSocket(&options, myIp, true);
678 }
679
AddHmlTrigger(int32_t fd,const char * myAddr,int64_t seq)680 static int32_t AddHmlTrigger(int32_t fd, const char *myAddr, int64_t seq)
681 {
682 ListenerModule moudleType;
683 SessionConn *conn = NULL;
684 if (SoftBusMutexLock(&g_hmlListenerList->lock) != SOFTBUS_OK) {
685 TRANS_LOGE(TRANS_CTRL, "StartHmlListener lock fail");
686 return SOFTBUS_LOCK_ERR;
687 }
688 HmlListenerInfo *item = NULL;
689 HmlListenerInfo *nextItem = NULL;
690 LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_hmlListenerList->list, HmlListenerInfo, node) {
691 if (strncmp(item->myIp, myAddr, IP_LEN) == 0) {
692 int32_t ret = AddTrigger(item->moudleType, fd, WRITE_TRIGGER);
693 if (ret != SOFTBUS_OK) {
694 TRANS_LOGE(TRANS_CTRL, "fail");
695 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
696 return ret;
697 }
698 moudleType = item->moudleType;
699 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
700 if (GetSessionConnLock() != SOFTBUS_OK) {
701 return SOFTBUS_LOCK_ERR;
702 }
703 conn = GetSessionConnByReq(seq);
704 if (conn == NULL) {
705 ReleaseSessionConnLock();
706 return SOFTBUS_NOT_FIND;
707 }
708 conn->listenMod = moudleType;
709 ReleaseSessionConnLock();
710 return SOFTBUS_OK;
711 }
712 }
713 TRANS_LOGE(TRANS_CTRL, "not found correct hml ip");
714 (void)SoftBusMutexUnlock(&g_hmlListenerList->lock);
715 return SOFTBUS_TRANS_ADD_HML_TRIGGER_FAILED;
716 }
717
AddP2pOrHmlTrigger(int32_t fd,const char * myAddr,int64_t seq)718 static int32_t AddP2pOrHmlTrigger(int32_t fd, const char *myAddr, int64_t seq)
719 {
720 if (IsHmlIpAddr(myAddr)) {
721 return AddHmlTrigger(fd, myAddr, seq);
722 } else {
723 int32_t ret = AddTrigger(DIRECT_CHANNEL_SERVER_P2P, fd, WRITE_TRIGGER);
724 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "AddTrigger fail");
725 }
726 return SOFTBUS_OK;
727 }
728
OnVerifyP2pReply(int64_t authId,int64_t seq,const cJSON * json)729 static int32_t OnVerifyP2pReply(int64_t authId, int64_t seq, const cJSON *json)
730 {
731 TRANS_LOGI(TRANS_CTRL, "authId=%{public}" PRId64 ", seq=%{public}" PRId64, authId, seq);
732 SessionConn *conn = NULL;
733 int32_t ret = SOFTBUS_TRANS_VERIFY_P2P_FAILED;
734 int32_t channelId = INVALID_CHANNEL_ID;
735 int32_t fd = -1;
736 char peerNetworkId[DEVICE_ID_SIZE_MAX] = { 0 };
737 char peerAddr[IP_LEN] = { 0 };
738 char myAddr[IP_LEN] = { 0 };
739 int32_t peerPort = -1;
740
741 if (GetSessionConnLock() != SOFTBUS_OK) {
742 TRANS_LOGE(TRANS_CTRL, "getsessionconnlock fail");
743 return SOFTBUS_LOCK_ERR;
744 }
745 conn = GetSessionConnByReq(seq);
746 if (conn == NULL) {
747 ReleaseSessionConnLock();
748 return SOFTBUS_NOT_FIND;
749 }
750 SoftbusHitraceStart(SOFTBUS_HITRACE_ID_VALID, (uint64_t)(conn->channelId + ID_OFFSET));
751 channelId = conn->channelId;
752
753 ret = VerifyP2pUnPack(json, conn->appInfo.peerData.addr, IP_LEN, &conn->appInfo.peerData.port);
754 if (ret != SOFTBUS_OK) {
755 ReleaseSessionConnLock();
756 TRANS_LOGE(TRANS_CTRL, "unpack fail: ret=%{public}d", ret);
757 goto EXIT_ERR;
758 }
759 TRANS_LOGI(TRANS_CTRL, "peer wifi: peerPort=%{public}d", conn->appInfo.peerData.port);
760
761 fd = ConnectTcpDirectPeer(conn->appInfo.peerData.addr, conn->appInfo.peerData.port, conn->appInfo.myData.addr);
762 if (fd <= 0) {
763 ReleaseSessionConnLock();
764 TRANS_LOGE(TRANS_CTRL, "conn fail: fd=%{public}d", fd);
765 goto EXIT_ERR;
766 }
767 conn->appInfo.fd = fd;
768 conn->status = TCP_DIRECT_CHANNEL_STATUS_CONNECTING;
769 if (strcpy_s(peerNetworkId, sizeof(peerNetworkId), conn->appInfo.peerNetWorkId) != EOK ||
770 strcpy_s(peerAddr, sizeof(peerAddr), conn->appInfo.peerData.addr) != EOK ||
771 strcpy_s(myAddr, sizeof(myAddr), conn->appInfo.myData.addr) != EOK) {
772 ReleaseSessionConnLock();
773 TRANS_LOGE(TRANS_CTRL, "strcpy_s failed!");
774 goto EXIT_ERR;
775 }
776 peerPort = conn->appInfo.peerData.port;
777 ReleaseSessionConnLock();
778
779 if (TransSrvAddDataBufNode(channelId, fd) != SOFTBUS_OK) {
780 goto EXIT_ERR;
781 }
782 if (AddP2pOrHmlTrigger(fd, myAddr, seq) != SOFTBUS_OK) {
783 TRANS_LOGE(TRANS_CTRL, "AddP2pOrHmlTrigger fail");
784 goto EXIT_ERR;
785 }
786
787 LaneAddP2pAddress(peerNetworkId, peerAddr, peerPort);
788
789 TRANS_LOGI(TRANS_CTRL, "end: fd=%{public}d", fd);
790 return SOFTBUS_OK;
791 EXIT_ERR:
792 TRANS_LOGE(TRANS_CTRL, "fail");
793 if (channelId != INVALID_CHANNEL_ID) {
794 OnChannelOpenFail(channelId, SOFTBUS_TRANS_HANDSHAKE_ERROR);
795 }
796 return SOFTBUS_TRANS_VERIFY_P2P_FAILED;
797 }
798
OnAuthMsgProc(AuthHandle authHandle,int32_t flags,int64_t seq,const cJSON * json)799 static void OnAuthMsgProc(AuthHandle authHandle, int32_t flags, int64_t seq, const cJSON *json)
800 {
801 int32_t ret = SOFTBUS_OK;
802 if (flags == MSG_FLAG_REQUEST) {
803 ret = OnVerifyP2pRequest(authHandle, seq, json, true);
804 } else {
805 ret = OnVerifyP2pReply(authHandle.authId, seq, json);
806 }
807 TRANS_LOGI(TRANS_CTRL, "result: ret=%{public}d", ret);
808 return;
809 }
810
OnAuthDataRecv(AuthHandle authHandle,const AuthTransData * data)811 static void OnAuthDataRecv(AuthHandle authHandle, const AuthTransData *data)
812 {
813 if (data == NULL || data->data == NULL || data->len < 1) {
814 TRANS_LOGW(TRANS_CTRL, "invalid param.");
815 return;
816 }
817 if (authHandle.type < AUTH_LINK_TYPE_WIFI || authHandle.type >= AUTH_LINK_TYPE_MAX) {
818 TRANS_LOGE(TRANS_CTRL, "authHandle type error");
819 return;
820 }
821 TRANS_LOGI(TRANS_CTRL, "module=%{public}d, seq=%{public}" PRId64 ", len=%{public}u",
822 data->module, data->seq, data->len);
823 if (data->module != MODULE_P2P_LISTEN) {
824 TRANS_LOGE(TRANS_CTRL, "module is not MODULE_P2P_LISTEN");
825 return;
826 }
827
828 cJSON *json = cJSON_ParseWithLength((const char *)(data->data), data->len);
829 if (json == NULL) {
830 TRANS_LOGE(TRANS_CTRL, "cjson parse with length failed");
831 return;
832 }
833 OnAuthMsgProc(authHandle, data->flag, data->seq, json);
834 cJSON_Delete(json);
835 }
836
OnAuthChannelClose(AuthHandle authHandle)837 static void OnAuthChannelClose(AuthHandle authHandle)
838 {
839 int32_t num = 0;
840 int32_t *channelIds = GetChannelIdsByAuthIdAndStatus(&num, &authHandle, TCP_DIRECT_CHANNEL_STATUS_VERIFY_P2P);
841 if (channelIds == NULL) {
842 TRANS_LOGE(TRANS_CTRL, "Fail to get channel ids with auth id %{public}" PRId64, authHandle.authId);
843 return;
844 }
845 TRANS_LOGW(TRANS_CTRL, "AuthId=%{public}" PRId64 ",channelIds num=%{public}d", authHandle.authId, num);
846 int32_t i;
847 for (i = 0; i < num; i++) {
848 (void)OnChannelOpenFail(channelIds[i], SOFTBUS_TRANS_OPEN_AUTH_CHANNEL_FAILED);
849 }
850 SoftBusFree(channelIds);
851 }
852
OpenNewAuthConn(const AppInfo * appInfo,SessionConn * conn,int32_t newChannelId,ConnectType type)853 static int32_t OpenNewAuthConn(const AppInfo *appInfo, SessionConn *conn,
854 int32_t newChannelId, ConnectType type)
855 {
856 int32_t ret = OpenAuthConn(appInfo->peerData.deviceId, conn->requestId, conn->isMeta, type);
857 if (ret != SOFTBUS_OK) {
858 TRANS_LOGE(TRANS_CTRL, "OpenP2pDirectChannel open auth conn fail");
859 return ret;
860 }
861 return SOFTBUS_OK;
862 }
863
OnP2pVerifyMsgReceived(int32_t channelId,const char * data,uint32_t len)864 static void OnP2pVerifyMsgReceived(int32_t channelId, const char *data, uint32_t len)
865 {
866 #define MAX_DATA_SIZE 1024
867 TRANS_CHECK_AND_RETURN_LOGW((data != NULL) && (len > sizeof(int64_t) + sizeof(int64_t)) && (len <= MAX_DATA_SIZE),
868 TRANS_CTRL, "received data is invalid");
869 cJSON *json = cJSON_ParseWithLength((data + sizeof(int64_t) + sizeof(int64_t)),
870 len - sizeof(int64_t) - sizeof(int64_t));
871 TRANS_CHECK_AND_RETURN_LOGW((json != NULL), TRANS_CTRL, "parse json failed");
872
873 int64_t msgType = (int64_t)SoftBusLtoHll((uint64_t)*(int64_t*)data);
874 AuthHandle authHandle = { .authId = channelId, .type = AUTH_LINK_TYPE_BLE };
875 if (msgType == P2P_VERIFY_REQUEST) {
876 OnVerifyP2pRequest(authHandle, SoftBusLtoHll((uint64_t)*(int64_t*)(data + sizeof(int64_t))), json, false);
877 } else if (msgType == P2P_VERIFY_REPLY) {
878 OnVerifyP2pReply(channelId, SoftBusLtoHll((uint64_t)*(int64_t*)(data + sizeof(int64_t))), json);
879 } else {
880 TRANS_LOGE(TRANS_CTRL, "invalid msgType=%{public}" PRIu64, msgType);
881 }
882 cJSON_Delete(json);
883 }
884
OnP2pVerifyChannelClosed(int32_t channelId)885 void OnP2pVerifyChannelClosed(int32_t channelId)
886 {
887 TRANS_LOGW(TRANS_CTRL, "receive p2p verify close. channelId=%{public}d", channelId);
888 }
889
TransProxyGetAuthIdByUuid(SessionConn * conn)890 static int32_t TransProxyGetAuthIdByUuid(SessionConn *conn)
891 {
892 AuthGetLatestIdByUuid(conn->appInfo.peerData.deviceId, AUTH_LINK_TYPE_WIFI, false, &conn->authHandle);
893 if (conn->authHandle.authId == AUTH_INVALID_ID) {
894 //get WIFI authManager failed,retry BLE
895 AuthGetLatestIdByUuid(conn->appInfo.peerData.deviceId, AUTH_LINK_TYPE_BLE, false, &conn->authHandle);
896 }
897 if (conn->authHandle.authId == AUTH_INVALID_ID) {
898 //get WIFI and BLE authManager failed,retry BR
899 AuthGetLatestIdByUuid(conn->appInfo.peerData.deviceId, AUTH_LINK_TYPE_BR, false, &conn->authHandle);
900 }
901 TRANS_CHECK_AND_RETURN_RET_LOGE(conn->authHandle.authId != AUTH_INVALID_ID, SOFTBUS_TRANS_TCP_GET_AUTHID_FAILED,
902 TRANS_CTRL, "get authManager failed");
903 return SOFTBUS_OK;
904 }
905
906
StartVerifyP2pInfo(const AppInfo * appInfo,SessionConn * conn,ConnectType type)907 static int32_t StartVerifyP2pInfo(const AppInfo *appInfo, SessionConn *conn, ConnectType type)
908 {
909 int32_t ret = SOFTBUS_TRANS_VERIFY_P2P_FAILED;
910 int32_t newChannelId = conn->channelId;
911 int32_t pipeLineChannelId = TransProxyPipelineGetChannelIdByNetworkId(appInfo->peerNetWorkId);
912 if (pipeLineChannelId == INVALID_CHANNEL_ID) {
913 TRANS_LOGI(TRANS_CTRL, "can not get channelid by networkid");
914 uint32_t requestId = AuthGenRequestId();
915 conn->status = TCP_DIRECT_CHANNEL_STATUS_AUTH_CHANNEL;
916 conn->requestId = requestId;
917 if (type == CONNECT_P2P_REUSE) {
918 type = IsHmlIpAddr(appInfo->myData.addr) ? CONNECT_HML : CONNECT_P2P;
919 }
920 TRANS_LOGD(TRANS_CTRL, "type=%{public}d", type);
921 ret = OpenNewAuthConn(appInfo, conn, newChannelId, type);
922 } else {
923 ret = TransProxyReuseByChannelId(pipeLineChannelId);
924 if (ret != SOFTBUS_OK) {
925 TRANS_LOGE(TRANS_CTRL, "channelId can't be repeated. channelId=%{public}d", pipeLineChannelId);
926 return ret;
927 }
928 TransProxyPipelineCloseChannelDelay(pipeLineChannelId);
929 ret = TransProxyGetAuthIdByUuid(conn);
930 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "get auth id failed");
931 conn->requestId = REQUEST_INVALID;
932 char *msg = VerifyP2pPack(conn->appInfo.myData.addr, conn->appInfo.myData.port, NULL);
933 if (msg == NULL) {
934 TRANS_LOGE(TRANS_CTRL, "verify p2p pack failed");
935 return SOFTBUS_TRANS_VERIFY_P2P_FAILED;
936 }
937 uint32_t strLen = strlen(msg) + 1;
938 char *sendMsg = (char*)SoftBusCalloc(strLen + sizeof(int64_t) + sizeof(int64_t));
939 if (sendMsg == NULL) {
940 cJSON_free(msg);
941 return SOFTBUS_MALLOC_ERR;
942 }
943 *(int64_t*)sendMsg = SoftBusHtoLll((uint64_t)P2P_VERIFY_REQUEST);
944 *(int64_t*)(sendMsg + sizeof(int64_t)) = SoftBusHtoLll((uint64_t)conn->req);
945 if (strcpy_s(sendMsg + sizeof(int64_t) + sizeof(int64_t), strLen, msg) != EOK) {
946 cJSON_free(msg);
947 SoftBusFree(sendMsg);
948 return SOFTBUS_STRCPY_ERR;
949 }
950 ret = TransProxyPipelineSendMessage(pipeLineChannelId, (uint8_t *)sendMsg,
951 strLen + sizeof(int64_t) + sizeof(int64_t), MSG_TYPE_IP_PORT_EXCHANGE);
952 cJSON_free(msg);
953 SoftBusFree(sendMsg);
954 }
955 return ret;
956 }
957
CopyAppInfoFastTransData(SessionConn * conn,const AppInfo * appInfo)958 static int32_t CopyAppInfoFastTransData(SessionConn *conn, const AppInfo *appInfo)
959 {
960 if (appInfo->fastTransData != NULL && appInfo->fastTransDataSize > 0) {
961 uint8_t *fastTransData = (uint8_t *)SoftBusCalloc(appInfo->fastTransDataSize);
962 if (fastTransData == NULL) {
963 return SOFTBUS_MALLOC_ERR;
964 }
965 if (memcpy_s((char *)fastTransData, appInfo->fastTransDataSize, (const char *)appInfo->fastTransData,
966 appInfo->fastTransDataSize) != EOK) {
967 TRANS_LOGE(TRANS_CTRL, "memcpy fastTransData fail");
968 SoftBusFree(fastTransData);
969 return SOFTBUS_MEM_ERR;
970 }
971 conn->appInfo.fastTransData = fastTransData;
972 }
973 return SOFTBUS_OK;
974 }
975
FreeFastTransData(AppInfo * appInfo)976 static void FreeFastTransData(AppInfo *appInfo)
977 {
978 if (appInfo != NULL && appInfo->fastTransData != NULL) {
979 SoftBusFree((void *)(appInfo->fastTransData));
980 }
981 }
982
BuildSessionConn(const AppInfo * appInfo,SessionConn ** conn)983 static int32_t BuildSessionConn(const AppInfo *appInfo, SessionConn **conn)
984 {
985 int32_t ret = SOFTBUS_TRANS_P2P_DIRECT_FAILED;
986 *conn = CreateNewSessinConn(DIRECT_CHANNEL_SERVER_P2P, false);
987 if (*conn == NULL) {
988 TRANS_LOGE(TRANS_CTRL, "create new sessin conn fail");
989 return SOFTBUS_MEM_ERR;
990 }
991
992 if (memcpy_s(&((*conn)->appInfo), sizeof(AppInfo), appInfo, sizeof(AppInfo)) != EOK) {
993 TRANS_LOGE(TRANS_CTRL, "copy appInfo fail");
994 SoftBusFree(*conn);
995 *conn = NULL;
996 return SOFTBUS_MEM_ERR;
997 }
998 ret = CopyAppInfoFastTransData(*conn, appInfo);
999 if (ret != SOFTBUS_OK) {
1000 TRANS_LOGE(TRANS_CTRL, "copy appinfo fast trans data fail");
1001 SoftBusFree(*conn);
1002 *conn = NULL;
1003 return ret;
1004 }
1005 return SOFTBUS_OK;
1006 }
1007
StartTransP2pDirectListener(ConnectType type,SessionConn * conn,const AppInfo * appInfo)1008 static int32_t StartTransP2pDirectListener(ConnectType type, SessionConn *conn, const AppInfo *appInfo)
1009 {
1010 if (type == CONNECT_P2P) {
1011 if (IsHmlIpAddr(conn->appInfo.myData.addr)) {
1012 return StartHmlListener(conn->appInfo.myData.addr, &conn->appInfo.myData.port, appInfo->peerData.deviceId);
1013 } else {
1014 return StartP2pListener(conn->appInfo.myData.addr, &conn->appInfo.myData.port);
1015 }
1016 }
1017 return StartHmlListener(conn->appInfo.myData.addr, &conn->appInfo.myData.port, appInfo->peerData.deviceId);
1018 }
1019
OpenP2pDirectChannel(const AppInfo * appInfo,const ConnectOption * connInfo,int32_t * channelId)1020 int32_t OpenP2pDirectChannel(const AppInfo *appInfo, const ConnectOption *connInfo, int32_t *channelId)
1021 {
1022 TRANS_LOGI(TRANS_CTRL, "enter.");
1023 if (appInfo == NULL || connInfo == NULL || channelId == NULL ||
1024 (connInfo->type != CONNECT_P2P && connInfo->type != CONNECT_HML && connInfo->type != CONNECT_P2P_REUSE)) {
1025 TRANS_LOGE(TRANS_CTRL, "invalid param");
1026 return SOFTBUS_INVALID_PARAM;
1027 }
1028 SessionConn *conn = NULL;
1029 int32_t ret = BuildSessionConn(appInfo, &conn);
1030 if (ret != SOFTBUS_OK) {
1031 TRANS_LOGE(TRANS_CTRL, "build new sessin conn fail, ret=%{public}d", ret);
1032 return ret;
1033 }
1034 SoftbusHitraceStart(SOFTBUS_HITRACE_ID_VALID, (uint64_t)(conn->channelId + (uint64_t)ID_OFFSET));
1035 TRANS_LOGI(TRANS_CTRL,
1036 "SoftbusHitraceChainBegin: set HitraceId=%{public}" PRIu64, (uint64_t)(conn->channelId + ID_OFFSET));
1037 ret = StartTransP2pDirectListener(connInfo->type, conn, appInfo);
1038 if (ret != SOFTBUS_OK) {
1039 FreeFastTransData(&(conn->appInfo));
1040 SoftBusFree(conn);
1041 TRANS_LOGE(TRANS_CTRL, "start listener fail");
1042 return ret;
1043 }
1044 uint64_t seq = TransTdcGetNewSeqId();
1045 if (seq == INVALID_SEQ_ID) {
1046 FreeFastTransData(&(conn->appInfo));
1047 SoftBusFree(conn);
1048 return SOFTBUS_TRANS_INVALID_SEQ_ID;
1049 }
1050 conn->req = (int64_t)seq;
1051 conn->isMeta = TransGetAuthTypeByNetWorkId(appInfo->peerNetWorkId);
1052 ret = TransTdcAddSessionConn(conn);
1053 if (ret != SOFTBUS_OK) {
1054 FreeFastTransData(&(conn->appInfo));
1055 SoftBusFree(conn);
1056 return ret;
1057 }
1058 ret = StartVerifyP2pInfo(appInfo, conn, connInfo->type);
1059 if (ret != SOFTBUS_OK) {
1060 TransDelSessionConnById(conn->channelId);
1061 TRANS_LOGE(TRANS_CTRL, "StartVerifyP2pInfo fail, ret=%{public}d", ret);
1062 return ret;
1063 }
1064 *channelId = conn->channelId;
1065 TRANS_LOGI(TRANS_CTRL, "end: channelId=%{public}d", conn->channelId);
1066 return ret;
1067 }
1068
P2pDirectChannelInit(void)1069 int32_t P2pDirectChannelInit(void)
1070 {
1071 TRANS_LOGI(TRANS_INIT, "enter.");
1072 if (SoftBusMutexInit(&g_p2pLock, NULL) != SOFTBUS_OK) {
1073 TRANS_LOGE(TRANS_INIT, "init lock failed");
1074 return SOFTBUS_TRANS_INIT_FAILED;
1075 }
1076 int32_t ret = CreatHmlListenerList();
1077 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "CreatHmlListenerList failed");
1078
1079 AuthTransListener p2pTransCb = {
1080 .onDataReceived = OnAuthDataRecv,
1081 .onDisconnected = OnAuthChannelClose,
1082 .onException = NULL,
1083 };
1084
1085 ret = RegAuthTransListener(MODULE_P2P_LISTEN, &p2pTransCb);
1086 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "P2pDirectChannelInit set cb fail");
1087
1088 ITransProxyPipelineListener listener = {
1089 .onDataReceived = OnP2pVerifyMsgReceived,
1090 .onDisconnected = OnP2pVerifyChannelClosed,
1091 };
1092
1093 ret = TransProxyPipelineRegisterListener(MSG_TYPE_IP_PORT_EXCHANGE, &listener);
1094 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "register listener failed");
1095
1096 TRANS_LOGI(TRANS_INIT, "ok");
1097 return SOFTBUS_OK;
1098 }
1099