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 #ifndef IPSEC_VPN_CTL_H
17 #define IPSEC_VPN_CTL_H
18 
19 #include <cstdint>
20 
21 #include "ipsecvpn_config.h"
22 #include "l2tpvpn_config.h"
23 #include "net_vpn_impl.h"
24 #include "netsys_controller.h"
25 
26 #define IPSEC_PIDDIR "/data/service/el1/public/vpn"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 namespace {
31 const std::string SWAN_CTL_FILE = IPSEC_PIDDIR "/swanctl.conf";
32 const std::string SWAN_CONFIG_FILE = IPSEC_PIDDIR "/strongswan.conf";
33 const std::string L2TP_IPSEC_CFG = IPSEC_PIDDIR "/ipsec.conf";
34 const std::string L2TP_CFG = IPSEC_PIDDIR "/xl2tpd.conf";
35 const std::string L2TP_IPSEC_SECRETS_CFG = IPSEC_PIDDIR "/ipsec.secrets.conf";
36 const std::string OPTIONS_L2TP_CLIENT = IPSEC_PIDDIR "/options.l2tpd.client.conf";
37 const std::string IPSEC_START_TAG = "start";
38 const std::string SWANCTL_START_TAG = "config";
39 const std::string IPSEC_CONNECT_TAG = "connect";
40 } // namespace
41 using namespace NetsysNative;
42 enum IpsecVpnStateCode {
43     STATE_INIT = 0,
44     STATE_STARTED,      // ipsec restart compelete
45     STATE_CONFIGED,     // swanctl load files compelete or xl2tpd start
46     STATE_CONTROLLED,   // control pppd startup
47     STATE_CONNECTED,    // ipsec up home or pppd started
48     STATE_DISCONNECTED, // stop
49 };
50 
51 enum IpsecVpnCertType : int32_t {
52     CA_CERT = 0,
53     USER_CERT,
54     SERVER_CERT,
55 };
56 
57 class IpsecVpnCtl : public NetVpnImpl {
58 public:
59     IpsecVpnCtl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId, std::vector<int32_t> &activeUserIds);
60     virtual ~IpsecVpnCtl();
61 
62     sptr<IpsecVpnConfig> ipsecVpnConfig_ = nullptr;
63     sptr<L2tpVpnConfig> l2tpVpnConfig_ = nullptr;
64 
65     bool IsInternalVpn() override;
66     int32_t SetUp() override;
67     int32_t Destroy() override;
68     int32_t GetConnectedSysVpnConfig(sptr<SysVpnConfig> &sysVpnConfig) override;
69     int32_t NotifyConnectStage(const std::string &stage, const int32_t &result) override;
70     int32_t GetSysVpnCertUri(const int32_t certType, std::string &certUri) override;
71 
72 protected:
73     int32_t state_ = STATE_INIT;
74     virtual int32_t StartSysVpn();
75     virtual int32_t StopSysVpn();
76     virtual int32_t InitConfigFile();
77     void CleanTempFiles();
78     void DeleteTempFile(const std::string &fileName);
79 };
80 } // namespace NetManagerStandard
81 } // namespace OHOS
82 #endif // IPSEC_VPN_CTL_H
83