1 /*
2  * Copyright (C) 2021 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 #ifndef OHOS_DHCP_CLIENT_STATEMACHINE_H
16 #define OHOS_DHCP_CLIENT_STATEMACHINE_H
17 
18 #include <thread>
19 #include <string>
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include "dhcp_arp_checker.h"
23 #include "dhcp_client_def.h"
24 #include "dhcp_ipv6_client.h"
25 #include "dhcp_thread.h"
26 
27 #ifndef OHOS_ARCH_LITE
28 #include "common_timer_errors.h"
29 #include "timer.h"
30 #endif
31 
32 namespace OHOS {
33 namespace DHCP {
34 class  DhcpClientStateMachine{
35 public:
36     DhcpClientStateMachine(std::string ifname);
37     virtual ~DhcpClientStateMachine();
38 private:
39     int InitSpecifiedClientCfg(const std::string &ifname, bool isIpv6);
40     int GetClientNetworkInfo(void);
41     int SetIpv4State(int state);
42     int PublishDhcpResultEvent(const char *ifname, const int code, struct DhcpIpResult *result);
43     int GetPacketHeaderInfo(struct DhcpPacket *packet, uint8_t type);
44     int GetPacketCommonInfo(struct DhcpPacket *packet);
45     int AddClientIdToOpts(struct DhcpPacket *packet);
46     int AddHostNameToOpts(struct DhcpPacket *packet);
47     int AddStrToOpts(struct DhcpPacket *packet, int option, std::string &value);
48     int DhcpDiscover(uint32_t transid, uint32_t requestip);
49     int DhcpRequest(uint32_t transid, uint32_t reqip, uint32_t servip);
50     int DhcpReboot(uint32_t transid, uint32_t reqip);
51     int DhcpDecline(uint32_t transid, uint32_t clientIp, uint32_t serverIp);
52     int GetDHCPServerHostName(const struct DhcpPacket *packet, struct DhcpIpResult *result);
53     int ParseNetworkVendorInfo(const struct DhcpPacket *packet, struct DhcpIpResult *result);
54     int GetPacketReadSockFd(void);
55     int GetSigReadSockFd(void);
56     int DhcpRenew(uint32_t transid, uint32_t clientip, uint32_t serverip);
57     int DhcpRelease(uint32_t clientip, uint32_t serverip);
58     int InitConfig(const std::string &ifname, bool isIpv6);
59     void SendReboot(uint32_t targetIp, time_t timestamp);
60     void AddParamaterRequestList(struct DhcpPacket *packet);
61     void DhcpResponseHandle(time_t timestamp);
62     void DhcpAckOrNakPacketHandle(uint8_t type, struct DhcpPacket *packet, time_t timestamp);
63     void ParseDhcpAckPacket(const struct DhcpPacket *packet, time_t timestamp);
64     void ParseDhcpNakPacket(const struct DhcpPacket *packet, time_t timestamp);
65     void DhcpInit(void);
66     void DhcpStop(void);
67     void InitSocketFd(void);
68     void SetSocketMode(uint32_t mode);
69     void ParseNetworkServerIdInfo(const struct DhcpPacket *packet, struct DhcpIpResult *result);
70     void ParseNetworkInfo(const struct DhcpPacket *packet, struct DhcpIpResult *result);
71     void ParseNetworkDnsInfo(const struct DhcpPacket *packet, struct DhcpIpResult *result);
72     void SetIpv4DefaultDns(struct DhcpIpResult *result);
73     void ParseNetworkDnsValue(struct DhcpIpResult *result, uint32_t uData, size_t &len, int &count);
74     void DhcpOfferPacketHandle(uint8_t type, const struct DhcpPacket *packet, time_t timestamp);
75     void DhcpRequestHandle(time_t timestamp);
76     void Rebinding(time_t timestamp);
77     void Requesting(time_t timestamp);
78     void Renewing(time_t timestamp);
79     void Reboot(time_t timestamp);
80     void Declining(time_t timestamp);
81     void AddParamaterRebootList(struct DhcpPacket *packet);
82     void InitSelecting(time_t timestamp);
83     void SignalReceiver(void);
84     void RunGetIPThreadFunc(const DhcpClientStateMachine &instance);
85     void FormatString(struct DhcpIpResult *result);
86     uint32_t GetRandomId(void);
87     uint32_t GetDhcpTransID(void);
88     void IpConflictDetect();
89     void FastArpDetect();
90     void SlowArpDetect(time_t timestamp);
91     void SlowArpDetectCallback(bool isReachable);
92     bool IsArpReachable(uint32_t timeoutMillis, std::string ipAddress);
93     void SaveIpInfoInLocalFile(const DhcpIpResult ipResult);
94     int32_t GetCachedDhcpResult(std::string targetBssid, IpInfoCached &ipCached);
95     void TryCachedIp();
96     void SetDefaultNetMask(struct DhcpIpResult *result);
97 #ifndef OHOS_ARCH_LITE
98     void GetDhcpOffer(DhcpPacket *getPacket, int64_t timestamp);
99 #endif
100 
101 public:
102 #ifndef OHOS_ARCH_LITE
103     void StartTimer(TimerType type, uint32_t &timerId, int64_t interval, bool once);
104     void StopTimer(uint32_t &timerId);
105     void GetIpTimerCallback();
106     void RenewDelayCallback();
107     void RebindDelayCallback();
108     void RemainingDelayCallback();
109 #endif
110     int StartIpv4Type(const std::string &ifname, bool isIpv6, ActionMode action);
111     int InitStartIpv4Thread(const std::string &ifname, bool isIpv6);
112     int StartIpv4(void);
113     int ExecDhcpRelease(void);
114     int ExecDhcpRenew(void);
115     int ExitIpv4(void);
116     int StopIpv4(void);
117     int InitSignalHandle();
118     int CloseSignalHandle();
119     ActionMode GetAction(void);
120     void SetConfiguration(const RouterCfg routerCfg);
121     void ScheduleLeaseTimers(bool isCachedIp);
122     void CloseAllRenewTimer();
123     int SendStopSignal();
124 private:
125     int m_dhcp4State;
126     int m_sockFd;
127     int m_sigSockFds[NUMBER_TWO];
128     int m_resendTimer;
129     uint32_t m_sentPacketNum;
130     uint32_t m_timeoutTimestamp;
131     int64_t m_renewalTimestamp;
132     int64_t m_renewalTimestampBoot;
133     uint32_t m_leaseTime;
134     uint32_t m_renewalSec;
135     uint32_t m_rebindSec;
136     uint32_t m_requestedIp4;
137     uint32_t m_serverIp4;
138     uint32_t m_socketMode;
139     uint32_t m_transID;
140     DhcpClientCfg m_cltCnf;
141     std::atomic<bool> threadExit_{true};
142     std::string m_ifName;  //对象服务的网卡名称
143     std::unique_ptr<DhcpThread> ipv4Thread_ = nullptr;
144     ActionMode m_action;
145 #ifndef OHOS_ARCH_LITE
146     std::mutex getIpTimerMutex;
147     uint32_t getIpTimerId;
148     uint32_t renewDelayTimerId;
149     uint32_t rebindDelayTimerId;
150     uint32_t remainingDelayTimerId;
151     uint32_t m_slowArpTaskId;
152 #endif
153     std::string m_arpDectionTargetIp;
154     RouterCfg m_routerCfg;
155     uint32_t m_conflictCount;
156     DhcpIpResult m_dhcpIpResult;
157     DhcpArpChecker m_dhcpArpChecker;
158     std::function<void(bool isReachable)> m_slowArpCallback;
159     bool m_slowArpDetecting;
160     int64_t firstSendPacketTime_;
161 };
162 
163 typedef struct{
164     std::string ifName;
165     bool isIpv6;
166     DhcpClientStateMachine *pStaStateMachine;
167     DhcpIpv6Client *pipv6Client;
168 }DhcpClient;
169 }  // namespace DHCP
170 }  // namespace OHOS
171 #endif