# NetManager ## Introduction The NetManager module is a tailorable component of the Telephony subsystem. Key components of the module include NetConnService (connection management), NetPolicyService (policy management), NetStatsService (traffic management), TetheringService (network sharing), VpnService (VPN management), and EtherNetService (Ethernet connection). **Figure 1** NetManager architecture ![net_conn_manager_arch](figures/net_conn_manager_arch.png) ## Directory Structure ``` foundation/communication/netmanager_base/ ├─figures // Architecture ├─frameworks // Framework layer │ ├─js // JavaScript APIs │ └─native // Native APIs ├─interfaces // API definitions │ ├─innerkits // JavaScript APIs │ └─kits // Native APIs ├─sa_profile // SA profile ├─services // IPC server-side implementation ├─test // Test code └─utils // Utilities ``` ## Available APIs | Type| API| Description| | ---- | ---- | ---- | | ohos.net.connection | function getDefaultNet(callback: AsyncCallback\): void; |Creates a **NetHandle** object that contains the **netId** of the default network. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function getDefaultNet(): Promise\; |Creates a **NetHandle** object that contains the **netId** of the default network. This API uses a promise to return the result.| | ohos.net.connection | function getAllNets(callback: AsyncCallback\>): void;| Obtains the list of **NetHandle** objects of the connected network. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function getAllNets(): Promise\>;| Obtains the list of **NetHandle** objects of the connected network. This API uses a promise to return the result.| | ohos.net.connection | function getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\): void; |Obtains link information of the default network. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function getConnectionProperties(netHandle: NetHandle): Promise\; |Obtains link information of the default network. This API uses a promise to return the result.| | ohos.net.connection | function getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\): void; |Obtains the capability set of the default network. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function getNetCapabilities(netHandle: NetHandle): Promise\; |Obtains the capability set of the default network. This API uses a promise to return the result.| | ohos.net.connection | function hasDefaultNet(callback: AsyncCallback\): void; |Checks whether the default network is available. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function hasDefaultNet(): Promise\; |Checks whether the default network is available. This API uses a promise to return the result.| | ohos.net.connection | function getAddressesByName(host: string, callback: AsyncCallback\>): void; |Obtains all IP addresses of the specified network by resolving the domain name. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function getAddressesByName(host: string): Promise\>; |Obtains all IP addresses of the specified network by resolving the domain name. This API uses a promise to return the result.| | ohos.net.connection | function createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection; | Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout interval in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used.| | ohos.net.connection | function enableAirplaneMode(callback: AsyncCallback\): void; | Enables the airplane mode. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function enableAirplaneMode(): Promise\;|Enables the airplane mode. This API uses a promise to return the result.| | ohos.net.connection | function disableAirplaneMode(callback: AsyncCallback\): void;| Disables the airplane mode. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function disableAirplaneMode(): Promise\;| Disables the airplane mode. This API uses a promise to return the result.| | ohos.net.connection | function reportNetConnected(netHandle: NetHandle, callback: AsyncCallback\): void;| Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function reportNetConnected(netHandle: NetHandle): Promise\;| Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses a promise to return the result.| | ohos.net.connection | function reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback\): void;| Reports a **netUnavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses an asynchronous callback to return the result.| | ohos.net.connection | function reportNetDisconnected(netHandle: NetHandle): Promise\;| Reports a **netUnavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses a promise to return the result.| | ohos.net.connection.NetHandle | bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\): void; | Binds TCPSocket or UDPSocket to the current network. This API uses an asynchronous callback to return the result.| | ohos.net.connection.NetHandle | bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\;| Binds TCPSocket or UDPSocket to the current network. This API uses a promise to return the result.| | ohos.net.connection.NetHandle | getAddressesByName(host: string, callback: AsyncCallback\>): void; |Obtains all IP addresses of the default network by resolving the domain name. This API uses an asynchronous callback to return the result.| | ohos.net.connection.NetHandle | getAddressesByName(host: string): Promise\>; |Obtains all IP addresses of the specified network by resolving the domain name. This API uses a promise to return the result.| | ohos.net.connection.NetHandle | getAddressByName(host: string, callback: AsyncCallback\): void; |Obtains an IP address of the specified network by resolving the domain name. This API uses an asynchronous callback to return the result.| | ohos.net.connection.NetHandle | getAddressByName(host: string): Promise\; |Obtains an IP address of the specified network by resolving the domain name. This API uses a promise to return the result.| | ohos.net.connection.NetConnection | on(type: 'netAvailable', callback: Callback\): void; |Listens to **netAvailable** events.| | ohos.net.connection.NetConnection | on(type: 'netCapabilitiesChange', callback: Callback\<{ netHandle: NetHandle, netCap: NetCapabilities }>): void; |Listens to **netCapabilitiesChange** events.| | ohos.net.connection.NetConnection | on(type: 'netConnectionPropertiesChange', callback: Callback\<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void; |Listens to **netConnectionPropertiesChange** events.| | ohos.net.connection.NetConnection | on(type: 'netLost', callback: Callback\): void; |Listens to **netLost** events.| | ohos.net.connection.NetConnection | on(type: 'netUnavailable', callback: Callback\): void; |Listens to **netUnavailable** events.| | ohos.net.connection.NetConnection | register(callback: AsyncCallback\): void; |Registers an observer for the default network or the network specified in **createNetConnection**.| | ohos.net.connection.NetConnection | unregister(callback: AsyncCallback\): void; |Unregisters the observer for the default network or the network specified in **createNetConnection**.| | @ohos.net.policy | function setBackgroundPolicy(allow: boolean, callback: AsyncCallback\): void; | Sets the background network policy. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function setBackgroundPolicy(allow: boolean): Promise\; | Sets the background network policy. This API uses a promise to return the result.| | @ohos.net.policy | function getBackgroundPolicy(callback: AsyncCallback\): void; | Obtains the background network policy. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function getBackgroundPolicy(): Promise\; | Obtains the background network policy. This API uses a promise to return the result.| | @ohos.net.policy | function setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback\): void; | Sets the network access policy for the application by the specified UID. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function setPolicyByUid(uid: number, policy: NetUidPolicy): Promise\; | Sets the network access policy for the application by the specified UID. This API uses a promise to return the result.| | @ohos.net.policy | function getPolicyByUid(uid: number, callback: AsyncCallback\): void; | Obtains the network access policy of the application by the specified UID. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function getPolicyByUid(uid: number): Promise\; | Obtains the network access policy of the application by the specified UID. This API uses a promise to return the result.| | @ohos.net.policy | function getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback\>): void; | Obtains the UID array of the application by the network access policy. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function getUidsByPolicy(policy: NetUidPolicy): Promise\>; | Obtains the UID array of the application by the network access policy. This API uses a promise to return the result.| | @ohos.net.policy | function getNetQuotaPolicies(callback: AsyncCallback\>): void; | Obtains the network quota policy. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function getNetQuotaPolicies(): Promise\>; | Obtains the network quota policy. This API uses a promise to return the result.| | @ohos.net.policy | function setNetQuotaPolicies(quotaPolicies: Array\, callback: AsyncCallback\): void; | Sets the network quota policy. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function setNetQuotaPolicies(quotaPolicies: Array\): Promise\; | Sets the network quota policy. This API uses a promise to return the result.| | @ohos.net.policy | function restoreAllPolicies(simId: string, callback: AsyncCallback\): void; | Restores all policies, including cellular network, background network, firewall, and application-specific policies, by the SIM ID. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function restoreAllPolicies(simId: string): Promise\; | Restores all policies, including cellular network, background network, firewall, and application-specific policies, by the SIM ID. This API uses a promise to return the result.| | @ohos.net.policy | function isUidNetAllowedIsMetered(uid: number, isMetered: boolean, callback: AsyncCallback\): void; | Checks whether the application corresponding to the specified UID can access the metering or non-metering network. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function isUidNetAllowedIsMetered(uid: number, isMetered: boolean): Promise\; | Checks whether the application corresponding to the specified UID can access the metering or non-metering network. This API uses a promise to return the result.| | @ohos.net.policy | function isUidNetAllowedIface(uid: number, iface: string, callback: AsyncCallback\): void; | Checks whether the application corresponding to the specified UID can access the network corresponding to the specified iface. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function isUidNetAllowedIface(uid: number, iface: string): Promise\; | Checks whether the application corresponding to the specified UID can access the network corresponding to the specified iface. This API uses a promise to return the result.| | @ohos.net.policy | function setDeviceIdleAllowlist(uid: number, isAllow: boolean, callback: AsyncCallback\): void | Sets whether the application corresponding to the specified UID is in the device idle allowlist. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function setDeviceIdleAllowlist(uid: number, isAllow: boolean): Promise\; | Sets whether the application corresponding to the specified UID is in the device idle allowlist. This API uses a promise to return the result.| | @ohos.net.policy | function getDeviceIdleAllowlist(callback: AsyncCallback\>): void | Obtains the UID array of the application in the device idle allowlist. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function getDeviceIdleAllowlist(): Promise\>; | Obtains the UID array of the application in the device idle allowlist. This API uses a promise to return the result.| | @ohos.net.policy | function getBackgroundPolicyByUid(uid: number, callback: AsyncCallback\): void | Checks whether the application corresponding to the specified UID can access the background network. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function getBackgroundPolicyByUid(uid: number): Promise\; | Checks whether the application corresponding to the specified UID can access the background network. This API uses a promise to return the result.| | @ohos.net.policy | function resetPolicies(simId: string, callback: AsyncCallback\): void; | Resets all policies, including cellular network, background network, firewall, and application-specific policies, by the SIM ID. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function resetPolicies(simId: string): Promise\; | Resets all policies, including cellular network, background network, firewall, and application-specific policies, by the SIM ID. This API uses a promise to return the result.| | @ohos.net.policy | function updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType, callback: AsyncCallback\): void | Updates the reminder policy. **netType** indicates the network type, **simId** indicates the SIM card ID, and **remindType** indicates the reminder type. This API uses an asynchronous callback to return the result.| | @ohos.net.policy | function updateRemindPolicy(netType: NetBearType, simId: string, remindType: RemindType): Promise\; | Updates the reminder policy. **netType** indicates the network type, **simId** indicates the SIM card ID, and **remindType** indicates the reminder type. This API uses a promise to return the result.| | @ohos.net.policy | function on(type: 'netUidPolicyChange', callback: Callback\<{ uid: number, policy: NetUidPolicy }>): void; | Registers the callback for policy changes.| | @ohos.net.policy | function off(type: 'netUidPolicyChange', callback: Callback\): void; | Unregisters the callback for policy changes.| | @ohos.net.policy | function on(type: "netUidRuleChange", callback: Callback\<{ uid: number, rule: NetUidRule }>): void; | Registers the callback for rule changes.| | @ohos.net.policy | function off(type: "netUidRuleChange", callback: Callback\): void; | Unregisters the callback for rule changes.| | @ohos.net.policy | function on(type: "netMeteredIfacesChange", callback: Callback\>): void; | Registers the callback for metered iface changes.| | @ohos.net.policy | function off(type: "netMeteredIfacesChange", callback: Callback\): void; | Unregisters the callback for metered iface changes.| | @ohos.net.policy | function on(type: "netQuotaPolicyChange", callback: Callback\>): void; | Registers the callback for network quota policy changes.| | @ohos.net.policy | function off(type: "netQuotaPolicyChange", callback: Callback\): void; | Unregisters the callback for network quota policy changes.| | @ohos.net.policy | function on(type: "netBackgroundPolicyChange", callback: Callback\): void; | Registers the callback for background network policy changes.| | @ohos.net.policy | function off(type: "netBackgroundPolicyChange", callback: Callback\): void; | Unregisters the callback for background network policy changes.| | ohos.net.statistics | function getIfaceRxBytes(nic: string, callback: AsyncCallback\): void; |Obtains the downlink traffic data of the specified NIC. This API uses an asynchronous callback to return the result.| | ohos.net.statistics | function getIfaceRxBytes(nic: string): Promise\; |Obtains the downlink traffic data of the specified NIC. This API uses a promise to return the result.| | ohos.net.statistics | function getIfaceTxBytes(nic: string, callback: AsyncCallback\): void; |Obtains the uplink traffic data of the specified NIC. This API uses an asynchronous callback to return the result.| | ohos.net.statistics | function getIfaceRxBytes(nic: string): Promise\; |Obtains the uplink traffic data of the specified NIC. This API uses a promise to return the result.| | ohos.net.statistics | function getCellularRxBytes(callback: AsyncCallback\): void; |Obtains the downlink traffic data of the specified cellular network. This API uses an asynchronous callback to return the result.| | ohos.net.statistics | function getCellularRxBytes(): Promise\; |Obtains the downlink traffic data of the specified cellular network. This API uses a promise to return the result.| | ohos.net.statistics | function getCellularTxBytes(callback: AsyncCallback\): void; |Obtains the uplink traffic data of the specified cellular network. This API uses an asynchronous callback to return the result.| | ohos.net.statistics | function getCellularTxBytes(): Promise\; |Obtains the uplink traffic data of the specified cellular network. This API uses a promise to return the result.| | ohos.net.statistics | function getAllRxBytes(callback: AsyncCallback\): void; |Obtains the downlink traffic data of all NICs. This API uses an asynchronous callback to return the result.| | ohos.net.statistics | function getAllRxBytes(): Promise\; |Obtains the downlink traffic data of all NICs. This API uses a promise to return the result.| | ohos.net.statistics | function getAllTxBytes(callback: AsyncCallback\): void; |Obtains the uplink traffic data of all NICs. This API uses an asynchronous callback to return the result.| | ohos.net.statistics | function getAllTxBytes(): Promise\; |Obtains the uplink traffic data of all NICs. This API uses a promise to return the result.| | ohos.net.statistics | function getUidRxBytes(uid: number, callback: AsyncCallback\): void; |Obtains the downlink traffic data of the specified application. This API uses an asynchronous callback to return the result.| | ohos.net.statistics | function getUidRxBytes(uid: number): Promise\; |Obtains the downlink traffic data of the specified application. This API uses a promise to return the result.| | ohos.net.statistics | function getUidTxBytes(uid: number, callback: AsyncCallback\): void; |Obtains the uplink traffic data of the specified application. This API uses an asynchronous callback to return the result.| | ohos.net.statistics | function getUidTxBytes(uid: number): Promise\; |Obtains the uplink traffic data of the specified application. This API uses a promise to return the result.| ## Usage ### Obtaining IP Addresses of the Default Network by Resolving the Domain Name (Promise Mode) * Example ```javascript import net_connection from "@ohos.net.connection" ``` ```javascript net_connection.getAddressesByName("www.example.com").then(function (addresses) { console.log(JSON.stringify(addresses)) }) ``` ### Registering an Observer for the Default Network * Example ```javascript import net_connection from "@ohos.net.connection" ``` ```javascript let netConnection = net_connection.createNetConnection() netConnection.on('netAvailable', function(data) { console.log(JSON.stringify(data)) }) netConnection.register(function (error) { if (error) { console.log(JSON.stringify(error)) } }) ``` ## Repositories Involved Network Management Subsystem **communication_netmanager_base** [communication_netmanager_ext](https://gitee.com/openharmony/communication_netmanager_ext) [communication_netstack](https://gitee.com/openharmony/communication_netstack)