A virtual private network (VPN) is a dedicated network established on a public network. On a VPN, the connection between any two nodes does not have an end-to-end physical link required by the traditional private network. Instead, user data is transmitted over a logical link because a VPN is a logical network deployed over the network platform (such as the Internet) provided by the public network service provider.
> **NOTE**
> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the callback mode. For details about the APIs, see [Traffic Management](../reference/apis/js-apis-net-vpn.md).
The following describes the development procedure specific to each application scenario.
## Available APIs
For the complete list of APIs and example code, see [VPN Management](../reference/apis/js-apis-net-vpn.md).
| Type| API| Description|
| ---- | ---- | ---- |
| ohos.net.vpn | setUp(config: VpnConfig, callback: AsyncCallback\<number\>): void | Establishes a VPN. This API uses an asynchronous callback to return the result.|
| ohos.net.vpn | protect(socketFd: number, callback: AsyncCallback\<void\>): void | Enables VPN tunnel protection. This API uses an asynchronous callback to return the result.|
| ohos.net.vpn | destroy(callback: AsyncCallback\<void\>): void | Destroys a VPN. This API uses an asynchronous callback to return the result.|
## Starting a VPN
1. Establish a VPN tunnel. The following uses the UDP tunnel as an example.
2. Enable protection for the UDP tunnel.
3. Establish a VPN.
4. Process data of the virtual network interface card (vNIC), such as reading or writing data.
5. Destroy the VPN.
This example shows how to develop an application using native C++ code. For details, see [Simple Native C++ Example (ArkTS) (API9)] (https://gitee.com/openharmony/codelabs/tree/master/NativeAPI/NativeTemplateDemo).
The sample application consists of two parts: JS code and C++ code.
## JS Code
The JS code is used to implement the service logic, such as creating a tunnel, establishing a VPN, enabling VPN protection, and destroying a VPN.
The **vpn** module implements virtual private network (VPN) management, such as starting and stopping a VPN.
> **NOTE**
> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Defines a VPN connection object. Before calling **VpnConnection** APIs, you need to create a VPN connection object by calling [vpn.createVpnConnection](#vpncreatevpnconnection).
| callback | AsyncCallback\<number\> | Yes | Callback used to return the result. If a VPN is created successfully, **error** is **undefined** and **data** is the file descriptor of the vNIC. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [VPN Error Codes](../errorcodes/errorcode-net-vpn.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2203001 | VPN creation denied, please check the user type. |
| Promise\<number\> | The obtaining result is returned in Promise format. The file descriptor fd of the specified virtual network adapter is returned.|
**Error codes**
For details about the error codes, see [VPN Error Codes](../errorcodes/errorcode-net-vpn.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2203001 | VPN creation denied, please check the user type. |
Protects sockets against a VPN connection. The data sent through sockets is directly transmitted over the physical network and therefore the traffic does not traverse through the VPN. This API uses an asynchronous callback to return the result.
| socketFd | number | Yes | Socket file descriptor. It can be obtained through [getSocketFd](js-apis-socket.md#getsocketfd10). |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined**. If the operation fails, an error message is returned.|
**Error codes**
For details about the error codes, see [VPN Error Codes](../errorcodes/errorcode-net-vpn.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2203004 | Invalid socket file descriptor. |
**Example**
```js
importsocketfrom"@ohos.net.socket";
vartcp=socket.constructTCPSocketInstance();
tcp.bind({
address:"0.0.0.0",
family:1
})
letconnectAddress={
address:"192.168.1.11",
port:8888,
family:1
};
tcp.connect({
address:connectAddress,timeout:6000
})
tcp.getSocketFd().then((tunnelfd)=>{
console.info("tunenlfd: "+tunnelfd);
VpnConnection.protect(tunnelfd,(error)=>{
console.info(JSON.stringify(error));
})
})
```
### protect
protect(socketFd: number): Promise\<void\>
Protects sockets against a VPN connection. The data sent through sockets is directly transmitted over the physical network and therefore traffic does not traverse through the VPN. This API uses a promise to return the result.
| Promise\<void\> | Promise used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
**Error codes**
For details about the error codes, see [VPN Error Codes](../errorcodes/errorcode-net-vpn.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
| 2203004 | Invalid socket file descriptor. |
**Example**
```js
importsocketfrom"@ohos.net.socket";
vartcp=socket.constructTCPSocketInstance();
tcp.bind({
address:"0.0.0.0",
family:1
})
letconnectAddress={
address:"192.168.1.11",
port:8888,
family:1
};
tcp.connect({
address:connectAddress,timeout:6000
})
tcp.getSocketFd().then((tunnelfd)=>{
console.info("tunenlfd: "+tunnelfd);
VpnConnection.protect(tunnelfd).then(()=>{
console.info("protect success.")
}).catch(err=>{
console.info("protect fail"+JSON.stringify(err))
})
})
```
### destroy
destroy(callback: AsyncCallback\<void\>): void
Destroys a VPN. This API uses an asynchronous callback to return the result.
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined**. If the operation fails, an error message is returned.|
**Error codes**
For details about the error codes, see [VPN Error Codes](../errorcodes/errorcode-net-vpn.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error. |
**Example**
```js
VpnConnection.destroy((error)=>{
console.info(JSON.stringify(error));
})
```
### destroy
destroy(): Promise\<void\>
Destroys a VPN. This API uses a promise to return the result.
| Promise\<void\> | Promise used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
**Error codes**
For details about the error codes, see [VPN Error Codes](../errorcodes/errorcode-net-vpn.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 2200002 | Operation failed. Cannot connect to service. |
## 3301100 Location Service Unavailable Because of Switch Toggled Off
**Error Message**
**Error Information**
The location switch is off.
...
...
@@ -44,15 +44,15 @@ The location service switch is toggled off, which makes basic functions such as
Display a prompt asking for enabling the location service.
## 3301200 Failure to Obtain the Positioning Result
## 3301200 Failed to Obtain the Positioning Result
**Error Message**
**Error Information**
Failed to obtain the geographical location.
**Description**
This error code is reported when the location service fails, and no positioning result is obtained.
This error code is reported if the location service has failed, leading to a failure to obtain the positioning result.
**Possible Causes**
...
...
@@ -64,15 +64,15 @@ This error code is reported when the location service fails, and no positioning
Initiate a positioning request again.
## 3301300 Reverse Geocoding Query Failure
## 3301300 Query Failed During Reverse Geocoding
**Error Message**
**Error Information**
Reverse geocoding query failed.
**Description**
This error code is reported for a reverse geocoding query failure.
This error code is reported if the query during reverse geocoding has failed.
**Possible Causes**
...
...
@@ -80,17 +80,17 @@ Network connection is poor, which makes the request fail to be sent from the dev
**Solution**
Try the reverse geocoding query again.
Perform a query again.
## 3301400 Geocoding Query Failure
## 3301400 Query Failed During Geocoding
**Error Message**
**Error Information**
Geocoding query failed.
**Description**
This error code is reported for a geocoding query failure.
This error code is reported if the query during geocoding has failed.
**Possible Causes**
...
...
@@ -98,17 +98,17 @@ Network connection is poor, which makes the request fail to be sent from the dev
**Solution**
Try the geocoding query again.
Perform a query again.
## 3301500 Area Information Query Failure
## 3301500 Area Information Query Failed
**Error Message**
**Error Information**
Failed to query the area information.
**Description**
This error code is reported for the failure to query the area information (including the country code).
This error code is reported if the query of the area information (including the country code) has failed.
**Possible Causes**
...
...
@@ -118,15 +118,15 @@ The correct area information is not found.
Stop calling the API for querying the country code.
## 3301600 Geofence Operation Failure
## 3301600 Geofence Operation Failed
**Error Message**
**Error Information**
Failed to operate the geofence.
**Description**
This error code is reported when an operation (like adding, deleting, pausing, and resuming) fails to be performed on the geofence.
This error code is reported if a geofence operation, for example, adding, deleting, pausing, or resuming a geofence, has failed.
**Possible Causes**
...
...
@@ -140,13 +140,13 @@ Stop calling the geofence operation API.
## 3301700 No Response to the Request
**Error Message**
**Error Information**
No response to the request.
**Description**
This error code is reported when no response is received for an asynchronous request that requires a user to click a button for confirmation or requires a response from the GNSS chip or network server.
This error code is reported if no response is received for an asynchronous request that requires a user to click a button for confirmation or requires a response from the GNSS chip or network server.
**Possible Causes**
...
...
@@ -159,3 +159,25 @@ This error code is reported when no response is received for an asynchronous req
**Solution**
Stop calling relevant APIs.
## 3301800 Failed to Start Wi-Fi or Bluetooth Scanning
**Error Information**
Failed to start WiFi or Bluetooth scanning.
**Description**
This error code is reported if Wi-Fi or Bluetooth scanning fails to start.
**Possible Causes**
1. The Wi-Fi or Bluetooth service incurs an internal error.
2. Power consumption control is activated because of low battery level.
3. Wi-Fi or Bluetooth is not enabled.
**Solution**
Turn off Wi-Fi or Bluetooth, and then turn it on again.