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 ident_ = linkInfo.ident_;
40 httpProxy_ = linkInfo.httpProxy_;
41 }
42
operator =(const NetLinkInfo & linkInfo)43 NetLinkInfo &NetLinkInfo::operator=(const NetLinkInfo &linkInfo)
44 {
45 ifaceName_ = linkInfo.ifaceName_;
46 domain_ = linkInfo.domain_;
47 netAddrList_.assign(linkInfo.netAddrList_.begin(), linkInfo.netAddrList_.end());
48 dnsList_.assign(linkInfo.dnsList_.begin(), linkInfo.dnsList_.end());
49 routeList_.assign(linkInfo.routeList_.begin(), linkInfo.routeList_.end());
50 mtu_ = linkInfo.mtu_;
51 tcpBufferSizes_ = linkInfo.tcpBufferSizes_;
52 ident_ = linkInfo.ident_;
53 httpProxy_ = linkInfo.httpProxy_;
54 return *this;
55 }
56
Marshalling(Parcel & parcel) const57 bool NetLinkInfo::Marshalling(Parcel &parcel) const
58 {
59 if (!parcel.WriteString(ifaceName_)) {
60 return false;
61 }
62 if (!parcel.WriteString(domain_)) {
63 return false;
64 }
65 if (!parcel.WriteUint32(netAddrList_.size())) {
66 return false;
67 }
68 for (auto it = netAddrList_.begin(); it != netAddrList_.end(); it++) {
69 if (!it->Marshalling(parcel)) {
70 NETMGR_LOG_E("write net address to parcel failed");
71 return false;
72 }
73 }
74 if (!parcel.WriteUint32(dnsList_.size())) {
75 return false;
76 }
77 for (auto it = dnsList_.begin(); it != dnsList_.end(); it++) {
78 if (!it->Marshalling(parcel)) {
79 NETMGR_LOG_E("write dns to parcel failed");
80 return false;
81 }
82 }
83 if (!parcel.WriteUint32(routeList_.size())) {
84 return false;
85 }
86 for (auto it = routeList_.begin(); it != routeList_.end(); it++) {
87 if (!it->Marshalling(parcel)) {
88 NETMGR_LOG_E("write route to parcel failed");
89 return false;
90 }
91 }
92 if (!parcel.WriteUint16(mtu_)) {
93 return false;
94 }
95 if (!parcel.WriteString(tcpBufferSizes_) || !parcel.WriteString(ident_)) {
96 return false;
97 }
98 if (!httpProxy_.Marshalling(parcel)) {
99 NETMGR_LOG_E("Write http proxy to parcel failed");
100 return false;
101 }
102 return true;
103 }
104
Unmarshalling(Parcel & parcel)105 sptr<NetLinkInfo> NetLinkInfo::Unmarshalling(Parcel &parcel)
106 {
107 sptr<NetLinkInfo> ptr = new (std::nothrow) NetLinkInfo();
108 if (ptr == nullptr) {
109 return nullptr;
110 }
111 uint32_t size = 0;
112 if (!parcel.ReadString(ptr->ifaceName_) || !parcel.ReadString(ptr->domain_) || !parcel.ReadUint32(size)) {
113 return nullptr;
114 }
115 size = size > MAX_ADDR_SIZE ? MAX_ADDR_SIZE : size;
116 sptr<INetAddr> netAddr;
117 for (uint32_t i = 0; i < size; i++) {
118 netAddr = INetAddr::Unmarshalling(parcel);
119 if (netAddr == nullptr) {
120 NETMGR_LOG_E("INetAddr::Unmarshalling(parcel) is null");
121 return nullptr;
122 }
123 ptr->netAddrList_.push_back(*netAddr);
124 }
125 if (!parcel.ReadUint32(size)) {
126 return nullptr;
127 }
128 size = size > MAX_ADDR_SIZE ? MAX_ADDR_SIZE : size;
129 for (uint32_t i = 0; i < size; i++) {
130 netAddr = INetAddr::Unmarshalling(parcel);
131 if (netAddr == nullptr) {
132 NETMGR_LOG_E("INetAddr::Unmarshalling(parcel) is null");
133 return nullptr;
134 }
135 ptr->dnsList_.push_back(*netAddr);
136 }
137 if (!parcel.ReadUint32(size)) {
138 return nullptr;
139 }
140 size = size > MAX_ROUTE_SIZE ? MAX_ROUTE_SIZE : size;
141 sptr<Route> route;
142 for (uint32_t i = 0; i < size; i++) {
143 route = Route::Unmarshalling(parcel);
144 if (route == nullptr) {
145 NETMGR_LOG_E("Route::Unmarshalling(parcel) is null");
146 return nullptr;
147 }
148 ptr->routeList_.push_back(*route);
149 }
150 if (!parcel.ReadUint16(ptr->mtu_) || !parcel.ReadString(ptr->tcpBufferSizes_) || !parcel.ReadString(ptr->ident_) ||
151 !HttpProxy::Unmarshalling(parcel, ptr->httpProxy_)) {
152 return nullptr;
153 }
154 return ptr;
155 }
156
Marshalling(Parcel & parcel,const sptr<NetLinkInfo> & object)157 bool NetLinkInfo::Marshalling(Parcel &parcel, const sptr<NetLinkInfo> &object)
158 {
159 if (object == nullptr) {
160 NETMGR_LOG_E("NetLinkInfo object ptr is nullptr");
161 return false;
162 }
163 if (!parcel.WriteString(object->ifaceName_)) {
164 return false;
165 }
166 if (!parcel.WriteString(object->domain_)) {
167 return false;
168 }
169 if (!parcel.WriteUint32(object->netAddrList_.size())) {
170 return false;
171 }
172 for (auto it = object->netAddrList_.begin(); it != object->netAddrList_.end(); it++) {
173 if (!it->Marshalling(parcel)) {
174 NETMGR_LOG_E("write objects net address to parcel failed");
175 return false;
176 }
177 }
178 if (!parcel.WriteUint32(object->dnsList_.size())) {
179 return false;
180 }
181 for (auto it = object->dnsList_.begin(); it != object->dnsList_.end(); it++) {
182 if (!it->Marshalling(parcel)) {
183 NETMGR_LOG_E("write objects dns to parcel failed");
184 return false;
185 }
186 }
187 if (!parcel.WriteUint32(object->routeList_.size())) {
188 return false;
189 }
190 for (auto it = object->routeList_.begin(); it != object->routeList_.end(); it++) {
191 if (!it->Marshalling(parcel)) {
192 NETMGR_LOG_E("write objects route to parcel failed");
193 return false;
194 }
195 }
196 if (!parcel.WriteUint16(object->mtu_) || !parcel.WriteString(object->tcpBufferSizes_) ||
197 !parcel.WriteString(object->ident_)) {
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 ident_ = "";
217 }
218
HasNetAddr(const INetAddr & netAddr) const219 bool NetLinkInfo::HasNetAddr(const INetAddr &netAddr) const
220 {
221 return std::find(netAddrList_.begin(), netAddrList_.end(), netAddr) != netAddrList_.end();
222 }
223
HasRoute(const Route & route) const224 bool NetLinkInfo::HasRoute(const Route &route) const
225 {
226 return std::find(routeList_.begin(), routeList_.end(), route) != routeList_.end();
227 }
228
ToString(const std::string & tab) const229 std::string NetLinkInfo::ToString(const std::string &tab) const
230 {
231 std::string str;
232 str.append(tab);
233 str.append("[NetLinkInfo]");
234
235 str.append(tab);
236 str.append("ifaceName_ = ");
237 str.append(ifaceName_);
238
239 str.append(tab);
240 str.append("domain_ = ");
241 str.append(domain_);
242
243 str.append(tab);
244 str.append(ToStringAddr(tab));
245
246 str.append(tab);
247 str.append(ToStringDns(tab));
248
249 str.append(tab);
250 str.append(ToStringRoute(tab));
251 str.append("routeList_ = ");
252
253 str.append(tab);
254 str.append("mtu_ = ");
255 str.append(std::to_string(mtu_));
256
257 str.append(tab);
258 str.append("tcpBufferSizes_ = ");
259 str.append(tcpBufferSizes_);
260
261 str.append(tab);
262 str.append("ident_ = ");
263 str.append(ident_);
264
265 str.append(tab);
266 str.append("httpProxy = ");
267 str.append(httpProxy_.ToString());
268 return str;
269 }
270
ToStringAddr(const std::string & tab) const271 std::string NetLinkInfo::ToStringAddr(const std::string &tab) const
272 {
273 std::string str;
274 str.append(tab);
275 str.append("netAddrList_ = ");
276 if (netAddrList_.empty()) {
277 str.append("null");
278 str.append(tab);
279 } else {
280 for (const auto &it : netAddrList_) {
281 str.append(it.ToString(tab));
282 }
283 }
284 return str;
285 }
286
ToStringDns(const std::string & tab) const287 std::string NetLinkInfo::ToStringDns(const std::string &tab) const
288 {
289 std::string str;
290 str.append(tab);
291 str.append("dnsList_ = ");
292 if (dnsList_.empty()) {
293 str.append("null");
294 str.append(tab);
295 } else {
296 for (const auto &it : dnsList_) {
297 str.append(it.ToString(tab));
298 }
299 }
300 return str;
301 }
302
ToStringRoute(const std::string & tab) const303 std::string NetLinkInfo::ToStringRoute(const std::string &tab) const
304 {
305 std::string str;
306 str.append(tab);
307 str.append("routeList_ = ");
308 if (routeList_.empty()) {
309 str.append("null");
310 str.append(tab);
311 } else {
312 for (const auto &it : routeList_) {
313 str.append(it.ToString(tab));
314 }
315 }
316 return str;
317 }
318 } // namespace NetManagerStandard
319 } // namespace OHOS
320