• 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_proxy_manager.h"
17 
18 #include <limits.h>
19 #include <securec.h>
20 #include <unistd.h>
21 
22 #include "client_trans_pending.h"
23 #include "client_trans_proxy_file_common.h"
24 #include "client_trans_proxy_file_manager.h"
25 #include "client_trans_session_manager.h"
26 #include "client_trans_tcp_direct_message.h"
27 #include "softbus_adapter_errcode.h"
28 #include "softbus_adapter_file.h"
29 #include "softbus_adapter_mem.h"
30 #include "softbus_adapter_timer.h"
31 #include "softbus_app_info.h"
32 #include "softbus_conn_interface.h"
33 #include "softbus_errcode.h"
34 #include "softbus_feature_config.h"
35 #include "softbus_log.h"
36 #include "softbus_utils.h"
37 #include "trans_server_proxy.h"
38 
39 static IClientSessionCallBack g_sessionCb;
40 static uint32_t g_authMaxByteBufSize;
41 static uint32_t g_authMaxMessageBufSize;
42 
ClinetTransProxyInit(const IClientSessionCallBack * cb)43 int32_t ClinetTransProxyInit(const IClientSessionCallBack *cb)
44 {
45     if (cb == NULL) {
46         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ClinetTransProxyInit cb param is null!");
47         return SOFTBUS_INVALID_PARAM;
48     }
49 
50     g_sessionCb = *cb;
51     if (ClinetTransProxyFileManagerInit() != SOFTBUS_OK) {
52         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ClinetTransProxyFileManagerInit init fail!");
53         return SOFTBUS_ERR;
54     }
55 
56     if (SoftbusGetConfig(SOFTBUS_INT_AUTH_MAX_BYTES_LENGTH,
57         (unsigned char*)&g_authMaxByteBufSize, sizeof(g_authMaxByteBufSize)) != SOFTBUS_OK) {
58         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get auth proxy channel max bytes length fail");
59     }
60     if (SoftbusGetConfig(SOFTBUS_INT_AUTH_MAX_MESSAGE_LENGTH,
61         (unsigned char*)&g_authMaxMessageBufSize, sizeof(g_authMaxMessageBufSize)) != SOFTBUS_OK) {
62         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "get auth proxy channel max message length fail");
63     }
64     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "proxy auth byteSize[%u], messageSize[%u]",
65         g_authMaxByteBufSize, g_authMaxMessageBufSize);
66     return SOFTBUS_OK;
67 }
68 
ClientTransProxyDeinit(void)69 void ClientTransProxyDeinit(void)
70 {
71     ClinetTransProxyFileManagerDeinit();
72 }
73 
ClientTransProxyOnChannelOpened(const char * sessionName,const ChannelInfo * channel)74 int32_t ClientTransProxyOnChannelOpened(const char *sessionName, const ChannelInfo *channel)
75 {
76     if (sessionName == NULL || channel == NULL) {
77         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ClientTransProxyOnChannelOpened invalid param.");
78         return SOFTBUS_INVALID_PARAM;
79     }
80 
81     int ret = g_sessionCb.OnSessionOpened(sessionName, channel, TYPE_MESSAGE);
82     if (ret != SOFTBUS_OK) {
83         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "notify session open fail, sessionName=[%s].", sessionName);
84         return ret;
85     }
86 
87     return SOFTBUS_OK;
88 }
89 
ClientTransProxyOnChannelClosed(int32_t channelId)90 int32_t ClientTransProxyOnChannelClosed(int32_t channelId)
91 {
92     int ret = g_sessionCb.OnSessionClosed(channelId, CHANNEL_TYPE_PROXY);
93     if (ret != SOFTBUS_OK) {
94         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "notify session closed err[%d], cId[%d].", ret, channelId);
95         return ret;
96     }
97     return SOFTBUS_OK;
98 }
99 
ClientTransProxyOnChannelOpenFailed(int32_t channelId,int32_t errCode)100 int32_t ClientTransProxyOnChannelOpenFailed(int32_t channelId, int32_t errCode)
101 {
102     int ret = g_sessionCb.OnSessionOpenFailed(channelId, CHANNEL_TYPE_PROXY, errCode);
103     if (ret != SOFTBUS_OK) {
104         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
105             "notify session openfail err[%d], cId[%d].", errCode, channelId);
106         return ret;
107     }
108 
109     return SOFTBUS_OK;
110 }
111 
ClientTransProxyOnDataReceived(int32_t channelId,const void * data,uint32_t len,SessionPktType type)112 int32_t ClientTransProxyOnDataReceived(int32_t channelId,
113     const void *data, uint32_t len, SessionPktType type)
114 {
115     if (data == NULL) {
116         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR,
117             "ClientTransProxyOnDataReceived cId[%d] data null.", channelId);
118         return SOFTBUS_INVALID_PARAM;
119     }
120     int ret = g_sessionCb.OnDataReceived(channelId, CHANNEL_TYPE_PROXY, data, len, type);
121     if (ret != SOFTBUS_OK) {
122         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "notify data recv err, cId[%d].", channelId);
123         return ret;
124     }
125     return SOFTBUS_OK;
126 }
127 
ClientTransProxyCloseChannel(int32_t channelId)128 void ClientTransProxyCloseChannel(int32_t channelId)
129 {
130     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransCloseProxyChannel, channelId [%d]", channelId);
131     if (ServerIpcCloseChannel(channelId, CHANNEL_TYPE_PROXY) != SOFTBUS_OK) {
132         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "server close channel[%d] err.", channelId);
133     }
134 }
135 
TransProxyChannelSendBytes(int32_t channelId,const void * data,uint32_t len)136 int32_t TransProxyChannelSendBytes(int32_t channelId, const void *data, uint32_t len)
137 {
138     int ret = ServerIpcSendMessage(channelId, CHANNEL_TYPE_PROXY, data, len, TRANS_SESSION_BYTES);
139     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "send bytes: channelId=%d, ret=%d", channelId, ret);
140     return ret;
141 }
142 
TransProxyChannelSendMessage(int32_t channelId,const void * data,uint32_t len)143 int32_t TransProxyChannelSendMessage(int32_t channelId, const void *data, uint32_t len)
144 {
145     int ret = ServerIpcSendMessage(channelId, CHANNEL_TYPE_PROXY, data, len, TRANS_SESSION_MESSAGE);
146     SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "send msg: channelId=%d, ret=%d", channelId, ret);
147     return ret;
148 }
149 
ProcessFileFrameData(int32_t sessionId,int32_t channelId,const char * data,uint32_t len,int32_t type)150 int32_t ProcessFileFrameData(int32_t sessionId, int32_t channelId, const char *data, uint32_t len, int32_t type)
151 {
152     FileFrame oneFrame;
153     oneFrame.frameType = type;
154     oneFrame.frameLength = len;
155     oneFrame.data = (uint8_t *)data;
156     return ProcessRecvFileFrameData(sessionId, channelId, &oneFrame);
157 }
158 
GenerateRemoteFiles(const char * sFileList[],uint32_t fileCnt)159 static const char **GenerateRemoteFiles(const char *sFileList[], uint32_t fileCnt)
160 {
161     const char **files = SoftBusCalloc(sizeof(const char *) * fileCnt);
162     if (files == NULL) {
163         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "%s:oom", __func__);
164         return NULL;
165     }
166     for (uint32_t i = 0; i < fileCnt; i++) {
167         files[i] = TransGetFileName(sFileList[i]);
168         if (files[i] == NULL) {
169             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "GetFileName failed at index %" PRIu32, i);
170             SoftBusFree(files);
171             return NULL;
172         }
173     }
174     return files;
175 }
176 
TransProxyChannelSendFile(int32_t channelId,const char * sFileList[],const char * dFileList[],uint32_t fileCnt)177 int32_t TransProxyChannelSendFile(int32_t channelId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt)
178 {
179     if (sFileList == NULL || fileCnt == 0 || fileCnt > MAX_SEND_FILE_NUM) {
180         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "%s:input para failed!fileCount=%" PRIu32, __func__, fileCnt);
181         return SOFTBUS_INVALID_PARAM;
182     }
183     const char **remoteFiles = NULL;
184     const char **generatedRemoteFiles = NULL;
185     if (dFileList == NULL) {
186         generatedRemoteFiles = GenerateRemoteFiles(sFileList, fileCnt);
187         if (generatedRemoteFiles == NULL) {
188             return SOFTBUS_ERR;
189         }
190         remoteFiles = generatedRemoteFiles;
191     } else {
192         remoteFiles = dFileList;
193     }
194     int32_t ret = ProxyChannelSendFile(channelId, sFileList, remoteFiles, fileCnt);
195     if (generatedRemoteFiles != NULL) {
196         SoftBusFree(generatedRemoteFiles);
197         generatedRemoteFiles = NULL;
198     }
199     return ret;
200 }