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 #ifndef OHOS_DHCP_CLIENT_DEF_H 16 #define OHOS_DHCP_CLIENT_DEF_H 17 18 #include <stdbool.h> 19 #include <stdint.h> 20 #include <netinet/udp.h> 21 #include <netinet/ip.h> 22 #include <string> 23 #include "dhcp_define.h" 24 #define DHCP_GET_TIMEOUT 15 25 #define MAXRETRYTIMEOUT 10 26 #define USECOND_CONVERT 1000 27 #define MAX_READ_EVENT_SIZE 512 28 #define NUMBER_ONE 1 29 #define NUMBER_TWO 2 30 #define NUMBER_THREE 3 31 #define NUMBER_FOUR 4 32 #define NUMBER_FIVE 5 33 #define NUMBER_FIFTY_FOUR 54 34 #define FIRST_TIMEOUT_SEC 1 35 #define MAX_TIMEOUT_SEC 6 36 #define DOUBLE_TIME 2 37 #define RENEWAL_SEC_MULTIPLE 0.5 38 #define REBIND_SEC_MULTIPLE 0.875 39 #define TIME_INTERVAL_MAX 1 40 #define TIMEOUT_TIMES_MAX 15 41 #define MAX_WAIT_TIMES 5 42 #define TIMEOUT_WAIT_SEC 1 43 #define TIMEOUT_MORE_WAIT_SEC 60 44 #define LEASETIME_DEFAULT 1 45 #define DHCP_FAILE_TIMEOUT_THR 6 46 #define MAX_CONFLICTS_COUNT 2 47 #define RENEWAL_SEC_MULTIPLE_SPECIFIC_NETWORK 0.9 48 #define SELECT_TIMEOUT_US 100000 49 50 #define MAC_ADDR_LEN 6 51 #define MAC_ADDR_CHAR_NUM 3 52 #define ETHERNET_TYPE 0x01 53 #define ETHERNET_LEN 6 54 #define VENDOR_MAX_LEN 64 55 #define MAGIC_COOKIE 0x63825363 56 #define BROADCAST_FLAG 0x8000 57 inline constexpr char MAC_BCAST_ADDR[] = "\xff\xff\xff\xff\xff\xff"; 58 #define SIGNED_INTEGER_MAX 0x7FFFFFFF 59 #define INFNAME_SIZE 16 /* Length of interface name */ 60 #define RWMODE 0600 61 #define MAX_MSG_SIZE 1500 62 #define STRING_MAX_LEN 1024 63 inline constexpr char EVENT_GET_IPV4[] = "usual.event.wifi.dhcp.GET_IPV4"; 64 65 /* UDP port numbers for BOOTP */ 66 #define BOOTP_SERVER 67 67 #define BOOTP_CLIENT 68 68 69 /* BOOTP message type */ 70 #define BOOT_REQUEST 1 71 #define BOOT_REPLY 2 72 73 /* DHCP packet type */ 74 #define DHCP_DISCOVER 1 75 #define DHCP_OFFER 2 76 #define DHCP_REQUEST 3 77 #define DHCP_DECLINE 4 78 #define DHCP_ACK 5 79 #define DHCP_NAK 6 80 #define DHCP_RELEASE 7 81 #define DHCP_INFORM 8 82 #define DHCP_FORCERENEW 9 83 84 /* Sizes for DhcpPacket Fields */ 85 #define DHCP_HWADDR_LENGTH 16 86 #define DHCP_HOST_NAME_LENGTH 64 87 #define DHCP_BOOT_FILE_LENGTH 128 88 #define DHCP_OPT_SIZE 1780 89 #define DHCP_OPT_CODE_INDEX 0 90 #define DHCP_OPT_LEN_INDEX 1 91 #define DHCP_OPT_DATA_INDEX 2 92 #define DHCP_OPT_CODE_BYTES 1 93 #define DHCP_OPT_LEN_BYTES 1 94 #define DHCP_UINT8_BYTES 1 95 #define DHCP_UINT16_BYTES 2 96 #define DHCP_UINT32_BYTES 4 97 #define DHCP_UINT32_DOUBLE_BYTES 8 98 #define DHCP_UINT16_BITS 16 99 #define DHCP_REQ_CODE_NUM 12 100 #define OPTION_FIELD 0 101 #define FILE_FIELD 1 102 #define SNAME_FIELD 2 103 #define DHCP_DNS_FIRST 1 104 #define DHCP_DNS_SECOND 2 105 #define DHCP_APPEND_LEN 2 106 #define WIFI_DHCP_CACHE_ADD 1 107 #define WIFI_DHCP_CACHE_REMOVE 2 108 #ifdef OHOS_EUPDATER 109 inline constexpr char WORKDIR[] = "/tmp/service/el1/public/dhcp/"; 110 #else 111 inline constexpr char WORKDIR[] = "/data/service/el1/public/dhcp/"; 112 #endif // OHOS_EUPDATER 113 114 inline constexpr char DHCPC_NAME[] = "dhcp_client_service"; 115 inline constexpr char DHCPC_CONF[] = "dhcp_client_service.conf"; 116 inline constexpr char DHCPC_PID[] = "dhcp_client_service.pid"; 117 inline constexpr char DHCPC_VERSION[] = "1.0"; 118 inline constexpr char DHCPC_LEASE[] = "dhcp_client_service-%s.lease"; 119 #ifdef OHOS_EUPDATER 120 inline constexpr char DHCP_CACHE_FILE[] = "/tmp/service/el1/public/dhcp/dhcp_cache.conf"; 121 #else 122 inline constexpr char DHCP_CACHE_FILE[] = "/data/service/el1/public/dhcp/dhcp_cache.conf"; 123 #endif // OHOS_EUPDATER 124 /* netmanager ipv6 */ 125 constexpr int DHCP_IPV6_ENABLE = 1; 126 constexpr int DHCP_IPV6_DISENABLE = 0; 127 128 /* dhcp action mode */ 129 enum ActionMode { 130 ACTION_START_NEW = 0, 131 ACTION_START_OLD, 132 ACTION_RENEW_T1, 133 ACTION_RENEW_T2, 134 ACTION_RENEW_T3, 135 ACTION_INVALID 136 }; 137 138 /* dhcp signal mode */ 139 enum SigMode { 140 SIG_START = 0, 141 SIG_STOP, 142 SIG_RENEW, 143 SIG_INVALID 144 }; 145 146 /* dhcp state code */ 147 enum EnumDhcpStateCode { 148 DHCP_STATE_INIT = 0, 149 DHCP_STATE_SELECTING, 150 DHCP_STATE_REQUESTING, 151 DHCP_STATE_BOUND, 152 DHCP_STATE_RENEWING, 153 DHCP_STATE_REBINDING, 154 DHCP_STATE_INITREBOOT, 155 DHCP_STATE_RELEASED, 156 DHCP_STATE_RENEWED, 157 DHCP_STATE_FAST_ARP, 158 DHCP_STATE_SLOW_ARP, 159 DHCP_STATE_DECLINE, 160 }; 161 162 /* socket return code */ 163 enum EnumSocketErrCode { 164 SOCKET_OPT_SUCCESS = 0, 165 SOCKET_OPT_FAILED = -1, 166 SOCKET_OPT_ERROR = -2 167 }; 168 169 /* socket mode */ 170 enum EnumSocketMode { 171 SOCKET_MODE_INVALID = 0, 172 SOCKET_MODE_RAW = 1, 173 SOCKET_MODE_KERNEL = 2 174 }; 175 176 enum DHCP_OPTION_DATA_TYPE { 177 DHCP_OPTION_DATA_INVALID = 0, 178 DHCP_OPTION_DATA_U8, 179 DHCP_OPTION_DATA_U16, 180 DHCP_OPTION_DATA_S16, 181 DHCP_OPTION_DATA_U32, 182 DHCP_OPTION_DATA_S32, 183 DHCP_OPTION_DATA_IP, 184 DHCP_OPTION_DATA_IP_PAIR, 185 DHCP_OPTION_DATA_IP_LIST, 186 DHCP_OPTION_DATA_IP_STRING 187 }; 188 189 190 /* publish event code */ 191 typedef enum EnumPublishEventCode { 192 /* success */ 193 PUBLISH_CODE_SUCCESS = 0, 194 /* failed */ 195 PUBLISH_CODE_FAILED = -1, 196 /* timeout */ 197 PUBLISH_CODE_TIMEOUT = -2, 198 /* lease expired */ 199 PUBLISH_CODE_EXPIRED = -3, 200 /* dhcp offer report*/ 201 PUBLISH_DHCP_OFFER_REPORT = -4 202 } DhcpEventCode; 203 204 /* DhcpPacket Fields */ 205 struct DhcpPacket { 206 uint8_t op; 207 uint8_t htype; 208 uint8_t hlen; 209 uint8_t hops; 210 uint32_t xid; 211 uint16_t secs; 212 uint16_t flags; 213 uint32_t ciaddr; 214 uint32_t yiaddr; 215 uint32_t siaddr; 216 uint32_t giaddr; 217 uint8_t chaddr[DHCP_HWADDR_LENGTH]; 218 uint8_t sname[DHCP_HOST_NAME_LENGTH]; 219 uint8_t file[DHCP_BOOT_FILE_LENGTH]; 220 int32_t cookie; 221 uint8_t options[DHCP_OPT_SIZE]; 222 }; 223 224 struct UdpDhcpPacket { 225 struct iphdr ip; 226 struct udphdr udp; 227 struct DhcpPacket data; 228 }; 229 230 struct DhcpIpResult{ 231 int32_t code; /* get result code */ 232 std::string ifname; /* ifname */ 233 uint32_t uAddTime; /* addTime */ 234 uint32_t uOptLeasetime; /* dhcp option IP_ADDRESS_LEASE_TIME_OPTION */ 235 char strYiaddr[INET_ADDRSTRLEN]; /* your (client) IP */ 236 char strOptServerId[INET_ADDRSTRLEN]; /* dhcp option SERVER_IDENTIFIER_OPTION */ 237 char strOptSubnet[INET_ADDRSTRLEN]; /* dhcp option SUBNET_MASK_OPTION */ 238 char strOptDns1[INET_ADDRSTRLEN]; /* dhcp option DOMAIN_NAME_SERVER_OPTION */ 239 char strOptDns2[INET_ADDRSTRLEN]; /* dhcp option DOMAIN_NAME_SERVER_OPTION */ 240 char strOptRouter1[INET_ADDRSTRLEN]; /* dhcp option ROUTER_OPTION */ 241 char strOptRouter2[INET_ADDRSTRLEN]; /* dhcp option ROUTER_OPTION */ 242 char strOptVendor[DHCP_FILE_MAX_BYTES]; /* dhcp option VENDOR_SPECIFIC_INFO_OPTION */ 243 std::vector<std::string> dnsAddr; /* dhcp option MULTI DOMAIN_NAME_SERVER_OPTION */ 244 }; 245 246 enum DHCP_IP_TYPE { 247 DHCP_IP_TYPE_NONE = 0, 248 DHCP_IP_TYPE_ALL = 1, 249 DHCP_IP_TYPE_V4 = 2, 250 DHCP_IP_TYPE_V6 = 3 251 }; 252 253 typedef struct{ 254 int ifaceIndex; /* Index of the network interface used by the current process. */ 255 bool isIpv6; /* IPv6 address of the network interface used to get. */ 256 unsigned int ifaceIpv4; /* IPv4 of the network interface used by the current process. */ 257 unsigned int getMode; /* Current process obtaining IPv4 address mode. */ 258 unsigned char ifaceMac[MAC_ADDR_LEN]; /* Mac addr of the network interface used by the current process. */ 259 char ifaceName[INFNAME_SIZE]; /* Name of the network interface used by the current process. */ 260 char workDir[DIR_MAX_LEN]; /* Current process working directory. */ 261 char confFile[DIR_MAX_LEN]; /* Current process Configuration Directory. */ 262 char leaseFile[DIR_MAX_LEN]; 263 char resultFile[DIR_MAX_LEN]; /* Save the obtained IPv4 result. */ 264 char result6File[DIR_MAX_LEN]; 265 }DhcpClientCfg; 266 267 struct IpInfoCached { 268 std::string bssid; 269 std::string ssid; 270 int64_t absoluteLeasetime; 271 struct DhcpIpResult ipResult; 272 }; 273 274 /* timer type */ 275 enum TimerType { 276 TIMER_GET_IP = 0, 277 TIMER_RENEW_DELAY, 278 TIMER_REBIND_DELAY, 279 TIMER_REMAINING_DELAY, 280 TIMER_SLOW_ARP, 281 }; 282 #endif