diff --git a/sdk/transmission/session/include/client_trans_session_manager.h b/sdk/transmission/session/include/client_trans_session_manager.h index 80d22b2d56..20cc69607e 100644 --- a/sdk/transmission/session/include/client_trans_session_manager.h +++ b/sdk/transmission/session/include/client_trans_session_manager.h @@ -172,6 +172,8 @@ int32_t ClientGetSessionCallbackAdapterByName(const char *sessionName, SessionLi int32_t ClientGetSessionCallbackAdapterById(int32_t sessionId, SessionListenerAdapter *callbackAdapter); int32_t ClientGetPeerSocketInfoById(int32_t sessionId, PeerSocketInfo *peerSocketInfo); + +bool IsSessionExceedLimit(); #ifdef __cplusplus } #endif diff --git a/sdk/transmission/session/src/client_trans_session_manager.c b/sdk/transmission/session/src/client_trans_session_manager.c index 01f73b00ba..4a206285b5 100644 --- a/sdk/transmission/session/src/client_trans_session_manager.c +++ b/sdk/transmission/session/src/client_trans_session_manager.c @@ -1913,4 +1913,19 @@ int32_t ClientGetPeerSocketInfoById(int32_t sessionId, PeerSocketInfo *peerSocke peerSocketInfo->dataType = (TransDataType)sessionNode->info.flag; (void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock)); return SOFTBUS_OK; +} + +bool IsSessionExceedLimit() +{ + if (SoftBusMutexLock(&(g_clientSessionServerList->lock)) != 0) { + TRANS_LOGE(TRANS_SDK, "lock failed"); + return true; + } + if (g_sessionIdNum >= MAX_SESSION_ID) { + (void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock)); + TRANS_LOGE(TRANS_SDK, "sessionId num exceed limit."); + return true; + } + (void)SoftBusMutexUnlock(&(g_clientSessionServerList->lock)); + return false; } \ No newline at end of file diff --git a/sdk/transmission/session/src/client_trans_socket_service.c b/sdk/transmission/session/src/client_trans_socket_service.c index fa471c98d9..4cd8191d88 100644 --- a/sdk/transmission/session/src/client_trans_socket_service.c +++ b/sdk/transmission/session/src/client_trans_socket_service.c @@ -16,8 +16,9 @@ #include "string.h" #include "anonymizer.h" #include "client_trans_session_adapter.h" -#include "socket.h" +#include "client_trans_session_manager.h" #include "inner_socket.h" +#include "socket.h" #include "softbus_adapter_mem.h" #include "softbus_def.h" #include "softbus_error_code.h" @@ -102,6 +103,9 @@ int32_t Listen(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISock int32_t Bind(int32_t socket, const QosTV qos[], uint32_t qosCount, const ISocketListener *listener) { TRANS_LOGI(TRANS_SDK, "Bind: socket=%d", socket); + if (IsSessionExceedLimit()) { + return SOFTBUS_TRANS_SESSION_CNT_EXCEEDS_LIMIT; + } return ClientBind(socket, qos, qosCount, listener); } diff --git a/sdk/transmission/trans_channel/manager/src/client_trans_channel_callback.c b/sdk/transmission/trans_channel/manager/src/client_trans_channel_callback.c index 062f2f16c9..d82ae53ebc 100644 --- a/sdk/transmission/trans_channel/manager/src/client_trans_channel_callback.c +++ b/sdk/transmission/trans_channel/manager/src/client_trans_channel_callback.c @@ -31,6 +31,9 @@ int32_t TransOnChannelOpened(const char *sessionName, const ChannelInfo *channel TRANS_LOGW(TRANS_SDK, "[client] invalid param."); return SOFTBUS_INVALID_PARAM; } + if (IsSessionExceedLimit()) { + return SOFTBUS_TRANS_SESSION_CNT_EXCEEDS_LIMIT; + } int32_t ret = SOFTBUS_ERR; int32_t udpPort = 0; diff --git a/sdk/transmission/trans_channel/udp/common/include/client_trans_udp_manager.h b/sdk/transmission/trans_channel/udp/common/include/client_trans_udp_manager.h index 79914fb30b..a2760eb0e6 100644 --- a/sdk/transmission/trans_channel/udp/common/include/client_trans_udp_manager.h +++ b/sdk/transmission/trans_channel/udp/common/include/client_trans_udp_manager.h @@ -31,7 +31,7 @@ typedef struct { const StreamFrameInfo *param); int32_t (*OnFileGetSessionId)(int32_t channelId, int32_t *sessionId); void (*OnMessageReceived)(void); - void (*OnUdpChannelOpened)(int32_t channelId); + int32_t (*OnUdpChannelOpened)(int32_t channelId); void (*OnUdpChannelClosed)(int32_t channelId, ShutdownReason reason); void (*OnQosEvent)(int channelId, int eventId, int tvCount, const QosTv *tvList); } UdpChannelMgrCb; diff --git a/sdk/transmission/trans_channel/udp/common/src/client_trans_udp_manager.c b/sdk/transmission/trans_channel/udp/common/src/client_trans_udp_manager.c index a4ff15642d..47b3d9982e 100644 --- a/sdk/transmission/trans_channel/udp/common/src/client_trans_udp_manager.c +++ b/sdk/transmission/trans_channel/udp/common/src/client_trans_udp_manager.c @@ -148,20 +148,22 @@ static int32_t TransSetUdpChannelEnable(int32_t channelId, bool isEnable) return SOFTBUS_ERR; } -static void OnUdpChannelOpened(int32_t channelId) +static int32_t OnUdpChannelOpened(int32_t channelId) { UdpChannel channel; if (memset_s(&channel, sizeof(UdpChannel), 0, sizeof(UdpChannel)) != EOK) { TRANS_LOGE(TRANS_SDK, "on udp channel opened memset failed."); - return; + return SOFTBUS_ERR; } - if (TransGetUdpChannel(channelId, &channel) != SOFTBUS_OK) { + int ret = TransGetUdpChannel(channelId, &channel); + if (ret != SOFTBUS_OK) { TRANS_LOGE(TRANS_SDK, "get udp channelId=%d failed.", channelId); - return; + return ret; } - if (TransSetUdpChannelEnable(channelId, true) != SOFTBUS_OK) { + ret = TransSetUdpChannelEnable(channelId, true); + if (ret != SOFTBUS_OK) { TRANS_LOGE(TRANS_SDK, "set udp channelId=%d enable failed.", channelId); - return; + return ret; } SessionType type = TYPE_BUTT; switch (channel.businessType) { @@ -173,7 +175,7 @@ static void OnUdpChannelOpened(int32_t channelId) break; default: TRANS_LOGE(TRANS_SDK, "unsupport businessType=%d.", channel.businessType); - return; + return SOFTBUS_ERR; } ChannelInfo info = {0}; info.channelId = channel.channelId; @@ -187,8 +189,9 @@ static void OnUdpChannelOpened(int32_t channelId) info.routeType = channel.routeType; info.businessType = channel.businessType; if ((g_sessionCb != NULL) && (g_sessionCb->OnSessionOpened != NULL)) { - g_sessionCb->OnSessionOpened(channel.info.mySessionName, &info, type); + return g_sessionCb->OnSessionOpened(channel.info.mySessionName, &info, type); } + return SOFTBUS_ERR; } static UdpChannel *ConvertChannelInfoToUdpChannel(const char *sessionName, const ChannelInfo *channel) diff --git a/sdk/transmission/trans_channel/udp/file/src/client_trans_file.c b/sdk/transmission/trans_channel/udp/file/src/client_trans_file.c index 658188450b..311d9f746c 100644 --- a/sdk/transmission/trans_channel/udp/file/src/client_trans_file.c +++ b/sdk/transmission/trans_channel/udp/file/src/client_trans_file.c @@ -314,9 +314,16 @@ int32_t TransOnFileChannelOpened(const char *sessionName, const ChannelInfo *cha TRANS_LOGE(TRANS_FILE, "start file channel as server failed"); return SOFTBUS_ERR; } - g_udpChannelMgrCb->OnUdpChannelOpened(channel->channelId); + if (g_udpChannelMgrCb->OnUdpChannelOpened(channel->channelId) != SOFTBUS_OK) { + TRANS_LOGE(TRANS_FILE, "udp channel open failed."); + NSTACKX_DFileClose(fileSession); + *filePort = 0; + return SOFTBUS_ERR; + } if (UpdateFileRecvPath(channel->channelId, &fileListener, fileSession)) { TRANS_LOGE(TRANS_FILE, "update receive file path failed"); + NSTACKX_DFileClose(fileSession); + *filePort = 0; return SOFTBUS_ERR; } } else {