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