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
HasNetAddr(const INetAddr & netAddr) const218 bool NetLinkInfo::HasNetAddr(const INetAddr &netAddr) const
219 {
220 return std::find(netAddrList_.begin(), netAddrList_.end(), netAddr) != netAddrList_.end();
221 }
222
HasRoute(const Route & route) const223 bool NetLinkInfo::HasRoute(const Route &route) const
224 {
225 return std::find(routeList_.begin(), routeList_.end(), route) != routeList_.end();
226 }
227
ToString(const std::string & tab) const228 std::string NetLinkInfo::ToString(const std::string &tab) const
229 {
230 std::string str;
231 str.append(tab);
232 str.append("[NetLinkInfo]");
233
234 str.append(tab);
235 str.append("ifaceName_ = ");
236 str.append(ifaceName_);
237
238 str.append(tab);
239 str.append("domain_ = ");
240 str.append(domain_);
241
242 str.append(tab);
243 str.append(ToStringAddr(tab));
244
245 str.append(tab);
246 str.append(ToStringDns(tab));
247
248 str.append(tab);
249 str.append(ToStringRoute(tab));
250 str.append("routeList_ = ");
251
252 str.append(tab);
253 str.append("mtu_ = ");
254 str.append(std::to_string(mtu_));
255
256 str.append(tab);
257 str.append("tcpBufferSizes_ = ");
258 str.append(tcpBufferSizes_);
259
260 str.append(tab);
261 str.append("httpProxy = ");
262 str.append(httpProxy_.ToString());
263 return str;
264 }
265
ToStringAddr(const std::string & tab) const266 std::string NetLinkInfo::ToStringAddr(const std::string &tab) const
267 {
268 std::string str;
269 str.append(tab);
270 str.append("netAddrList_ = ");
271 if (netAddrList_.empty()) {
272 str.append("null");
273 str.append(tab);
274 } else {
275 for (const auto &it : netAddrList_) {
276 str.append(it.ToString(tab));
277 }
278 }
279 return str;
280 }
281
ToStringDns(const std::string & tab) const282 std::string NetLinkInfo::ToStringDns(const std::string &tab) const
283 {
284 std::string str;
285 str.append(tab);
286 str.append("dnsList_ = ");
287 if (dnsList_.empty()) {
288 str.append("null");
289 str.append(tab);
290 } else {
291 for (const auto &it : dnsList_) {
292 str.append(it.ToString(tab));
293 }
294 }
295 return str;
296 }
297
ToStringRoute(const std::string & tab) const298 std::string NetLinkInfo::ToStringRoute(const std::string &tab) const
299 {
300 std::string str;
301 str.append(tab);
302 str.append("routeList_ = ");
303 if (routeList_.empty()) {
304 str.append("null");
305 str.append(tab);
306 } else {
307 for (const auto &it : routeList_) {
308 str.append(it.ToString(tab));
309 }
310 }
311 return str;
312 }
313 } // namespace NetManagerStandard
314 } // namespace OHOS
315