1 /*
2 * Copyright (c) 2021-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 "softbus_proxychannel_transceiver.h"
17
18 #include <securec.h>
19
20 #include "auth_device_common_key.h"
21 #include "lnn_device_info_recovery.h"
22 #include "message_handler.h"
23 #include "legacy/softbus_adapter_hitrace.h"
24 #include "legacy/softbus_hisysevt_transreporter.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_adapter_thread.h"
27 #include "softbus_conn_interface.h"
28 #include "softbus_def.h"
29 #include "softbus_error_code.h"
30 #include "softbus_proxychannel_control.h"
31 #include "softbus_proxychannel_listener.h"
32 #include "softbus_proxychannel_manager.h"
33 #include "softbus_proxychannel_message.h"
34 #include "softbus_proxychannel_pipeline.h"
35 #include "softbus_utils.h"
36 #include "trans_auth_negotiation.h"
37 #include "trans_channel_common.h"
38 #include "trans_channel_manager.h"
39 #include "trans_event.h"
40 #include "trans_log.h"
41
42 #define ID_OFFSET (1)
43
44 static SoftBusList *g_proxyConnectionList = NULL;
45 const char *g_transProxyLoopName = "transProxyLoopName";
46 SoftBusHandler g_transLoopHandler = { 0 };
47
48 typedef enum {
49 LOOP_HANDSHAKE_MSG,
50 LOOP_DISCONNECT_MSG,
51 LOOP_OPENFAIL_MSG,
52 LOOP_OPENCLOSE_MSG,
53 LOOP_KEEPALIVE_MSG,
54 LOOP_RESETPEER_MSG,
55 LOOP_AUTHSTATECHECK_MSG,
56 } LoopMsg;
57
TransDelConnByReqId(uint32_t requestId)58 int32_t TransDelConnByReqId(uint32_t requestId)
59 {
60 ProxyConnInfo *removeNode = NULL;
61 ProxyConnInfo *tmpNode = NULL;
62
63 if (g_proxyConnectionList == NULL) {
64 TRANS_LOGE(TRANS_CTRL, "g_proxyConnectionList is null!");
65 return SOFTBUS_NO_INIT;
66 }
67
68 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
69 TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
70 return SOFTBUS_LOCK_ERR;
71 }
72
73 LIST_FOR_EACH_ENTRY_SAFE(removeNode, tmpNode, &g_proxyConnectionList->list, ProxyConnInfo, node) {
74 if (removeNode->requestId == requestId && removeNode->state == PROXY_CHANNEL_STATUS_PYH_CONNECTING) {
75 ListDelete(&(removeNode->node));
76 TRANS_LOGI(TRANS_CTRL, "delete requestId=%{public}u", removeNode->requestId);
77 SoftBusFree(removeNode);
78 g_proxyConnectionList->cnt--;
79 break;
80 }
81 }
82 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
83 return SOFTBUS_OK;
84 }
85
TransDelConnByConnId(uint32_t connId)86 void TransDelConnByConnId(uint32_t connId)
87 {
88 ProxyConnInfo *removeNode = NULL;
89 ProxyConnInfo *tmpNode = NULL;
90
91 if ((g_proxyConnectionList == NULL) || (connId == 0)) {
92 TRANS_LOGE(TRANS_CTRL, "g_proxyConnectionList or connId is null");
93 return;
94 }
95
96 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
97 TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
98 return;
99 }
100
101 LIST_FOR_EACH_ENTRY_SAFE(removeNode, tmpNode, &g_proxyConnectionList->list, ProxyConnInfo, node) {
102 if (removeNode->connId == connId) {
103 ListDelete(&(removeNode->node));
104 SoftBusFree(removeNode);
105 TRANS_LOGI(TRANS_CTRL, "del conn item. connId=%{public}d", connId);
106 g_proxyConnectionList->cnt--;
107 }
108 }
109 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
110 return;
111 }
112
TransDecConnRefByConnId(uint32_t connId,bool isServer)113 int32_t TransDecConnRefByConnId(uint32_t connId, bool isServer)
114 {
115 ProxyConnInfo *removeNode = NULL;
116 ProxyConnInfo *tmpNode = NULL;
117
118 if ((g_proxyConnectionList == NULL) || (connId == 0)) {
119 TRANS_LOGE(TRANS_MSG, "g_proxyConnectionList or connId is null");
120 return SOFTBUS_NO_INIT;
121 }
122
123 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
124 TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
125 return SOFTBUS_LOCK_ERR;
126 }
127
128 LIST_FOR_EACH_ENTRY_SAFE(removeNode, tmpNode, &g_proxyConnectionList->list, ProxyConnInfo, node) {
129 if (removeNode->connId == connId && removeNode->isServerSide == isServer) {
130 removeNode->ref--;
131 if (removeNode->ref <= 0) {
132 ListDelete(&(removeNode->node));
133 SoftBusFree(removeNode);
134 g_proxyConnectionList->cnt--;
135 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
136 TRANS_LOGI(TRANS_CTRL, "conn ref is 0. connId=%{public}u", connId);
137 return SOFTBUS_OK;
138 } else {
139 TRANS_LOGI(TRANS_CTRL, "connId=%{public}u, proxyConnRef=%{public}d", connId, removeNode->ref);
140 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
141 return SOFTBUS_TRANS_NOT_MATCH;
142 }
143 }
144 }
145
146 TRANS_LOGW(TRANS_CTRL, "not find item. connId=%{public}u", connId);
147 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
148 return SOFTBUS_OK;
149 }
150
TransAddConnRefByConnId(uint32_t connId,bool isServer)151 int32_t TransAddConnRefByConnId(uint32_t connId, bool isServer)
152 {
153 ProxyConnInfo *item = NULL;
154
155 if (g_proxyConnectionList == NULL) {
156 TRANS_LOGE(TRANS_MSG, "g_proxyConnectionList is null");
157 return SOFTBUS_NO_INIT;
158 }
159
160 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
161 TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
162 return SOFTBUS_LOCK_ERR;
163 }
164
165 LIST_FOR_EACH_ENTRY(item, &g_proxyConnectionList->list, ProxyConnInfo, node) {
166 if (item->connId == connId && item->isServerSide == isServer) {
167 item->ref++;
168 TRANS_LOGI(TRANS_CTRL, "add connId=%{public}u, proxyConnRef=%{public}d.", connId, item->ref);
169 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
170 return SOFTBUS_OK;
171 }
172 }
173 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
174 return SOFTBUS_TRANS_PROXY_CONN_ADD_REF_FAILED;
175 }
176
TransProxyLoopMsgHandler(SoftBusMessage * msg)177 static void TransProxyLoopMsgHandler(SoftBusMessage *msg)
178 {
179 TRANS_CHECK_AND_RETURN_LOGE(msg != NULL, TRANS_MSG, "param invalid");
180 TRANS_LOGD(TRANS_CTRL, "trans loop process msgType=%{public}d", msg->what);
181 int32_t channelId;
182 ProxyChannelInfo *chan = NULL;
183 switch (msg->what) {
184 case LOOP_HANDSHAKE_MSG:
185 channelId = *((int32_t *)msg->obj);
186 TransProxyOpenProxyChannelSuccess(channelId);
187 break;
188 case LOOP_DISCONNECT_MSG: {
189 bool isServer = (bool)msg->arg1;
190 uint32_t connectionId = (uint32_t)msg->arg2;
191 chan = (ProxyChannelInfo *)msg->obj;
192 TRANS_CHECK_AND_RETURN_LOGE(chan != NULL, TRANS_MSG, "LOOP_DISCONNECT_MSG, chan is null");
193 TransProxyCloseConnChannelReset(connectionId, (isServer == 0), isServer, chan->deviceTypeIsWinpc);
194 break;
195 }
196 case LOOP_OPENFAIL_MSG:
197 chan = (ProxyChannelInfo *)msg->obj;
198 TRANS_CHECK_AND_RETURN_LOGE(chan != NULL, TRANS_MSG, "LOOP_OPENFAIL_MSG, chan is null");
199 TransProxyOpenProxyChannelFail(chan->channelId, &(chan->appInfo), (int32_t)msg->arg1);
200 break;
201 case LOOP_OPENCLOSE_MSG:
202 chan = (ProxyChannelInfo *)msg->obj;
203 TRANS_CHECK_AND_RETURN_LOGE(chan != NULL, TRANS_MSG, "LOOP_OPENCLOSE_MSG, chan is null");
204 OnProxyChannelClosed(chan->channelId, &(chan->appInfo));
205 break;
206 case LOOP_KEEPALIVE_MSG:
207 chan = (ProxyChannelInfo *)msg->obj;
208 TRANS_CHECK_AND_RETURN_LOGE(chan != NULL, TRANS_MSG, "LOOP_KEEPALIVE_MSG, chan is null");
209 TransProxyKeepalive(chan->connId, chan);
210 break;
211 case LOOP_RESETPEER_MSG:
212 chan = (ProxyChannelInfo *)msg->obj;
213 TRANS_CHECK_AND_RETURN_LOGE(chan != NULL, TRANS_MSG, "LOOP_RESETPEER_MSG, chan is null");
214 TransProxyResetPeer(chan);
215 break;
216 case LOOP_AUTHSTATECHECK_MSG: {
217 uint32_t authRequestId = (uint32_t)msg->arg1;
218 channelId = (int32_t)msg->arg2;
219 TransAuthNegoTaskManager(authRequestId, channelId);
220 break;
221 }
222 default:
223 break;
224 }
225 }
226
TransProxyFreeLoopMsg(SoftBusMessage * msg)227 void TransProxyFreeLoopMsg(SoftBusMessage *msg)
228 {
229 if (msg != NULL) {
230 if (msg->obj != NULL) {
231 SoftBusFree(msg->obj);
232 }
233 SoftBusFree((void *)msg);
234 }
235 }
236
TransProxyCreateLoopMsg(int32_t what,uint64_t arg1,uint64_t arg2,char * data)237 static SoftBusMessage *TransProxyCreateLoopMsg(int32_t what, uint64_t arg1, uint64_t arg2, char *data)
238 {
239 SoftBusMessage *msg = (SoftBusMessage *)SoftBusCalloc(sizeof(SoftBusMessage));
240 if (msg == NULL) {
241 TRANS_LOGE(TRANS_MSG, "msg calloc failed");
242 return NULL;
243 }
244 msg->what = what;
245 msg->arg1 = arg1;
246 msg->arg2 = arg2;
247 msg->handler = &g_transLoopHandler;
248 msg->FreeMessage = TransProxyFreeLoopMsg;
249 msg->obj = (void *)data;
250 return msg;
251 }
252
TransProxyPostAuthNegoMsgToLooperDelay(uint32_t authRequestId,int32_t channelId,uint32_t delayTime)253 void TransProxyPostAuthNegoMsgToLooperDelay(uint32_t authRequestId, int32_t channelId, uint32_t delayTime)
254 {
255 SoftBusMessage *msg = TransProxyCreateLoopMsg(LOOP_AUTHSTATECHECK_MSG, authRequestId, channelId, NULL);
256 TRANS_CHECK_AND_RETURN_LOGE(msg != NULL, TRANS_MSG, "msg create failed");
257
258 g_transLoopHandler.looper->PostMessageDelay(g_transLoopHandler.looper, msg, delayTime);
259 }
260
TransProxyPostResetPeerMsgToLoop(const ProxyChannelInfo * chan)261 void TransProxyPostResetPeerMsgToLoop(const ProxyChannelInfo *chan)
262 {
263 SoftBusMessage *msg = TransProxyCreateLoopMsg(LOOP_RESETPEER_MSG, 0, 0, (char *)chan);
264 if (msg == NULL) {
265 TRANS_LOGE(TRANS_MSG, "msg create failed");
266 if (chan != NULL) {
267 (void)memset_s(
268 (void *)chan->appInfo.sessionKey,
269 sizeof(chan->appInfo.sessionKey), 0, sizeof(chan->appInfo.sessionKey));
270 SoftBusFree((void *)chan);
271 }
272 return;
273 }
274 g_transLoopHandler.looper->PostMessage(g_transLoopHandler.looper, msg);
275 }
276
TransProxyPostHandshakeMsgToLoop(int32_t channelId)277 void TransProxyPostHandshakeMsgToLoop(int32_t channelId)
278 {
279 int32_t *chanIdMsg = (int32_t *)SoftBusCalloc(sizeof(int32_t));
280 if (chanIdMsg == NULL) {
281 TRANS_LOGE(TRANS_MSG, "chanIdMsg calloc failed");
282 return;
283 }
284 *chanIdMsg = channelId;
285 SoftBusMessage *msg = TransProxyCreateLoopMsg(LOOP_HANDSHAKE_MSG, 0, 0, (char *)chanIdMsg);
286 if (msg == NULL) {
287 SoftBusFree((void *)chanIdMsg);
288 return;
289 }
290 g_transLoopHandler.looper->PostMessage(g_transLoopHandler.looper, msg);
291 }
292
TransProxyPostDisConnectMsgToLoop(uint32_t connId,bool isServer,const ProxyChannelInfo * chan)293 void TransProxyPostDisConnectMsgToLoop(uint32_t connId, bool isServer, const ProxyChannelInfo *chan)
294 {
295 SoftBusMessage *msg = TransProxyCreateLoopMsg(LOOP_DISCONNECT_MSG, isServer, connId, (char *)chan);
296 if (msg == NULL) {
297 TRANS_LOGE(TRANS_MSG, "msg create failed");
298 SoftBusFree((void *)chan);
299 return;
300 }
301 g_transLoopHandler.looper->PostMessage(g_transLoopHandler.looper, msg);
302 }
303
TransProxyPostKeepAliveMsgToLoop(const ProxyChannelInfo * chan)304 void TransProxyPostKeepAliveMsgToLoop(const ProxyChannelInfo *chan)
305 {
306 if (chan == NULL) {
307 TRANS_LOGE(TRANS_MSG, "param invalid");
308 return;
309 }
310 SoftBusMessage *msg = TransProxyCreateLoopMsg(LOOP_KEEPALIVE_MSG, 0, 0, (char *)chan);
311 if (msg == NULL) {
312 TRANS_LOGE(TRANS_MSG, "msg create failed");
313 if (chan != NULL) {
314 SoftBusFree((void *)chan);
315 }
316 return;
317 }
318 g_transLoopHandler.looper->PostMessage(g_transLoopHandler.looper, msg);
319 }
320
TransProxyPostOpenFailMsgToLoop(const ProxyChannelInfo * chan,int32_t errCode)321 void TransProxyPostOpenFailMsgToLoop(const ProxyChannelInfo *chan, int32_t errCode)
322 {
323 if (chan == NULL) {
324 TRANS_LOGE(TRANS_MSG, "param invalid");
325 return;
326 }
327 SoftBusMessage *msg = TransProxyCreateLoopMsg(LOOP_OPENFAIL_MSG, errCode, 0, (char *)chan);
328 if (msg == NULL) {
329 TRANS_LOGE(TRANS_MSG, "msg create failed");
330 if (chan != NULL) {
331 SoftBusFree((void *)chan);
332 }
333 return;
334 }
335 g_transLoopHandler.looper->PostMessage(g_transLoopHandler.looper, msg);
336 }
337
TransProxyPostOpenClosedMsgToLoop(const ProxyChannelInfo * chan)338 void TransProxyPostOpenClosedMsgToLoop(const ProxyChannelInfo *chan)
339 {
340 if (chan == NULL) {
341 TRANS_LOGE(TRANS_MSG, "param invalid");
342 return;
343 }
344 SoftBusMessage *msg = TransProxyCreateLoopMsg(LOOP_OPENCLOSE_MSG, 0, 0, (char *)chan);
345 if (msg == NULL) {
346 TRANS_LOGE(TRANS_MSG, "msg create failed");
347 if (chan != NULL) {
348 SoftBusFree((void *)chan);
349 }
350 return;
351 }
352 g_transLoopHandler.looper->PostMessage(g_transLoopHandler.looper, msg);
353 }
354
TransProxyLoopInit(void)355 static int32_t TransProxyLoopInit(void)
356 {
357 g_transLoopHandler.name = (char *)g_transProxyLoopName;
358 g_transLoopHandler.looper = GetLooper(LOOP_TYPE_DEFAULT);
359 if (g_transLoopHandler.looper == NULL) {
360 return SOFTBUS_TRANS_INIT_FAILED;
361 }
362 g_transLoopHandler.HandleMessage = TransProxyLoopMsgHandler;
363 return SOFTBUS_OK;
364 }
365
TransProxyTransSendMsg(uint32_t connectionId,uint8_t * buf,uint32_t len,int32_t priority,int32_t pid)366 int32_t TransProxyTransSendMsg(uint32_t connectionId, uint8_t *buf, uint32_t len, int32_t priority, int32_t pid)
367 {
368 ConnPostData data = { 0 };
369 static uint64_t seq = 1;
370
371 data.module = MODULE_PROXY_CHANNEL;
372 data.seq = seq++;
373 data.flag = priority;
374 data.pid = pid;
375 data.len = len;
376 data.buf = (char *)buf;
377 TRANS_LOGI(TRANS_MSG,
378 "send msg connId=%{public}u, len=%{public}u, seq=%{public}" PRIu64 ", priority=%{public}d, pid=%{public}d",
379 connectionId, len, data.seq, priority, pid);
380 int32_t ret = ConnPostBytes(connectionId, &data);
381 if (ret < 0) {
382 TRANS_LOGE(TRANS_MSG, "conn send buf fail ret=%{public}d", ret);
383 return ret;
384 }
385 return SOFTBUS_OK;
386 }
387
TransProxyOnConnected(uint32_t connId,const ConnectionInfo * connInfo)388 static void TransProxyOnConnected(uint32_t connId, const ConnectionInfo *connInfo)
389 {
390 (void)connInfo;
391 TRANS_LOGI(TRANS_CTRL, "connect enabled, connId=%{public}u", connId);
392 }
393
TransProxyOnDisConnect(uint32_t connId,const ConnectionInfo * connInfo)394 static void TransProxyOnDisConnect(uint32_t connId, const ConnectionInfo *connInfo)
395 {
396 (void)connInfo;
397 TRANS_LOGI(TRANS_CTRL, "connect disabled, connId=%{public}u", connId);
398 TransProxyDelByConnId(connId);
399 TransDelConnByConnId(connId);
400 }
401
CompareConnectOption(const ConnectOption * itemConnInfo,const ConnectOption * connInfo)402 static bool CompareConnectOption(const ConnectOption *itemConnInfo, const ConnectOption *connInfo)
403 {
404 if (connInfo->type == CONNECT_TCP) {
405 TRANS_LOGI(TRANS_CTRL, "CONNECT_TCP");
406 if (connInfo->socketOption.protocol == itemConnInfo->socketOption.protocol &&
407 strcasecmp(connInfo->socketOption.addr, itemConnInfo->socketOption.addr) == 0 &&
408 connInfo->socketOption.port == itemConnInfo->socketOption.port) {
409 return true;
410 }
411 return false;
412 } else if (connInfo->type == CONNECT_BR) {
413 TRANS_LOGI(TRANS_CTRL, "CONNECT_BR");
414 if (strcasecmp(connInfo->brOption.brMac, itemConnInfo->brOption.brMac) == 0) {
415 return true;
416 }
417 return false;
418 } else if (connInfo->type == CONNECT_BLE) {
419 TRANS_LOGI(TRANS_CTRL, "CONNECT_BLE");
420 if (strcasecmp(connInfo->bleOption.bleMac, itemConnInfo->bleOption.bleMac) == 0 &&
421 (connInfo->bleOption.protocol == itemConnInfo->bleOption.protocol) &&
422 connInfo->bleOption.psm == itemConnInfo->bleOption.psm) {
423 return true;
424 }
425 return false;
426 } else if (connInfo->type == CONNECT_BLE_DIRECT) {
427 TRANS_LOGI(TRANS_CTRL, "CONNECT_BLE_DIRECT");
428 if ((strcmp(connInfo->bleDirectOption.networkId, itemConnInfo->bleDirectOption.networkId) == 0) &&
429 (connInfo->bleDirectOption.protoType == itemConnInfo->bleDirectOption.protoType)) {
430 return true;
431 }
432 return false;
433 }
434 return false;
435 }
436
TransAddConnItem(ProxyConnInfo * chan)437 int32_t TransAddConnItem(ProxyConnInfo *chan)
438 {
439 if (chan == NULL) {
440 TRANS_LOGE(TRANS_CTRL, "param invalid");
441 return SOFTBUS_INVALID_PARAM;
442 }
443 ProxyConnInfo *item = NULL;
444 ProxyConnInfo *tmpItem = NULL;
445
446 if (g_proxyConnectionList == NULL) {
447 TRANS_LOGE(TRANS_CTRL, "g_proxyConnectionList is null");
448 return SOFTBUS_NO_INIT;
449 }
450
451 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
452 TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
453 return SOFTBUS_LOCK_ERR;
454 }
455
456 LIST_FOR_EACH_ENTRY_SAFE(item, tmpItem, &g_proxyConnectionList->list, ProxyConnInfo, node) {
457 if (item->isServerSide == chan->isServerSide &&
458 item->connInfo.type == chan->connInfo.type &&
459 CompareConnectOption(&item->connInfo, &chan->connInfo) == true) {
460 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
461 return SOFTBUS_TRANS_NOT_MATCH;
462 }
463 }
464 ListAdd(&(g_proxyConnectionList->list), &(chan->node));
465 TRANS_LOGI(TRANS_CTRL, "add requestId=%{public}u", chan->requestId);
466 g_proxyConnectionList->cnt++;
467 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
468 return SOFTBUS_OK;
469 }
470
TransConnInfoToConnOpt(ConnectionInfo * connInfo,ConnectOption * connOption)471 static void TransConnInfoToConnOpt(ConnectionInfo *connInfo, ConnectOption *connOption)
472 {
473 connOption->type = connInfo->type;
474 if (connOption->type == CONNECT_BR) {
475 TRANS_LOGI(TRANS_CTRL, "CONNECT_BR");
476 (void)memcpy_s(connOption->brOption.brMac, sizeof(char) * BT_MAC_LEN,
477 connInfo->brInfo.brMac, sizeof(char) * BT_MAC_LEN);
478 } else if (connOption->type == CONNECT_BLE) {
479 TRANS_LOGI(TRANS_CTRL, "CONNECT_BLE");
480 (void)memcpy_s(connOption->bleOption.bleMac, sizeof(char) * BT_MAC_LEN,
481 connInfo->bleInfo.bleMac, sizeof(char) * BT_MAC_LEN);
482 (void)memcpy_s(connOption->bleOption.deviceIdHash, sizeof(char) * UDID_HASH_LEN,
483 connInfo->bleInfo.deviceIdHash, sizeof(char) * UDID_HASH_LEN);
484 } else {
485 (void)memcpy_s(connOption->socketOption.addr, sizeof(char) * IP_LEN,
486 connInfo->socketInfo.addr, sizeof(char) * IP_LEN);
487 connOption->socketOption.protocol = connInfo->socketInfo.protocol;
488 connOption->socketOption.port = connInfo->socketInfo.port;
489 connOption->socketOption.moduleId = connInfo->socketInfo.moduleId;
490 }
491 }
492
TransCreateConnByConnId(uint32_t connId,bool isServer)493 void TransCreateConnByConnId(uint32_t connId, bool isServer)
494 {
495 ProxyConnInfo *item = NULL;
496 ProxyConnInfo *tmpNode = NULL;
497 ConnectionInfo info = {0};
498
499 if (g_proxyConnectionList == NULL) {
500 TRANS_LOGE(TRANS_CTRL, "g_proxyConnectionList is null");
501 return;
502 }
503
504 if (ConnGetConnectionInfo(connId, &info) != SOFTBUS_OK) {
505 TRANS_LOGE(TRANS_CTRL, "CreateConn get conn info fail connId=%{public}u", connId);
506 return;
507 }
508
509 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
510 TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
511 return;
512 }
513 LIST_FOR_EACH_ENTRY_SAFE(item, tmpNode, &g_proxyConnectionList->list, ProxyConnInfo, node) {
514 if (item->connId == connId && item->isServerSide == isServer) {
515 item->ref++;
516 TRANS_LOGI(TRANS_CTRL, "repeat conn proxyConnRef=%{public}d", item->ref);
517 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
518 return;
519 }
520 }
521
522 item = (ProxyConnInfo *)SoftBusCalloc(sizeof(ProxyConnInfo));
523 if (item == NULL) {
524 TRANS_LOGE(TRANS_CTRL, "item calloc failed");
525 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
526 return;
527 }
528 item->ref++;
529 item->state = PROXY_CHANNEL_STATUS_PYH_CONNECTED;
530 TRANS_LOGI(TRANS_CTRL, "create conn proxyConnRef=%{public}d", item->ref);
531 item->connId = connId;
532 item->isServerSide = isServer;
533 TransConnInfoToConnOpt(&info, &item->connInfo);
534 ListAdd(&(g_proxyConnectionList->list), &(item->node));
535 TRANS_LOGI(TRANS_CTRL, "add connId=%{public}u", item->connId);
536 g_proxyConnectionList->cnt++;
537 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
538 return;
539 }
540
TransGetConn(const ConnectOption * connInfo,ProxyConnInfo * proxyConn,bool isServer)541 static int32_t TransGetConn(const ConnectOption *connInfo, ProxyConnInfo *proxyConn, bool isServer)
542 {
543 ProxyConnInfo *item = NULL;
544
545 if (g_proxyConnectionList == NULL) {
546 TRANS_LOGE(TRANS_CTRL, "proxy connection list not inited!");
547 return SOFTBUS_NO_INIT;
548 }
549
550 if (connInfo == NULL || proxyConn == NULL) {
551 TRANS_LOGE(TRANS_CTRL, "invalid para in trans get conn.");
552 return SOFTBUS_INVALID_PARAM;
553 }
554
555 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
556 TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
557 return SOFTBUS_LOCK_ERR;
558 }
559
560 LIST_FOR_EACH_ENTRY(item, &g_proxyConnectionList->list, ProxyConnInfo, node) {
561 if (item->connInfo.type != connInfo->type || item->isServerSide != isServer) {
562 continue;
563 }
564 if (CompareConnectOption(&item->connInfo, connInfo)) {
565 (void)memcpy_s(proxyConn, sizeof(ProxyConnInfo), item, sizeof(ProxyConnInfo));
566 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
567 return SOFTBUS_OK;
568 }
569 }
570 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
571 TRANS_LOGE(TRANS_CTRL, "can not find proxy conn in list.");
572 return SOFTBUS_TRANS_NOT_MATCH;
573 }
574
TransSetConnStateByReqId(uint32_t requestId,uint32_t connId,uint32_t state)575 static int32_t TransSetConnStateByReqId(uint32_t requestId, uint32_t connId, uint32_t state)
576 {
577 ProxyConnInfo *getNode = NULL;
578
579 if (g_proxyConnectionList == NULL) {
580 return SOFTBUS_NO_INIT;
581 }
582
583 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
584 TRANS_LOGE(TRANS_CTRL, "lock mutex fail!");
585 return SOFTBUS_LOCK_ERR;
586 }
587
588 LIST_FOR_EACH_ENTRY(getNode, &g_proxyConnectionList->list, ProxyConnInfo, node) {
589 if (getNode->requestId == requestId && getNode->state == PROXY_CHANNEL_STATUS_PYH_CONNECTING) {
590 getNode->state = state;
591 getNode->connId = connId;
592 getNode->requestId = 0;
593 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
594 return SOFTBUS_OK;
595 }
596 }
597 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
598 TRANS_LOGE(TRANS_CTRL,
599 "can not find proxy conn when set conn state. requestId=%{public}u, connId=%{public}u", requestId, connId);
600 (void)ConnDisconnectDevice(connId);
601 return SOFTBUS_TRANS_SESSION_INFO_NOT_FOUND;
602 }
603
TransOnConnectSucceed(uint32_t requestId,uint32_t connectionId,const ConnectionInfo * connInfo)604 static void TransOnConnectSucceed(uint32_t requestId, uint32_t connectionId, const ConnectionInfo *connInfo)
605 {
606 TransEventExtra extra = {
607 .socketName = NULL,
608 .peerNetworkId = NULL,
609 .calleePkg = NULL,
610 .callerPkg = NULL,
611 .requestId = (int32_t)requestId,
612 .connectionId = (int32_t)connectionId,
613 .result = EVENT_STAGE_RESULT_OK
614 };
615 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_START_CONNECT, extra);
616 TRANS_LOGI(TRANS_CTRL,
617 "Connect Success requestId=%{public}u, connId=%{public}u", requestId, connectionId);
618 int32_t ret = TransSetConnStateByReqId(requestId, connectionId, PROXY_CHANNEL_STATUS_PYH_CONNECTED);
619 TransProxyChanProcessByReqId((int32_t)requestId, connectionId, ret);
620 }
621
TransOnConnectFailed(uint32_t requestId,int32_t reason)622 static void TransOnConnectFailed(uint32_t requestId, int32_t reason)
623 {
624 TransEventExtra extra = {
625 .socketName = NULL,
626 .peerNetworkId = NULL,
627 .calleePkg = NULL,
628 .callerPkg = NULL,
629 .requestId = requestId,
630 .errcode = reason,
631 .result = EVENT_STAGE_RESULT_FAILED
632 };
633 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_START_CONNECT, extra);
634 TRANS_LOGE(TRANS_CTRL, "Connect fail requestId=%{public}u, reason=%{public}d", requestId, reason);
635 if (TransDelConnByReqId(requestId) != SOFTBUS_OK) {
636 TRANS_LOGE(TRANS_CTRL, "Connect fail del fail. requestId=%{public}u", requestId);
637 }
638 TransProxyDelChanByReqId((int32_t)requestId, reason);
639 }
640
TransProxyCloseConnChannel(uint32_t connectionId,bool isServer)641 int32_t TransProxyCloseConnChannel(uint32_t connectionId, bool isServer)
642 {
643 if (TransDecConnRefByConnId(connectionId, isServer) == SOFTBUS_OK) {
644 TRANS_LOGI(TRANS_CTRL, "disconnect device connId=%{public}d", connectionId);
645 // BR don't disconnect
646 (void)ConnDisconnectDevice(connectionId);
647 }
648 return SOFTBUS_OK;
649 }
650
TransProxyCloseConnChannelReset(uint32_t connectionId,bool isDisconnect,bool isServer,bool deviceType)651 int32_t TransProxyCloseConnChannelReset(uint32_t connectionId, bool isDisconnect, bool isServer, bool deviceType)
652 {
653 if (TransDecConnRefByConnId(connectionId, isServer) == SOFTBUS_OK) {
654 TRANS_LOGI(TRANS_CTRL, "reset dis device. isDisconnect=%{public}d, connId=%{public}u, deviceType=%{public}d",
655 isDisconnect, connectionId, deviceType);
656 // only client side can disconnect connection
657 if (isDisconnect && deviceType != true) {
658 (void)ConnDisconnectDevice(connectionId);
659 }
660 }
661 return SOFTBUS_OK;
662 }
663
TransProxyConnExistProc(bool isServer,int32_t chanNewId,const ConnectOption * connInfo)664 static int32_t TransProxyConnExistProc(bool isServer, int32_t chanNewId, const ConnectOption *connInfo)
665 {
666 SoftbusHitraceStart(SOFTBUS_HITRACE_ID_VALID, (uint64_t)(chanNewId + ID_OFFSET));
667 TRANS_LOGI(TRANS_CTRL,
668 "SoftBusHiTraceChainBegin: set hiTraceId=%{public}" PRIu64, (uint64_t)(chanNewId + ID_OFFSET));
669 int32_t ret = GetProxyChannelLock();
670 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_CTRL, "lock mutex fail!");
671 ProxyConnInfo conn;
672 if (TransGetConn(connInfo, &conn, false) != SOFTBUS_OK) {
673 ReleaseProxyChannelLock();
674 TRANS_LOGE(TRANS_CTRL, "get connectInfo failed, channelId=%{public}d", chanNewId);
675 return SOFTBUS_TRANS_PROXY_CONN_ADD_REF_FAILED;
676 }
677 ConnectType type = conn.connInfo.type;
678 if (conn.state == PROXY_CHANNEL_STATUS_PYH_CONNECTING) {
679 ProxyChannelInfo channelInfo = {
680 .channelId = chanNewId,
681 .reqId = (int32_t)conn.requestId,
682 .isServer = -1,
683 .type = type,
684 .status = PROXY_CHANNEL_STATUS_PYH_CONNECTING,
685 .connId = 0
686 };
687
688 TransProxySpecialUpdateChanInfo(&channelInfo);
689 ReleaseProxyChannelLock();
690 TRANS_LOGI(TRANS_CTRL, "reuse connection requestId=%{public}d", channelInfo.reqId);
691 } else {
692 ReleaseProxyChannelLock();
693 ProxyChannelInfo channelInfo = {
694 .channelId = chanNewId,
695 .reqId = -1,
696 .isServer = -1,
697 .type = type,
698 .status = PROXY_CHANNEL_STATUS_HANDSHAKEING,
699 .connId = conn.connId
700 };
701 if (TransAddConnRefByConnId(conn.connId, isServer) != SOFTBUS_OK) {
702 TRANS_LOGE(TRANS_CTRL, "TransAddConnRefByConnId: connId=%{public}u err", conn.connId);
703 return SOFTBUS_TRANS_PROXY_CONN_ADD_REF_FAILED;
704 }
705 TransProxySpecialUpdateChanInfo(&channelInfo);
706 TransProxyPostHandshakeMsgToLoop(chanNewId);
707 }
708 return SOFTBUS_OK;
709 }
710
TransProxyConnectDevice(const ConnectOption * connInfo,uint32_t requestId)711 static int32_t TransProxyConnectDevice(const ConnectOption *connInfo, uint32_t requestId)
712 {
713 ConnectResult result;
714 result.OnConnectFailed = TransOnConnectFailed;
715 result.OnConnectSuccessed = TransOnConnectSucceed;
716 if (connInfo->type == CONNECT_BLE_DIRECT) {
717 return ConnBleDirectConnectDevice(connInfo, requestId, &result);
718 } else {
719 return ConnConnectDevice(connInfo, requestId, &result);
720 }
721 }
722
TransProxyOpenNewConnChannel(ListenerModule moduleId,const ConnectOption * connInfo,int32_t channelId)723 static int32_t TransProxyOpenNewConnChannel(ListenerModule moduleId, const ConnectOption *connInfo, int32_t channelId)
724 {
725 SoftbusHitraceStart(SOFTBUS_HITRACE_ID_VALID, (uint64_t)(channelId + ID_OFFSET));
726 TRANS_LOGI(TRANS_CTRL,
727 "SoftBusHiTraceChainBegin: set hiTraceId=%{public}" PRIu64, (uint64_t)(channelId + ID_OFFSET));
728 uint32_t requestId = ConnGetNewRequestId(MODULE_PROXY_CHANNEL);
729 ProxyChannelInfo channelInfo = {
730 .channelId = channelId,
731 .reqId = (int32_t)requestId,
732 .isServer = 0,
733 .type = CONNECT_TYPE_MAX,
734 .status = PROXY_CHANNEL_STATUS_PYH_CONNECTING,
735 .connId = 0
736 };
737 TransProxySpecialUpdateChanInfo(&channelInfo);
738
739 ProxyConnInfo *connChan = (ProxyConnInfo *)SoftBusCalloc(sizeof(ProxyConnInfo));
740 if (connChan == NULL) {
741 TRANS_LOGE(TRANS_CTRL, "connChan calloc failed");
742 TransProxyDelChanByChanId(channelId);
743 return SOFTBUS_MALLOC_ERR;
744 }
745 connChan->requestId = requestId;
746 connChan->state = PROXY_CHANNEL_STATUS_PYH_CONNECTING;
747 connChan->ref = 0;
748 connChan->isServerSide = false;
749
750 TRANS_LOGI(TRANS_CTRL, "Connect dev, channelId=%{public}d, requestId=%{public}u", channelId, requestId);
751 connChan->connInfo = (*connInfo);
752 if (connInfo->type == CONNECT_TCP) {
753 connChan->connInfo.socketOption.moduleId = moduleId;
754 }
755 if (TransAddConnItem(connChan) != SOFTBUS_OK) {
756 TRANS_LOGE(TRANS_CTRL, "conn add repeat");
757 SoftBusFree(connChan);
758 return SOFTBUS_TRANS_PROXY_CONN_REPEAT;
759 }
760 int32_t ret = TransProxyConnectDevice(&connChan->connInfo, requestId);
761 if (ret != SOFTBUS_OK) {
762 TRANS_LOGE(TRANS_CTRL, "connect device err");
763 TransDelConnByReqId(requestId);
764 TransProxyDelChanByChanId(channelId);
765 }
766 return ret;
767 }
768
TransReportStartConnectEvent(const AppInfo * appInfo,const ConnectOption * connInfo,int32_t channelId)769 static void TransReportStartConnectEvent(const AppInfo *appInfo, const ConnectOption *connInfo, int32_t channelId)
770 {
771 ProxyChannelInfo *channelInfo = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
772 TRANS_CHECK_AND_RETURN_LOGE(channelInfo != NULL, TRANS_CTRL, "malloc proxyChannelInfo failed");
773 if (TransProxyGetChanByChanId(channelId, channelInfo) != SOFTBUS_OK) {
774 SoftBusFree(channelInfo);
775 TRANS_LOGE(TRANS_CTRL, "TransProxyGetChanByChanId failed, channelId=%{public}d", channelId);
776 return;
777 }
778 TransEventExtra extra = {
779 .peerNetworkId = NULL,
780 .calleePkg = NULL,
781 .callerPkg = NULL,
782 .socketName = appInfo->myData.sessionName,
783 .channelType = appInfo->appType == APP_TYPE_AUTH ? CHANNEL_TYPE_AUTH : CHANNEL_TYPE_PROXY,
784 .channelId = channelId,
785 .requestId = channelInfo->reqId,
786 .linkType = connInfo->type
787 };
788 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_START_CONNECT, extra);
789 SoftBusFree(channelInfo);
790 }
791
TransProxyOpenConnChannel(const AppInfo * appInfo,const ConnectOption * connInfo,int32_t * channelId)792 int32_t TransProxyOpenConnChannel(const AppInfo *appInfo, const ConnectOption *connInfo, int32_t *channelId)
793 {
794 if (appInfo == NULL || connInfo == NULL) {
795 TRANS_LOGE(TRANS_CTRL, "invalid param");
796 return SOFTBUS_INVALID_PARAM;
797 }
798 int32_t ret;
799 ProxyConnInfo conn;
800 int32_t chanNewId = INVALID_CHANNEL_ID;
801 if (*channelId != INVALID_CHANNEL_ID) {
802 chanNewId = *channelId;
803 } else {
804 chanNewId = GenerateChannelId(false);
805 }
806 if (chanNewId <= INVALID_CHANNEL_ID) {
807 TRANS_LOGE(TRANS_CTRL, "proxy channelId is invalid");
808 return SOFTBUS_TRANS_INVALID_CHANNEL_ID;
809 }
810 ProxyChannelInfo *chan = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
811 if (chan == NULL) {
812 ReleaseProxyChannelId(chanNewId);
813 TRANS_LOGE(TRANS_CTRL, "SoftBusCalloc fail");
814 return SOFTBUS_MALLOC_ERR;
815 }
816 chan->type = connInfo->type;
817 if (TransProxyCreateChanInfo(chan, chanNewId, appInfo) != SOFTBUS_OK) {
818 TRANS_LOGE(TRANS_CTRL, "TransProxyCreateChanInfo err");
819 ReleaseProxyChannelId(chanNewId);
820 (void)memset_s(chan->appInfo.sessionKey, sizeof(chan->appInfo.sessionKey), 0, sizeof(chan->appInfo.sessionKey));
821 SoftBusFree(chan);
822 return SOFTBUS_TRANS_PROXY_CREATE_CHANNEL_FAILED;
823 }
824 if (TransGetConn(connInfo, &conn, false) == SOFTBUS_OK) {
825 ret = TransProxyConnExistProc(false, chanNewId, connInfo);
826 if (ret == SOFTBUS_TRANS_PROXY_CONN_ADD_REF_FAILED) {
827 ret = TransProxyOpenNewConnChannel(PROXY, connInfo, chanNewId);
828 }
829 } else {
830 ret = TransProxyOpenNewConnChannel(PROXY, connInfo, chanNewId);
831 if ((ret == SOFTBUS_TRANS_PROXY_CONN_REPEAT) && (TransGetConn(connInfo, &conn, false) == SOFTBUS_OK)) {
832 ret = TransProxyConnExistProc(false, chanNewId, connInfo);
833 }
834 }
835 if (ret == SOFTBUS_OK) {
836 *channelId = chanNewId;
837 } else if (ret == SOFTBUS_TRANS_PROXY_CONN_ADD_REF_FAILED || ret == SOFTBUS_TRANS_PROXY_CONN_REPEAT) {
838 TransProxyDelChanByChanId(chanNewId);
839 }
840 TransReportStartConnectEvent(appInfo, connInfo, chanNewId);
841 return ret;
842 }
843
TransProxySendBadKeyMessage(ProxyMessage * msg,const AuthHandle * authHandle)844 static int32_t TransProxySendBadKeyMessage(ProxyMessage *msg, const AuthHandle *authHandle)
845 {
846 ProxyDataInfo dataInfo;
847 dataInfo.inData = (uint8_t *)msg->data;
848 dataInfo.inLen = (uint32_t)msg->dateLen;
849 dataInfo.outData = NULL;
850 dataInfo.outLen = 0;
851
852 msg->msgHead.type = (PROXYCHANNEL_MSG_TYPE_RESET & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
853 if (AuthCheckSessionKeyValidByAuthHandle(authHandle) == SOFTBUS_AUTH_SESSION_KEY_INVALID) {
854 TRANS_LOGE(TRANS_MSG, "ble single online, send renegotiate msg");
855 msg->msgHead.cipher |= AUTH_SINGLE_CIPHER;
856 } else {
857 msg->msgHead.cipher |= BAD_CIPHER;
858 }
859 TRANS_LOGW(TRANS_MSG, "send msg is bad key myChannelId=%{public}d, peerChannelId=%{public}d",
860 msg->msgHead.myId, msg->msgHead.peerId);
861
862 int32_t ret = PackPlaintextMessage(&msg->msgHead, &dataInfo);
863 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_MSG, "PackPlaintextMessage fail");
864
865 ret = TransProxyTransSendMsg(msg->connId, dataInfo.outData, dataInfo.outLen, CONN_HIGH, 0);
866 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_MSG, "send bad key buf fail");
867
868 return SOFTBUS_OK;
869 }
870
TransProxyOnDataReceived(uint32_t connectionId,ConnModule moduleId,int64_t seq,char * data,int32_t len)871 static void TransProxyOnDataReceived(uint32_t connectionId, ConnModule moduleId, int64_t seq, char *data, int32_t len)
872 {
873 TRANS_LOGI(TRANS_CTRL, "recv data connId=%{public}u, moduleId=%{public}d, seq=%{public}" PRId64 ", len=%{public}d",
874 connectionId, moduleId, seq, len);
875 TRANS_CHECK_AND_RETURN_LOGE(data != NULL && moduleId == MODULE_PROXY_CHANNEL, TRANS_CTRL, "invalid param");
876
877 ProxyMessage msg;
878 (void)memset_s(&msg, sizeof(ProxyMessage), 0, sizeof(ProxyMessage));
879 msg.connId = connectionId;
880
881 AuthHandle authHandle = { .authId = AUTH_INVALID_ID };
882 int32_t ret = TransProxyParseMessage((char *)data, len, &msg, &authHandle);
883 if (((ret == SOFTBUS_AUTH_NOT_FOUND) || (ret == SOFTBUS_DECRYPT_ERR)) &&
884 (msg.msgHead.type == PROXYCHANNEL_MSG_TYPE_HANDSHAKE)) {
885 TransReportBadKeyEvent(ret, connectionId, seq, len);
886 if (TransProxySendBadKeyMessage(&msg, &authHandle) != SOFTBUS_OK) {
887 TRANS_LOGE(TRANS_CTRL, "send bad key msg ret=%{public}d", ret);
888 return;
889 }
890 char peerBrMac[BT_MAC_LEN] = {0};
891 char udid[UDID_BUF_LEN] = {0};
892 if (GetBrMacFromConnInfo(connectionId, peerBrMac, BT_MAC_LEN) == SOFTBUS_OK) {
893 if (LnnGetUdidByBrMac(peerBrMac, udid, UDID_BUF_LEN) == SOFTBUS_OK) {
894 AuthRemoveDeviceKeyByUdid(udid);
895 }
896 }
897 }
898 if (ret != SOFTBUS_OK) {
899 TRANS_LOGE(TRANS_CTRL, "parse proxy msg ret=%{public}d", ret);
900 return;
901 }
902 TransProxyOnMessageReceived(&msg);
903 SoftBusFree(msg.data);
904 }
905
TransProxyTransInit(void)906 int32_t TransProxyTransInit(void)
907 {
908 ConnectCallback proxyCallback = { 0 };
909
910 proxyCallback.OnConnected = TransProxyOnConnected;
911 proxyCallback.OnDisconnected = TransProxyOnDisConnect;
912 proxyCallback.OnDataReceived = TransProxyOnDataReceived;
913 int32_t ret = ConnSetConnectCallback(MODULE_PROXY_CHANNEL, &proxyCallback);
914 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "ConnSetConnectCallback fail");
915
916 g_proxyConnectionList = CreateSoftBusList();
917 if (g_proxyConnectionList == NULL) {
918 TRANS_LOGE(TRANS_INIT, "create observer list failed");
919 return SOFTBUS_MALLOC_ERR;
920 }
921
922 ret = TransProxyLoopInit();
923 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "create loopInit fail");
924
925 ret = TransProxyPipelineInit();
926 TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_INIT, "init proxy pipeline failed");
927
928 return SOFTBUS_OK;
929 }
930
TransProxyGetConnInfoByConnId(uint32_t connId,ConnectOption * connInfo)931 int32_t TransProxyGetConnInfoByConnId(uint32_t connId, ConnectOption *connInfo)
932 {
933 if (connInfo == NULL) {
934 TRANS_LOGW(TRANS_CTRL, "invalid param.");
935 return SOFTBUS_INVALID_PARAM;
936 }
937
938 if (g_proxyConnectionList == NULL) {
939 TRANS_LOGE(TRANS_CTRL, "proxy connect list empty.");
940 return SOFTBUS_NO_INIT;
941 }
942
943 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
944 TRANS_LOGE(TRANS_CTRL, "lock mutex fail.");
945 return SOFTBUS_LOCK_ERR;
946 }
947
948 ProxyConnInfo *item = NULL;
949 LIST_FOR_EACH_ENTRY(item, &g_proxyConnectionList->list, ProxyConnInfo, node) {
950 if (item->connId == connId) {
951 if (memcpy_s(connInfo, sizeof(ConnectOption), &(item->connInfo), sizeof(ConnectOption)) != EOK) {
952 TRANS_LOGE(TRANS_CTRL, "memcpy_s connInfo failed. connId=%{public}u", connId);
953 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
954 return SOFTBUS_MEM_ERR;
955 }
956 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
957 return SOFTBUS_OK;
958 }
959 }
960 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
961 TRANS_LOGE(TRANS_INIT, "proxy conn node not found. connId=%{public}u", connId);
962 return SOFTBUS_TRANS_SESSION_INFO_NOT_FOUND;
963 }
964
CheckIsProxyAuthChannel(ConnectOption * connInfo)965 int32_t CheckIsProxyAuthChannel(ConnectOption *connInfo)
966 {
967 if (connInfo == NULL) {
968 TRANS_LOGW(TRANS_CTRL, "invalid param.");
969 return SOFTBUS_INVALID_PARAM;
970 }
971
972 if (g_proxyConnectionList == NULL) {
973 TRANS_LOGE(TRANS_CTRL, "proxy connect list empty.");
974 return SOFTBUS_NO_INIT;
975 }
976
977 if (SoftBusMutexLock(&g_proxyConnectionList->lock) != SOFTBUS_OK) {
978 TRANS_LOGE(TRANS_CTRL, "lock mutex fail.");
979 return SOFTBUS_LOCK_ERR;
980 }
981
982 ProxyConnInfo *item = NULL;
983 LIST_FOR_EACH_ENTRY(item, &g_proxyConnectionList->list, ProxyConnInfo, node) {
984 if (memcmp(item->connInfo.bleOption.bleMac, connInfo->bleOption.bleMac,
985 sizeof(connInfo->bleOption.bleMac)) == 0 ||
986 memcmp(item->connInfo.bleOption.deviceIdHash, connInfo->bleOption.deviceIdHash,
987 SHORT_UDID_HASH_LEN) == 0) {
988 TRANS_LOGI(TRANS_CTRL, "auth channel type is ble");
989 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
990 return SOFTBUS_OK;
991 } else if (memcmp(item->connInfo.brOption.brMac, connInfo->brOption.brMac,
992 sizeof(connInfo->brOption.brMac)) == 0) {
993 TRANS_LOGI(TRANS_CTRL, "auth channel type is br");
994 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
995 return SOFTBUS_OK;
996 }
997 }
998 (void)SoftBusMutexUnlock(&g_proxyConnectionList->lock);
999 TRANS_LOGE(TRANS_INIT, "proxy conn node not found.");
1000 return SOFTBUS_TRANS_SESSION_INFO_NOT_FOUND;
1001 }
1002
TransProxyUdpateNewPeerUdidHash(const char * deviceId,ConnectOption * connOpt)1003 static int32_t TransProxyUdpateNewPeerUdidHash(const char *deviceId, ConnectOption *connOpt)
1004 {
1005 uint8_t udidHash[UDID_HASH_LEN] = { 0 };
1006 int32_t ret = SoftBusGenerateStrHash((const unsigned char *)deviceId, strlen(deviceId), udidHash);
1007 if (ret != SOFTBUS_OK) {
1008 TRANS_LOGE(TRANS_CTRL, "Generate UDID HexStringHash fail, ret=%{public}d", ret);
1009 return ret;
1010 }
1011 if (memcpy_s(connOpt->bleOption.deviceIdHash, UDID_HASH_LEN, udidHash, UDID_HASH_LEN) != EOK) {
1012 TRANS_LOGE(TRANS_CTRL, "memcpy_s udid hash err");
1013 return SOFTBUS_MEM_ERR;
1014 }
1015 return SOFTBUS_OK;
1016 }
1017
TransProxyGetConnOptionByChanId(int32_t channelId,ConnectOption * connOpt)1018 int32_t TransProxyGetConnOptionByChanId(int32_t channelId, ConnectOption *connOpt)
1019 {
1020 TRANS_CHECK_AND_RETURN_RET_LOGE(connOpt != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "invalid param");
1021 ProxyChannelInfo chan;
1022 (void)memset_s(&chan, sizeof(ProxyChannelInfo), 0, sizeof(ProxyChannelInfo));
1023 int32_t ret = TransProxyGetProxyChannelInfoByChannelId(channelId, &chan);
1024 if (ret != SOFTBUS_OK) {
1025 TRANS_LOGE(TRANS_CTRL, "get proxy channel info fail, channelId=%{public}d, ret=%{public}d", channelId, ret);
1026 return ret;
1027 }
1028
1029 ret = TransProxyGetConnInfoByConnId(chan.connId, connOpt);
1030 if (ret != SOFTBUS_OK) {
1031 TRANS_LOGE(TRANS_CTRL, "get conn optinfo fail, channelId=%{public}d, connId=%{public}d, ret=%{public}d",
1032 chan.channelId, chan.connId, ret);
1033 return ret;
1034 }
1035
1036 ret = TransProxyUdpateNewPeerUdidHash(chan.appInfo.peerData.deviceId, connOpt);
1037 if (ret != SOFTBUS_OK) {
1038 TRANS_LOGE(TRANS_CTRL, "get new peer udid hash fail, channelId=%{public}d, connId=%{public}d, ret=%{public}d",
1039 chan.channelId, chan.connId, ret);
1040 return ret;
1041 }
1042
1043 char udidHashStr[HEXIFY_LEN(SHORT_UDID_HASH_LEN)] = { 0 };
1044 ret = ConvertBytesToHexString(udidHashStr, HEXIFY_LEN(SHORT_UDID_HASH_LEN),
1045 (unsigned char *)connOpt->bleOption.deviceIdHash, SHORT_UDID_HASH_LEN);
1046 if (ret != SOFTBUS_OK) {
1047 TRANS_LOGE(
1048 TRANS_CTRL, "convert udid hash to string fail, channelId=%{public}d, connId=%{public}d, ret=%{public}d",
1049 chan.channelId, chan.connId, ret);
1050 return ret;
1051 }
1052
1053 char *udidHash = NULL;
1054 Anonymize(udidHashStr, &udidHash);
1055 TRANS_LOGI(TRANS_CTRL, "channelId=%{public}d, connId=%{public}d, udidHash=%{public}s",
1056 chan.channelId, chan.connId, AnonymizeWrapper(udidHash));
1057 AnonymizeFree(udidHash);
1058 return SOFTBUS_OK;
1059 }
1060
TransProxyNegoSessionKeyFail(int32_t channelId,int32_t errCode)1061 void TransProxyNegoSessionKeyFail(int32_t channelId, int32_t errCode)
1062 {
1063 ProxyChannelInfo *channelInfo = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1064 TRANS_CHECK_AND_RETURN_LOGE(channelInfo != NULL, TRANS_CTRL, "malloc proxyChannelInfo failed");
1065
1066 if (TransProxyGetChanByChanId(channelId, channelInfo) != SOFTBUS_OK) {
1067 SoftBusFree(channelInfo);
1068 TRANS_LOGE(TRANS_CTRL, "disconnect device channelId=%{public}d", channelId);
1069 return;
1070 }
1071
1072 (void)TransProxyCloseConnChannel(channelInfo->connId, channelInfo->isServer);
1073 (void)OnProxyChannelOpenFailed(channelId, &(channelInfo->appInfo), errCode);
1074 (void)memset_s(channelInfo->appInfo.sessionKey, sizeof(channelInfo->appInfo.sessionKey), 0,
1075 sizeof(channelInfo->appInfo.sessionKey));
1076 SoftBusFree(channelInfo);
1077 TransProxyDelChanByChanId(channelId);
1078 }
1079
TransProxyNegoSessionKeySucc(int32_t channelId)1080 void TransProxyNegoSessionKeySucc(int32_t channelId)
1081 {
1082 ProxyChannelInfo *channelInfo = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
1083 TRANS_CHECK_AND_RETURN_LOGE(channelInfo != NULL, TRANS_CTRL, "malloc proxyChannelInfo failed");
1084
1085 if (TransProxyGetChanByChanId(channelId, channelInfo) != SOFTBUS_OK) {
1086 SoftBusFree(channelInfo);
1087 TRANS_LOGE(TRANS_CTRL, "disconnect device channelId=%{public}d", channelId);
1088 return;
1089 }
1090
1091 channelInfo->appInfo.connectedStart = GetSoftbusRecordTimeMillis();
1092 int32_t ret = TransProxyHandshake(channelInfo);
1093 if (ret != SOFTBUS_OK) {
1094 TransEventExtra extra = {
1095 .socketName = NULL,
1096 .peerNetworkId = NULL,
1097 .calleePkg = NULL,
1098 .callerPkg = NULL,
1099 .channelId = channelId,
1100 .connectionId = channelInfo->connId,
1101 .errcode = ret,
1102 .result = EVENT_STAGE_RESULT_FAILED
1103 };
1104 TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_START, extra);
1105 (void)TransProxyCloseConnChannel(channelInfo->connId, channelInfo->isServer);
1106 TRANS_LOGE(TRANS_CTRL, "channelId=%{public}d handshake err, ret=%{public}d", channelId, ret);
1107 TransProxyOpenProxyChannelFail(channelInfo->channelId, &(channelInfo->appInfo), ret);
1108 TransProxyDelChanByChanId(channelId);
1109 }
1110 (void)memset_s(channelInfo->appInfo.sessionKey, sizeof(channelInfo->appInfo.sessionKey), 0,
1111 sizeof(channelInfo->appInfo.sessionKey));
1112 SoftBusFree(channelInfo);
1113 }
1114