• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "client_trans_tcp_direct_manager.h"
17 
18 #include <securec.h>
19 
20 #include "client_trans_tcp_direct_callback.h"
21 #include "client_trans_tcp_direct_listener.h"
22 #include "softbus_adapter_mem.h"
23 #include "softbus_base_listener.h"
24 #include "softbus_def.h"
25 #include "softbus_errcode.h"
26 #include "softbus_socket.h"
27 #include "softbus_utils.h"
28 #include "trans_log.h"
29 #include "trans_pending_pkt.h"
30 #include "trans_server_proxy.h"
31 
32 #define HEART_TIME 300
33 #define TCP_KEEPALIVE_INTERVAL 2
34 #define TCP_KEEPALIVE_COUNT 5
35 #define USER_TIME_OUT (305 * 1000)
36 
37 static SoftBusList *g_tcpDirectChannelInfoList = NULL;
38 
CheckInfoAndMutexLock(TcpDirectChannelInfo * info)39 static bool CheckInfoAndMutexLock(TcpDirectChannelInfo *info)
40 {
41     if (info == NULL) {
42         TRANS_LOGE(TRANS_SDK, "param invalid.");
43         return false;
44     }
45     if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
46         TRANS_LOGE(TRANS_SDK, "lock failed");
47         return false;
48     }
49     return true;
50 }
51 
TransTdcGetInfoById(int32_t channelId,TcpDirectChannelInfo * info)52 TcpDirectChannelInfo *TransTdcGetInfoById(int32_t channelId, TcpDirectChannelInfo *info)
53 {
54     if (!CheckInfoAndMutexLock(info)) {
55         return NULL;
56     }
57 
58     TcpDirectChannelInfo *item = NULL;
59     LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
60         if (item->channelId == channelId) {
61             (void)memcpy_s(info, sizeof(TcpDirectChannelInfo), item, sizeof(TcpDirectChannelInfo));
62             (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
63             return item;
64         }
65     }
66 
67     (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
68     return NULL;
69 }
70 
TransTdcSetListenerStateById(int32_t channelId,bool needStopListener)71 int32_t TransTdcSetListenerStateById(int32_t channelId, bool needStopListener)
72 {
73     if (g_tcpDirectChannelInfoList == NULL) {
74         TRANS_LOGE(TRANS_SDK, "g_tcpDirectChannelInfoList is NULL, channelId=%{public}d", channelId);
75         return SOFTBUS_INVALID_PARAM;
76     }
77     if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
78         TRANS_LOGE(TRANS_SDK, "lock failed, channelId=%{public}d", channelId);
79         return SOFTBUS_LOCK_ERR;
80     }
81 
82     TcpDirectChannelInfo *item = NULL;
83     LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
84         if (item->channelId == channelId) {
85             item->detail.needStopListener = needStopListener;
86             (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
87             TRANS_LOGI(TRANS_SDK, "succ, channelId=%{public}d, needStopListener=%{public}d", channelId,
88                 needStopListener);
89             return SOFTBUS_OK;
90         }
91     }
92 
93     (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
94     TRANS_LOGE(TRANS_SDK, "channel not found, channelId=%{public}d", channelId);
95     return SOFTBUS_NOT_FIND;
96 }
97 
TransTdcGetInfoByIdWithIncSeq(int32_t channelId,TcpDirectChannelInfo * info)98 TcpDirectChannelInfo *TransTdcGetInfoByIdWithIncSeq(int32_t channelId, TcpDirectChannelInfo *info)
99 {
100     if (!CheckInfoAndMutexLock(info)) {
101         return NULL;
102     }
103 
104     TcpDirectChannelInfo *item = NULL;
105     LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
106         if (item->channelId == channelId) {
107             (void)memcpy_s(info, sizeof(TcpDirectChannelInfo), item, sizeof(TcpDirectChannelInfo));
108             item->detail.sequence++;
109             item->detail.fdRefCnt++;
110             (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
111             return item;
112         }
113     }
114 
115     (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
116     return NULL;
117 }
118 
TransTdcGetInfoByFd(int32_t fd,TcpDirectChannelInfo * info)119 TcpDirectChannelInfo *TransTdcGetInfoByFd(int32_t fd, TcpDirectChannelInfo *info)
120 {
121     if (!CheckInfoAndMutexLock(info)) {
122         return NULL;
123     }
124 
125     TcpDirectChannelInfo *item = NULL;
126     LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
127         if (item->detail.fd == fd) {
128             (void)memcpy_s(info, sizeof(TcpDirectChannelInfo), item, sizeof(TcpDirectChannelInfo));
129             (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
130             return item;
131         }
132     }
133 
134     (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
135     return NULL;
136 }
137 
TransTdcCloseChannel(int32_t channelId)138 void TransTdcCloseChannel(int32_t channelId)
139 {
140     TRANS_LOGI(TRANS_SDK, "Close tdc Channel, channelId=%{public}d.", channelId);
141     if (ServerIpcCloseChannel(NULL, channelId, CHANNEL_TYPE_TCP_DIRECT) != SOFTBUS_OK) {
142         TRANS_LOGE(TRANS_SDK, "close server tdc channelId=%{public}d err.", channelId);
143     }
144 
145     TcpDirectChannelInfo *item = NULL;
146     if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
147         TRANS_LOGE(TRANS_SDK, "lock failed");
148         return;
149     }
150 
151     LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
152         if (item->channelId != channelId) {
153             continue;
154         }
155         TransTdcReleaseFd(item->detail.fd);
156         item->detail.needRelease = true;
157         if (item->detail.fdRefCnt <= 0) {
158             SoftBusMutexDestroy(&(item->detail.fdLock));
159             ListDelete(&item->node);
160             SoftBusFree(item);
161             item = NULL;
162         }
163         (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
164         DelPendingPacket(channelId, PENDING_TYPE_DIRECT);
165         TRANS_LOGI(TRANS_SDK, "Delete tdc item success. channelId=%{public}d", channelId);
166         return;
167     }
168 
169     TRANS_LOGE(TRANS_SDK, "Target item not exist. channelId=%{public}d", channelId);
170     (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
171 }
172 
TransGetNewTcpChannel(const ChannelInfo * channel)173 static TcpDirectChannelInfo *TransGetNewTcpChannel(const ChannelInfo *channel)
174 {
175     if (channel == NULL) {
176         TRANS_LOGE(TRANS_SDK, "param invalid");
177         return NULL;
178     }
179     TcpDirectChannelInfo *item = (TcpDirectChannelInfo *)SoftBusCalloc(sizeof(TcpDirectChannelInfo));
180     if (item == NULL) {
181         TRANS_LOGE(TRANS_SDK, "calloc failed");
182         return NULL;
183     }
184     item->channelId = channel->channelId;
185     item->detail.fd = channel->fd;
186     item->detail.channelType = channel->channelType;
187     if (SoftBusMutexInit(&(item->detail.fdLock), NULL) != SOFTBUS_OK) {
188         SoftBusFree(item);
189         TRANS_LOGE(TRANS_SDK, "init fd lock failed");
190         return NULL;
191     }
192     if (memcpy_s(item->detail.sessionKey, SESSION_KEY_LENGTH, channel->sessionKey, SESSION_KEY_LENGTH) != EOK) {
193         SoftBusFree(item);
194         TRANS_LOGE(TRANS_SDK, "sessionKey copy failed");
195         return NULL;
196     }
197     if (strcpy_s(item->detail.myIp, IP_LEN, channel->myIp) != EOK) {
198         SoftBusFree(item);
199         TRANS_LOGE(TRANS_SDK, "myIp copy failed");
200         return NULL;
201     }
202     return item;
203 }
204 
ClientTransCheckTdcChannelExist(int32_t channelId)205 static int32_t ClientTransCheckTdcChannelExist(int32_t channelId)
206 {
207     if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
208         TRANS_LOGE(TRANS_SDK, "lock failed.");
209         return SOFTBUS_LOCK_ERR;
210     }
211     TcpDirectChannelInfo *item = NULL;
212     LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
213         if (item->channelId == channelId) {
214             TRANS_LOGE(TRANS_SDK, "tcp direct already exist. channelId=%{public}d", channelId);
215             (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
216             return SOFTBUS_TRANS_TDC_CHANNEL_ALREADY_EXIST;
217         }
218     }
219     (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
220     return SOFTBUS_OK;
221 }
222 
TransTdcDelChannelInfo(int32_t channelId,int32_t errCode)223 static void TransTdcDelChannelInfo(int32_t channelId, int32_t errCode)
224 {
225     TRANS_LOGI(TRANS_SDK, "Delete tdc channelId=%{public}d.", channelId);
226 
227     TcpDirectChannelInfo *item = NULL;
228     TcpDirectChannelInfo *nextNode = NULL;
229     if (g_tcpDirectChannelInfoList == NULL) {
230         return;
231     }
232     if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
233         TRANS_LOGE(TRANS_SDK, "lock failed");
234         return;
235     }
236 
237     LIST_FOR_EACH_ENTRY_SAFE(item, nextNode, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
238         if (item->channelId == channelId) {
239             if (errCode == SOFTBUS_TRANS_NEGOTIATE_REJECTED) {
240                 TransTdcCloseFd(item->detail.fd);
241                 TRANS_LOGI(
242                     TRANS_SDK, "Server reject conn, channelId=%{public}d, fd=%{public}d", channelId, item->detail.fd);
243             } else {
244                 TransTdcReleaseFd(item->detail.fd);
245             }
246             ListDelete(&item->node);
247             SoftBusMutexDestroy(&(item->detail.fdLock));
248             SoftBusFree(item);
249             item = NULL;
250             (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
251             TRANS_LOGI(TRANS_SDK, "Delete tdc item success. channelId=%{public}d", channelId);
252             return;
253         }
254     }
255 
256     TRANS_LOGE(TRANS_SDK, "Target item not exist. channelId=%{public}d", channelId);
257     (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
258 }
259 
ClientTransTdcHandleListener(const char * sessionName,const ChannelInfo * channel)260 static int32_t ClientTransTdcHandleListener(const char *sessionName, const ChannelInfo *channel)
261 {
262     bool isSocket = false;
263     int32_t ret = ClientTransTdcIfChannelForSocket(sessionName, &isSocket);
264     if (ret != SOFTBUS_OK) {
265         TRANS_LOGE(TRANS_SDK, "get channel socket fail, channelId=%{public}d", channel->channelId);
266         return ret;
267     }
268 
269     if (channel->isServer && isSocket) {
270         TRANS_LOGI(TRANS_SDK, "no need listen here, channelId=%{public}d", channel->channelId);
271         return SOFTBUS_OK;
272     }
273     ret = TransTdcCreateListener(channel->fd);
274     if (ret != SOFTBUS_OK) {
275         TRANS_LOGE(TRANS_SDK, "create listener fail, channelId=%{public}d", channel->channelId);
276         return ret;
277     }
278 
279     TcpDirectChannelInfo info;
280     (void)memset_s(&info, sizeof(TcpDirectChannelInfo), 0, sizeof(TcpDirectChannelInfo));
281     TcpDirectChannelInfo *res = TransTdcGetInfoById(channel->channelId, &info);
282     if (res == NULL) {
283         DelTrigger(DIRECT_CHANNEL_CLIENT, channel->fd, READ_TRIGGER);
284         TRANS_LOGE(TRANS_SDK, "TransTdcGetInfoById failed, channelId=%{public}d", channel->channelId);
285         return SOFTBUS_NOT_FIND;
286     }
287 
288     if (info.detail.needStopListener) {
289         (void)TransTdcStopRead(channel->fd);
290         TRANS_LOGI(TRANS_SDK, "listener has been disabled, stop read now, channelId=%{public}d", channel->channelId);
291     }
292     return SOFTBUS_OK;
293 }
294 
ClientTransSetTcpOption(int32_t fd)295 static int32_t ClientTransSetTcpOption(int32_t fd)
296 {
297     int32_t ret = ConnSetTcpKeepalive(fd, HEART_TIME, TCP_KEEPALIVE_INTERVAL, TCP_KEEPALIVE_COUNT);
298     if (ret != SOFTBUS_OK) {
299         TRANS_LOGE(TRANS_SDK, "ConnSetTcpKeepalive failed, fd=%{public}d.", fd);
300         return ret;
301     }
302     ret = ConnSetTcpUserTimeOut(fd, USER_TIME_OUT);
303     if (ret != SOFTBUS_OK) {
304         TRANS_LOGE(TRANS_SDK, "ConnSetTcpUserTimeOut failed, fd=%{public}d.", fd);
305         return ret;
306     }
307     return SOFTBUS_OK;
308 }
309 
ClientTransTdcOnChannelOpened(const char * sessionName,const ChannelInfo * channel)310 int32_t ClientTransTdcOnChannelOpened(const char *sessionName, const ChannelInfo *channel)
311 {
312     TRANS_CHECK_AND_RETURN_RET_LOGE(sessionName != NULL && channel != NULL,
313         SOFTBUS_INVALID_PARAM, TRANS_SDK, "param invalid");
314 
315     int32_t ret = ClientTransCheckTdcChannelExist(channel->channelId);
316     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret, TRANS_FILE, "check tdc channel fail!");
317 
318     TcpDirectChannelInfo *item = TransGetNewTcpChannel(channel);
319     TRANS_CHECK_AND_RETURN_RET_LOGE(item != NULL, SOFTBUS_MEM_ERR,
320         TRANS_SDK, "get new tcp channel err. channelId=%{public}d", channel->channelId);
321     ret = TransAddDataBufNode(channel->channelId, channel->fd);
322     if (ret != SOFTBUS_OK) {
323         TRANS_LOGE(TRANS_SDK, "add node fail. channelId=%{public}d, fd=%{public}d", channel->channelId, channel->fd);
324         SoftBusFree(item);
325         return ret;
326     }
327 
328     ret = ClientTransSetTcpOption(channel->fd);
329     if (ret != SOFTBUS_OK) {
330         goto EXIT_ERR;
331     }
332     ret = SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock);
333     if (ret != SOFTBUS_OK) {
334         TRANS_LOGE(TRANS_SDK, "lock failed.");
335         goto EXIT_ERR;
336     }
337     ListAdd(&g_tcpDirectChannelInfoList->list, &item->node);
338     TRANS_LOGI(TRANS_SDK, "add channelId=%{public}d, fd=%{public}d", item->channelId, channel->fd);
339     (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
340 
341     ret = ClientTransTdcOnSessionOpened(sessionName, channel);
342     if (ret != SOFTBUS_OK) {
343         TransDelDataBufNode(channel->channelId);
344         TransTdcDelChannelInfo(channel->channelId, ret);
345         TRANS_LOGE(TRANS_SDK, "notify on session opened err.");
346         return ret;
347     }
348 
349     ret = ClientTransTdcHandleListener(sessionName, channel);
350     if (ret != SOFTBUS_OK) {
351         ClientTransTdcOnSessionClosed(channel->channelId, SHUTDOWN_REASON_LOCAL);
352         TransDelDataBufNode(channel->channelId);
353         TransTdcDelChannelInfo(channel->channelId, ret);
354         return ret;
355     }
356 
357     return SOFTBUS_OK;
358 EXIT_ERR:
359     TransDelDataBufNode(channel->channelId);
360     SoftBusFree(item);
361     return ret;
362 }
363 
TransTdcManagerInit(const IClientSessionCallBack * callback)364 int32_t TransTdcManagerInit(const IClientSessionCallBack *callback)
365 {
366     g_tcpDirectChannelInfoList = CreateSoftBusList();
367     if (g_tcpDirectChannelInfoList == NULL || TransDataListInit() != SOFTBUS_OK) {
368         TRANS_LOGE(TRANS_INIT, "init tcp direct channel fail.");
369         return SOFTBUS_NO_INIT;
370     }
371     int32_t ret = ClientTransTdcSetCallBack(callback);
372     if (ret != SOFTBUS_OK) {
373         TRANS_LOGE(TRANS_INIT, "ClientTransTdcSetCallBack fail, ret=%{public}d", ret);
374         return ret;
375     }
376     ret = PendingInit(PENDING_TYPE_DIRECT);
377     if (ret != SOFTBUS_OK) {
378         TRANS_LOGE(TRANS_INIT, "trans direct pending init failed, ret=%{public}d", ret);
379         return SOFTBUS_NO_INIT;
380     }
381     TRANS_LOGE(TRANS_INIT, "init tcp direct channel success.");
382     return SOFTBUS_OK;
383 }
384 
TransTdcManagerDeinit(void)385 void TransTdcManagerDeinit(void)
386 {
387     if (g_tcpDirectChannelInfoList == NULL) {
388         return;
389     }
390 
391     TransDataListDeinit();
392     DestroySoftBusList(g_tcpDirectChannelInfoList);
393     g_tcpDirectChannelInfoList = NULL;
394     PendingDeinit(PENDING_TYPE_DIRECT);
395 }
396 
ClientTransTdcOnChannelOpenFailed(int32_t channelId,int32_t errCode)397 int32_t ClientTransTdcOnChannelOpenFailed(int32_t channelId, int32_t errCode)
398 {
399     return ClientTransTdcOnSessionOpenFailed(channelId, errCode);
400 }
401 
TransTdcGetSessionKey(int32_t channelId,char * key,unsigned int len)402 int32_t TransTdcGetSessionKey(int32_t channelId, char *key, unsigned int len)
403 {
404     if (key == NULL) {
405         TRANS_LOGW(TRANS_SDK, "invalid param.");
406         return SOFTBUS_INVALID_PARAM;
407     }
408     TcpDirectChannelInfo channel;
409     if (TransTdcGetInfoById(channelId, &channel) == NULL) {
410         TRANS_LOGE(TRANS_SDK, "get tdc info failed. channelId=%{public}d", channelId);
411         return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
412     }
413     if (memcpy_s(key, len, channel.detail.sessionKey, SESSION_KEY_LENGTH) != EOK) {
414         TRANS_LOGE(TRANS_SDK, "copy session key failed.");
415         return SOFTBUS_MEM_ERR;
416     }
417     return SOFTBUS_OK;
418 }
419 
TransTdcGetHandle(int32_t channelId,int * handle)420 int32_t TransTdcGetHandle(int32_t channelId, int *handle)
421 {
422     if (handle == NULL) {
423         TRANS_LOGW(TRANS_SDK, "invalid param.");
424         return SOFTBUS_INVALID_PARAM;
425     }
426     TcpDirectChannelInfo channel;
427     if (TransTdcGetInfoById(channelId, &channel) == NULL) {
428         TRANS_LOGE(TRANS_SDK, "get tdc info failed. channelId=%{public}d", channelId);
429         return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
430     }
431     *handle = channel.detail.fd;
432     return SOFTBUS_OK;
433 }
434 
TransDisableSessionListener(int32_t channelId)435 int32_t TransDisableSessionListener(int32_t channelId)
436 {
437     TcpDirectChannelInfo channel;
438     if (TransTdcGetInfoById(channelId, &channel) == NULL) {
439         TRANS_LOGE(TRANS_SDK, "get tdc info failed. channelId=%{public}d", channelId);
440         return SOFTBUS_TRANS_TDC_CHANNEL_NOT_FOUND;
441     }
442     if (channel.detail.fd < 0) {
443         TRANS_LOGE(TRANS_SDK, "invalid handle.");
444         return SOFTBUS_INVALID_FD;
445     }
446 
447     (void)TransTdcSetListenerStateById(channelId, true);
448     int32_t ret = TransTdcStopRead(channel.detail.fd);
449     if (ret != SOFTBUS_OK) {
450         TRANS_LOGW(TRANS_SDK, "stop read failed. channelId=%{public}d, ret=%{public}d", channelId, ret);
451     }
452     return SOFTBUS_OK;
453 }
454 
TransUpdateFdState(int32_t channelId)455 void TransUpdateFdState(int32_t channelId)
456 {
457     if (g_tcpDirectChannelInfoList == NULL) {
458         TRANS_LOGE(TRANS_SDK, "g_tcpDirectChannelInfoList is NULL, channelId=%{public}d", channelId);
459         return;
460     }
461     if (SoftBusMutexLock(&g_tcpDirectChannelInfoList->lock) != SOFTBUS_OK) {
462         TRANS_LOGE(TRANS_SDK, "lock failed, channelId=%{public}d", channelId);
463         return;
464     }
465 
466     TcpDirectChannelInfo *item = NULL;
467     LIST_FOR_EACH_ENTRY(item, &(g_tcpDirectChannelInfoList->list), TcpDirectChannelInfo, node) {
468         if (item->channelId == channelId) {
469             item->detail.fdRefCnt--;
470             if (item->detail.needRelease && item->detail.fdRefCnt <= 0) {
471                 SoftBusMutexDestroy(&(item->detail.fdLock));
472                 ListDelete(&item->node);
473                 SoftBusFree(item);
474                 item = NULL;
475                 TRANS_LOGI(TRANS_SDK, "Delete tdc item success. channelId=%{public}d", channelId);
476             }
477             (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
478             return;
479         }
480     }
481 
482     (void)SoftBusMutexUnlock(&g_tcpDirectChannelInfoList->lock);
483     TRANS_LOGE(TRANS_SDK, "channel not found, channelId=%{public}d", channelId);
484     return;
485 }
486