• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Wifi {
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 #ifdef OHOS_EUPDATER
73 const std::string DHCP_WORK_DIR("/tmp/service/el1/public/dhcp/");
74 #else
75 const std::string DHCP_WORK_DIR("/data/service/el1/public/dhcp/");
76 #endif // OHOS_EUPDATER
77 
78 const std::string DHCP_CLIENT_PID_FILETYPE(".pid");
79 const std::string DHCP_RESULT_FILETYPE(".result");
80 #ifdef OHOS_ARCH_LITE
81 const std::string DHCP_CLIENT_FILE("/bin/dhcp_client_service");
82 const std::string DHCP_SERVER_FILE("/bin/dhcp_server");
83 #else
84 const std::string DHCP_CLIENT_FILE("/system/bin/dhcp_client_service");
85 const std::string DHCP_SERVER_FILE("/system/bin/dhcp_server");
86 #endif
87 const std::string DHCP_SERVER_CONFIG_FILE(DHCP_WORK_DIR + "etc/dhcpd.conf");
88 const std::string DHCP_SERVER_CONFIG_DIR(DHCP_WORK_DIR + "etc/");
89 const std::string DHCP_SERVER_LEASES_FILE(DHCP_WORK_DIR + "dhcpd_lease.lease");
90 const std::string DHCP_SERVER_CFG_IPV4("#ipv4");
91 const std::string DHCP_SERVER_CFG_IPV6("#ipv6");
92 const std::string COMMON_EVENT_DHCP_GET_IPV4 = "usual.event.wifi.dhcp.GET_IPV4";
93 const std::string IP_V4_MASK("255.255.255.0");
94 const std::string IP_V4_DEFAULT("192.168.62.2");
95 
96 typedef enum EnumErrCode {
97     /* success */
98     DHCP_OPT_SUCCESS = 0,
99     /* failed */
100     DHCP_OPT_FAILED,
101     /* null pointer */
102     DHCP_OPT_NULL,
103     /* timeout */
104     DHCP_OPT_TIMEOUT,
105     /* error */
106     DHCP_OPT_ERROR,
107 } DhcpErrCode;
108 
109 /* publish event code */
110 typedef enum EnumPublishEventCode {
111     /* success */
112     PUBLISH_CODE_SUCCESS = 0,
113     /* failed */
114     PUBLISH_CODE_FAILED = -1
115 } DhcpEventCode;
116 
117 typedef enum EnumServiceStatus {
118     SERVICE_STATUS_INVALID  = 0,
119     SERVICE_STATUS_START    = 1,
120     SERVICE_STATUS_STOP     = 2
121 } DhcpServiceStatus;
122 
123 struct DhcpResult {
124     int iptype;             /* 0-ipv4,1-ipv6 */
125     bool isOptSuc;          /* get result */
126     std::string strYourCli; /* your (client) IP */
127     std::string strServer;  /* dhcp server IP */
128     std::string strSubnet;  /* your (client) subnet mask */
129     std::string strDns1;    /* your (client) DNS server1 */
130     std::string strDns2;    /* your (client) DNS server2 */
131     std::string strRouter1; /* your (client) router1 */
132     std::string strRouter2; /* your (client) router2 */
133     std::string strVendor;  /* your (client) vendor */
134     uint32_t uLeaseTime;    /* your (client) IP lease time (s) */
135     uint32_t uAddTime;      /* dhcp result add time */
136     uint32_t uGetTime;      /* dhcp result get time */
137 
DhcpResultDhcpResult138     DhcpResult()
139     {
140         iptype      = -1;
141         isOptSuc    = false;
142         strYourCli  = "";
143         strServer   = "";
144         strSubnet   = "";
145         strDns1     = "";
146         strDns2     = "";
147         strRouter1  = "";
148         strRouter2  = "";
149         strVendor   = "";
150         uLeaseTime  = 0;
151         uAddTime    = 0;
152         uGetTime    = 0;
153     }
154 };
155 
156 struct DhcpPacketResult {
157     char strYiaddr[INET_ADDRSTRLEN];        /* your (client) IP */
158     char strOptServerId[INET_ADDRSTRLEN];   /* dhcp option DHO_SERVERID */
159     char strOptSubnet[INET_ADDRSTRLEN];     /* dhcp option DHO_SUBNETMASK */
160     char strOptDns1[INET_ADDRSTRLEN];       /* dhcp option DHO_DNSSERVER */
161     char strOptDns2[INET_ADDRSTRLEN];       /* dhcp option DHO_DNSSERVER */
162     char strOptRouter1[INET_ADDRSTRLEN];    /* dhcp option DHO_ROUTER */
163     char strOptRouter2[INET_ADDRSTRLEN];    /* dhcp option DHO_ROUTER */
164     char strOptVendor[DHCP_FILE_MAX_BYTES]; /* dhcp option DHO_VENDOR */
165     uint32_t uOptLeasetime;                 /* dhcp option DHO_LEASETIME */
166     uint32_t uAddTime;                      /* dhcp result add time */
167 };
168 
169 struct DhcpServiceInfo {
170     bool enableIPv6;        /* true:ipv4 and ipv6,false:ipv4 */
171     int clientRunStatus;    /* dhcp client service status */
172     pid_t clientProPid;     /* dhcp client process pid */
173     std::string serverIp;   /* dhcp server IP */
174 
DhcpServiceInfoDhcpServiceInfo175     DhcpServiceInfo()
176     {
177         enableIPv6 = true;
178         clientRunStatus = -1;
179         clientProPid = 0;
180         serverIp = "";
181     }
182 };
183 
184 struct DhcpServerInfo {
185     pid_t proPid;           /* dhcp server process id */
186     bool normalExit;        /* dhcp server process normal exit */
187     bool exitSig;           /* dhcp server process exit signal */
188 
DhcpServerInfoDhcpServerInfo189     DhcpServerInfo()
190     {
191         proPid = 0;
192         normalExit = false;
193         exitSig = false;
194     }
195 };
196 
197 struct DhcpRange {
198     int iptype;             /* 0-ipv4,1-ipv6 */
199     int leaseHours;         /* lease hours */
200     std::string strTagName; /* dhcp-range tag name */
201     std::string strStartip; /* dhcp-range start ip */
202     std::string strEndip;   /* dhcp-range end ip */
203     std::string strSubnet;  /* dhcp-range subnet */
204 
DhcpRangeDhcpRange205     DhcpRange()
206     {
207         iptype = -1;
208         leaseHours = LEASETIME_DEFAULT;
209         strTagName = "";
210         strStartip = "";
211         strEndip = "";
212         strSubnet = "";
213     }
214 };
215 }  // namespace Wifi
216 }  // namespace OHOS
217 #endif /* OHOS_DHCP_DEFINE_H */
218