• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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_service_stub.h"
17 
18 #include "net_manager_constants.h"
19 #include "net_mgr_log_wrapper.h"
20 #include "netmanager_base_permission.h"
21 
22 namespace OHOS {
23 namespace NetManagerStandard {
NetStatsServiceStub()24 NetStatsServiceStub::NetStatsServiceStub()
25 {
26     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_NSM_REGISTER_NET_STATS_CALLBACK)] =
27         &NetStatsServiceStub::OnRegisterNetStatsCallback;
28     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_NSM_UNREGISTER_NET_STATS_CALLBACK)] =
29         &NetStatsServiceStub::OnUnregisterNetStatsCallback;
30     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_IFACE_RXBYTES)] =
31         &NetStatsServiceStub::OnGetIfaceRxBytes;
32     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_IFACE_TXBYTES)] =
33         &NetStatsServiceStub::OnGetIfaceTxBytes;
34     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_CELLULAR_RXBYTES)] =
35         &NetStatsServiceStub::OnGetCellularRxBytes;
36     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_CELLULAR_TXBYTES)] =
37         &NetStatsServiceStub::OnGetCellularTxBytes;
38     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_RXBYTES)] =
39         &NetStatsServiceStub::OnGetAllRxBytes;
40     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_TXBYTES)] =
41         &NetStatsServiceStub::OnGetAllTxBytes;
42     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_UID_RXBYTES)] =
43         &NetStatsServiceStub::OnGetUidRxBytes;
44     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_UID_TXBYTES)] =
45         &NetStatsServiceStub::OnGetUidTxBytes;
46     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_IFACE_STATS_DETAIL)] =
47         &NetStatsServiceStub::OnGetIfaceStatsDetail;
48     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_UID_STATS_DETAIL)] =
49         &NetStatsServiceStub::OnGetUidStatsDetail;
50     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_UPDATE_IFACES_STATS)] =
51         &NetStatsServiceStub::OnUpdateIfacesStats;
52     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_UPDATE_STATS_DATA)] =
53         &NetStatsServiceStub::OnUpdateStatsData;
54     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_NSM_RESET_FACTORY)] =
55         &NetStatsServiceStub::OnResetFactory;
56     memberFuncMap_[static_cast<uint32_t>(StatsInterfaceCode::CMD_GET_ALL_STATS_INFO)] =
57         &NetStatsServiceStub::OnGetAllStatsInfo;
58 }
59 
60 NetStatsServiceStub::~NetStatsServiceStub() = default;
61 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)62 int32_t NetStatsServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
63                                              MessageOption &option)
64 {
65     NETMGR_LOG_D("stub call start, code = [%{public}d]", code);
66 
67     std::u16string myDescripters = NetStatsServiceStub::GetDescriptor();
68     std::u16string remoteDescripters = data.ReadInterfaceToken();
69     if (myDescripters != remoteDescripters) {
70         NETMGR_LOG_D("descriptor checked fail");
71         return NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
72     }
73 
74     auto itFunc = memberFuncMap_.find(code);
75     if (itFunc != memberFuncMap_.end()) {
76         auto requestFunc = itFunc->second;
77         if (requestFunc != nullptr) {
78             return (this->*requestFunc)(data, reply);
79         }
80     }
81     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
82 }
83 
OnRegisterNetStatsCallback(MessageParcel & data,MessageParcel & reply)84 int32_t NetStatsServiceStub::OnRegisterNetStatsCallback(MessageParcel &data, MessageParcel &reply)
85 {
86     if (!NetManagerPermission::IsSystemCaller()) {
87         NETMGR_LOG_E("Permission check failed.");
88         if (!reply.WriteInt32(NETMANAGER_ERR_NOT_SYSTEM_CALL)) {
89             return IPC_STUB_WRITE_PARCEL_ERR;
90         }
91         return NETMANAGER_SUCCESS;
92     }
93     if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_STATS)) {
94         if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
95             return IPC_STUB_WRITE_PARCEL_ERR;
96         }
97         return NETMANAGER_SUCCESS;
98     }
99     int32_t result = NETMANAGER_ERR_LOCAL_PTR_NULL;
100     sptr<IRemoteObject> remote = data.ReadRemoteObject();
101     if (remote == nullptr) {
102         NETMGR_LOG_E("Callback ptr is nullptr.");
103         reply.WriteInt32(result);
104         return result;
105     }
106 
107     sptr<INetStatsCallback> callback = iface_cast<INetStatsCallback>(remote);
108     result = RegisterNetStatsCallback(callback);
109     if (!reply.WriteInt32(result)) {
110         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
111     }
112     return NETMANAGER_SUCCESS;
113 }
114 
OnUnregisterNetStatsCallback(MessageParcel & data,MessageParcel & reply)115 int32_t NetStatsServiceStub::OnUnregisterNetStatsCallback(MessageParcel &data, MessageParcel &reply)
116 {
117     if (!NetManagerPermission::IsSystemCaller()) {
118         NETMGR_LOG_E("Permission check failed.");
119         if (!reply.WriteInt32(NETMANAGER_ERR_NOT_SYSTEM_CALL)) {
120             return IPC_STUB_WRITE_PARCEL_ERR;
121         }
122         return NETMANAGER_SUCCESS;
123     }
124     if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_STATS)) {
125         if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
126             return IPC_STUB_WRITE_PARCEL_ERR;
127         }
128         return NETMANAGER_SUCCESS;
129     }
130     int32_t result = NETMANAGER_ERR_LOCAL_PTR_NULL;
131     sptr<IRemoteObject> remote = data.ReadRemoteObject();
132     if (remote == nullptr) {
133         NETMGR_LOG_E("callback ptr is nullptr.");
134         reply.WriteInt32(result);
135         return result;
136     }
137     sptr<INetStatsCallback> callback = iface_cast<INetStatsCallback>(remote);
138     result = UnregisterNetStatsCallback(callback);
139     if (!reply.WriteInt32(result)) {
140         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
141     }
142     return result;
143 }
144 
OnGetIfaceRxBytes(MessageParcel & data,MessageParcel & reply)145 int32_t NetStatsServiceStub::OnGetIfaceRxBytes(MessageParcel &data, MessageParcel &reply)
146 {
147     uint64_t stats = 0;
148     std::string iface;
149     if (!data.ReadString(iface)) {
150         NETMGR_LOG_E("Read string failed");
151         return NETMANAGER_ERR_READ_DATA_FAIL;
152     }
153     int32_t result = GetIfaceRxBytes(stats, iface);
154     if (!reply.WriteInt32(result)) {
155         NETMGR_LOG_E("WriteInt32 failed");
156         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
157     }
158     if (result == NETMANAGER_SUCCESS) {
159         if (!reply.WriteUint64(stats)) {
160             NETMGR_LOG_E("WriteUint64 failed");
161             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
162         }
163     }
164     return NETMANAGER_SUCCESS;
165 }
166 
OnGetIfaceTxBytes(MessageParcel & data,MessageParcel & reply)167 int32_t NetStatsServiceStub::OnGetIfaceTxBytes(MessageParcel &data, MessageParcel &reply)
168 {
169     uint64_t stats = 0;
170     std::string iface;
171     if (!data.ReadString(iface)) {
172         NETMGR_LOG_E("Read string failed");
173         return NETMANAGER_ERR_READ_DATA_FAIL;
174     }
175 
176     int32_t result = GetIfaceTxBytes(stats, iface);
177     if (!reply.WriteInt32(result)) {
178         NETMGR_LOG_E("WriteInt32 failed");
179         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
180     }
181     if (result == NETMANAGER_SUCCESS) {
182         if (!reply.WriteUint64(stats)) {
183             NETMGR_LOG_E("WriteUint64 failed");
184             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
185         }
186     }
187     return NETMANAGER_SUCCESS;
188 }
189 
OnGetCellularRxBytes(MessageParcel & data,MessageParcel & reply)190 int32_t NetStatsServiceStub::OnGetCellularRxBytes(MessageParcel &data, MessageParcel &reply)
191 {
192     uint64_t stats = 0;
193     int32_t ret = GetCellularRxBytes(stats);
194     if (!reply.WriteInt32(ret)) {
195         NETMGR_LOG_E("WriteInt32 failed");
196         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
197     }
198     if (ret == NETMANAGER_SUCCESS) {
199         if (!reply.WriteUint64(stats)) {
200             NETMGR_LOG_E("WriteUint64 failed");
201             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
202         }
203     }
204     return NETMANAGER_SUCCESS;
205 }
206 
OnGetCellularTxBytes(MessageParcel & data,MessageParcel & reply)207 int32_t NetStatsServiceStub::OnGetCellularTxBytes(MessageParcel &data, MessageParcel &reply)
208 {
209     uint64_t stats = 0;
210     int32_t ret = GetCellularTxBytes(stats);
211     if (!reply.WriteInt32(ret)) {
212         NETMGR_LOG_E("WriteInt32 failed");
213         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
214     }
215     if (ret == NETMANAGER_SUCCESS) {
216         if (!reply.WriteUint64(stats)) {
217             NETMGR_LOG_E("WriteUint64 failed");
218             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
219         }
220     }
221     return NETMANAGER_SUCCESS;
222 }
223 
OnGetAllRxBytes(MessageParcel & data,MessageParcel & reply)224 int32_t NetStatsServiceStub::OnGetAllRxBytes(MessageParcel &data, MessageParcel &reply)
225 {
226     uint64_t stats = 0;
227     int32_t ret = GetAllRxBytes(stats);
228     if (!reply.WriteInt32(ret)) {
229         NETMGR_LOG_E("WriteInt32 failed");
230         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
231     }
232     if (ret == NETMANAGER_SUCCESS) {
233         if (!reply.WriteUint64(stats)) {
234             NETMGR_LOG_E("WriteUint64 failed");
235             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
236         }
237     }
238 
239     return NETMANAGER_SUCCESS;
240 }
241 
OnGetAllTxBytes(MessageParcel & data,MessageParcel & reply)242 int32_t NetStatsServiceStub::OnGetAllTxBytes(MessageParcel &data, MessageParcel &reply)
243 {
244     uint64_t stats = 0;
245     int32_t ret = GetAllTxBytes(stats);
246     if (!reply.WriteInt32(ret)) {
247         NETMGR_LOG_E("WriteInt32 failed");
248         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
249     }
250     if (ret == NETMANAGER_SUCCESS) {
251         if (!reply.WriteUint64(stats)) {
252             NETMGR_LOG_E("WriteUint64 failed");
253             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
254         }
255     }
256 
257     return NETMANAGER_SUCCESS;
258 }
259 
OnGetUidRxBytes(MessageParcel & data,MessageParcel & reply)260 int32_t NetStatsServiceStub::OnGetUidRxBytes(MessageParcel &data, MessageParcel &reply)
261 {
262     uint32_t uid;
263     uint64_t stats = 0;
264     if (!data.ReadUint32(uid)) {
265         NETMGR_LOG_E("ReadInt32 failed");
266         return NETMANAGER_ERR_READ_DATA_FAIL;
267     }
268 
269     int32_t result = GetUidRxBytes(stats, uid);
270     if (!reply.WriteInt32(result)) {
271         NETMGR_LOG_E("WriteInt32 failed");
272         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
273     }
274     if (result == NETMANAGER_SUCCESS) {
275         if (!reply.WriteUint64(stats)) {
276             NETMGR_LOG_E("WriteUint64 failed");
277             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
278         }
279     }
280     return NETMANAGER_SUCCESS;
281 }
282 
OnGetUidTxBytes(MessageParcel & data,MessageParcel & reply)283 int32_t NetStatsServiceStub::OnGetUidTxBytes(MessageParcel &data, MessageParcel &reply)
284 {
285     uint32_t uid;
286     uint64_t stats = 0;
287     if (!data.ReadUint32(uid)) {
288         NETMGR_LOG_E("ReadInt32 failed");
289         return NETMANAGER_ERR_READ_DATA_FAIL;
290     }
291 
292     int32_t result = GetUidTxBytes(stats, uid);
293     if (!reply.WriteInt32(result)) {
294         NETMGR_LOG_E("WriteInt32 failed");
295         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
296     }
297     if (result == NETMANAGER_SUCCESS) {
298         if (!reply.WriteUint64(stats)) {
299             NETMGR_LOG_E("WriteUint64 failed");
300             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
301         }
302     }
303     return NETMANAGER_SUCCESS;
304 }
305 
OnGetIfaceStatsDetail(MessageParcel & data,MessageParcel & reply)306 int32_t NetStatsServiceStub::OnGetIfaceStatsDetail(MessageParcel &data, MessageParcel &reply)
307 {
308     if (!NetManagerPermission::IsSystemCaller()) {
309         NETMGR_LOG_E("Permission check failed.");
310         if (!reply.WriteInt32(NETMANAGER_ERR_NOT_SYSTEM_CALL)) {
311             return IPC_STUB_WRITE_PARCEL_ERR;
312         }
313         return NETMANAGER_SUCCESS;
314     }
315     if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_STATS)) {
316         if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
317             return IPC_STUB_WRITE_PARCEL_ERR;
318         }
319         return NETMANAGER_SUCCESS;
320     }
321     std::string iface;
322     uint64_t start = 0;
323     uint64_t end = 0;
324     if (!(data.ReadString(iface) && data.ReadUint64(start) && data.ReadUint64(end))) {
325         return NETMANAGER_ERR_READ_DATA_FAIL;
326     }
327     NetStatsInfo info;
328     int32_t ret = GetIfaceStatsDetail(iface, start, end, info);
329     if (!reply.WriteInt32(ret)) {
330         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
331     }
332     if (ret == NETMANAGER_SUCCESS) {
333         if (!info.Marshalling(reply)) {
334             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
335         }
336     }
337     return NETMANAGER_SUCCESS;
338 }
339 
OnGetUidStatsDetail(MessageParcel & data,MessageParcel & reply)340 int32_t NetStatsServiceStub::OnGetUidStatsDetail(MessageParcel &data, MessageParcel &reply)
341 {
342     if (!NetManagerPermission::IsSystemCaller()) {
343         NETMGR_LOG_E("Permission check failed.");
344         if (!reply.WriteInt32(NETMANAGER_ERR_NOT_SYSTEM_CALL)) {
345             return IPC_STUB_WRITE_PARCEL_ERR;
346         }
347         return NETMANAGER_SUCCESS;
348     }
349     if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_STATS)) {
350         if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
351             return IPC_STUB_WRITE_PARCEL_ERR;
352         }
353         return NETMANAGER_SUCCESS;
354     }
355     std::string iface;
356     uint32_t uid = 0;
357     uint64_t start = 0;
358     uint64_t end = 0;
359     if (!(data.ReadString(iface) && data.ReadUint32(uid) && data.ReadUint64(start) && data.ReadUint64(end))) {
360         return NETMANAGER_ERR_READ_DATA_FAIL;
361     }
362     NetStatsInfo info;
363     int32_t ret = GetUidStatsDetail(iface, uid, start, end, info);
364     if (!reply.WriteInt32(ret)) {
365         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
366     }
367     if (ret == NETMANAGER_SUCCESS) {
368         if (!info.Marshalling(reply)) {
369             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
370         }
371     }
372     return NETMANAGER_SUCCESS;
373 }
374 
OnUpdateIfacesStats(MessageParcel & data,MessageParcel & reply)375 int32_t NetStatsServiceStub::OnUpdateIfacesStats(MessageParcel &data, MessageParcel &reply)
376 {
377     std::string iface;
378     uint64_t start = 0;
379     uint64_t end = 0;
380     if (!(data.ReadString(iface) && data.ReadUint64(start) && data.ReadUint64(end))) {
381         return NETMANAGER_ERR_READ_DATA_FAIL;
382     }
383 
384     NetStatsInfo infos;
385     if (!NetStatsInfo::Unmarshalling(data, infos)) {
386         return NETMANAGER_ERR_READ_DATA_FAIL;
387     }
388 
389     int32_t ret = UpdateIfacesStats(iface, start, end, infos);
390     if (!reply.WriteInt32(ret)) {
391         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
392     }
393     return NETMANAGER_SUCCESS;
394 }
395 
OnUpdateStatsData(MessageParcel & data,MessageParcel & reply)396 int32_t NetStatsServiceStub::OnUpdateStatsData(MessageParcel &data, MessageParcel &reply)
397 {
398     int32_t ret = UpdateStatsData();
399     if (!reply.WriteInt32(ret)) {
400         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
401     }
402     return NETMANAGER_SUCCESS;
403 }
404 
OnResetFactory(MessageParcel & data,MessageParcel & reply)405 int32_t NetStatsServiceStub::OnResetFactory(MessageParcel &data, MessageParcel &reply)
406 {
407     if (!NetManagerPermission::IsSystemCaller()) {
408         NETMGR_LOG_E("Permission check failed.");
409         if (!reply.WriteInt32(NETMANAGER_ERR_NOT_SYSTEM_CALL)) {
410             return IPC_STUB_WRITE_PARCEL_ERR;
411         }
412         return NETMANAGER_SUCCESS;
413     }
414     if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_STATS)) {
415         if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
416             return IPC_STUB_WRITE_PARCEL_ERR;
417         }
418         return NETMANAGER_SUCCESS;
419     }
420 
421     int32_t ret = ResetFactory();
422     if (!reply.WriteInt32(ret)) {
423         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
424     }
425     return NETMANAGER_SUCCESS;
426 }
427 
OnGetAllStatsInfo(MessageParcel & data,MessageParcel & reply)428 int32_t NetStatsServiceStub::OnGetAllStatsInfo(MessageParcel &data, MessageParcel &reply)
429 {
430     std::vector<NetStatsInfo> infos;
431     int32_t result = GetAllStatsInfo(infos);
432     if (!reply.WriteInt32(result)) {
433         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
434     }
435     if (result == NETMANAGER_SUCCESS) {
436         if (!NetStatsInfo::Marshalling(reply, infos)) {
437             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
438         }
439     }
440     return NETMANAGER_SUCCESS;
441 }
442 } // namespace NetManagerStandard
443 } // namespace OHOS
444