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