1 /*
2 * Copyright (c) 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 "soft_bus_server_socket.h"
17
18 #include "remote_connect_listener_manager.h"
19
20 #define LOG_TAG "USER_AUTH_SA"
21 namespace OHOS {
22 namespace UserIam {
23 namespace UserAuth {
ServerSocket(const int32_t socketId)24 ServerSocket::ServerSocket(const int32_t socketId)
25 : BaseSocket(socketId)
26 {
27 IAM_LOGI("server socket id is %{public}d.", socketId);
28 }
29
SendMessage(const std::string & connectionName,const std::string & srcEndPoint,const std::string & destEndPoint,const std::shared_ptr<Attributes> & attributes,MsgCallback & callback)30 ResultCode ServerSocket::SendMessage(const std::string &connectionName, const std::string &srcEndPoint,
31 const std::string &destEndPoint, const std::shared_ptr<Attributes> &attributes, MsgCallback &callback)
32 {
33 IAM_LOGI("start.");
34 int32_t socketId = GetSocketIdByClientConnectionName(connectionName);
35 if (socketId == INVALID_SOCKET_ID) {
36 IAM_LOGE("socket id is invalid");
37 return GENERAL_ERROR;
38 }
39
40 const ConnectionInfo connectionInfo = {
41 .socketId = socketId,
42 .connectionName = connectionName,
43 .srcEndPoint = srcEndPoint,
44 .destEndPoint = destEndPoint,
45 .attributes = attributes,
46 .callback = callback
47 };
48 return SendRequest(connectionInfo);
49 }
50
OnBind(int32_t socketId,PeerSocketInfo info)51 void ServerSocket::OnBind(int32_t socketId, PeerSocketInfo info)
52 {
53 IAM_LOGI("start, socket id is %{public}d", socketId);
54 if (socketId <= INVALID_SOCKET_ID) {
55 IAM_LOGE("socket id invalid.");
56 return;
57 }
58
59 std::string peerNetworkId(info.networkId);
60 AddServerSocket(socketId, peerNetworkId);
61 }
62
OnShutdown(int32_t socketId,ShutdownReason reason)63 void ServerSocket::OnShutdown(int32_t socketId, ShutdownReason reason)
64 {
65 IAM_LOGI("start, socket id is %{public}d", socketId);
66 std::string connectionName = GetClientConnectionName(socketId);
67 if (!connectionName.empty()) {
68 RemoteConnectListenerManager::GetInstance().OnConnectionDown(connectionName);
69 }
70 DeleteServerSocket(socketId);
71 DeleteClientConnection(socketId);
72 }
73
OnBytes(int32_t socketId,const void * data,uint32_t dataLen)74 void ServerSocket::OnBytes(int32_t socketId, const void *data, uint32_t dataLen)
75 {
76 IAM_LOGI("start, socket id is %{public}d", socketId);
77 std::string networkId = GetNetworkIdBySocketId(socketId);
78 if (networkId.empty()) {
79 IAM_LOGE("networkId id is null, socketId:%{public}d.", socketId);
80 return;
81 }
82
83 std::shared_ptr<SoftBusMessage> softBusMessage = ParseMessage(networkId, const_cast<void *>(data), dataLen);
84 if (softBusMessage == nullptr) {
85 IAM_LOGE("serverSocket parse message fail.");
86 return;
87 }
88
89 bool ack = softBusMessage->GetAckFlag();
90 std::string connectionName = softBusMessage->GetConnectionName();
91 if (ack == false && !connectionName.empty()) {
92 AddClientConnection(socketId, connectionName);
93 }
94
95 ResultCode ret = ProcDataReceive(socketId, softBusMessage);
96 if (ret != SUCCESS) {
97 IAM_LOGE("HandleDataReceive fail, socketId:%{public}d.", socketId);
98 return;
99 }
100 }
101
OnQos(int32_t socketId,QoSEvent eventId,const QosTV * qos,uint32_t qosCount)102 void ServerSocket::OnQos(int32_t socketId, QoSEvent eventId, const QosTV *qos, uint32_t qosCount)
103 {
104 IAM_LOGI("start, socket id is %{public}d", socketId);
105 }
106
AddServerSocket(const int32_t socketId,const std::string & networkId)107 void ServerSocket::AddServerSocket(const int32_t socketId, const std::string &networkId)
108 {
109 IAM_LOGI("start, socketId %{public}d.", socketId);
110 IF_FALSE_LOGE_AND_RETURN(socketId != INVALID_SOCKET_ID);
111
112 std::lock_guard<std::recursive_mutex> lock(socketMutex_);
113 auto iter = serverSocketBindMap_.find(socketId);
114 if (iter == serverSocketBindMap_.end()) {
115 serverSocketBindMap_.insert(std::pair<int32_t, std::string>(socketId, networkId));
116 } else {
117 iter->second = networkId;
118 }
119 }
120
DeleteServerSocket(const int32_t socketId)121 void ServerSocket::DeleteServerSocket(const int32_t socketId)
122 {
123 IAM_LOGI("start, socketId %{public}d.", socketId);
124 IF_FALSE_LOGE_AND_RETURN(socketId != INVALID_SOCKET_ID);
125
126 std::lock_guard<std::recursive_mutex> lock(socketMutex_);
127 auto iter = serverSocketBindMap_.find(socketId);
128 if (iter != serverSocketBindMap_.end()) {
129 serverSocketBindMap_.erase(iter);
130 }
131 }
132
GetNetworkIdBySocketId(int32_t socketId)133 std::string ServerSocket::GetNetworkIdBySocketId(int32_t socketId)
134 {
135 IF_FALSE_LOGE_AND_RETURN_VAL(socketId != INVALID_SOCKET_ID, "");
136
137 std::lock_guard<std::recursive_mutex> lock(socketMutex_);
138 std::string networkId;
139 auto iter = serverSocketBindMap_.find(socketId);
140 if (iter != serverSocketBindMap_.end()) {
141 networkId = iter->second;
142 }
143 return networkId;
144 }
145
AddClientConnection(const int32_t socketId,const std::string & connectionName)146 void ServerSocket::AddClientConnection(const int32_t socketId, const std::string &connectionName)
147 {
148 IAM_LOGI("add socketId %{public}d connectionName %{public}s.", socketId, connectionName.c_str());
149 IF_FALSE_LOGE_AND_RETURN(socketId != INVALID_SOCKET_ID);
150
151 std::lock_guard<std::recursive_mutex> lock(connectionMutex_);
152 auto iter = clientConnectionMap_.find(socketId);
153 if (iter == clientConnectionMap_.end()) {
154 clientConnectionMap_.insert(std::pair<int32_t, std::string>(socketId, connectionName));
155 }
156 }
157
DeleteClientConnection(const int32_t socketId)158 void ServerSocket::DeleteClientConnection(const int32_t socketId)
159 {
160 IAM_LOGI("start, socketId %{public}d.", socketId);
161 IF_FALSE_LOGE_AND_RETURN(socketId != INVALID_SOCKET_ID);
162
163 std::lock_guard<std::recursive_mutex> lock(connectionMutex_);
164 auto iter = clientConnectionMap_.find(socketId);
165 if (iter != clientConnectionMap_.end()) {
166 std::string connectionName = iter->second;
167 IAM_LOGI("delete socketId %{public}d connectionName %{public}s.", socketId, connectionName.c_str());
168 clientConnectionMap_.erase(iter);
169 }
170 }
171
GetClientConnectionName(const int32_t socketId)172 std::string ServerSocket::GetClientConnectionName(const int32_t socketId)
173 {
174 IF_FALSE_LOGE_AND_RETURN_VAL(socketId != INVALID_SOCKET_ID, "");
175
176 std::lock_guard<std::recursive_mutex> lock(connectionMutex_);
177 std::string ConnectionName;
178 auto iter = clientConnectionMap_.find(socketId);
179 if (iter != clientConnectionMap_.end()) {
180 ConnectionName = iter->second;
181 }
182 return ConnectionName;
183 }
184
GetSocketIdByClientConnectionName(const std::string & connectionName)185 int32_t ServerSocket::GetSocketIdByClientConnectionName(const std::string &connectionName)
186 {
187 std::lock_guard<std::recursive_mutex> lock(connectionMutex_);
188 int32_t socketId = INVALID_SOCKET_ID;
189 for (const auto &iter : clientConnectionMap_) {
190 if (iter.second == connectionName) {
191 socketId = iter.first;
192 break;
193 }
194 }
195
196 return socketId;
197 }
198
GetConnectionName()199 std::string ServerSocket::GetConnectionName()
200 {
201 return "";
202 }
203
GetNetworkId()204 std::string ServerSocket::GetNetworkId()
205 {
206 return "";
207 }
208 } // namespace UserAuth
209 } // namespace UserIam
210 } // namespace OHOS