• 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_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)113 NetStatsInfo* NetStatsInfo::Unmarshalling(Parcel &parcel)
114 {
115     std::unique_ptr<NetStatsInfo> stats = std::make_unique<NetStatsInfo>();
116     if (stats == nullptr) {
117         NETMGR_LOG_E("make ptr NetStatsInfo failed");
118         return nullptr;
119     }
120     if (!parcel.ReadUint32(stats->uid_)) {
121         return nullptr;
122     }
123     if (!parcel.ReadString(stats->iface_)) {
124         return nullptr;
125     }
126     if (!parcel.ReadString(stats->ident_)) {
127         return nullptr;
128     }
129     if (!parcel.ReadUint64(stats->date_)) {
130         return nullptr;
131     }
132     if (!parcel.ReadUint64(stats->rxBytes_)) {
133         return nullptr;
134     }
135     if (!parcel.ReadUint64(stats->txBytes_)) {
136         return nullptr;
137     }
138     if (!parcel.ReadUint64(stats->rxPackets_)) {
139         return nullptr;
140     }
141     if (!parcel.ReadUint64(stats->txPackets_)) {
142         return nullptr;
143     }
144     return stats.release();
145 }
146 
Unmarshalling(Parcel & parcel,std::vector<NetStatsInfo> & statsInfos)147 bool NetStatsInfo::Unmarshalling(Parcel &parcel, std::vector<NetStatsInfo> &statsInfos)
148 {
149     uint32_t vSize = 0;
150     if (!parcel.ReadUint32(vSize)) {
151         NETMGR_LOG_E("Read size of the statsInfos fail.");
152         return false;
153     }
154 
155     if (vSize > STATS_INFO_MAX_SIZE) {
156         NETMGR_LOG_E("Size of the statsInfos exceeds maximum.");
157         return false;
158     }
159     statsInfos.reserve(vSize);
160     for (uint32_t i = 0; i < vSize; i++) {
161         NetStatsInfo tmpData;
162         if (!NetStatsInfo::Unmarshalling(parcel, tmpData)) {
163             NETMGR_LOG_E("Unmarshalling the statsInfo fail.");
164             return false;
165         }
166         statsInfos.push_back(std::move(tmpData));
167     }
168 
169     return true;
170 }
171 
Unmarshalling(Parcel & parcel,std::unordered_map<uint32_t,NetStatsInfo> & statsInfos)172 bool NetStatsInfo::Unmarshalling(Parcel &parcel, std::unordered_map<uint32_t, NetStatsInfo> &statsInfos)
173 {
174     uint32_t vSize = 0;
175     if (!parcel.ReadUint32(vSize)) {
176         NETMGR_LOG_E("Read size of the statsInfos fail.");
177         return false;
178     }
179     if (vSize > STATS_INFO_MAX_SIZE) {
180         NETMGR_LOG_E("Size of the statsInfos exceeds maximum.");
181         return false;
182     }
183     for (uint32_t i = 0; i < vSize; i++) {
184         NetStatsInfo tmpData;
185         if (!NetStatsInfo::Unmarshalling(parcel, tmpData)) {
186             NETMGR_LOG_E("Unmarshalling the statsInfo fail.");
187             return false;
188         }
189         statsInfos.emplace(tmpData.uid_, std::move(tmpData));
190     }
191     return true;
192 }
193 
Unmarshalling(Parcel & parcel,NetStatsInfo & stats)194 bool NetStatsInfo::Unmarshalling(Parcel &parcel, NetStatsInfo &stats)
195 {
196     if (!parcel.ReadUint32(stats.uid_)) {
197         return false;
198     }
199     if (!parcel.ReadString(stats.iface_)) {
200         return false;
201     }
202     if (!parcel.ReadString(stats.ident_)) {
203         return false;
204     }
205     if (!parcel.ReadUint64(stats.date_)) {
206         return false;
207     }
208     if (!parcel.ReadUint64(stats.rxBytes_)) {
209         return false;
210     }
211     if (!parcel.ReadUint64(stats.txBytes_)) {
212         return false;
213     }
214     if (!parcel.ReadUint64(stats.rxPackets_)) {
215         return false;
216     }
217     if (!parcel.ReadUint64(stats.txPackets_)) {
218         return false;
219     }
220     return true;
221 }
222 
StatsInfoUnmarshallingVector(Parcel & parcel,std::vector<NetStatsInfo> & statsInfos)223 bool StatsInfoUnmarshallingVector(Parcel &parcel, std::vector<NetStatsInfo> &statsInfos)
224 {
225     return NetManagerStandard::NetStatsInfo::Unmarshalling(parcel, statsInfos);
226 }
227 } // namespace NetManagerStandard
228 } // namespace OHOS
229