1 /*
2  * Copyright (c) 2024 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 "system_vpn_wrapper.h"
17 
18 #include <unistd.h>
19 #include "netnative_log_wrapper.h"
20 #include "netmanager_base_common_utils.h"
21 #include "net_manager_constants.h"
22 
23 namespace OHOS {
24 namespace nmd {
25 using namespace NetManagerStandard;
26 
SystemVpnWrapper()27 SystemVpnWrapper::SystemVpnWrapper()
28 {
29     isIpSecAccess_ = access(IPSEC_CMD_PATH, F_OK) == 0;
30     vpnFfrtQueue_ = std::make_shared<ffrt::queue>("SystemVpnWrapper");
31 }
32 
~SystemVpnWrapper()33 SystemVpnWrapper::~SystemVpnWrapper()
34 {
35     vpnFfrtQueue_.reset();
36 }
37 
ExecuteUpdate(SysVpnStageCode stage)38 void SystemVpnWrapper::ExecuteUpdate(SysVpnStageCode stage)
39 {
40     NETNATIVE_LOGI("run ExecuteUpdate stage %{public}d", stage);
41     std::string param = std::string(IPSEC_CMD_PATH) + " ";
42     switch (stage) {
43         case SysVpnStageCode::VPN_STAGE_RESTART:
44             param.append(VPN_STAGE_RESTART);
45             break;
46         case SysVpnStageCode::VPN_STAGE_UP_HOME:
47             param.append(VPN_STAGE_UP_HOME);
48             break;
49         case SysVpnStageCode::VPN_STAGE_SWANCTL_LOAD:
50             param.append(VPN_STAGE_SWANCTL_LOAD).append(SWAN_CTL_FILE);
51             break;
52         case SysVpnStageCode::VPN_STAGE_L2TP_LOAD:
53             param.append(VPN_STAGE_L2TP_LOAD).append(L2TP_CFG).append(IPSEC_L2TP_CTL);
54             break;
55         case SysVpnStageCode::VPN_STAGE_L2TP_CTL:
56             param.append(VPN_STAGE_L2TP_CTL);
57             break;
58         case SysVpnStageCode::VPN_STAGE_DOWN_HOME:
59             param.append(VPN_STAGE_DOWN_HOME);
60             break;
61         case SysVpnStageCode::VPN_STAGE_STOP:
62             param.append(VPN_STAGE_STOP);
63             break;
64         case SysVpnStageCode::VPN_STAGE_OPENVPN_RESTART:
65             param.append(VPN_STAGE_OPENVPN_RESTART).append(OPENVPN_CONFIG_FILE);
66             break;
67         case SysVpnStageCode::VPN_STAGE_OPENVPN_STOP:
68             param.append(VPN_STAGE_OPENVPN_STOP);
69             break;
70         default:
71             NETNATIVE_LOGE("run ExecuteUpdate failed, unknown stage %{public}d", stage);
72             return;
73     }
74     if (CommonUtils::ForkExec(param) == NETMANAGER_ERROR) {
75         NETNATIVE_LOGE("run ExecuteUpdate failed");
76     }
77 }
78 
Update(NetsysNative::SysVpnStageCode stage)79 int32_t SystemVpnWrapper::Update(NetsysNative::SysVpnStageCode stage)
80 {
81     if (!vpnFfrtQueue_) {
82         NETNATIVE_LOGE("FFRT Init Fail");
83         return NETMANAGER_ERROR;
84     }
85 
86     if (!isIpSecAccess_) {
87         NETNATIVE_LOGE("Update failed! exec program is not exist");
88         return NETMANAGER_ERROR;
89     }
90 #if UNITTEST_FORBID_FFRT // Forbid FFRT for unittest, which will cause crash in destructor process
91     ExecuteUpdate(stage);
92 #else
93     std::function<void()> update = std::bind(&SystemVpnWrapper::ExecuteUpdate, shared_from_this(), stage);
94     vpnFfrtQueue_->submit(update);
95 #endif // UNITTEST_FORBID_FFRT
96     return NetManagerStandard::NETMANAGER_SUCCESS;
97 }
98 } // namespace nmd
99 } // namespace OHOS
100