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 } // namespace NetsysNative
117 } // namespace OHOS
118