js-apis-net-ethernet.md 12.4 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.net.ethernet
G
Gloria 已提交
2

S
shawn_he 已提交
3
The **ethernet** module provides wired network capabilities, which allow users to set the IP address, subnet mask, gateway, and Domain Name System (DNS) server of a wired network.
G
Gloria 已提交
4

S
shawn_he 已提交
5
> **NOTE**<br>
G
Gloria 已提交
6 7 8 9 10 11 12 13 14 15
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

```js
import ethernet from '@ohos.net.ethernet'
```

## ethernet.setIfaceConfig

S
shawn_he 已提交
16
setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback\<void>): void
G
Gloria 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

Sets the network interface configuration. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                             | Mandatory| Description                                      |
| -------- | ------------------------------------------------- | ---- | ------------------------------------------ |
| iface    | string                                            | Yes  | Name of the network interface.                                    |
| ic       | [InterfaceConfiguration](#interfaceconfiguration) | Yes  | Network interface configuration to set.                  |
| callback | AsyncCallback\<void>                     | Yes  | Callback used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.|

**Example**

```js
ethernet.setIfaceConfig("eth0", {mode:ethernet.STATIC,ipAddr:"192.168.1.123", routeAddr:"192.168.1.1",
    gateAddr:"192.168.1.1", maskAddr:"255.255.255.0", dnsAddr0:"1.1.1.1", dnsAddr1:"2.2.2.2"},
    (error) => {
        if (error) {
            console.log("setIfaceConfig callback error = " + error);
        } else {
            console.log("setIfaceConfig callback ok ");
        }
    });
```

## ethernet.setIfaceConfig

S
shawn_he 已提交
48
setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise\<void>
G
Gloria 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

Sets the network interface configuration. This API uses a promise to return the result.

**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name| Type                                             | Mandatory| Description                    |
| ------ | ------------------------------------------------- | ---- | ------------------------ |
| iface  | string                                            | Yes  | Name of the network interface.                  |
| ic     | [InterfaceConfiguration](#interfaceconfiguration) | Yes  | Network interface configuration to set.|

**Return value**

| Type               | Description                                                       |
| ------------------- | ----------------------------------------------------------- |
| Promise\<void>       | Promise that returns no value.|

**Example**

```js
ethernet.setIfaceConfig("eth0", {mode:ethernet.STATIC,ipAddr:"192.168.1.123", routeAddr:"192.168.1.1",
    gateAddr:"192.168.1.1", maskAddr:"255.255.255.0", dnsAddr0:"1.1.1.1", dnsAddr1:"2.2.2.2"}).then(() => {
    console.log("setIfaceConfig promiss ok ");
}).catch((error) => {
    console.log("setIfaceConfig promiss error = " + error);
});
```

## ethernet.getIfaceConfig

S
shawn_he 已提交
82
getIfaceConfig(iface: string, callback: AsyncCallback\<InterfaceConfiguration>): void
G
Gloria 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

Obtains the configuration of a network interface. This API uses an asynchronous callback to return the result. 

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                           | Mandatory | Description        |
| -------- | ----------------------------------------------- | ----- | ------------ |
| iface    | string                                          | Yes   | Name of the network interface.|
| callback | AsyncCallback\<[InterfaceConfiguration](#interfaceconfiguration)> | Yes   | Callback used to return the configuration.  |

**Example**

```js
ethernet.getIfaceConfig("eth0", (error, value) => {
    if (error) {
        console.log("getIfaceConfig  callback error = " + error);
    } else {
        console.log("getIfaceConfig callback mode = " + value.mode);
        console.log("getIfaceConfig callback ipAddr = " + value.ipAddr);
        console.log("getIfaceConfig callback routeAddr = " + value.routeAddr);
        console.log("getIfaceConfig callback gateAddr = " + value.gateAddr);
        console.log("getIfaceConfig callback maskAddr = " + value.maskAddr);
        console.log("getIfaceConfig callback dns0Addr = " + value.dns0Addr);
        console.log("getIfaceConfig callback dns1Addr = " + value.dns1Addr);
    }
});
```

## ethernet.getIfaceConfig

S
shawn_he 已提交
117
getIfaceConfig(iface: string): Promise\<InterfaceConfiguration>
G
Gloria 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

Obtains the configuration of a network interface. This API uses a promise to return the result. 

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                   | Mandatory| Description        |
| -------- | --------------------------------------- | ---- | ------------ |
| iface    | string                                  | Yes  | Name of the network interface.|

**Return value**

| Type                             | Description                              |
| --------------------------------- | ---------------------------------- |
| Promise\<[InterfaceConfiguration](#interfaceconfiguration)>   | Promise used to return the configuration.       |

**Example**

```js
ethernet.getIfaceConfig("eth0").then((data) => {
    console.log("getIfaceConfig promiss mode = " + data.mode);
    console.log("getIfaceConfig promiss ipAddr = " + data.ipAddr);
    console.log("getIfaceConfig promiss routeAddr = " + data.routeAddr);
    console.log("getIfaceConfig promiss gateAddr = " + data.gateAddr);
    console.log("getIfaceConfig promiss maskAddr = " + data.maskAddr);
    console.log("getIfaceConfig promiss dns0Addr = " + data.dns0Addr);
    console.log("getIfaceConfig promiss dns1Addr = " + data.dns1Addr);
}).catch((error) => {
    console.log("getIfaceConfig promiss error = " + error);
});
```

## ethernet.isIfaceActive

S
shawn_he 已提交
155
isIfaceActive(iface?: string, callback: AsyncCallback\<number>): void
G
Gloria 已提交
156 157 158 159 160 161 162 163 164 165 166

Checks whether a network interface is active. This API uses an asynchronous callback to return the result.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                       | Mandatory| Description                                              |
| -------- | --------------------------- | ---- | -------------------------------------------------- |
S
shawn_he 已提交
167
| iface    | string                      | Yes  | Name of the network interface. If this parameter is left empty, the API checks for any active network interface.            |
G
Gloria 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| callback | AsyncCallback\<number>       | Yes  | Callback used to return the result. The value **1** means that the network interface is active, **0** means that the network interface is inactive, and any other value means that an error has occurred.|

**Example**

```js
ethernet.isIfaceActive("eth0", (error, value) => {
  if (error) {
    console.log("whether2Activate callback error = " + error);
  } else {
    console.log("whether2Activate callback = " + value);
  }
});
```

## ethernet.isIfaceActive

S
shawn_he 已提交
184
isIfaceActive(iface: string): Promise\<number>
G
Gloria 已提交
185 186 187 188 189 190 191 192 193 194 195

Checks whether a network interface is active. This API uses a promise to return the result.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
S
shawn_he 已提交
196
| iface  | string | Yes  | Name of the network interface. If this parameter is left empty, the API checks for any active network interface.|
G
Gloria 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

**Return value**

| Type           | Description                                                              |
| ----------------| ------------------------------------------------------------------ |
| Promise\<number> | Promise used to return the result. The value **1** means that the network interface is active, **0** means that the network interface is inactive, and any other value means that an error has occurred.|

**Example**

```js
ethernet.isIfaceActive("eth0").then((data) => {
  console.log("isIfaceActive promiss = " + data);
}).catch((error) => {
  console.log("isIfaceActive promiss error = " + error);
});
```

## ethernet.getAllActiveIfaces

S
shawn_he 已提交
216
getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void
G
Gloria 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

Obtains all active network interfaces. This API uses an asynchronous callback to return the result.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

| Name  | Type                                | Mandatory| Description                          |
| -------- | ------------------------------------ | ---- | ------------------------------ |
| callback | AsyncCallback\<Array\<string>>         | Yes  | Callback used to return all the active network interface names obtained.|

**Example**

```js
ethernet.getAllActiveIfaces((error, value) => {
  if (error) {
    console.log("getAllActiveIfaces callback error = " + error);
  } else {
      console.log("getAllActiveIfaces callback value.length = " + value.length);
    for (let i = 0; i < value.length; i++) {
      console.log("getAllActiveIfaces callback = " + value[i]);
    }
  }
});
```

## ethernet.getAllActiveIfaces

S
shawn_he 已提交
247
getAllActiveIfaces(): Promise\<Array\<string>>
G
Gloria 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

Obtains all active network interfaces. This API uses a promise to return the result.

**Required permission**: ohos.permission.GET_NETWORK_INFO

**System capability**: SystemCapability.Communication.NetManager.Core

**Parameters**

**Return value**

| Type                          | Description                                           |
| ------------------------------ | ----------------------------------------------- |
| Promise\<Array\<string>>         | Promise used to return all the active network interface names obtained.|

**Example**

```js
ethernet.getAllActiveIfaces().then((data) => {
    console.log("getAllActiveIfaces promiss data.length = " + data.length);
  for (let i = 0; i < data.length; i++) {
    console.log("getAllActiveIfaces promiss  = " + data[i]);
  }
}).catch((error) => {
  console.log("getAllActiveIfaces promiss error = " + error);
});
```

## InterfaceConfiguration

Defines the network configuration for the Ethernet connection.

**System capability**: SystemCapability.Communication.NetManager.Core

S
shawn_he 已提交
282 283 284 285 286 287 288 289
| Name     | Type    | Mandatory | Description |
| -------- | ------- | --------- | ----------- |
| mode         | [IPSetMode](#ipsetmode) | Yes | Configuration mode of the Ethernet connection.|
| ipAddr       | string                  | Yes | Static IP address of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in Dynamic Host Configuration Protocol (DHCP) mode.|
| route        | string                  | Yes | Route of the Ethernet connection. The value must be an IPv4 address. This parameter does not need to be configured in DHCP mode.|
| gateway      | string                  | Yes | Gateway of the Ethernet connection. The value must be an IPv4 address. This parameter does not need to be configured in DHCP mode.|
| netMask      | string                  | Yes | Subnet mask of the Ethernet connection. The value must be an IPv4 address. This parameter does not need to be configured in DHCP mode.|
| dnsServers   | string                  | Yes | DNS server addresses of the Ethernet connection. The value must be an IPv4 address. This parameter does not need to be configured in DHCP mode. Multiple addresses are separated by commas (,).|
G
Gloria 已提交
290 291 292 293 294 295 296 297 298 299 300

## IPSetMode

Defines the configuration mode of the Ethernet connection.

**System capability**: SystemCapability.Communication.NetManager.Core

| Name                 | Value  | Description                  |
| ------------------------ | ---- | ---------------------- |
| STATIC | 0 | Static configuration.|
| DHCP   | 1 | Dynamic configuration.|