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
SetDhcpReqState(bool dhcpReqState)72 void DevInterfaceState::SetDhcpReqState(bool dhcpReqState)
73 {
74 dhcpReqState_ = dhcpReqState;
75 }
76
GetDevName() const77 std::string DevInterfaceState::GetDevName() const
78 {
79 return devName_;
80 }
81
GetNetCaps() const82 const std::set<NetCap> &DevInterfaceState::GetNetCaps() const
83 {
84 return netCaps_;
85 }
86
GetNetCaps()87 std::set<NetCap> DevInterfaceState::GetNetCaps()
88 {
89 return netCaps_;
90 }
91
GetLinkUp() const92 bool DevInterfaceState::GetLinkUp() const
93 {
94 return linkUp_;
95 }
96
GetLinkInfo() const97 sptr<NetLinkInfo> DevInterfaceState::GetLinkInfo() const
98 {
99 return linkInfo_;
100 }
101
GetIfcfg() const102 sptr<InterfaceConfiguration> DevInterfaceState::GetIfcfg() const
103 {
104 return ifCfg_;
105 }
106
GetIPSetMode() const107 IPSetMode DevInterfaceState::GetIPSetMode() const
108 {
109 if (ifCfg_ == nullptr) {
110 return IPSetMode::STATIC;
111 }
112 return ifCfg_->mode_;
113 }
114
GetDhcpReqState() const115 bool DevInterfaceState::GetDhcpReqState() const
116 {
117 return dhcpReqState_;
118 }
119
RemoteRegisterNetSupplier()120 void DevInterfaceState::RemoteRegisterNetSupplier()
121 {
122 if (connLinkState_ == UNREGISTERED) {
123 if (netCaps_.empty()) {
124 netCaps_.insert(NET_CAPABILITY_INTERNET);
125 }
126 int32_t result =
127 NetManagerCenter::GetInstance().RegisterNetSupplier(bearerType_, devName_, netCaps_, netSupplier_);
128 if (result == NETMANAGER_SUCCESS) {
129 connLinkState_ = REGISTERED;
130 }
131 NETMGR_EXT_LOG_D("DevInterfaceCfg RemoteRegisterNetSupplier netSupplier_[%{public}d]", netSupplier_);
132 }
133 }
134
RemoteUnregisterNetSupplier()135 void DevInterfaceState::RemoteUnregisterNetSupplier()
136 {
137 if (connLinkState_ == UNREGISTERED) {
138 return;
139 }
140 int ret = NetManagerCenter::GetInstance().UnregisterNetSupplier(netSupplier_);
141 if (ret == NETMANAGER_SUCCESS) {
142 connLinkState_ = UNREGISTERED;
143 netSupplier_ = 0;
144 }
145 }
146
RemoteUpdateNetLinkInfo()147 void DevInterfaceState::RemoteUpdateNetLinkInfo()
148 {
149 if (connLinkState_ == LINK_UNAVAILABLE) {
150 NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetLinkInfo regState_:LINK_UNAVAILABLE");
151 return;
152 }
153 if (linkInfo_ == nullptr) {
154 NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetLinkInfo linkInfo_ is nullptr");
155 return;
156 }
157 NetManagerCenter::GetInstance().UpdateNetLinkInfo(netSupplier_, linkInfo_);
158 }
159
RemoteUpdateNetSupplierInfo()160 void DevInterfaceState::RemoteUpdateNetSupplierInfo()
161 {
162 if (connLinkState_ == UNREGISTERED) {
163 NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetSupplierInfo regState_:UNREGISTERED");
164 return;
165 }
166 if (netSupplierInfo_ == nullptr) {
167 NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetSupplierInfo netSupplierInfo_ is nullptr");
168 return;
169 }
170 UpdateSupplierAvailable();
171 NetManagerCenter::GetInstance().UpdateNetSupplierInfo(netSupplier_, netSupplierInfo_);
172 }
173
UpdateNetHttpProxy(const HttpProxy & httpProxy)174 void DevInterfaceState::UpdateNetHttpProxy(const HttpProxy &httpProxy)
175 {
176 if (httpProxy == ifCfg_->httpProxy_) {
177 NETMGR_EXT_LOG_E("The currently set http proxy is the same as the entered http proxy");
178 return;
179 }
180 ifCfg_->httpProxy_ = httpProxy;
181 if (connLinkState_ == LINK_AVAILABLE) {
182 if (linkInfo_ == nullptr) {
183 NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
184 return;
185 }
186 linkInfo_->httpProxy_ = httpProxy;
187 RemoteUpdateNetLinkInfo();
188 }
189 }
190
UpdateLinkInfo()191 void DevInterfaceState::UpdateLinkInfo()
192 {
193 if (ifCfg_ == nullptr || ifCfg_->mode_ != STATIC) {
194 return;
195 }
196 if (linkInfo_ == nullptr) {
197 linkInfo_ = new (std::nothrow) NetLinkInfo();
198 if (linkInfo_ == nullptr) {
199 NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
200 return;
201 }
202 }
203 std::list<INetAddr>().swap(linkInfo_->netAddrList_);
204 std::list<Route>().swap(linkInfo_->routeList_);
205 std::list<INetAddr>().swap(linkInfo_->dnsList_);
206 linkInfo_->ifaceName_ = devName_;
207 for (const auto &ipAddr : ifCfg_->ipStatic_.ipAddrList_) {
208 linkInfo_->netAddrList_.push_back(ipAddr);
209 }
210
211 for (const auto &netAddr : ifCfg_->ipStatic_.routeList_) {
212 Route route;
213 route.iface_ = devName_;
214 route.destination_ = netAddr;
215 GetTargetNetAddrWithSameFamily(netAddr.address_, ifCfg_->ipStatic_.gatewayList_, route.gateway_);
216 linkInfo_->routeList_.push_back(route);
217 }
218 CreateLocalRoute(devName_, ifCfg_->ipStatic_.ipAddrList_, ifCfg_->ipStatic_.netMaskList_);
219
220 for (auto dnsServer : ifCfg_->ipStatic_.dnsServers_) {
221 linkInfo_->dnsList_.push_back(dnsServer);
222 }
223 linkInfo_->httpProxy_ = ifCfg_->httpProxy_;
224 }
225
UpdateLinkInfo(const sptr<StaticConfiguration> & config)226 void DevInterfaceState::UpdateLinkInfo(const sptr<StaticConfiguration> &config)
227 {
228 if (config == nullptr) {
229 NETMGR_EXT_LOG_E("config is nullptr");
230 return;
231 }
232 if (linkInfo_ == nullptr) {
233 linkInfo_ = new (std::nothrow) NetLinkInfo();
234 if (linkInfo_ == nullptr) {
235 NETMGR_EXT_LOG_E("NetLinkInfo new failed");
236 }
237 }
238
239 std::list<INetAddr>().swap(linkInfo_->netAddrList_);
240 std::list<Route>().swap(linkInfo_->routeList_);
241 std::list<INetAddr>().swap(linkInfo_->dnsList_);
242 linkInfo_->ifaceName_ = devName_;
243 for (const auto &ipAddr : config->ipAddrList_) {
244 linkInfo_->netAddrList_.push_back(ipAddr);
245 }
246
247 for (const auto &routeAddr : config->routeList_) {
248 Route routeStc;
249 routeStc.iface_ = devName_;
250 routeStc.destination_ = routeAddr;
251 GetTargetNetAddrWithSameFamily(routeAddr.address_, config->gatewayList_, routeStc.gateway_);
252 linkInfo_->routeList_.push_back(routeStc);
253 }
254 CreateLocalRoute(devName_, config->ipAddrList_, config->netMaskList_);
255
256 for (auto dns : config->dnsServers_) {
257 linkInfo_->dnsList_.push_back(dns);
258 }
259 if (ifCfg_) {
260 linkInfo_->httpProxy_ = ifCfg_->httpProxy_;
261 }
262 }
263
UpdateSupplierAvailable()264 void DevInterfaceState::UpdateSupplierAvailable()
265 {
266 netSupplierInfo_->isAvailable_ = linkUp_;
267 connLinkState_ = linkUp_ ? LINK_AVAILABLE : LINK_UNAVAILABLE;
268 }
269
CreateLocalRoute(const std::string & iface,const std::vector<INetAddr> & ipAddrList,const std::vector<INetAddr> & netMaskList)270 void DevInterfaceState::CreateLocalRoute(const std::string &iface, const std::vector<INetAddr> &ipAddrList,
271 const std::vector<INetAddr> &netMaskList)
272 {
273 if (linkInfo_ == nullptr) {
274 NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
275 return;
276 }
277
278 for (const auto &ipAddr : ipAddrList) {
279 auto family = CommonUtils::GetAddrFamily(ipAddr.address_);
280 std::string routeAddr = (family == AF_INET6) ? CommonUtils::GetIpv6Prefix(ipAddr.address_, ipAddr.prefixlen_)
281 : GetIpv4Prefix(ipAddr.address_, netMaskList);
282 Route localRoute;
283 localRoute.iface_ = iface;
284 localRoute.destination_.type_ = family;
285 localRoute.destination_.address_ = routeAddr;
286 localRoute.destination_.prefixlen_ = ipAddr.prefixlen_;
287 localRoute.gateway_.address_ = (family == AF_INET) ? DEFAULT_ROUTE_ADDR : "";
288 linkInfo_->routeList_.push_back(localRoute);
289 }
290 }
291
GetIpv4Prefix(const std::string & ipv4Addr,const std::vector<INetAddr> & netMaskList)292 std::string DevInterfaceState::GetIpv4Prefix(const std::string &ipv4Addr, const std::vector<INetAddr> &netMaskList)
293 {
294 INetAddr maskAddr;
295 GetTargetNetAddrWithSameFamily(ipv4Addr, netMaskList, maskAddr);
296 uint32_t ipInt = CommonUtils::ConvertIpv4Address(ipv4Addr);
297 uint32_t maskInt = CommonUtils::ConvertIpv4Address(maskAddr.address_);
298 return CommonUtils::ConvertIpv4Address(ipInt & maskInt);
299 }
300
GetTargetNetAddrWithSameFamily(const std::string & bySrcAddr,const std::vector<INetAddr> & fromAddrList,INetAddr & targetNetAddr)301 void DevInterfaceState::GetTargetNetAddrWithSameFamily(const std::string &bySrcAddr,
302 const std::vector<INetAddr> &fromAddrList,
303 INetAddr &targetNetAddr)
304 {
305 auto family = CommonUtils::GetAddrFamily(bySrcAddr);
306 for (const auto &addr : fromAddrList) {
307 if (family != CommonUtils::GetAddrFamily(addr.address_)) {
308 continue;
309 }
310 targetNetAddr = addr;
311 return;
312 }
313 }
314
GetDumpInfo(std::string & info)315 void DevInterfaceState::GetDumpInfo(std::string &info)
316 {
317 const std::string TAB = " ";
318 std::list<std::string> dumpInfo = {
319 "DevName: " + devName_,
320 "ConnLinkState: " + std::to_string(connLinkState_),
321 "LinkUp: " + std::to_string(linkUp_),
322 "DHCPReqState: " + std::to_string(dhcpReqState_),
323 };
324 std::string data = "DevInterfaceState: \n";
325 std::for_each(dumpInfo.begin(), dumpInfo.end(),
326 [&data, &TAB](const auto &msg) { data.append(TAB + TAB + msg + "\n"); });
327 if (linkInfo_ != nullptr) {
328 data.append(linkInfo_->ToString(TAB) + "\n");
329 }
330 if (netSupplierInfo_ != nullptr) {
331 data.append(netSupplierInfo_->ToString(TAB) + "\n");
332 }
333 if (ifCfg_ != nullptr) {
334 data.append("\n" + TAB + TAB + "InterfaceConfig: \n" + TAB + TAB + TAB +
335 "Mode: " + std::to_string(ifCfg_->mode_) + "\n");
336 data.append("\nConfig: \n");
337 data.append(TAB + TAB + "IpAddr: ");
338 std::for_each(ifCfg_->ipStatic_.ipAddrList_.begin(), ifCfg_->ipStatic_.ipAddrList_.end(),
339 [&data, &TAB](const auto &ipAddr) { data.append(TAB + TAB + ipAddr.ToString(TAB)); });
340
341 data.append("\n" + TAB + TAB + "Route: ");
342 std::for_each(ifCfg_->ipStatic_.routeList_.begin(), ifCfg_->ipStatic_.routeList_.end(),
343 [&data, &TAB](const auto &routeAddr) { data.append(TAB + TAB + routeAddr.ToString(TAB)); });
344
345 data.append("\n" + TAB + TAB + "GateWay: ");
346 std::for_each(ifCfg_->ipStatic_.gatewayList_.begin(), ifCfg_->ipStatic_.gatewayList_.end(),
347 [&data, &TAB](const auto &gateway) { data.append(TAB + TAB + gateway.ToString(TAB)); });
348
349 data.append("\n" + TAB + TAB + "NetMask: ");
350 std::for_each(ifCfg_->ipStatic_.netMaskList_.begin(), ifCfg_->ipStatic_.netMaskList_.end(),
351 [&data, &TAB](const auto &netMask) { data.append(TAB + TAB + netMask.ToString(TAB)); });
352
353 data.append("\n" + TAB + TAB + "DNSServers: ");
354 std::for_each(ifCfg_->ipStatic_.dnsServers_.begin(), ifCfg_->ipStatic_.dnsServers_.end(),
355 [&data, &TAB](const auto &server) { data.append(TAB + TAB + server.ToString(TAB)); });
356
357 data.append("\n" + TAB + TAB + "Domain: " + ifCfg_->ipStatic_.domain_ + "\n" + TAB + TAB + "NetCaps: {");
358 std::for_each(netCaps_.begin(), netCaps_.end(),
359 [&data, &TAB](const auto &cap) { data.append(std::to_string(cap) + ", "); });
360 data.append("}\n");
361 }
362 data.append(TAB + TAB + "BearerType :" + std::to_string(bearerType_) + "\n");
363 info.append(data);
364 }
365 } // namespace NetManagerStandard
366 } // namespace OHOS
367