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