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_MANAGER_H 17 #define NET_CONN_MANAGER_H 18 19 #include <map> 20 #include <string> 21 22 #include "parcel.h" 23 #include "singleton.h" 24 25 #include "http_proxy.h" 26 #include "i_net_conn_service.h" 27 #include "i_net_interface_callback.h" 28 #include "i_net_supplier_callback.h" 29 #include "net_handle.h" 30 #include "net_link_info.h" 31 #include "net_specifier.h" 32 #include "net_supplier_callback_base.h" 33 34 namespace OHOS { 35 namespace nmd { 36 class FwmarkClient; 37 } 38 namespace NetManagerStandard { 39 class NetConnClient { 40 public: 41 static NetConnClient &GetInstance(); 42 43 /** 44 * The interface in NetConnService can be called when the system is ready 45 * 46 * @return Returns 0, the system is ready, otherwise the system is not ready 47 * @permission ohos.permission.CONNECTIVITY_INTERNAL 48 * @systemapi Hide this for inner system use. 49 */ 50 int32_t SystemReady(); 51 52 /** 53 * The interface is set permission for network 54 * 55 * @param The specified UID of app 56 * @param allow internet permission 57 * @return Returns 0, unregister the network successfully, otherwise it will fail 58 * @permission ohos.permission.CONNECTIVITY_INTERNAL 59 * @systemapi Hide this for inner system use. 60 */ 61 int32_t SetInternetPermission(uint32_t uid, uint8_t allow); 62 63 /** 64 * The interface is register the network 65 * 66 * @param bearerType Bearer Network Type 67 * @param ident Unique identification of mobile phone card 68 * @param netCaps Network capabilities registered by the network supplier 69 * @param supplierId out param, return supplier id 70 * @return Returns 0, unregister the network successfully, otherwise it will fail 71 */ 72 int32_t RegisterNetSupplier(NetBearType bearerType, const std::string &ident, const std::set<NetCap> &netCaps, 73 uint32_t &supplierId); 74 75 /** 76 * The interface is unregister the network 77 * 78 * @param supplierId The id of the network supplier 79 * @return Returns 0, unregister the network successfully, otherwise it will fail 80 * @permission ohos.permission.CONNECTIVITY_INTERNAL 81 * @systemapi Hide this for inner system use. 82 */ 83 int32_t UnregisterNetSupplier(uint32_t supplierId); 84 85 /** 86 * Register supplier callback 87 * 88 * @param supplierId The id of the network supplier 89 * @param callback INetSupplierCallback callback interface 90 * @return Returns 0, unregister the network successfully, otherwise it will fail 91 * @permission ohos.permission.CONNECTIVITY_INTERNAL 92 * @systemapi Hide this for inner system use. 93 */ 94 int32_t RegisterNetSupplierCallback(uint32_t supplierId, const sptr<NetSupplierCallbackBase> &callback); 95 96 /** 97 * The interface is update network connection status information 98 * 99 * @param supplierId The id of the network supplier 100 * @param netSupplierInfo network connection status information 101 * @return Returns 0, successfully update the network connection status information, otherwise it will fail 102 * @permission ohos.permission.CONNECTIVITY_INTERNAL 103 * @systemapi Hide this for inner system use. 104 */ 105 int32_t UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo); 106 107 /** 108 * The interface is update network link attribute information 109 * 110 * @param supplierId The id of the network supplier 111 * @param netLinkInfo network link attribute information 112 * @return Returns 0, successfully update the network link attribute information, otherwise it will fail 113 * @permission ohos.permission.CONNECTIVITY_INTERNAL 114 * @systemapi Hide this for inner system use. 115 */ 116 int32_t UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo); 117 118 /** 119 * Register net connection callback 120 * 121 * @param callback The callback of INetConnCallback interface 122 * @return Returns 0, successfully register net connection callback, otherwise it will failed 123 * @permission ohos.permission.CONNECTIVITY_INTERNAL 124 * @systemapi Hide this for inner system use. 125 */ 126 int32_t RegisterNetConnCallback(const sptr<INetConnCallback> &callback); 127 128 /** 129 * Register net connection callback by NetSpecifier 130 * 131 * @param netSpecifier specifier information 132 * @param callback The callback of INetConnCallback interface 133 * @param timeoutMS net connection time out 134 * @return Returns 0, successfully register net connection callback, otherwise it will failed 135 * @permission ohos.permission.CONNECTIVITY_INTERNAL 136 * @systemapi Hide this for inner system use. 137 */ 138 int32_t RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> &callback, 139 const uint32_t &timeoutMS); 140 141 /** 142 * Unregister net connection callback 143 * 144 * @param callback The callback of INetConnCallback interface 145 * @return Returns 0, successfully unregister net connection callback, otherwise it will fail 146 * @permission ohos.permission.CONNECTIVITY_INTERNAL 147 * @systemapi Hide this for inner system use. 148 */ 149 int32_t UnregisterNetConnCallback(const sptr<INetConnCallback> &callback); 150 151 /** 152 * The interface is to get default network 153 * 154 * @param netHandle network handle 155 * @return Returns 0 success. Otherwise fail. 156 * @permission ohos.permission.CONNECTIVITY_INTERNAL 157 * @systemapi Hide this for inner system use. 158 * @permission ohos.permission.CONNECTIVITY_INTERNAL 159 * @systemapi Hide this for inner system use. 160 */ 161 int32_t GetDefaultNet(NetHandle &netHandle); 162 163 /** 164 * The interface is to check whether has default network 165 * 166 * @param flag has default network or not 167 * @return Returns 0 success. Otherwise fail. 168 * @permission ohos.permission.CONNECTIVITY_INTERNAL 169 * @systemapi Hide this for inner system use. 170 */ 171 int32_t HasDefaultNet(bool &flag); 172 173 /** 174 * The interface is to get all acvite network 175 * 176 * @param netList a list of network 177 * @return Returns 0 success. Otherwise fail. 178 * @permission ohos.permission.CONNECTIVITY_INTERNAL 179 * @systemapi Hide this for inner system use. 180 */ 181 int32_t GetAllNets(std::list<sptr<NetHandle>> &netList); 182 183 /** 184 * get the network link information of the connection 185 * 186 * @param netHandle network handle 187 * @param info network link infomation 188 * @return Returns 0 success. Otherwise fail. 189 * @permission ohos.permission.CONNECTIVITY_INTERNAL 190 * @systemapi Hide this for inner system use. 191 */ 192 int32_t GetConnectionProperties(const NetHandle &netHandle, NetLinkInfo &info); 193 194 /** 195 * get all capabilities from network 196 * 197 * @param netHandle network handle 198 * @param netAllCap network all of capabilities 199 * @return Returns 0 success. Otherwise fail. 200 * @permission ohos.permission.CONNECTIVITY_INTERNAL 201 * @systemapi Hide this for inner system use. 202 */ 203 int32_t GetNetCapabilities(const NetHandle &netHandle, NetAllCapabilities &netAllCap); 204 205 /** 206 * The interface is to get addresses by network name 207 * 208 * @param host domain name 209 * @param netId network id 210 * @param addrList list of network addresses 211 * @return Returns 0 success. Otherwise fail. 212 * @permission ohos.permission.CONNECTIVITY_INTERNAL 213 * @systemapi Hide this for inner system use. 214 */ 215 int32_t GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList); 216 217 /** 218 * The interface is to get address by network name 219 * 220 * @param host domain name 221 * @param netId network 222 * @param addr network address 223 * @return Returns 0 success. Otherwise fail. 224 * @permission ohos.permission.CONNECTIVITY_INTERNAL 225 * @systemapi Hide this for inner system use. 226 */ 227 int32_t GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr); 228 229 /** 230 * The interface is to bind socket 231 * 232 * @param socket_fd socket file description 233 * @param netId network id 234 * @return Returns 0 success. Otherwise fail. 235 * @permission ohos.permission.CONNECTIVITY_INTERNAL 236 * @systemapi Hide this for inner system use. 237 */ 238 int32_t BindSocket(int32_t socket_fd, int32_t netId); 239 240 /** 241 * The interface of network detection called by the application 242 * 243 * @param netHandle network handle 244 * @return int32_t Whether the network probe is successful 245 * @permission ohos.permission.CONNECTIVITY_INTERNAL 246 * @systemapi Hide this for inner system use. 247 */ 248 int32_t NetDetection(const NetHandle &netHandle); 249 250 /** 251 * set air plane mode on or off 252 * 253 * @param state air plane mode on or not 254 * @return Returns 0 success. Otherwise fail. 255 * @permission ohos.permission.CONNECTIVITY_INTERNAL 256 * @systemapi Hide this for inner system use. 257 */ 258 int32_t SetAirplaneMode(bool state); 259 260 /** 261 * check whether the network meter is default 262 * 263 * @param isMetered the network meter is default or not 264 * @return Returns 0 success. Otherwise fail. 265 * @permission ohos.permission.CONNECTIVITY_INTERNAL 266 * @systemapi Hide this for inner system use. 267 */ 268 int32_t IsDefaultNetMetered(bool &isMetered); 269 270 /** 271 * set global http proxy in the network 272 * 273 * @param httpProxy http proxy 274 * @return Returns 0 success. Otherwise fail. 275 * @permission ohos.permission.CONNECTIVITY_INTERNAL 276 * @systemapi Hide this for inner system use. 277 */ 278 int32_t SetGlobalHttpProxy(const HttpProxy &httpProxy); 279 280 /** 281 * get global http proxy in the network 282 * 283 * @param httpProxy http proxy 284 * @return Returns 0 success. Otherwise fail. 285 * @permission ohos.permission.CONNECTIVITY_INTERNAL 286 * @systemapi Hide this for inner system use. 287 */ 288 int32_t GetGlobalHttpProxy(HttpProxy &httpProxy); 289 290 /** 291 * set network id of app binding network 292 * 293 * @param netId network id 294 * @return Returns 0 success. Otherwise fail. 295 * @permission ohos.permission.CONNECTIVITY_INTERNAL 296 * @systemapi Hide this for inner system use. 297 */ 298 int32_t GetDefaultHttpProxy(HttpProxy &httpProxy); 299 300 /** 301 * set network id of app binding network 302 * 303 * @param netId network id 304 * @return Returns 0 success. Otherwise fail. 305 * @permission ohos.permission.CONNECTIVITY_INTERNAL 306 * @systemapi Hide this for inner system use. 307 */ 308 int32_t SetAppNet(int32_t netId); 309 310 /** 311 * get network id of app binding network 312 * 313 * @param netId network id 314 * @return Returns 0 success. Otherwise fail. 315 * @permission ohos.permission.CONNECTIVITY_INTERNAL 316 * @systemapi Hide this for inner system use. 317 */ 318 int32_t GetAppNet(int32_t &netId); 319 320 /** 321 * Get network id by identifier 322 * 323 * @param ident identifier 324 * @param netIdList list of network id 325 * @return Returns 0 success. Otherwise fail. 326 * @permission ohos.permission.CONNECTIVITY_INTERNAL 327 * @systemapi Hide this for inner system use. 328 */ 329 int32_t GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList); 330 331 /** 332 * Register network interface state change callback 333 * 334 * @param callback The callback of INetInterfaceStateCallback interface 335 * @return Returns 0, successfully register net connection callback, otherwise it will failed 336 * @permission ohos.permission.CONNECTIVITY_INTERNAL 337 * @systemapi Hide this for inner system use. 338 */ 339 int32_t RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback); 340 341 /** 342 * Get network interface configuration 343 * 344 * @param ifaceName Network port device name 345 * @param config Network interface configuration 346 * @return Returns 0, successfully register net connection callback, otherwise it will failed 347 * @permission ohos.permission.CONNECTIVITY_INTERNAL 348 * @systemapi Hide this for inner system use. 349 */ 350 int32_t GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config); 351 352 private: 353 class NetConnDeathRecipient : public IRemoteObject::DeathRecipient { 354 public: NetConnDeathRecipient(NetConnClient & client)355 explicit NetConnDeathRecipient(NetConnClient &client) : client_(client) {} 356 ~NetConnDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & remote)357 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 358 { 359 client_.OnRemoteDied(remote); 360 } 361 362 private: 363 NetConnClient &client_; 364 }; 365 366 private: 367 NetConnClient(); 368 ~NetConnClient(); 369 NetConnClient& operator=(const NetConnClient&) = delete; 370 NetConnClient(const NetConnClient&) = delete; 371 372 sptr<INetConnService> GetProxy(); 373 void OnRemoteDied(const wptr<IRemoteObject> &remote); 374 375 private: 376 std::mutex mutex_; 377 sptr<INetConnService> NetConnService_; 378 sptr<IRemoteObject::DeathRecipient> deathRecipient_; 379 std::map<uint32_t, sptr<INetSupplierCallback>> netSupplierCallback_; 380 }; 381 } // namespace NetManagerStandard 382 } // namespace OHOS 383 384 #endif // NET_CONN_MANAGER_H 385