• 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 "trans_auth_manager.h"
17 
18 #include "auth_channel.h"
19 #include "auth_manager.h"
20 #include "auth_meta_manager.h"
21 #include "auth_tcp_connection.h"
22 #include "bus_center_manager.h"
23 #include "common_list.h"
24 #include "lnn_connection_addr_utils.h"
25 #include "lnn_net_builder.h"
26 #include "securec.h"
27 #include "softbus_adapter_mem.h"
28 #include "softbus_adapter_thread.h"
29 #include "softbus_def.h"
30 #include "softbus_error_code.h"
31 #include "softbus_feature_config.h"
32 #include "legacy/softbus_hisysevt_transreporter.h"
33 #include "softbus_utils.h"
34 #include "trans_auth_message.h"
35 #include "trans_channel_common.h"
36 #include "trans_channel_limit.h"
37 #include "trans_event.h"
38 #include "trans_session_manager.h"
39 #include "trans_channel_manager.h"
40 #include "trans_log.h"
41 #include "trans_lane_manager.h"
42 #include "wifi_direct_manager.h"
43 
44 #define AUTH_CHANNEL_REQ 0
45 #define AUTH_CHANNEL_REPLY 1
46 
47 #define IPV4_TYPE 1
48 #define IPV6_TYPE 2
49 
50 #define AUTH_GROUP_ID "auth group id"
51 #define AUTH_SESSION_KEY "auth session key"
52 #define ISHARE_AUTH_SESSION "IShareAuthSession"
53 #define DM_PKG_NAME "ohos.distributedhardware.devicemanager.resident"
54 
55 const char *g_serviceForAction[] = {
56     "IShareAuthSession",
57 };
58 
59 #define SERVICE_FOR_ACTION_LEN (sizeof(g_serviceForAction) / sizeof(g_serviceForAction[0]))
60 
61 typedef struct {
62     int32_t channelType;
63     int32_t businessType;
64     ConfigType configType;
65 } ConfigTypeMap;
66 
67 static SoftBusList *g_authChannelList = NULL;
68 static IServerChannelCallBack *g_cb = NULL;
69 
70 static void TransPostAuthChannelErrMsg(int32_t authId, int32_t errcode, const char *errMsg);
71 static int32_t TransPostAuthChannelMsg(const AppInfo *appInfo, int32_t authId, int32_t flag);
72 static AuthChannelInfo *CreateAuthChannelInfo(const char *sessionName, bool isClient);
73 static int32_t AddAuthChannelInfo(AuthChannelInfo *info);
74 static void DelAuthChannelInfoByChanId(int32_t channelId);
75 static void DelAuthChannelInfoByAuthId(int32_t authId);
76 
GetAuthChannelListHead(void)77 SoftBusList *GetAuthChannelListHead(void)
78 {
79     return g_authChannelList;
80 }
81 
GetAuthChannelLock(void)82 int32_t GetAuthChannelLock(void)
83 {
84     if (g_authChannelList == NULL) {
85         TRANS_LOGE(TRANS_SVC, "g_authChannelList not init");
86         return SOFTBUS_NO_INIT;
87     }
88     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
89         TRANS_LOGE(TRANS_SVC, "lock failed");
90         return SOFTBUS_LOCK_ERR;
91     }
92     return SOFTBUS_OK;
93 }
94 
ReleaseAuthChannelLock(void)95 void ReleaseAuthChannelLock(void)
96 {
97     if (g_authChannelList == NULL) {
98         TRANS_LOGE(TRANS_SVC, "g_authChannelList not init");
99         return;
100     }
101     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
102 }
103 
GetAuthChannelInfoByChanId(int32_t channelId,AuthChannelInfo * dstInfo)104 static int32_t GetAuthChannelInfoByChanId(int32_t channelId, AuthChannelInfo *dstInfo)
105 {
106     if (g_authChannelList == NULL) {
107         TRANS_LOGE(TRANS_SVC, "invalid param");
108         return SOFTBUS_INVALID_PARAM;
109     }
110 
111     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
112         TRANS_LOGE(TRANS_SVC, "lock failed");
113         return SOFTBUS_LOCK_ERR;
114     }
115     AuthChannelInfo *info = NULL;
116     LIST_FOR_EACH_ENTRY(info, &g_authChannelList->list, AuthChannelInfo, node) {
117         if (info->appInfo.myData.channelId == channelId) {
118             if (memcpy_s(dstInfo, sizeof(AuthChannelInfo), info, sizeof(AuthChannelInfo)) != EOK) {
119                 TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
120                 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
121                 return SOFTBUS_MEM_ERR;
122             }
123             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
124             return SOFTBUS_OK;
125         }
126     }
127     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
128     return SOFTBUS_TRANS_NODE_NOT_FOUND;
129 }
130 
GetAuthIdByChannelId(int32_t channelId)131 static int32_t GetAuthIdByChannelId(int32_t channelId)
132 {
133     if (g_authChannelList == NULL) {
134         return SOFTBUS_NO_INIT;
135     }
136 
137     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
138         TRANS_LOGE(TRANS_SVC, "lock failed");
139         return SOFTBUS_LOCK_ERR;
140     }
141     int32_t authId = AUTH_INVALID_ID;
142     AuthChannelInfo *info = NULL;
143     LIST_FOR_EACH_ENTRY(info, &g_authChannelList->list, AuthChannelInfo, node) {
144         if (info->appInfo.myData.channelId == channelId) {
145             authId = info->authId;
146             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
147             return authId;
148         }
149     }
150     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
151     return authId;
152 }
153 
GetChannelInfoByAuthId(int32_t authId,AuthChannelInfo * dstInfo)154 static int32_t GetChannelInfoByAuthId(int32_t authId, AuthChannelInfo *dstInfo)
155 {
156     if (dstInfo == NULL || g_authChannelList == NULL) {
157         return SOFTBUS_INVALID_PARAM;
158     }
159 
160     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
161         TRANS_LOGE(TRANS_SVC, "lock failed");
162         return SOFTBUS_LOCK_ERR;
163     }
164     AuthChannelInfo *info = NULL;
165     LIST_FOR_EACH_ENTRY(info, &g_authChannelList->list, AuthChannelInfo, node) {
166         if (info->authId == authId) {
167             if (memcpy_s(dstInfo, sizeof(AuthChannelInfo), info, sizeof(AuthChannelInfo)) != EOK) {
168                 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
169                 return SOFTBUS_MEM_ERR;
170             }
171             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
172             return SOFTBUS_OK;
173         }
174     }
175     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
176     return SOFTBUS_TRANS_NODE_NOT_FOUND;
177 }
178 
NotifyOpenAuthChannelSuccess(const AppInfo * appInfo,bool isServer)179 static int32_t NotifyOpenAuthChannelSuccess(const AppInfo *appInfo, bool isServer)
180 {
181     ChannelInfo channelInfo = {0};
182     channelInfo.channelType = CHANNEL_TYPE_AUTH;
183     channelInfo.isServer = isServer;
184     channelInfo.isEnabled = true;
185     channelInfo.channelId = appInfo->myData.channelId;
186     channelInfo.peerDeviceId = strlen(appInfo->peerNetWorkId) == 0 ?
187         (char *)appInfo->peerData.deviceId : (char *)appInfo->peerNetWorkId;
188     channelInfo.peerSessionName = (char *)appInfo->peerData.sessionName;
189     channelInfo.businessType = BUSINESS_TYPE_NOT_CARE;
190     channelInfo.groupId = (char *)AUTH_GROUP_ID;
191     channelInfo.isEncrypt = false;
192     channelInfo.sessionKey = (char *)AUTH_SESSION_KEY;
193     channelInfo.keyLen = strlen(channelInfo.sessionKey) + 1;
194     channelInfo.autoCloseTime = appInfo->autoCloseTime;
195     channelInfo.reqId = (char *)appInfo->reqId;
196     channelInfo.dataConfig = appInfo->myData.dataConfig;
197     channelInfo.timeStart = appInfo->timeStart;
198     channelInfo.connectType = appInfo->connectType;
199     channelInfo.routeType = appInfo->routeType;
200     channelInfo.osType = appInfo->osType;
201     return g_cb->OnChannelOpened(appInfo->myData.pkgName, appInfo->myData.pid,
202         appInfo->myData.sessionName, &channelInfo);
203 }
204 
NotifyOpenAuthChannelFailed(const char * pkgName,int32_t pid,int32_t channelId,int32_t errCode)205 int32_t NotifyOpenAuthChannelFailed(const char *pkgName, int32_t pid, int32_t channelId, int32_t errCode)
206 {
207     return g_cb->OnChannelOpenFailed(pkgName, pid, channelId, CHANNEL_TYPE_AUTH, errCode);
208 }
209 
NotifyCloseAuthChannel(const char * pkgName,int32_t pid,int32_t channelId)210 static int32_t NotifyCloseAuthChannel(const char *pkgName, int32_t pid, int32_t channelId)
211 {
212     return g_cb->OnChannelClosed(pkgName, pid, channelId, CHANNEL_TYPE_AUTH, MESSAGE_TYPE_NOMAL);
213 }
214 
AuthGetUidAndPidBySessionName(const char * sessionName,int32_t * uid,int32_t * pid)215 static int32_t AuthGetUidAndPidBySessionName(const char *sessionName, int32_t *uid, int32_t *pid)
216 {
217     return g_cb->GetUidAndPidBySessionName(sessionName, uid, pid);
218 }
219 
NotifyOnDataReceived(int32_t authId,const void * data,uint32_t len)220 static int32_t NotifyOnDataReceived(int32_t authId, const void *data, uint32_t len)
221 {
222     AuthChannelInfo channel;
223     int32_t ret = GetChannelInfoByAuthId(authId, &channel);
224     if (ret != SOFTBUS_OK) {
225         TRANS_LOGE(TRANS_SVC, "GetChannelInfoByAuthId failed");
226         return ret;
227     }
228     TransReceiveData receiveData;
229     receiveData.data = (void *)data;
230     receiveData.dataLen = len;
231     receiveData.dataType = TRANS_SESSION_BYTES;
232 
233     return g_cb->OnDataReceived(channel.appInfo.myData.pkgName, channel.appInfo.myData.pid,
234         channel.appInfo.myData.channelId, CHANNEL_TYPE_AUTH, &receiveData);
235 }
236 
CopyPeerAppInfo(AppInfo * recvAppInfo,AppInfo * channelAppInfo)237 static int32_t CopyPeerAppInfo(AppInfo *recvAppInfo, AppInfo *channelAppInfo)
238 {
239     if (memcpy_s(channelAppInfo->peerData.deviceId, DEVICE_ID_SIZE_MAX,
240                  recvAppInfo->peerData.deviceId, DEVICE_ID_SIZE_MAX) != EOK ||
241         memcpy_s(recvAppInfo->myData.deviceId, DEVICE_ID_SIZE_MAX,
242                  channelAppInfo->myData.deviceId, DEVICE_ID_SIZE_MAX) != EOK ||
243         memcpy_s(channelAppInfo->peerData.pkgName, PKG_NAME_SIZE_MAX,
244                  recvAppInfo->peerData.pkgName, PKG_NAME_SIZE_MAX) != EOK ||
245         memcpy_s(recvAppInfo->myData.pkgName, PKG_NAME_SIZE_MAX,
246                  channelAppInfo->myData.pkgName, PKG_NAME_SIZE_MAX) != EOK ||
247         memcpy_s(channelAppInfo->peerData.sessionName, SESSION_NAME_SIZE_MAX,
248                  recvAppInfo->peerData.sessionName, SESSION_NAME_SIZE_MAX) != EOK) {
249         return SOFTBUS_MEM_ERR;
250     }
251     return SOFTBUS_OK;
252 }
253 
InitAuthChannelInfo(int32_t authId,AuthChannelInfo * item,AppInfo * appInfo)254 static int32_t InitAuthChannelInfo(int32_t authId, AuthChannelInfo *item, AppInfo *appInfo)
255 {
256     item->authId = authId;
257     appInfo->myData.channelId = item->appInfo.myData.channelId;
258     appInfo->myData.dataConfig = item->appInfo.myData.dataConfig;
259     item->connOpt.socketOption.moduleId = AUTH_RAW_P2P_SERVER;
260     if (appInfo->linkType == LANE_HML_RAW) {
261         item->appInfo.linkType = appInfo->linkType;
262         if (memcpy_s(item->appInfo.peerData.addr, IP_LEN, appInfo->peerData.addr, IP_LEN) != EOK ||
263             memcpy_s(item->appInfo.myData.addr, IP_LEN, appInfo->myData.addr, IP_LEN) != EOK) {
264             TRANS_LOGE(TRANS_SVC, "copy clientIp and serverIp fail, authId=%{public}d", authId);
265             return SOFTBUS_MEM_ERR;
266         }
267     }
268     return SOFTBUS_OK;
269 }
270 
OnRequsetUpdateAuthChannel(int32_t authId,AppInfo * appInfo)271 static int32_t OnRequsetUpdateAuthChannel(int32_t authId, AppInfo *appInfo)
272 {
273     AuthChannelInfo *item = NULL;
274     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
275         TRANS_LOGE(TRANS_SVC, "lock failed, authId=%{public}d", authId);
276         return SOFTBUS_LOCK_ERR;
277     }
278     bool exists = false;
279     LIST_FOR_EACH_ENTRY(item, &g_authChannelList->list, AuthChannelInfo, node) {
280         if (item->authId == authId) {
281             exists = true;
282             break;
283         }
284     }
285     int32_t ret = SOFTBUS_OK;
286     if (!exists) {
287         item = CreateAuthChannelInfo(appInfo->myData.sessionName, false);
288         if (item == NULL) {
289             TRANS_LOGE(TRANS_SVC, "CreateAuthChannelInfo failed, authId=%{public}d", authId);
290             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
291             return SOFTBUS_TRANS_AUTH_CREATE_CHANINFO_FAIL;
292         }
293         ret = InitAuthChannelInfo(authId, item, appInfo);
294         if (ret != SOFTBUS_OK) {
295             TRANS_LOGE(TRANS_SVC, "init auth channel info failed, ret=%{public}d, authId=%{public}d", ret, authId);
296             SoftBusFree(item);
297             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
298             return ret;
299         }
300         ret = AddAuthChannelInfo(item);
301         if (ret != SOFTBUS_OK) {
302             TRANS_LOGE(TRANS_SVC, "AddAuthChannelInfo failed");
303             SoftBusFree(item);
304             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
305             return ret;
306         }
307     }
308     ret = CopyPeerAppInfo(appInfo, &(item->appInfo));
309     if (ret != SOFTBUS_OK) {
310         ListDelete(&item->node);
311         TRANS_LOGE(TRANS_CTRL, "copy peer appInfo failed ret=%{public}d, delete channelId=%{public}" PRId64,
312             ret, item->appInfo.myData.channelId);
313         SoftBusFree(item);
314         g_authChannelList->cnt--;
315         (void)SoftBusMutexUnlock(&g_authChannelList->lock);
316         return SOFTBUS_MEM_ERR;
317     }
318     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
319     return SOFTBUS_OK;
320 }
321 
322 static const ConfigTypeMap g_configTypeMap[] = {
323     {CHANNEL_TYPE_AUTH, BUSINESS_TYPE_BYTE, SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH},
324     {CHANNEL_TYPE_AUTH, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH},
325 };
326 
FindConfigType(int32_t channelType,int32_t businessType)327 static int32_t FindConfigType(int32_t channelType, int32_t businessType)
328 {
329     uint32_t size = (uint32_t)(sizeof(g_configTypeMap) / sizeof(g_configTypeMap[0]));
330     for (uint32_t i = 0; i < size; i++) {
331         if ((g_configTypeMap[i].channelType == channelType) && (g_configTypeMap[i].businessType == businessType)) {
332             return g_configTypeMap[i].configType;
333         }
334     }
335     return SOFTBUS_CONFIG_TYPE_MAX;
336 }
337 
TransGetLocalConfig(int32_t channelType,int32_t businessType,uint32_t * len)338 static int32_t TransGetLocalConfig(int32_t channelType, int32_t businessType, uint32_t *len)
339 {
340     ConfigType configType = (ConfigType)FindConfigType(channelType, businessType);
341     if (configType == SOFTBUS_CONFIG_TYPE_MAX) {
342         TRANS_LOGE(TRANS_SVC, "Invalid channelType=%{public}d, businessType=%{public}d", channelType, businessType);
343         return SOFTBUS_INVALID_PARAM;
344     }
345     uint32_t maxLen;
346     if (SoftbusGetConfig(configType, (unsigned char *)&maxLen, sizeof(maxLen)) != SOFTBUS_OK) {
347         TRANS_LOGE(TRANS_SVC, "get fail configType=%{public}d", configType);
348         return SOFTBUS_GET_CONFIG_VAL_ERR;
349     }
350     *len = maxLen;
351     TRANS_LOGI(TRANS_SVC, "get appinfo local config len=%{public}d", *len);
352     return SOFTBUS_OK;
353 }
354 
TransAuthFillDataConfig(AppInfo * appInfo)355 static int32_t TransAuthFillDataConfig(AppInfo *appInfo)
356 {
357     if (appInfo == NULL) {
358         TRANS_LOGE(TRANS_SVC, "appInfo is null");
359         return SOFTBUS_INVALID_PARAM;
360     }
361     appInfo->businessType = BUSINESS_TYPE_BYTE;
362     if (appInfo->peerData.dataConfig != 0) {
363         uint32_t localDataConfig = 0;
364         int32_t ret = TransGetLocalConfig(CHANNEL_TYPE_AUTH, appInfo->businessType, &localDataConfig);
365         if (ret != SOFTBUS_OK) {
366             TRANS_LOGE(TRANS_SVC, "get local config failed");
367             return ret;
368         }
369         appInfo->myData.dataConfig = MIN(localDataConfig, appInfo->peerData.dataConfig);
370         TRANS_LOGI(TRANS_SVC, "fill dataConfig succ. dataConfig=%{public}u", appInfo->myData.dataConfig);
371         return SOFTBUS_OK;
372     }
373     ConfigType configType = appInfo->businessType == BUSINESS_TYPE_BYTE ?
374         SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH : SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH;
375     if (SoftbusGetConfig(configType, (unsigned char *)&appInfo->myData.dataConfig,
376         sizeof(appInfo->myData.dataConfig)) != SOFTBUS_OK) {
377         TRANS_LOGE(TRANS_SVC, "get config failed, configType=%{public}d", configType);
378         return SOFTBUS_GET_CONFIG_VAL_ERR;
379     }
380     TRANS_LOGI(TRANS_SVC, "fill dataConfig=%{public}d", appInfo->myData.dataConfig);
381     return SOFTBUS_OK;
382 }
383 
TransAuthCloseChannel(int32_t authId,int32_t linkType,bool isClient)384 static void TransAuthCloseChannel(int32_t authId, int32_t linkType, bool isClient)
385 {
386     TRANS_LOGI(TRANS_SVC, "authId=%{public}d, linkType=%{public}d, isClient=%{public}d", authId, linkType, isClient);
387     if (linkType == LANE_HML_RAW && isClient) {
388         AuthCloseChannel(authId, AUTH_RAW_P2P_CLIENT);
389     } else if (linkType == LANE_HML_RAW && !isClient) {
390         AuthCloseChannel(authId, AUTH_RAW_P2P_SERVER);
391     } else {
392         AuthCloseChannel(authId, AUTH);
393     }
394 }
395 
TransHandleErrorAndCloseChannel(TransEventExtra * extra,int32_t authId,int32_t linkType,bool isClient,int32_t ret)396 static void TransHandleErrorAndCloseChannel(TransEventExtra *extra, int32_t authId, int32_t linkType, bool isClient,
397     int32_t ret)
398 {
399     if (extra != NULL && extra->socketName != NULL) {
400         extra->result = EVENT_STAGE_RESULT_FAILED;
401         extra->errcode = ret;
402         TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_HANDSHAKE_REPLY, *extra);
403     }
404 
405     DelAuthChannelInfoByAuthId(authId);
406     TransAuthCloseChannel(authId, linkType, isClient);
407 }
408 
TransHandleAuthChannelSetupProcess(TransEventExtra * extra,int32_t authId,AppInfo * appInfo)409 static void TransHandleAuthChannelSetupProcess(TransEventExtra *extra, int32_t authId, AppInfo *appInfo)
410 {
411     int32_t ret = AuthGetUidAndPidBySessionName(
412         appInfo->myData.sessionName, &appInfo->myData.uid, &appInfo->myData.pid);
413     if (ret != SOFTBUS_OK) {
414         TRANS_LOGE(TRANS_SVC, "auth get id by sessionName failed and send msg to peer");
415         TransPostAuthChannelErrMsg(authId, ret, "session not created");
416         TransHandleErrorAndCloseChannel(extra, authId, appInfo->linkType, appInfo->isClient, ret);
417         return;
418     }
419     ret = TransAuthFillDataConfig(appInfo);
420     if (ret != SOFTBUS_OK) {
421         TRANS_LOGE(TRANS_SVC, "TransAuthFillDataConfig failed");
422         TransHandleErrorAndCloseChannel(extra, authId, appInfo->linkType, appInfo->isClient, ret);
423         return;
424     }
425     ret = OnRequsetUpdateAuthChannel(authId, appInfo);
426     if (ret != SOFTBUS_OK) {
427         TRANS_LOGE(TRANS_SVC, "update auth channel failed");
428         TransPostAuthChannelErrMsg(authId, ret, "unpackRequest");
429         TransHandleErrorAndCloseChannel(extra, authId, appInfo->linkType, appInfo->isClient, ret);
430         return;
431     }
432     extra->result = EVENT_STAGE_RESULT_OK;
433     extra->channelId = appInfo->myData.channelId;
434     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_HANDSHAKE_START, *extra);
435     ret = NotifyOpenAuthChannelSuccess(appInfo, true);
436     if (ret != SOFTBUS_OK) {
437         TRANS_LOGE(TRANS_SVC, "Notify Open Auth Channel send request failed, ret=%{public}d", ret);
438         TransPostAuthChannelErrMsg(authId, ret, "NotifyOpenAuthChannelSuccess failed");
439         TransHandleErrorAndCloseChannel(extra, authId, appInfo->linkType, appInfo->isClient, ret);
440         return;
441     }
442     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_HANDSHAKE_REPLY, *extra);
443 }
444 
OnRecvAuthChannelRequest(int32_t authId,const char * data,int32_t len)445 static void OnRecvAuthChannelRequest(int32_t authId, const char *data, int32_t len)
446 {
447     if (data == NULL || len <= 0) {
448         return;
449     }
450 
451     TransEventExtra extra = {
452         .peerNetworkId = NULL,
453         .calleePkg = NULL,
454         .callerPkg = NULL,
455         .socketName = NULL,
456         .channelType = CHANNEL_TYPE_AUTH,
457         .authId = authId
458     };
459     char localUdid[UDID_BUF_LEN] = { 0 };
460     if (LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, localUdid, UDID_BUF_LEN) == SOFTBUS_OK) {
461         extra.localUdid = localUdid;
462     }
463     AppInfo appInfo;
464     (void)memset_s(&appInfo, sizeof(AppInfo), 0, sizeof(AppInfo));
465     int32_t ret = TransAuthChannelMsgUnpack(data, &appInfo, len);
466     if (ret != SOFTBUS_OK) {
467         TRANS_LOGE(TRANS_SVC, "unpackRequest failed");
468         TransPostAuthChannelErrMsg(authId, ret, "unpackRequest");
469         TransHandleErrorAndCloseChannel(&extra, authId, appInfo.linkType, appInfo.isClient, ret);
470         return;
471     }
472     extra.socketName = appInfo.myData.sessionName;
473     extra.peerUdid = appInfo.peerData.deviceId;
474     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL_SERVER, EVENT_STAGE_HANDSHAKE_START, extra);
475     if (!CheckSessionNameValidOnAuthChannel(appInfo.myData.sessionName)) {
476         TRANS_LOGE(TRANS_SVC, "check auth channel pkginfo invalid.");
477         TransPostAuthChannelErrMsg(authId, SOFTBUS_TRANS_AUTH_NOTALLOW_OPENED, "check msginfo failed");
478         TransHandleErrorAndCloseChannel(&extra, authId, appInfo.linkType, appInfo.isClient,
479             SOFTBUS_TRANS_INVALID_SESSION_NAME);
480         return;
481     }
482 
483     TransHandleAuthChannelSetupProcess(&extra, authId, &appInfo);
484 }
485 
TransAuthProcessDataConfig(AppInfo * appInfo)486 static int32_t TransAuthProcessDataConfig(AppInfo *appInfo)
487 {
488     if (appInfo == NULL) {
489         TRANS_LOGE(TRANS_SVC, "appInfo is null");
490         return SOFTBUS_INVALID_PARAM;
491     }
492     if (appInfo->businessType != BUSINESS_TYPE_MESSAGE && appInfo->businessType != BUSINESS_TYPE_BYTE) {
493         TRANS_LOGI(TRANS_SVC, "invalid businessType=%{public}d", appInfo->businessType);
494         return SOFTBUS_OK;
495     }
496     if (appInfo->peerData.dataConfig != 0) {
497         appInfo->myData.dataConfig = MIN(appInfo->myData.dataConfig, appInfo->peerData.dataConfig);
498         TRANS_LOGI(TRANS_SVC, "process dataConfig succ. dataConfig=%{public}u", appInfo->myData.dataConfig);
499         return SOFTBUS_OK;
500     }
501     ConfigType configType = appInfo->businessType == BUSINESS_TYPE_BYTE ?
502         SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH : SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH;
503     if (SoftbusGetConfig(configType, (unsigned char *)&appInfo->myData.dataConfig,
504         sizeof(appInfo->myData.dataConfig)) != SOFTBUS_OK) {
505         TRANS_LOGE(TRANS_SVC, "get config failed, configType=%{public}d", configType);
506         return SOFTBUS_GET_CONFIG_VAL_ERR;
507     }
508     TRANS_LOGI(TRANS_SVC, "process dataConfig=%{public}d", appInfo->myData.dataConfig);
509     return SOFTBUS_OK;
510 }
511 
FillExtraByAuthChannelErrorEnd(TransEventExtra * extra,AuthChannelInfo * info,int32_t ret)512 static void FillExtraByAuthChannelErrorEnd(TransEventExtra *extra, AuthChannelInfo *info, int32_t ret)
513 {
514     if (extra == NULL || info == NULL) {
515         TRANS_LOGE(TRANS_SVC, "invalid param.");
516         return;
517     }
518     extra->result = EVENT_STAGE_RESULT_FAILED;
519     extra->errcode = ret;
520     extra->localUdid = info->appInfo.myData.deviceId;
521     if (strlen(info->appInfo.peerVersion) == 0) {
522         TransGetRemoteDeviceVersion(extra->peerUdid, CATEGORY_UDID, info->appInfo.peerVersion,
523             sizeof(info->appInfo.peerVersion));
524     }
525     extra->peerDevVer = info->appInfo.peerVersion;
526 }
527 
InitExtra(TransEventExtra * extra,const AuthChannelInfo * info,int32_t authId)528 static void InitExtra(TransEventExtra *extra, const AuthChannelInfo *info, int32_t authId)
529 {
530     extra->peerNetworkId = NULL;
531     extra->calleePkg = NULL;
532     extra->callerPkg = NULL;
533     extra->socketName = info->appInfo.myData.sessionName;
534     extra->channelId = info->appInfo.myData.channelId;
535     extra->channelType = CHANNEL_TYPE_AUTH;
536     extra->authId = authId;
537     extra->linkType = info->connOpt.type;
538     extra->osType = (info->appInfo.osType < 0) ? UNKNOW_OS_TYPE : info->appInfo.osType;
539 }
540 
ChannelReplyErrProc(TransEventExtra * extra,int32_t errorCode,AuthChannelInfo * info,int32_t authId)541 static void ChannelReplyErrProc(TransEventExtra *extra, int32_t errorCode, AuthChannelInfo *info, int32_t authId)
542 {
543     FillExtraByAuthChannelErrorEnd(extra, info, errorCode);
544     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_OPEN_CHANNEL_END, *extra);
545     TransAuthCloseChannel(authId, info->appInfo.linkType, info->isClient);
546     (void)TransLaneMgrDelLane(info->appInfo.myData.channelId, CHANNEL_TYPE_AUTH, true);
547     DelAuthChannelInfoByChanId((int32_t)(info->appInfo.myData.channelId));
548     (void)NotifyOpenAuthChannelFailed((const char *)(info->appInfo.myData.pkgName),
549         (int32_t)(info->appInfo.myData.pid), (int32_t)(info->appInfo.myData.channelId), errorCode);
550 }
551 
TransAuthGetPeerUdidByChanId(int32_t channelId,char * peerUdid,uint32_t len)552 int32_t TransAuthGetPeerUdidByChanId(int32_t channelId, char *peerUdid, uint32_t len)
553 {
554     if (len < DEVICE_ID_SIZE_MAX || peerUdid == NULL) {
555         TRANS_LOGE(TRANS_SVC, "param err");
556         return SOFTBUS_INVALID_PARAM;
557     }
558     AuthChannelInfo info;
559     int32_t ret = GetAuthChannelInfoByChanId(channelId, &info);
560     if (ret != SOFTBUS_OK) {
561         TRANS_LOGE(TRANS_SVC, "get channel info by chanId failed. chanId=%{public}d", channelId);
562         return ret;
563     }
564     if (strlen(info.appInfo.peerUdid) != 0) {
565         if (memcpy_s(peerUdid, len, info.appInfo.peerUdid, DEVICE_ID_SIZE_MAX) != EOK) {
566             return SOFTBUS_MEM_ERR;
567         }
568     } else {
569         if (memcpy_s(peerUdid, len, info.appInfo.peerData.deviceId, DEVICE_ID_SIZE_MAX) != EOK) {
570             return SOFTBUS_MEM_ERR;
571         }
572     }
573     return SOFTBUS_OK;
574 }
575 
LnnSvrJoinCallback(const ConnectionAddr * connAddr,int32_t errCode)576 static void LnnSvrJoinCallback(const ConnectionAddr *connAddr, int32_t errCode)
577 {
578     if (connAddr == NULL) {
579         TRANS_LOGE(TRANS_SVC, "invalid param");
580         return;
581     }
582     TRANS_LOGI(TRANS_SVC, "LnnSvrJoinCallback enter");
583     AuthChannelInfo info;
584     int32_t ret = GetAuthChannelInfoByChanId(connAddr->info.session.channelId, &info);
585     if (ret != SOFTBUS_OK) {
586         TRANS_LOGE(TRANS_SVC, "get channel info by chanId failed. chanId=%{public}d", connAddr->info.session.channelId);
587         return;
588     }
589 
590     TransEventExtra extra;
591     (void)memset_s(&extra, sizeof(TransEventExtra), 0, sizeof(TransEventExtra));
592     InitExtra(&extra, &info, info.authId);
593     extra.peerUdid = strlen(info.appInfo.peerUdid) != 0 ? info.appInfo.peerUdid : info.appInfo.peerData.deviceId;
594     if (errCode != SOFTBUS_OK) {
595         TRANS_LOGE(TRANS_SVC, "LnnServerJoinExt failed, chanId=%{public}d, errCode=%{public}d",
596             connAddr->info.session.channelId, errCode);
597     }
598     extra.result = EVENT_STAGE_RESULT_OK;
599     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_REPLY, extra);
600     ret = NotifyOpenAuthChannelSuccess(&info.appInfo, false);
601     if (ret != SOFTBUS_OK) {
602         TRANS_LOGE(TRANS_SVC, "NotifyOpenAuthChannelSuccess failed");
603         ChannelReplyErrProc(&extra, ret, &info, info.authId);
604     }
605 }
606 
UpdateChannelInfo(int32_t authId,const AuthChannelInfo * info)607 static int32_t UpdateChannelInfo(int32_t authId, const AuthChannelInfo *info)
608 {
609     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
610         TRANS_LOGE(TRANS_SVC, "lock failed");
611         return SOFTBUS_LOCK_ERR;
612     }
613     AuthChannelInfo *item = NULL;
614     LIST_FOR_EACH_ENTRY(item, &g_authChannelList->list, AuthChannelInfo, node) {
615         if (item->authId == authId) {
616             if (memcpy_s(&(item->appInfo), sizeof(item->appInfo), &(info->appInfo), sizeof(info->appInfo)) != EOK) {
617                 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
618                 return SOFTBUS_MEM_ERR;
619             }
620             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
621             return SOFTBUS_OK;
622         }
623     }
624     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
625     return SOFTBUS_TRANS_NODE_NOT_FOUND;
626 }
627 
AdaptLnnServerJoinExt(int64_t channelId)628 static int32_t AdaptLnnServerJoinExt(int64_t channelId)
629 {
630     ConnectionAddr connAddr;
631     (void)memset_s(&connAddr, sizeof(ConnectionAddr), 0, sizeof(ConnectionAddr));
632     connAddr.type = CONNECTION_ADDR_SESSION;
633     connAddr.info.session.channelId = channelId;
634     LnnServerJoinExtCallBack svrJoinCallBack = {
635         .lnnServerJoinExtCallback = LnnSvrJoinCallback,
636     };
637     return LnnServerJoinExt(&connAddr, &svrJoinCallBack);
638 }
639 
OnRecvAuthChannelReply(int32_t authId,const char * data,int32_t len)640 static void OnRecvAuthChannelReply(int32_t authId, const char *data, int32_t len)
641 {
642     if (data == NULL || len <= 0) {
643         return;
644     }
645     AuthChannelInfo info;
646     if (GetChannelInfoByAuthId(authId, &info) != SOFTBUS_OK) {
647         TRANS_LOGE(TRANS_SVC, "can not find channel info by auth id");
648         return;
649     }
650     TransEventExtra extra;
651     (void)memset_s(&extra, sizeof(TransEventExtra), 0, sizeof(TransEventExtra));
652     InitExtra(&extra, &info, authId);
653     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_REPLY, extra);
654     int32_t ret = TransAuthChannelMsgUnpack(data, &info.appInfo, len);
655     if (ret != SOFTBUS_OK) {
656         TRANS_LOGE(TRANS_SVC, "unpackReply failed");
657         ChannelReplyErrProc(&extra, ret, &info, authId);
658         return;
659     }
660     extra.peerUdid = strlen(info.appInfo.peerUdid) != 0 ? info.appInfo.peerUdid : info.appInfo.peerData.deviceId;
661     ret = TransAuthProcessDataConfig(&info.appInfo);
662     if (ret != SOFTBUS_OK) {
663         TRANS_LOGE(TRANS_SVC, "ProcessDataConfig failed");
664         ChannelReplyErrProc(&extra, ret, &info, authId);
665         return;
666     }
667     extra.result = EVENT_STAGE_RESULT_OK;
668     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_REPLY, extra);
669 
670     if (info.accountInfo) {
671         TRANS_LOGI(TRANS_SVC, "accountInfo=%{public}d, authId=%{public}d, channelId=%{public}" PRId64,
672             info.accountInfo, info.authId, info.appInfo.myData.channelId);
673         if (UpdateChannelInfo(authId, &info) != SOFTBUS_OK) {
674             TRANS_LOGE(TRANS_SVC, "update channelInfo failed, authId=%{public}d", info.authId);
675             return;
676         }
677         int64_t channelId = info.appInfo.myData.channelId;
678         ret = AdaptLnnServerJoinExt(channelId);
679         if (ret == SOFTBUS_OK) {
680             return;
681         }
682         TRANS_LOGI(TRANS_SVC, "adapt LnnServerJoinExt fail, channelId=%{public}" PRId64, channelId);
683     }
684 
685     ret = NotifyOpenAuthChannelSuccess(&info.appInfo, false);
686     if (ret != SOFTBUS_OK) {
687         TRANS_LOGE(TRANS_SVC, "NotifyOpenAuthChannelSuccess failed");
688         ChannelReplyErrProc(&extra, ret, &info, authId);
689     }
690 }
691 
OnAuthChannelDataRecv(int32_t authId,const AuthChannelData * data)692 static void OnAuthChannelDataRecv(int32_t authId, const AuthChannelData *data)
693 {
694     if (data == NULL || data->data == NULL || data->len < 1) {
695         TRANS_LOGW(TRANS_SVC, "invalid param.");
696         return;
697     }
698 
699     if (data->flag == AUTH_CHANNEL_REQ) {
700         OnRecvAuthChannelRequest(authId, (const char *)data->data, (int32_t)data->len);
701     } else if (data->flag == AUTH_CHANNEL_REPLY) {
702         OnRecvAuthChannelReply(authId, (const char *)data->data, (int32_t)data->len);
703     } else {
704         TRANS_LOGE(TRANS_SVC, "auth channel flags err, authId=%{public}d", authId);
705     }
706 }
707 
OnAuthMsgDataRecv(int32_t authId,const AuthChannelData * data)708 static void OnAuthMsgDataRecv(int32_t authId, const AuthChannelData *data)
709 {
710     if (data == NULL || data->data == NULL) {
711         return;
712     }
713     if (NotifyOnDataReceived(authId, data->data, data->len) != SOFTBUS_OK) {
714         TRANS_LOGE(TRANS_SVC, "recv MODULE_AUTH_MSG err. authId=%{public}d", authId);
715     }
716 }
717 
OnDisconnect(int32_t authId)718 static void OnDisconnect(int32_t authId)
719 {
720     AuthChannelInfo dstInfo;
721     if (GetChannelInfoByAuthId(authId, &dstInfo) != EOK) {
722         TRANS_LOGE(TRANS_SVC, "channel already removed. authId=%{public}d", authId);
723         return;
724     }
725     TRANS_LOGI(TRANS_SVC, "recv channel disconnect event. authId=%{public}d", authId);
726 
727     // If it is an ishare session, clean up the auth manager
728     if (strcmp(dstInfo.appInfo.myData.sessionName, ISHARE_AUTH_SESSION) == 0) {
729         DelAuthMetaManagerByConnectionId(authId);
730     }
731     TransAuthCloseChannel(authId, dstInfo.appInfo.linkType, dstInfo.isClient);
732     DelAuthChannelInfoByChanId((int32_t)(dstInfo.appInfo.myData.channelId));
733     (void)NotifyCloseAuthChannel((const char *)dstInfo.appInfo.myData.pkgName,
734         (int32_t)dstInfo.appInfo.myData.pid, (int32_t)dstInfo.appInfo.myData.channelId);
735 }
736 
GetAppInfo(const char * sessionName,int32_t channelId,AppInfo * appInfo,bool isClient)737 int32_t GetAppInfo(const char *sessionName, int32_t channelId, AppInfo *appInfo, bool isClient)
738 {
739     if (sessionName == NULL || appInfo == NULL) {
740         return SOFTBUS_INVALID_PARAM;
741     }
742     appInfo->appType = APP_TYPE_NOT_CARE;
743     appInfo->businessType = BUSINESS_TYPE_BYTE;
744     appInfo->channelType = CHANNEL_TYPE_AUTH;
745     appInfo->myData.channelId = channelId;
746     appInfo->myData.apiVersion = API_V2;
747     appInfo->peerData.apiVersion = API_V2;
748     appInfo->autoCloseTime = 0;
749     int32_t ret = TransGetUidAndPid(sessionName, &appInfo->myData.uid, &appInfo->myData.pid);
750     if (ret != SOFTBUS_OK) {
751         TRANS_LOGE(TRANS_SVC, "TransGetUidAndPid failed");
752         return ret;
753     }
754     ret = TransGetPkgNameBySessionName(sessionName, appInfo->myData.pkgName, PKG_NAME_SIZE_MAX);
755     if (ret != SOFTBUS_OK) {
756         TRANS_LOGE(TRANS_SVC, "TransGetPkgNameBySessionName failed");
757         return ret;
758     }
759     ret = LnnGetLocalStrInfo(STRING_KEY_DEV_UDID, appInfo->myData.deviceId, sizeof(appInfo->myData.deviceId));
760     if (ret != SOFTBUS_OK) {
761         TRANS_LOGE(TRANS_SVC, "LnnGetLocalStrInfo failed");
762         return ret;
763     }
764     if (strcpy_s(appInfo->myData.sessionName, sizeof(appInfo->myData.sessionName), sessionName) != EOK) {
765         TRANS_LOGE(TRANS_SVC, "copy sessionName failed");
766         return SOFTBUS_STRCPY_ERR;
767     }
768     appInfo->peerData.apiVersion = API_V2;
769     if (strcpy_s(appInfo->peerData.sessionName, sizeof(appInfo->peerData.sessionName), sessionName) != EOK) {
770         return SOFTBUS_STRCPY_ERR;
771     }
772     ret = TransGetLocalConfig(appInfo->channelType, appInfo->businessType, &appInfo->myData.dataConfig);
773     if (ret != SOFTBUS_OK) {
774         return ret;
775     }
776     return SOFTBUS_OK;
777 }
778 
AddAuthChannelInfo(AuthChannelInfo * info)779 static int32_t AddAuthChannelInfo(AuthChannelInfo *info)
780 {
781     if (g_authChannelList == NULL || info == NULL) {
782         TRANS_LOGE(TRANS_SVC, "invalid param");
783         return SOFTBUS_INVALID_PARAM;
784     }
785     DeleteWifiConnItemByConnId(info->authId);
786     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
787         TRANS_LOGE(TRANS_SVC, "fail to lock authChannelList.");
788         return SOFTBUS_LOCK_ERR;
789     }
790     AuthChannelInfo *item = NULL;
791     LIST_FOR_EACH_ENTRY(item, &g_authChannelList->list, AuthChannelInfo, node) {
792         if (item->appInfo.myData.channelId == info->appInfo.myData.channelId) {
793             (void)SoftBusMutexLock(&g_authChannelList->lock);
794             TRANS_LOGE(TRANS_SVC, "found auth channel, channelId=%{public}" PRId64,
795                 info->appInfo.myData.channelId);
796             return SOFTBUS_TRANS_INVALID_CHANNEL_ID;
797         }
798     }
799     ListAdd(&g_authChannelList->list, &info->node);
800     TRANS_LOGI(TRANS_CTRL, "add channelId=%{public}" PRId64 ", isClient=%{public}d",
801         info->appInfo.myData.channelId, info->isClient);
802     g_authChannelList->cnt++;
803     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
804     return SOFTBUS_OK;
805 }
806 
DelAuthChannelInfoByChanId(int32_t channelId)807 static void DelAuthChannelInfoByChanId(int32_t channelId)
808 {
809     if (g_authChannelList == NULL) {
810         return;
811     }
812     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
813         TRANS_LOGE(TRANS_SVC, "lock failed");
814         return;
815     }
816     AuthChannelInfo *item = NULL;
817     AuthChannelInfo *tmp = NULL;
818     LIST_FOR_EACH_ENTRY_SAFE(item, tmp, &g_authChannelList->list, AuthChannelInfo, node) {
819         if (item->appInfo.myData.channelId == channelId) {
820             ListDelete(&item->node);
821             TRANS_LOGI(TRANS_CTRL, "delete channelId=%{public}d", channelId);
822             SoftBusFree(item);
823             g_authChannelList->cnt--;
824             break;
825         }
826     }
827     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
828 }
829 
DelAuthChannelInfoByAuthId(int32_t authId)830 static void DelAuthChannelInfoByAuthId(int32_t authId)
831 {
832     if (g_authChannelList == NULL) {
833         return;
834     }
835     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
836         TRANS_LOGE(TRANS_SVC, "lock failed");
837         return;
838     }
839     AuthChannelInfo *item = NULL;
840     AuthChannelInfo *tmp = NULL;
841     LIST_FOR_EACH_ENTRY_SAFE(item, tmp, &g_authChannelList->list, AuthChannelInfo, node) {
842         if (item->authId == authId) {
843             ListDelete(&item->node);
844             TRANS_LOGI(TRANS_CTRL, "delete authId = %{public}d", item->authId);
845             SoftBusFree(item);
846             g_authChannelList->cnt--;
847             break;
848         }
849     }
850     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
851 }
852 
TransAuthGetNameByChanId(int32_t chanId,char * pkgName,char * sessionName,uint16_t pkgLen,uint16_t sessionLen)853 int32_t TransAuthGetNameByChanId(int32_t chanId, char *pkgName, char *sessionName, uint16_t pkgLen, uint16_t sessionLen)
854 {
855     if (pkgName == NULL || sessionName == NULL) {
856         TRANS_LOGE(TRANS_SVC, "invalid param");
857         return SOFTBUS_INVALID_PARAM;
858     }
859 
860     AuthChannelInfo info;
861     int32_t ret = GetAuthChannelInfoByChanId(chanId, &info);
862     if (ret != SOFTBUS_OK) {
863         TRANS_LOGE(TRANS_SVC, "get channel info by chanId failed. chanId=%{public}d", chanId);
864         return ret;
865     }
866 
867     if (memcpy_s(pkgName, pkgLen, info.appInfo.myData.pkgName, PKG_NAME_SIZE_MAX) != EOK ||
868         memcpy_s(sessionName, sessionLen, info.appInfo.myData.sessionName, SESSION_NAME_SIZE_MAX) != EOK) {
869         TRANS_LOGE(TRANS_SVC, "memcpy_s failed");
870         return SOFTBUS_MEM_ERR;
871     }
872     return SOFTBUS_OK;
873 }
874 
TransAuthInit(IServerChannelCallBack * cb)875 int32_t TransAuthInit(IServerChannelCallBack *cb)
876 {
877     if (cb == NULL) {
878         return SOFTBUS_INVALID_PARAM;
879     }
880     AuthChannelListener channelListener = {
881         .onDataReceived = OnAuthChannelDataRecv,
882         .onDisconnected = OnDisconnect,
883     };
884     AuthChannelListener msgListener = {
885         .onDataReceived = OnAuthMsgDataRecv,
886         .onDisconnected = OnDisconnect,
887     };
888     if (RegAuthChannelListener(MODULE_AUTH_CHANNEL, &channelListener) != SOFTBUS_OK ||
889         RegAuthChannelListener(MODULE_AUTH_MSG, &msgListener) != SOFTBUS_OK) {
890         UnregAuthChannelListener(MODULE_AUTH_CHANNEL);
891         UnregAuthChannelListener(MODULE_AUTH_MSG);
892         return SOFTBUS_TRANS_REG_AUTH_CHANNEL_LISTERNER_FAILED;
893     }
894     if (g_authChannelList == NULL) {
895         g_authChannelList = CreateSoftBusList();
896     }
897     if (g_authChannelList == NULL) {
898         return SOFTBUS_INVALID_PARAM;
899     }
900     if (g_cb == NULL) {
901         g_cb = cb;
902     }
903     return SOFTBUS_OK;
904 }
905 
TransAuthDeinit(void)906 void TransAuthDeinit(void)
907 {
908     UnregAuthChannelListener(MODULE_AUTH_CHANNEL);
909     UnregAuthChannelListener(MODULE_AUTH_MSG);
910     DestroySoftBusList(g_authChannelList);
911     g_authChannelList = NULL;
912     g_cb = NULL;
913 }
914 
TransPostAuthChannelMsg(const AppInfo * appInfo,int32_t authId,int32_t flag)915 static int32_t TransPostAuthChannelMsg(const AppInfo *appInfo, int32_t authId, int32_t flag)
916 {
917     if (appInfo == NULL) {
918         TRANS_LOGW(TRANS_SVC, "invalid param");
919         return SOFTBUS_INVALID_PARAM;
920     }
921     cJSON *msg = cJSON_CreateObject();
922     if (msg == NULL) {
923         TRANS_LOGE(TRANS_SVC, "json failed");
924         return SOFTBUS_MALLOC_ERR;
925     }
926     int32_t ret = TransAuthChannelMsgPack(msg, appInfo);
927     if (ret != SOFTBUS_OK) {
928         cJSON_Delete(msg);
929         TRANS_LOGE(TRANS_SVC, "tran channel msg pack failed");
930         return ret;
931     }
932     char *data = cJSON_PrintUnformatted(msg);
933     cJSON_Delete(msg);
934     if (data == NULL) {
935         TRANS_LOGE(TRANS_SVC, "json failed");
936         return SOFTBUS_PARSE_JSON_ERR;
937     }
938 
939     AuthChannelData channelData = {
940         .module = MODULE_AUTH_CHANNEL,
941         .flag = flag,
942         .seq = 0,
943         .len = strlen(data) + 1,
944         .data = (const uint8_t *)data,
945     };
946     ret = AuthPostChannelData(authId, &channelData);
947     if (ret != SOFTBUS_OK) {
948         TRANS_LOGE(TRANS_SVC, "auth post channel data fail");
949         cJSON_free(data);
950         return ret;
951     }
952     cJSON_free(data);
953     return SOFTBUS_OK;
954 }
955 
TransPostAuthChannelErrMsg(int32_t authId,int32_t errcode,const char * errMsg)956 static void TransPostAuthChannelErrMsg(int32_t authId, int32_t errcode, const char *errMsg)
957 {
958     if (errMsg == NULL) {
959         return;
960     }
961     char cJsonStr[ERR_MSG_MAX_LEN] = {0};
962     int32_t ret = TransAuthChannelErrorPack(errcode, errMsg, cJsonStr, ERR_MSG_MAX_LEN);
963     if (ret != SOFTBUS_OK) {
964         TRANS_LOGE(TRANS_SVC, "TransAuthChannelErrorPack failed");
965         return;
966     }
967     AuthChannelData channelData = {
968         .module = MODULE_AUTH_CHANNEL,
969         .flag = AUTH_CHANNEL_REPLY,
970         .seq = 0,
971         .len = strlen(cJsonStr) + 1,
972         .data = (const uint8_t *)cJsonStr,
973     };
974     if (AuthPostChannelData(authId, &channelData) != SOFTBUS_OK) {
975         TRANS_LOGE(TRANS_SVC, "auth post channel data fail");
976     }
977 }
978 
CreateAuthChannelInfo(const char * sessionName,bool isClient)979 static AuthChannelInfo *CreateAuthChannelInfo(const char *sessionName, bool isClient)
980 {
981     AuthChannelInfo *info = (AuthChannelInfo *)SoftBusCalloc(sizeof(AuthChannelInfo));
982     if (info == NULL) {
983         return NULL;
984     }
985     info->appInfo.myData.channelId = GenerateChannelId(true);
986     if (info->appInfo.myData.channelId <= INVALID_CHANNEL_ID) {
987         TRANS_LOGE(TRANS_SVC, "channelId is invalid");
988         goto EXIT_ERR;
989     }
990     if (GetAppInfo(sessionName, info->appInfo.myData.channelId, &info->appInfo, isClient) != SOFTBUS_OK) {
991         goto EXIT_ERR;
992     }
993     info->isClient = isClient;
994     return info;
995 EXIT_ERR:
996     SoftBusFree(info);
997     return NULL;
998 }
999 
FillAndReportEventStart(const char * sessionName,int32_t * channelId,int32_t connType,TransEventExtra * extra,AuthChannelInfo * channel)1000 static void FillAndReportEventStart(const char *sessionName, int32_t *channelId, int32_t connType,
1001     TransEventExtra *extra, AuthChannelInfo *channel)
1002 {
1003     extra->peerNetworkId = NULL;
1004     extra->calleePkg = NULL;
1005     extra->callerPkg = NULL;
1006     extra->socketName = sessionName;
1007     extra->channelId = *channelId;
1008     extra->channelType = CHANNEL_TYPE_AUTH;
1009     extra->linkType = connType;
1010     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_START_CONNECT, *extra);
1011 }
1012 
FillAndReportEventEnd(int32_t authId,TransEventExtra * extra)1013 static void FillAndReportEventEnd(int32_t authId, TransEventExtra *extra)
1014 {
1015     extra->result = EVENT_STAGE_RESULT_OK;
1016     extra->authId = authId;
1017     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_START_CONNECT, *extra);
1018 }
1019 
CheckForAuthWithParam(const char * sessionName,const LaneConnInfo * connInfo,const int32_t * channelId)1020 static bool CheckForAuthWithParam(const char *sessionName, const LaneConnInfo *connInfo, const int32_t *channelId)
1021 {
1022     if (sessionName == NULL || connInfo == NULL || channelId == NULL) {
1023         TRANS_LOGE(TRANS_SVC, "CheckForAuthWithParam invalid param");
1024         return false;
1025     }
1026     if (g_authChannelList == NULL) {
1027         TRANS_LOGE(TRANS_SVC, "CheckForAuthWithParam g_authChannelList is null");
1028         return false;
1029     }
1030     if (connInfo->type != LANE_HML_RAW) {
1031         TRANS_LOGE(TRANS_SVC, "CheckForAuthWithParam connInfo->type is %{public}d", connInfo->type);
1032         return false;
1033     }
1034     return true;
1035 }
1036 
TransFillAuthChannelInfo(AuthChannelInfo * channel,const LaneConnInfo * connInfo,const int32_t * channelId,bool accountInfo)1037 static int32_t TransFillAuthChannelInfo(AuthChannelInfo *channel, const LaneConnInfo *connInfo,
1038     const int32_t *channelId, bool accountInfo)
1039 {
1040     if (channel == NULL || connInfo == NULL || channelId == NULL) {
1041         TRANS_LOGE(TRANS_SVC, "TransFillAuthChannelInfo invalid parm");
1042         return SOFTBUS_INVALID_PARAM;
1043     }
1044 
1045     (void)memset_s(channel->appInfo.reqId, REQ_ID_SIZE_MAX, 0, REQ_ID_SIZE_MAX);
1046     channel->appInfo.myData.channelId = *channelId;
1047     channel->appInfo.timeStart = GetSoftbusRecordTimeMillis();
1048     channel->appInfo.linkType = connInfo->type;
1049     channel->appInfo.routeType = WIFI_P2P;
1050     channel->connOpt.socketOption.moduleId = AUTH_RAW_P2P_CLIENT;
1051     channel->accountInfo = accountInfo;
1052 
1053     if (LnnGetLocalStrInfo(STRING_KEY_NETWORKID, channel->appInfo.peerNetWorkId,
1054                            sizeof(channel->appInfo.peerNetWorkId)) != SOFTBUS_OK) {
1055         TRANS_LOGW(TRANS_SVC, "LnnGetLocalStrInfo STRING_KEY_NETWORKID failed");
1056     }
1057 
1058     if (strcpy_s(channel->appInfo.myData.addr, IP_LEN, connInfo->connInfo.rawWifiDirect.localIp) != EOK) {
1059         TRANS_LOGE(TRANS_SVC, "TransFillAuthChannelInfo strcpy_s localIp failed");
1060         return SOFTBUS_STRCPY_ERR;
1061     }
1062     if (strcpy_s(channel->appInfo.peerData.addr, IP_LEN, connInfo->connInfo.rawWifiDirect.peerIp) != EOK) {
1063         TRANS_LOGE(TRANS_SVC, "TransFillAuthChannelInfo strcpy_s perrIp failed");
1064         return SOFTBUS_STRCPY_ERR;
1065     }
1066     return SOFTBUS_OK;
1067 }
1068 
PostAuthMsg(AuthChannelInfo * channel,TransEventExtra * extra,const LaneConnInfo * connInfo,const int32_t * channelId)1069 static int32_t PostAuthMsg(AuthChannelInfo *channel, TransEventExtra *extra, const LaneConnInfo *connInfo,
1070     const int32_t *channelId)
1071 {
1072     int32_t authId = AuthOpenChannelWithAllIp(connInfo->connInfo.rawWifiDirect.localIp,
1073         connInfo->connInfo.rawWifiDirect.peerIp, connInfo->connInfo.rawWifiDirect.port);
1074     if (authId < 0) {
1075         TRANS_LOGE(TRANS_SVC, "AuthOpenChannelWithAllIp failed");
1076         SoftBusFree(channel);
1077         return SOFTBUS_TRANS_OPEN_AUTH_CHANNEL_FAILED;
1078     }
1079     extra->result = EVENT_STAGE_RESULT_OK;
1080     extra->authId = authId;
1081     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_START_CONNECT, *extra);
1082     channel->authId = authId;
1083     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
1084         TRANS_LOGE(TRANS_SVC, "SoftBusMutexLock failed");
1085         TransAuthCloseChannel(channel->authId, LANE_HML_RAW, true);
1086         SoftBusFree(channel);
1087         return SOFTBUS_LOCK_ERR;
1088     }
1089     if (AddAuthChannelInfo(channel) != SOFTBUS_OK) {
1090         TRANS_LOGE(TRANS_SVC, "AddAuthChannelInfo failed");
1091         (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1092         TransAuthCloseChannel(channel->authId, LANE_HML_RAW, true);
1093         SoftBusFree(channel);
1094         return SOFTBUS_TRANS_AUTH_ADD_CHANINFO_FAIL;
1095     }
1096     extra->result = 0;
1097     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_START, *extra);
1098     if (TransPostAuthChannelMsg(&channel->appInfo, authId, AUTH_CHANNEL_REQ) !=SOFTBUS_OK) {
1099         TRANS_LOGE(TRANS_SVC, "TransPostAuthRequest failed");
1100         DelAuthChannelInfoByChanId(*channelId);
1101         (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1102         TransAuthCloseChannel(authId, LANE_HML_RAW, true);
1103         return SOFTBUS_TRANS_AUTH_POST_CHANMSG_FAIL;
1104     }
1105     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1106     return SOFTBUS_OK;
1107 }
1108 
TransOpenAuthMsgChannelWithPara(const char * sessionName,const LaneConnInfo * connInfo,int32_t * channelId,bool accountInfo)1109 int32_t TransOpenAuthMsgChannelWithPara(const char *sessionName, const LaneConnInfo *connInfo, int32_t *channelId,
1110     bool accountInfo)
1111 {
1112     if (!CheckForAuthWithParam(sessionName, connInfo, channelId)) {
1113         TRANS_LOGE(TRANS_SVC, "TransOpenAuthMsgChannelWithPara CheckForAuthWithParam fail");
1114         return SOFTBUS_INVALID_PARAM;
1115     }
1116 
1117     AuthChannelInfo *channel = CreateAuthChannelInfo(sessionName, true);
1118     if (channel == NULL) {
1119         TRANS_LOGE(TRANS_SVC, "TransOpenAuthMsgChannelWithPara CreateAuthChannelInfo fail");
1120         return SOFTBUS_TRANS_AUTH_CREATE_CHANINFO_FAIL;
1121     }
1122     if (TransFillAuthChannelInfo(channel, connInfo, channelId, accountInfo) != SOFTBUS_OK) {
1123         SoftBusFree(channel);
1124         TRANS_LOGE(TRANS_SVC, "TransOpenAuthMsgChannelWithPara TransFillAuthChannelInfo failed");
1125         return SOFTBUS_TRANS_AUTH_FILL_CHANINFO_FAIL;
1126     }
1127 
1128     TransEventExtra extra = {
1129         .peerNetworkId = NULL,
1130         .calleePkg = NULL,
1131         .callerPkg = NULL,
1132         .socketName = sessionName,
1133         .channelId = *channelId,
1134         .channelType = CHANNEL_TYPE_AUTH,
1135         .linkType = CONNECT_HML
1136      };
1137     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_START_CONNECT, extra);
1138 
1139     int32_t ret = PostAuthMsg(channel, &extra, connInfo, channelId);
1140     if (ret != SOFTBUS_OK) {
1141         TRANS_LOGE(TRANS_SVC, "PostAuthMsg failed, ret=%{public}d", ret);
1142         return ret;
1143     }
1144     extra.result = EVENT_STAGE_RESULT_OK;
1145     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_START, extra);
1146     return SOFTBUS_OK;
1147 }
1148 
TransOpenAuthChannelPrepareParam(const char * sessionName,const ConnectOption * connOpt,int32_t * channelId,const char * reqId)1149 static AuthChannelInfo *TransOpenAuthChannelPrepareParam(const char *sessionName, const ConnectOption *connOpt,
1150     int32_t *channelId, const char *reqId)
1151 {
1152     AuthChannelInfo *channel = CreateAuthChannelInfo(sessionName, true);
1153     TRANS_CHECK_AND_RETURN_RET_LOGE(channel != NULL, NULL, TRANS_SVC, "fail to add pid");
1154     if (strcpy_s(channel->appInfo.reqId, REQ_ID_SIZE_MAX, reqId) != EOK ||
1155         memcpy_s(&channel->connOpt, sizeof(ConnectOption), connOpt, sizeof(ConnectOption)) != EOK) {
1156         SoftBusFree(channel);
1157         TRANS_LOGE(TRANS_SVC, "fail to copy appInfo and connOpt.");
1158         return NULL;
1159     }
1160     *channelId = (int32_t)channel->appInfo.myData.channelId;
1161     channel->appInfo.timeStart = GetSoftbusRecordTimeMillis();
1162     channel->appInfo.connectType = connOpt->type;
1163     return channel;
1164 }
1165 
TransOpenAuthMsgChannel(const char * sessionName,const ConnectOption * connOpt,int32_t * channelId,const char * reqId)1166 int32_t TransOpenAuthMsgChannel(const char *sessionName, const ConnectOption *connOpt,
1167     int32_t *channelId, const char *reqId)
1168 {
1169     if (connOpt == NULL || channelId == NULL || connOpt->type != CONNECT_TCP || g_authChannelList == NULL) {
1170         return SOFTBUS_INVALID_PARAM;
1171     }
1172     AuthChannelInfo *channel = TransOpenAuthChannelPrepareParam(sessionName, connOpt, channelId, reqId);
1173     if (channel == NULL) {
1174         TRANS_LOGE(TRANS_SVC, "fail to get auth channel info.");
1175         return SOFTBUS_INVALID_PARAM;
1176     }
1177     TransEventExtra extra;
1178     FillAndReportEventStart(sessionName, channelId, connOpt->type, &extra, channel);
1179     int32_t authId = AuthOpenChannel(connOpt->socketOption.addr, connOpt->socketOption.port);
1180     if (authId < 0) {
1181         TRANS_LOGE(TRANS_SVC, "AuthOpenChannel failed");
1182         SoftBusFree(channel);
1183         return SOFTBUS_TRANS_OPEN_AUTH_CHANNEL_FAILED;
1184     }
1185     FillAndReportEventEnd(authId, &extra);
1186     channel->authId = authId;
1187     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
1188         TRANS_LOGE(TRANS_SVC, "SoftBusMutexLock failed");
1189         TransAuthCloseChannel(channel->authId, LANE_HML_RAW, true);
1190         SoftBusFree(channel);
1191         return SOFTBUS_LOCK_ERR;
1192     }
1193     int32_t ret = AddAuthChannelInfo(channel);
1194     if (ret != SOFTBUS_OK) {
1195         TRANS_LOGE(TRANS_SVC, "AddAuthChannelInfo failed");
1196         TransAuthCloseChannel(channel->authId, LANE_HML_RAW, true);
1197         SoftBusFree(channel);
1198         (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1199         return ret;
1200     }
1201     extra.result = 0;
1202     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_START, extra);
1203     ret = TransPostAuthChannelMsg(&channel->appInfo, authId, AUTH_CHANNEL_REQ);
1204     if (ret != SOFTBUS_OK) {
1205         TRANS_LOGE(TRANS_SVC, "TransPostAuthRequest failed");
1206         TransAuthCloseChannel(channel->authId, LANE_HML_RAW, true);
1207         DelAuthChannelInfoByChanId(*channelId);
1208         (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1209         return ret;
1210     }
1211     extra.result = EVENT_STAGE_RESULT_OK;
1212     TRANS_EVENT(EVENT_SCENE_OPEN_CHANNEL, EVENT_STAGE_HANDSHAKE_START, extra);
1213     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1214     return SOFTBUS_OK;
1215 }
1216 
TransCloseAuthChannel(int32_t channelId)1217 int32_t TransCloseAuthChannel(int32_t channelId)
1218 {
1219     AuthChannelInfo *channel = NULL;
1220     AuthChannelInfo *tmp = NULL;
1221     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
1222         TRANS_LOGE(TRANS_SVC, "lock failed");
1223         return SOFTBUS_LOCK_ERR;
1224     }
1225     LIST_FOR_EACH_ENTRY_SAFE(channel, tmp, &g_authChannelList->list, AuthChannelInfo, node) {
1226         if (channel->appInfo.myData.channelId != channelId) {
1227             continue;
1228         }
1229         ListDelete(&channel->node);
1230         TRANS_LOGI(TRANS_CTRL, "delete channelId=%{public}d, authId=%{public}d", channelId, channel->authId);
1231         g_authChannelList->cnt--;
1232         // If it is an ishare session, clean up the auth manager
1233         if (strcmp(channel->appInfo.myData.sessionName, ISHARE_AUTH_SESSION) == 0) {
1234             DelAuthMetaManagerByConnectionId(channel->authId);
1235         }
1236         if (strcmp(channel->appInfo.myData.sessionName, DM_PKG_NAME) == 0) {
1237             DelAuthManagerByConnectionId(channel->authId);
1238         }
1239         TransAuthCloseChannel(channel->authId, channel->appInfo.linkType, channel->isClient);
1240         NotifyCloseAuthChannel(channel->appInfo.myData.pkgName, channel->appInfo.myData.pid, channelId);
1241         SoftBusFree(channel);
1242         (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1243         return SOFTBUS_OK;
1244     }
1245     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1246     return SOFTBUS_TRANS_NODE_NOT_FOUND;
1247 }
1248 
TransSendAuthMsg(int32_t channelId,const char * msg,int32_t len)1249 int32_t TransSendAuthMsg(int32_t channelId, const char *msg, int32_t len)
1250 {
1251     if (msg == NULL || len <= 0) {
1252         return SOFTBUS_INVALID_PARAM;
1253     }
1254 
1255     int32_t authId = GetAuthIdByChannelId(channelId);
1256     if (authId < 0) {
1257         TRANS_LOGE(TRANS_SVC, "Get AuthId failed");
1258         return SOFTBUS_TRANS_AUTH_CHANNEL_NOT_FOUND;
1259     }
1260 
1261     AuthChannelData channelData = {
1262         .module = MODULE_AUTH_MSG,
1263         .flag = 0,
1264         .seq = 0,
1265         .len = (uint32_t)len,
1266         .data = (const uint8_t *)msg,
1267     };
1268     int32_t ret = AuthPostChannelData(authId, &channelData);
1269     if (ret != SOFTBUS_OK) {
1270         TRANS_LOGE(TRANS_SVC, "auth post channel data fail");
1271         return ret;
1272     }
1273     return SOFTBUS_OK;
1274 }
1275 
TransAuthGetConnOptionByChanId(int32_t channelId,ConnectOption * connOpt)1276 int32_t TransAuthGetConnOptionByChanId(int32_t channelId, ConnectOption *connOpt)
1277 {
1278     AuthChannelInfo chanInfo;
1279     int32_t ret = GetAuthChannelInfoByChanId(channelId, &chanInfo);
1280     if (ret != SOFTBUS_OK) {
1281         TRANS_LOGE(TRANS_SVC, "get auth channel info by channelId fail. channelId=%{public}d", channelId);
1282         return ret;
1283     }
1284 
1285     if (!chanInfo.isClient) {
1286         TRANS_LOGE(TRANS_SVC, "auth channel of conn opt invalid");
1287         return SOFTBUS_TRANS_INVALID_CHANNEL_TYPE;
1288     }
1289 
1290     if (memcpy_s(connOpt, sizeof(ConnectOption), &(chanInfo.connOpt), sizeof(ConnectOption)) != EOK) {
1291         TRANS_LOGE(TRANS_SVC, "auth channel connopt memcpy fail");
1292         return SOFTBUS_MEM_ERR;
1293     }
1294     return SOFTBUS_OK;
1295 }
1296 
TransNotifyAuthDataSuccess(int32_t channelId,const ConnectOption * connOpt)1297 int32_t TransNotifyAuthDataSuccess(int32_t channelId, const ConnectOption *connOpt)
1298 {
1299     if (connOpt == NULL) {
1300         TRANS_LOGW(TRANS_SVC, "invalid param.");
1301         return SOFTBUS_INVALID_PARAM;
1302     }
1303     ConnectionAddr addr;
1304     (void)memset_s(&addr, sizeof(ConnectionAddr), 0, sizeof(ConnectionAddr));
1305     if (!LnnConvertOptionToAddr(&addr, connOpt, CONNECTION_ADDR_WLAN)) {
1306         TRANS_LOGE(TRANS_SVC, "channelId convert addr fail. channelId=%{public}d", channelId);
1307         return SOFTBUS_TRANS_CHANNELID_CONVERT_ADDR_FAILED;
1308     }
1309     LnnDfxDeviceInfoReport infoReport;
1310     (void)memset_s(&infoReport, sizeof(LnnDfxDeviceInfoReport), 0, sizeof(LnnDfxDeviceInfoReport));
1311     return LnnNotifyDiscoveryDevice(&addr, &infoReport, true);
1312 }
1313 
TransAuthGetAppInfoByChanId(int32_t channelId,AppInfo * appInfo)1314 int32_t TransAuthGetAppInfoByChanId(int32_t channelId, AppInfo *appInfo)
1315 {
1316     if (appInfo == NULL || g_authChannelList == NULL) {
1317         return SOFTBUS_INVALID_PARAM;
1318     }
1319 
1320     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
1321         TRANS_LOGE(TRANS_SVC, "lock failed");
1322         return SOFTBUS_LOCK_ERR;
1323     }
1324     AuthChannelInfo *info = NULL;
1325     LIST_FOR_EACH_ENTRY(info, &g_authChannelList->list, AuthChannelInfo, node) {
1326         if (info->appInfo.myData.channelId == channelId) {
1327             if (memcpy_s(appInfo, sizeof(AppInfo), &info->appInfo, sizeof(AppInfo)) != EOK) {
1328                 (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1329                 TRANS_LOGE(TRANS_SVC, "auth channel appinfo memcpy fail");
1330                 return SOFTBUS_MEM_ERR;
1331             }
1332             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1333             return SOFTBUS_OK;
1334         }
1335     }
1336     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1337     TRANS_LOGE(TRANS_CTRL, "Auth channel not find: channelId=%{public}d", channelId);
1338     return SOFTBUS_NOT_FIND;
1339 }
1340 
TransAuthGetConnIdByChanId(int32_t channelId,int32_t * connId)1341 int32_t TransAuthGetConnIdByChanId(int32_t channelId, int32_t *connId)
1342 {
1343     if ((g_authChannelList == NULL) || (connId == NULL)) {
1344         TRANS_LOGE(TRANS_SVC, "invalid param");
1345         return SOFTBUS_INVALID_PARAM;
1346     }
1347 
1348     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
1349         TRANS_LOGE(TRANS_SVC, "get mutex lock failed");
1350         return SOFTBUS_LOCK_ERR;
1351     }
1352 
1353     AuthChannelInfo *item = NULL;
1354     LIST_FOR_EACH_ENTRY(item, &g_authChannelList->list, AuthChannelInfo, node) {
1355         if (item->appInfo.myData.channelId == channelId) {
1356             *connId = item->authId;
1357             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1358             return SOFTBUS_OK;
1359         }
1360     }
1361     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1362     TRANS_LOGE(TRANS_SVC, "get connid failed");
1363     return SOFTBUS_TRANS_NODE_NOT_FOUND;
1364 }
1365 
CheckIsWifiAuthChannel(ConnectOption * connInfo)1366 int32_t CheckIsWifiAuthChannel(ConnectOption *connInfo)
1367 {
1368     if (connInfo == NULL || connInfo->socketOption.moduleId != AUTH) {
1369         TRANS_LOGE(
1370             TRANS_SVC, "invalid param, moduleId=%{public}d", connInfo == NULL ? -1 : connInfo->socketOption.moduleId);
1371         return SOFTBUS_INVALID_PARAM;
1372     }
1373     if (g_authChannelList == NULL) {
1374         TRANS_LOGE(TRANS_SVC, "not init auth channel");
1375         return SOFTBUS_NO_INIT;
1376     }
1377     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
1378         TRANS_LOGE(TRANS_SVC, "get mutex lock failed");
1379         return SOFTBUS_LOCK_ERR;
1380     }
1381     AuthChannelInfo *info = NULL;
1382     LIST_FOR_EACH_ENTRY(info, &g_authChannelList->list, AuthChannelInfo, node) {
1383         if (info->connOpt.socketOption.port == connInfo->socketOption.port &&
1384             memcmp(info->connOpt.socketOption.addr, connInfo->socketOption.addr,
1385             strlen(connInfo->socketOption.addr)) == 0) {
1386             TRANS_LOGI(TRANS_SVC, "auth channel type is wifi");
1387             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1388             return SOFTBUS_OK;
1389         }
1390     }
1391     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1392     TRANS_LOGE(TRANS_SVC, "auth channel is not exit");
1393     return SOFTBUS_TRANS_NODE_NOT_FOUND;
1394 }
1395 
TransSetAuthChannelReplyCnt(int32_t channelId)1396 static int32_t TransSetAuthChannelReplyCnt(int32_t channelId)
1397 {
1398     TRANS_CHECK_AND_RETURN_RET_LOGE(
1399         g_authChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_authChannelList is NULL");
1400     int32_t ret = SoftBusMutexLock(&g_authChannelList->lock);
1401     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
1402     AuthChannelInfo *item = NULL;
1403     LIST_FOR_EACH_ENTRY(item, &g_authChannelList->list, AuthChannelInfo, node) {
1404         if (item->appInfo.myData.channelId == channelId) {
1405             item->appInfo.waitOpenReplyCnt = CHANNEL_OPEN_SUCCESS;
1406             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1407             return SOFTBUS_OK;
1408         }
1409     }
1410     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1411     TRANS_LOGE(TRANS_SVC, "Auth channel not find: channelId=%{public}d", channelId);
1412     return SOFTBUS_TRANS_NODE_NOT_FOUND;
1413 }
1414 
TransDealAuthChannelOpenResult(int32_t channelId,int32_t openResult)1415 int32_t TransDealAuthChannelOpenResult(int32_t channelId, int32_t openResult)
1416 {
1417     AuthChannelInfo info;
1418     (void)memset_s(&info, sizeof(AuthChannelInfo), 0, sizeof(AuthChannelInfo));
1419     int32_t ret = GetAuthChannelInfoByChanId(channelId, &info);
1420     if (ret != SOFTBUS_OK) {
1421         TRANS_LOGE(TRANS_CTRL, "get auth channel failed, channelId=%{public}d, ret=%{public}d", channelId, ret);
1422         return ret;
1423     }
1424     ret = TransSetAuthChannelReplyCnt(channelId);
1425     if (ret != SOFTBUS_OK) {
1426         TRANS_LOGE(TRANS_CTRL, "update cnt failed, channelId=%{public}d, ret=%{public}d", channelId, ret);
1427         return ret;
1428     }
1429     TransEventExtra extra = {
1430         .peerNetworkId = NULL,
1431         .calleePkg = NULL,
1432         .callerPkg = NULL,
1433         .channelType = CHANNEL_TYPE_AUTH,
1434         .authId = info.authId,
1435         .result = EVENT_STAGE_RESULT_OK,
1436         .channelId = info.appInfo.myData.channelId,
1437         .socketName = info.appInfo.myData.sessionName,
1438         .peerUdid = info.appInfo.peerData.deviceId,
1439     };
1440     if (openResult != SOFTBUS_OK) {
1441         TRANS_LOGE(TRANS_SVC, "open auth channel failed, openResult=%{public}d", openResult);
1442         TransPostAuthChannelErrMsg(info.authId, openResult, "open auth channel failed");
1443         TransHandleErrorAndCloseChannel(&extra, info.authId, info.appInfo.linkType, info.appInfo.isClient, openResult);
1444         return SOFTBUS_OK;
1445     }
1446     ret = TransPostAuthChannelMsg(&info.appInfo, info.authId, AUTH_CHANNEL_REPLY);
1447     if (ret != SOFTBUS_OK) {
1448         TRANS_LOGE(TRANS_SVC, "send reply failed, ret=%{public}d", ret);
1449         TransHandleErrorAndCloseChannel(&extra, info.authId, info.appInfo.linkType, info.appInfo.isClient, ret);
1450         return ret;
1451     }
1452     return SOFTBUS_OK;
1453 }
1454 
TransCheckAuthChannelOpenStatus(int32_t channelId,int32_t * curCount)1455 static int32_t TransCheckAuthChannelOpenStatus(int32_t channelId, int32_t *curCount)
1456 {
1457     TRANS_CHECK_AND_RETURN_RET_LOGE(
1458         g_authChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_authChannelList is null");
1459     int32_t ret = SoftBusMutexLock(&g_authChannelList->lock);
1460     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
1461     AuthChannelInfo *item = NULL;
1462     LIST_FOR_EACH_ENTRY(item, &g_authChannelList->list, AuthChannelInfo, node) {
1463         if (item->appInfo.myData.channelId == channelId) {
1464             if (item->appInfo.waitOpenReplyCnt != CHANNEL_OPEN_SUCCESS) {
1465                 item->appInfo.waitOpenReplyCnt++;
1466             }
1467             *curCount = item->appInfo.waitOpenReplyCnt;
1468             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1469             return SOFTBUS_OK;
1470         }
1471     }
1472     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1473     TRANS_LOGE(TRANS_SVC, "Auth channel not find: channelId=%{public}d", channelId);
1474     return SOFTBUS_TRANS_NODE_NOT_FOUND;
1475 }
1476 
TransAsyncAuthChannelTask(int32_t channelId)1477 void TransAsyncAuthChannelTask(int32_t channelId)
1478 {
1479     int32_t curCount = 0;
1480     int32_t ret = TransCheckAuthChannelOpenStatus(channelId, &curCount);
1481     if (ret != SOFTBUS_OK) {
1482         TRANS_LOGE(TRANS_CTRL, "check auth channel open failed, channelId=%{public}d, ret=%{public}d", channelId, ret);
1483         return;
1484     }
1485     if (curCount == CHANNEL_OPEN_SUCCESS) {
1486         TRANS_LOGI(TRANS_CTRL, "open auth channel success, channelId=%{public}d", channelId);
1487         return;
1488     }
1489     AuthChannelInfo info;
1490     (void)memset_s(&info, sizeof(AuthChannelInfo), 0, sizeof(AuthChannelInfo));
1491     ret = GetAuthChannelInfoByChanId(channelId, &info);
1492     if (ret != SOFTBUS_OK) {
1493         TRANS_LOGE(TRANS_CTRL, "get auth channel failed, channelId=%{public}d, ret=%{public}d", channelId, ret);
1494         return;
1495     }
1496     if (curCount >= LOOPER_REPLY_CNT_MAX) {
1497         TRANS_LOGE(TRANS_CTRL, "open auth channel timeout, channelId=%{public}d", channelId);
1498         TransPostAuthChannelErrMsg(
1499             info.authId,  SOFTBUS_TRANS_OPEN_CHANNEL_NEGTIATE_TIMEOUT, "open auth channel failed");
1500         DelAuthChannelInfoByAuthId(info.authId);
1501         TransAuthCloseChannel(info.authId, info.appInfo.linkType, info.isClient);
1502         return;
1503     }
1504     TRANS_LOGI(TRANS_CTRL, "Open channelId=%{public}d not finished, generate new task and waiting", channelId);
1505     uint32_t delayTime = (curCount <= LOOPER_SEPARATE_CNT) ? FAST_INTERVAL_MILLISECOND : SLOW_INTERVAL_MILLISECOND;
1506     TransCheckChannelOpenToLooperDelay(channelId, CHANNEL_TYPE_AUTH, delayTime);
1507 }
1508 
TransAuthDestroyChannelList(const ListNode * destroyList)1509 static void TransAuthDestroyChannelList(const ListNode *destroyList)
1510 {
1511     TRANS_CHECK_AND_RETURN_LOGE(
1512         (destroyList != NULL && !IsListEmpty(destroyList)), TRANS_CTRL, "destroyList is null");
1513 
1514     AuthChannelInfo *destroyNode = NULL;
1515     AuthChannelInfo *nextDestroyNode = NULL;
1516     LIST_FOR_EACH_ENTRY_SAFE(destroyNode, nextDestroyNode, destroyList, AuthChannelInfo, node) {
1517         ListDelete(&(destroyNode->node));
1518         TransAuthCloseChannel(destroyNode->authId, destroyNode->appInfo.linkType, destroyNode->isClient);
1519         NotifyCloseAuthChannel(destroyNode->appInfo.myData.pkgName,
1520             destroyNode->appInfo.myData.pid, destroyNode->appInfo.myData.channelId);
1521 
1522         if (destroyNode->appInfo.fastTransData != NULL) {
1523             SoftBusFree((void *)destroyNode->appInfo.fastTransData);
1524         }
1525         (void)memset_s(destroyNode->appInfo.sessionKey, sizeof(destroyNode->appInfo.sessionKey), 0,
1526             sizeof(destroyNode->appInfo.sessionKey));
1527         SoftBusFree(destroyNode);
1528     }
1529     return;
1530 }
1531 
TransAuthDeathCallback(const char * pkgName,int32_t pid)1532 void TransAuthDeathCallback(const char *pkgName, int32_t pid)
1533 {
1534     if (g_authChannelList == NULL || pkgName == NULL) {
1535         TRANS_LOGE(TRANS_CTRL, "invalid param.");
1536         return;
1537     }
1538 
1539     char *anonymizePkgName = NULL;
1540     Anonymize(pkgName, &anonymizePkgName);
1541     TRANS_LOGI(TRANS_CTRL, "pkgName=%{public}s, pid=%{public}d.", AnonymizeWrapper(anonymizePkgName), pid);
1542     AnonymizeFree(anonymizePkgName);
1543 
1544     ListNode destroyList;
1545     ListInit(&destroyList);
1546     AuthChannelInfo *item = NULL;
1547     AuthChannelInfo *tmp = NULL;
1548 
1549     if (SoftBusMutexLock(&g_authChannelList->lock) != SOFTBUS_OK) {
1550         TRANS_LOGE(TRANS_CTRL, "lock failed.");
1551         return;
1552     }
1553     LIST_FOR_EACH_ENTRY_SAFE(item, tmp, &g_authChannelList->list, AuthChannelInfo, node) {
1554         if ((strcmp(item->appInfo.myData.pkgName, pkgName) == 0) && (item->appInfo.myData.pid == pid)) {
1555             ListDelete(&(item->node));
1556             g_authChannelList->cnt--;
1557             ListAdd(&destroyList, &(item->node));
1558             TRANS_LOGE(TRANS_CTRL, "add to destroyList channelId=%{public}" PRId64, item->appInfo.myData.channelId);
1559             continue;
1560         }
1561     }
1562     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1563     TransAuthDestroyChannelList(&destroyList);
1564 }
1565 
TransAuthGetRoleByAuthId(int32_t authId,bool * isClient)1566 int32_t TransAuthGetRoleByAuthId(int32_t authId, bool *isClient)
1567 {
1568     TRANS_CHECK_AND_RETURN_RET_LOGE(
1569         g_authChannelList != NULL, SOFTBUS_NO_INIT, TRANS_CTRL, "g_authChannelList is null.");
1570     TRANS_CHECK_AND_RETURN_RET_LOGE(
1571         isClient != NULL, SOFTBUS_INVALID_PARAM, TRANS_CTRL, "invalid param.");
1572     int32_t ret = SoftBusMutexLock(&g_authChannelList->lock);
1573     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_LOCK_ERR, TRANS_CTRL, "lock mutex fail!");
1574     AuthChannelInfo *item = NULL;
1575     LIST_FOR_EACH_ENTRY(item, &g_authChannelList->list, AuthChannelInfo, node) {
1576         if (item->authId == authId) {
1577             *isClient = item->isClient;
1578             (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1579             return SOFTBUS_OK;
1580         }
1581     }
1582     (void)SoftBusMutexUnlock(&g_authChannelList->lock);
1583     TRANS_LOGE(TRANS_SVC, "Auth channel not find: authId=%{public}d", authId);
1584     return SOFTBUS_TRANS_NODE_NOT_FOUND;
1585 }
1586