• 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 #include <shared_mutex>
24 
25 #include <parcel.h>
26 #include <refbase.h>
27 #include <unistd.h>
28 
29 #include "inetwork_vpn_service.h"
30 #include "ivpn_event_callback.h"
31 #include "vpn_event_callback_stub.h"
32 #include "vpn_interface.h"
33 
34 namespace OHOS {
35 namespace NetManagerStandard {
36 
37 class VpnSetUpEventCallback : public VpnEventCallbackStub {
38 public:
OnVpnStateChanged(bool isConnected)39     int32_t OnVpnStateChanged(bool isConnected) override{ return ERR_OK; };
OnMultiVpnStateChanged(bool isConnected,const std::string & bundleName,const std::string & vpnId)40     int32_t OnMultiVpnStateChanged(bool isConnected, const std::string &bundleName,
41         const std::string &vpnId) override{ return ERR_OK; };
42     int32_t OnVpnMultiUserSetUp() override;
43 };
44 
45 class VpnEventCallbackCollection final : public VpnEventCallbackStub {
46 public:
47     int32_t OnVpnStateChanged(bool isConnected) override;
48     int32_t OnMultiVpnStateChanged(bool isConnected, const std::string &bundleName,
49         const std::string &vpnId) override;
50     int32_t OnVpnMultiUserSetUp() override;
51 
52     int32_t RegisterCallback(sptr<IVpnEventCallback> callback);
53     int32_t UnregisterCallback(sptr<IVpnEventCallback> callback);
54     int32_t GetCallbackNum();
55 
56 private:
57     std::shared_mutex vpnEventCbMutex_;
58     std::list<sptr<IVpnEventCallback>> vpnEventCbList_;
59 };
60 
61 class NetworkVpnClient {
62 private:
63     NetworkVpnClient();
64     ~NetworkVpnClient();
65     NetworkVpnClient(const NetworkVpnClient &) = delete;
66     NetworkVpnClient &operator=(const NetworkVpnClient &) = delete;
67     void Subscribe();
68     void Unsubscribe();
69 
70 public:
71     static NetworkVpnClient &GetInstance();
72 
73 public:
74     /**
75      * start internal vpn
76      *
77      * @param isExistVpn check whether exist vpn connection
78      * @param isRun if isExistVpn=true, check the vpn is running or not
79      * @param pkg Indicates which application the current vpn belongs to
80      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
81      * @permission ohos.permission.MANAGE_VPN
82      * @systemapi Hide this for inner system use.
83      */
84     int32_t Prepare(bool &isExistVpn, bool &isRun, std::string &pkg);
85 
86     /**
87      * extended vpn need always communication with remote vpn server, the data is send/receive by default network but
88      * not vpn network.
89      *
90      * @param socketFd extended vpn opened soecket fd
91      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
92      * @permission ohos.permission.MANAGE_VPN
93      * @systemapi Hide this for inner system use.
94      */
95     int32_t Protect(int32_t socketFd, bool isVpnExtCall = false);
96 
97     /**
98      * after extended vpn's negotiation over, need system create a VPN interface using the config parameters.
99      *
100      * @param config VPN interface parameters
101      * @param tunFd the virtual interface fd(out param)
102      * @return the interface node's file descriptor(>0) if process normal, others is error
103      * @permission ohos.permission.MANAGE_VPN
104      * @systemapi Hide this for inner system use.
105      */
106     int32_t SetUpVpn(sptr<VpnConfig> config, int32_t &tunFd, bool isVpnExtCall = false);
107 
108     /**
109      * stop the vpn connection, system will destroy the vpn network.
110      *
111      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
112      * @permission ohos.permission.MANAGE_VPN
113      * @systemapi Hide this for inner system use.
114      */
115     int32_t DestroyVpn(bool isVpnExtCall = false);
116 
117 #ifdef SUPPORT_SYSVPN
118     /**
119      * get vpn certificate data
120      *
121      * @param certType the certificate type (ca certificate, user certificate or server certificate)
122      * @param certData the certificate data (out param)
123      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
124      * @systemapi Hide this for inner use.
125      */
126     int32_t GetVpnCertData(const int32_t certType, std::vector<int8_t> &certData);
127 
128     /**
129      * setup system vpn.
130      *
131      * @param config system VPN interface parameters
132      * @param isVpnExtCall is vpnext call
133      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
134      * @permission ohos.permission.MANAGE_VPN
135      * @systemapi Hide this for inner system use.
136      */
137     int32_t SetUpVpn(const sptr<SysVpnConfig> &config, bool isVpnExtCall = false);
138 
139     /**
140      * save vpn
141      *
142      * @param config vpn config
143      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
144      * @permission ohos.permission.MANAGE_VPN
145      * @systemapi Hide this for inner system use.
146      */
147     int32_t AddSysVpnConfig(sptr<SysVpnConfig> &config);
148 
149     /**
150      * delete vpn
151      *
152      * @param vpnId vpn vpnId
153      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
154      * @permission ohos.permission.MANAGE_VPN
155      * @systemapi Hide this for inner system use.
156      */
157     int32_t DeleteSysVpnConfig(const std::string &vpnId);
158 
159     /**
160      * get app info of connected vpn
161      *
162      * @param bundleNameList app bundleName list (in param)
163      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
164      * @permission ohos.permission.MANAGE_VPN
165      * @systemapi Hide this for inner system use.
166      */
167     int32_t GetConnectedVpnAppInfo(std::vector<std::string> &bundleNameList);
168 
169     /**
170      * get vpn list
171      *
172      * @param vpnList vpn list (out param)
173      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
174      * @permission ohos.permission.MANAGE_VPN
175      * @systemapi Hide this for inner system use.
176      */
177     int32_t GetSysVpnConfigList(std::vector<sptr<SysVpnConfig>> &vpnList);
178 
179     /**
180      * get vpn detail
181      *
182      * @param config vpn config (out param)
183      * @param vpnId vpn vpnId
184      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
185      * @permission ohos.permission.MANAGE_VPN
186      * @systemapi Hide this for inner system use.
187      */
188     int32_t GetSysVpnConfig(sptr<SysVpnConfig> &config, const std::string &vpnId);
189 
190     /**
191      * get connected vpn
192      *
193      * @param config VpnConfig
194      * @return VpnConnectState
195      * @permission ohos.permission.MANAGE_VPN
196      * @systemapi Hide this for inner system use.
197      */
198     int32_t GetConnectedSysVpnConfig(sptr<SysVpnConfig> &config);
199 
200     /**
201      * nofytify the connect stage to fwk
202      *
203      * @param stage the connect stage
204      * @param result the connect result
205      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
206      * @systemapi Hide this for inner system use.
207      */
208     int32_t NotifyConnectStage(const std::string &stage, const int32_t &result);
209 
210     /**
211      * get system vpn certificate uri
212      *
213      * @param certType the certificate type (ca certificate, user certificate or server certificate)
214      * @param certUri the certificate uri (out param)
215      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
216      * @systemapi Hide this for inner system use.
217      */
218     int32_t GetSysVpnCertUri(const int32_t certType, std::string &certUri);
219 
220     /**
221      * stop the vpn connection, system will destroy the vpn network.
222      *
223      * @param vpnId vpnId
224      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
225      * @permission ohos.permission.MANAGE_VPN
226      * @systemapi Hide this for inner system use.
227      */
228     int32_t DestroyVpn(const std::string &vpnId);
229 
230     /**
231      * register the multi vpn state callback
232      *
233      * @param callback if this fuction return NETMANAGER_EXT_SUCCESS(0), this callback will be called by service
234      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
235      * @permission ohos.permission.MANAGE_VPN
236      * @systemapi Hide this for inner system use.
237      */
238     int32_t RegisterMultiVpnEvent(sptr<IVpnEventCallback> callback);
239 
240     /**
241      * unregister the multi vpn state callback
242      *
243      * @param callback if this fuction return NETMANAGER_EXT_SUCCESS(0), this callback will not be called by service
244      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
245      * @permission ohos.permission.MANAGE_VPN
246      * @systemapi Hide this for inner system use.
247      */
248     int32_t UnregisterMultiVpnEvent(sptr<IVpnEventCallback> callback);
249 
250 #endif // SUPPORT_SYSVPN
251 
252     /**
253      * register the vpn state callback
254      *
255      * @param callback if this fuction return NETMANAGER_EXT_SUCCESS(0), this callback will be called by service
256      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
257      * @permission ohos.permission.MANAGE_VPN
258      * @systemapi Hide this for inner system use.
259      */
260     int32_t RegisterVpnEvent(sptr<IVpnEventCallback> callback);
261 
262     /**
263      * unregister the vpn state callback
264      *
265      * @param callback if this fuction return NETMANAGER_EXT_SUCCESS(0), this callback will not be called by service
266      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
267      * @permission ohos.permission.MANAGE_VPN
268      * @systemapi Hide this for inner system use.
269      */
270     int32_t UnregisterVpnEvent(sptr<IVpnEventCallback> callback);
271 
272     /**
273      * create vpn connection.
274      *
275      * @return NETMANAGER_EXT_SUCCESS(0) if process normal, others is error
276      * @permission ohos.permission.MANAGE_VPN
277      * @systemapi Hide this for inner system use.
278      */
279     int32_t CreateVpnConnection(bool isVpnExtCall = false);
280 
281     /**
282      * close the tunfd of vpn interface and unregister VpnEvent.
283      */
284     void multiUserSetUpEvent();
285     int32_t RegisterBundleName(const std::string &bundleName, const std::string &abilityName);
286 
287     int32_t GetSelfAppName(std::string &selfAppName, std::string &selfBundleName);
288 
289     int32_t SetSelfVpnPid();
290     void SetVpnSaState(bool state);
291 
292 private:
293     class MonitorVpnServiceDead : public IRemoteObject::DeathRecipient {
294     public:
MonitorVpnServiceDead(NetworkVpnClient & client)295         explicit MonitorVpnServiceDead(NetworkVpnClient &client) : client_(client) {}
296         ~MonitorVpnServiceDead() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)297         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
298         {
299             client_.OnRemoteDied(remote);
300         }
301 
302     private:
303         NetworkVpnClient &client_;
304     };
305 
306     class SystemAbilityListener;
307 
308     sptr<INetworkVpnService> GetProxy();
309     void RecoverCallback();
310     void OnRemoteDied(const wptr<IRemoteObject> &remote);
311     void RegisterVpnEventCbCollection();
312     void UnregisterVpnEventCbCollection();
313 #ifdef SUPPORT_SYSVPN
314     void RegisterMultiVpnEventCbCollection();
315     void UnregisterMultiVpnEventCbCollection();
316 #endif
317 
318 private:
319     std::mutex mutex_;
320     VpnInterface vpnInterface_;
321     sptr<SystemAbilityListener> saStatusChangeListener_;
322     sptr<IVpnEventCallback> vpnEventCallback_ = nullptr;
323     sptr<INetworkVpnService> networkVpnService_ = nullptr;
324     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
325     sptr<VpnEventCallbackCollection> vpnEventCbCollection_ = sptr<VpnEventCallbackCollection>::MakeSptr();
326     sptr<VpnEventCallbackCollection> multiVpnEventCbCollection_ = sptr<VpnEventCallbackCollection>::MakeSptr();
327     std::pair<sptr<VpnConfig>, bool> clientVpnConfig_;
328     bool saStart_ = false;
329 };
330 } // namespace NetManagerStandard
331 } // namespace OHOS
332 #endif // NETWORKVPN_CLIENT_H
333