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      * @systemapi Hide this for inner system use.
363      */
364     int32_t GetAppNet(int32_t &netId);
365 
366     /**
367      * Get network id by identifier
368      *
369      * @param ident identifier
370      * @param netIdList  list of network id
371      * @return Returns 0 success. Otherwise fail.
372      * @permission ohos.permission.CONNECTIVITY_INTERNAL
373      * @systemapi Hide this for inner system use.
374      */
375     int32_t GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList);
376 
377     /**
378      * Register network interface state change callback
379      *
380      * @param callback The callback of INetInterfaceStateCallback interface
381      * @return Returns 0, successfully register net connection callback, otherwise it will failed
382      * @permission ohos.permission.CONNECTIVITY_INTERNAL
383      * @systemapi Hide this for inner system use.
384      */
385     int32_t RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback);
386 
387     /**
388      * Get network interface configuration
389      *
390      * @param ifaceName Network port device name
391      * @param config Network interface configuration
392      * @return Returns 0, successfully register net connection callback, otherwise it will failed
393      * @permission ohos.permission.CONNECTIVITY_INTERNAL
394      * @systemapi Hide this for inner system use.
395      */
396     int32_t GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config);
397 
398     int32_t AddNetworkRoute(int32_t netId, const std::string &ifName, const std::string &destination,
399                             const std::string &nextHop);
400     int32_t RemoveNetworkRoute(int32_t netId, const std::string &ifName, const std::string &destination,
401                                const std::string &nextHop);
402     int32_t AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
403                                 int32_t prefixLength);
404     int32_t DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
405                                 int32_t prefixLength);
406     int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName);
407     int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName);
408     int32_t GetPinSetForHostName(const std::string &hostname, std::string &pins);
409     bool IsPinOpenMode(const std::string &hostname);
410     int32_t GetTrustAnchorsForHostName(const std::string &hostname, std::vector<std::string> &certs);
411 
412     int32_t RegisterSlotType(uint32_t supplierId, int32_t type);
413     int32_t GetSlotType(std::string &type);
414     int32_t FactoryResetNetwork();
415     int32_t RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback);
416     void RegisterAppHttpProxyCallback(std::function<void(const HttpProxy &httpProxy)> callback, uint32_t &callbackid);
417     void UnregisterAppHttpProxyCallback(uint32_t callbackid);
418     int32_t SetAppHttpProxy(const HttpProxy &httpProxy);
419      /**
420      * Whether this url prefer cellular
421      *
422      * @param url url input
423      * @param preferCellular out param, whether prefer cellular
424      * @return Returns 0, unregister the network successfully, otherwise it will fail
425      */
426     int32_t IsPreferCellularUrl(const std::string& url, bool& preferCellular);
427 
428     int32_t RegisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback);
429 
430     int32_t UnregisterPreAirplaneCallback(const sptr<IPreAirplaneCallback> callback);
431 
432     int32_t UpdateSupplierScore(NetBearType bearerType, uint32_t detectionStatus, uint32_t& supplierId);
433 
434     int32_t EnableVnicNetwork(const sptr<NetLinkInfo> &netLinkInfo, const std::set<int32_t> &uids);
435 
436     int32_t DisableVnicNetwork();
437 
438     /**
439      * This function returns whether the caller process's API version is not earlier
440      * than {@link targetApiVersion}, which meaning the caller process has same or later
441      * target API version.
442      *
443      * @param targetApiVersion target API version.
444      * @return true for supported and false for not, and true by default if cannot get
445      * process bundle's information.
446      */
447     static bool IsAPIVersionSupported(int targetApiVersion);
448 
449     /**
450      * This function returns the caller's bundle name.
451      * This function is defined here because it is required in some Network Kit APIs.
452      * Please do not use this function except Network Kit APIs.
453      *
454      * @return optional bundle name in string format, return empty if cannot get bundle
455      * info from bundle manager.
456      */
457     static std::optional<std::string> ObtainBundleNameForSelf();
458 
459     int32_t EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif);
460 
461     int32_t EnableDistributedServerNet(const std::string &iif, const std::string &devIface, const std::string &dstAddr);
462 
463     int32_t DisableDistributedNet(bool isServer);
464 
465     int32_t CloseSocketsUid(int32_t netId, uint32_t uid);
466 private:
467     class NetConnDeathRecipient : public IRemoteObject::DeathRecipient {
468     public:
NetConnDeathRecipient(NetConnClient &client)469         explicit NetConnDeathRecipient(NetConnClient &client) : client_(client) {}
470         ~NetConnDeathRecipient() override = default;
471         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
472         {
473             client_.OnRemoteDied(remote);
474         }
475 
476     private:
477         NetConnClient &client_;
478     };
479 
480 private:
481     NetConnClient();
482     ~NetConnClient();
483     NetConnClient& operator=(const NetConnClient&) = delete;
484     NetConnClient(const NetConnClient&) = delete;
485 
486     sptr<INetConnService> GetProxy();
487     void RecoverCallbackAndGlobalProxy();
488     void OnRemoteDied(const wptr<IRemoteObject> &remote);
489     void DlCloseRemoveDeathRecipient();
490     static std::optional<int32_t> ObtainTargetApiVersionForSelf();
491     static std::optional<std::string> ObtainBundleNameFromBundleMgr();
492 
493 private:
494     std::mutex appHttpProxyCbMapMutex_;
495     uint32_t currentCallbackId_ = 0;
496     std::map<uint32_t, std::function<void(const HttpProxy &httpProxy)>> appHttpProxyCbMap_;
497     HttpProxy appHttpProxy_;
498     HttpProxy globalHttpProxy_;
499     char buffer_[RESERVED_BUFFER_SIZE] = {0};
500     std::mutex mutex_;
501     sptr<INetConnService> NetConnService_;
502     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
503     std::map<uint32_t, sptr<INetSupplierCallback>> netSupplierCallback_;
504     std::list<std::tuple<sptr<NetSpecifier>, sptr<INetConnCallback>, uint32_t>> registerConnTupleList_;
505     SafeMap<uint32_t, uint8_t> netPermissionMap_;
506     sptr<IPreAirplaneCallback> preAirplaneCallback_;
507     std::mutex registerConnTupleListMutex_;
508     std::mutex netSupplierCallbackMutex_;
509 };
510 } // namespace NetManagerStandard
511 } // namespace OHOS
512 
513 #endif // NET_CONN_MANAGER_H
514