• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "vpn_exec.h"
17 
18 #include <cstdint>
19 #include <securec.h>
20 
21 #include "napi_utils.h"
22 #include "net_manager_constants.h"
23 #include "netmanager_ext_log.h"
24 #include "networkvpn_client.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 namespace VpnExec {
GetVpnConnectionInstance(ContextT * context)29 template <typename ContextT> static inline NetworkVpnClient *GetVpnConnectionInstance(ContextT *context)
30 {
31     auto manager = context->GetManager();
32     return (manager == nullptr) ? nullptr : reinterpret_cast<NetworkVpnClient *>(manager->GetData());
33 }
34 
ExecPrepare(PrepareContext * context)35 bool ExecPrepare(PrepareContext *context)
36 {
37     auto vpnClient = GetVpnConnectionInstance(context);
38     if (vpnClient == nullptr) {
39         NETMANAGER_EXT_LOGE("vpnClient is nullptr");
40         return false;
41     }
42     int32_t result = vpnClient->Prepare(context->isExistVpn_, context->isRun_, context->package_);
43     if (result != NETMANAGER_EXT_SUCCESS) {
44         context->SetErrorCode(result);
45         return false;
46     }
47     return true;
48 }
49 
ExecSetUp(SetUpContext * context)50 bool ExecSetUp(SetUpContext *context)
51 {
52     auto vpnClient = GetVpnConnectionInstance(context);
53     if (vpnClient == nullptr) {
54         NETMANAGER_EXT_LOGE("vpnClient is nullptr");
55         return false;
56     }
57     int32_t result = vpnClient->SetUpVpn(context->vpnConfig_, context->fd_);
58     if (result != NETMANAGER_EXT_SUCCESS) {
59         context->SetErrorCode(result);
60         return false;
61     }
62     return true;
63 }
64 
ExecProtect(ProtectContext * context)65 bool ExecProtect(ProtectContext *context)
66 {
67     auto vpnClient = GetVpnConnectionInstance(context);
68     if (vpnClient == nullptr) {
69         NETMANAGER_EXT_LOGE("vpnClient is nullptr");
70         return false;
71     }
72     int32_t result = vpnClient->Protect(context->socketFd_);
73     if (result != NETMANAGER_EXT_SUCCESS) {
74         context->SetErrorCode(result);
75         return false;
76     }
77     return true;
78 }
79 
ExecDestroy(DestroyContext * context)80 bool ExecDestroy(DestroyContext *context)
81 {
82     auto vpnClient = GetVpnConnectionInstance(context);
83     if (vpnClient == nullptr) {
84         NETMANAGER_EXT_LOGE("vpnClient is nullptr");
85         return false;
86     }
87     int32_t result = vpnClient->DestroyVpn();
88     if (result != NETMANAGER_EXT_SUCCESS) {
89         context->SetErrorCode(result);
90         return false;
91     }
92     return true;
93 }
94 
PrepareCallback(PrepareContext * context)95 napi_value PrepareCallback(PrepareContext *context)
96 {
97     napi_value obj = NapiUtils::CreateObject(context->GetEnv());
98     NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isExistVpn", context->isExistVpn_);
99     NapiUtils::SetBooleanProperty(context->GetEnv(), obj, "isRun", context->isRun_);
100     NapiUtils::SetStringPropertyUtf8(context->GetEnv(), obj, "package", context->package_);
101     return obj;
102 }
103 
SetUpCallback(SetUpContext * context)104 napi_value SetUpCallback(SetUpContext *context)
105 {
106     return NapiUtils::CreateInt32(context->GetEnv(), context->fd_);
107 }
108 
ProtectCallback(ProtectContext * context)109 napi_value ProtectCallback(ProtectContext *context)
110 {
111     return NapiUtils::GetUndefined(context->GetEnv());
112 }
113 
DestroyCallback(DestroyContext * context)114 napi_value DestroyCallback(DestroyContext *context)
115 {
116     return NapiUtils::GetUndefined(context->GetEnv());
117 }
118 } // namespace VpnExec
119 } // namespace NetManagerStandard
120 } // namespace OHOS