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