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