1 /*
2  * Copyright (c) 2021-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 NET_CONN_MANAGER_H
17 #define NET_CONN_MANAGER_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "parcel.h"
23 #include "singleton.h"
24 
25 #include "http_proxy.h"
26 #include "i_net_conn_service.h"
27 #include "i_net_interface_callback.h"
28 #include "i_net_supplier_callback.h"
29 #include "net_handle.h"
30 #include "net_link_info.h"
31 #include "net_specifier.h"
32 #include "net_supplier_callback_base.h"
33 #include "i_net_factoryreset_callback.h"
34 #include "safe_map.h"
35 
36 namespace OHOS {
37 namespace nmd {
38 class FwmarkClient;
39 }
40 namespace NetManagerStandard {
41 constexpr uint32_t RESERVED_BUFFER_SIZE = 512;
42 class NetConnClient {
43 public:
44     static NetConnClient &GetInstance();
45 
46     /**
47      * The interface in NetConnService can be called when the system is ready
48      *
49      * @return Returns 0, the system is ready, otherwise the system is not ready
50      * @permission ohos.permission.CONNECTIVITY_INTERNAL
51      * @systemapi Hide this for inner system use.
52      */
53     int32_t SystemReady();
54 
55     /**
56      * The interface is set permission for network
57      *
58      * @param The specified UID of app
59      * @param allow internet permission
60      * @return Returns 0, unregister the network successfully, otherwise it will fail
61      * @permission ohos.permission.CONNECTIVITY_INTERNAL
62      * @systemapi Hide this for inner system use.
63      */
64     int32_t SetInternetPermission(uint32_t uid, uint8_t allow);
65 
66     /**
67      * The interface is register the network
68      *
69      * @param bearerType Bearer Network Type
70      * @param ident Unique identification of mobile phone card
71      * @param netCaps Network capabilities registered by the network supplier
72      * @param supplierId out param, return supplier id
73      * @return Returns 0, unregister the network successfully, otherwise it will fail
74      */
75     int32_t RegisterNetSupplier(NetBearType bearerType, const std::string &ident, const std::set<NetCap> &netCaps,
76                                 uint32_t &supplierId);
77 
78     /**
79      * The interface is unregister the network
80      *
81      * @param supplierId The id of the network supplier
82      * @return Returns 0, unregister the network successfully, otherwise it will fail
83      * @permission ohos.permission.CONNECTIVITY_INTERNAL
84      * @systemapi Hide this for inner system use.
85      */
86     int32_t UnregisterNetSupplier(uint32_t supplierId);
87 
88     /**
89      * Register supplier callback
90      *
91      * @param supplierId The id of the network supplier
92      * @param callback INetSupplierCallback callback interface
93      * @return Returns 0, unregister the network successfully, otherwise it will fail
94      * @permission ohos.permission.CONNECTIVITY_INTERNAL
95      * @systemapi Hide this for inner system use.
96      */
97     int32_t RegisterNetSupplierCallback(uint32_t supplierId, const sptr<NetSupplierCallbackBase> &callback);
98 
99     /**
100      * The interface is update network connection status information
101      *
102      * @param supplierId The id of the network supplier
103      * @param netSupplierInfo network connection status information
104      * @return Returns 0, successfully update the network connection status information, otherwise it will fail
105      * @permission ohos.permission.CONNECTIVITY_INTERNAL
106      * @systemapi Hide this for inner system use.
107      */
108     int32_t UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo);
109 
110     /**
111      * The interface is update network link attribute information
112      *
113      * @param supplierId The id of the network supplier
114      * @param netLinkInfo network link attribute information
115      * @return Returns 0, successfully update the network link attribute information, otherwise it will fail
116      * @permission ohos.permission.CONNECTIVITY_INTERNAL
117      * @systemapi Hide this for inner system use.
118      */
119     int32_t UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo);
120 
121     /**
122      * Register net connection callback
123      *
124      * @param callback The callback of INetConnCallback interface
125      * @return Returns 0, successfully register net connection callback, otherwise it will failed
126      * @permission ohos.permission.CONNECTIVITY_INTERNAL
127      * @systemapi Hide this for inner system use.
128      */
129     int32_t RegisterNetConnCallback(const sptr<INetConnCallback> callback);
130 
131     /**
132      * Register net connection callback by NetSpecifier
133      *
134      * @param netSpecifier specifier information
135      * @param callback The callback of INetConnCallback interface
136      * @param timeoutMS net connection time out
137      * @return Returns 0, successfully register net connection callback, otherwise it will failed
138      * @permission ohos.permission.CONNECTIVITY_INTERNAL
139      * @systemapi Hide this for inner system use.
140      */
141     int32_t RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> callback,
142                                     const uint32_t &timeoutMS);
143 
144     /**
145      * Request net connection callback by NetSpecifier
146      *
147      * @param netSpecifier specifier information
148      * @param callback The callback of INetConnCallback interface
149      * @param timeoutMS net connection time out
150      * @return Returns 0, successfully register net connection callback, otherwise it will failed
151      * @permission ohos.permission.CONNECTIVITY_INTERNAL
152      * @systemapi Hide this for inner system use.
153      */
154     int32_t RequestNetConnection(const sptr<NetSpecifier> netSpecifier, const sptr<INetConnCallback> callback,
155                                     const uint32_t timeoutMS);
156     /**
157      * Unregister net connection callback
158      *
159      * @param callback The callback of INetConnCallback interface
160      * @return Returns 0, successfully unregister net connection callback, otherwise it will fail
161      * @permission ohos.permission.CONNECTIVITY_INTERNAL
162      * @systemapi Hide this for inner system use.
163      */
164     int32_t UnregisterNetConnCallback(const sptr<INetConnCallback> &callback);
165 
166     /**
167      * Register net detection callback by netId
168      *
169      * @param netSpecifier specifier information
170      * @param callback The callback of INetDetectionCallback interface
171      * @param timeoutMS net connection time out
172      * @return Returns 0, successfully register net detection callback, otherwise it will failed
173      * @permission ohos.permission.CONNECTIVITY_INTERNAL
174      * @systemapi Hide this for inner system use.
175      */
176     int32_t RegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback);
177     /**
178      * Unregister net detection callback by netId
179      *
180      * @param callback The callback of INetDetectionCallback interface
181      * @return Returns 0, successfully unregister net detection callback, otherwise it will fail
182      * @permission ohos.permission.CONNECTIVITY_INTERNAL
183      * @systemapi Hide this for inner system use.
184      */
185     int32_t UnRegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback);
186 
187     /**
188      * The interface is to get default network
189      *
190      * @param netHandle network handle
191      * @return Returns 0 success. Otherwise fail.
192      * @permission ohos.permission.CONNECTIVITY_INTERNAL
193      * @systemapi Hide this for inner system use.
194      * @permission ohos.permission.CONNECTIVITY_INTERNAL
195      * @systemapi Hide this for inner system use.
196      */
197     int32_t GetDefaultNet(NetHandle &netHandle);
198 
199     /**
200      * The interface is to check whether has default network
201      *
202      * @param flag has default network or not
203      * @return Returns 0 success. Otherwise fail.
204      * @permission ohos.permission.CONNECTIVITY_INTERNAL
205      * @systemapi Hide this for inner system use.
206      */
207     int32_t HasDefaultNet(bool &flag);
208 
209     /**
210      * The interface is to get all acvite network
211      *
212      * @param netList a list of network
213      * @return Returns 0 success. Otherwise fail.
214      * @permission ohos.permission.CONNECTIVITY_INTERNAL
215      * @systemapi Hide this for inner system use.
216      */
217     int32_t GetAllNets(std::list<sptr<NetHandle>> &netList);
218 
219     /**
220      * get the network link information of the connection
221      *
222      * @param netHandle network handle
223      * @param info network link infomation
224      * @return Returns 0 success. Otherwise fail.
225      * @permission ohos.permission.CONNECTIVITY_INTERNAL
226      * @systemapi Hide this for inner system use.
227      */
228     int32_t GetConnectionProperties(const NetHandle &netHandle, NetLinkInfo &info);
229 
230     /**
231      * get all capabilities from network
232      *
233      * @param netHandle network handle
234      * @param netAllCap network all of capabilities
235      * @return Returns 0 success. Otherwise fail.
236      * @permission ohos.permission.CONNECTIVITY_INTERNAL
237      * @systemapi Hide this for inner system use.
238      */
239     int32_t GetNetCapabilities(const NetHandle &netHandle, NetAllCapabilities &netAllCap);
240 
241     /**
242      * The interface is to get addresses by network name
243      *
244      * @param host domain name
245      * @param netId network id
246      * @param addrList list of network addresses
247      * @return Returns 0 success. Otherwise fail.
248      * @permission ohos.permission.CONNECTIVITY_INTERNAL
249      * @systemapi Hide this for inner system use.
250      */
251     int32_t GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList);
252 
253     /**
254      * The interface is to get address by network name
255      *
256      * @param host domain name
257      * @param netId network
258      * @param addr network address
259      * @return Returns 0 success. Otherwise fail.
260      * @permission ohos.permission.CONNECTIVITY_INTERNAL
261      * @systemapi Hide this for inner system use.
262      */
263     int32_t GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr);
264 
265     /**
266      * The interface is to get all iface and ident maps
267      *
268      * @param bearerType the type of network
269      * @param ifaceNameIdentMaps the map of ifaceName and ident
270      * @return Returns 0 success. Otherwise fail.
271      * @permission ohos.permission.CONNECTIVITY_INTERNAL
272      * @systemapi Hide this for inner system use.
273      */
274     int32_t GetIfaceNameIdentMaps(NetBearType bearerType, SafeMap<std::string, std::string> &ifaceNameIdentMaps);
275 
276     /**
277      * The interface is to bind socket
278      *
279      * @param socketFd socket file description
280      * @param netId network id
281      * @return Returns 0 success. Otherwise fail.
282      * @permission ohos.permission.CONNECTIVITY_INTERNAL
283      * @systemapi Hide this for inner system use.
284      */
285     int32_t BindSocket(int32_t socketFd, int32_t netId);
286 
287     /**
288      * The interface of network detection called by the application
289      *
290      * @param netHandle network handle
291      * @return int32_t Whether the network probe is successful
292      * @permission ohos.permission.CONNECTIVITY_INTERNAL
293      * @systemapi Hide this for inner system use.
294      */
295     int32_t NetDetection(const NetHandle &netHandle);
296 
297     /**
298      * set air plane mode on or off
299      *
300      * @param state air plane mode on or not
301      * @return Returns 0 success. Otherwise fail.
302      * @permission ohos.permission.CONNECTIVITY_INTERNAL
303      * @systemapi Hide this for inner system use.
304      */
305     int32_t SetAirplaneMode(bool state);
306 
307     /**
308      * check whether the network meter is default
309      *
310      * @param isMetered the network meter is default or not
311      * @return Returns 0 success. Otherwise fail.
312      * @permission ohos.permission.CONNECTIVITY_INTERNAL
313      * @systemapi Hide this for inner system use.
314      */
315     int32_t IsDefaultNetMetered(bool &isMetered);
316 
317     /**
318      * set global http proxy in the network
319      *
320      * @param httpProxy http proxy
321      * @return Returns 0 success. Otherwise fail.
322      * @permission ohos.permission.CONNECTIVITY_INTERNAL
323      * @systemapi Hide this for inner system use.
324      */
325     int32_t SetGlobalHttpProxy(const HttpProxy &httpProxy);
326 
327     /**
328      * get global http proxy in the network
329      *
330      * @param httpProxy http proxy
331      * @return Returns 0 success. Otherwise fail.
332      * @permission ohos.permission.CONNECTIVITY_INTERNAL
333      * @systemapi Hide this for inner system use.
334      */
335     int32_t GetGlobalHttpProxy(HttpProxy &httpProxy);
336 
337     /**
338      * set network id of app binding network
339      *
340      * @param netId network id
341      * @return Returns 0 success. Otherwise fail.
342      * @permission ohos.permission.CONNECTIVITY_INTERNAL
343      * @systemapi Hide this for inner system use.
344      */
345     int32_t GetDefaultHttpProxy(HttpProxy &httpProxy);
346 
347     /**
348      * set network id of app binding network
349      *
350      * @param netId network id
351      * @return Returns 0 success. Otherwise fail.
352      * @permission ohos.permission.CONNECTIVITY_INTERNAL
353      * @systemapi Hide this for inner system use.
354      */
355     int32_t SetAppNet(int32_t netId);
356 
357     /**
358      * get network id of app binding network
359      *
360      * @param netId network id
361      * @return Returns 0 success. Otherwise fail.
362      * @permission ohos.permission.CONNECTIVITY_INTERNAL
363      * @systemapi Hide this for inner system use.
364      */
365     int32_t GetAppNet(int32_t &netId);
366 
367     /**
368      * Get network id by identifier
369      *
370      * @param ident identifier
371      * @param netIdList  list of network id
372      * @return Returns 0 success. Otherwise fail.
373      * @permission ohos.permission.CONNECTIVITY_INTERNAL
374      * @systemapi Hide this for inner system use.
375      */
376     int32_t GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList);
377 
378     /**
379      * Register network interface state change callback
380      *
381      * @param callback The callback of INetInterfaceStateCallback interface
382      * @return Returns 0, successfully register net connection callback, otherwise it will failed
383      * @permission ohos.permission.CONNECTIVITY_INTERNAL
384      * @systemapi Hide this for inner system use.
385      */
386     int32_t RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback);
387 
388     /**
389      * Get network interface configuration
390      *
391      * @param ifaceName Network port device name
392      * @param config Network interface configuration
393      * @return Returns 0, successfully register net connection callback, otherwise it will failed
394      * @permission ohos.permission.CONNECTIVITY_INTERNAL
395      * @systemapi Hide this for inner system use.
396      */
397     int32_t GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config);
398 
399     int32_t AddNetworkRoute(int32_t netId, const std::string &ifName, const std::string &destination,
400                             const std::string &nextHop);
401     int32_t RemoveNetworkRoute(int32_t netId, const std::string &ifName, const std::string &destination,
402                                const std::string &nextHop);
403     int32_t AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
404                                 int32_t prefixLength);
405     int32_t DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
406                                 int32_t prefixLength);
407     int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName);
408     int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName);
409     int32_t GetPinSetForHostName(const std::string &hostname, std::string &pins);
410     bool IsPinOpenMode(const std::string &hostname);
411     int32_t GetTrustAnchorsForHostName(const std::string &hostname, std::vector<std::string> &certs);
412 
413     int32_t RegisterSlotType(uint32_t supplierId, int32_t type);
414     int32_t GetSlotType(std::string &type);
415     int32_t FactoryResetNetwork();
416     int32_t RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback);
417     void RegisterAppHttpProxyCallback(std::function<void(const HttpProxy &httpProxy)> callback, uint32_t &callbackid);
418     void UnregisterAppHttpProxyCallback(uint32_t callbackid);
419     int32_t SetAppHttpProxy(const HttpProxy &httpProxy);
420      /**
421      * Whether this url prefer cellular
422      *
423      * @param url url input
424      * @param preferCellular out param, whether prefer cellular
425      * @return Returns 0, unregister the network successfully, otherwise it will fail
426      */
427     int32_t IsPreferCellularUrl(const std::string& url, bool& preferCellular);
428 
429     int32_t RegisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback);
430 
431     int32_t UnregisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback);
432 
433     int32_t UpdateSupplierScore(NetBearType bearerType, uint32_t detectionStatus, uint32_t& supplierId);
434 
435     int32_t EnableVnicNetwork(const sptr<NetLinkInfo> &netLinkInfo, const std::set<int32_t> &uids);
436 
437     int32_t DisableVnicNetwork();
438 
439     /**
440      * This function returns whether the caller process's API version is not earlier
441      * than {@link targetApiVersion}, which meaning the caller process has same or later
442      * target API version.
443      *
444      * @param targetApiVersion target API version.
445      * @return true for supported and false for not, and true by default if cannot get
446      * process bundle's information.
447      */
448     static bool IsAPIVersionSupported(int targetApiVersion);
449 
450     /**
451      * This function returns the caller's bundle name.
452      * This function is defined here because it is required in some Network Kit APIs.
453      * Please do not use this function except Network Kit APIs.
454      *
455      * @return optional bundle name in string format, return empty if cannot get bundle
456      * info from bundle manager.
457      */
458     static std::optional<std::string> ObtainBundleNameForSelf();
459 
460 private:
461     class NetConnDeathRecipient : public IRemoteObject::DeathRecipient {
462     public:
NetConnDeathRecipient(NetConnClient & client)463         explicit NetConnDeathRecipient(NetConnClient &client) : client_(client) {}
464         ~NetConnDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)465         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
466         {
467             client_.OnRemoteDied(remote);
468         }
469 
470     private:
471         NetConnClient &client_;
472     };
473 
474 private:
475     NetConnClient();
476     ~NetConnClient();
477     NetConnClient& operator=(const NetConnClient&) = delete;
478     NetConnClient(const NetConnClient&) = delete;
479 
480     sptr<INetConnService> GetProxy();
481     void RecoverCallbackAndGlobalProxy();
482     void OnRemoteDied(const wptr<IRemoteObject> &remote);
483     void DlCloseRemoveDeathRecipient();
484     static std::optional<int32_t> ObtainTargetApiVersionForSelf();
485     static std::optional<std::string> ObtainBundleNameFromBundleMgr();
486 
487 private:
488     std::mutex appHttpProxyCbMapMutex_;
489     uint32_t currentCallbackId_ = 0;
490     std::map<uint32_t, std::function<void(const HttpProxy &httpProxy)>> appHttpProxyCbMap_;
491     HttpProxy appHttpProxy_;
492     HttpProxy globalHttpProxy_;
493     char buffer_[RESERVED_BUFFER_SIZE] = {0};
494     std::mutex mutex_;
495     sptr<INetConnService> NetConnService_;
496     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
497     std::map<uint32_t, sptr<INetSupplierCallback>> netSupplierCallback_;
498     std::list<std::tuple<sptr<NetSpecifier>, sptr<INetConnCallback>, uint32_t>> registerConnTupleList_;
499     SafeMap<uint32_t, uint8_t> netPermissionMap_;
500     sptr<IPreAirplaneCallback> preAirplaneCallback_;
501     std::mutex registerConnTupleListMutex_;
502 };
503 } // namespace NetManagerStandard
504 } // namespace OHOS
505 
506 #endif // NET_CONN_MANAGER_H
507