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 DEV_INTERFACE_STATE_H
17 #define DEV_INTERFACE_STATE_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <iosfwd>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 #include "http_proxy.h"
27 #include "mac_address_info.h"
28 #include "interface_configuration.h"
29 #include "net_all_capabilities.h"
30 #include "net_link_info.h"
31 #include "net_specifier.h"
32 #include "net_supplier_info.h"
33 #include "refbase.h"
34 
35 namespace OHOS {
36 namespace NetManagerStandard {
37 class DevInterfaceState : public virtual RefBase {
38 public:
39     DevInterfaceState();
40     ~DevInterfaceState() = default;
41     void SetDevName(const std::string &devName);
42     void SetNetCaps(const std::set<NetCap> &netCaps);
43     void SetLinkUp(bool up);
44     void SetlinkInfo(sptr<NetLinkInfo> &linkInfo);
45     void SetIfcfg(sptr<InterfaceConfiguration> &ifCfg);
46     void SetLancfg(sptr<InterfaceConfiguration> &ifCfg);
47     void SetDhcpReqState(bool dhcpReqState);
48     void UpdateNetHttpProxy(const HttpProxy &httpProxy);
49     void UpdateLinkInfo(const sptr<StaticConfiguration> &config);
50     std::string GetDevName() const;
51     const std::set<NetCap> &GetNetCaps() const;
52     std::set<NetCap> GetNetCaps();
53     bool GetLinkUp() const;
54     sptr<NetLinkInfo> GetLinkInfo() const;
55     sptr<InterfaceConfiguration> GetIfcfg() const;
56     IPSetMode GetIPSetMode() const;
57     bool GetDhcpReqState() const;
58     bool IsLanIface();
59     void UpdateLanLinkInfo();
60     void UpdateLanLinkInfo(const sptr<StaticConfiguration> &config);
61 
62     void RemoteRegisterNetSupplier();
63     void RemoteUnregisterNetSupplier();
64     void RemoteUpdateNetLinkInfo();
65     void RemoteUpdateNetSupplierInfo();
66 
67     void GetDumpInfo(std::string &info);
68 
69 private:
70     enum ConnLinkState { REGISTERED, UNREGISTERED, LINK_AVAILABLE, LINK_UNAVAILABLE };
71     void UpdateLinkInfo();
72     void UpdateSupplierAvailable();
73     void CreateLocalRoute(const std::string &iface, const std::vector<INetAddr> &ipAddrList,
74                           const std::vector<INetAddr> &netMaskList);
75     std::string GetIpv4Prefix(const std::string &ipv4Addr, const std::vector<INetAddr> &netMaskList);
76     void GetTargetNetAddrWithSameFamily(const std::string &bySrcAddr, const std::vector<INetAddr> &fromAddrList,
77                                         INetAddr &targetNetAddr);
78     void GetRoutePrefixlen(const std::string &bySrcAddr, const std::vector<INetAddr> &fromAddrList,
79                            INetAddr &targetNetAddr);
80 
81 private:
82     ConnLinkState connLinkState_ = UNREGISTERED;
83     uint32_t netSupplier_ = 0;
84     std::string devName_;
85     bool linkUp_ = false;
86     bool dhcpReqState_ = false;
87     sptr<NetLinkInfo> linkInfo_ = nullptr;
88     sptr<NetSupplierInfo> netSupplierInfo_ = nullptr;
89     sptr<InterfaceConfiguration> ifCfg_ = nullptr;
90     sptr<MacAddressInfo> macAddrInfo_ = nullptr;
91     std::set<NetCap> netCaps_;
92     NetBearType bearerType_ = BEARER_ETHERNET;
93 };
94 } // namespace NetManagerStandard
95 } // namespace OHOS
96 #endif // DEV_INTERFACE_STATE_H
97