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