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