1 /*
2 * Copyright (c) 2023-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_socket_listener.h"
17
18 #include "accesstoken_common_log.h"
19 #include "constant.h"
20 #include "remote_command_manager.h"
21 #include "socket.h"
22 #include "soft_bus_manager.h"
23
24 namespace OHOS {
25 namespace Security {
26 namespace AccessToken {
27 namespace {
28 static const int32_t MAX_ONBYTES_RECEIVED_DATA_LEN = 1024 * 1024 * 10;
29 } // namespace
30
31 std::mutex SoftBusSocketListener::socketMutex_;
32 std::map<int32_t, std::string> SoftBusSocketListener::socketBindMap_;
33
OnBind(int32_t socket,PeerSocketInfo info)34 void SoftBusSocketListener::OnBind(int32_t socket, PeerSocketInfo info)
35 {
36 LOGI(ATM_DOMAIN, ATM_TAG, "Socket fd is %{public}d.", socket);
37
38 if (socket <= Constant::INVALID_SOCKET_FD) {
39 LOGE(ATM_DOMAIN, ATM_TAG, "Socket fd invalid.");
40 return;
41 }
42
43 std::string peerNetworkId(info.networkId);
44 std::lock_guard<std::mutex> guard(socketMutex_);
45 auto iter = socketBindMap_.find(socket);
46 if (iter == socketBindMap_.end()) {
47 socketBindMap_.insert(std::pair<int32_t, std::string>(socket, peerNetworkId));
48 } else {
49 iter->second = peerNetworkId;
50 }
51 }
52
OnShutdown(int32_t socket,ShutdownReason reason)53 void SoftBusSocketListener::OnShutdown(int32_t socket, ShutdownReason reason)
54 {
55 LOGI(ATM_DOMAIN, ATM_TAG, "Socket fd %{public}d shutdown because %{public}u.", socket, reason);
56
57 if (socket <= Constant::INVALID_SOCKET_FD) {
58 LOGE(ATM_DOMAIN, ATM_TAG, "Socket fd invalid.");
59 return;
60 }
61
62 // clear sessionId state
63 std::lock_guard<std::mutex> guard(socketMutex_);
64 auto iter = socketBindMap_.find(socket);
65 if (iter != socketBindMap_.end()) {
66 socketBindMap_.erase(iter);
67 }
68 }
69
GetNetworkIdBySocket(const int32_t socket,std::string & networkId)70 bool SoftBusSocketListener::GetNetworkIdBySocket(const int32_t socket, std::string& networkId)
71 {
72 if (socket <= Constant::INVALID_SOCKET_FD) {
73 LOGE(ATM_DOMAIN, ATM_TAG, "Socket fd invalid.");
74 return false;
75 }
76
77 std::lock_guard<std::mutex> guard(socketMutex_);
78 auto iter = socketBindMap_.find(socket);
79 if (iter != socketBindMap_.end()) {
80 networkId = iter->second;
81 return true;
82 }
83 return false;
84 }
85
OnClientBytes(int32_t socket,const void * data,uint32_t dataLen)86 void SoftBusSocketListener::OnClientBytes(int32_t socket, const void *data, uint32_t dataLen)
87 {
88 LOGI(ATM_DOMAIN, ATM_TAG, "Socket fd %{public}d, recv len %{public}d.", socket, dataLen);
89
90 if ((socket <= Constant::INVALID_SOCKET_FD) || (data == nullptr) ||
91 (dataLen == 0) || (dataLen > MAX_ONBYTES_RECEIVED_DATA_LEN)) {
92 LOGE(ATM_DOMAIN, ATM_TAG, "Params invalid.");
93 return;
94 }
95
96 std::string networkId;
97 if (!GetNetworkIdBySocket(socket, networkId)) {
98 LOGE(ATM_DOMAIN, ATM_TAG, "Socket invalid, bind service first.");
99 return;
100 }
101
102 // channel create in SoftBusDeviceConnectionListener::OnDeviceOnline->RemoteCommandManager::NotifyDeviceOnline
103 auto channel = RemoteCommandManager::GetInstance().GetExecutorChannel(networkId);
104 if (channel == nullptr) {
105 LOGE(ATM_DOMAIN, ATM_TAG, "GetExecutorChannel failed");
106 return;
107 }
108 channel->HandleDataReceived(socket, static_cast<unsigned char *>(const_cast<void *>(data)), dataLen);
109 }
110
OnServiceBytes(int32_t socket,const void * data,uint32_t dataLen)111 void SoftBusSocketListener::OnServiceBytes(int32_t socket, const void *data, uint32_t dataLen)
112 {
113 LOGI(ATM_DOMAIN, ATM_TAG, "Socket fd %{public}d, recv len %{public}d.", socket, dataLen);
114
115 if ((socket <= Constant::INVALID_SOCKET_FD) || (data == nullptr) ||
116 (dataLen == 0) || (dataLen > MAX_ONBYTES_RECEIVED_DATA_LEN)) {
117 LOGE(ATM_DOMAIN, ATM_TAG, "Params invalid.");
118 return;
119 }
120
121 std::string networkId;
122 if (SoftBusManager::GetInstance().GetNetworkIdBySocket(socket, networkId)) {
123 // channel create in SoftBusDeviceConnectionListener::OnDeviceOnline->RemoteCommandManager::NotifyDeviceOnline
124 auto channel = RemoteCommandManager::GetInstance().GetExecutorChannel(networkId);
125 if (channel == nullptr) {
126 LOGE(ATM_DOMAIN, ATM_TAG, "GetExecutorChannel failed");
127 return;
128 }
129 channel->HandleDataReceived(socket, static_cast<unsigned char *>(const_cast<void *>(data)), dataLen);
130 } else {
131 LOGE(ATM_DOMAIN, ATM_TAG, "Unkonow socket.");
132 }
133 }
134
CleanUpAllBindSocket()135 void SoftBusSocketListener::CleanUpAllBindSocket()
136 {
137 std::lock_guard<std::mutex> guard(socketMutex_);
138 for (auto it = socketBindMap_.begin(); it != socketBindMap_.end();) {
139 ::Shutdown(it->first);
140 it = socketBindMap_.erase(it);
141 }
142 }
143 } // namespace AccessToken
144 } // namespace Security
145 } // namespace OHOS
146