• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef 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 
35 namespace OHOS {
36 namespace nmd {
37 class FwmarkClient;
38 }
39 namespace NetManagerStandard {
40 constexpr uint32_t RESERVED_BUFFER_SIZE = 512;
41 class NetConnClient {
42 public:
43     static NetConnClient &GetInstance();
44 
45     /**
46      * The interface in NetConnService can be called when the system is ready
47      *
48      * @return Returns 0, the system is ready, otherwise the system is not ready
49      * @permission ohos.permission.CONNECTIVITY_INTERNAL
50      * @systemapi Hide this for inner system use.
51      */
52     int32_t SystemReady();
53 
54     /**
55      * The interface is set permission for network
56      *
57      * @param The specified UID of app
58      * @param allow internet permission
59      * @return Returns 0, unregister the network successfully, otherwise it will fail
60      * @permission ohos.permission.CONNECTIVITY_INTERNAL
61      * @systemapi Hide this for inner system use.
62      */
63     int32_t SetInternetPermission(uint32_t uid, uint8_t allow);
64 
65     /**
66      * The interface is register the network
67      *
68      * @param bearerType Bearer Network Type
69      * @param ident Unique identification of mobile phone card
70      * @param netCaps Network capabilities registered by the network supplier
71      * @param supplierId out param, return supplier id
72      * @return Returns 0, unregister the network successfully, otherwise it will fail
73      */
74     int32_t RegisterNetSupplier(NetBearType bearerType, const std::string &ident, const std::set<NetCap> &netCaps,
75                                 uint32_t &supplierId);
76 
77     /**
78      * The interface is unregister the network
79      *
80      * @param supplierId The id of the network supplier
81      * @return Returns 0, unregister the network successfully, otherwise it will fail
82      * @permission ohos.permission.CONNECTIVITY_INTERNAL
83      * @systemapi Hide this for inner system use.
84      */
85     int32_t UnregisterNetSupplier(uint32_t supplierId);
86 
87     /**
88      * Register supplier callback
89      *
90      * @param supplierId The id of the network supplier
91      * @param callback INetSupplierCallback callback interface
92      * @return Returns 0, unregister the network successfully, otherwise it will fail
93      * @permission ohos.permission.CONNECTIVITY_INTERNAL
94      * @systemapi Hide this for inner system use.
95      */
96     int32_t RegisterNetSupplierCallback(uint32_t supplierId, const sptr<NetSupplierCallbackBase> &callback);
97 
98     /**
99      * The interface is update network connection status information
100      *
101      * @param supplierId The id of the network supplier
102      * @param netSupplierInfo network connection status information
103      * @return Returns 0, successfully update the network connection status information, otherwise it will fail
104      * @permission ohos.permission.CONNECTIVITY_INTERNAL
105      * @systemapi Hide this for inner system use.
106      */
107     int32_t UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo);
108 
109     /**
110      * The interface is update network link attribute information
111      *
112      * @param supplierId The id of the network supplier
113      * @param netLinkInfo network link attribute information
114      * @return Returns 0, successfully update the network link attribute information, otherwise it will fail
115      * @permission ohos.permission.CONNECTIVITY_INTERNAL
116      * @systemapi Hide this for inner system use.
117      */
118     int32_t UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo);
119 
120     /**
121      * Register net connection callback
122      *
123      * @param callback The callback of INetConnCallback interface
124      * @return Returns 0, successfully register net connection callback, otherwise it will failed
125      * @permission ohos.permission.CONNECTIVITY_INTERNAL
126      * @systemapi Hide this for inner system use.
127      */
128     int32_t RegisterNetConnCallback(const sptr<INetConnCallback> &callback);
129 
130     /**
131      * Register net connection callback by NetSpecifier
132      *
133      * @param netSpecifier specifier information
134      * @param callback The callback of INetConnCallback interface
135      * @param timeoutMS net connection time out
136      * @return Returns 0, successfully register net connection callback, otherwise it will failed
137      * @permission ohos.permission.CONNECTIVITY_INTERNAL
138      * @systemapi Hide this for inner system use.
139      */
140     int32_t RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> &callback,
141                                     const uint32_t &timeoutMS);
142 
143     /**
144      * Unregister net connection callback
145      *
146      * @param callback The callback of INetConnCallback interface
147      * @return Returns 0, successfully unregister net connection callback, otherwise it will fail
148      * @permission ohos.permission.CONNECTIVITY_INTERNAL
149      * @systemapi Hide this for inner system use.
150      */
151     int32_t UnregisterNetConnCallback(const sptr<INetConnCallback> &callback);
152 
153     /**
154      * Register net detection callback by netId
155      *
156      * @param netSpecifier specifier information
157      * @param callback The callback of INetDetectionCallback interface
158      * @param timeoutMS net connection time out
159      * @return Returns 0, successfully register net detection callback, otherwise it will failed
160      * @permission ohos.permission.CONNECTIVITY_INTERNAL
161      * @systemapi Hide this for inner system use.
162      */
163     int32_t RegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback);
164     /**
165      * Unregister net detection callback by netId
166      *
167      * @param callback The callback of INetDetectionCallback interface
168      * @return Returns 0, successfully unregister net detection callback, otherwise it will fail
169      * @permission ohos.permission.CONNECTIVITY_INTERNAL
170      * @systemapi Hide this for inner system use.
171      */
172     int32_t UnRegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback);
173 
174     /**
175      * The interface is to get default network
176      *
177      * @param netHandle network handle
178      * @return Returns 0 success. Otherwise fail.
179      * @permission ohos.permission.CONNECTIVITY_INTERNAL
180      * @systemapi Hide this for inner system use.
181      * @permission ohos.permission.CONNECTIVITY_INTERNAL
182      * @systemapi Hide this for inner system use.
183      */
184     int32_t GetDefaultNet(NetHandle &netHandle);
185 
186     /**
187      * The interface is to check whether has default network
188      *
189      * @param flag has default network or not
190      * @return Returns 0 success. Otherwise fail.
191      * @permission ohos.permission.CONNECTIVITY_INTERNAL
192      * @systemapi Hide this for inner system use.
193      */
194     int32_t HasDefaultNet(bool &flag);
195 
196     /**
197      * The interface is to get all acvite network
198      *
199      * @param netList a list of network
200      * @return Returns 0 success. Otherwise fail.
201      * @permission ohos.permission.CONNECTIVITY_INTERNAL
202      * @systemapi Hide this for inner system use.
203      */
204     int32_t GetAllNets(std::list<sptr<NetHandle>> &netList);
205 
206     /**
207      * get the network link information of the connection
208      *
209      * @param netHandle network handle
210      * @param info network link infomation
211      * @return Returns 0 success. Otherwise fail.
212      * @permission ohos.permission.CONNECTIVITY_INTERNAL
213      * @systemapi Hide this for inner system use.
214      */
215     int32_t GetConnectionProperties(const NetHandle &netHandle, NetLinkInfo &info);
216 
217     /**
218      * get all capabilities from network
219      *
220      * @param netHandle network handle
221      * @param netAllCap network all of capabilities
222      * @return Returns 0 success. Otherwise fail.
223      * @permission ohos.permission.CONNECTIVITY_INTERNAL
224      * @systemapi Hide this for inner system use.
225      */
226     int32_t GetNetCapabilities(const NetHandle &netHandle, NetAllCapabilities &netAllCap);
227 
228     /**
229      * The interface is to get addresses by network name
230      *
231      * @param host domain name
232      * @param netId network id
233      * @param addrList list of network addresses
234      * @return Returns 0 success. Otherwise fail.
235      * @permission ohos.permission.CONNECTIVITY_INTERNAL
236      * @systemapi Hide this for inner system use.
237      */
238     int32_t GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList);
239 
240     /**
241      * The interface is to get address by network name
242      *
243      * @param host domain name
244      * @param netId network
245      * @param addr network address
246      * @return Returns 0 success. Otherwise fail.
247      * @permission ohos.permission.CONNECTIVITY_INTERNAL
248      * @systemapi Hide this for inner system use.
249      */
250     int32_t GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr);
251 
252     /**
253      * The interface is to bind socket
254      *
255      * @param socketFd socket file description
256      * @param netId network id
257      * @return Returns 0 success. Otherwise fail.
258      * @permission ohos.permission.CONNECTIVITY_INTERNAL
259      * @systemapi Hide this for inner system use.
260      */
261     int32_t BindSocket(int32_t socketFd, int32_t netId);
262 
263     /**
264      * The interface of network detection called by the application
265      *
266      * @param netHandle network handle
267      * @return int32_t Whether the network probe is successful
268      * @permission ohos.permission.CONNECTIVITY_INTERNAL
269      * @systemapi Hide this for inner system use.
270      */
271     int32_t NetDetection(const NetHandle &netHandle);
272 
273     /**
274      * set air plane mode on or off
275      *
276      * @param state air plane mode on or not
277      * @return Returns 0 success. Otherwise fail.
278      * @permission ohos.permission.CONNECTIVITY_INTERNAL
279      * @systemapi Hide this for inner system use.
280      */
281     int32_t SetAirplaneMode(bool state);
282 
283     /**
284      * check whether the network meter is default
285      *
286      * @param isMetered the network meter is default or not
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 IsDefaultNetMetered(bool &isMetered);
292 
293     /**
294      * set global http proxy in the network
295      *
296      * @param httpProxy http proxy
297      * @return Returns 0 success. Otherwise fail.
298      * @permission ohos.permission.CONNECTIVITY_INTERNAL
299      * @systemapi Hide this for inner system use.
300      */
301     int32_t SetGlobalHttpProxy(const HttpProxy &httpProxy);
302 
303     /**
304      * get global http proxy in the network
305      *
306      * @param httpProxy http proxy
307      * @return Returns 0 success. Otherwise fail.
308      * @permission ohos.permission.CONNECTIVITY_INTERNAL
309      * @systemapi Hide this for inner system use.
310      */
311     int32_t GetGlobalHttpProxy(HttpProxy &httpProxy);
312 
313     /**
314      * set network id of app binding network
315      *
316      * @param netId network id
317      * @return Returns 0 success. Otherwise fail.
318      * @permission ohos.permission.CONNECTIVITY_INTERNAL
319      * @systemapi Hide this for inner system use.
320      */
321     int32_t GetDefaultHttpProxy(HttpProxy &httpProxy);
322 
323     /**
324      * set network id of app binding network
325      *
326      * @param netId network id
327      * @return Returns 0 success. Otherwise fail.
328      * @permission ohos.permission.CONNECTIVITY_INTERNAL
329      * @systemapi Hide this for inner system use.
330      */
331     int32_t SetAppNet(int32_t netId);
332 
333     /**
334      * get network id of app binding network
335      *
336      * @param netId network id
337      * @return Returns 0 success. Otherwise fail.
338      * @permission ohos.permission.CONNECTIVITY_INTERNAL
339      * @systemapi Hide this for inner system use.
340      */
341     int32_t GetAppNet(int32_t &netId);
342 
343     /**
344      * Get network id by identifier
345      *
346      * @param ident identifier
347      * @param netIdList  list of network id
348      * @return Returns 0 success. Otherwise fail.
349      * @permission ohos.permission.CONNECTIVITY_INTERNAL
350      * @systemapi Hide this for inner system use.
351      */
352     int32_t GetNetIdByIdentifier(const std::string &ident, std::list<int32_t> &netIdList);
353 
354     /**
355      * Register network interface state change callback
356      *
357      * @param callback The callback of INetInterfaceStateCallback interface
358      * @return Returns 0, successfully register net connection callback, otherwise it will failed
359      * @permission ohos.permission.CONNECTIVITY_INTERNAL
360      * @systemapi Hide this for inner system use.
361      */
362     int32_t RegisterNetInterfaceCallback(const sptr<INetInterfaceStateCallback> &callback);
363 
364     /**
365      * Get network interface configuration
366      *
367      * @param ifaceName Network port device name
368      * @param config Network interface configuration
369      * @return Returns 0, successfully register net connection callback, otherwise it will failed
370      * @permission ohos.permission.CONNECTIVITY_INTERNAL
371      * @systemapi Hide this for inner system use.
372      */
373     int32_t GetNetInterfaceConfiguration(const std::string &iface, NetInterfaceConfiguration &config);
374 
375     int32_t AddNetworkRoute(int32_t netId, const std::string &ifName, const std::string &destination,
376                             const std::string &nextHop);
377     int32_t RemoveNetworkRoute(int32_t netId, const std::string &ifName, const std::string &destination,
378                                const std::string &nextHop);
379     int32_t AddInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
380                                 int32_t prefixLength);
381     int32_t DelInterfaceAddress(const std::string &ifName, const std::string &ipAddr,
382                                 int32_t prefixLength);
383     int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName);
384     int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName);
385     int32_t GetPinSetForHostName(const std::string &hostname, std::string &pins);
386     int32_t GetTrustAnchorsForHostName(const std::string &hostname, std::vector<std::string> &certs);
387 
388     int32_t RegisterSlotType(uint32_t supplierId, int32_t type);
389     int32_t GetSlotType(std::string &type);
390 
391     int32_t FactoryResetNetwork();
392     int32_t RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback);
393     void RegisterAppHttpProxyCallback(std::function<void(const HttpProxy &httpProxy)> callback, uint32_t &callbackid);
394     void UnregisterAppHttpProxyCallback(uint32_t callbackid);
395     int32_t SetAppHttpProxy(const HttpProxy &httpProxy);
396      /**
397      * Whether this url prefer cellular
398      *
399      * @param url url input
400      * @param preferCellular out param, whether prefer cellular
401      * @return Returns 0, unregister the network successfully, otherwise it will fail
402      */
403     int32_t IsPreferCellularUrl(const std::string& url, bool& preferCellular);
404 
405 private:
406     class NetConnDeathRecipient : public IRemoteObject::DeathRecipient {
407     public:
NetConnDeathRecipient(NetConnClient & client)408         explicit NetConnDeathRecipient(NetConnClient &client) : client_(client) {}
409         ~NetConnDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)410         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
411         {
412             client_.OnRemoteDied(remote);
413         }
414 
415     private:
416         NetConnClient &client_;
417     };
418 
419 private:
420     NetConnClient();
421     ~NetConnClient();
422     NetConnClient& operator=(const NetConnClient&) = delete;
423     NetConnClient(const NetConnClient&) = delete;
424 
425     sptr<INetConnService> GetProxy();
426     void RecoverCallback();
427     void OnRemoteDied(const wptr<IRemoteObject> &remote);
428 
429 private:
430     std::mutex appHttpProxyCbMapMutex_;
431     uint32_t currentCallbackId_ = 0;
432     std::map<uint32_t, std::function<void(const HttpProxy &httpProxy)>> appHttpProxyCbMap_;
433     HttpProxy appHttpProxy_;
434     char buffer_[RESERVED_BUFFER_SIZE] = {0};
435     std::mutex mutex_;
436     sptr<INetConnService> NetConnService_;
437     sptr<IRemoteObject::DeathRecipient> deathRecipient_;
438     std::map<uint32_t, sptr<INetSupplierCallback>> netSupplierCallback_;
439     std::list<std::tuple<sptr<NetSpecifier>, sptr<INetConnCallback>, uint32_t>> registerConnTupleList_;
440 };
441 } // namespace NetManagerStandard
442 } // namespace OHOS
443 
444 #endif // NET_CONN_MANAGER_H
445