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_info.h"
17 #include "net_mgr_log_wrapper.h"
18 #include "parcel.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
22 static constexpr uint32_t STATS_INFO_MAX_SIZE = 5000;
23
Marshalling(Parcel & parcel) const24 bool NetStatsInfo::Marshalling(Parcel &parcel) const
25 {
26 if (!parcel.WriteUint32(uid_)) {
27 return false;
28 }
29 if (!parcel.WriteString(iface_)) {
30 return false;
31 }
32 if (!parcel.WriteString(ident_)) {
33 return false;
34 }
35 if (!parcel.WriteUint64(date_)) {
36 return false;
37 }
38 if (!parcel.WriteUint64(rxBytes_)) {
39 return false;
40 }
41 if (!parcel.WriteUint64(txBytes_)) {
42 return false;
43 }
44 if (!parcel.WriteUint64(rxPackets_)) {
45 return false;
46 }
47 if (!parcel.WriteUint64(txPackets_)) {
48 return false;
49 }
50 return true;
51 }
52
Marshalling(Parcel & parcel,const NetStatsInfo & stats)53 bool NetStatsInfo::Marshalling(Parcel &parcel, const NetStatsInfo &stats)
54 {
55 if (!parcel.WriteUint32(stats.uid_)) {
56 return false;
57 }
58 if (!parcel.WriteString(stats.iface_)) {
59 return false;
60 }
61 if (!parcel.WriteString(stats.ident_)) {
62 return false;
63 }
64 if (!parcel.WriteUint64(stats.date_)) {
65 return false;
66 }
67 if (!parcel.WriteUint64(stats.rxBytes_)) {
68 return false;
69 }
70 if (!parcel.WriteUint64(stats.txBytes_)) {
71 return false;
72 }
73 if (!parcel.WriteUint64(stats.rxPackets_)) {
74 return false;
75 }
76 if (!parcel.WriteUint64(stats.txPackets_)) {
77 return false;
78 }
79 return true;
80 }
81
Marshalling(Parcel & parcel,const std::vector<NetStatsInfo> & statsInfos)82 bool NetStatsInfo::Marshalling(Parcel &parcel, const std::vector<NetStatsInfo> &statsInfos)
83 {
84 uint32_t vSize = statsInfos.size();
85 if (vSize > STATS_INFO_MAX_SIZE) {
86 NETMGR_LOG_E("Size of the statsInfos exceeds maximum.");
87 return false;
88 }
89 if (!parcel.WriteUint32(vSize)) {
90 NETMGR_LOG_E("Write size of the statsInfos fail.");
91 return false;
92 }
93
94 return std::all_of(statsInfos.begin(), statsInfos.end(),
95 [&parcel](const auto &info) { return info.Marshalling(parcel); });
96 }
97
Marshalling(Parcel & parcel,const std::unordered_map<uint32_t,NetStatsInfo> & statsInfos)98 bool NetStatsInfo::Marshalling(Parcel &parcel, const std::unordered_map<uint32_t, NetStatsInfo> &statsInfos)
99 {
100 uint32_t vSize = statsInfos.size();
101 if (vSize > STATS_INFO_MAX_SIZE) {
102 NETMGR_LOG_E("Size of the statsInfos exceeds maximum.");
103 return false;
104 }
105 if (!parcel.WriteUint32(vSize)) {
106 NETMGR_LOG_E("Write size of the statsInfos fail.");
107 return false;
108 }
109 return std::all_of(statsInfos.begin(), statsInfos.end(),
110 [&parcel](const auto &info) { return info.second.Marshalling(parcel); });
111 }
112
Unmarshalling(Parcel & parcel,std::vector<NetStatsInfo> & statsInfos)113 bool NetStatsInfo::Unmarshalling(Parcel &parcel, std::vector<NetStatsInfo> &statsInfos)
114 {
115 uint32_t vSize = 0;
116 if (!parcel.ReadUint32(vSize)) {
117 NETMGR_LOG_E("Read size of the statsInfos fail.");
118 return false;
119 }
120
121 if (vSize > STATS_INFO_MAX_SIZE) {
122 NETMGR_LOG_E("Size of the statsInfos exceeds maximum.");
123 return false;
124 }
125 statsInfos.reserve(vSize);
126 for (uint32_t i = 0; i < vSize; i++) {
127 NetStatsInfo tmpData;
128 if (!NetStatsInfo::Unmarshalling(parcel, tmpData)) {
129 NETMGR_LOG_E("Unmarshalling the statsInfo fail.");
130 return false;
131 }
132 statsInfos.push_back(std::move(tmpData));
133 }
134
135 return true;
136 }
137
Unmarshalling(Parcel & parcel,std::unordered_map<uint32_t,NetStatsInfo> & statsInfos)138 bool NetStatsInfo::Unmarshalling(Parcel &parcel, std::unordered_map<uint32_t, NetStatsInfo> &statsInfos)
139 {
140 uint32_t vSize = 0;
141 if (!parcel.ReadUint32(vSize)) {
142 NETMGR_LOG_E("Read size of the statsInfos fail.");
143 return false;
144 }
145 if (vSize > STATS_INFO_MAX_SIZE) {
146 NETMGR_LOG_E("Size of the statsInfos exceeds maximum.");
147 return false;
148 }
149 for (uint32_t i = 0; i < vSize; i++) {
150 NetStatsInfo tmpData;
151 if (!NetStatsInfo::Unmarshalling(parcel, tmpData)) {
152 NETMGR_LOG_E("Unmarshalling the statsInfo fail.");
153 return false;
154 }
155 statsInfos.emplace(tmpData.uid_, std::move(tmpData));
156 }
157 return true;
158 }
159
Unmarshalling(Parcel & parcel,NetStatsInfo & stats)160 bool NetStatsInfo::Unmarshalling(Parcel &parcel, NetStatsInfo &stats)
161 {
162 if (!parcel.ReadUint32(stats.uid_)) {
163 return false;
164 }
165 if (!parcel.ReadString(stats.iface_)) {
166 return false;
167 }
168 if (!parcel.ReadString(stats.ident_)) {
169 return false;
170 }
171 if (!parcel.ReadUint64(stats.date_)) {
172 return false;
173 }
174 if (!parcel.ReadUint64(stats.rxBytes_)) {
175 return false;
176 }
177 if (!parcel.ReadUint64(stats.txBytes_)) {
178 return false;
179 }
180 if (!parcel.ReadUint64(stats.rxPackets_)) {
181 return false;
182 }
183 if (!parcel.ReadUint64(stats.txPackets_)) {
184 return false;
185 }
186 return true;
187 }
188
StatsInfoUnmarshallingVector(Parcel & parcel,std::vector<NetStatsInfo> & statsInfos)189 bool StatsInfoUnmarshallingVector(Parcel &parcel, std::vector<NetStatsInfo> &statsInfos)
190 {
191 return NetManagerStandard::NetStatsInfo::Unmarshalling(parcel, statsInfos);
192 }
193 } // namespace NetManagerStandard
194 } // namespace OHOS
195