From 8efa4b791478a450c5b558a4178b9493c5685074 Mon Sep 17 00:00:00 2001 From: shengyy0704 Date: Mon, 30 Oct 2023 16:21:59 +0800 Subject: [PATCH] add api Signed-off-by: liuxiyao223 --- .../netmanager_base/netconnclient/BUILD.gn | 51 +++++++++++ .../include/native_net_conn_api.h | 86 +++++++++++++++++++ .../netconnclient/libnet_connection.ndk.json | 4 + .../netconnclient/src/native_net_conn_api.cpp | 56 ++++++++++++ 4 files changed, 197 insertions(+) create mode 100644 communication/netmanager_base/netconnclient/BUILD.gn create mode 100644 communication/netmanager_base/netconnclient/include/native_net_conn_api.h create mode 100644 communication/netmanager_base/netconnclient/libnet_connection.ndk.json create mode 100644 communication/netmanager_base/netconnclient/src/native_net_conn_api.cpp diff --git a/communication/netmanager_base/netconnclient/BUILD.gn b/communication/netmanager_base/netconnclient/BUILD.gn new file mode 100644 index 000000000..fadf45877 --- /dev/null +++ b/communication/netmanager_base/netconnclient/BUILD.gn @@ -0,0 +1,51 @@ +# Copyright (c) 2023 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("//build/ohos.gni") +import("//foundation/communication/netmanager_base/netmanager_base_config.gni") + +ohos_ndk_library("libnet_connection_ndk") { + output_name = "net_connection" + output_extension = "so" + ndk_description_file = "./libnet_connection.ndk.json" + min_compact_version = "1" + system_capability = "SystemCapability.Communication.NetManager.Core" + system_capability_headers = [ "./include/native_net_conn_api.h" ] +} + +ohos_ndk_headers("netconn_header") { + dest_dir = "$ndk_headers_out_dir/netconn" + sources = [ "./include/native_net_conn_api.h" ] +} + +ohos_shared_library("net_connection") { + output_extension = "so" + include_dirs = + [ "$NETMANAGER_BASE_ROOT/interfaces/kits/c/netconnclient/include" ] + + sources = [ "$NETMANAGER_BASE_ROOT/interfaces/kits/c/netconnclient/src/native_net_conn_api.cpp" ] + + deps = [ + "$INNERKITS_ROOT/netconnclient:net_conn_manager_if", + "$NETMANAGER_BASE_ROOT/utils:net_manager_common", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + + part_name = "netmanager_base" + subsystem_name = "communication" + relative_install_dir = "ndk" +} diff --git a/communication/netmanager_base/netconnclient/include/native_net_conn_api.h b/communication/netmanager_base/netconnclient/include/native_net_conn_api.h new file mode 100644 index 000000000..3777b6374 --- /dev/null +++ b/communication/netmanager_base/netconnclient/include/native_net_conn_api.h @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef NATIVE_NET_CONN_API_H +#define NATIVE_NET_CONN_API_H + +/** + * @addtogroup NetConn + * @{ + * + * @brief Provide C interface for the data network connection module of network management. + * + * @since 11 + * @version 1.0 + */ + +/** + * @file native_net_conn_api.h + * + * @brief Provide C interface for the data network connection module of network management. + * + * @syscap SystemCapability.Communication.NetManager.Core + * @since 11 + * @version 1.0 + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Get DNS result with netId. + * + * @param host The host name to query. + * @param serv Service name. + * @param hint Pointer to the addrinfo structure. + * @param res Store DNS query results and return them in a linked list format. + * @param netId DNS query netId, 0 is used for default netid query. + * @return 0 - Success. + * @return 201 - Missing permissions. + * @return 401 - Parameter error. + * @return 2100002 - Unable to connect to service. + * @return 2100003 - Internal error. + * @permission ohos.permission.INTERNET + * @syscap SystemCapability.Communication.NetManager.Core + * @since 11 + * @version 1.0 +*/ +int32_t OH_NetConn_GetAddrInfo(char *host, char *serv, struct addrinfo *hint, struct addrinfo **res, int32_t netId); + +/** + * @brief Free DNS result. + * + * @param res DNS query result chain header. + * @return 0 - Success. + * @return 201 - Missing permissions. + * @return 401 - Parameter error. + * @return 2100002 - Unable to connect to service. + * @return 2100003 - Internal error. + * @permission ohos.permission.INTERNET + * @syscap SystemCapability.Communication.NetManager.Core + * @since 11 + * @version 1.0 +*/ +int32_t OH_NetConn_FreeDnsResult(struct addrinfo *res); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* NATIVE_NET_CONN_API_H */ diff --git a/communication/netmanager_base/netconnclient/libnet_connection.ndk.json b/communication/netmanager_base/netconnclient/libnet_connection.ndk.json new file mode 100644 index 000000000..4717b9bd7 --- /dev/null +++ b/communication/netmanager_base/netconnclient/libnet_connection.ndk.json @@ -0,0 +1,4 @@ +[ + {"name": "OH_NetConn_GetAddrInfo"}, + {"name": "OH_NetConn_FreeDnsResult"} +] \ No newline at end of file diff --git a/communication/netmanager_base/netconnclient/src/native_net_conn_api.cpp b/communication/netmanager_base/netconnclient/src/native_net_conn_api.cpp new file mode 100644 index 000000000..e5896d25f --- /dev/null +++ b/communication/netmanager_base/netconnclient/src/native_net_conn_api.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 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. + */ + +#include "native_net_conn_api.h" +#include "net_conn_client.h" +#include "net_manager_constants.h" +#include "net_mgr_log_wrapper.h" + +using namespace OHOS::NetManagerStandard; + +int32_t OH_NetConn_GetAddrInfo(char *host, char *serv, struct addrinfo *hint, struct addrinfo **res, int32_t netId) +{ + int32_t ret = NETMANAGER_SUCCESS; + int status = 0; + struct queryparam qp_param; + if (host == nullptr || res == nullptr) { + NETMGR_LOG_E("OH_NetConn_GetAddrInfo received invalid parameters"); + return NETMANAGER_ERR_PARAMETER_ERROR; + } + + memset(&qp_param, 0, sizeof(struct queryparam)); + qp_param.qp_netid = netId; + qp_param.qp_type = 0; + + status = getaddrinfo_ext(host, serv, hint, res, &qp_param); + if (status < 0) { + ret = NETMANAGER_ERR_PARAMETER_ERROR; + } + + return ret; +} + + +int32_t OH_NetConn_FreeDnsResult(struct addrinfo *res) +{ + if (res == nullptr) { + NETMGR_LOG_E("OH_NetConn_FreeDnsResult received invalid parameters"); + return NETMANAGER_ERR_PARAMETER_ERROR; + } + + freeaddrinfo(res); + + return NETMANAGER_SUCCESS; +} -- Gitee