• 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 "softbus_session.h"
17 #include "softbus_adapter.h"
18 #include "anonymous_string.h"
19 #include "device_manager_log.h"
20 #include "device_manager_errno.h"
21 #include "auth_manager.h"
22 #include "encrypt_utils.h"
23 #include "constants.h"
24 #include "session.h"
25 #include "inner_session.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
SessionOpened(int sessionId,int result)29 static int SessionOpened(int sessionId, int result)
30 {
31     return SoftbusSession::GetInstance().OnSessionOpened(sessionId, result);
32 }
33 
SessionClosed(int sessionId)34 static void SessionClosed(int sessionId)
35 {
36     SoftbusSession::GetInstance().OnSessionClosed(sessionId);
37 }
38 
BytesReceived(int sessionId,const void * data,unsigned int dataLen)39 static void BytesReceived(int sessionId, const void *data, unsigned int dataLen)
40 {
41     SoftbusSession::GetInstance().GetInstance().OnBytesReceived(sessionId, data, dataLen);
42 }
43 
MessageReceived(int sessionId,const void * data,unsigned int dataLen)44 static void MessageReceived(int sessionId, const void *data, unsigned int dataLen)
45 {
46     (void)sessionId;
47     (void)data;
48     (void)dataLen;
49     DMLOG(DM_LOG_INFO, "sessionId:%d, dataLen:%d", sessionId, dataLen);
50 }
51 
StreamReceived(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)52 static void StreamReceived(int sessionId, const StreamData *data, const StreamData *ext, const StreamFrameInfo *param)
53 {
54     (void)sessionId;
55     (void)data;
56     (void)ext;
57     (void)param;
58     DMLOG(DM_LOG_INFO, "sessionId:%d", sessionId);
59 }
60 
61 IMPLEMENT_SINGLE_INSTANCE(SoftbusSession);
62 
~SoftbusSession()63 SoftbusSession::~SoftbusSession()
64 {
65     (void)RemoveSessionServer(PKG_NAME, SESSION_NAME);
66 }
67 
Start()68 int32_t SoftbusSession::Start()
69 {
70     DMLOG(DM_LOG_INFO, "SoftbusSession start");
71     ISessionListener iSessionListener = {
72         .OnSessionOpened = SessionOpened,
73         .OnSessionClosed = SessionClosed,
74         .OnBytesReceived = BytesReceived,
75         .OnMessageReceived = MessageReceived,
76         .OnStreamReceived = StreamReceived
77     };
78     int32_t ret = CreateSessionServer(PKG_NAME, SESSION_NAME, &iSessionListener);
79     if (ret != DEVICEMANAGER_OK) {
80         DMLOG(DM_LOG_DEBUG, "CreateSessionServer failed");
81         return DEVICEMANAGER_CREATE_SESSION_SERVER_FAILED;
82     }
83     return DEVICEMANAGER_OK;
84 }
85 
OnSessionOpened(int sessionId,int result)86 int SoftbusSession::OnSessionOpened(int sessionId, int result)
87 {
88     if (result != DEVICEMANAGER_OK) {
89         DMLOG(DM_LOG_INFO, "session open failed, sessionId %d", sessionId);
90         if (sessionIdSet_.count(sessionId) > 0) {
91             sessionIdSet_.erase(sessionId);
92             messages_.clear();
93         }
94         return DEVICEMANAGER_OK;
95     }
96 
97     int32_t sessionSide = GetSessionSide(sessionId);
98     DMLOG(DM_LOG_INFO, "session open succeed, sessionId %d, sessionSide %d", sessionId, sessionSide);
99     if (sessionSide == AUTH_SESSION_SIDE_CLIENT) {
100         for (auto msg : messages_) {
101             int32_t ret = SendMsg(sessionId, msg);
102             if (ret != DEVICEMANAGER_OK) {
103                 DMLOG(DM_LOG_INFO, "send message failed");
104                 return ret;
105             }
106         }
107     } else {
108         sessionIdSet_.insert(sessionId);
109     }
110     return DEVICEMANAGER_OK;
111 }
112 
OnSessionClosed(int sessionId)113 void SoftbusSession::OnSessionClosed(int sessionId)
114 {
115     DMLOG(DM_LOG_INFO, "OnSessionClosed, sessionId:%d", sessionId);
116     if (sessionIdSet_.count(sessionId) > 0) {
117         sessionIdSet_.erase(sessionId);
118     }
119 }
120 
OnBytesReceived(int sessionId,const void * data,int dataLen)121 void SoftbusSession::OnBytesReceived(int sessionId, const void *data, int dataLen)
122 {
123     DMLOG(DM_LOG_INFO, "OnBytesReceived, sessionId:%d, dataLen:%d", sessionId, dataLen);
124     if (sessionId < 0 || data == nullptr || dataLen <= 0) {
125         DMLOG(DM_LOG_INFO, "OnBytesReceived param check failed");
126         return;
127     }
128 
129     uint8_t *buf = (uint8_t *)calloc(sizeof(uint8_t), dataLen + 1);
130     if (buf == nullptr) {
131         DMLOG(DM_LOG_ERROR, "SendMsg: malloc memory failed");
132         return;
133     }
134 
135     int32_t outLen = 0;
136     int32_t ret = EncryptUtils::MbedtlsDecrypt((const uint8_t*)data, dataLen, buf, dataLen, &outLen);
137     if (ret != DEVICEMANAGER_OK || outLen > (int32_t)dataLen) {
138         DMLOG(DM_LOG_ERROR, "MbedtlsDecrypt data failed");
139         free(buf);
140         return;
141     }
142 
143     std::string message = (char *)buf;
144     AuthManager::GetInstance().OnReceiveMsg(sessionId, message);
145     free(buf);
146     DMLOG(DM_LOG_INFO, "OnBytesReceived completed");
147     return;
148 }
149 
CloseSession(int32_t sessionId)150 void SoftbusSession::CloseSession(int32_t sessionId)
151 {
152     DMLOG(DM_LOG_INFO, "CloseSession in");
153     ::CloseSession(sessionId);
154 }
155 
SendData(int32_t sessionId,const void * data,int32_t len)156 int32_t SoftbusSession::SendData(int32_t sessionId, const void *data, int32_t len)
157 {
158     DMLOG(DM_LOG_INFO, "in, datalen:%d", len);
159     int32_t ret = DEVICEMANAGER_FAILED;
160     if (sessionIdSet_.count(sessionId) > 0) {
161         ret = SendBytes(sessionId, data, len);
162         if (ret != DEVICEMANAGER_OK) {
163             return DEVICEMANAGER_FAILED;
164         }
165     } else {
166         DMLOG(DM_LOG_INFO, "in, datalen:%d", len);
167     }
168     return ret;
169 }
170 
171 // send message by sessionId (channel opened)
SendMsg(int32_t sessionId,std::string & message)172 int32_t SoftbusSession::SendMsg(int32_t sessionId, std::string &message)
173 {
174     DMLOG(DM_LOG_ERROR, "start SendMsg");
175     uint8_t *buf = (uint8_t *)calloc(sizeof(uint8_t), (MSG_MAX_SIZE + ENCRYPT_TAG_LEN));
176     if (buf == nullptr) {
177         DMLOG(DM_LOG_ERROR, "SendMsg: malloc memory failed");
178         return DEVICEMANAGER_MALLOC_ERROR;
179     }
180     int32_t outLen = 0;
181     int32_t ret = EncryptUtils::MbedtlsEncrypt((const uint8_t *)message.c_str(), message.size(), buf, MSG_MAX_SIZE,
182         &outLen);
183     if (ret != DEVICEMANAGER_OK || outLen > MSG_MAX_SIZE) {
184         DMLOG(DM_LOG_ERROR, "MbedtlsEncrypt data failed");
185         free(buf);
186         return ENCRYPT_UTILS_AES_GCM_ENCRYPT_FAILED;
187     }
188     ret = SendData(sessionId, buf, outLen);
189     free(buf);
190     return ret;
191 }
192 
193 // send message while the channel is not opend
SendMessages(const char * deviceId,std::vector<std::string> & message)194 int32_t SoftbusSession::SendMessages(const char *deviceId, std::vector<std::string> &message)
195 {
196     DMLOG(DM_LOG_ERROR, "open channel and start SendMsg");
197     int32_t sessionId = -1;
198     messages_ = message;
199     ConnectionAddr *addrInfo = SoftbusAdapter::GetConnectAddr(deviceId);
200     if (addrInfo == nullptr) {
201         DMLOG(DM_LOG_ERROR, "GetConnectAddr error");
202         return sessionId;
203     }
204 
205     sessionId = ::OpenAuthSession(SESSION_NAME, addrInfo, 1, nullptr);
206     if (sessionId < 0) {
207         DMLOG(DM_LOG_ERROR, "open session error, ret:%d", sessionId);
208         return sessionId;
209     }
210     sessionIdSet_.insert(sessionId);
211     DMLOG(DM_LOG_INFO, "opened auth session is:%d", sessionId);
212     return sessionId;
213 }
214 
GetPeerDeviceId(int32_t sessionId,std::string & peerDevId)215 void SoftbusSession::GetPeerDeviceId(int32_t sessionId, std::string &peerDevId)
216 {
217     char peerDeviceId[DEVICE_UUID_LENGTH] = {0};
218     int32_t ret = ::GetPeerDeviceId(sessionId, &peerDeviceId[0], DEVICE_UUID_LENGTH);
219     if (ret == 0) {
220         peerDevId = peerDeviceId;
221         DMLOG(DM_LOG_INFO, "GetPeerDeviceId success for session:%d, peerDeviceId:%s", sessionId,
222             GetAnonyString(peerDevId).c_str());
223         return;
224     }
225 
226     DMLOG(DM_LOG_ERROR, "GetPeerDeviceId failed for session:%d", sessionId);
227     peerDevId = "";
228 }
229 } // namespace DistributedHardware
230 } // namespace OHOS
231