1# Net Manager<a name="EN-US_TOPIC_0000001105058232"></a> 2 3- [简介](#简介) 4- [目录](#目录) 5- [约束](#约束) 6- [接口说明](#接口说明) 7- [使用说明](#使用说明) 8- [相关仓](#相关仓) 9 10 11 12## 简介 13 14网络管理介绍: 15 16 网络管理主要分为连接管理、策略管理、流量管理、网络共享、VPN管理以及以太网连接等模块,其中连接管理、策略管理、流量管理为基础服务,归档在netmanager_base仓,以太网连接、网络共享、VPN管理三个模块为可裁剪扩展模块,归档在netmanager_ext仓,netmanager_ext编译构建依赖netmanager_base库内容。如图1:网络管理架构图; 17 18**图 1** 网络管理架构图 19 20 21 22## 目录 23 24``` 25foundation/communication/netmanager_ext/ 26├─figures #README用于的png图片 27├─frameworks #框架层目录 28│ ├─js #js接口实现 29│ └─native #C++接口实现 30├─interfaces #C++接口头文件 31│ ├─innerkits #部件间的内部接口 32│ └─kits #对应用提供的接口(例如JS接口) 33├─sa_profile #SA服务配置文件 34├─services #C++内部实现 35│ ├─ethernetmanager #以太网模块 36│ └─networksharemanager #网络共享模块 37├─test #单元测试代码 38│ ├─ethernetmanager #以太网模块单元测试 39│ └─networkshare #网络共享模块单元测试 40└─utils #核心服务工具代码目录 41 ├─event_report #核心服务事件工具目录 42 └─log #核心服务日志工具目录 43``` 44 45## 约束 46 47- 开发语言:C++ JS 48 49## 接口说明 50 51| 类型 | 接口 | 功能说明 | 52| ---- | ---- | ---- | 53| ohos.net.ethernet | function setIfaceConfig\(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback<void\>\): void; |设置网络接口配置信息,调用callback | 54| ohos.net.ethernet | function setIfaceConfig\(iface: string, ic: InterfaceConfiguration\): Promise<void\>; |设置网络接口配置信息,返回Promise | 55| ohos.net.ethernet | function getIfaceConfig\(iface: string, callback: AsyncCallback<InterfaceConfiguration\>\): void; |获取网络接口配置信息,调用callback | 56| ohos.net.ethernet | function getIfaceConfig\(iface: string\): Promise<InterfaceConfiguration\>; |获取网络接口配置信息,返回Promise | 57| ohos.net.ethernet | function isIfaceActive\(iface?: string, callback: AsyncCallback<number\>\): void; |判断接口是否已激活,调用callback | 58| ohos.net.ethernet | function isIfaceActive\(iface?: string\): Promise<number\>; |判断接口是否已激活,返回Promise | 59| ohos.net.ethernet | function getAllActiveIfaces\(callback: AsyncCallback<Array\<string\>\>\): void; |获取活动的网络接口,调用callback | 60| ohos.net.ethernet | function getAllActiveIfaces\(\): Promise<Array\<string\>\>; |获取活动的网络接口,返回Promise | 61| ohos.net.ethernet | function getEthernetDeviceInfos\(\): Promise<Array\<EthernetDeviceInfos\>\>; |获取以太网设备信息,返回Promise | 62| ohos.net.sharing | function isSharingSupported(callback: AsyncCallback\<boolean>): void; | 获取当前系统是否支持网络共享,调用callback | 63| ohos.net.sharing | function isSharingSupported(): Promise\<boolean>; | 获取当前系统是否支持网络共享,返回promise | 64| ohos.net.sharing | function isSharing(callback: AsyncCallback\<boolean>): void; | 获取当前共享状态,调用callback | 65| ohos.net.sharing | function isSharing(): Promise\<boolean>; | 获取当前共享状态,返回promise | 66| ohos.net.sharing | function startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | 开启共享,type为共享类型,目前支持Wifi热点、蓝牙、USB,调用callback | 67| ohos.net.sharing | function startSharing(type: SharingIfaceType): Promise\<void>; | 开启共享,type为共享类型,目前支持Wifi热点、蓝牙、USB,返回promise | 68| ohos.net.sharing | function stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void; | 停止指定类型共享,type为共享类型,包括Wifi热点、蓝牙、USB,调用callback | 69| ohos.net.sharing | function stopSharing(type: SharingIfaceType): Promise\<void>; | 停止指定类型共享,type为共享类型,包括Wifi热点、蓝牙、USB,返回promise | 70| ohos.net.sharing | function getStatsRxBytes(callback: AsyncCallback\<number>): void; | 获取共享接收数据量,单位KB,调用callback | 71| ohos.net.sharing | function getStatsRxBytes(): Promise\<number>; | 获取共享接收数据量,单位KB,返回promise | 72| ohos.net.sharing | function getStatsTxBytes(callback: AsyncCallback\<number>): void; | 获取共享发送数据量,单位KB,调用callback | 73| ohos.net.sharing | function getStatsTxBytes(): Promise\<number>; | 获取共享发送数据量,单位KB,返回promise | 74| ohos.net.sharing | function getStatsTotalBytes(callback: AsyncCallback\<number>): void; | 获取共享总数据流量,单位KB,调用callback | 75| ohos.net.sharing | function getStatsTotalBytes(): Promise\<number>; | 获取共享总数据流量,单位KB,返回promise | 76| ohos.net.sharing | function getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\<Array\<string>>): void; | 获取指定状态的网卡名称,state为状态,包括正在共享、可共享、共享错误,调用callback | 77| ohos.net.sharing | function getSharingIfaces(state: SharingIfaceState): Promise\<Array\<string>>; | 获取指定状态的网卡名称,state为状态,包括正在共享、可共享、共享错误,返回promise | 78| ohos.net.sharing | function getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceState>): void; | 获取指定类型共享状态,type为类型,目前支持Wifi热点、蓝牙、USB,调用callback | 79| ohos.net.sharing | function getSharingState(type: SharingIfaceType): Promise\<SharingIfaceState>; | 获取指定类型共享状态,type为类型,目前支持Wifi热点、蓝牙、USB,返回promise | 80| ohos.net.sharing | function getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<string>>): void; | 获取与指定类型匹配的网卡正则表达式列表,type为类型,目前支持Wifi热点、蓝牙、USB,调用callback | 81| ohos.net.sharing | function getSharableRegexes(type: SharingIfaceType): Promise\<Array\<string>>; | 获取与指定类型匹配的网卡正则表达式列表,type为类型,目前支持Wifi热点、蓝牙、USB,返回promise | 82| ohos.net.sharing | function on(type: 'sharingStateChange', callback: Callback\<boolean>): void; | 注册共享状态改变监听 | 83| ohos.net.sharing | function off(type: 'sharingStateChange', callback?: Callback\<boolean>): void; | 注销共享状态改变监听 | 84| ohos.net.sharing | unction on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void; | 注册指定网卡共享状态改变监听 | 85| ohos.net.sharing | function off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void; | 注销指定网卡共享状态改变监听 | 86| ohos.net.sharing | function on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void; | 注册上行网卡改变监听 | 87| ohos.net.sharing | function off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void; | 注销上行网卡改变监听 | 88 89## 接口使用说明 90 91### 设置网络接口配置信息 92 93* 示例 94 ```javascript 95 import ethernet from '@ohos.net.ethernet' 96 ``` 97 ```javascript 98 ethernet.setIfaceConfig("eth0", {mode:ethernet.STATIC,ipAddr:"192.168.1.123", routeAddr:"192.168.1.1", 99 gateAddr:"192.168.1.1", maskAddr:"255.255.255.0", dnsAddr0:"1.1.1.1", dnsAddr1:"2.2.2.2"}, 100 (error) => { 101 if (error) { 102 console.log("setIfaceConfig callback error = " + error); 103 } else { 104 console.log("setIfaceConfig callback ok "); 105 } 106 }); 107 ``` 108 109### 获取网络接口配置信息 110 111* 示例 112 ```javascript 113 import ethernet from '@ohos.net.ethernet' 114 ``` 115 ```javascript 116 ethernet.getIfaceConfig("eth0", (error, value) => { 117 if (error) { 118 console.log("getIfaceConfig callback error = " + error); 119 } else { 120 console.log("getIfaceConfig callback mode = " + value.mode); 121 console.log("getIfaceConfig callback ipAddr = " + value.ipAddr); 122 console.log("getIfaceConfig callback routeAddr = " + value.routeAddr); 123 console.log("getIfaceConfig callback gateAddr = " + value.gateAddr); 124 console.log("getIfaceConfig callback maskAddr = " + value.maskAddr); 125 console.log("getIfaceConfig callback dns0Addr = " + value.dns0Addr); 126 console.log("getIfaceConfig callback dns1Addr = " + value.dns1Addr); 127 } 128 }); 129 ``` 130 131### 开始网络共享 132 133* 示例 134 ```javascript 135 import sharing from '@ohos.net.sharing' 136 ``` 137 ```javascript 138 sharing.startSharing(this.sharingType,(err)=>{ 139 this.callBack(err); 140 }) 141 ``` 142 143### 停止网络共享 144 145* 示例 146 ```javascript 147 import sharing from '@ohos.net.sharing' 148 ``` 149 ```javascript 150 sharing.stopSharing(this.sharingType,(err)=>{ 151 this.callBack(err); 152 }) 153 ``` 154 155## 相关仓 156 157[网络管理子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E7%BD%91%E7%BB%9C%E7%AE%A1%E7%90%86%E5%AD%90%E7%B3%BB%E7%BB%9F.md) 158 159**communication_netmanager_ext** 160 161[communication_netmanager_base](https://gitee.com/openharmony/communication_netmanager_base) 162 163[communication_netstack](https://gitee.com/openharmony/communication_netstack) 164