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_DHCP_DEFINE_H 17 #define OHOS_DHCP_DEFINE_H 18 19 #include <string> 20 #include <cstdint> 21 #include <netinet/ip.h> 22 #include <sys/stat.h> 23 24 const int ETH_MAC_ADDR_INDEX_0 = 0; 25 const int ETH_MAC_ADDR_INDEX_1 = 1; 26 const int ETH_MAC_ADDR_INDEX_2 = 2; 27 const int ETH_MAC_ADDR_INDEX_3 = 3; 28 const int ETH_MAC_ADDR_INDEX_4 = 4; 29 const int ETH_MAC_ADDR_INDEX_5 = 5; 30 const int ETH_MAC_ADDR_LEN = 6; 31 const int ETH_MAC_ADDR_CHAR_NUM = 3; 32 const int IP_SIZE = 18; 33 const int LEASETIME_DEFAULT_SERVER = 6; 34 const int ONE_HOURS_SEC = 3600; 35 const int RECEIVER_TIMEOUT = 6; 36 const int EVENT_DATA_NUM = 11; 37 const int IPV6_EVENT_DATA_NUM = 9; 38 const int DHCP_NUM_ZERO = 0; 39 const int DHCP_NUM_ONE = 1; 40 const int DHCP_NUM_TWO = 2; 41 const int DHCP_NUM_THREE = 3; 42 const int DHCP_NUM_FOUR = 4; 43 const int DHCP_NUM_FIVE = 5; 44 const int DHCP_NUM_SIX = 6; 45 const int DHCP_NUM_SEVEN = 7; 46 const int DHCP_NUM_EIGHT = 8; 47 const int DHCP_NUM_NINE = 9; 48 const int DHCP_NUM_TEN = 10; 49 const int DHCP_FILE_MAX_BYTES = 128; 50 const int FILE_LINE_MAX_SIZE = 1024; 51 const int DHCP_SER_ARGSNUM = 6; 52 const int DHCP_CLI_ARGSNUM = 5; 53 const int SLEEP_TIME_200_MS = 200 * 1000; 54 const int SLEEP_TIME_500_MS = 500 * 1000; 55 const int PID_MAX_LEN = 16; 56 const int PARAM_MAX_SIZE = 40; 57 const int DEFAULT_UMASK = 027; 58 const int DIR_MAX_LEN = 256; 59 const int DIR_DEFAULT_MODE = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; 60 const int DHCP_IPV4_GETTED = 1; 61 const int DHCP_IPV6_GETTED = 2; 62 const int DHCP_IPALL_GETED = DHCP_IPV4_GETTED | DHCP_IPV6_GETTED; 63 inline const std::string IP4_SEPARATOR("."); 64 inline const std::string IP6_SEPARATOR(":"); 65 inline const std::string INVALID_STRING("*"); 66 inline const std::string EVENT_DATA_DELIMITER(","); 67 inline const std::string EVENT_DATA_IPV4("ipv4"); 68 inline const std::string EVENT_DATA_IPV6("ipv6"); 69 70 #define DHCP_CLIENT_ABILITY_ID 1126 71 #define DHCP_SERVER_ABILITY_ID 1127 72 73 #ifdef OHOS_EUPDATER 74 inline const std::string DHCP_WORK_DIR("/tmp/service/el1/public/dhcp/"); 75 #else 76 inline const std::string DHCP_WORK_DIR("/data/service/el1/public/dhcp/"); 77 #endif // OHOS_EUPDATER 78 79 inline const std::string DHCP_CLIENT_PID_FILETYPE(".pid"); 80 inline const std::string DHCP_RESULT_FILETYPE(".result"); 81 #ifdef OHOS_ARCH_LITE 82 inline const std::string DHCP_CLIENT_FILE("/bin/dhcp_client_service"); 83 inline const std::string DHCP_SERVER_FILE("/bin/dhcp_server"); 84 #else 85 inline const std::string DHCP_CLIENT_FILE("/system/bin/dhcp_client_service"); 86 inline const std::string DHCP_SERVER_FILE("/system/bin/dhcp_server"); 87 #endif 88 inline const std::string DHCP_SERVER_CONFIG_FILE(DHCP_WORK_DIR + "etc/dhcpd.conf"); 89 inline const std::string DHCP_SERVER_CONFIG_DIR(DHCP_WORK_DIR + "etc/"); 90 inline const std::string DHCP_SERVER_LEASES_FILE(DHCP_WORK_DIR + "dhcpd_lease.lease"); 91 inline const std::string DHCP_SERVER_CFG_IPV4("#ipv4"); 92 inline const std::string DHCP_SERVER_CFG_IPV6("#ipv6"); 93 inline const std::string COMMON_EVENT_DHCP_GET_IPV4 = "usual.event.wifi.dhcp.GET_IPV4"; 94 95 // See: RFC 2132 DHCP Options and BOOTP Vendor Extensions March 1997 96 enum DhcpOptions { 97 /* 98 * 3. RFC 1497 Vendor Extensions 99 * This section lists the vendor extensions as defined in RFC 1497. 100 * They are defined here for completeness. 101 */ 102 PAD_OPTION = 0, 103 END_OPTION = 255, 104 105 SUBNET_MASK_OPTION = 1, 106 TIME_OFFSET_OPTION = 2, 107 ROUTER_OPTION = 3, 108 TIME_SERVER_OPTION = 4, 109 NAME_SERVER_OPTION = 5, 110 DOMAIN_NAME_SERVER_OPTION = 6, 111 LOG_SERVER_OPTION = 7, 112 COOKIE_SERVER_OPTION = 8, 113 LPR_SERVER_OPTION = 9, 114 IMPRESS_SERVER_OPTION = 10, 115 RESOURCE_LOCATION_SERVER_OPTION = 11, 116 HOST_NAME_OPTION = 12, 117 BOOT_FILE_SIZE_OPTION = 13, 118 MERIT_DUMP_FILE_OPTION = 14, 119 DOMAIN_NAME_OPTION = 15, 120 SWAP_SERVER_OPTION = 16, 121 ROOT_PATH_OPTION = 17, 122 EXTENSIONS_PATH_OPTION = 18, 123 124 /* 125 * IP Layer Parameters per Host 126 * This section details the options that affect the operation of the IP layer on a per-host basis. 127 */ 128 IP_FORWARDING_OPTION = 19, 129 NON_LOCAL_SOURCE_ROUTING_OPTION = 20, 130 POLICY_FILTER_OPTION = 21, 131 MAXIMUM_DATAGRAM_REASSEMBLY_SIZE_OPTION = 22, 132 DEFAULT_IP_TIME_TO_LIVE_OPTION = 23, 133 PATH_MTU_AGING_TIMEOUT_OPTION = 24, 134 PATH_MTU_PLATEAU_TABLE_OPTION = 25, 135 136 /* 137 * 5. IP Layer Parameters per Interface 138 * This section details the options that affect the operation of the IP layer on a per-interface basis. It is 139 * expected that a client can issue multiple requests, one per interface, in order to configure interfaces with 140 * their specific parameters. 141 */ 142 INTERFACE_MTU_OPTION = 26, 143 ALL_SUBNETS_ARE_LOCAL_OPTION = 27, 144 BROADCAST_ADDRESS_OPTION = 28, 145 PERFORM_MASK_DISCOVERY_OPTION = 29, 146 MASK_SUPPLIER_OPTION = 30, 147 PERFORM_ROUTER_DISCOVERY_OPTION = 31, 148 ROUTER_SOLICITATION_ADDRESS_OPTION = 32, 149 STATIC_ROUTE_OPTION = 33, 150 151 /* 152 * 6. Link Layer Parameters per Interface 153 * This section lists the options that affect the operation of the data link layer on a per-interface basis. 154 */ 155 TRAILER_ENCAPSULATION_OPTION = 34, // 6.1. Trailer Encapsulation Option 156 ARP_CACHE_TIMEOUT_OPTION = 35, // 6.2. ARP Cache Timeout Option 157 ETHERNET_ENCAPSULATION_OPTION = 36, // 6.3. Ethernet Encapsulation Option 158 159 /* 160 * 7. TCP Parameters 161 * This section lists the options that affect the operation of the TCP layer on a per-interface basis. 162 */ 163 TCP_DEFAULT_TTL_OPTION = 37, // 7.1. TCP Default TTL Option 164 TCP_KEEPALIVE_INTERVAL_OPTION = 38, // 7.2. TCP Keepalive Interval Option 165 TCP_KEEPALIVE_GARBAGE_OPTION = 39, // 7.3. TCP Keepalive Garbage Option 166 167 /* 168 * 8. Application and Service Parameters 169 * This section details some miscellaneous options used to configure miscellaneous applications and services. 170 */ 171 NETWORK_INFO_SERVICE_DOMAIN_OPTION = 40, // 8.1. Network Information Service Domain Option 172 NETWORK_INFO_SERVERS_OPTION = 41, // 8.2. Network Information Servers Option 173 NETWORK_TIME_PROTOCOL_SERVERS_OPTION = 42, // 8.3. Network Time Protocol Servers Option 174 VENDOR_SPECIFIC_INFO_OPTION = 43, // 8.4. Vendor Specific Information 175 NETBIOS_OVER_IP_NAME_SERVER_OPTION = 44, // 8.5. NetBIOS over TCP/IP Name Server Option 176 NETBIOS_OVER_IP_DATAGRAM_DIST_SERVER_OPTION = 45, // 8.6. NetBIOS over TCP/IP Datagram Distribution Server Option 177 NETBIOS_OVER_IP_NODE_TYPE_OPTION = 46, // 8.7. NetBIOS over TCP/IP Node Type Option 178 NETBIOS_OVER_IP_SCOPE_OPTION = 47, // 8.8. NetBIOS over TCP/IP Scope Option 179 X_WINDOW_SYSTEM_FONT_SERVER_OPTION = 48, // 8.9. X Window System Font Server Option 180 X_WINDOW_SYSTEM_DISPLAY_MANAGER_OPTION = 49, // 8.10. X Window System Display Manager Option 181 NETWORK_INFO_SERVICE_PLUS_DOMAIN_OPTION = 64, // 8.11. Network Information Service+ Domain Option 182 NETWORK_INFO_SERVICE_PLUS_SERVERS_OPTION = 65, // 8.12. Network Information Service+ Servers Option 183 MOBILE_IP_HOME_AGENT_OPTION = 68, // 8.13. Mobile IP Home Agent option 184 SMTP_SERVER_OPTION = 69, // 8.14. Simple Mail Transport Protocol (SMTP) Server Option 185 POP3_SERVER_OPTION = 70, // 8.15. Post Office Protocol (POP3) Server Option 186 NNTP_SERVER_OPTION = 71, // 8.16. Network News Transport Protocol (NNTP) Server Option 187 DEFAULT_WEB_SERVER_OPTION = 72, // 8.17. Default World Wide Web (WWW) Server Option 188 DEFAULT_FINGER_SERVER_OPTION = 73, // 8.18. Default Finger Server Option 189 DEFAULT_IRC_SERVER_OPTION = 74, // 8.19. Default Internet Relay Chat (IRC) Server Option 190 STREETTALK_SERVER_OPTION = 75, // 8.20. StreetTalk Server Option 191 STDA_SERVER_OPTION = 76, // 8.21. StreetTalk Directory Assistance (STDA) Server Option 192 /* 193 * 9. DHCP Extensions 194 * This section details the options that are specific to DHCP. 195 */ 196 REQUESTED_IP_ADDRESS_OPTION = 50, 197 IP_ADDRESS_LEASE_TIME_OPTION = 51, 198 OPTION_OVERLOAD_OPTION = 52, 199 TFTP_SERVER_NAME_OPTION = 66, 200 BOOTFILE_NAME_OPTION = 67, 201 DHCP_MESSAGE_TYPE_OPTION = 53, 202 SERVER_IDENTIFIER_OPTION = 54, 203 PARAMETER_REQUEST_LIST_OPTION = 55, 204 MESSAGE_OPTION = 56, 205 MAXIMUM_DHCP_MESSAGE_SIZE_OPTION = 57, 206 RENEWAL_TIME_VALUE_OPTION = 58, 207 REBINDING_TIME_VALUE_OPTION = 59, 208 VENDOR_CLASS_IDENTIFIER_OPTION = 60, 209 CLIENT_IDENTIFIER_OPTION = 61, 210 USER_CLASS_OPTION = 77, 211 RAPID_COMMIT_OPTION = 80, 212 IPV6_ONLY_PREFERRED_OPTION = 108, 213 CAPTIVE_PORTAL_OPTION = 114, 214 /* RFC 6704 */ 215 FORCERENEW_NONCE_OPTION = 145, /* Forcerenew Nonce Authentication */ 216 }; 217 typedef enum EnumErrCode { 218 /* success */ 219 DHCP_OPT_SUCCESS = 0, 220 /* failed */ 221 DHCP_OPT_FAILED, 222 /* null pointer */ 223 DHCP_OPT_NULL, 224 /* get ip timeout */ 225 DHCP_OPT_TIMEOUT, 226 /* renew failed */ 227 DHCP_OPT_RENEW_FAILED, 228 /* renew timeout */ 229 DHCP_OPT_RENEW_TIMEOUT, 230 /* lease expired */ 231 DHCP_OPT_LEASE_EXPIRED, 232 /* dhcp offer */ 233 DHCP_OPT_OFFER_REPORT, 234 /* error */ 235 DHCP_OPT_ERROR, 236 /* not support */ 237 DHCP_OPT_NOT_SUPPORTED, 238 } DhcpErrCode; 239 240 enum DhcpServerStatus { 241 DHCP_SERVER_OFF = 0, 242 DHCP_SERVER_ON, 243 }; 244 namespace OHOS { 245 namespace DHCP { 246 inline const std::string IP_V4_MASK("255.255.255.0"); 247 inline const std::string IP_V4_DEFAULT("192.168.62.1"); 248 struct DhcpResult { 249 bool isOptSuc; /* get result */ 250 int iptype; /* 0-ipv4,1-ipv6 */ 251 std::string strYourCli; /* your (client) IP */ 252 std::string strServer; /* dhcp server IP */ 253 std::string strSubnet; /* your (client) subnet mask */ 254 std::string strDns1; /* your (client) DNS server1 */ 255 std::string strDns2; /* your (client) DNS server2 */ 256 std::string strRouter1; /* your (client) router1 */ 257 std::string strRouter2; /* your (client) router2 */ 258 std::string strVendor; /* your (client) vendor */ 259 std::string strLinkIpv6Addr; /* your (client) link ipv6 addr */ 260 std::string strRandIpv6Addr; /* your (client) rand ipv6 addr */ 261 std::string strLocalAddr1; /* your (client) unique local ipv6 addr */ 262 std::string strLocalAddr2; /* your (client) unique local ipv6 addr */ 263 uint32_t uLeaseTime; /* your (client) IP lease time (s) */ 264 uint32_t uAddTime; /* dhcp result add time */ 265 uint32_t uGetTime; /* dhcp result get time */ 266 std::vector<std::string> vectorDnsAddr; /* your (client) multi dns server */ 267 DhcpResultDhcpResult268 DhcpResult() 269 { 270 iptype = -1; 271 isOptSuc = false; 272 strYourCli = ""; 273 strServer = ""; 274 strSubnet = ""; 275 strDns1 = ""; 276 strDns2 = ""; 277 strRouter1 = ""; 278 strRouter2 = ""; 279 strVendor = ""; 280 strLinkIpv6Addr = ""; 281 strRandIpv6Addr = ""; 282 strLocalAddr1 = ""; 283 strLocalAddr2 = ""; 284 uLeaseTime = 0; 285 uAddTime = 0; 286 uGetTime = 0; 287 vectorDnsAddr.clear(); 288 } 289 }; 290 291 struct DhcpPacketResult { 292 char strYiaddr[INET_ADDRSTRLEN]; /* your (client) IP */ 293 char strOptServerId[INET_ADDRSTRLEN]; /* dhcp option DHO_SERVERID */ 294 char strOptSubnet[INET_ADDRSTRLEN]; /* dhcp option DHO_SUBNETMASK */ 295 char strOptDns1[INET_ADDRSTRLEN]; /* dhcp option DHO_DNSSERVER */ 296 char strOptDns2[INET_ADDRSTRLEN]; /* dhcp option DHO_DNSSERVER */ 297 char strOptRouter1[INET_ADDRSTRLEN]; /* dhcp option DHO_ROUTER */ 298 char strOptRouter2[INET_ADDRSTRLEN]; /* dhcp option DHO_ROUTER */ 299 char strOptVendor[DHCP_FILE_MAX_BYTES]; /* dhcp option DHO_VENDOR */ 300 uint32_t uOptLeasetime; /* dhcp option DHO_LEASETIME */ 301 uint32_t uAddTime; /* dhcp result add time */ 302 }; 303 304 struct DhcpServiceInfo { 305 bool enableIPv6; /* true:ipv4 and ipv6,false:ipv4 */ 306 int clientRunStatus; /* dhcp client service status */ 307 pid_t clientProPid; /* dhcp client process pid */ 308 std::string serverIp; /* dhcp server IP */ 309 DhcpServiceInfoDhcpServiceInfo310 DhcpServiceInfo() 311 { 312 enableIPv6 = true; 313 clientRunStatus = -1; 314 clientProPid = 0; 315 serverIp = ""; 316 } 317 }; 318 319 struct DhcpServerInfo { 320 pid_t proPid; /* dhcp server process id */ 321 bool normalExit; /* dhcp server process normal exit */ 322 bool exitSig; /* dhcp server process exit signal */ 323 DhcpServerInfoDhcpServerInfo324 DhcpServerInfo() 325 { 326 proPid = 0; 327 normalExit = false; 328 exitSig = false; 329 } 330 }; 331 332 struct DhcpRange { 333 int iptype; /* 0-ipv4,1-ipv6 */ 334 int leaseHours; /* lease hours */ 335 std::string strTagName; /* dhcp-range tag name */ 336 std::string strStartip; /* dhcp-range start ip */ 337 std::string strEndip; /* dhcp-range end ip */ 338 std::string strSubnet; /* dhcp-range subnet */ 339 DhcpRangeDhcpRange340 DhcpRange() 341 { 342 iptype = -1; 343 leaseHours = LEASETIME_DEFAULT_SERVER; 344 strTagName = ""; 345 strStartip = ""; 346 strEndip = ""; 347 strSubnet = ""; 348 } 349 }; 350 351 struct RouterConfig { 352 std::string ifname; 353 std::string bssid; 354 bool prohibitUseCacheIp { false }; 355 bool bIpv6 { true }; 356 bool bSpecificNetwork { false }; 357 bool isStaticIpv4 { false }; 358 bool bIpv4 { true }; 359 }; 360 361 struct IpCacheInfo { 362 std::string ssid; 363 std::string bssid; 364 }; 365 } // namespace DHCP 366 } // namespace OHOS 367 #endif /* OHOS_DHCP_DEFINE_H */ 368