1 /*
2 * Copyright (C) 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 "wifi_net_agent.h"
17 #include <cinttypes>
18 #include <thread>
19 #include "inet_addr.h"
20 #include "ip_tools.h"
21 #include "iservice_registry.h"
22 #include "netsys_native_service_proxy.h"
23 #include "net_conn_client.h"
24 #include "system_ability_definition.h"
25 #include "wifi_common_util.h"
26 #include "wifi_logger.h"
27 #include "wifi_settings.h"
28
29 DEFINE_WIFILOG_LABEL("WifiNetAgent");
30
31 namespace OHOS {
32 namespace Wifi {
33 using namespace NetManagerStandard;
34
35 WifiNetAgent::WifiNetAgent() = default;
36 WifiNetAgent::~WifiNetAgent() = default;
37
RegisterNetSupplier()38 bool WifiNetAgent::RegisterNetSupplier()
39 {
40 TimeStats timeStats(__func__);
41 WIFI_LOGI("Enter RegisterNetSupplier.");
42 auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
43 if (netManager == nullptr) {
44 WIFI_LOGE("NetConnClient is null");
45 return false;
46 }
47 std::string ident = "wifi";
48 using NetManagerStandard::NetBearType;
49 using NetManagerStandard::NetCap;
50 std::set<NetCap> netCaps {NetCap::NET_CAPABILITY_INTERNET};
51 int32_t result = netManager->RegisterNetSupplier(NetBearType::BEARER_WIFI, ident, netCaps, supplierId);
52 if (result == NETMANAGER_SUCCESS) {
53 WIFI_LOGI("Register NetSupplier successful");
54 return true;
55 }
56 WIFI_LOGI("Register NetSupplier failed");
57 return false;
58 }
59
RegisterNetSupplierCallback(const StaServiceCallback & callback)60 bool WifiNetAgent::RegisterNetSupplierCallback(const StaServiceCallback &callback)
61 {
62 TimeStats timeStats(__func__);
63 WIFI_LOGI("Enter RegisterNetSupplierCallback.");
64 auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
65 if (netManager == nullptr) {
66 WIFI_LOGE("NetConnClient is null");
67 return false;
68 }
69
70 sptr<NetConnCallback> pNetConnCallback = (std::make_unique<NetConnCallback>(callback)).release();
71 if (pNetConnCallback == nullptr) {
72 WIFI_LOGE("pNetConnCallback is null\n");
73 return false;
74 }
75
76 int32_t result = netManager->RegisterNetSupplierCallback(supplierId, pNetConnCallback);
77 if (result == NETMANAGER_SUCCESS) {
78 WIFI_LOGI("Register NetSupplierCallback successful");
79 return true;
80 }
81 WIFI_LOGE("Register NetSupplierCallback failed [%{public}d]", result);
82 return false;
83 }
84
UnregisterNetSupplier()85 void WifiNetAgent::UnregisterNetSupplier()
86 {
87 TimeStats timeStats(__func__);
88 WIFI_LOGI("Enter UnregisterNetSupplier.");
89 auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
90 if (netManager == nullptr) {
91 WIFI_LOGE("NetConnClient is null");
92 return;
93 }
94 int32_t result = netManager->UnregisterNetSupplier(supplierId);
95 WIFI_LOGI("Unregister network result:%{public}d", result);
96 }
97
UpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> & netSupplierInfo)98 void WifiNetAgent::UpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo)
99 {
100 TimeStats timeStats(__func__);
101 WIFI_LOGI("Enter UpdateNetSupplierInfo.");
102 auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
103 if (netManager == nullptr) {
104 WIFI_LOGE("NetConnClient is null");
105 return;
106 }
107
108 int32_t result = netManager->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
109 WIFI_LOGI("Update network result:%{public}d", result);
110 }
111
UpdateNetLinkInfo(const std::string & ip,const std::string & mask,const std::string & gateWay,const std::string & strDns,const std::string & strBakDns)112 void WifiNetAgent::UpdateNetLinkInfo(const std::string &ip, const std::string &mask, const std::string &gateWay,
113 const std::string &strDns, const std::string &strBakDns)
114 {
115 TimeStats timeStats(__func__);
116 WIFI_LOGI("Enter UpdateNetLinkInfo.");
117 auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
118 if (netManager == nullptr) {
119 WIFI_LOGE("NetConnClient is null");
120 return;
121 }
122
123 sptr<NetManagerStandard::NetLinkInfo> netLinkInfo = (std::make_unique<NetManagerStandard::NetLinkInfo>()).release();
124 netLinkInfo->ifaceName_ = "wlan0";
125
126 unsigned int prefixLength = IpTools::GetMaskLength(mask);
127 sptr<NetManagerStandard::INetAddr> netAddr = (std::make_unique<NetManagerStandard::INetAddr>()).release();
128 netAddr->type_ = NetManagerStandard::INetAddr::IPV4;
129 netAddr->family_ = NetManagerStandard::INetAddr::IPV4;
130 netAddr->address_ = ip;
131 netAddr->netMask_ = mask;
132 netAddr->prefixlen_ = prefixLength;
133 netLinkInfo->netAddrList_.push_back(*netAddr);
134
135 sptr<NetManagerStandard::INetAddr> dns = (std::make_unique<NetManagerStandard::INetAddr>()).release();
136 dns->type_ = NetManagerStandard::INetAddr::IPV4;
137 dns->family_ = NetManagerStandard::INetAddr::IPV4;
138 dns->address_ = strDns;
139 netLinkInfo->dnsList_.push_back(*dns);
140 dns->address_ = strBakDns;
141 netLinkInfo->dnsList_.push_back(*dns);
142
143 sptr<NetManagerStandard::Route> route = (std::make_unique<NetManagerStandard::Route>()).release();
144 route->iface_ = "wlan0";
145 route->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
146 route->destination_.address_ = "0.0.0.0";
147 route->gateway_.address_ = gateWay;
148 netLinkInfo->routeList_.push_back(*route);
149
150 sptr<NetManagerStandard::Route> localRoute = (std::make_unique<NetManagerStandard::Route>()).release();
151 unsigned int ipInt = IpTools::ConvertIpv4Address(ip);
152 unsigned int maskInt = IpTools::ConvertIpv4Address(mask);
153 std::string strLocalRoute = IpTools::ConvertIpv4Address(ipInt & maskInt);
154 localRoute->iface_ = route->iface_;
155 localRoute->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
156 localRoute->destination_.address_ = strLocalRoute;
157 localRoute->destination_.prefixlen_ = prefixLength;
158 localRoute->gateway_.address_ = "0.0.0.0";
159 netLinkInfo->routeList_.push_back(*localRoute);
160
161 int32_t result = netManager->UpdateNetLinkInfo(supplierId, netLinkInfo);
162 WIFI_LOGI("UpdateNetLinkInfo result:%{public}d", result);
163 }
164
AddRoute(const std::string interface,const std::string ipAddress,int prefixLength)165 bool WifiNetAgent::AddRoute(const std::string interface, const std::string ipAddress, int prefixLength)
166 {
167 TimeStats timeStats(__func__);
168 LOGI("NetAgent add route");
169 unsigned int ipInt = IpTools::ConvertIpv4Address(ipAddress);
170 std::string mask = IpTools::ConvertIpv4Mask(prefixLength);
171 unsigned int maskInt = IpTools::ConvertIpv4Address(mask);
172 std::string strLocalRoute = IpTools::ConvertIpv4Address(ipInt & maskInt);
173 std::string destAddress = strLocalRoute + "/" + std::to_string(prefixLength);
174
175 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
176 if (samgr == nullptr) {
177 LOGE("GetSystemAbilityManager failed!");
178 return false;
179 }
180 auto remote = samgr->GetSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
181 if (remote == nullptr) {
182 LOGE("GetSystemAbility failed!");
183 return false;
184 }
185 OHOS::sptr<OHOS::NetsysNative::INetsysService> netsysService = iface_cast<NetsysNative::INetsysService>(remote);
186 if (netsysService == nullptr) {
187 LOGE("NetdService is nullptr!");
188 return false;
189 }
190 LOGI("Add route, interface: %{public}s, destAddress: %{public}s, ipAddress: %{public}s, prefixLength: %{public}d",
191 interface.c_str(), IpAnonymize(destAddress).c_str(), IpAnonymize(ipAddress).c_str(), prefixLength);
192 netsysService->NetworkAddRoute(OHOS::nmd::LOCAL_NETWORK_NETID, interface, destAddress, ipAddress);
193 LOGI("NetAgent add route finish");
194 return true;
195 }
196
OnStaMachineUpdateNetLinkInfo(const std::string & strIp,const std::string & strMask,const std::string & strGateWay,const std::string & strDns,const std::string & strBakDns)197 void WifiNetAgent::OnStaMachineUpdateNetLinkInfo(const std::string &strIp, const std::string &strMask,
198 const std::string &strGateWay, const std::string &strDns, const std::string &strBakDns)
199 {
200 std::thread([ip = strIp, mask = strMask, gateWay = strGateWay, dns = strDns, bakDns = strBakDns, this]() {
201 UpdateNetLinkInfo(ip, mask, gateWay, dns, bakDns);
202 }).detach();
203 }
204
OnStaMachineUpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> & netSupplierInfo)205 void WifiNetAgent::OnStaMachineUpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo)
206 {
207 std::thread([netInfo = netSupplierInfo, this]() {
208 UpdateNetSupplierInfo(netInfo);
209 }).detach();
210 }
211
OnStaMachineWifiStart(const StaServiceCallback & callback)212 void WifiNetAgent::OnStaMachineWifiStart(const StaServiceCallback &callback)
213 {
214 std::thread([cb = callback, this]() {
215 RegisterNetSupplier();
216 RegisterNetSupplierCallback(cb);
217 }).detach();
218 }
219
OnStaMachineNetManagerRestart(const sptr<NetManagerStandard::NetSupplierInfo> & netSupplierInfo,const StaServiceCallback & callback)220 void WifiNetAgent::OnStaMachineNetManagerRestart(const sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo,
221 const StaServiceCallback &callback)
222 {
223 std::thread([cb = callback, supplierInfo = netSupplierInfo, this]() {
224 RegisterNetSupplier();
225 RegisterNetSupplierCallback(cb);
226 WifiLinkedInfo linkedInfo;
227 WifiSettings::GetInstance().GetLinkedInfo(linkedInfo);
228 if ((linkedInfo.detailedState == DetailedState::NOTWORKING)
229 && (linkedInfo.connState == ConnState::CONNECTED)) {
230 if (supplierInfo != nullptr) {
231 TimeStats timeStats("Call UpdateNetSupplierInfo");
232 UpdateNetSupplierInfo(supplierInfo);
233 }
234 IpInfo wifiIpInfo;
235 WifiSettings::GetInstance().GetIpInfo(wifiIpInfo);
236 std::string ipAddress = IpTools::ConvertIpv4Address(wifiIpInfo.ipAddress);
237 std::string gateway = IpTools::ConvertIpv4Address(wifiIpInfo.gateway);
238 std::string netmask = IpTools::ConvertIpv4Address(wifiIpInfo.netmask);
239 std::string primaryDns = IpTools::ConvertIpv4Address(wifiIpInfo.primaryDns);
240 std::string secondDns = IpTools::ConvertIpv4Address(wifiIpInfo.secondDns);
241 UpdateNetLinkInfo(ipAddress, netmask, gateway, primaryDns, secondDns);
242 }
243 }).detach();
244 }
245
NetConnCallback(const StaServiceCallback & callback)246 WifiNetAgent::NetConnCallback::NetConnCallback(const StaServiceCallback &callback)
247 {
248 staCallback = callback;
249 }
250
~NetConnCallback()251 WifiNetAgent::NetConnCallback::~NetConnCallback()
252 {}
253
RequestNetwork(const std::string & ident,const std::set<NetManagerStandard::NetCap> & netCaps)254 int32_t WifiNetAgent::NetConnCallback::RequestNetwork(
255 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps)
256 {
257 WIFI_LOGD("Enter NetConnCallback::RequestNetwork");
258 LogNetCaps(ident, netCaps);
259 return 0;
260 }
261
ReleaseNetwork(const std::string & ident,const std::set<NetManagerStandard::NetCap> & netCaps)262 int32_t WifiNetAgent::NetConnCallback::ReleaseNetwork(
263 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps)
264 {
265 WIFI_LOGD("Enter NetConnCallback::ReleaseNetwork");
266 LogNetCaps(ident, netCaps);
267 return 0;
268 }
269
LogNetCaps(const std::string & ident,const std::set<NetManagerStandard::NetCap> & netCaps) const270 void WifiNetAgent::NetConnCallback::LogNetCaps(
271 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps) const
272 {
273 WIFI_LOGD("ident=[%s]", ident.c_str());
274 std::string logStr;
275 const std::string logStrEnd("]");
276 logStr = "netCaps[";
277 for (auto netCap : netCaps) {
278 logStr += std::to_string(static_cast<uint32_t>(netCap));
279 logStr += " ";
280 }
281 logStr += logStrEnd;
282 WIFI_LOGD("%{public}s", logStr.c_str());
283 }
284 }
285 }
286