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