• 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 #ifndef NETWORKVPN_CLIENT_H
17 #define NETWORKVPN_CLIENT_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include <parcel.h>
25 #include <refbase.h>
26 #include <unistd.h>
27 
28 #include "i_networkvpn_service.h"
29 #include "i_vpn_event_callback.h"
30 #include "vpn_event_callback_stub.h"
31 #include "vpn_interface.h"
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 
36 class VpnSetUpEventCallback : public VpnEventCallbackStub {
37 public:
OnVpnStateChanged(const bool & isConnected)38     void OnVpnStateChanged(const bool &isConnected) override{};
39     void OnVpnMultiUserSetUp() override;
40 };
41 
42 class NetworkVpnClient {
43 private:
44     NetworkVpnClient() = default;
45     ~NetworkVpnClient() = default;
46     NetworkVpnClient(const NetworkVpnClient &) = delete;
47     NetworkVpnClient &operator=(const NetworkVpnClient &) = delete;
48 
49 public:
50     static NetworkVpnClient &GetInstance();
51 
52 public:
53     /**
54      * start internal vpn
55      *
56      * @param isExistVpn check whether exist vpn connection
57      * @param isRun if isExistVpn=true, check the vpn is running or not
58      * @param pkg Indicates which application the current vpn belongs to
59      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
60      * @permission ohos.permission.MANAGE_VPN
61      * @systemapi Hide this for inner system use.
62      */
63     int32_t Prepare(bool &isExistVpn, bool &isRun, std::string &pkg);
64 
65     /**
66      * extended vpn need always communication with remote vpn server, the data is send/receive by default network but
67      * not vpn network.
68      *
69      * @param socketFd extended vpn opened soecket fd
70      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
71      * @permission ohos.permission.MANAGE_VPN
72      * @systemapi Hide this for inner system use.
73      */
74     int32_t Protect(int32_t socketFd);
75 
76     /**
77      * after extended vpn's negotiation over, need system create a VPN interface using the config parameters.
78      *
79      * @param config VPN interface parameters
80      * @param tunFd the virtual interface fd(out param)
81      * @return the interface node's file descriptor(>0) if process normal, others is error
82      * @permission ohos.permission.MANAGE_VPN
83      * @systemapi Hide this for inner system use.
84      */
85     int32_t SetUpVpn(sptr<VpnConfig> config, int32_t &tunFd);
86 
87     /**
88      * stop the vpn connection, system will destroy the vpn network.
89      *
90      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
91      * @permission ohos.permission.MANAGE_VPN
92      * @systemapi Hide this for inner system use.
93      */
94     int32_t DestroyVpn();
95 
96     /**
97      * register the vpn state callback
98      *
99      * @param callback if this fuction return NETMANAGER_EXT_SUCCESS(0), this callback will be called by service
100      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
101      * @permission ohos.permission.MANAGE_VPN
102      * @systemapi Hide this for inner system use.
103      */
104     int32_t RegisterVpnEvent(sptr<IVpnEventCallback> callback);
105 
106     /**
107      * unregister the vpn state callback
108      *
109      * @param callback if this fuction return NETMANAGER_EXT_SUCCESS(0), this callback will not be called by service
110      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
111      * @permission ohos.permission.MANAGE_VPN
112      * @systemapi Hide this for inner system use.
113      */
114     int32_t UnregisterVpnEvent(sptr<IVpnEventCallback> callback);
115 
116     /**
117      * create vpn connection.
118      *
119      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
120      * @permission ohos.permission.MANAGE_VPN
121      * @systemapi Hide this for inner system use.
122      */
123     int32_t CreateVpnConnection();
124 
125     /**
126      * close the tunfd of vpn interface and unregister VpnEvent.
127      */
128     void multiUserSetUpEvent();
129 
130 private:
131     class MonitorVpnServiceDead : public IRemoteObject::DeathRecipient {
132     public:
MonitorVpnServiceDead(NetworkVpnClient & client)133         explicit MonitorVpnServiceDead(NetworkVpnClient &client) : client_(client) {}
134         ~MonitorVpnServiceDead() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)135         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
136         {
137             client_.OnRemoteDied(remote);
138         }
139 
140     private:
141         NetworkVpnClient &client_;
142     };
143 
144     sptr<INetworkVpnService> GetProxy();
145     void OnRemoteDied(const wptr<IRemoteObject> &remote);
146 
147 private:
148     std::mutex mutex_;
149     VpnInterface vpnInterface_;
150     sptr<IVpnEventCallback> vpnEventCallback_ = nullptr;
151     sptr<INetworkVpnService> networkVpnService_ = nullptr;
152     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
153 };
154 } // namespace NetManagerStandard
155 } // namespace OHOS
156 #endif // NETWORKVPN_CLIENT_H
157