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