From 442fa215c58af40e9ef45170dd0ba630405c5bf3 Mon Sep 17 00:00:00 2001 From: fuchao Date: Sun, 9 Oct 2022 09:30:25 +0800 Subject: [PATCH 1/7] Add @ohos.net.ethernet.d.ts file Signed-off-by: fuchao --- api/@ohos.net.ethernet.d.ts | 88 +++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 api/@ohos.net.ethernet.d.ts diff --git a/api/@ohos.net.ethernet.d.ts b/api/@ohos.net.ethernet.d.ts new file mode 100644 index 0000000000..24621e4b1f --- /dev/null +++ b/api/@ohos.net.ethernet.d.ts @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {AsyncCallback, Callback} from "./basic"; + +/** + * Provides interfaces to manage ethernet. + * + * @since 9 + * @syscap SystemCapability.Communication.NetManager.Ethernet + */ +declare namespace ethernet { + /** + * Get the specified network interface information. + * + * @param iface Indicates the network interface name. + * @permission ohos.permission.GET_NETWORK_INFO + * @systemapi Hide this for inner system use. + */ + function getIfaceConfig(iface: string, callback: AsyncCallback): void; + function getIfaceConfig(iface: string): Promise; + + /** + * Set the specified network interface parameters. + * + * @param iface Indicates the network interface name of the network parameter. + * @param ic Indicates the ic. See {@link InterfaceConfiguration}. + * @permission ohos.permission.CONNECTIVITY_INTERNAL + * @systemapi Hide this for inner system use. + */ + function setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback): void; + function setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise; + + /** + * Check whether the specified network is active. + * + * @param iface Indicates the network interface name. + * @permission ohos.permission.GET_NETWORK_INFO + * @systemapi Hide this for inner system use. + */ + function isIfaceActive(iface: string, callback: AsyncCallback): void; + function isIfaceActive(iface: string): Promise; + + /** + * Gets the names of all active network interfaces. + * + * @permission ohos.permission.GET_NETWORK_INFO + * @systemapi Hide this for inner system use. + */ + function getAllActiveIfaces(callback: AsyncCallback>): void; + function getAllActiveIfaces(): Promise>; + + /** + * @systemapi Hide this for inner system use. + */ + export interface InterfaceConfiguration { + /*See {@link IPSetMode}*/ + mode: IPSetMode; + ipAddr: string; + route: string; + gateway: string; + netMask: string; + dnsServers: string; + domain: string; + } + + /** + * @systemapi Hide this for inner system use. + */ + export enum IPSetMode { + STATIC = 0, + DHCP = 1 + } +} + +export default ethernet; \ No newline at end of file -- Gitee From ef64d9b5be265f1e5c822bedde7b01d7986371e5 Mon Sep 17 00:00:00 2001 From: fuchao Date: Mon, 10 Oct 2022 20:28:08 +0800 Subject: [PATCH 2/7] Add netstack dts files Signed-off-by: fuchao --- api/@ohos.net.http.d.ts | 92 ++++++++++++++++++++++++++++ api/@ohos.net.socket.d.ts | 125 +++++++++++++++++++++++++++++++++++++- 2 files changed, 215 insertions(+), 2 deletions(-) diff --git a/api/@ohos.net.http.d.ts b/api/@ohos.net.http.d.ts index b6ecab57c1..c1732e1aff 100644 --- a/api/@ohos.net.http.d.ts +++ b/api/@ohos.net.http.d.ts @@ -14,6 +14,7 @@ */ import {AsyncCallback, Callback} from "./basic"; +import socket, {X509CertRawData} from './@ohos.net.socket' /** * Provides http related APIs. @@ -22,6 +23,9 @@ import {AsyncCallback, Callback} from "./basic"; * @syscap SystemCapability.Communication.NetStack */ declare namespace http { + type TLSSecureOptions = socket.TLSSecureOptions; + type X509CertRawData = socket.X509CertRawData; + /** * Creates an HTTP request task. */ @@ -37,6 +41,20 @@ declare namespace http { * extraData can be a string or an Object (API 6) or an ArrayBuffer(API 8). */ extraData?: string | Object | ArrayBuffer; + /** + * Data type to be returned. If this parameter is set, the system preferentially returns the specified type. + * + * @since 9 + */ + expectDataType?: HttpDataType; + /** + * @since 9 + */ + usingCache?: boolean; // default is true + /** + * @since 9 + */ + priority?: number; // [1, 1000], default is 1. /** * HTTP request header. */ @@ -49,6 +67,16 @@ declare namespace http { * Connection timeout interval. The default value is 60,000, in ms. */ connectTimeout?: number; // default is 60s. + /** + * @since 9 + */ + usingProtocol?: HttpProtocol; // default is automatically specified by the system. + /** + * For https. + * + * @since 9 + */ + secureOptions?: TLSSecureOptions; } export interface HttpRequest { @@ -154,11 +182,50 @@ declare namespace http { VERSION } + /** + * Supported protocols. + * + * @since 9 + */ + export enum HttpProtocol { + HTTP1_1, + HTTP2, + } + + /** + * Indicates the type of the returned data. + * + * @since 9 + */ + export enum HttpDataType { + /** + * The returned type is string. + */ + STRING, + /** + * The returned type is Object. + */ + OBJECT = 1, + /** + * The returned type is ArrayBuffer. + */ + ARRAY_BUFFER = 2, + } + export interface HttpResponse { /** * result can be a string (API 6) or an ArrayBuffer(API 8). Object is deprecated from API 8. + * If {@link HttpRequestOptions#expectDataType} is set, the system preferentially returns this parameter. */ result: string | Object | ArrayBuffer; + /** + * If the resultType is string, you can get result directly. + * If the resultType is Object, you can get result such as this: result['key']. + * If the resultType is ArrayBuffer, you can use ArrayBuffer to create the binary objects. + * + * @since 9 + */ + resultType: HttpDataType; /** * Server status code. */ @@ -172,6 +239,31 @@ declare namespace http { */ cookies: string; } + + /** + * Creates a default {@code HttpResponseCache} object to store the responses of HTTP access requests. + * + * @param cacheSize the size of cache, default is 10*1024*1024(10MB). + * @since 9 + */ + function createHttpResponseCache(cacheSize?: number): HttpResponseCache; + + /** + * @since 9 + */ + export interface HttpResponseCache { + /** + * Writes data in the cache to the file system so that all the cached data can be accessed in the next HTTP request. + */ + flush(callback: AsyncCallback): void; + flush(): Promise; + + /** + * Disables a cache and deletes the data in it. + */ + delete(callback: AsyncCallback): void; + delete(): Promise; + } } export default http; \ No newline at end of file diff --git a/api/@ohos.net.socket.d.ts b/api/@ohos.net.socket.d.ts index fb88669e14..d9a3f2332b 100644 --- a/api/@ohos.net.socket.d.ts +++ b/api/@ohos.net.socket.d.ts @@ -15,6 +15,7 @@ import {AsyncCallback, Callback, ErrorCallback} from "./basic"; import connection from "./@ohos.net.connection"; +import cryptoFramework from "./@ohos.security.cryptoFramework"; /** * Provides TCP and UDP Socket APIs. @@ -24,6 +25,7 @@ import connection from "./@ohos.net.connection"; */ declare namespace socket { export import NetAddress = connection.NetAddress; + export type X509CertRawData = cryptoFramework.EncodingBlob; /** * Creates a UDPSocket object. @@ -35,6 +37,20 @@ declare namespace socket { */ function constructTCPSocketInstance(): TCPSocket; + /** + * Creates a TLSSocket object. + * + * @since 9 + */ + function constructTLSSocketInstance(): TLSSocket; + + /** + * Creates a Local Socket object. + * + * @since 10 + */ + function constructLocalSocketInstance(name: string, isServer?: boolean): LocalSocket; + export interface UDPSendOptions { /** * Data to send. @@ -219,7 +235,7 @@ declare namespace socket { /** * Socket linger. */ - socketLinger: {on: boolean, linger: number}; + socketLinger?: {on: boolean, linger: number}; } export interface TCPSocket { @@ -314,6 +330,111 @@ declare namespace socket { */ off(type: 'error', callback?: ErrorCallback): void; } + + /** + * @since 9 + */ + export interface TLSSocket extends TCPSocket { + + /** + * Returns an object representing a local certificate. + */ + getCertificate(callback: AsyncCallback): void; + getCertificate(): Promise; + + /** + * Returns an object representing the peer certificate. If the peer does not provide a certificate, + * an empty object will be returned. If the socket is destroyed, null is returned. + * If needChain is true, it contains the complete certificate chain. Otherwise, + * it only contains the peer's certificate. + */ + getRemoteCertificate(callback: AsyncCallback): void; + getRemoteCertificate(): Promise; + + /** + * Returns a string containing the negotiated SSL/TLS protocol version of the current connection. + * For connected sockets that have not completed the handshake process, the value 'unknown' will be returned. + * Server sockets or disconnected client sockets will return a value of null. + */ + getProtocol(callback: AsyncCallback): void; + getProtocol(): Promise; + + /** + * Returns an object containing the negotiated cipher suite information. + * For example:{"name": "AES128-SHA256", "standardName": "TLS_RSA_WITH_AES_128_CBC_SHA256", "version": "TLSv1.2"} + */ + getCipherSuite(callback: AsyncCallback>): void; + getCipherSuite(): Promise>; + + /** + * The list of signature algorithms shared between the server and the client, in descending order of priority. + * @see https://www.openssl.org/docs/man1.1.1/man3/SSL_get_shared_sigalgs.html + */ + getSignatureAlgorithms(callback: AsyncCallback>): void; + getSignatureAlgorithms(): Promise>; + + /** + * @permission ohos.permission.INTERNET + */ + connect(options: TLSConnectOptions, callback: AsyncCallback): void; + connect(options: TLSConnectOptions): Promise; + + /** + * Sends data over a TLSSocket connection. + * + * @param data Optional parameters {@link string}. + * @permission ohos.permission.INTERNET + */ + send(data: string, callback: AsyncCallback): void; + send(data: string): Promise; + } + + /** + * @since 9 + */ + export interface TLSSecureOptions { + ca: string | Array; + cert: string; + key: string; + passwd?: string; + protocols?: Protocol | Array; + /** + * default is false, use local cipher. + */ + useRemoteCipherPrefer?: boolean; + /** + * Supported signature algorithms. This list can contain summary algorithms(SHA256、MD5、etc)、 + * Public key algorithm(RSA-PSS、ECDSA、etc)、Combination of the two(For example 'RSA+SHA384') + * or TLS v1.3 Scheme name(For example rsa_pss_pss_sha512) + */ + signatureAlgorithms?: string; + + /** + * Crypto suite specification + */ + cipherSuite?: string; + } + + /** + * @since 9 + */ + export interface TLSConnectOptions { + address: NetAddress; + secureOptions: TLSSecureOptions; + checkServerIdentity?: (hostname: string, cert: X509CertRawData) => Error | undefined; + /** + * Application layer protocol negotiation extension, supporting HTTP, HTTP/2 + */ + ALPNProtocols?: Array; + } + + /** + * @since 9 + */ + export enum Protocol { + TLSv12 = "TLSv1.2", + TLSv13 = "TLSv1.3", + } } -export default socket; \ No newline at end of file +export default socket; -- Gitee From 1c15e4bae6cf94fa06e4a57fe09e77ec0ce44370 Mon Sep 17 00:00:00 2001 From: fuchao Date: Mon, 10 Oct 2022 21:06:28 +0800 Subject: [PATCH 3/7] Update dts files Signed-off-by: fuchao --- api/@ohos.net.ethernet.d.ts | 6 ++++-- api/@ohos.net.http.d.ts | 17 ++++++++++++++++- api/@ohos.net.socket.d.ts | 24 +++++++++++++++++------- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/api/@ohos.net.ethernet.d.ts b/api/@ohos.net.ethernet.d.ts index 24621e4b1f..3835c1ffc7 100644 --- a/api/@ohos.net.ethernet.d.ts +++ b/api/@ohos.net.ethernet.d.ts @@ -66,7 +66,9 @@ declare namespace ethernet { * @systemapi Hide this for inner system use. */ export interface InterfaceConfiguration { - /*See {@link IPSetMode}*/ + /** + *See {@link IPSetMode} + */ mode: IPSetMode; ipAddr: string; route: string; @@ -85,4 +87,4 @@ declare namespace ethernet { } } -export default ethernet; \ No newline at end of file +export default ethernet; diff --git a/api/@ohos.net.http.d.ts b/api/@ohos.net.http.d.ts index c1732e1aff..91732b3c02 100644 --- a/api/@ohos.net.http.d.ts +++ b/api/@ohos.net.http.d.ts @@ -36,41 +36,50 @@ declare namespace http { * Request method. */ method?: RequestMethod; // default is GET + /** * Additional data of the request. * extraData can be a string or an Object (API 6) or an ArrayBuffer(API 8). */ extraData?: string | Object | ArrayBuffer; + /** * Data type to be returned. If this parameter is set, the system preferentially returns the specified type. * * @since 9 */ expectDataType?: HttpDataType; + /** * @since 9 */ usingCache?: boolean; // default is true + /** * @since 9 */ priority?: number; // [1, 1000], default is 1. + /** * HTTP request header. */ header?: Object; // default is 'content-type': 'application/json' + /** * Read timeout period. The default value is 60,000, in ms. */ readTimeout?: number; // default is 60s + /** * Connection timeout interval. The default value is 60,000, in ms. */ connectTimeout?: number; // default is 60s. + /** * @since 9 */ usingProtocol?: HttpProtocol; // default is automatically specified by the system. + /** * For https. * @@ -202,10 +211,12 @@ declare namespace http { * The returned type is string. */ STRING, + /** * The returned type is Object. */ OBJECT = 1, + /** * The returned type is ArrayBuffer. */ @@ -218,6 +229,7 @@ declare namespace http { * If {@link HttpRequestOptions#expectDataType} is set, the system preferentially returns this parameter. */ result: string | Object | ArrayBuffer; + /** * If the resultType is string, you can get result directly. * If the resultType is Object, you can get result such as this: result['key']. @@ -226,14 +238,17 @@ declare namespace http { * @since 9 */ resultType: HttpDataType; + /** * Server status code. */ responseCode: ResponseCode | number; + /** * All headers in the response from the server. */ header: Object; + /** * @since 8 */ @@ -266,4 +281,4 @@ declare namespace http { } } -export default http; \ No newline at end of file +export default http; diff --git a/api/@ohos.net.socket.d.ts b/api/@ohos.net.socket.d.ts index d9a3f2332b..6eda33e83c 100644 --- a/api/@ohos.net.socket.d.ts +++ b/api/@ohos.net.socket.d.ts @@ -44,18 +44,12 @@ declare namespace socket { */ function constructTLSSocketInstance(): TLSSocket; - /** - * Creates a Local Socket object. - * - * @since 10 - */ - function constructLocalSocketInstance(name: string, isServer?: boolean): LocalSocket; - export interface UDPSendOptions { /** * Data to send. */ data: string | ArrayBuffer; + /** * Destination address. */ @@ -67,14 +61,17 @@ declare namespace socket { * Size of the receive buffer, in MBS. */ receiveBufferSize?: number; + /** * Size of the send buffer, in MBS. */ sendBufferSize?: number; + /** * Whether to reuse addresses. The default value is false. */ reuseAddress?: boolean; + /** * Timeout duration of the UDPSocket connection, in milliseconds. */ @@ -93,10 +90,12 @@ declare namespace socket { * Whether the connection is in the bound state. */ isBound: boolean; + /** * Whether the connection is in the closed state. */ isClose: boolean; + /** * Whether the connection is in the connected state. */ @@ -108,14 +107,17 @@ declare namespace socket { * Bound IP address. */ address: string; + /** * Network protocol type. The options are as follows: IPv4, IPv6. */ family: 'IPv4' | 'IPv6'; + /** * Port number. The value ranges from 0 to 65535. */ port: number; + /** * Length of the server response message, in bytes. */ @@ -202,6 +204,7 @@ declare namespace socket { * Bound IP address and port number. */ address: NetAddress; + /** * Timeout duration of the TCPSocket connection, in milliseconds. */ @@ -213,6 +216,7 @@ declare namespace socket { * Data to send. */ data: string | ArrayBuffer; + /** * Character encoding format. */ @@ -224,14 +228,17 @@ declare namespace socket { * Whether to keep the connection alive. The default value is false. */ keepAlive?: boolean; + /** * Whether to enable OOBInline. The default value is false. */ OOBInline?: boolean; + /** * Whether to enable no-delay on the TCPSocket connection. The default value is false. */ TCPNoDelay?: boolean; + /** * Socket linger. */ @@ -398,10 +405,12 @@ declare namespace socket { key: string; passwd?: string; protocols?: Protocol | Array; + /** * default is false, use local cipher. */ useRemoteCipherPrefer?: boolean; + /** * Supported signature algorithms. This list can contain summary algorithms(SHA256、MD5、etc)、 * Public key algorithm(RSA-PSS、ECDSA、etc)、Combination of the two(For example 'RSA+SHA384') @@ -422,6 +431,7 @@ declare namespace socket { address: NetAddress; secureOptions: TLSSecureOptions; checkServerIdentity?: (hostname: string, cert: X509CertRawData) => Error | undefined; + /** * Application layer protocol negotiation extension, supporting HTTP, HTTP/2 */ -- Gitee From fbf2edfed27226608b142b2e0f8363e5ee3249e2 Mon Sep 17 00:00:00 2001 From: fuchao Date: Mon, 10 Oct 2022 21:07:53 +0800 Subject: [PATCH 4/7] fix comment Signed-off-by: fuchao --- api/@ohos.net.ethernet.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/@ohos.net.ethernet.d.ts b/api/@ohos.net.ethernet.d.ts index 3835c1ffc7..a26a3848ee 100644 --- a/api/@ohos.net.ethernet.d.ts +++ b/api/@ohos.net.ethernet.d.ts @@ -67,7 +67,7 @@ declare namespace ethernet { */ export interface InterfaceConfiguration { /** - *See {@link IPSetMode} + * See {@link IPSetMode} */ mode: IPSetMode; ipAddr: string; -- Gitee From eb9957db5106b1c4b7cbadd13f9693c972746ced Mon Sep 17 00:00:00 2001 From: fuchao Date: Mon, 10 Oct 2022 22:47:59 +0800 Subject: [PATCH 5/7] fix review issue Signed-off-by: fuchao --- api/@ohos.net.ethernet.d.ts | 30 +++++++++++++++++++++++++++++- api/@ohos.net.http.d.ts | 21 --------------------- api/@ohos.net.socket.d.ts | 1 - 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/api/@ohos.net.ethernet.d.ts b/api/@ohos.net.ethernet.d.ts index a26a3848ee..b1a3e6a33f 100644 --- a/api/@ohos.net.ethernet.d.ts +++ b/api/@ohos.net.ethernet.d.ts @@ -70,12 +70,40 @@ declare namespace ethernet { * See {@link IPSetMode} */ mode: IPSetMode; + /** + * Ethernet connection static configuration IP information. + * The address value range is 0-255.0-255.0-255.0-255.0-255 + * (DHCP mode does not need to be configured) + */ ipAddr: string; + + /** + * Ethernet connection static configuration route information. + * The address value range is 0-255.0-255.0-255.0-255.0-255 + * (DHCP mode does not need to be configured) + */ route: string; + + /** + * Ethernet connection static configuration gateway information. + * The address value range is 0-255.0-255.0-255.0-255.0-255 + * (DHCP mode does not need to be configured) + */ gateway: string; + + /** + * Ethernet connection static configuration netMask information. + * The address value range is 0-255.0-255.0-255.0-255.0-255 + * (DHCP mode does not need to be configured) + */ netMask: string; + + /** + * The Ethernet connection is configured with the dns service address. + * The address value range is 0-255.0-255.0-255.0-255.0-255 + * (DHCP mode does not need to be configured, Multiple addresses are separated by ",") + */ dnsServers: string; - domain: string; } /** diff --git a/api/@ohos.net.http.d.ts b/api/@ohos.net.http.d.ts index 91732b3c02..3c48f0f106 100644 --- a/api/@ohos.net.http.d.ts +++ b/api/@ohos.net.http.d.ts @@ -23,9 +23,6 @@ import socket, {X509CertRawData} from './@ohos.net.socket' * @syscap SystemCapability.Communication.NetStack */ declare namespace http { - type TLSSecureOptions = socket.TLSSecureOptions; - type X509CertRawData = socket.X509CertRawData; - /** * Creates an HTTP request task. */ @@ -79,13 +76,6 @@ declare namespace http { * @since 9 */ usingProtocol?: HttpProtocol; // default is automatically specified by the system. - - /** - * For https. - * - * @since 9 - */ - secureOptions?: TLSSecureOptions; } export interface HttpRequest { @@ -207,19 +197,8 @@ declare namespace http { * @since 9 */ export enum HttpDataType { - /** - * The returned type is string. - */ STRING, - - /** - * The returned type is Object. - */ OBJECT = 1, - - /** - * The returned type is ArrayBuffer. - */ ARRAY_BUFFER = 2, } diff --git a/api/@ohos.net.socket.d.ts b/api/@ohos.net.socket.d.ts index 6eda33e83c..5316565b99 100644 --- a/api/@ohos.net.socket.d.ts +++ b/api/@ohos.net.socket.d.ts @@ -430,7 +430,6 @@ declare namespace socket { export interface TLSConnectOptions { address: NetAddress; secureOptions: TLSSecureOptions; - checkServerIdentity?: (hostname: string, cert: X509CertRawData) => Error | undefined; /** * Application layer protocol negotiation extension, supporting HTTP, HTTP/2 -- Gitee From c3570e2b5287540ff043776e44c467704ffd70c1 Mon Sep 17 00:00:00 2001 From: fuchao Date: Tue, 11 Oct 2022 11:28:00 +0800 Subject: [PATCH 6/7] delete import Signed-off-by: fuchao --- api/@ohos.net.http.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/api/@ohos.net.http.d.ts b/api/@ohos.net.http.d.ts index 3c48f0f106..ce56e55549 100644 --- a/api/@ohos.net.http.d.ts +++ b/api/@ohos.net.http.d.ts @@ -14,7 +14,6 @@ */ import {AsyncCallback, Callback} from "./basic"; -import socket, {X509CertRawData} from './@ohos.net.socket' /** * Provides http related APIs. -- Gitee From 9472e5a861be607db394b4995d1bdf64749ff89f Mon Sep 17 00:00:00 2001 From: fuchao Date: Tue, 11 Oct 2022 11:46:57 +0800 Subject: [PATCH 7/7] Add comments Signed-off-by: fuchao --- api/@ohos.net.socket.d.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/api/@ohos.net.socket.d.ts b/api/@ohos.net.socket.d.ts index 5316565b99..3fcdd5c8f9 100644 --- a/api/@ohos.net.socket.d.ts +++ b/api/@ohos.net.socket.d.ts @@ -400,10 +400,29 @@ declare namespace socket { * @since 9 */ export interface TLSSecureOptions { + /** + * Certificate used to verify the identity of the server + */ ca: string | Array; + + /** + * Certificate proving the identity of the client + */ cert: string; + + /** + * Private key of client certificate + */ key: string; + + /** + * Password of the private key + */ passwd?: string; + + /** + * TLS protocol version + */ protocols?: Protocol | Array; /** @@ -432,7 +451,7 @@ declare namespace socket { secureOptions: TLSSecureOptions; /** - * Application layer protocol negotiation extension, supporting HTTP, HTTP/2 + * Application layer protocol negotiation extension, such as "spdy/1", "http/1.1", "h2" */ ALPNProtocols?: Array; } -- Gitee