• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "inet_addr.h"
19 #include "net_conn_client.h"
20 #include "net_manager_native.h"
21 #include "route_controller.h"
22 #include "wifi_common_util.h"
23 #include "wifi_logger.h"
24 
25 DEFINE_WIFILOG_LABEL("WifiNetAgent");
26 
27 namespace OHOS {
28 namespace Wifi {
29 using namespace NetManagerStandard;
30 
31 WifiNetAgent::WifiNetAgent() = default;
32 WifiNetAgent::~WifiNetAgent() = default;
33 
RegisterNetSupplier()34 bool WifiNetAgent::RegisterNetSupplier()
35 {
36     WIFI_LOGI("Enter RegisterNetSupplier.");
37     auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
38     if (netManager == nullptr) {
39         WIFI_LOGE("NetConnClient is null");
40         return false;
41     }
42     std::string ident = "wifi";
43     using NetManagerStandard::NetBearType;
44     using NetManagerStandard::NetCap;
45     std::set<NetCap> netCaps {NetCap::NET_CAPABILITY_INTERNET};
46     int32_t result = netManager->RegisterNetSupplier(NetBearType::BEARER_WIFI, ident, netCaps, supplierId);
47     if (result == ERR_NONE) {
48         WIFI_LOGI("Register NetSupplier successful");
49         return true;
50     }
51     WIFI_LOGI("Register NetSupplier failed");
52     return false;
53 }
54 
RegisterNetSupplierCallback(const StaServiceCallback & callback)55 bool WifiNetAgent::RegisterNetSupplierCallback(const StaServiceCallback &callback)
56 {
57     WIFI_LOGI("Enter RegisterNetSupplierCallback.");
58     auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
59     if (netManager == nullptr) {
60         WIFI_LOGE("NetConnClient is null");
61         return false;
62     }
63 
64     sptr<NetConnCallback> pNetConnCallback = (std::make_unique<NetConnCallback>(callback)).release();
65     if (pNetConnCallback == nullptr) {
66         WIFI_LOGE("pNetConnCallback is null\n");
67         return false;
68     }
69 
70     int32_t result = netManager->RegisterNetSupplierCallback(supplierId, pNetConnCallback);
71     if (result == ERR_NONE) {
72         WIFI_LOGI("Register NetSupplierCallback successful");
73         return true;
74     }
75     WIFI_LOGE("Register NetSupplierCallback failed [%{public}d]", result);
76     return false;
77 }
78 
UnregisterNetSupplier()79 void WifiNetAgent::UnregisterNetSupplier()
80 {
81     WIFI_LOGI("Enter UnregisterNetSupplier.");
82     auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
83     if (netManager == nullptr) {
84         WIFI_LOGE("NetConnClient is null");
85         return;
86     }
87     int32_t result = netManager->UnregisterNetSupplier(supplierId);
88     WIFI_LOGI("Unregister network result:%{public}d", result);
89 }
90 
UpdateNetSupplierInfo(sptr<NetManagerStandard::NetSupplierInfo> & netSupplierInfo)91 void WifiNetAgent::UpdateNetSupplierInfo(sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo)
92 {
93     WIFI_LOGI("Enter UpdateNetSupplierInfo.");
94     auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
95     if (netManager == nullptr) {
96         WIFI_LOGE("NetConnClient is null");
97         return;
98     }
99 
100     int32_t result = netManager->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
101     WIFI_LOGI("Update network result:%{public}d", result);
102 }
103 
UpdateNetLinkInfo(std::string & ip,std::string & mask,std::string & gateWay,std::string & strDns,std::string & strBakDns)104 void WifiNetAgent::UpdateNetLinkInfo(std::string &ip, std::string &mask, std::string &gateWay,
105     std::string &strDns, std::string &strBakDns)
106 {
107     WIFI_LOGI("Enter UpdateNetLinkInfo.");
108     auto netManager = DelayedSingleton<NetConnClient>::GetInstance();
109     if (netManager == nullptr) {
110         WIFI_LOGE("NetConnClient is null");
111         return;
112     }
113 
114     sptr<NetManagerStandard::NetLinkInfo> netLinkInfo = (std::make_unique<NetManagerStandard::NetLinkInfo>()).release();
115     netLinkInfo->ifaceName_ = "wlan0";
116 
117     unsigned int prefixLength = IpTools::GetMaskLength(mask);
118     sptr<NetManagerStandard::INetAddr> netAddr = (std::make_unique<NetManagerStandard::INetAddr>()).release();
119     netAddr->type_ = NetManagerStandard::INetAddr::IPV4;
120     netAddr->address_ = ip;
121     netAddr->netMask_ = mask;
122     netAddr->prefixlen_ = prefixLength;
123     netLinkInfo->netAddrList_.push_back(*netAddr);
124 
125     sptr<NetManagerStandard::INetAddr> dns = (std::make_unique<NetManagerStandard::INetAddr>()).release();
126     dns->type_ = NetManagerStandard::INetAddr::IPV4;
127     dns->address_ = strDns;
128     netLinkInfo->dnsList_.push_back(*dns);
129     dns->address_ = strBakDns;
130     netLinkInfo->dnsList_.push_back(*dns);
131 
132     sptr<NetManagerStandard::Route> route = (std::make_unique<NetManagerStandard::Route>()).release();
133     route->iface_ = "wlan0";
134     route->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
135     route->destination_.address_ = "0.0.0.0";
136     route->gateway_.address_ = gateWay;
137     netLinkInfo->routeList_.push_back(*route);
138 
139     sptr<NetManagerStandard::Route> localRoute = (std::make_unique<NetManagerStandard::Route>()).release();
140     unsigned int ipInt = IpTools::ConvertIpv4Address(ip);
141     unsigned int maskInt = IpTools::ConvertIpv4Address(mask);
142     std::string strLocalRoute = IpTools::ConvertIpv4Address(ipInt & maskInt);
143     localRoute->iface_ = route->iface_;
144     localRoute->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
145     localRoute->destination_.address_ = strLocalRoute;
146     localRoute->destination_.prefixlen_ = prefixLength;
147     localRoute->gateway_.address_ = "0.0.0.0";
148     netLinkInfo->routeList_.push_back(*localRoute);
149 
150     int32_t result = netManager->UpdateNetLinkInfo(supplierId, netLinkInfo);
151     WIFI_LOGI("result:%{public}d", result);
152 }
153 
AddRoute(const std::string interface,const std::string ipAddress,int prefixLength)154 bool WifiNetAgent::AddRoute(const std::string interface, const std::string ipAddress, int prefixLength)
155 {
156     LOGI("Net agent addroute");
157     unsigned int ipInt = IpTools::ConvertIpv4Address(ipAddress);
158     std::string mask = IpTools::ConvertIpv4Mask(prefixLength);
159     unsigned int maskInt = IpTools::ConvertIpv4Address(mask);
160     std::string strLocalRoute = IpTools::ConvertIpv4Address(ipInt & maskInt);
161     std::string destAddress = strLocalRoute + "/" + std::to_string(prefixLength);
162     std::unique_ptr<OHOS::nmd::NetManagerNative> netdService = std::make_unique<nmd::NetManagerNative>();
163     if (netdService == nullptr) {
164         LOGE("NetdService is nullptr!");
165         return false;
166     }
167     LOGI("Add route, interface: %{public}s, destAddress: %{public}s, ipAddress: %{public}s, prefixLength: %{public}d",
168         interface.c_str(), IpAnonymize(destAddress).c_str(), IpAnonymize(ipAddress).c_str(), prefixLength);
169     netdService->NetworkAddRoute(OHOS::nmd::LOCAL_NETWORK_NETID, interface, destAddress, ipAddress);
170     return true;
171 }
172 
NetConnCallback(const StaServiceCallback & callback)173 WifiNetAgent::NetConnCallback::NetConnCallback(const StaServiceCallback &callback)
174 {
175     staCallback = callback;
176 }
177 
~NetConnCallback()178 WifiNetAgent::NetConnCallback::~NetConnCallback()
179 {}
180 
RequestNetwork(const std::string & ident,const std::set<NetManagerStandard::NetCap> & netCaps)181 int32_t WifiNetAgent::NetConnCallback::RequestNetwork(
182     const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps)
183 {
184     WIFI_LOGD("Enter NetConnCallback::RequestNetwork");
185     LogNetCaps(ident, netCaps);
186     return 0;
187 }
188 
ReleaseNetwork(const std::string & ident,const std::set<NetManagerStandard::NetCap> & netCaps)189 int32_t WifiNetAgent::NetConnCallback::ReleaseNetwork(
190     const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps)
191 {
192     WIFI_LOGD("Enter NetConnCallback::ReleaseNetwork");
193     LogNetCaps(ident, netCaps);
194     return 0;
195 }
196 
LogNetCaps(const std::string & ident,const std::set<NetManagerStandard::NetCap> & netCaps) const197 void WifiNetAgent::NetConnCallback::LogNetCaps(
198     const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps) const
199 {
200     WIFI_LOGD("ident=[%s]", ident.c_str());
201     std::string logStr;
202     const std::string logStrEnd("]");
203     logStr = "netCaps[";
204     for (auto netCap : netCaps) {
205         logStr += std::to_string(static_cast<uint32_t>(netCap));
206         logStr += " ";
207     }
208     logStr += logStrEnd;
209     WIFI_LOGD("%{public}s", logStr.c_str());
210 }
211 }
212 }
213