1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstdint>
17
18 #include "errorcode_convertor.h"
19 #include "module_template.h"
20 #include "napi_utils.h"
21 #include "net_manager_constants.h"
22 #include "netmanager_ext_log.h"
23 #include "networkvpn_client.h"
24 #include "vpn_connection.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 constexpr int32_t ARG_NUM_0 = 0;
29 constexpr int32_t PARAM_ONE = 1;
30
MakeData(napi_env env,size_t argc,napi_value * argv,EventManager * manager)31 static void *MakeData(napi_env env, size_t argc, napi_value *argv, EventManager *manager)
32 {
33 if ((argc != PARAM_ONE) || (NapiUtils::GetValueType(env, argv[ARG_NUM_0]) != napi_object)) {
34 NETMANAGER_EXT_LOGE("funciton prameter error");
35 napi_throw_error(env, std::to_string(NETMANAGER_EXT_ERR_PARAMETER_ERROR).c_str(), "Parameter error");
36 return nullptr;
37 }
38
39 int32_t ret = NetworkVpnClient::GetInstance().CreateVpnConnection();
40 if (ret != NETMANAGER_EXT_SUCCESS) {
41 NETMANAGER_EXT_LOGE("execute CreateVpnConnection failed: %{public}d", ret);
42 std::string errorMsg = NetBaseErrorCodeConvertor().ConvertErrorCode(ret);
43 napi_throw_error(env, std::to_string(ret).c_str(), errorMsg.c_str());
44 return nullptr;
45 }
46 return reinterpret_cast<void *>(&NetworkVpnClient::GetInstance());
47 }
48
CreateVpnConnection(napi_env env,napi_callback_info info)49 static napi_value CreateVpnConnection(napi_env env, napi_callback_info info)
50 {
51 return ModuleTemplate::NewInstance(env, info, VPN_CONNECTION, MakeData, [](napi_env, void *data, void *) {
52 NETMANAGER_EXT_LOGI("finalize VpnConnection");
53 });
54 }
55
RegisterVpnModule(napi_env env,napi_value exports)56 napi_value RegisterVpnModule(napi_env env, napi_value exports)
57 {
58 NapiUtils::DefineProperties(env, exports,
59 {
60 DECLARE_NAPI_FUNCTION(CREATE_VPN_CONNECTION, CreateVpnConnection),
61 });
62 ModuleTemplate::DefineClass(env, exports,
63 {
64 DECLARE_NAPI_FUNCTION(SET_UP, VpnConnection::SetUp),
65 DECLARE_NAPI_FUNCTION(PROTECT, VpnConnection::Protect),
66 DECLARE_NAPI_FUNCTION(DESTROY, VpnConnection::Destroy),
67 },
68 VPN_CONNECTION);
69 return exports;
70 }
71
72 static napi_module g_vpnModule = {
73 .nm_version = 1,
74 .nm_flags = 0,
75 .nm_filename = nullptr,
76 .nm_register_func = RegisterVpnModule,
77 .nm_modname = VPN_MODULE_NAME,
78 .nm_priv = nullptr,
79 .reserved = {nullptr},
80 };
81
VpnNapiRegister()82 extern "C" __attribute__((constructor)) void VpnNapiRegister()
83 {
84 napi_module_register(&g_vpnModule);
85 }
86 } // namespace NetManagerStandard
87 } // namespace OHOS