1 /*
2 * Copyright (c) 2021-2022 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 }
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
UpdateLinkInfo()174 void DevInterfaceState::UpdateLinkInfo()
175 {
176 if (ifCfg_ == nullptr || ifCfg_->mode_ != STATIC) {
177 return;
178 }
179 if (linkInfo_ == nullptr) {
180 linkInfo_ = new (std::nothrow) NetLinkInfo();
181 if (linkInfo_ == nullptr) {
182 NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
183 return;
184 }
185 }
186 std::list<INetAddr>().swap(linkInfo_->netAddrList_);
187 std::list<Route>().swap(linkInfo_->routeList_);
188 std::list<INetAddr>().swap(linkInfo_->dnsList_);
189 linkInfo_->ifaceName_ = devName_;
190 linkInfo_->netAddrList_.push_back(ifCfg_->ipStatic_.ipAddr_);
191 Route route;
192 route.iface_ = devName_;
193 route.destination_ = ifCfg_->ipStatic_.route_;
194 route.gateway_ = ifCfg_->ipStatic_.gateway_;
195 linkInfo_->routeList_.push_back(route);
196 const auto &routeLocal =
197 CreateLocalRoute(devName_, ifCfg_->ipStatic_.ipAddr_.address_, ifCfg_->ipStatic_.netMask_.address_);
198 linkInfo_->routeList_.push_back(routeLocal);
199 for (auto dnsServer : ifCfg_->ipStatic_.dnsServers_) {
200 linkInfo_->dnsList_.push_back(dnsServer);
201 }
202 }
UpdateLinkInfo(const INetAddr & ipAddr,const INetAddr & netMask,const INetAddr & gateWay,const INetAddr & route,const INetAddr & dns1,const INetAddr & dns2)203 void DevInterfaceState::UpdateLinkInfo(const INetAddr &ipAddr, const INetAddr &netMask, const INetAddr &gateWay,
204 const INetAddr &route, const INetAddr &dns1, const INetAddr &dns2)
205 {
206 if (linkInfo_ == nullptr) {
207 linkInfo_ = new (std::nothrow) NetLinkInfo();
208 if (linkInfo_ == nullptr) {
209 NETMGR_EXT_LOG_E("NetLinkInfo new failed");
210 }
211 }
212 std::list<INetAddr>().swap(linkInfo_->netAddrList_);
213 std::list<Route>().swap(linkInfo_->routeList_);
214 std::list<INetAddr>().swap(linkInfo_->dnsList_);
215 linkInfo_->ifaceName_ = devName_;
216 linkInfo_->netAddrList_.push_back(ipAddr);
217 Route routeStc;
218 routeStc.iface_ = devName_;
219 routeStc.destination_ = route;
220 routeStc.gateway_ = gateWay;
221 linkInfo_->routeList_.push_back(routeStc);
222 Route routeLocal = CreateLocalRoute(devName_, ipAddr.address_, netMask.address_);
223 linkInfo_->routeList_.push_back(routeLocal);
224 linkInfo_->dnsList_.push_back(dns1);
225 linkInfo_->dnsList_.push_back(dns2);
226 }
227
UpdateSupplierAvailable()228 void DevInterfaceState::UpdateSupplierAvailable()
229 {
230 netSupplierInfo_->isAvailable_ = linkUp_;
231 connLinkState_ = linkUp_ ? LINK_AVAILABLE : LINK_UNAVAILABLE;
232 }
233
CreateLocalRoute(const std::string & iface,const std::string & ipAddr,const std::string & maskAddr)234 Route DevInterfaceState::CreateLocalRoute(const std::string &iface, const std::string &ipAddr,
235 const std::string &maskAddr)
236 {
237 NETMGR_EXT_LOG_D("create local route ipAddr=%{public}s maskAddr=%{public}s.",
238 CommonUtils::ToAnonymousIp(ipAddr).c_str(), maskAddr.c_str());
239 Route localRoute;
240 int prefixLength = CommonUtils::GetMaskLength(maskAddr);
241 uint32_t ipInt = CommonUtils::ConvertIpv4Address(ipAddr);
242 uint32_t maskInt = CommonUtils::ConvertIpv4Address(maskAddr);
243 std::string strLocalRoute = CommonUtils::ConvertIpv4Address(ipInt & maskInt);
244 localRoute.iface_ = iface;
245 localRoute.destination_.type_ = INetAddr::IPV4;
246 localRoute.destination_.address_ = strLocalRoute;
247 localRoute.destination_.prefixlen_ = prefixLength;
248 localRoute.gateway_.address_ = DEFAULT_ROUTE_ADDR;
249 return localRoute;
250 }
251
GetDumpInfo(std::string & info)252 void DevInterfaceState::GetDumpInfo(std::string &info)
253 {
254 const std::string TAB = " ";
255 std::list<std::string> dumpInfo = {
256 "DevName: " + devName_,
257 "ConnLinkState: " + std::to_string(connLinkState_),
258 "LinkUp: " + std::to_string(linkUp_),
259 "DHCPReqState: " + std::to_string(dhcpReqState_),
260 };
261 std::string data = "DevInterfaceState: ";
262 std::for_each(dumpInfo.begin(), dumpInfo.end(),
263 [&data, &TAB](const auto &msg) { data.append(TAB + TAB + msg + "\n"); });
264 if (linkInfo_ != nullptr) {
265 data.append(linkInfo_->ToString(TAB));
266 }
267 if (netSupplierInfo_ != nullptr) {
268 data.append(netSupplierInfo_->ToString(TAB));
269 }
270 if (ifCfg_ != nullptr) {
271 data.append(TAB + TAB + "InterfaceConfig: \n" + TAB + TAB + TAB + "Mode: " + std::to_string(ifCfg_->mode_) +
272 "\n");
273 data.append("\nConfig: \n");
274 data.append(TAB + TAB + "IpAddr:" + ifCfg_->ipStatic_.ipAddr_.ToString(TAB) + "\n" + TAB + TAB +
275 "Route: " + ifCfg_->ipStatic_.route_.ToString(TAB) + "\n" + TAB + TAB +
276 "GateWay: " + ifCfg_->ipStatic_.gateway_.ToString(TAB) + "\n" + TAB + TAB +
277 "NetMask: " + ifCfg_->ipStatic_.netMask_.ToString(TAB) + "\n" + TAB + TAB + "DNSServers: \n");
278 std::for_each(ifCfg_->ipStatic_.dnsServers_.begin(), ifCfg_->ipStatic_.dnsServers_.end(),
279 [&data, &TAB](const auto &server) { data.append(TAB + TAB + server.ToString(TAB)); });
280 data.append(TAB + TAB + "Domain: " + ifCfg_->ipStatic_.domain_ + "\n" + TAB + TAB + "NetCaps: {");
281 std::for_each(netCaps_.begin(), netCaps_.end(),
282 [&data, &TAB](const auto &cap) { data.append(std::to_string(cap) + ", "); });
283 data.append("}\n");
284 }
285 data.append(TAB + TAB + "BearerType :" + std::to_string(bearerType_) + "\n");
286 info.append(data);
287 }
288 } // namespace NetManagerStandard
289 } // namespace OHOS
290