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