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 NETWORK_H
17#define NETWORK_H
18
19#include "event_report.h"
20#include "i_net_detection_callback.h"
21#include "i_net_monitor_callback.h"
22#include "inet_addr.h"
23#include "net_conn_event_handler.h"
24#include "net_conn_types.h"
25#include "net_link_info.h"
26#include "net_monitor.h"
27#include "net_supplier_info.h"
28#include "route.h"
29#include "nat464_service.h"
30#include "netmanager_base_common_utils.h"
31
32namespace OHOS {
33namespace NetManagerStandard {
34using NetDetectionHandler = std::function<void(uint32_t supplierId, NetDetectionStatus netState)>;
35class Network : public virtual RefBase, public INetMonitorCallback, public std::enable_shared_from_this<Network> {
36public:
37    Network(int32_t netId, uint32_t supplierId, const NetDetectionHandler &handler, NetBearType bearerType,
38            const std::shared_ptr<NetConnEventHandler> &eventHandler);
39    ~Network() = default;
40    bool operator==(const Network &network) const;
41    int32_t GetNetId() const;
42    uint32_t GetSupplierId() const;
43    bool UpdateBasicNetwork(bool isAvailable_);
44    bool UpdateNetLinkInfo(const NetLinkInfo &netLinkInfo);
45    NetLinkInfo GetNetLinkInfo() const;
46    std::string GetIfaceName() const;
47    std::string GetIdent() const;
48    void UpdateIpAddrs(const NetLinkInfo &newNetLinkInfo);
49    void HandleUpdateIpAddrs(const NetLinkInfo &newNetLinkInfo);
50    void UpdateInterfaces(const NetLinkInfo &newNetLinkInfo);
51    void UpdateRoutes(const NetLinkInfo &newNetLinkInfo);
52    void UpdateDns(const NetLinkInfo &netLinkInfo);
53    void UpdateMtu(const NetLinkInfo &netLinkInfo);
54    void UpdateTcpBufferSize(const NetLinkInfo &netLinkInfo);
55    void UpdateStatsCached(const NetLinkInfo &netLinkInfo);
56    void RegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback);
57    int32_t UnRegisterNetDetectionCallback(const sptr<INetDetectionCallback> &callback);
58    void StartNetDetection(bool needReport);
59    void SetNetCaps(const std::set<NetCap> &netCaps);
60    void SetDefaultNetWork();
61    void ClearDefaultNetWorkNetId();
62    bool IsConnecting() const;
63    bool IsConnected() const;
64    void UpdateNetConnState(NetConnState netConnState);
65    void UpdateGlobalHttpProxy(const HttpProxy &httpProxy);
66    void NetDetectionForDnsHealth(bool dnsHealthSuccess);
67
68    void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override;
69
70    bool ResumeNetworkInfo();
71    void CloseSocketsUid(uint32_t uid);
72    void StopNetDetection();
73#ifdef FEATURE_SUPPORT_POWERMANAGER
74    void UpdateForbidDetectionFlag(bool forbidDetectionFlag);
75#endif
76
77private:
78    bool CreateBasicNetwork();
79    bool CreateVirtualNetwork();
80    bool ReleaseBasicNetwork();
81    bool ReleaseVirtualNetwork();
82    void InitNetMonitor();
83    void HandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect);
84    void NotifyNetDetectionResult(NetDetectionResultCode detectionResult, const std::string &urlRedirect);
85    NetDetectionResultCode NetDetectionResultConvert(int32_t internalRet);
86    void SendConnectionChangedBroadcast(const NetConnState &netConnState) const;
87    void SendSupplierFaultHiSysEvent(NetConnSupplerFault errorType, const std::string &errMsg);
88    void ResetNetlinkInfo();
89    bool IsDetectionForDnsSuccess(NetDetectionStatus netDetectionState, bool dnsHealthSuccess);
90    bool IsDetectionForDnsFail(NetDetectionStatus netDetectionState, bool dnsHealthSuccess);
91    bool IsIfaceNameInUse();
92    bool IsNat464Prefered();
93    std::string GetNetCapabilitiesAsString(const uint32_t supplierId) const;
94
95private:
96    int32_t netId_ = 0;
97    uint32_t supplierId_ = 0;
98    NetLinkInfo netLinkInfo_;
99    NetConnState state_ = NET_CONN_STATE_UNKNOWN;
100    NetDetectionStatus detectResult_ = UNKNOWN_STATE;
101    std::atomic_bool isPhyNetCreated_ = false;
102    std::atomic_bool isVirtualCreated_ = false;
103    std::shared_ptr<NetMonitor> netMonitor_ = nullptr;
104    NetDetectionHandler netCallback_;
105    NetBearType netSupplierType_;
106    std::vector<sptr<INetDetectionCallback>> netDetectionRetCallback_;
107    std::shared_ptr<NetConnEventHandler> eventHandler_;
108    std::atomic<bool> isDetectingForDns_ = false;
109    std::set<NetCap> netCaps_;
110    std::unique_ptr<Nat464Service> nat464Service_;
111#ifdef FEATURE_SUPPORT_POWERMANAGER
112    bool forbidDetectionFlag_ = false;
113#endif
114    bool isNeedResume_ = false;
115};
116} // namespace NetManagerStandard
117} // namespace OHOS
118#endif // NETWORK_H
119