• 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 "softbus_proxychannel_session.h"
17 
18 #include <securec.h>
19 
20 #include "softbus_adapter_mem.h"
21 #include "softbus_conn_interface.h"
22 #include "softbus_datahead_transform.h"
23 #include "softbus_def.h"
24 #include "softbus_error_code.h"
25 #include "softbus_property.h"
26 #include "softbus_proxychannel_callback.h"
27 #include "softbus_proxychannel_manager.h"
28 #include "softbus_proxychannel_message.h"
29 #include "softbus_proxychannel_transceiver.h"
30 #include "trans_log.h"
31 
32 #define TIME_OUT 10
33 #define USECTONSEC 1000
34 #define MSG_HEAD_LENGTH (28 + 16 + 16)
35 
36 int32_t TransProxyTransDataSendMsg(ProxyChannelInfo *chanInfo, const unsigned char *payLoad,
37     int32_t payLoadLen, ProxyPacketType flag);
38 
NotifyClientMsgReceived(const char * pkgName,int32_t pid,int32_t channelId,TransReceiveData * receiveData)39 int32_t NotifyClientMsgReceived(const char *pkgName, int32_t pid, int32_t channelId, TransReceiveData *receiveData)
40 {
41     if (pkgName == NULL) {
42         TRANS_LOGE(TRANS_MSG, "param invalid");
43         return SOFTBUS_INVALID_PARAM;
44     }
45     int32_t ret = TransProxyOnMsgReceived(pkgName, pid, channelId, receiveData);
46     if (ret != SOFTBUS_OK) {
47         TRANS_LOGE(TRANS_MSG, "notify ret=%{public}d", ret);
48     }
49     return ret;
50 }
51 
SessionTypeToPacketType(SessionPktType sessionType)52 ProxyPacketType SessionTypeToPacketType(SessionPktType sessionType)
53 {
54     switch (sessionType) {
55         case TRANS_SESSION_BYTES:
56             return PROXY_FLAG_BYTES;
57         case TRANS_SESSION_MESSAGE:
58             return PROXY_FLAG_MESSAGE;
59         case TRANS_SESSION_FILE_FIRST_FRAME:
60             return PROXY_FILE_FIRST_FRAME;
61         case TRANS_SESSION_FILE_ONGOINE_FRAME:
62             return PROXY_FILE_ONGOINE_FRAME;
63         case TRANS_SESSION_FILE_LAST_FRAME:
64             return PROXY_FILE_LAST_FRAME;
65         case TRANS_SESSION_FILE_ONLYONE_FRAME:
66             return PROXY_FILE_ONLYONE_FRAME;
67         case TRANS_SESSION_FILE_ALLFILE_SENT:
68             return PROXY_FILE_ALLFILE_SENT;
69         case TRANS_SESSION_FILE_CRC_CHECK_FRAME:
70             return PROXY_FILE_CRC_CHECK_FRAME;
71         case TRANS_SESSION_FILE_RESULT_FRAME:
72             return PROXY_FILE_RESULT_FRAME;
73         case TRANS_SESSION_FILE_ACK_REQUEST_SENT:
74             return PROXY_FILE_ACK_REQUEST_SENT;
75         case TRANS_SESSION_FILE_ACK_RESPONSE_SENT:
76             return PROXY_FILE_ACK_RESPONSE_SENT;
77         default:
78             return PROXY_FLAG_BYTES;
79     }
80 }
81 
ProxyTypeToConnPri(ProxyPacketType proxyType)82 SendPriority ProxyTypeToConnPri(ProxyPacketType proxyType)
83 {
84     switch (proxyType) {
85         case PROXY_FLAG_BYTES:
86             return CONN_MIDDLE;
87         case PROXY_FLAG_MESSAGE:
88         case PROXY_FLAG_ASYNC_MESSAGE:
89         case PROXY_FLAG_ACK:
90             return CONN_HIGH;
91         default:
92             return CONN_DEFAULT;
93     }
94 }
95 
TransProxyPostPacketData(int32_t channelId,const unsigned char * data,uint32_t len,ProxyPacketType flags)96 static int32_t TransProxyPostPacketData(int32_t channelId, const unsigned char *data,
97     uint32_t len, ProxyPacketType flags)
98 {
99     if (data == NULL || len == 0) {
100         TRANS_LOGE(TRANS_MSG, "invalid param");
101         return SOFTBUS_INVALID_PARAM;
102     }
103     ProxyChannelInfo *chanInfo = (ProxyChannelInfo *)SoftBusCalloc(sizeof(ProxyChannelInfo));
104     if (chanInfo == NULL) {
105         TRANS_LOGE(TRANS_MSG, "malloc in channelId=%{public}d", channelId);
106         return SOFTBUS_MALLOC_ERR;
107     }
108     if (TransProxyGetSendMsgChanInfo(channelId, chanInfo) != SOFTBUS_OK) {
109         SoftBusFree(chanInfo);
110         TRANS_LOGE(TRANS_MSG, "can not find proxy channel channelId=%{public}d", channelId);
111         return SOFTBUS_TRANS_PROXY_CHANNEL_NOT_FOUND;
112     }
113     (void)memset_s(chanInfo->appInfo.sessionKey, sizeof(chanInfo->appInfo.sessionKey), 0,
114         sizeof(chanInfo->appInfo.sessionKey));
115     int32_t ret = TransProxyTransDataSendMsg(chanInfo, data, len, flags);
116     if (ret != SOFTBUS_OK) {
117         TRANS_LOGE(TRANS_MSG, "send msg fail, len=%{public}u, flags=%{public}d, ret=%{public}d", len, flags, ret);
118     }
119 
120     SoftBusFree(chanInfo);
121     return ret;
122 }
123 
TransProxyPostSessionData(int32_t channelId,const unsigned char * data,uint32_t len,SessionPktType flags)124 int32_t TransProxyPostSessionData(int32_t channelId, const unsigned char *data, uint32_t len, SessionPktType flags)
125 {
126     ProxyPacketType type = SessionTypeToPacketType(flags);
127     return TransProxyPostPacketData(channelId, data, len, type);
128 }
129 
TransProxyPackAppNormalMsg(const ProxyMessageHead * msg,const char * payLoad,int32_t datalen,int32_t * outlen)130 static char *TransProxyPackAppNormalMsg(const ProxyMessageHead *msg, const char *payLoad,
131     int32_t datalen, int32_t *outlen)
132 {
133     ProxyMessageHead proxyMessageHead;
134     uint32_t connHeadLen = ConnGetHeadSize();
135     uint32_t bufLen = PROXY_CHANNEL_HEAD_LEN + connHeadLen + (uint32_t)datalen;
136 
137     char *buf = (char *)SoftBusCalloc(bufLen);
138     if (buf == NULL) {
139         TRANS_LOGE(TRANS_MSG, "buf calloc failed");
140         return NULL;
141     }
142     if (memcpy_s(&proxyMessageHead, sizeof(ProxyMessageHead), msg, sizeof(ProxyMessageHead)) != EOK) {
143         TRANS_LOGE(TRANS_MSG, "memcpy_s message failed.");
144         SoftBusFree(buf);
145         return NULL;
146     }
147     PackProxyMessageHead(&proxyMessageHead);
148     if (memcpy_s(buf + connHeadLen, bufLen - connHeadLen, &proxyMessageHead, sizeof(ProxyMessageHead)) != EOK) {
149         TRANS_LOGE(TRANS_MSG, "memcpy_s buf failed.");
150         SoftBusFree(buf);
151         return NULL;
152     }
153     uint32_t dstLen = bufLen - connHeadLen - sizeof(ProxyMessageHead);
154     if (memcpy_s(buf + connHeadLen + sizeof(ProxyMessageHead), dstLen, payLoad, datalen) != EOK) {
155         TRANS_LOGE(TRANS_MSG, "memcpy_s buf failed.");
156         SoftBusFree(buf);
157         return NULL;
158     }
159     *outlen = (int32_t)bufLen;
160 
161     return buf;
162 }
163 
TransProxyTransNormalMsg(const ProxyChannelInfo * info,const char * payLoad,int32_t payLoadLen,ProxyPacketType flag)164 static int32_t TransProxyTransNormalMsg(const ProxyChannelInfo *info, const char *payLoad, int32_t payLoadLen,
165     ProxyPacketType flag)
166 {
167     ProxyMessageHead msgHead = { 0 };
168     msgHead.type = (PROXYCHANNEL_MSG_TYPE_NORMAL & FOUR_BIT_MASK) | (VERSION << VERSION_SHIFT);
169     msgHead.myId = info->myId;
170     msgHead.peerId = info->peerId;
171     int32_t bufLen = 0;
172     char *buf = TransProxyPackAppNormalMsg(&msgHead, payLoad, payLoadLen, &bufLen);
173     if (buf == NULL) {
174         TRANS_LOGE(TRANS_MSG, "proxy pack msg error");
175         return SOFTBUS_TRANS_PROXY_PACKMSG_ERR;
176     }
177     int32_t ret = TransProxyTransSendMsg(info->connId, (uint8_t *)buf, (uint32_t)bufLen,
178         ProxyTypeToConnPri(flag), info->appInfo.myData.pid);
179     if (ret == SOFTBUS_CONNECTION_ERR_SENDQUEUE_FULL) {
180         TRANS_LOGE(TRANS_MSG, "proxy send queue full.");
181         return SOFTBUS_CONNECTION_ERR_SENDQUEUE_FULL;
182     }
183     if (ret != SOFTBUS_OK) {
184         TRANS_LOGE(TRANS_MSG, "proxy send msg error");
185         return SOFTBUS_TRANS_PROXY_SENDMSG_ERR;
186     }
187     return SOFTBUS_OK;
188 }
189 
TransProxyTransDataSendMsg(ProxyChannelInfo * info,const unsigned char * payLoad,int32_t payLoadLen,ProxyPacketType flag)190 int32_t TransProxyTransDataSendMsg(ProxyChannelInfo *info, const unsigned char *payLoad,
191     int32_t payLoadLen, ProxyPacketType flag)
192 {
193     if (info == NULL || payLoad == NULL) {
194         TRANS_LOGE(TRANS_MSG, "param invalid");
195         return SOFTBUS_INVALID_PARAM;
196     }
197     if ((info->status != PROXY_CHANNEL_STATUS_COMPLETED && info->status != PROXY_CHANNEL_STATUS_KEEPLIVEING)) {
198         TRANS_LOGE(TRANS_MSG, "status is err status=%{public}d", info->status);
199         return SOFTBUS_TRANS_PROXY_CHANNLE_STATUS_INVALID;
200     }
201     if (info->appInfo.appType == APP_TYPE_INNER) {
202         TRANS_LOGE(TRANS_MSG, "err app type Inner");
203         return SOFTBUS_TRANS_PROXY_ERROR_APP_TYPE;
204     }
205 
206     return TransProxyTransNormalMsg(info, (const char *)payLoad, payLoadLen, flag);
207 }
208 
TransOnNormalMsgReceived(const char * pkgName,int32_t pid,int32_t channelId,const char * data,uint32_t len)209 int32_t TransOnNormalMsgReceived(const char *pkgName, int32_t pid, int32_t channelId, const char *data, uint32_t len)
210 {
211     if (data == NULL || pkgName == NULL) {
212         TRANS_LOGE(TRANS_MSG, "data or pkgname is null.");
213         return SOFTBUS_INVALID_PARAM;
214     }
215     TransReceiveData receiveData;
216     receiveData.data = (void *)data;
217     receiveData.dataLen = len;
218 
219     int32_t ret = NotifyClientMsgReceived(pkgName, pid, channelId, &receiveData);
220     TRANS_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, ret,
221         TRANS_MSG, "msg receive err, channelId=%{public}d, len=%{public}u, pid=%{public}d", channelId, len, pid);
222 
223     return SOFTBUS_OK;
224 }
225