• 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 "dev_interface_state.h"
17 
18 #include "inet_addr.h"
19 #include "net_manager_center.h"
20 #include "net_manager_constants.h"
21 #include "netmanager_base_common_utils.h"
22 #include "netmgr_ext_log_wrapper.h"
23 #include "netsys_controller.h"
24 #include "route.h"
25 #include "static_configuration.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 constexpr const char *DEFAULT_ROUTE_ADDR = "0.0.0.0";
31 } // namespace
32 
DevInterfaceState()33 DevInterfaceState::DevInterfaceState()
34 {
35     netSupplierInfo_ = new (std::nothrow) NetSupplierInfo();
36     if (netSupplierInfo_ == nullptr) {
37         NETMGR_EXT_LOG_E("NetSupplierInfo new failed");
38     }
39 }
40 
SetDevName(const std::string & devName)41 void DevInterfaceState::SetDevName(const std::string &devName)
42 {
43     devName_ = devName;
44 }
45 
SetNetCaps(const std::set<NetCap> & netCaps)46 void DevInterfaceState::SetNetCaps(const std::set<NetCap> &netCaps)
47 {
48     netCaps_ = netCaps;
49 }
50 
SetLinkUp(bool up)51 void DevInterfaceState::SetLinkUp(bool up)
52 {
53     linkUp_ = up;
54 }
55 
SetlinkInfo(sptr<NetLinkInfo> & linkInfo)56 void DevInterfaceState::SetlinkInfo(sptr<NetLinkInfo> &linkInfo)
57 {
58     linkInfo_ = linkInfo;
59 }
60 
SetIfcfg(sptr<InterfaceConfiguration> & ifCfg)61 void DevInterfaceState::SetIfcfg(sptr<InterfaceConfiguration> &ifCfg)
62 {
63     ifCfg_ = ifCfg;
64     if (ifCfg_->mode_ == STATIC) {
65         UpdateLinkInfo();
66         if (connLinkState_ == LINK_AVAILABLE) {
67             RemoteUpdateNetLinkInfo();
68         }
69     }
70 }
71 
SetLancfg(sptr<InterfaceConfiguration> & ifCfg)72 void DevInterfaceState::SetLancfg(sptr<InterfaceConfiguration> &ifCfg)
73 {
74     ifCfg_ = ifCfg;
75     if (ifCfg_->mode_ == LAN_STATIC) {
76         UpdateLanLinkInfo();
77     }
78 }
79 
SetDhcpReqState(bool dhcpReqState)80 void DevInterfaceState::SetDhcpReqState(bool dhcpReqState)
81 {
82     dhcpReqState_ = dhcpReqState;
83 }
84 
GetDevName() const85 std::string DevInterfaceState::GetDevName() const
86 {
87     return devName_;
88 }
89 
GetNetCaps() const90 const std::set<NetCap> &DevInterfaceState::GetNetCaps() const
91 {
92     return netCaps_;
93 }
94 
GetNetCaps()95 std::set<NetCap> DevInterfaceState::GetNetCaps()
96 {
97     return netCaps_;
98 }
99 
GetLinkUp() const100 bool DevInterfaceState::GetLinkUp() const
101 {
102     return linkUp_;
103 }
104 
GetLinkInfo() const105 sptr<NetLinkInfo> DevInterfaceState::GetLinkInfo() const
106 {
107     return linkInfo_;
108 }
109 
GetIfcfg() const110 sptr<InterfaceConfiguration> DevInterfaceState::GetIfcfg() const
111 {
112     return ifCfg_;
113 }
114 
IsLanIface()115 bool DevInterfaceState::IsLanIface()
116 {
117     if (ifCfg_ == nullptr) {
118         return false;
119     }
120     if (ifCfg_->mode_ == LAN_STATIC || ifCfg_->mode_ == LAN_DHCP) {
121         return true;
122     }
123     return false;
124 }
125 
GetIPSetMode() const126 IPSetMode DevInterfaceState::GetIPSetMode() const
127 {
128     if (ifCfg_ == nullptr) {
129         return IPSetMode::STATIC;
130     }
131     return ifCfg_->mode_;
132 }
133 
GetDhcpReqState() const134 bool DevInterfaceState::GetDhcpReqState() const
135 {
136     return dhcpReqState_;
137 }
138 
RemoteRegisterNetSupplier()139 void DevInterfaceState::RemoteRegisterNetSupplier()
140 {
141     if (connLinkState_ == UNREGISTERED) {
142         if (netCaps_.empty()) {
143             netCaps_.insert(NET_CAPABILITY_INTERNET);
144         }
145         int32_t result =
146             NetManagerCenter::GetInstance().RegisterNetSupplier(bearerType_, devName_, netCaps_, netSupplier_);
147         if (result == NETMANAGER_SUCCESS) {
148             connLinkState_ = REGISTERED;
149         }
150         NETMGR_EXT_LOG_D("DevInterfaceCfg RemoteRegisterNetSupplier netSupplier_[%{public}d]", netSupplier_);
151     }
152 }
153 
RemoteUnregisterNetSupplier()154 void DevInterfaceState::RemoteUnregisterNetSupplier()
155 {
156     if (connLinkState_ == UNREGISTERED) {
157         return;
158     }
159     int ret = NetManagerCenter::GetInstance().UnregisterNetSupplier(netSupplier_);
160     if (ret == NETMANAGER_SUCCESS) {
161         connLinkState_ = UNREGISTERED;
162         netSupplier_ = 0;
163     }
164 }
165 
RemoteUpdateNetLinkInfo()166 void DevInterfaceState::RemoteUpdateNetLinkInfo()
167 {
168     if (connLinkState_ == LINK_UNAVAILABLE) {
169         NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetLinkInfo regState_:LINK_UNAVAILABLE");
170         return;
171     }
172     if (linkInfo_ == nullptr) {
173         NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetLinkInfo linkInfo_ is nullptr");
174         return;
175     }
176     auto newNetLinkinfo = linkInfo_;
177     for (auto &netAddr: newNetLinkinfo->netAddrList_) {
178         if (netAddr.family_ == AF_INET) {
179             netAddr.family_ = INetAddr::IpType::IPV4;
180         } else if (netAddr.family_ == AF_INET6) {
181             netAddr.family_ = INetAddr::IpType::IPV6;
182         }
183     }
184     NetManagerCenter::GetInstance().UpdateNetLinkInfo(netSupplier_, newNetLinkinfo);
185 }
186 
RemoteUpdateNetSupplierInfo()187 void DevInterfaceState::RemoteUpdateNetSupplierInfo()
188 {
189     if (connLinkState_ == UNREGISTERED) {
190         NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetSupplierInfo regState_:UNREGISTERED");
191         return;
192     }
193     if (netSupplierInfo_ == nullptr) {
194         NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetSupplierInfo netSupplierInfo_ is nullptr");
195         return;
196     }
197     UpdateSupplierAvailable();
198     NetManagerCenter::GetInstance().UpdateNetSupplierInfo(netSupplier_, netSupplierInfo_);
199 }
200 
UpdateNetHttpProxy(const HttpProxy & httpProxy)201 void DevInterfaceState::UpdateNetHttpProxy(const HttpProxy &httpProxy)
202 {
203     if (httpProxy == ifCfg_->httpProxy_) {
204         NETMGR_EXT_LOG_E("The currently set http proxy is the same as the entered http proxy");
205         return;
206     }
207     ifCfg_->httpProxy_ = httpProxy;
208     if (connLinkState_ == LINK_AVAILABLE) {
209         if (linkInfo_ == nullptr) {
210             NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
211             return;
212         }
213         linkInfo_->httpProxy_ = httpProxy;
214         RemoteUpdateNetLinkInfo();
215     }
216 }
217 
UpdateLinkInfo()218 void DevInterfaceState::UpdateLinkInfo()
219 {
220     if (ifCfg_ == nullptr || ifCfg_->mode_ != STATIC) {
221         return;
222     }
223     if (linkInfo_ == nullptr) {
224         linkInfo_ = new (std::nothrow) NetLinkInfo();
225         if (linkInfo_ == nullptr) {
226             NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
227             return;
228         }
229     }
230     std::list<INetAddr>().swap(linkInfo_->netAddrList_);
231     std::list<Route>().swap(linkInfo_->routeList_);
232     std::list<INetAddr>().swap(linkInfo_->dnsList_);
233     linkInfo_->ifaceName_ = devName_;
234     for (const auto &ipAddr : ifCfg_->ipStatic_.ipAddrList_) {
235         linkInfo_->netAddrList_.push_back(ipAddr);
236     }
237 
238     for (const auto &netAddr : ifCfg_->ipStatic_.routeList_) {
239         Route route;
240         route.iface_ = devName_;
241         route.destination_ = netAddr;
242         GetTargetNetAddrWithSameFamily(netAddr.address_, ifCfg_->ipStatic_.gatewayList_, route.gateway_);
243         linkInfo_->routeList_.push_back(route);
244     }
245     CreateLocalRoute(devName_, ifCfg_->ipStatic_.ipAddrList_, ifCfg_->ipStatic_.netMaskList_);
246 
247     for (auto dnsServer : ifCfg_->ipStatic_.dnsServers_) {
248         linkInfo_->dnsList_.push_back(dnsServer);
249     }
250     linkInfo_->httpProxy_ = ifCfg_->httpProxy_;
251 }
252 
UpdateLanLinkInfo()253 void DevInterfaceState::UpdateLanLinkInfo()
254 {
255     if (ifCfg_ == nullptr || ifCfg_->mode_ != LAN_STATIC) {
256         return;
257     }
258     if (linkInfo_ == nullptr) {
259         linkInfo_ = new (std::nothrow) NetLinkInfo();
260         if (linkInfo_ == nullptr) {
261             NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
262             return;
263         }
264     }
265     std::list<INetAddr>().swap(linkInfo_->netAddrList_);
266     std::list<Route>().swap(linkInfo_->routeList_);
267     linkInfo_->ifaceName_ = devName_;
268     for (const auto &ipAddr : ifCfg_->ipStatic_.ipAddrList_) {
269         linkInfo_->netAddrList_.push_back(ipAddr);
270     }
271 
272     for (const auto &netAddr : ifCfg_->ipStatic_.routeList_) {
273         Route route;
274         route.iface_ = devName_;
275         route.destination_ = netAddr;
276         GetRoutePrefixlen(netAddr.address_, ifCfg_->ipStatic_.netMaskList_, route.destination_);
277         GetTargetNetAddrWithSameFamily(netAddr.address_, ifCfg_->ipStatic_.gatewayList_, route.gateway_);
278         linkInfo_->routeList_.push_back(route);
279     }
280 }
281 
UpdateLanLinkInfo(const sptr<StaticConfiguration> & config)282 void DevInterfaceState::UpdateLanLinkInfo(const sptr<StaticConfiguration> &config)
283 {
284     if (config == nullptr) {
285         NETMGR_EXT_LOG_E("config is nullptr");
286         return;
287     }
288     if (linkInfo_ == nullptr) {
289         linkInfo_ = new (std::nothrow) NetLinkInfo();
290         if (linkInfo_ == nullptr) {
291             NETMGR_EXT_LOG_E("NetLinkInfo new failed");
292             return;
293         }
294     }
295     std::list<INetAddr>().swap(linkInfo_->netAddrList_);
296     std::list<Route>().swap(linkInfo_->routeList_);
297     linkInfo_->ifaceName_ = devName_;
298     for (const auto &ipAddr : config->ipAddrList_) {
299         linkInfo_->netAddrList_.push_back(ipAddr);
300     }
301 
302     for (const auto &routeAddr : config->routeList_) {
303         Route routeStc;
304         routeStc.iface_ = devName_;
305         routeStc.destination_ = routeAddr;
306         GetRoutePrefixlen(routeAddr.address_, config->netMaskList_, routeStc.destination_);
307         GetTargetNetAddrWithSameFamily(routeAddr.address_, config->gatewayList_, routeStc.gateway_);
308         linkInfo_->routeList_.push_back(routeStc);
309     }
310 }
311 
UpdateLinkInfo(const sptr<StaticConfiguration> & config)312 void DevInterfaceState::UpdateLinkInfo(const sptr<StaticConfiguration> &config)
313 {
314     if (config == nullptr) {
315         NETMGR_EXT_LOG_E("config is nullptr");
316         return;
317     }
318     if (linkInfo_ == nullptr) {
319         linkInfo_ = new (std::nothrow) NetLinkInfo();
320         if (linkInfo_ == nullptr) {
321             NETMGR_EXT_LOG_E("NetLinkInfo new failed");
322             return;
323         }
324     }
325 
326     std::list<INetAddr>().swap(linkInfo_->netAddrList_);
327     std::list<Route>().swap(linkInfo_->routeList_);
328     std::list<INetAddr>().swap(linkInfo_->dnsList_);
329     linkInfo_->ifaceName_ = devName_;
330     for (const auto &ipAddr : config->ipAddrList_) {
331         linkInfo_->netAddrList_.push_back(ipAddr);
332     }
333 
334     for (const auto &routeAddr : config->routeList_) {
335         Route routeStc;
336         routeStc.iface_ = devName_;
337         routeStc.destination_ = routeAddr;
338         GetTargetNetAddrWithSameFamily(routeAddr.address_, config->gatewayList_, routeStc.gateway_);
339         linkInfo_->routeList_.push_back(routeStc);
340     }
341     CreateLocalRoute(devName_, config->ipAddrList_, config->netMaskList_);
342 
343     for (auto dns : config->dnsServers_) {
344         linkInfo_->dnsList_.push_back(dns);
345     }
346     if (ifCfg_) {
347         linkInfo_->httpProxy_ = ifCfg_->httpProxy_;
348     }
349 }
350 
UpdateSupplierAvailable()351 void DevInterfaceState::UpdateSupplierAvailable()
352 {
353     if (netSupplierInfo_ == nullptr) {
354         return;
355     }
356     netSupplierInfo_->isAvailable_ = linkUp_;
357     connLinkState_ = linkUp_ ? LINK_AVAILABLE : LINK_UNAVAILABLE;
358 }
359 
CreateLocalRoute(const std::string & iface,const std::vector<INetAddr> & ipAddrList,const std::vector<INetAddr> & netMaskList)360 void DevInterfaceState::CreateLocalRoute(const std::string &iface, const std::vector<INetAddr> &ipAddrList,
361                                          const std::vector<INetAddr> &netMaskList)
362 {
363     if (linkInfo_ == nullptr) {
364         NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
365         return;
366     }
367 
368     for (const auto &ipAddr : ipAddrList) {
369         auto family = CommonUtils::GetAddrFamily(ipAddr.address_);
370         std::string routeAddr = (family == AF_INET6) ? CommonUtils::GetIpv6Prefix(ipAddr.address_, ipAddr.prefixlen_)
371                                                      : GetIpv4Prefix(ipAddr.address_, netMaskList);
372         Route localRoute;
373         localRoute.iface_ = iface;
374         localRoute.destination_.type_ = family;
375         localRoute.destination_.address_ = routeAddr;
376         localRoute.destination_.prefixlen_ = ipAddr.prefixlen_;
377         localRoute.gateway_.address_ = (family == AF_INET) ? DEFAULT_ROUTE_ADDR : "";
378         linkInfo_->routeList_.push_back(localRoute);
379     }
380 }
381 
GetIpv4Prefix(const std::string & ipv4Addr,const std::vector<INetAddr> & netMaskList)382 std::string DevInterfaceState::GetIpv4Prefix(const std::string &ipv4Addr, const std::vector<INetAddr> &netMaskList)
383 {
384     INetAddr maskAddr;
385     GetTargetNetAddrWithSameFamily(ipv4Addr, netMaskList, maskAddr);
386     uint32_t ipInt = CommonUtils::ConvertIpv4Address(ipv4Addr);
387     uint32_t maskInt = CommonUtils::ConvertIpv4Address(maskAddr.address_);
388     return CommonUtils::ConvertIpv4Address(ipInt & maskInt);
389 }
390 
GetTargetNetAddrWithSameFamily(const std::string & bySrcAddr,const std::vector<INetAddr> & fromAddrList,INetAddr & targetNetAddr)391 void DevInterfaceState::GetTargetNetAddrWithSameFamily(const std::string &bySrcAddr,
392                                                        const std::vector<INetAddr> &fromAddrList,
393                                                        INetAddr &targetNetAddr)
394 {
395     auto family = CommonUtils::GetAddrFamily(bySrcAddr);
396     for (const auto &addr : fromAddrList) {
397         if (family != CommonUtils::GetAddrFamily(addr.address_)) {
398             continue;
399         }
400         targetNetAddr = addr;
401         return;
402     }
403 }
404 
GetRoutePrefixlen(const std::string & bySrcAddr,const std::vector<INetAddr> & fromAddrList,INetAddr & targetNetAddr)405 void DevInterfaceState::GetRoutePrefixlen(const std::string &bySrcAddr,
406                                           const std::vector<INetAddr> &fromAddrList,
407                                           INetAddr &targetNetAddr)
408 {
409     auto route_family = CommonUtils::GetAddrFamily(bySrcAddr);
410     for (const auto &netMask : fromAddrList) {
411         auto route_mask_family = CommonUtils::GetAddrFamily(netMask.address_);
412         if (route_family == route_mask_family) {
413             targetNetAddr.prefixlen_ = (route_family == AF_INET6)
414                 ? static_cast<uint32_t>(CommonUtils::Ipv6PrefixLen(netMask.address_))
415                 : static_cast<uint32_t>(CommonUtils::Ipv4PrefixLen(netMask.address_));
416         }
417     }
418 }
419 
GetDumpInfo(std::string & info)420 void DevInterfaceState::GetDumpInfo(std::string &info)
421 {
422     const std::string TAB = "  ";
423     std::list<std::string> dumpInfo = {
424         "DevName: " + devName_,
425         "ConnLinkState: " + std::to_string(connLinkState_),
426         "LinkUp: " + std::to_string(linkUp_),
427         "DHCPReqState: " + std::to_string(dhcpReqState_),
428     };
429     std::string data = "DevInterfaceState: \n";
430     std::for_each(dumpInfo.begin(), dumpInfo.end(),
431                   [&data, &TAB](const auto &msg) { data.append(TAB + TAB + msg + "\n"); });
432     if (linkInfo_ != nullptr) {
433         data.append(linkInfo_->ToString(TAB) + "\n");
434     }
435     if (netSupplierInfo_ != nullptr) {
436         data.append(netSupplierInfo_->ToString(TAB) + "\n");
437     }
438     if (ifCfg_ != nullptr) {
439         data.append("\n" + TAB + TAB + "InterfaceConfig: \n" + TAB + TAB + TAB +
440                     "Mode: " + std::to_string(ifCfg_->mode_) + "\n");
441         data.append("\nConfig: \n");
442         data.append(TAB + TAB + "IpAddr: ");
443         std::for_each(ifCfg_->ipStatic_.ipAddrList_.begin(), ifCfg_->ipStatic_.ipAddrList_.end(),
444                       [&data, &TAB](const auto &ipAddr) { data.append(TAB + TAB + ipAddr.ToString(TAB)); });
445 
446         data.append("\n" + TAB + TAB + "Route: ");
447         std::for_each(ifCfg_->ipStatic_.routeList_.begin(), ifCfg_->ipStatic_.routeList_.end(),
448                       [&data, &TAB](const auto &routeAddr) { data.append(TAB + TAB + routeAddr.ToString(TAB)); });
449 
450         data.append("\n" + TAB + TAB + "GateWay: ");
451         std::for_each(ifCfg_->ipStatic_.gatewayList_.begin(), ifCfg_->ipStatic_.gatewayList_.end(),
452                       [&data, &TAB](const auto &gateway) { data.append(TAB + TAB + gateway.ToString(TAB)); });
453 
454         data.append("\n" + TAB + TAB + "NetMask: ");
455         std::for_each(ifCfg_->ipStatic_.netMaskList_.begin(), ifCfg_->ipStatic_.netMaskList_.end(),
456                       [&data, &TAB](const auto &netMask) { data.append(TAB + TAB + netMask.ToString(TAB)); });
457 
458         data.append("\n" + TAB + TAB + "DNSServers: ");
459         std::for_each(ifCfg_->ipStatic_.dnsServers_.begin(), ifCfg_->ipStatic_.dnsServers_.end(),
460                       [&data, &TAB](const auto &server) { data.append(TAB + TAB + server.ToString(TAB)); });
461 
462         data.append("\n" + TAB + TAB + "Domain: " + ifCfg_->ipStatic_.domain_ + "\n" + TAB + TAB + "NetCaps: {");
463         std::for_each(netCaps_.begin(), netCaps_.end(),
464                       [&data, &TAB](const auto &cap) { data.append(std::to_string(cap) + ", "); });
465         data.append("}\n");
466     }
467     data.append(TAB + TAB + "BearerType :" + std::to_string(bearerType_) + "\n");
468     info.append(data);
469 }
470 } // namespace NetManagerStandard
471 } // namespace OHOS
472