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 namespace OHOS {
25 namespace DHCP {
26 const int ETH_MAC_ADDR_INDEX_0  = 0;
27 const int ETH_MAC_ADDR_INDEX_1  = 1;
28 const int ETH_MAC_ADDR_INDEX_2  = 2;
29 const int ETH_MAC_ADDR_INDEX_3  = 3;
30 const int ETH_MAC_ADDR_INDEX_4  = 4;
31 const int ETH_MAC_ADDR_INDEX_5  = 5;
32 const int ETH_MAC_ADDR_LEN      = 6;
33 const int ETH_MAC_ADDR_CHAR_NUM = 3;
34 const int IP_SIZE               = 18;
35 const int LEASETIME_DEFAULT     = 6;
36 const int ONE_HOURS_SEC         = 3600;
37 const int RECEIVER_TIMEOUT      = 6;
38 const int EVENT_DATA_NUM        = 11;
39 const int IPV6_EVENT_DATA_NUM   = 9;
40 const int DHCP_NUM_ZERO         = 0;
41 const int DHCP_NUM_ONE          = 1;
42 const int DHCP_NUM_TWO          = 2;
43 const int DHCP_NUM_THREE        = 3;
44 const int DHCP_NUM_FOUR         = 4;
45 const int DHCP_NUM_FIVE         = 5;
46 const int DHCP_NUM_SIX          = 6;
47 const int DHCP_NUM_SEVEN        = 7;
48 const int DHCP_NUM_EIGHT        = 8;
49 const int DHCP_NUM_NINE         = 9;
50 const int DHCP_NUM_TEN          = 10;
51 const int DHCP_FILE_MAX_BYTES   = 128;
52 const int FILE_LINE_MAX_SIZE    = 1024;
53 const int DHCP_SER_ARGSNUM      = 6;
54 const int DHCP_CLI_ARGSNUM      = 5;
55 const int SLEEP_TIME_200_MS     = 200 * 1000;
56 const int SLEEP_TIME_500_MS     = 500 * 1000;
57 const int PID_MAX_LEN           = 16;
58 const int PARAM_MAX_SIZE        = 40;
59 const int DEFAULT_UMASK         = 027;
60 const int DIR_MAX_LEN           = 256;
61 const int DIR_DEFAULT_MODE      = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH;
62 const int DHCP_IPV4_GETTED  = 1;
63 const int DHCP_IPV6_GETTED  = 2;
64 const int DHCP_IPALL_GETED  = DHCP_IPV4_GETTED | DHCP_IPV6_GETTED;
65 const std::string IP4_SEPARATOR(".");
66 const std::string IP6_SEPARATOR(":");
67 const std::string INVALID_STRING("*");
68 const std::string EVENT_DATA_DELIMITER(",");
69 const std::string EVENT_DATA_IPV4("ipv4");
70 const std::string EVENT_DATA_IPV6("ipv6");
71 
72 #define DHCP_CLIENT_ABILITY_ID  1126
73 #define DHCP_SERVER_ABILITY_ID  1127
74 
75 #ifdef OHOS_EUPDATER
76 const std::string DHCP_WORK_DIR("/tmp/service/el1/public/dhcp/");
77 #else
78 const std::string DHCP_WORK_DIR("/data/service/el1/public/dhcp/");
79 #endif // OHOS_EUPDATER
80 
81 const std::string DHCP_CLIENT_PID_FILETYPE(".pid");
82 const std::string DHCP_RESULT_FILETYPE(".result");
83 #ifdef OHOS_ARCH_LITE
84 const std::string DHCP_CLIENT_FILE("/bin/dhcp_client_service");
85 const std::string DHCP_SERVER_FILE("/bin/dhcp_server");
86 #else
87 const std::string DHCP_CLIENT_FILE("/system/bin/dhcp_client_service");
88 const std::string DHCP_SERVER_FILE("/system/bin/dhcp_server");
89 #endif
90 const std::string DHCP_SERVER_CONFIG_FILE(DHCP_WORK_DIR + "etc/dhcpd.conf");
91 const std::string DHCP_SERVER_CONFIG_DIR(DHCP_WORK_DIR + "etc/");
92 const std::string DHCP_SERVER_LEASES_FILE(DHCP_WORK_DIR + "dhcpd_lease.lease");
93 const std::string DHCP_SERVER_CFG_IPV4("#ipv4");
94 const std::string DHCP_SERVER_CFG_IPV6("#ipv6");
95 const std::string COMMON_EVENT_DHCP_GET_IPV4 = "usual.event.wifi.dhcp.GET_IPV4";
96 const std::string IP_V4_MASK("255.255.255.0");
97 const std::string IP_V4_DEFAULT("192.168.62.1");
98 
99 typedef enum EnumErrCode {
100     /* success */
101     DHCP_OPT_SUCCESS = 0,
102     /* failed */
103     DHCP_OPT_FAILED,
104     /* null pointer */
105     DHCP_OPT_NULL,
106     /* get ip timeout */
107     DHCP_OPT_TIMEOUT,
108     /* renew failed */
109     DHCP_OPT_RENEW_FAILED,
110     /* renew timeout */
111     DHCP_OPT_RENEW_TIMEOUT,
112     /* lease expired */
113     DHCP_OPT_LEASE_EXPIRED,
114     /* dhcp offer */
115     DHCP_OPT_OFFER_REPORT,
116     /* error */
117     DHCP_OPT_ERROR,
118 } DhcpErrCode;
119 
120 enum DhcpServerState {
121     DHCP_SERVER_OFF = 0,
122     DHCP_SERVER_ON,
123 };
124 
125 typedef enum EnumServiceStatus {
126     SERVICE_STATUS_INVALID  = 0,
127     SERVICE_STATUS_START    = 1,
128     SERVICE_STATUS_STOP     = 2
129 } DhcpServiceStatus;
130 
131 struct DhcpResult {
132     bool isOptSuc;          /* get result */
133     int iptype;             /* 0-ipv4,1-ipv6 */
134     std::string strYourCli; /* your (client) IP */
135     std::string strServer;  /* dhcp server IP */
136     std::string strSubnet;  /* your (client) subnet mask */
137     std::string strDns1;    /* your (client) DNS server1 */
138     std::string strDns2;    /* your (client) DNS server2 */
139     std::string strRouter1; /* your (client) router1 */
140     std::string strRouter2; /* your (client) router2 */
141     std::string strVendor;  /* your (client) vendor */
142     std::string strLinkIpv6Addr;  /* your (client) link ipv6 addr */
143     std::string strRandIpv6Addr;  /* your (client) rand ipv6 addr */
144     std::string strLocalAddr1;  /* your (client) unique local ipv6 addr */
145     std::string strLocalAddr2;  /* your (client) unique local ipv6 addr */
146     uint32_t uLeaseTime;    /* your (client) IP lease time (s) */
147     uint32_t uAddTime;      /* dhcp result add time */
148     uint32_t uGetTime;      /* dhcp result get time */
149     std::vector<std::string> vectorDnsAddr; /* your (client) multi dns server */
150 
DhcpResultDhcpResult151     DhcpResult()
152     {
153         iptype      = -1;
154         isOptSuc    = false;
155         strYourCli  = "";
156         strServer   = "";
157         strSubnet   = "";
158         strDns1     = "";
159         strDns2     = "";
160         strRouter1  = "";
161         strRouter2  = "";
162         strVendor   = "";
163         strLinkIpv6Addr = "";
164         strRandIpv6Addr = "";
165         strLocalAddr1   = "";
166         strLocalAddr2   = "";
167         uLeaseTime  = 0;
168         uAddTime    = 0;
169         uGetTime    = 0;
170         vectorDnsAddr.clear();
171     }
172 };
173 
174 struct DhcpPacketResult {
175     char strYiaddr[INET_ADDRSTRLEN];        /* your (client) IP */
176     char strOptServerId[INET_ADDRSTRLEN];   /* dhcp option DHO_SERVERID */
177     char strOptSubnet[INET_ADDRSTRLEN];     /* dhcp option DHO_SUBNETMASK */
178     char strOptDns1[INET_ADDRSTRLEN];       /* dhcp option DHO_DNSSERVER */
179     char strOptDns2[INET_ADDRSTRLEN];       /* dhcp option DHO_DNSSERVER */
180     char strOptRouter1[INET_ADDRSTRLEN];    /* dhcp option DHO_ROUTER */
181     char strOptRouter2[INET_ADDRSTRLEN];    /* dhcp option DHO_ROUTER */
182     char strOptVendor[DHCP_FILE_MAX_BYTES]; /* dhcp option DHO_VENDOR */
183     uint32_t uOptLeasetime;                 /* dhcp option DHO_LEASETIME */
184     uint32_t uAddTime;                      /* dhcp result add time */
185 };
186 
187 struct DhcpServiceInfo {
188     bool enableIPv6;        /* true:ipv4 and ipv6,false:ipv4 */
189     int clientRunStatus;    /* dhcp client service status */
190     pid_t clientProPid;     /* dhcp client process pid */
191     std::string serverIp;   /* dhcp server IP */
192 
DhcpServiceInfoDhcpServiceInfo193     DhcpServiceInfo()
194     {
195         enableIPv6 = true;
196         clientRunStatus = -1;
197         clientProPid = 0;
198         serverIp = "";
199     }
200 };
201 
202 struct DhcpServerInfo {
203     pid_t proPid;           /* dhcp server process id */
204     bool normalExit;        /* dhcp server process normal exit */
205     bool exitSig;           /* dhcp server process exit signal */
206 
DhcpServerInfoDhcpServerInfo207     DhcpServerInfo()
208     {
209         proPid = 0;
210         normalExit = false;
211         exitSig = false;
212     }
213 };
214 
215 struct DhcpRange {
216     int iptype;             /* 0-ipv4,1-ipv6 */
217     int leaseHours;         /* lease hours */
218     std::string strTagName; /* dhcp-range tag name */
219     std::string strStartip; /* dhcp-range start ip */
220     std::string strEndip;   /* dhcp-range end ip */
221     std::string strSubnet;  /* dhcp-range subnet */
222 
DhcpRangeDhcpRange223     DhcpRange()
224     {
225         iptype = -1;
226         leaseHours = LEASETIME_DEFAULT;
227         strTagName = "";
228         strStartip = "";
229         strEndip = "";
230         strSubnet = "";
231     }
232 };
233 
234 struct RouterConfig {
235     std::string bssid;
236     bool prohibitUseCacheIp;
237 };
238 }  // namespace DHCP
239 }  // namespace OHOS
240 #endif /* OHOS_DHCP_DEFINE_H */
241