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 <algorithm>
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_config_center.h"
28 #include "ipv6_address.h"
29 #include "wifi_global_func.h"
30 #include "wifi_app_state_aware.h"
31
32 DEFINE_WIFILOG_LABEL("WifiNetAgent");
33
34 namespace OHOS {
35 namespace Wifi {
36 constexpr const char *WIFI_NET_CONN_MGR_WORK_THREAD = "WIFI_NET_CONN_MGR_WORK_THREAD";
37 using namespace NetManagerStandard;
38
39 #define INVALID_SUPPLIER_ID 0
40 #define ACCEPT_UNVALIDATED 7
41
GetInstance()42 WifiNetAgent &WifiNetAgent::GetInstance()
43 {
44 static WifiNetAgent gWifiNetAgent;
45 return gWifiNetAgent;
46 }
47
WifiNetAgent()48 WifiNetAgent::WifiNetAgent()
49 {
50 netAgentEventHandler_ = std::make_unique<WifiEventHandler>(WIFI_NET_CONN_MGR_WORK_THREAD);
51 }
~WifiNetAgent()52 WifiNetAgent::~WifiNetAgent()
53 {
54 if (netAgentEventHandler_) {
55 netAgentEventHandler_.reset();
56 }
57 }
58
RegisterNetSupplier()59 bool WifiNetAgent::RegisterNetSupplier()
60 {
61 TimeStats timeStats(__func__);
62 WIFI_LOGI("Enter RegisterNetSupplier.");
63
64 std::string ident = "wifi";
65 using NetManagerStandard::NetBearType;
66 using NetManagerStandard::NetCap;
67 std::set<NetCap> netCaps {NetCap::NET_CAPABILITY_INTERNET};
68 if (supplierId != INVALID_SUPPLIER_ID) {
69 WIFI_LOGI("RegisterNetSupplier supplierId alread exist.");
70 return true;
71 }
72 int32_t result = NetConnClient::GetInstance().RegisterNetSupplier(NetBearType::BEARER_WIFI,
73 ident, netCaps, supplierId);
74 if (result == NETMANAGER_SUCCESS) {
75 WIFI_LOGI("Register NetSupplier successful, supplierId is [%{public}d]", supplierId);
76 return true;
77 }
78 WIFI_LOGI("Register NetSupplier failed");
79 return false;
80 }
81
RegisterNetSupplierCallback()82 bool WifiNetAgent::RegisterNetSupplierCallback()
83 {
84 TimeStats timeStats(__func__);
85 WIFI_LOGI("Enter RegisterNetSupplierCallback.");
86 sptr<NetConnCallback> pNetConnCallback = (std::make_unique<NetConnCallback>()).release();
87 if (pNetConnCallback == nullptr) {
88 WIFI_LOGE("pNetConnCallback is null\n");
89 return false;
90 }
91
92 int32_t result = NetConnClient::GetInstance().RegisterNetSupplierCallback(supplierId, pNetConnCallback);
93 if (result == NETMANAGER_SUCCESS) {
94 WIFI_LOGI("Register NetSupplierCallback successful");
95 return true;
96 }
97 WIFI_LOGE("Register NetSupplierCallback failed [%{public}d]", result);
98 return false;
99 }
100
UnregisterNetSupplier()101 void WifiNetAgent::UnregisterNetSupplier()
102 {
103 TimeStats timeStats(__func__);
104 WIFI_LOGI("Enter UnregisterNetSupplier.");
105 int32_t result = NetConnClient::GetInstance().UnregisterNetSupplier(supplierId);
106 WIFI_LOGI("Unregister network result:%{public}d", result);
107 supplierId = INVALID_SUPPLIER_ID;
108 }
109
UpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> & netSupplierInfo)110 void WifiNetAgent::UpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo)
111 {
112 TimeStats timeStats(__func__);
113 WIFI_LOGI("Enter UpdateNetSupplierInfo.");
114 isWifiAvaliable_ = netSupplierInfo->isAvailable_;
115 int32_t result = NetConnClient::GetInstance().UpdateNetSupplierInfo(supplierId, netSupplierInfo);
116 WIFI_LOGI("Update network result:%{public}d", result);
117 }
118
UpdateNetLinkInfo(IpInfo & wifiIpInfo,IpV6Info & wifiIpV6Info,WifiProxyConfig & wifiProxyConfig,int instId)119 void WifiNetAgent::UpdateNetLinkInfo(IpInfo &wifiIpInfo, IpV6Info &wifiIpV6Info, WifiProxyConfig &wifiProxyConfig,
120 int instId)
121 {
122 TimeStats timeStats(__func__);
123 WIFI_LOGI("Enter UpdateNetLinkInfo.");
124 if (!isWifiAvaliable_) {
125 WIFI_LOGE("wifi is not avaliable, no need UpdateNetLinkInfo");
126 return;
127 }
128 sptr<NetManagerStandard::NetLinkInfo> netLinkInfo = (std::make_unique<NetManagerStandard::NetLinkInfo>()).release();
129 CreateNetLinkInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info, wifiProxyConfig, instId);
130 int32_t result = NetConnClient::GetInstance().UpdateNetLinkInfo(supplierId, netLinkInfo);
131 WIFI_LOGI("UpdateNetLinkInfo result:%{public}d", result);
132 }
133
AddRoute(const std::string interface,const std::string ipAddress,int prefixLength)134 bool WifiNetAgent::AddRoute(const std::string interface, const std::string ipAddress, int prefixLength)
135 {
136 TimeStats timeStats(__func__);
137 LOGI("NetAgent add route");
138 unsigned int ipInt = IpTools::ConvertIpv4Address(ipAddress);
139 std::string mask = IpTools::ConvertIpv4Mask(prefixLength);
140 unsigned int maskInt = IpTools::ConvertIpv4Address(mask);
141 std::string strLocalRoute = IpTools::ConvertIpv4Address(ipInt & maskInt);
142 std::string destAddress = strLocalRoute + "/" + std::to_string(prefixLength);
143
144 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
145 if (samgr == nullptr) {
146 LOGE("GetSystemAbilityManager failed!");
147 return false;
148 }
149 auto remote = samgr->GetSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
150 if (remote == nullptr) {
151 LOGE("GetSystemAbility failed!");
152 return false;
153 }
154 OHOS::sptr<OHOS::NetsysNative::INetsysService> netsysService = iface_cast<NetsysNative::INetsysService>(remote);
155 if (netsysService == nullptr) {
156 LOGE("NetdService is nullptr!");
157 return false;
158 }
159 LOGI("Add route, interface: %{public}s, destAddress: %{public}s, ipAddress: %{public}s, prefixLength: %{public}d",
160 interface.c_str(), IpAnonymize(destAddress).c_str(), IpAnonymize(ipAddress).c_str(), prefixLength);
161 netsysService->NetworkAddRoute(OHOS::nmd::LOCAL_NETWORK_NETID, interface, destAddress, ipAddress);
162 LOGI("NetAgent add route finish");
163 return true;
164 }
165
DelInterfaceAddress(const std::string & interface,const std::string & ipAddress,int prefixLength)166 bool WifiNetAgent::DelInterfaceAddress(const std::string &interface, const std::string &ipAddress, int prefixLength)
167 {
168 int32_t result = NetConnClient::GetInstance().DelInterfaceAddress(interface, ipAddress, prefixLength);
169 if (result == NETMANAGER_SUCCESS) {
170 WIFI_LOGI("DelInterfaceAddress successful");
171 return true;
172 }
173 WIFI_LOGI("DelInterfaceAddress failed");
174 return false;
175 }
176
OnStaMachineUpdateNetLinkInfo(IpInfo wifiIpInfo,IpV6Info wifiIpV6Info,WifiProxyConfig wifiProxyConfig,int instId)177 void WifiNetAgent::OnStaMachineUpdateNetLinkInfo(IpInfo wifiIpInfo, IpV6Info wifiIpV6Info,
178 WifiProxyConfig wifiProxyConfig, int instId)
179 {
180 if (netAgentEventHandler_) {
181 #ifdef FEATURE_ITNETWORK_PREFERRED_SUPPORT
182 netAgentEventHandler_->PostSyncTask(
183 #else
184 netAgentEventHandler_->PostAsyncTask(
185 #endif
186 [this, wifiIpInfo, wifiIpV6Info, wifiProxyConfig, instId]() mutable {
187 this->UpdateNetLinkInfo(wifiIpInfo, wifiIpV6Info, wifiProxyConfig, instId);
188 });
189 }
190 }
191
OnStaMachineUpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo)192 void WifiNetAgent::OnStaMachineUpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo)
193 {
194 if (netAgentEventHandler_) {
195 #ifdef FEATURE_ITNETWORK_PREFERRED_SUPPORT
196 netAgentEventHandler_->PostSyncTask([this, netInfo = netSupplierInfo]() {
197 #else
198 netAgentEventHandler_->PostAsyncTask([this, netInfo = netSupplierInfo]() {
199 #endif
200 this->UpdateNetSupplierInfo(netInfo);
201 });
202 }
203 }
204
205 void WifiNetAgent::OnStaMachineWifiStart()
206 {
207 if (netAgentEventHandler_) {
208 #ifdef FEATURE_ITNETWORK_PREFERRED_SUPPORT
209 netAgentEventHandler_->PostSyncTask([this]() {
210 #else
211 netAgentEventHandler_->PostAsyncTask([this]() {
212 #endif
213 this->RegisterNetSupplier();
214 this->RegisterNetSupplierCallback();
215 });
216 }
217 }
218
219 void WifiNetAgent::OnStaMachineNetManagerRestart(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo,
220 int instId)
221 {
222 if (!netAgentEventHandler_) {
223 WIFI_LOGE("%{public}s netAgentEventHandler_ is null", __func__);
224 return;
225 }
226 #ifdef FEATURE_ITNETWORK_PREFERRED_SUPPORT
227 netAgentEventHandler_->PostSyncTask([this, supplierInfo = netSupplierInfo, m_instId = instId]() {
228 #else
229 netAgentEventHandler_->PostAsyncTask([this, supplierInfo = netSupplierInfo, m_instId = instId]() {
230 #endif
231 this->RegisterNetSupplier();
232 this->RegisterNetSupplierCallback();
233 WifiLinkedInfo linkedInfo;
234 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo, m_instId);
235 if (linkedInfo.connState == ConnState::CONNECTED) {
236 #ifndef OHOS_ARCH_LITE
237 if (supplierInfo != nullptr) {
238 TimeStats timeStats("Call UpdateNetSupplierInfo");
239 this->UpdateNetSupplierInfo(supplierInfo);
240 }
241 #endif
242 IpInfo wifiIpInfo;
243 WifiConfigCenter::GetInstance().GetIpInfo(wifiIpInfo, m_instId);
244 IpV6Info wifiIpV6Info;
245 WifiConfigCenter::GetInstance().GetIpv6Info(wifiIpV6Info, m_instId);
246 WifiDeviceConfig config;
247 WifiSettings::GetInstance().GetDeviceConfig(linkedInfo.networkId, config);
248 this->UpdateNetLinkInfo(wifiIpInfo, wifiIpV6Info, config.wifiProxyconfig, m_instId);
249 }
250 });
251 }
252
253 void WifiNetAgent::CreateNetLinkInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
254 IpV6Info &wifiIpV6Info, WifiProxyConfig &wifiProxyConfig, int instId)
255 {
256 netLinkInfo->ifaceName_ = WifiConfigCenter::GetInstance().GetStaIfaceName(instId);
257
258 SetNetLinkIPInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info);
259 SetNetLinkHostRouteInfo(netLinkInfo, wifiIpInfo);
260 SetNetLinkRouteInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info);
261 SetNetLinkDnsInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info);
262 SetNetLinkLocalRouteInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info);
263 if (wifiProxyConfig.configureMethod == ConfigureProxyMethod::AUTOCONFIGUE) {
264 /* Automatic proxy is not supported */
265 } else if (wifiProxyConfig.configureMethod == ConfigureProxyMethod::MANUALCONFIGUE) {
266 std::vector<std::string> exclusionList;
267 wifiProxyConfig.manualProxyConfig.GetExclusionObjectList(exclusionList);
268 std::list<std::string> tmpExclusionList;
269 std::copy_if(exclusionList.begin(), exclusionList.end(), std::back_inserter(tmpExclusionList),
270 [](const std::string &str) { return !str.empty(); } );
271 netLinkInfo->httpProxy_.SetHost(std::move(wifiProxyConfig.manualProxyConfig.serverHostName));
272 netLinkInfo->httpProxy_.SetPort(wifiProxyConfig.manualProxyConfig.serverPort);
273 netLinkInfo->httpProxy_.SetExclusionList(tmpExclusionList);
274 } else {
275 netLinkInfo->httpProxy_.SetHost("");
276 netLinkInfo->httpProxy_.SetPort(0);
277 }
278
279 return;
280 }
281
282 void WifiNetAgent::SetNetLinkIPInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
283 IpV6Info &wifiIpV6Info)
284 {
285 unsigned int prefixLength =
286 static_cast<unsigned int>(IpTools::GetMaskLength(IpTools::ConvertIpv4Address(wifiIpInfo.netmask)));
287 sptr<NetManagerStandard::INetAddr> netAddr = (std::make_unique<NetManagerStandard::INetAddr>()).release();
288 netAddr->type_ = NetManagerStandard::INetAddr::IPV4;
289 netAddr->family_ = NetManagerStandard::INetAddr::IPV4;
290 netAddr->address_ = IpTools::ConvertIpv4Address(wifiIpInfo.ipAddress);
291 netAddr->netMask_ = IpTools::ConvertIpv4Address(wifiIpInfo.netmask);
292 netAddr->prefixlen_ = prefixLength;
293 netLinkInfo->netAddrList_.push_back(*netAddr);
294
295 LOGD("SetNetLinkIPInfo %{public}s", wifiIpV6Info.globalIpV6Address.c_str());
296 sptr<NetManagerStandard::INetAddr> netIpv6Addr = nullptr;
297 if (!wifiIpV6Info.globalIpV6Address.empty()) {
298 netIpv6Addr = (std::make_unique<NetManagerStandard::INetAddr>()).release();
299 netIpv6Addr->address_ = wifiIpV6Info.globalIpV6Address;
300 }
301 LOGD("SetNetLinkIPInfo randGlobalIpV6Address:%{public}s", wifiIpV6Info.randGlobalIpV6Address.c_str());
302 if (!wifiIpV6Info.randGlobalIpV6Address.empty()) {
303 netIpv6Addr = (std::make_unique<NetManagerStandard::INetAddr>()).release();
304 netIpv6Addr->address_ = wifiIpV6Info.randGlobalIpV6Address;
305 }
306 LOGD("SetNetLinkIPInfo uniqueLocalAddress1:%{public}s", wifiIpV6Info.uniqueLocalAddress1.c_str());
307 if (!wifiIpV6Info.uniqueLocalAddress1.empty()) {
308 netIpv6Addr = (std::make_unique<NetManagerStandard::INetAddr>()).release();
309 netIpv6Addr->address_ = wifiIpV6Info.uniqueLocalAddress1;
310 }
311 LOGD("SetNetLinkIPInfo uniqueLocalAddress2:%{public}s", wifiIpV6Info.uniqueLocalAddress2.c_str());
312 if (!wifiIpV6Info.uniqueLocalAddress2.empty()) {
313 netIpv6Addr = (std::make_unique<NetManagerStandard::INetAddr>()).release();
314 netIpv6Addr->address_ = wifiIpV6Info.uniqueLocalAddress2;
315 }
316 if (netIpv6Addr != nullptr) {
317 netIpv6Addr->type_ = NetManagerStandard::INetAddr::IPV6;
318 netIpv6Addr->family_ = NetManagerStandard::INetAddr::IPV6;
319 netIpv6Addr->netMask_ = wifiIpV6Info.netmask;
320 netIpv6Addr->prefixlen_ = 0;
321 netLinkInfo->netAddrList_.push_back(*netIpv6Addr);
322 }
323 }
324
325 void WifiNetAgent::SetNetLinkDnsInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
326 IpV6Info &wifiIpV6Info)
327 {
328 sptr<NetManagerStandard::INetAddr> dns = (std::make_unique<NetManagerStandard::INetAddr>()).release();
329 dns->type_ = NetManagerStandard::INetAddr::IPV4;
330 dns->family_ = NetManagerStandard::INetAddr::IPV4;
331 if (wifiIpInfo.primaryDns != 0) {
332 dns->address_ = IpTools::ConvertIpv4Address(wifiIpInfo.primaryDns);
333 netLinkInfo->dnsList_.push_back(*dns);
334 LOGI("SetNetLinkDnsInfo ipv4 address:%{public}s", IpAnonymize(dns->address_).c_str());
335 }
336 if (wifiIpInfo.secondDns != 0) {
337 dns->address_ = IpTools::ConvertIpv4Address(wifiIpInfo.secondDns);
338 netLinkInfo->dnsList_.push_back(*dns);
339 LOGI("SetNetLinkDnsInfo ipv4 address:%{public}s", IpAnonymize(dns->address_).c_str());
340 }
341 sptr<NetManagerStandard::INetAddr> ipv6dns = (std::make_unique<NetManagerStandard::INetAddr>()).release();
342 ipv6dns->type_ = NetManagerStandard::INetAddr::IPV6;
343 ipv6dns->family_ = NetManagerStandard::INetAddr::IPV6;
344 if (!wifiIpV6Info.primaryDns.empty()) {
345 ipv6dns->address_ = wifiIpV6Info.primaryDns;
346 netLinkInfo->dnsList_.push_back(*ipv6dns);
347 }
348 if (!wifiIpV6Info.secondDns.empty()) {
349 ipv6dns->address_ = wifiIpV6Info.secondDns;
350 netLinkInfo->dnsList_.push_back(*ipv6dns);
351 }
352 }
353
354 void WifiNetAgent::SetNetLinkRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
355 IpV6Info &wifiIpV6Info)
356 {
357 sptr<NetManagerStandard::Route> route = (std::make_unique<NetManagerStandard::Route>()).release();
358 route->iface_ = netLinkInfo->ifaceName_;
359 route->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
360 route->destination_.address_ = "0.0.0.0";
361 route->destination_.family_ = NetManagerStandard::INetAddr::IPV4;
362 route->gateway_.address_ = IpTools::ConvertIpv4Address(wifiIpInfo.gateway);
363 route->gateway_.family_ = NetManagerStandard::INetAddr::IPV4;
364 netLinkInfo->routeList_.push_back(*route);
365 LOGI("SetNetLinkRouteInfo gateway:%{public}s", IpAnonymize(route->gateway_.address_).c_str());
366 if (!wifiIpV6Info.gateway.empty()) {
367 sptr<NetManagerStandard::Route> ipv6route = (std::make_unique<NetManagerStandard::Route>()).release();
368 ipv6route->iface_ = netLinkInfo->ifaceName_;
369 ipv6route->destination_.type_ = NetManagerStandard::INetAddr::IPV6;
370 ipv6route->destination_.family_ = NetManagerStandard::INetAddr::IPV6;
371 ipv6route->destination_.address_ = "::";
372 ipv6route->destination_.prefixlen_ = 0;
373 ipv6route->gateway_.address_ = wifiIpV6Info.gateway;
374 ipv6route->gateway_.family_ = NetManagerStandard::INetAddr::IPV6;
375 netLinkInfo->routeList_.push_back(*ipv6route);
376 LOGI("SetNetLinkRouteInfo gateway:%{public}s", MacAnonymize(wifiIpV6Info.gateway).c_str());
377 }
378 }
379
380 void WifiNetAgent::SetNetLinkHostRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo)
381 {
382 if ((wifiIpInfo.ipAddress & wifiIpInfo.netmask) != (wifiIpInfo.gateway & wifiIpInfo.netmask)) {
383 sptr<NetManagerStandard::Route> hostRoute = (std::make_unique<NetManagerStandard::Route>()).release();
384 hostRoute->iface_ = netLinkInfo->ifaceName_;
385 hostRoute->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
386 hostRoute->destination_.address_ = IpTools::ConvertIpv4Address(wifiIpInfo.gateway);
387 hostRoute->destination_.family_ = NetManagerStandard::INetAddr::IPV4;
388 hostRoute->destination_.prefixlen_ = MAX_PREFIX_LEN;
389 netLinkInfo->routeList_.push_back(*hostRoute);
390 LOGI("SetNetLinkHostRouteInfo gateway:%{public}s", IpAnonymize(hostRoute->gateway_.address_).c_str());
391 }
392 }
393
394 void WifiNetAgent::SetNetLinkLocalRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
395 IpV6Info &wifiIpV6Info)
396 {
397 unsigned int prefixLength =
398 static_cast<unsigned int>(IpTools::GetMaskLength(IpTools::ConvertIpv4Address(wifiIpInfo.netmask)));
399 sptr<NetManagerStandard::Route> localRoute = (std::make_unique<NetManagerStandard::Route>()).release();
400 std::string strLocalRoute = IpTools::ConvertIpv4Address(wifiIpInfo.ipAddress & wifiIpInfo.netmask);
401 localRoute->iface_ = netLinkInfo->ifaceName_;
402 localRoute->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
403 localRoute->destination_.address_ = strLocalRoute;
404 localRoute->destination_.prefixlen_ = prefixLength;
405 localRoute->gateway_.address_ = "0.0.0.0";
406 netLinkInfo->routeList_.push_back(*localRoute);
407 LOGI("SetNetLinkLocalRouteInfo ifaceName_:%{public}s %{public}u %{public}s", netLinkInfo->ifaceName_.c_str(),
408 prefixLength, IpAnonymize(strLocalRoute).c_str());
409 if (!wifiIpV6Info.netmask.empty()) {
410 unsigned int ipv6PrefixLength = IpTools::GetIPV6MaskLength(wifiIpV6Info.netmask);
411 sptr<NetManagerStandard::Route> ipv6route = (std::make_unique<NetManagerStandard::Route>()).release();
412 ipv6route->iface_ = netLinkInfo->ifaceName_;
413 ipv6route->destination_.type_ = NetManagerStandard::INetAddr::IPV6;
414 ipv6route->destination_.prefixlen_ = ipv6PrefixLength;
415 ipv6route->gateway_.address_ = "";
416 if (!wifiIpV6Info.globalIpV6Address.empty()) {
417 ipv6route->destination_.address_ =
418 Ipv6Address::GetPrefixByAddr(wifiIpV6Info.globalIpV6Address, ipv6PrefixLength);
419 netLinkInfo->routeList_.push_back(*ipv6route);
420 LOGI("SetNetLinkLocalRouteInfo ipv6PrefixLength:%{public}u globalIpv6:%{public}s", ipv6PrefixLength,
421 MacAnonymize(wifiIpV6Info.globalIpV6Address).c_str());
422 }
423 if (!wifiIpV6Info.uniqueLocalAddress1.empty()) {
424 ipv6route->destination_.address_ =
425 Ipv6Address::GetPrefixByAddr(wifiIpV6Info.uniqueLocalAddress1, ipv6PrefixLength);
426 netLinkInfo->routeList_.push_back(*ipv6route);
427 LOGI("SetNetLinkLocalRouteInfo ipv6PrefixLength:%{public}u uniqueLocalIpv6:%{public}s", ipv6PrefixLength,
428 MacAnonymize(wifiIpV6Info.uniqueLocalAddress1).c_str());
429 }
430 }
431 }
432
433 void WifiNetAgent::InitWifiNetAgent(const WifiNetAgentCallbacks &wifiNetAgentCallbacks)
434 {
435 wifiNetAgentCallbacks_ = wifiNetAgentCallbacks;
436 }
437
438 bool WifiNetAgent::RequestNetwork(const int uid, const int networkId)
439 {
440 if (!wifiNetAgentCallbacks_.OnRequestNetwork) {
441 WIFI_LOGE("OnRequestNetwork is nullptr.");
442 return false;
443 }
444 if (wifiNetAgentCallbacks_.OnRequestNetwork(uid, networkId)) {
445 return true;
446 }
447 return false;
448 }
449
450 WifiNetAgent::NetConnCallback::NetConnCallback()
451 {
452 }
453
454 WifiNetAgent::NetConnCallback::~NetConnCallback()
455 {}
456
457 void WifiNetAgent::ResetSupplierId()
458 {
459 supplierId = INVALID_SUPPLIER_ID;
460 }
461
462 uint32_t WifiNetAgent::GetSupplierId()
463 {
464 return supplierId;
465 }
466
467 int32_t WifiNetAgent::NetConnCallback::RequestNetwork(
468 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps,
469 const NetManagerStandard::NetRequest &netrequest)
470 {
471 WIFI_LOGD("Enter NetConnCallback::RequestNetwork");
472 LogNetCaps(ident, netCaps);
473 #ifndef OHOS_ARCH_LITE
474 if (requestIds_.find(netrequest.requestId) != requestIds_.end()) {
475 return -1;
476 }
477 requestIds_.insert(netrequest.requestId);
478
479 int networkId = ConvertStringToInt(netrequest.ident);
480 if (networkId <= INVALID_NETWORK_ID || std::to_string(networkId) != netrequest.ident) {
481 WIFI_LOGE("networkId is invaild.");
482 return -1;
483 }
484
485 WIFI_LOGI("RequestNetwork uid[%{public}d], networkId[%{public}d].", netrequest.uid, networkId);
486 if (!WifiAppStateAware::GetInstance().IsForegroundApp(netrequest.uid)) {
487 WIFI_LOGE("App is not in foreground.");
488 return -1;
489 }
490
491 WifiLinkedInfo linkedInfo;
492 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo);
493 if (linkedInfo.connState == ConnState::CONNECTING || linkedInfo.connState == ConnState::CONNECTED) {
494 if (linkedInfo.networkId == networkId) {
495 WIFI_LOGI("RequestNetwork networkId is connecting or connected.");
496 return 0;
497 }
498 }
499
500 if (!WifiNetAgent::GetInstance().RequestNetwork(netrequest.uid, networkId)) {
501 WIFI_LOGE("RequestNetwork fail.");
502 return -1;
503 }
504 #endif
505 return 0;
506 }
507
508 int32_t WifiNetAgent::NetConnCallback::ReleaseNetwork(
509 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps)
510 {
511 WIFI_LOGD("Enter NetConnCallback::ReleaseNetwork");
512 LogNetCaps(ident, netCaps);
513 return 0;
514 }
515
516 void WifiNetAgent::NetConnCallback::LogNetCaps(
517 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps) const
518 {
519 WIFI_LOGD("ident=[%s]", ident.c_str());
520 std::string logStr;
521 const std::string logStrEnd("]");
522 logStr = "netCaps[";
523 for (auto netCap : netCaps) {
524 logStr += std::to_string(static_cast<uint32_t>(netCap));
525 logStr += " ";
526 }
527 logStr += logStrEnd;
528 WIFI_LOGD("%{public}s", logStr.c_str());
529 }
530
531 void WifiNetAgent::RestoreWifiConnection()
532 {
533 using NetManagerStandard::NetBearType;
534 int32_t result = NetConnClient::GetInstance().UpdateSupplierScore(NetBearType::BEARER_WIFI,
535 ACCEPT_UNVALIDATED, supplierId);
536 WIFI_LOGI("Restore Wifi Connection, result:%{public}d", result);
537 }
538 }
539 }
540