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_link_info.h"
17
18 #include "parcel.h"
19 #include "refbase.h"
20 #include "route.h"
21
22 #include "inet_addr.h"
23 #include "net_mgr_log_wrapper.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27 static constexpr uint32_t MAX_ADDR_SIZE = 16;
28 static constexpr uint32_t MAX_ROUTE_SIZE = 32;
29
NetLinkInfo(const NetLinkInfo & linkInfo)30 NetLinkInfo::NetLinkInfo(const NetLinkInfo &linkInfo)
31 {
32 ifaceName_ = linkInfo.ifaceName_;
33 domain_ = linkInfo.domain_;
34 netAddrList_.assign(linkInfo.netAddrList_.begin(), linkInfo.netAddrList_.end());
35 dnsList_.assign(linkInfo.dnsList_.begin(), linkInfo.dnsList_.end());
36 routeList_.assign(linkInfo.routeList_.begin(), linkInfo.routeList_.end());
37 mtu_ = linkInfo.mtu_;
38 tcpBufferSizes_ = linkInfo.tcpBufferSizes_;
39 httpProxy_ = linkInfo.httpProxy_;
40 }
41
operator =(const NetLinkInfo & linkInfo)42 NetLinkInfo &NetLinkInfo::operator=(const NetLinkInfo &linkInfo)
43 {
44 ifaceName_ = linkInfo.ifaceName_;
45 domain_ = linkInfo.domain_;
46 netAddrList_.assign(linkInfo.netAddrList_.begin(), linkInfo.netAddrList_.end());
47 dnsList_.assign(linkInfo.dnsList_.begin(), linkInfo.dnsList_.end());
48 routeList_.assign(linkInfo.routeList_.begin(), linkInfo.routeList_.end());
49 mtu_ = linkInfo.mtu_;
50 tcpBufferSizes_ = linkInfo.tcpBufferSizes_;
51 httpProxy_ = linkInfo.httpProxy_;
52 return *this;
53 }
54
Marshalling(Parcel & parcel) const55 bool NetLinkInfo::Marshalling(Parcel &parcel) const
56 {
57 if (!parcel.WriteString(ifaceName_)) {
58 return false;
59 }
60 if (!parcel.WriteString(domain_)) {
61 return false;
62 }
63 if (!parcel.WriteUint32(netAddrList_.size())) {
64 return false;
65 }
66 for (auto it = netAddrList_.begin(); it != netAddrList_.end(); it++) {
67 if (!it->Marshalling(parcel)) {
68 NETMGR_LOG_E("write net address to parcel failed");
69 return false;
70 }
71 }
72 if (!parcel.WriteUint32(dnsList_.size())) {
73 return false;
74 }
75 for (auto it = dnsList_.begin(); it != dnsList_.end(); it++) {
76 if (!it->Marshalling(parcel)) {
77 NETMGR_LOG_E("write dns to parcel failed");
78 return false;
79 }
80 }
81 if (!parcel.WriteUint32(routeList_.size())) {
82 return false;
83 }
84 for (auto it = routeList_.begin(); it != routeList_.end(); it++) {
85 if (!it->Marshalling(parcel)) {
86 NETMGR_LOG_E("write route to parcel failed");
87 return false;
88 }
89 }
90 if (!parcel.WriteUint16(mtu_)) {
91 return false;
92 }
93 if (!parcel.WriteString(tcpBufferSizes_)) {
94 return false;
95 }
96 if (!httpProxy_.Marshalling(parcel)) {
97 NETMGR_LOG_E("Write http proxy to parcel failed");
98 return false;
99 }
100 return true;
101 }
102
Unmarshalling(Parcel & parcel)103 sptr<NetLinkInfo> NetLinkInfo::Unmarshalling(Parcel &parcel)
104 {
105 sptr<NetLinkInfo> ptr = new (std::nothrow) NetLinkInfo();
106 if (ptr == nullptr) {
107 return nullptr;
108 }
109 uint32_t size = 0;
110 if (!parcel.ReadString(ptr->ifaceName_) || !parcel.ReadString(ptr->domain_) || !parcel.ReadUint32(size)) {
111 return nullptr;
112 }
113 size = size > MAX_ADDR_SIZE ? MAX_ADDR_SIZE : size;
114 sptr<INetAddr> netAddr;
115 for (uint32_t i = 0; i < size; i++) {
116 netAddr = INetAddr::Unmarshalling(parcel);
117 if (netAddr == nullptr) {
118 NETMGR_LOG_E("INetAddr::Unmarshalling(parcel) is null");
119 return nullptr;
120 }
121 ptr->netAddrList_.push_back(*netAddr);
122 }
123 if (!parcel.ReadUint32(size)) {
124 return nullptr;
125 }
126 size = size > MAX_ADDR_SIZE ? MAX_ADDR_SIZE : size;
127 for (uint32_t i = 0; i < size; i++) {
128 netAddr = INetAddr::Unmarshalling(parcel);
129 if (netAddr == nullptr) {
130 NETMGR_LOG_E("INetAddr::Unmarshalling(parcel) is null");
131 return nullptr;
132 }
133 ptr->dnsList_.push_back(*netAddr);
134 }
135 if (!parcel.ReadUint32(size)) {
136 return nullptr;
137 }
138 size = size > MAX_ROUTE_SIZE ? MAX_ROUTE_SIZE : size;
139 sptr<Route> route;
140 for (uint32_t i = 0; i < size; i++) {
141 route = Route::Unmarshalling(parcel);
142 if (route == nullptr) {
143 NETMGR_LOG_E("Route::Unmarshalling(parcel) is null");
144 return nullptr;
145 }
146 ptr->routeList_.push_back(*route);
147 }
148 if (!parcel.ReadUint16(ptr->mtu_) || !parcel.ReadString(ptr->tcpBufferSizes_) ||
149 !HttpProxy::Unmarshalling(parcel, ptr->httpProxy_)) {
150 return nullptr;
151 }
152 return ptr;
153 }
154
Marshalling(Parcel & parcel,const sptr<NetLinkInfo> & object)155 bool NetLinkInfo::Marshalling(Parcel &parcel, const sptr<NetLinkInfo> &object)
156 {
157 if (object == nullptr) {
158 NETMGR_LOG_E("NetLinkInfo object ptr is nullptr");
159 return false;
160 }
161 if (!parcel.WriteString(object->ifaceName_)) {
162 return false;
163 }
164 if (!parcel.WriteString(object->domain_)) {
165 return false;
166 }
167 if (!parcel.WriteUint32(object->netAddrList_.size())) {
168 return false;
169 }
170 for (auto it = object->netAddrList_.begin(); it != object->netAddrList_.end(); it++) {
171 if (!it->Marshalling(parcel)) {
172 NETMGR_LOG_E("write objects net address to parcel failed");
173 return false;
174 }
175 }
176 if (!parcel.WriteUint32(object->dnsList_.size())) {
177 return false;
178 }
179 for (auto it = object->dnsList_.begin(); it != object->dnsList_.end(); it++) {
180 if (!it->Marshalling(parcel)) {
181 NETMGR_LOG_E("write objects dns to parcel failed");
182 return false;
183 }
184 }
185 if (!parcel.WriteUint32(object->routeList_.size())) {
186 return false;
187 }
188 for (auto it = object->routeList_.begin(); it != object->routeList_.end(); it++) {
189 if (!it->Marshalling(parcel)) {
190 NETMGR_LOG_E("write objects route to parcel failed");
191 return false;
192 }
193 }
194 if (!parcel.WriteUint16(object->mtu_)) {
195 return false;
196 }
197 if (!parcel.WriteString(object->tcpBufferSizes_)) {
198 return false;
199 }
200 if (!object->httpProxy_.Marshalling(parcel)) {
201 NETMGR_LOG_E("Write http proxy to parcel failed");
202 return false;
203 }
204 return true;
205 }
206
Initialize()207 void NetLinkInfo::Initialize()
208 {
209 ifaceName_ = "";
210 domain_ = "";
211 std::list<INetAddr>().swap(netAddrList_);
212 std::list<INetAddr>().swap(dnsList_);
213 std::list<Route>().swap(routeList_);
214 mtu_ = 0;
215 tcpBufferSizes_ = "";
216 }
217
ToString(const std::string & tab) const218 std::string NetLinkInfo::ToString(const std::string &tab) const
219 {
220 std::string str;
221 str.append(tab);
222 str.append("[NetLinkInfo]");
223
224 str.append(tab);
225 str.append("ifaceName_ = ");
226 str.append(ifaceName_);
227
228 str.append(tab);
229 str.append("domain_ = ");
230 str.append(domain_);
231
232 str.append(tab);
233 str.append(ToStringAddr(tab));
234
235 str.append(tab);
236 str.append(ToStringDns(tab));
237
238 str.append(tab);
239 str.append(ToStringRoute(tab));
240 str.append("routeList_ = ");
241
242 str.append(tab);
243 str.append("mtu_ = ");
244 str.append(std::to_string(mtu_));
245
246 str.append(tab);
247 str.append("tcpBufferSizes_ = ");
248 str.append(tcpBufferSizes_);
249
250 str.append(tab);
251 str.append("httpProxy = ");
252 str.append(httpProxy_.ToString());
253 return str;
254 }
255
ToStringAddr(const std::string & tab) const256 std::string NetLinkInfo::ToStringAddr(const std::string &tab) const
257 {
258 std::string str;
259 str.append(tab);
260 str.append("netAddrList_ = ");
261 if (netAddrList_.empty()) {
262 str.append("null");
263 str.append(tab);
264 } else {
265 for (const auto &it : netAddrList_) {
266 str.append(it.ToString(tab));
267 }
268 }
269 return str;
270 }
271
ToStringDns(const std::string & tab) const272 std::string NetLinkInfo::ToStringDns(const std::string &tab) const
273 {
274 std::string str;
275 str.append(tab);
276 str.append("dnsList_ = ");
277 if (dnsList_.empty()) {
278 str.append("null");
279 str.append(tab);
280 } else {
281 for (const auto &it : dnsList_) {
282 str.append(it.ToString(tab));
283 }
284 }
285 return str;
286 }
287
ToStringRoute(const std::string & tab) const288 std::string NetLinkInfo::ToStringRoute(const std::string &tab) const
289 {
290 std::string str;
291 str.append(tab);
292 str.append("routeList_ = ");
293 if (routeList_.empty()) {
294 str.append("null");
295 str.append(tab);
296 } else {
297 for (const auto &it : routeList_) {
298 str.append(it.ToString(tab));
299 }
300 }
301 return str;
302 }
303 } // namespace NetManagerStandard
304 } // namespace OHOS
305