1 /*
2 * Copyright (c) 2021-2022 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 "net_stats_client.h"
17 #include <thread>
18
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 #include "net_manager_constants.h"
23 #include "net_mgr_log_wrapper.h"
24 #include "sys/socket.h"
25
26 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
27 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
28
29 namespace OHOS {
30 namespace NetManagerStandard {
NetStatsClient()31 NetStatsClient::NetStatsClient() : netStatsService_(nullptr), deathRecipient_(nullptr), callback_(nullptr) {}
32
33 NetStatsClient::~NetStatsClient() = default;
34
RegisterNetStatsCallback(const sptr<INetStatsCallback> & callback)35 int32_t NetStatsClient::RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
36 {
37 NETMGR_LOG_D("RegisterNetStatsCallback client in");
38 sptr<INetStatsService> proxy = GetProxy();
39 if (proxy == nullptr) {
40 NETMGR_LOG_E("proxy is nullptr");
41 return NETMANAGER_ERR_GET_PROXY_FAIL;
42 }
43 int32_t ret = proxy->RegisterNetStatsCallback(callback);
44 if (ret == NETMANAGER_SUCCESS) {
45 NETMGR_LOG_D("RegisterNetStatsCallback success, save callback");
46 callback_ = callback;
47 }
48
49 return ret;
50 }
51
UnregisterNetStatsCallback(const sptr<INetStatsCallback> & callback)52 int32_t NetStatsClient::UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
53 {
54 sptr<INetStatsService> proxy = GetProxy();
55 if (proxy == nullptr) {
56 NETMGR_LOG_E("proxy is nullptr");
57 return NETMANAGER_ERR_GET_PROXY_FAIL;
58 }
59 int32_t ret = proxy->UnregisterNetStatsCallback(callback);
60 if (ret == NETMANAGER_SUCCESS) {
61 NETMGR_LOG_D("UnRegisterNetStatsCallback success, delete callback");
62 callback_ = nullptr;
63 }
64
65 return ret;
66 }
67
GetProxy()68 sptr<INetStatsService> NetStatsClient::GetProxy()
69 {
70 std::lock_guard lock(mutex_);
71
72 if (netStatsService_ != nullptr) {
73 NETMGR_LOG_D("get proxy is ok");
74 return netStatsService_;
75 }
76
77 NETMGR_LOG_D("execute GetSystemAbilityManager");
78 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
79 if (sam == nullptr) {
80 NETMGR_LOG_E("NetPolicyManager::GetProxy(), get SystemAbilityManager failed");
81 return nullptr;
82 }
83
84 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_STATS_MANAGER_SYS_ABILITY_ID);
85 if (remote == nullptr) {
86 NETMGR_LOG_E("get Remote service failed");
87 return nullptr;
88 }
89
90 deathRecipient_ = new (std::nothrow) NetStatsDeathRecipient(*this);
91 if (deathRecipient_ == nullptr) {
92 NETMGR_LOG_E("get deathRecipient_ failed");
93 return nullptr;
94 }
95 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
96 NETMGR_LOG_E("add death recipient failed");
97 return nullptr;
98 }
99
100 netStatsService_ = iface_cast<INetStatsService>(remote);
101 if (netStatsService_ == nullptr) {
102 NETMGR_LOG_E("get Remote service proxy failed");
103 return nullptr;
104 }
105 return netStatsService_;
106 }
107
RecoverCallback()108 void NetStatsClient::RecoverCallback()
109 {
110 uint32_t count = 0;
111 while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
112 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
113 count++;
114 }
115 auto proxy = GetProxy();
116 NETMGR_LOG_W("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
117 if (proxy != nullptr && callback_ != nullptr) {
118 int32_t ret = proxy->RegisterNetStatsCallback(callback_);
119 NETMGR_LOG_D("Register result %{public}d", ret);
120 }
121 }
122
OnRemoteDied(const wptr<IRemoteObject> & remote)123 void NetStatsClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
124 {
125 NETMGR_LOG_D("on remote died");
126 if (remote == nullptr) {
127 NETMGR_LOG_E("remote object is nullptr");
128 return;
129 }
130
131 std::lock_guard lock(mutex_);
132 if (netStatsService_ == nullptr) {
133 NETMGR_LOG_E("NetConnService_ is nullptr");
134 return;
135 }
136
137 sptr<IRemoteObject> local = netStatsService_->AsObject();
138 if (local != remote.promote()) {
139 NETMGR_LOG_E("proxy and stub is not same remote object");
140 return;
141 }
142
143 local->RemoveDeathRecipient(deathRecipient_);
144 netStatsService_ = nullptr;
145 if (callback_ != nullptr) {
146 NETMGR_LOG_D("on remote died recover callback");
147 std::thread t([this]() {
148 RecoverCallback();
149 });
150 std::string threadName = "nestatsRecoverCallback";
151 pthread_setname_np(t.native_handle(), threadName.c_str());
152 t.detach();
153 }
154 }
155
GetIfaceRxBytes(uint64_t & stats,const std::string & interfaceName)156 int32_t NetStatsClient::GetIfaceRxBytes(uint64_t &stats, const std::string &interfaceName)
157 {
158 sptr<INetStatsService> proxy = GetProxy();
159 if (proxy == nullptr) {
160 NETMGR_LOG_E("proxy is nullptr");
161 return NETMANAGER_ERR_GET_PROXY_FAIL;
162 }
163 return proxy->GetIfaceRxBytes(stats, interfaceName);
164 }
165
GetIfaceTxBytes(uint64_t & stats,const std::string & interfaceName)166 int32_t NetStatsClient::GetIfaceTxBytes(uint64_t &stats, const std::string &interfaceName)
167 {
168 sptr<INetStatsService> proxy = GetProxy();
169 if (proxy == nullptr) {
170 NETMGR_LOG_E("proxy is nullptr");
171 return NETMANAGER_ERR_GET_PROXY_FAIL;
172 }
173 return proxy->GetIfaceTxBytes(stats, interfaceName);
174 }
175
GetCellularRxBytes(uint64_t & stats)176 int32_t NetStatsClient::GetCellularRxBytes(uint64_t &stats)
177 {
178 sptr<INetStatsService> proxy = GetProxy();
179 if (proxy == nullptr) {
180 NETMGR_LOG_E("proxy is nullptr");
181 return NETMANAGER_ERR_GET_PROXY_FAIL;
182 }
183 return proxy->GetCellularRxBytes(stats);
184 }
185
GetCellularTxBytes(uint64_t & stats)186 int32_t NetStatsClient::GetCellularTxBytes(uint64_t &stats)
187 {
188 sptr<INetStatsService> proxy = GetProxy();
189 if (proxy == nullptr) {
190 NETMGR_LOG_E("proxy is nullptr");
191 return NETMANAGER_ERR_GET_PROXY_FAIL;
192 }
193 return proxy->GetCellularTxBytes(stats);
194 }
195
GetAllRxBytes(uint64_t & stats)196 int32_t NetStatsClient::GetAllRxBytes(uint64_t &stats)
197 {
198 sptr<INetStatsService> proxy = GetProxy();
199 if (proxy == nullptr) {
200 NETMGR_LOG_E("proxy is nullptr");
201 return NETMANAGER_ERR_GET_PROXY_FAIL;
202 }
203 return proxy->GetAllRxBytes(stats);
204 }
205
GetAllTxBytes(uint64_t & stats)206 int32_t NetStatsClient::GetAllTxBytes(uint64_t &stats)
207 {
208 sptr<INetStatsService> proxy = GetProxy();
209 if (proxy == nullptr) {
210 NETMGR_LOG_E("proxy is nullptr");
211 return NETMANAGER_ERR_GET_PROXY_FAIL;
212 }
213 return proxy->GetAllTxBytes(stats);
214 }
215
GetUidRxBytes(uint64_t & stats,uint32_t uid)216 int32_t NetStatsClient::GetUidRxBytes(uint64_t &stats, uint32_t uid)
217 {
218 sptr<INetStatsService> proxy = GetProxy();
219 if (proxy == nullptr) {
220 NETMGR_LOG_E("proxy is nullptr");
221 return NETMANAGER_ERR_GET_PROXY_FAIL;
222 }
223 return proxy->GetUidRxBytes(stats, uid);
224 }
225
GetUidTxBytes(uint64_t & stats,uint32_t uid)226 int32_t NetStatsClient::GetUidTxBytes(uint64_t &stats, uint32_t uid)
227 {
228 sptr<INetStatsService> proxy = GetProxy();
229 if (proxy == nullptr) {
230 NETMGR_LOG_E("proxy is nullptr");
231 return NETMANAGER_ERR_GET_PROXY_FAIL;
232 }
233 return proxy->GetUidTxBytes(stats, uid);
234 }
235
GetIfaceStatsDetail(const std::string & iface,uint64_t start,uint64_t end,NetStatsInfo & statsInfo)236 int32_t NetStatsClient::GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
237 NetStatsInfo &statsInfo)
238 {
239 sptr<INetStatsService> proxy = GetProxy();
240 if (proxy == nullptr) {
241 NETMGR_LOG_E("proxy is nullptr");
242 return NETMANAGER_ERR_GET_PROXY_FAIL;
243 }
244 return proxy->GetIfaceStatsDetail(iface, start, end, statsInfo);
245 }
246
GetUidStatsDetail(const std::string & iface,uint32_t uid,uint64_t start,uint64_t end,NetStatsInfo & statsInfo)247 int32_t NetStatsClient::GetUidStatsDetail(const std::string &iface, uint32_t uid, uint64_t start, uint64_t end,
248 NetStatsInfo &statsInfo)
249 {
250 sptr<INetStatsService> proxy = GetProxy();
251 if (proxy == nullptr) {
252 NETMGR_LOG_E("proxy is nullptr");
253 return NETMANAGER_ERR_GET_PROXY_FAIL;
254 }
255 return proxy->GetUidStatsDetail(iface, uid, start, end, statsInfo);
256 }
257
UpdateIfacesStats(const std::string & iface,uint64_t start,uint64_t end,const NetStatsInfo & stats)258 int32_t NetStatsClient::UpdateIfacesStats(const std::string &iface, uint64_t start, uint64_t end,
259 const NetStatsInfo &stats)
260 {
261 sptr<INetStatsService> proxy = GetProxy();
262 if (proxy == nullptr) {
263 NETMGR_LOG_E("proxy is nullptr");
264 return NETMANAGER_ERR_GET_PROXY_FAIL;
265 }
266 return proxy->UpdateIfacesStats(iface, start, end, stats);
267 }
268
UpdateStatsData()269 int32_t NetStatsClient::UpdateStatsData()
270 {
271 sptr<INetStatsService> proxy = GetProxy();
272 if (proxy == nullptr) {
273 NETMGR_LOG_E("proxy is nullptr");
274 return NETMANAGER_ERR_GET_PROXY_FAIL;
275 }
276 return proxy->UpdateStatsData();
277 }
278
ResetFactory()279 int32_t NetStatsClient::ResetFactory()
280 {
281 sptr<INetStatsService> proxy = GetProxy();
282 if (proxy == nullptr) {
283 NETMGR_LOG_E("proxy is nullptr");
284 return NETMANAGER_ERR_GET_PROXY_FAIL;
285 }
286 return proxy->ResetFactory();
287 }
288
GetAllStatsInfo(std::vector<NetStatsInfo> & infos)289 int32_t NetStatsClient::GetAllStatsInfo(std::vector<NetStatsInfo> &infos)
290 {
291 sptr<INetStatsService> proxy = GetProxy();
292 if (proxy == nullptr) {
293 NETMGR_LOG_E("proxy is nullptr");
294 return NETMANAGER_ERR_GET_PROXY_FAIL;
295 }
296 return proxy->GetAllStatsInfo(infos);
297 }
298
GetSockfdRxBytes(uint64_t & stats,int32_t sockfd)299 int32_t NetStatsClient::GetSockfdRxBytes(uint64_t &stats, int32_t sockfd)
300 {
301 if (sockfd <= 0) {
302 NETMGR_LOG_E("sockfd is invalid");
303 return NETMANAGER_ERR_INVALID_PARAMETER;
304 }
305
306 sptr<INetStatsService> proxy = GetProxy();
307 if (proxy == nullptr) {
308 NETMGR_LOG_E("proxy is nullptr");
309 return NETMANAGER_ERR_GET_PROXY_FAIL;
310 }
311
312 uint64_t optrval = 0;
313 uint32_t optlen = sizeof(optrval);
314 if (getsockopt(sockfd, SOL_SOCKET, SO_COOKIE, &optrval, &optlen) == -1) {
315 NETMGR_LOG_E("getsockopt error");
316 return NETMANAGER_ERR_OPERATION_FAILED;
317 }
318
319 return proxy->GetCookieRxBytes(stats, optrval);
320 }
321
GetSockfdTxBytes(uint64_t & stats,int32_t sockfd)322 int32_t NetStatsClient::GetSockfdTxBytes(uint64_t &stats, int32_t sockfd)
323 {
324 if (sockfd <= 0) {
325 NETMGR_LOG_E("sockfd is invalid");
326 return NETMANAGER_ERR_INVALID_PARAMETER;
327 }
328
329 sptr<INetStatsService> proxy = GetProxy();
330 if (proxy == nullptr) {
331 NETMGR_LOG_E("proxy is nullptr");
332 return NETMANAGER_ERR_GET_PROXY_FAIL;
333 }
334
335 uint64_t optrval = 0;
336 uint32_t optlen = sizeof(optrval);
337 if (getsockopt(sockfd, SOL_SOCKET, SO_COOKIE, &optrval, &optlen) == -1) {
338 NETMGR_LOG_E("getsockopt error");
339 return NETMANAGER_ERR_OPERATION_FAILED;
340 }
341
342 return proxy->GetCookieTxBytes(stats, optrval);
343 }
344 } // namespace NetManagerStandard
345 } // namespace OHOS
346