1 /*
2 * Copyright (c) 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 "netnative_log_wrapper.h"
17 #include "netsys_net_dns_result_data.h"
18
19 namespace OHOS {
20 namespace NetsysNative {
21
Marshalling(Parcel & parcel) const22 bool NetDnsResultAddrInfo::Marshalling(Parcel &parcel) const
23 {
24 if (!parcel.WriteUint32(type_)) {
25 return false;
26 }
27 if (!parcel.WriteString(addr_)) {
28 return false;
29 }
30 return true;
31 }
32
33
Unmarshalling(Parcel & parcel,NetDnsResultAddrInfo & addrInfo)34 bool NetDnsResultAddrInfo::Unmarshalling(Parcel &parcel, NetDnsResultAddrInfo &addrInfo)
35 {
36 if (!parcel.ReadUint32(addrInfo.type_)) {
37 return false;
38 }
39
40 if (!parcel.ReadString(addrInfo.addr_)) {
41 return false;
42 }
43
44 return true;
45 }
46
Marshalling(Parcel & parcel) const47 bool NetDnsResultReport::Marshalling(Parcel &parcel) const
48 {
49 if (!parcel.WriteUint32(netid_)) {
50 return false;
51 }
52 if (!parcel.WriteUint32(uid_)) {
53 return false;
54 }
55 if (!parcel.WriteUint32(pid_)) {
56 return false;
57 }
58 if (!parcel.WriteUint32(timeused_)) {
59 return false;
60 }
61 if (!parcel.WriteUint32(queryresult_)) {
62 return false;
63 }
64 if (!parcel.WriteString(host_)) {
65 return false;
66 }
67 if (!parcel.WriteUint32(static_cast<uint32_t>(
68 std::min(DNS_RESULT_MAX_SIZE, static_cast<uint32_t>(addrlist_.size()))))) {
69 return false;
70 }
71 uint32_t count = 0;
72 for (const auto &addr : addrlist_) {
73 if (!addr.Marshalling(parcel)) {
74 return false;
75 }
76 if (++count >= DNS_RESULT_MAX_SIZE) {
77 break;
78 }
79 }
80 return true;
81 }
82
83
Unmarshalling(Parcel & parcel,NetDnsResultReport & resultReport)84 bool NetDnsResultReport::Unmarshalling(Parcel &parcel, NetDnsResultReport &resultReport)
85 {
86 std::list<NetDnsResultAddrInfo>().swap(resultReport.addrlist_);
87
88 if (!parcel.ReadUint32(resultReport.netid_) || !parcel.ReadUint32(resultReport.uid_) ||
89 !parcel.ReadUint32(resultReport.pid_) || !parcel.ReadUint32(resultReport.timeused_)) {
90 return false;
91 }
92
93 if (!parcel.ReadUint32(resultReport.queryresult_)) {
94 return false;
95 }
96
97 if (!parcel.ReadString(resultReport.host_)) {
98 return false;
99 }
100
101 uint32_t size = 0;
102 if (!parcel.ReadUint32(size)) {
103 return false;
104 }
105 size = (size > DNS_RESULT_MAX_SIZE) ? DNS_RESULT_MAX_SIZE : size;
106 for (uint32_t i = 0; i < size; ++i) {
107 NetDnsResultAddrInfo addrInfo;
108 if (!NetDnsResultAddrInfo::Unmarshalling(parcel, addrInfo)) {
109 return false;
110 }
111 resultReport.addrlist_.push_back(addrInfo);
112 }
113
114 return true;
115 }
116
Marshalling(Parcel & parcel) const117 bool NetDnsQueryResultAddrInfo::Marshalling(Parcel &parcel) const
118 {
119 if (!parcel.WriteUint16(type_)) {
120 return false;
121 }
122 if (!parcel.WriteString(addr_)) {
123 return false;
124 }
125 return true;
126 }
127
Unmarshalling(Parcel & parcel,NetDnsQueryResultAddrInfo & addrInfo)128 bool NetDnsQueryResultAddrInfo::Unmarshalling(Parcel &parcel, NetDnsQueryResultAddrInfo &addrInfo)
129 {
130 if (!parcel.ReadUint16(addrInfo.type_)) {
131 return false;
132 }
133 if (!parcel.ReadString(addrInfo.addr_)) {
134 return false;
135 }
136 return true;
137 }
138
MarshallingExt(Parcel & parcel) const139 bool NetDnsQueryResultReport::MarshallingExt(Parcel &parcel) const
140 {
141 return (!parcel.WriteUint32(uid_)
142 ||!parcel.WriteUint32(pid_)
143 ||!parcel.WriteString(srcAddr_)
144 ||!parcel.WriteUint8(addrSize_)
145 ||!parcel.WriteUint64(queryTime_)
146 ||!parcel.WriteString(host_)
147 ||!parcel.WriteInt32(retCode_)
148 ||!parcel.WriteUint32(firstQueryEndDuration_)
149 ||!parcel.WriteUint32(firstQueryEnd2AppDuration_)
150 ||!parcel.WriteInt32(ipv4RetCode_)
151 ||!parcel.WriteString(ipv4ServerName_)
152 ||!parcel.WriteInt32(ipv6RetCode_)
153 ||!parcel.WriteString(ipv6ServerName_)
154 ||!parcel.WriteUint8(sourceFrom_)
155 ||!parcel.WriteUint8(dnsServerSize_)
156 ||!parcel.WriteUint16(resBitInfo_)
157 );
158 }
159
Marshalling(Parcel & parcel) const160 bool NetDnsQueryResultReport::Marshalling(Parcel &parcel) const
161 {
162 if (MarshallingExt(parcel)) {
163 return false;
164 }
165 for (std::string server : dnsServerList_) {
166 if (!parcel.WriteString(server)) {
167 return false;
168 }
169 }
170 for (const auto &addr : addrlist_) {
171 if (!addr.Marshalling(parcel)) {
172 return false;
173 }
174 }
175 return true;
176 }
177
UnmarshallingExt(Parcel & parcel,NetDnsQueryResultReport & resultReport)178 bool NetDnsQueryResultReport::UnmarshallingExt(Parcel &parcel, NetDnsQueryResultReport &resultReport)
179 {
180 return (
181 !parcel.ReadUint32(resultReport.uid_)
182 ||!parcel.ReadUint32(resultReport.pid_)
183 ||!parcel.ReadString(resultReport.srcAddr_)
184 ||!parcel.ReadUint8(resultReport.addrSize_)
185 ||!parcel.ReadUint64(resultReport.queryTime_)
186 ||!parcel.ReadString(resultReport.host_)
187 ||!parcel.ReadInt32(resultReport.retCode_)
188 ||!parcel.ReadUint32(resultReport.firstQueryEndDuration_)
189 ||!parcel.ReadUint32(resultReport.firstQueryEnd2AppDuration_)
190 ||!parcel.ReadInt32(resultReport.ipv4RetCode_)
191 ||!parcel.ReadString(resultReport.ipv4ServerName_)
192 ||!parcel.ReadInt32(resultReport.ipv6RetCode_)
193 ||!parcel.ReadString(resultReport.ipv6ServerName_)
194 ||!parcel.ReadUint8(resultReport.sourceFrom_)
195 ||!parcel.ReadUint8(resultReport.dnsServerSize_)
196 ||!parcel.ReadUint16(resultReport.resBitInfo_)
197 );
198 }
199
Unmarshalling(Parcel & parcel,NetDnsQueryResultReport & resultReport)200 bool NetDnsQueryResultReport::Unmarshalling(Parcel &parcel, NetDnsQueryResultReport &resultReport)
201 {
202 if (NetDnsQueryResultReport::UnmarshallingExt(parcel, resultReport)) {
203 return false;
204 }
205 for (uint32_t i = 0; i < resultReport.dnsServerSize_; ++i) {
206 std::string dnsServer;
207 if (!parcel.ReadString(dnsServer)) {
208 return false;
209 }
210 resultReport.dnsServerList_.push_back(dnsServer);
211 }
212
213 uint8_t size = resultReport.addrSize_;
214 for (uint8_t j = 0; j < size; ++j) {
215 NetDnsQueryResultAddrInfo addrInfo;
216 if (!NetDnsQueryResultAddrInfo::Unmarshalling(parcel, addrInfo)) {
217 return false;
218 }
219 resultReport.addrlist_.push_back(addrInfo);
220 }
221 return true;
222 }
223 } // namespace NetsysNative
224 } // namespace OHOS
225