1 /*
2  * Copyright (C) 2021-2022 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 OHOS_DHCPD_INTERFACE_H
17 #define OHOS_DHCPD_INTERFACE_H
18 #include <string>
19 #include <memory>
20 #include <map>
21 #include <functional>
22 
23 #include "ipv4_address.h"
24 #include "ipv6_address.h"
25 #include "mac_address.h"
26 #include "wifi_ap_msg.h"
27 #include "dhcp_c_api.h"
28 
29 #define IP_V4 0
30 #define IP_V6 1
31 #define IP_ADDR_MAX_LEN 64
32 
33 /* Calculate the network number form the IP address and mask. */
34 #define CALC_SUBNET(IPADD, MASK) ((IPADD) & (MASK))
35 
36 namespace OHOS {
37 namespace Wifi {
38 class DhcpdInterface {
39 public:
40     /**
41      * @Description Constructs object.
42      */
43     DhcpdInterface();
44 
45     /**
46      * @Description Destructor methods.
47      */
48     ~DhcpdInterface();
49 
50     bool RegisterDhcpCallBack(const std::string &ifaceName, ServerCallBack &event);
51 
52     /**
53      * @Description Start the DHCP server and pass the IP address.
54      * @param ifaceName - Network Interface
55      * @param ipv4 - The ipv4 address of interface
56      * @param ipv6 - The ipv6 address of interface
57      * @param ipAddress - The dhcp server address
58      * @param isIpV4 - Is an ipv4 network
59      * @return true: success   false: fail
60      */
61     bool StartDhcpServerFromInterface(const std::string &ifaceName, Ipv4Address &ipv4, Ipv6Address &ipv6,
62         const std::string &ipAddress = "", bool isIpV4 = true, const int32_t &leaseTime = DHCP_LEASE_TIME);
63 
64     /**
65      * @Description Stop the DHCP server.
66      * @param ifaceName - Network Interface
67      * @return true: success      false: fail
68      */
69     bool StopDhcp(const std::string &ifaceName);
70 
71     /**
72      * @Description Get currently connected station info: ip address, device name
73      *
74      * @param ifaceName - Network Interface
75      * @param result - Connected station info
76      * @return true - success
77      * @return false - fail
78      */
79     bool GetConnectedStationInfo(const std::string &ifaceName, std::map<std::string, StationInfo> &result);
80 
81 private:
82     bool SetDhcpIpRange(const std::string &ifaceName);
83     bool CompareSubNet(
84         const std::vector<Ipv4Address> &vecIpAddr, const struct in_addr &input, const struct in_addr &mask) const;
85     Ipv4Address AssignIpAddrV4(const std::vector<Ipv4Address> &vecIpAddr, const std::string &mask,
86         const std::string &ipAddress) const;
87     Ipv6Address AssignIpAddrV6(const std::vector<Ipv6Address> &vecIpAddr);
88     bool AssignIpAddr(Ipv4Address &ipv4, Ipv6Address &ipv6, const std::vector<Ipv4Address> &vecIpv4Addr,
89         const std::vector<Ipv6Address> &vecIpv6Addr, const std::string &ipAddress, bool isIpV4);
90     bool ApplyIpAddress(const std::string &ifaceName, const Ipv4Address &ipv4, const Ipv6Address &ipv6);
91     bool UpdateDefaultConfigFile(const int32_t &leaseTime);
92     bool CallAdapterSetRange(std::string &ipAddr, const std::string &ifaceName);
93     bool GetConnectedStaInfo(const std::string &ifaceName, int staNumber, DhcpStationInfo *staInfos, int *staSize);
94 private:
95     Ipv4Address mBindIpv4;
96     Ipv6Address mBindIpv6;
97     bool mStartDhcpServerFlag = false;
98 };
99 } // namespace Wifi
100 } // namespace OHOS
101 #endif