• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_session_service.h"
17 
18 #ifndef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #endif
21 
22 #include <unistd.h>
23 
24 #include "client_qos_manager.h"
25 #include "client_trans_channel_manager.h"
26 #include "client_trans_file_listener.h"
27 #include "client_trans_session_manager.h"
28 #include "dfs_session.h"
29 #include "inner_session.h"
30 #include "securec.h"
31 #include "softbus_adapter_mem.h"
32 #include "softbus_client_frame_manager.h"
33 #include "softbus_def.h"
34 #include "softbus_errcode.h"
35 #include "softbus_json_utils.h"
36 #include "softbus_log.h"
37 #include "softbus_trans_def.h"
38 #include "softbus_feature_config.h"
39 #include "softbus_utils.h"
40 #include "trans_server_proxy.h"
41 
42 typedef int (*SessionOptionRead)(int32_t channelId, int32_t type, void* value, uint32_t valueSize);
43 typedef int (*SessionOptionWrite)(int32_t channelId, int32_t type, void* value, uint32_t valueSize);
44 
45 typedef struct {
46     bool canRead;
47     SessionOptionRead readFunc;
48 } SessionOptionItem;
49 
50 typedef struct {
51     int32_t channelType;
52     int32_t businessType;
53     ConfigType configType;
54 } ConfigTypeMap;
55 
IsValidSessionId(int sessionId)56 static bool IsValidSessionId(int sessionId)
57 {
58     if (sessionId <= 0) {
59         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid sessionId [%d]", sessionId);
60         return false;
61     }
62     return true;
63 }
64 
IsValidListener(const ISessionListener * listener)65 static bool IsValidListener(const ISessionListener *listener)
66 {
67     if ((listener != NULL) &&
68         (listener->OnSessionOpened != NULL) &&
69         (listener->OnSessionClosed != NULL)) {
70         return true;
71     }
72     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid ISessionListener");
73     return false;
74 }
75 
OpenSessionWithExistSession(int32_t sessionId,bool isEnabled)76 static int32_t OpenSessionWithExistSession(int32_t sessionId, bool isEnabled)
77 {
78     if (!isEnabled) {
79         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "the channel is opening");
80         return sessionId;
81     }
82 
83     ISessionListener listener = {0};
84     if (ClientGetSessionCallbackById(sessionId, &listener) != SOFTBUS_OK) {
85         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session listener failed");
86         return sessionId;
87     }
88 
89     if (listener.OnSessionOpened(sessionId, SOFTBUS_OK) != 0) {
90         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "session callback OnSessionOpened failed");
91         CloseSession(sessionId);
92         return INVALID_SESSION_ID;
93     }
94     return sessionId;
95 }
96 
CreateSessionServer(const char * pkgName,const char * sessionName,const ISessionListener * listener)97 int CreateSessionServer(const char *pkgName, const char *sessionName, const ISessionListener *listener)
98 {
99     if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX - 1) || !IsValidString(sessionName, SESSION_NAME_SIZE_MAX - 1) ||
100         !IsValidListener(listener)) {
101         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CreateSessionServer invalid param");
102         return SOFTBUS_INVALID_PARAM;
103     }
104     char *anonyOut = NULL;
105     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CreateSessionServer: pkgName=%s, sessionName=%s",
106         pkgName, AnonyDevId(&anonyOut, sessionName));
107     SoftBusFree(anonyOut);
108 
109     if (InitSoftBus(pkgName) != SOFTBUS_OK) {
110         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "init softbus err");
111         return SOFTBUS_TRANS_SESSION_ADDPKG_FAILED;
112     }
113 
114     if (CheckPackageName(pkgName) != SOFTBUS_OK) {
115         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid pkg name");
116         return SOFTBUS_INVALID_PKGNAME;
117     }
118 
119     int ret = ClientAddSessionServer(SEC_TYPE_CIPHERTEXT, pkgName, sessionName, listener);
120     if (ret == SOFTBUS_SERVER_NAME_REPEATED) {
121         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "SessionServer is already created in client");
122     } else if (ret != SOFTBUS_OK) {
123         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add session server err, ret=%d.", ret);
124         return ret;
125     }
126 
127     ret = ServerIpcCreateSessionServer(pkgName, sessionName);
128     if (ret == SOFTBUS_SERVER_NAME_REPEATED) {
129         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "SessionServer is already created in server");
130         ret = SOFTBUS_OK;
131     } else if (ret != SOFTBUS_OK) {
132         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Server createSessionServer failed");
133         (void)ClientDeleteSessionServer(SEC_TYPE_CIPHERTEXT, sessionName);
134     }
135     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CreateSessionServer ok: ret=%d", ret);
136     return ret;
137 }
138 
RemoveSessionServer(const char * pkgName,const char * sessionName)139 int RemoveSessionServer(const char *pkgName, const char *sessionName)
140 {
141     if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX - 1) || !IsValidString(sessionName, SESSION_NAME_SIZE_MAX - 1)) {
142         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "RemoveSessionServer invalid param");
143         return SOFTBUS_INVALID_PARAM;
144     }
145     char *anonyOut = NULL;
146     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "RemoveSessionServer: pkgName=%s, sessionName=%s",
147         pkgName, AnonyDevId(&anonyOut, sessionName));
148     SoftBusFree(anonyOut);
149 
150     int32_t ret = ServerIpcRemoveSessionServer(pkgName, sessionName);
151     if (ret != SOFTBUS_OK) {
152         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "remove in server failed, ret=%d.", ret);
153         return ret;
154     }
155 
156     ret = ClientDeleteSessionServer(SEC_TYPE_CIPHERTEXT, sessionName);
157     if (ret != SOFTBUS_OK) {
158         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "delete session server[%s] failed, ret=%d.", sessionName, ret);
159         DeleteFileListener(sessionName);
160         return ret;
161     }
162     DeleteFileListener(sessionName);
163     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "RemoveSessionServer ok: ret=%d", ret);
164     return ret;
165 }
166 
CheckParamIsValid(const char * mySessionName,const char * peerSessionName,const char * peerNetworkId,const char * groupId,const SessionAttribute * attr)167 static int32_t CheckParamIsValid(const char *mySessionName, const char *peerSessionName,
168     const char *peerNetworkId, const char *groupId, const SessionAttribute *attr)
169 {
170     if (!IsValidString(mySessionName, SESSION_NAME_SIZE_MAX) ||
171         !IsValidString(peerSessionName, SESSION_NAME_SIZE_MAX) ||
172         !IsValidString(peerNetworkId, DEVICE_ID_SIZE_MAX) ||
173         (attr == NULL) ||
174         (attr->dataType >= TYPE_BUTT)) {
175         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
176         return SOFTBUS_INVALID_PARAM;
177     }
178 
179     if (groupId == NULL || strlen(groupId) >= GROUP_ID_SIZE_MAX) {
180         return SOFTBUS_INVALID_PARAM;
181     }
182 
183     return SOFTBUS_OK;
184 }
185 
OpenSession(const char * mySessionName,const char * peerSessionName,const char * peerNetworkId,const char * groupId,const SessionAttribute * attr)186 int OpenSession(const char *mySessionName, const char *peerSessionName, const char *peerNetworkId,
187     const char *groupId, const SessionAttribute *attr)
188 {
189     int ret = CheckParamIsValid(mySessionName, peerSessionName, peerNetworkId, groupId, attr);
190     if (ret != SOFTBUS_OK) {
191         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenSession invalid param, ret=%d.", ret);
192         return SOFTBUS_INVALID_PARAM;
193     }
194     char *anonyOutMy = NULL;
195     char *anonyOutPeer = NULL;
196     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenSession: mySessionName=%s, peerSessionName=%s",
197         AnonyDevId(&anonyOutMy, mySessionName), AnonyDevId(&anonyOutPeer, peerSessionName));
198     SoftBusFree(anonyOutMy);
199     SoftBusFree(anonyOutPeer);
200 
201     TransInfo transInfo;
202     SessionParam param = {
203         .sessionName = mySessionName,
204         .peerSessionName = peerSessionName,
205         .peerDeviceId = peerNetworkId,
206         .groupId = groupId,
207         .attr = attr,
208     };
209 
210     int32_t sessionId = INVALID_SESSION_ID;
211     bool isEnabled = false;
212 
213     ret = ClientAddSession(&param, &sessionId, &isEnabled);
214     if (ret != SOFTBUS_OK) {
215         if (ret == SOFTBUS_TRANS_SESSION_REPEATED) {
216             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "session already opened");
217             return OpenSessionWithExistSession(sessionId, isEnabled);
218         }
219         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add session err: ret=%d", ret);
220         return ret;
221     }
222 
223     ret = ServerIpcOpenSession(&param, &transInfo);
224     if (ret != SOFTBUS_OK) {
225         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "open session ipc err: ret=%d", ret);
226         (void)ClientDeleteSession(sessionId);
227         return ret;
228     }
229 
230     ret = ClientSetChannelBySessionId(sessionId, &transInfo);
231     if (ret != SOFTBUS_OK) {
232         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "open session failed");
233         (void)ClientDeleteSession(sessionId);
234         return INVALID_SESSION_ID;
235     }
236     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenSession ok: sessionId=%d, channelId=%d, channelType = %d",
237         sessionId, transInfo.channelId, transInfo.channelType);
238     return sessionId;
239 }
240 
ConvertAddrStr(const char * addrStr,ConnectionAddr * addrInfo)241 static int32_t ConvertAddrStr(const char *addrStr, ConnectionAddr *addrInfo)
242 {
243     if (addrStr == NULL || addrInfo == NULL) {
244         return SOFTBUS_INVALID_PARAM;
245     }
246 
247     cJSON *obj = cJSON_Parse(addrStr);
248     if (obj == NULL) {
249         return SOFTBUS_PARSE_JSON_ERR;
250     }
251     if (memset_s(addrInfo, sizeof(ConnectionAddr), 0x0, sizeof(ConnectionAddr)) != EOK) {
252         cJSON_Delete(obj);
253         return SOFTBUS_MEM_ERR;
254     }
255     int port;
256     if (GetJsonObjectStringItem(obj, "ETH_IP", addrInfo->info.ip.ip, IP_STR_MAX_LEN) &&
257         GetJsonObjectNumberItem(obj, "ETH_PORT", &port)) {
258         addrInfo->info.ip.port = (uint16_t)port;
259         if (IsValidString(addrInfo->info.ip.ip, IP_STR_MAX_LEN) && addrInfo->info.ip.port > 0) {
260             cJSON_Delete(obj);
261             addrInfo->type = CONNECTION_ADDR_ETH;
262             return SOFTBUS_OK;
263         }
264     }
265     if (GetJsonObjectStringItem(obj, "WIFI_IP", addrInfo->info.ip.ip, IP_STR_MAX_LEN) &&
266         GetJsonObjectNumberItem(obj, "WIFI_PORT", &port)) {
267         addrInfo->info.ip.port = (uint16_t)port;
268         if (IsValidString(addrInfo->info.ip.ip, IP_STR_MAX_LEN) && addrInfo->info.ip.port > 0) {
269             cJSON_Delete(obj);
270             addrInfo->type = CONNECTION_ADDR_WLAN;
271             return SOFTBUS_OK;
272         }
273     }
274     if (GetJsonObjectStringItem(obj, "BR_MAC", addrInfo->info.br.brMac, BT_MAC_LEN)) {
275         cJSON_Delete(obj);
276         addrInfo->type = CONNECTION_ADDR_BR;
277         return SOFTBUS_OK;
278     }
279     if (GetJsonObjectStringItem(obj, "BLE_MAC", addrInfo->info.ble.bleMac, BT_MAC_LEN)) {
280         cJSON_Delete(obj);
281         addrInfo->type = CONNECTION_ADDR_BLE;
282         return SOFTBUS_OK;
283     }
284     cJSON_Delete(obj);
285     return SOFTBUS_ERR;
286 }
287 
IsValidAddrInfoArr(const ConnectionAddr * addrInfo,int num)288 static int IsValidAddrInfoArr(const ConnectionAddr *addrInfo, int num)
289 {
290     int32_t addrIndex = -1;
291     if (addrInfo == NULL || num <= 0) {
292         return addrIndex;
293     }
294     int32_t wifiIndex = -1;
295     int32_t brIndex = -1;
296     int32_t bleIndex = -1;
297     for (int32_t index = 0; index < num; index++) {
298         if ((addrInfo[index].type == CONNECTION_ADDR_ETH || addrInfo[index].type == CONNECTION_ADDR_WLAN) &&
299             wifiIndex < 0) {
300             wifiIndex = index;
301         }
302         if (addrInfo[index].type == CONNECTION_ADDR_BR && brIndex < 0) {
303             brIndex = index;
304         }
305         if (addrInfo[index].type == CONNECTION_ADDR_BLE && bleIndex < 0) {
306             bleIndex = index;
307         }
308     }
309     addrIndex = (wifiIndex >= 0) ? wifiIndex : addrIndex;
310     addrIndex = (addrIndex < 0) ? brIndex : addrIndex;
311     addrIndex = (addrIndex < 0) ? bleIndex : addrIndex;
312     return addrIndex;
313 }
314 
OpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo,int num,const char * mixAddr)315 int OpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo, int num, const char *mixAddr)
316 {
317     if (!IsValidString(sessionName, SESSION_NAME_SIZE_MAX - 1)) {
318         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
319         return SOFTBUS_INVALID_PARAM;
320     }
321     TransInfo transInfo;
322     int32_t addrIndex = IsValidAddrInfoArr(addrInfo, num);
323     ConnectionAddr *addr = NULL;
324     ConnectionAddr mix;
325     if (addrIndex < 0) {
326         if (ConvertAddrStr(mixAddr, &mix) != SOFTBUS_OK) {
327             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid addrInfo param");
328             return SOFTBUS_INVALID_PARAM;
329         }
330         addr = &mix;
331     } else {
332         addr = (ConnectionAddr *)&addrInfo[addrIndex];
333     }
334     char *anonyOut = NULL;
335     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenAuthSession: mySessionName=%s",
336         AnonyDevId(&anonyOut, sessionName));
337     SoftBusFree(anonyOut);
338 
339     int32_t sessionId;
340     int32_t ret = ClientAddAuthSession(sessionName, &sessionId);
341     if (ret != SOFTBUS_OK) {
342         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add non encrypt session err: ret=%d", ret);
343         return ret;
344     }
345 
346     transInfo.channelId = ServerIpcOpenAuthSession(sessionName, addr);
347     if (addr->type == CONNECTION_ADDR_BR || addr->type == CONNECTION_ADDR_BLE) {
348         transInfo.channelType = CHANNEL_TYPE_PROXY;
349     } else {
350         transInfo.channelType = CHANNEL_TYPE_AUTH;
351     }
352     ret = ClientSetChannelBySessionId(sessionId, &transInfo);
353     if (ret != SOFTBUS_OK) {
354         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenAuthSession failed");
355         (void)ClientDeleteSession(sessionId);
356         return INVALID_SESSION_ID;
357     }
358     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenAuthSession ok: sessionId=%d, channelId=%d, channelType = %d",
359         sessionId, transInfo.channelId, transInfo.channelType);
360     return sessionId;
361 }
362 
NotifyAuthSuccess(int sessionId)363 void NotifyAuthSuccess(int sessionId)
364 {
365     int32_t channelId = -1;
366     int32_t channelType = -1;
367     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "NotifyAuthSuccess sessionId:%d", sessionId);
368     int32_t ret = ClientGetChannelBySessionId(sessionId, &channelId, &channelType, NULL);
369     if (ret != SOFTBUS_OK) {
370         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get session=%d channel err, ret:%d.", sessionId, ret);
371         return;
372     }
373 
374     int isServer = 0;
375     if (ClientGetSessionIntegerDataById(sessionId, &isServer, KEY_IS_SERVER) != SOFTBUS_OK) {
376         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "get isServer failed");
377         return;
378     }
379     if (isServer) {
380         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "device is service side, no notification");
381         return;
382     }
383     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "device is client side");
384 
385     if (ServerIpcNotifyAuthSuccess(channelId, channelType) != SOFTBUS_OK) {
386         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
387             "channel=%d type=%d ServerIpcNotifyAuthSuccess err", channelId, channelType);
388         return;
389     }
390 }
391 
CheckSessionIsOpened(int32_t sessionId)392 static int32_t CheckSessionIsOpened(int32_t sessionId)
393 {
394 #define SESSION_STATUS_CHECK_MAX_NUM 100
395 #define SESSION_CHECK_PERIOD 50000
396     int32_t i = 0;
397     bool isEnable = false;
398 
399     while (i < SESSION_STATUS_CHECK_MAX_NUM) {
400         if (ClientGetChannelBySessionId(sessionId, NULL, NULL, &isEnable) != SOFTBUS_OK) {
401             return SOFTBUS_NOT_FIND;
402         }
403         if (isEnable == true) {
404             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CheckSessionIsOpened session is enable");
405             return SOFTBUS_OK;
406         }
407         usleep(SESSION_CHECK_PERIOD);
408         i++;
409     }
410 
411     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CheckSessionIsOpened session open timeout");
412     return SOFTBUS_ERR;
413 }
414 
OpenSessionSyncOutSessionName(const char * mySessionName,const char * peerSessionName)415 static void OpenSessionSyncOutSessionName(const char *mySessionName, const char *peerSessionName)
416 {
417     char *anonyOutMy = NULL;
418     char *anonyOutPeer = NULL;
419     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenSessionSync: mySessionName=%s, peerSessionName=%s",
420         AnonyDevId(&anonyOutMy, mySessionName), AnonyDevId(&anonyOutPeer, peerSessionName));
421     SoftBusFree(anonyOutMy);
422     SoftBusFree(anonyOutPeer);
423 }
424 
OpenSessionSync(const char * mySessionName,const char * peerSessionName,const char * peerNetworkId,const char * groupId,const SessionAttribute * attr)425 int OpenSessionSync(const char *mySessionName, const char *peerSessionName, const char *peerNetworkId,
426     const char *groupId, const SessionAttribute *attr)
427 {
428     int ret = CheckParamIsValid(mySessionName, peerSessionName, peerNetworkId, groupId, attr);
429     if (ret != SOFTBUS_OK) {
430         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenSessionSync invalid param");
431         return INVALID_SESSION_ID;
432     }
433     OpenSessionSyncOutSessionName(mySessionName, peerSessionName);
434 
435     TransInfo transInfo;
436     SessionParam param = {
437         .sessionName = mySessionName,
438         .peerSessionName = peerSessionName,
439         .peerDeviceId = peerNetworkId,
440         .groupId = groupId,
441         .attr = attr,
442     };
443 
444     int32_t sessionId = INVALID_SESSION_ID;
445     bool isEnabled = false;
446 
447     ret = ClientAddSession(&param, &sessionId, &isEnabled);
448     if (ret != SOFTBUS_OK) {
449         if (ret == SOFTBUS_TRANS_SESSION_REPEATED) {
450             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "session already opened");
451             CheckSessionIsOpened(sessionId);
452             return OpenSessionWithExistSession(sessionId, isEnabled);
453         }
454         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "add session err: ret=%d", ret);
455         return ret;
456     }
457 
458     ret = ServerIpcOpenSession(&param, &transInfo);
459     if (ret != SOFTBUS_OK) {
460         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "open session ipc err: ret=%d", ret);
461         (void)ClientDeleteSession(sessionId);
462         return ret;
463     }
464     ret = ClientSetChannelBySessionId(sessionId, &transInfo);
465     if (ret != SOFTBUS_OK) {
466         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server open session err: ret=%d", ret);
467         (void)ClientDeleteSession(sessionId);
468         return SOFTBUS_TRANS_SESSION_SET_CHANNEL_FAILED;
469     }
470 
471     ret = CheckSessionIsOpened(sessionId);
472     if (ret != SOFTBUS_OK) {
473         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CheckSessionIsOpened err: ret=%d", ret);
474         (void)ClientDeleteSession(sessionId);
475         return SOFTBUS_TRANS_SESSION_NO_ENABLE;
476     }
477     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "OpenSessionSync ok: sessionId=%d, channelId=%d",
478         sessionId, transInfo.channelId);
479     return sessionId;
480 }
481 
CloseSession(int sessionId)482 void CloseSession(int sessionId)
483 {
484     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CloseSession: sessionId=%d", sessionId);
485     int32_t channelId = INVALID_CHANNEL_ID;
486     int32_t type = CHANNEL_TYPE_BUTT;
487     int32_t ret;
488 
489     if (!IsValidSessionId(sessionId)) {
490         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
491         return;
492     }
493     ret = ClientGetChannelBySessionId(sessionId, &channelId, &type, NULL);
494     if (ret != SOFTBUS_OK) {
495         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get channel err");
496         return;
497     }
498     ret = ClientTransCloseChannel(channelId, type);
499     if (ret != SOFTBUS_OK) {
500         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "close channel err: ret=%d, channelId=%d, channeType=%d",
501             ret, channelId, type);
502     }
503 
504     ret = ClientDeleteSession(sessionId);
505     if (ret != SOFTBUS_OK) {
506         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "CloseSession delete session err");
507     }
508     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "CloseSession ok");
509     return;
510 }
511 
GetMySessionName(int sessionId,char * sessionName,unsigned int len)512 int GetMySessionName(int sessionId, char *sessionName, unsigned int len)
513 {
514     if (!IsValidSessionId(sessionId) || (sessionName == NULL) || (len > SESSION_NAME_SIZE_MAX)) {
515         return SOFTBUS_INVALID_PARAM;
516     }
517 
518     return ClientGetSessionDataById(sessionId, sessionName, len, KEY_SESSION_NAME);
519 }
520 
GetPeerSessionName(int sessionId,char * sessionName,unsigned int len)521 int GetPeerSessionName(int sessionId, char *sessionName, unsigned int len)
522 {
523     if (!IsValidSessionId(sessionId) || (sessionName == NULL) || (len > SESSION_NAME_SIZE_MAX)) {
524         return SOFTBUS_INVALID_PARAM;
525     }
526 
527     return ClientGetSessionDataById(sessionId, sessionName, len, KEY_PEER_SESSION_NAME);
528 }
529 
GetPeerDeviceId(int sessionId,char * networkId,unsigned int len)530 int GetPeerDeviceId(int sessionId, char *networkId, unsigned int len)
531 {
532     if (!IsValidSessionId(sessionId) || (networkId  == NULL) || (len > SESSION_NAME_SIZE_MAX)) {
533         return SOFTBUS_INVALID_PARAM;
534     }
535 
536     return ClientGetSessionDataById(sessionId, networkId, len, KEY_PEER_DEVICE_ID);
537 }
538 
GetSessionSide(int sessionId)539 int GetSessionSide(int sessionId)
540 {
541     return ClientGetSessionSide(sessionId);
542 }
543 
IsValidFileReceivePath(const char * rootDir)544 static bool IsValidFileReceivePath(const char *rootDir)
545 {
546     if (!IsValidString(rootDir, FILE_RECV_ROOT_DIR_SIZE_MAX)) {
547         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "receive path[%s] invalid.", rootDir);
548         return false;
549     }
550     char *absPath = realpath(rootDir, NULL);
551     if (absPath == NULL) {
552         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "receive path[%s] not exist,[%d].", rootDir, errno);
553         return false;
554     }
555     SoftBusFree(absPath);
556     return true;
557 }
558 
SetFileReceiveListener(const char * pkgName,const char * sessionName,const IFileReceiveListener * recvListener,const char * rootDir)559 int SetFileReceiveListener(const char *pkgName, const char *sessionName,
560     const IFileReceiveListener *recvListener, const char *rootDir)
561 {
562     if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX - 1) || !IsValidString(sessionName, SESSION_NAME_SIZE_MAX - 1) ||
563         !IsValidFileReceivePath(rootDir) || (recvListener == NULL)) {
564         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "set file receive listener invalid param");
565         return SOFTBUS_INVALID_PARAM;
566     }
567     if (InitSoftBus(pkgName) != SOFTBUS_OK) {
568         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "set file receive listener init softbus client error");
569         return SOFTBUS_TRANS_SESSION_ADDPKG_FAILED;
570     }
571     return TransSetFileReceiveListener(sessionName, recvListener, rootDir);
572 }
573 
SetFileSendListener(const char * pkgName,const char * sessionName,const IFileSendListener * sendListener)574 int SetFileSendListener(const char *pkgName, const char *sessionName, const IFileSendListener *sendListener)
575 {
576     if (!IsValidString(pkgName, PKG_NAME_SIZE_MAX - 1) || !IsValidString(sessionName, SESSION_NAME_SIZE_MAX - 1) ||
577         sendListener == NULL) {
578         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "set file send listener invalid param");
579         return SOFTBUS_INVALID_PARAM;
580     }
581     if (InitSoftBus(pkgName) != SOFTBUS_OK) {
582         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "set file send listener init softbus client error");
583         return SOFTBUS_TRANS_SESSION_ADDPKG_FAILED;
584     }
585     return TransSetFileSendListener(sessionName, sendListener);
586 }
587 
588 static const char *g_busName = "DistributedFileService";
589 
IsValidDFSSession(int32_t sessionId,int32_t * channelId)590 static int32_t IsValidDFSSession(int32_t sessionId, int32_t *channelId)
591 {
592     char sessionName[SESSION_NAME_SIZE_MAX] = {0};
593     int32_t type;
594     if (GetMySessionName(sessionId, sessionName, SESSION_NAME_SIZE_MAX) != SOFTBUS_OK) {
595         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get dfs session name failed");
596         return SOFTBUS_ERR;
597     }
598     if (strncmp(sessionName, g_busName, strlen(g_busName)) != 0) {
599         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid dfs session name");
600         return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
601     }
602 
603     if (ClientGetChannelBySessionId(sessionId, channelId, &type, NULL) != SOFTBUS_OK) {
604         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get channel failed");
605         return SOFTBUS_ERR;
606     }
607     if (type != CHANNEL_TYPE_TCP_DIRECT) {
608         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid channel type");
609         return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
610     }
611     return SOFTBUS_OK;
612 }
613 
GetSessionKey(int32_t sessionId,char * key,unsigned int len)614 int32_t GetSessionKey(int32_t sessionId, char *key, unsigned int len)
615 {
616     int32_t channelId;
617     if (!IsValidSessionId(sessionId) || key == NULL || len < SESSION_KEY_LEN) {
618         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
619         return SOFTBUS_INVALID_PARAM;
620     }
621     if (IsValidDFSSession(sessionId, &channelId) != SOFTBUS_OK) {
622         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid dfs session");
623         return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
624     }
625     return ClientGetSessionKey(channelId, key, len);
626 }
627 
GetSessionHandle(int32_t sessionId,int * handle)628 int32_t GetSessionHandle(int32_t sessionId, int *handle)
629 {
630     int32_t channelId;
631     if (!IsValidSessionId(sessionId) || handle == NULL) {
632         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
633         return SOFTBUS_INVALID_PARAM;
634     }
635     if (IsValidDFSSession(sessionId, &channelId) != SOFTBUS_OK) {
636         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid dfs session");
637         return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
638     }
639     return ClientGetHandle(channelId, handle);
640 }
641 
DisableSessionListener(int32_t sessionId)642 int32_t DisableSessionListener(int32_t sessionId)
643 {
644     int32_t channelId;
645     if (!IsValidSessionId(sessionId)) {
646         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid param");
647         return SOFTBUS_INVALID_PARAM;
648     }
649     if (IsValidDFSSession(sessionId, &channelId) != SOFTBUS_OK) {
650         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid dfs session");
651         return SOFTBUS_TRANS_FUNC_NOT_SUPPORT;
652     }
653     return ClientDisableSessionListener(channelId);
654 }
655 
QosReport(int32_t sessionId,int32_t appType,int32_t quality)656 int32_t QosReport(int32_t sessionId, int32_t appType, int32_t quality)
657 {
658     if (quality != QOS_IMPROVE && quality != QOS_RECOVER) {
659         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "qos report invalid param");
660         return SOFTBUS_INVALID_PARAM;
661     }
662 
663     int32_t channelId = INVALID_CHANNEL_ID;
664     int32_t type = CHANNEL_TYPE_BUTT;
665     int32_t ret = ClientGetChannelBySessionId(sessionId, &channelId, &type, NULL);
666     if (ret != SOFTBUS_OK) {
667         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get channel err, ret=%d.", ret);
668         return ret;
669     }
670     if (ClientGetSessionSide(sessionId) != IS_CLIENT) {
671         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
672             "qos report sessionId[%d] not exist or not client side", sessionId);
673         return SOFTBUS_TRANS_INVALID_SESSION_ID;
674     }
675     if ((ret = ClientQosReport(channelId, type, appType, quality)) != SOFTBUS_OK) {
676         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "qos report sessionId[%d] failed", sessionId);
677     }
678     return ret;
679 }
680 
681 static const ConfigTypeMap g_configTypeMap[] = {
682     {CHANNEL_TYPE_AUTH, BUSINESS_TYPE_BYTE, SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH},
683     {CHANNEL_TYPE_AUTH, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH},
684     {CHANNEL_TYPE_PROXY, BUSINESS_TYPE_BYTE, SOFTBUS_INT_PROXY_MAX_BYTES_LENGTH},
685     {CHANNEL_TYPE_PROXY, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_PROXY_MAX_MESSAGE_LENGTH},
686     {CHANNEL_TYPE_TCP_DIRECT, BUSINESS_TYPE_BYTE, SOFTBUS_INT_MAX_BYTES_LENGTH},
687     {CHANNEL_TYPE_TCP_DIRECT, BUSINESS_TYPE_MESSAGE, SOFTBUS_INT_MAX_MESSAGE_LENGTH},
688 };
689 
FindConfigType(int32_t channelType,int32_t businessType)690 int32_t FindConfigType(int32_t channelType, int32_t businessType)
691 {
692     for (uint32_t i = 0; i < sizeof(g_configTypeMap) / sizeof(ConfigTypeMap); i++) {
693         if ((g_configTypeMap[i].channelType == channelType) &&
694             (g_configTypeMap[i].businessType == businessType)) {
695             return g_configTypeMap[i].configType;
696         }
697     }
698     return SOFTBUS_CONFIG_TYPE_MAX;
699 }
700 
ReadMaxSendBytesSize(int32_t channelId,int32_t type,void * value,uint32_t valueSize)701 int ReadMaxSendBytesSize(int32_t channelId, int32_t type, void* value, uint32_t valueSize)
702 {
703     (void)channelId;
704     if (valueSize != sizeof(uint32_t)) {
705         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "value size is %d, not match", valueSize);
706         return SOFTBUS_INVALID_PARAM;
707     }
708 
709     ConfigType configType = (ConfigType)FindConfigType(type, BUSINESS_TYPE_BYTE);
710     if (configType == SOFTBUS_CONFIG_TYPE_MAX) {
711         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Invalid channelType: %d, businessType: %d",
712             type, BUSINESS_TYPE_BYTE);
713         return SOFTBUS_INVALID_PARAM;
714     }
715     uint32_t maxLen;
716     if (SoftbusGetConfig(configType, (unsigned char *)&maxLen, sizeof(maxLen)) != SOFTBUS_OK) {
717         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get config failed, configType: %d.", configType);
718         return SOFTBUS_GET_CONFIG_VAL_ERR;
719     }
720 
721     (*(uint32_t*)value) = maxLen;
722     return SOFTBUS_OK;
723 }
724 
ReadMaxSendMessageSize(int32_t channelId,int32_t type,void * value,uint32_t valueSize)725 int ReadMaxSendMessageSize(int32_t channelId, int32_t type, void* value, uint32_t valueSize)
726 {
727     (void)channelId;
728     if (valueSize != sizeof(uint32_t)) {
729         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "value size is %d, not match", valueSize);
730         return SOFTBUS_INVALID_PARAM;
731     }
732 
733     ConfigType configType = (ConfigType)FindConfigType(type, BUSINESS_TYPE_MESSAGE);
734     if (configType == SOFTBUS_CONFIG_TYPE_MAX) {
735         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Invalid channelType: %d, businessType: %d",
736             type, BUSINESS_TYPE_MESSAGE);
737         return SOFTBUS_INVALID_PARAM;
738     }
739     uint32_t maxLen;
740     if (SoftbusGetConfig(configType, (unsigned char *)&maxLen, sizeof(maxLen)) != SOFTBUS_OK) {
741         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get config failed, configType: %d.", configType);
742         return SOFTBUS_GET_CONFIG_VAL_ERR;
743     }
744 
745     (*(uint32_t*)value) = maxLen;
746     return SOFTBUS_OK;
747 }
748 
749 
750 static const SessionOptionItem g_SessionOptionArr[SESSION_OPTION_BUTT] = {
751     {true, ReadMaxSendBytesSize},
752     {true, ReadMaxSendMessageSize},
753 };
754 
GetSessionOption(int sessionId,SessionOption option,void * optionValue,uint32_t valueSize)755 int GetSessionOption(int sessionId, SessionOption option, void* optionValue, uint32_t valueSize)
756 {
757     if ((option >= SESSION_OPTION_BUTT) || (optionValue == NULL) || (valueSize == 0)) {
758         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetSessionOption invalid param");
759         return SOFTBUS_INVALID_PARAM;
760     }
761     if (!g_SessionOptionArr[option].canRead) {
762         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "option %d can not be get", option);
763         return SOFTBUS_INVALID_PARAM;
764     }
765 
766     int32_t channelId = INVALID_CHANNEL_ID;
767     int32_t type = CHANNEL_TYPE_BUTT;
768     int32_t ret = ClientGetChannelBySessionId(sessionId, &channelId, &type, NULL);
769     if (ret != SOFTBUS_OK) {
770         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get channel err, ret=%d.", ret);
771         return ret;
772     }
773 
774     return g_SessionOptionArr[option].readFunc(channelId, type, optionValue, valueSize);
775 }
776