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 #ifndef NET_CONN_SERVICE_H 17 #define NET_CONN_SERVICE_H 18 19 #include <functional> 20 #include <list> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <vector> 25 26 #include "singleton.h" 27 #include "system_ability.h" 28 29 #include "http_proxy.h" 30 #include "net_activate.h" 31 #include "net_conn_event_handler.h" 32 #include "net_conn_service_iface.h" 33 #include "net_conn_service_stub.h" 34 #include "net_score.h" 35 #include "net_supplier.h" 36 #include "netsys_controller_callback.h" 37 #include "network.h" 38 39 namespace OHOS { 40 namespace NetManagerStandard { 41 42 class NetConnService : public SystemAbility, 43 public INetActivateCallback, 44 public NetConnServiceStub, 45 public std::enable_shared_from_this<NetConnService> { 46 DECLARE_SYSTEM_ABILITY(NetConnService) 47 48 NetConnService(); 49 virtual ~NetConnService(); 50 using NET_SUPPLIER_MAP = std::map<uint32_t, sptr<NetSupplier>>; 51 using NET_NETWORK_MAP = std::map<int32_t, std::shared_ptr<Network>>; 52 using NET_ACTIVATE_MAP = std::map<uint32_t, std::shared_ptr<NetActivate>>; 53 54 public: GetInstance()55 static std::shared_ptr<NetConnService> &GetInstance() 56 { 57 static std::shared_ptr<NetConnService> instance = std::make_shared<NetConnService>(); 58 return instance; 59 } 60 void OnStart() override; 61 void OnStop() override; 62 /** 63 * The interface in NetConnService can be called when the system is ready 64 * 65 * @return Returns 0, the system is ready, otherwise the system is not ready 66 */ 67 int32_t SystemReady() override; 68 69 /** 70 * Disallow or allow a app to create AF_INET or AF_INET6 socket 71 * 72 * @param uid App's uid which need to be disallowed ot allowed to create AF_INET or AF_INET6 socket 73 * @param allow 0 means disallow, 1 means allow 74 * @return return 0 if OK, return error number if not OK 75 */ 76 int32_t SetInternetPermission(uint32_t uid, uint8_t allow) override; 77 78 /** 79 * The interface is register the network 80 * 81 * @param bearerType Bearer Network Type 82 * @param ident Unique identification of mobile phone card 83 * @param netCaps Network capabilities registered by the network supplier 84 * @param supplierId out param, return supplier id 85 * 86 * @return function result 87 */ 88 int32_t RegisterNetSupplier(NetBearType bearerType, const std::string &ident, const std::set<NetCap> &netCaps, 89 uint32_t &supplierId) override; 90 91 /** 92 * The interface is unregister the network 93 * 94 * @param supplierId The id of the network supplier 95 * 96 * @return Returns 0, unregister the network successfully, otherwise it will fail 97 */ 98 int32_t UnregisterNetSupplier(uint32_t supplierId) override; 99 100 /** 101 * Register supplier callback 102 * 103 * @param supplierId The id of the network supplier 104 * @param callback INetSupplierCallback callback interface 105 * 106 * @return Returns 0, unregister the network successfully, otherwise it will fail 107 */ 108 int32_t RegisterNetSupplierCallback(uint32_t supplierId, const sptr<INetSupplierCallback> &callback) override; 109 110 /** 111 * Register net connection callback 112 * 113 * @param netSpecifier specifier information 114 * @param callback The callback of INetConnCallback interface 115 * 116 * @return Returns 0, successfully register net connection callback, otherwise it will failed 117 */ 118 int32_t RegisterNetConnCallback(const sptr<INetConnCallback> &callback) override; 119 120 /** 121 * Register net connection callback by NetSpecifier 122 * 123 * @param netSpecifier specifier information 124 * @param callback The callback of INetConnCallback interface 125 * @param timeoutMS net connection time out 126 * 127 * @return Returns 0, successfully register net connection callback, otherwise it will failed 128 */ 129 int32_t RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> &callback, 130 const uint32_t &timeoutMS) override; 131 132 /** 133 * Unregister net connection callback 134 * 135 * @return Returns 0, successfully unregister net connection callback, otherwise it will fail 136 */ 137 int32_t UnregisterNetConnCallback(const sptr<INetConnCallback> &callback) override; 138 139 int32_t UpdateNetStateForTest(const sptr<NetSpecifier> &netSpecifier, int32_t netState) override; 140 /** 141 * The interface is update network connection status information 142 * 143 * @param supplierId The id of the network supplier 144 * @param netSupplierInfo network connection status information 145 * 146 * @return Returns 0, successfully update the network connection status information, otherwise it will fail 147 */ 148 int32_t UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo) override; 149 150 /** 151 * The interface is update network link attribute information 152 * 153 * @param supplierId The id of the network supplier 154 * @param netLinkInfo network link attribute information 155 * 156 * @return Returns 0, successfully update the network link attribute information, otherwise it will fail 157 */ 158 int32_t UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo) override; 159 160 /** 161 * The interface names which NetBearType is equal than bearerType 162 * 163 * @param bearerType Network bearer type 164 * @param ifaceNames save the obtained ifaceNames 165 * @return Returns 0, successfully get the network link attribute iface name, otherwise it will fail 166 */ 167 int32_t GetIfaceNames(NetBearType bearerType, std::list<std::string> &ifaceNames) override; 168 169 /** 170 * The interface is get the iface name for network 171 * 172 * @param bearerType Network bearer type 173 * @param ident Unique identification of mobile phone card 174 * @param ifaceName save the obtained ifaceName 175 * @return Returns 0, successfully get the network link attribute iface name, otherwise it will fail 176 */ 177 int32_t GetIfaceNameByType(NetBearType bearerType, const std::string &ident, std::string &ifaceName) override; 178 179 /** 180 * register network detection return result method 181 * 182 * @param netId Network ID 183 * @param callback The callback of INetDetectionCallback interface 184 * @return int32_t Returns 0, unregister the network successfully, otherwise it will fail 185 */ 186 int32_t RegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback) override; 187 188 /** 189 * unregister network detection return result method 190 * 191 * @param netId Network ID 192 * @param callback The callback of INetDetectionCallback interface 193 * @return int32_t Returns 0, unregister the network successfully, otherwise it will fail 194 */ 195 int32_t UnRegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback) override; 196 197 /** 198 * The interface of network detection called by the application 199 * 200 * @param netId network ID 201 * @return int32_t Whether the network probe is successful 202 */ 203 int32_t NetDetection(int32_t netId) override; 204 int32_t GetDefaultNet(int32_t &netId) override; 205 int32_t HasDefaultNet(bool &flag) override; 206 int32_t GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList) override; 207 int32_t GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr) override; 208 int32_t GetSpecificNet(NetBearType bearerType, std::list<int32_t> &netIdList) override; 209 int32_t GetAllNets(std::list<int32_t> &netIdList) override; 210 int32_t GetSpecificUidNet(int32_t uid, int32_t &netId) override; 211 int32_t GetConnectionProperties(int32_t netId, NetLinkInfo &info) override; 212 int32_t GetNetCapabilities(int32_t netId, NetAllCapabilities &netAllCap) override; 213 int32_t BindSocket(int32_t socket_fd, int32_t netId) override; 214 void HandleDetectionResult(uint32_t supplierId, bool ifValid); 215 int32_t RestrictBackgroundChanged(bool isRestrictBackground); 216 /** 217 * Set airplane mode 218 * 219 * @param state airplane state 220 * @return Returns 0, successfully set airplane mode, otherwise it will fail 221 */ 222 int32_t SetAirplaneMode(bool state) override; 223 /** 224 * Dump 225 * 226 * @param fd file description 227 * @param args unused 228 * @return Returns 0, successfully get dump info, otherwise it will fail 229 */ 230 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 231 /** 232 * Is default network metered 233 * 234 * @param save the metered state 235 * @return Returns 0, Successfully get whether the default network is metered, otherwise it will fail 236 */ 237 int32_t IsDefaultNetMetered(bool &isMetered) override; 238 239 /** 240 * Set http proxy server 241 * 242 * @param httpProxy the http proxy server 243 * @return NETMANAGER_SUCCESS if OK, NET_CONN_ERR_HTTP_PROXY_INVALID if httpProxy is null string 244 */ 245 int32_t SetGlobalHttpProxy(const HttpProxy &httpProxy) override; 246 247 /** 248 * Get http proxy server 249 * 250 * @param httpProxy output param, the http proxy server 251 * @return NETMANAGER_SUCCESS if OK, NET_CONN_ERR_NO_HTTP_PROXY if httpProxy is null string 252 */ 253 int32_t GetGlobalHttpProxy(HttpProxy &httpProxy) override; 254 255 /** 256 * Obtains the default proxy settings. 257 * 258 * <p>If a global proxy is set, the global proxy parameters are returned. 259 * If the process is bound to a network using {@link setAppNet}, 260 * the {@link Network} proxy settings are returned. 261 * In other cases, the default proxy settings of network are returned. 262 * 263 * @param bindNetId App bound network ID 264 * @param httpProxy output param, the http proxy server 265 * @return Returns NETMANAGER_SUCCESS even if HttpProxy is empty 266 */ 267 int32_t GetDefaultHttpProxy(int32_t bindNetId, HttpProxy &httpProxy) override; 268 269 /** 270 * Get net id by identifier 271 * 272 * @param ident Net identifier 273 * @param netIdList output param, the net id list 274 * @return NETMANAGER_SUCCESS if OK, ERR_NO_NET_IDENT if ident is null string 275 */ 276 int32_t GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList) override; 277 278 /** 279 * Activate network timeout 280 * 281 * @param reqId Net request id 282 */ 283 void OnNetActivateTimeOut(uint32_t reqId) override; 284 285 int32_t SetAppNet(int32_t netId) override; 286 int32_t RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback) override; 287 int32_t GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config) override; 288 289 private: 290 class NetInterfaceStateCallback : public NetsysControllerCallback { 291 public: 292 NetInterfaceStateCallback() = default; 293 ~NetInterfaceStateCallback() = default; 294 int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags, 295 int scope) override; 296 int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags, 297 int scope) override; 298 int32_t OnInterfaceAdded(const std::string &iface) override; 299 int32_t OnInterfaceRemoved(const std::string &iface) override; 300 int32_t OnInterfaceChanged(const std::string &iface, bool up) override; 301 int32_t OnInterfaceLinkStateChanged(const std::string &iface, bool up) override; 302 int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway, 303 const std::string &ifName) override; 304 int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) override; 305 int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override; 306 307 int32_t RegisterInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback); 308 309 private: 310 std::mutex mutex_; 311 std::vector<sptr<INetInterfaceStateCallback>> ifaceStateCallbacks_; 312 }; 313 314 private: 315 bool Init(); 316 std::list<sptr<NetSupplier>> GetNetSupplierFromList(NetBearType bearerType, const std::string &ident = ""); 317 sptr<NetSupplier> GetNetSupplierFromList(NetBearType bearerType, const std::string &ident, 318 const std::set<NetCap> &netCaps); 319 int32_t ActivateNetwork(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> &callback, 320 const uint32_t &timeoutMS); 321 void CallbackForSupplier(sptr<NetSupplier> &supplier, CallbackType type); 322 void CallbackForAvailable(sptr<NetSupplier> &supplier, const sptr<INetConnCallback> &callback); 323 uint32_t FindBestNetworkForRequest(sptr<NetSupplier> &supplier, std::shared_ptr<NetActivate> &netActivateNetwork); 324 void SendRequestToAllNetwork(std::shared_ptr<NetActivate> request); 325 void SendBestScoreAllNetwork(uint32_t reqId, int32_t bestScore, uint32_t supplierId); 326 void SendAllRequestToNetwork(sptr<NetSupplier> supplier); 327 void FindBestNetworkForAllRequest(); 328 void MakeDefaultNetWork(sptr<NetSupplier> &oldService, sptr<NetSupplier> &newService); 329 void NotFindBestSupplier(uint32_t reqId, const std::shared_ptr<NetActivate> &active, 330 const sptr<NetSupplier> &supplier, const sptr<INetConnCallback> &callback); 331 void CreateDefaultRequest(); 332 int32_t RegUnRegNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback, bool isReg); 333 int32_t GenerateNetId(); 334 bool FindSameCallback(const sptr<INetConnCallback> &callback, uint32_t &reqId); 335 void GetDumpMessage(std::string &message); 336 sptr<NetSupplier> FindNetSupplier(uint32_t supplierId); 337 int32_t RegisterNetSupplierAsync(NetBearType bearerType, const std::string &ident, const std::set<NetCap> &netCaps, 338 uint32_t &supplierId); 339 int32_t UnregisterNetSupplierAsync(uint32_t supplierId); 340 int32_t RegisterNetSupplierCallbackAsync(uint32_t supplierId, const sptr<INetSupplierCallback> &callback); 341 int32_t RegisterNetConnCallbackAsync(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> &callback, 342 const uint32_t &timeoutMS); 343 int32_t UnregisterNetConnCallbackAsync(const sptr<INetConnCallback> &callback); 344 int32_t RegUnRegNetDetectionCallbackAsync(int32_t netId, const sptr<INetDetectionCallback> &callback, bool isReg); 345 int32_t UpdateNetStateForTestAsync(const sptr<NetSpecifier> &netSpecifier, int32_t netState); 346 int32_t UpdateNetSupplierInfoAsync(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo); 347 int32_t UpdateNetLinkInfoAsync(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo); 348 int32_t NetDetectionAsync(int32_t netId); 349 int32_t RestrictBackgroundChangedAsync(bool restrictBackground); 350 void SendHttpProxyChangeBroadcast(const HttpProxy &httpProxy); 351 void RequestAllNetworkExceptDefault(); 352 void LoadGlobalHttpProxy(); 353 354 private: 355 enum ServiceRunningState { 356 STATE_STOPPED = 0, 357 STATE_RUNNING, 358 }; 359 360 bool registerToService_; 361 ServiceRunningState state_; 362 sptr<NetSpecifier> defaultNetSpecifier_ = nullptr; 363 std::shared_ptr<NetActivate> defaultNetActivate_ = nullptr; 364 sptr<NetSupplier> defaultNetSupplier_ = nullptr; 365 NET_SUPPLIER_MAP netSuppliers_; 366 NET_ACTIVATE_MAP netActivates_; 367 NET_NETWORK_MAP networks_; 368 std::unique_ptr<NetScore> netScore_ = nullptr; 369 sptr<NetConnServiceIface> serviceIface_ = nullptr; 370 std::atomic<int32_t> netIdLastValue_ = MIN_NET_ID - 1; 371 std::atomic<bool> isGlobalProxyLoaded_ = false; 372 HttpProxy globalHttpProxy_; 373 std::mutex netManagerMutex_; 374 std::shared_ptr<AppExecFwk::EventRunner> netConnEventRunner_ = nullptr; 375 std::shared_ptr<NetConnEventHandler> netConnEventHandler_ = nullptr; 376 std::shared_ptr<AppExecFwk::EventRunner> netActEventRunner_ = nullptr; 377 std::shared_ptr<AppExecFwk::EventHandler> netActEventHandler_ = nullptr; 378 sptr<NetInterfaceStateCallback> interfaceStateCallback_ = nullptr; 379 }; 380 } // namespace NetManagerStandard 381 } // namespace OHOS 382 #endif // NET_CONN_SERVICE_H 383