4 #ifndef __LWIP_DHCP_H__
5 #define __LWIP_DHCP_H__
9 #if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
11 #include "lwip/netif.h"
18 /** period (in seconds) of the application calling dhcp_coarse_tmr() */
19 #define DHCP_COARSE_TIMER_SECS 60
20 /** period (in milliseconds) of the application calling dhcp_coarse_tmr() */
21 #define DHCP_COARSE_TIMER_MSECS (DHCP_COARSE_TIMER_SECS * 1000UL)
22 /** period (in milliseconds) of the application calling dhcp_fine_tmr() */
23 #define DHCP_FINE_TIMER_MSECS 500
27 /** transaction identifier of last sent request */
29 /** our connection to the DHCP server */
32 struct dhcp_msg *msg_in;
33 /** incoming msg options */
35 /** ingoing msg options length */
37 /** current DHCP state machine state */
39 /** retries of current request */
42 struct pbuf *p_out; /* pbuf of outcoming msg */
43 struct dhcp_msg *msg_out; /* outgoing msg */
44 u16_t options_out_len; /* outgoing msg options length */
45 u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
46 u16_t t1_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
47 u16_t t2_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
48 struct ip_addr server_ip_addr; /* dhcp server address that offered this lease */
49 struct ip_addr offered_ip_addr;
50 struct ip_addr offered_sn_mask;
51 struct ip_addr offered_gw_addr;
52 struct ip_addr offered_bc_addr;
53 #define DHCP_MAX_DNS 2
54 u32_t dns_count; /* actual number of DNS servers obtained */
55 struct ip_addr offered_dns_addr[DHCP_MAX_DNS]; /* DNS server addresses */
57 u32_t offered_t0_lease; /* lease period (in seconds) */
58 u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
59 u32_t offered_t2_rebind; /* recommended rebind time (usually 66% of lease period) */
60 #if LWIP_DHCP_AUTOIP_COOP
61 u8_t autoip_coop_state;
64 * TODO: See dhcp.c "TODO"s
67 struct ip_addr offered_si_addr;
72 /* MUST be compiled with "pack structs" or equivalent! */
73 #ifdef PACK_STRUCT_USE_INCLUDES
74 # include "arch/bpstruct.h"
77 /** minimum set of fields of any DHCP message */
80 PACK_STRUCT_FIELD(u8_t op);
81 PACK_STRUCT_FIELD(u8_t htype);
82 PACK_STRUCT_FIELD(u8_t hlen);
83 PACK_STRUCT_FIELD(u8_t hops);
84 PACK_STRUCT_FIELD(u32_t xid);
85 PACK_STRUCT_FIELD(u16_t secs);
86 PACK_STRUCT_FIELD(u16_t flags);
87 PACK_STRUCT_FIELD(struct ip_addr ciaddr);
88 PACK_STRUCT_FIELD(struct ip_addr yiaddr);
89 PACK_STRUCT_FIELD(struct ip_addr siaddr);
90 PACK_STRUCT_FIELD(struct ip_addr giaddr);
91 #define DHCP_CHADDR_LEN 16U
92 PACK_STRUCT_FIELD(u8_t chaddr[DHCP_CHADDR_LEN]);
93 #define DHCP_SNAME_LEN 64U
94 PACK_STRUCT_FIELD(u8_t sname[DHCP_SNAME_LEN]);
95 #define DHCP_FILE_LEN 128U
96 PACK_STRUCT_FIELD(u8_t file[DHCP_FILE_LEN]);
97 PACK_STRUCT_FIELD(u32_t cookie);
98 #define DHCP_MIN_OPTIONS_LEN 68U
99 /** make sure user does not configure this too small */
100 #if ((defined(DHCP_OPTIONS_LEN)) && (DHCP_OPTIONS_LEN < DHCP_MIN_OPTIONS_LEN))
101 # undef DHCP_OPTIONS_LEN
103 /** allow this to be configured in lwipopts.h, but not too small */
104 #if (!defined(DHCP_OPTIONS_LEN))
105 /** set this to be sufficient for your options in outgoing DHCP msgs */
106 # define DHCP_OPTIONS_LEN DHCP_MIN_OPTIONS_LEN
108 PACK_STRUCT_FIELD(u8_t options[DHCP_OPTIONS_LEN]);
109 } PACK_STRUCT_STRUCT;
111 #ifdef PACK_STRUCT_USE_INCLUDES
112 # include "arch/epstruct.h"
115 /** start DHCP configuration */
116 err_t dhcp_start(struct netif *netif);
117 /** enforce early lease renewal (not needed normally)*/
118 err_t dhcp_renew(struct netif *netif);
119 /** release the DHCP lease, usually called before dhcp_stop()*/
120 err_t dhcp_release(struct netif *netif);
121 /** stop DHCP configuration */
122 void dhcp_stop(struct netif *netif);
123 /** inform server of our manual IP address */
124 void dhcp_inform(struct netif *netif);
125 /** Handle a possible change in the network configuration */
126 void dhcp_network_changed(struct netif *netif);
128 /** if enabled, check whether the offered IP address is not in use, using ARP */
129 #if DHCP_DOES_ARP_CHECK
130 void dhcp_arp_reply(struct netif *netif, struct ip_addr *addr);
133 /** to be called every minute */
134 void dhcp_coarse_tmr(void);
135 /** to be called every half second */
136 void dhcp_fine_tmr(void);
138 /** DHCP message item offsets and length */
139 #define DHCP_MSG_OFS (UDP_DATA_OFS)
140 #define DHCP_OP_OFS (DHCP_MSG_OFS + 0)
141 #define DHCP_HTYPE_OFS (DHCP_MSG_OFS + 1)
142 #define DHCP_HLEN_OFS (DHCP_MSG_OFS + 2)
143 #define DHCP_HOPS_OFS (DHCP_MSG_OFS + 3)
144 #define DHCP_XID_OFS (DHCP_MSG_OFS + 4)
145 #define DHCP_SECS_OFS (DHCP_MSG_OFS + 8)
146 #define DHCP_FLAGS_OFS (DHCP_MSG_OFS + 10)
147 #define DHCP_CIADDR_OFS (DHCP_MSG_OFS + 12)
148 #define DHCP_YIADDR_OFS (DHCP_MSG_OFS + 16)
149 #define DHCP_SIADDR_OFS (DHCP_MSG_OFS + 20)
150 #define DHCP_GIADDR_OFS (DHCP_MSG_OFS + 24)
151 #define DHCP_CHADDR_OFS (DHCP_MSG_OFS + 28)
152 #define DHCP_SNAME_OFS (DHCP_MSG_OFS + 44)
153 #define DHCP_FILE_OFS (DHCP_MSG_OFS + 108)
154 #define DHCP_MSG_LEN 236
156 #define DHCP_COOKIE_OFS (DHCP_MSG_OFS + DHCP_MSG_LEN)
157 #define DHCP_OPTIONS_OFS (DHCP_MSG_OFS + DHCP_MSG_LEN + 4)
159 #define DHCP_CLIENT_PORT 68
160 #define DHCP_SERVER_PORT 67
162 /** DHCP client states */
163 #define DHCP_REQUESTING 1
165 #define DHCP_REBOOTING 3
166 #define DHCP_REBINDING 4
167 #define DHCP_RENEWING 5
168 #define DHCP_SELECTING 6
169 #define DHCP_INFORMING 7
170 #define DHCP_CHECKING 8
171 #define DHCP_PERMANENT 9
172 #define DHCP_BOUND 10
173 /** not yet implemented #define DHCP_RELEASING 11 */
174 #define DHCP_BACKING_OFF 12
177 /** AUTOIP cooperatation flags */
178 #define DHCP_AUTOIP_COOP_STATE_OFF 0
179 #define DHCP_AUTOIP_COOP_STATE_ON 1
181 #define DHCP_BOOTREQUEST 1
182 #define DHCP_BOOTREPLY 2
184 #define DHCP_DISCOVER 1
186 #define DHCP_REQUEST 3
187 #define DHCP_DECLINE 4
190 #define DHCP_RELEASE 7
191 #define DHCP_INFORM 8
193 #define DHCP_HTYPE_ETH 1
195 #define DHCP_HLEN_ETH 6
197 #define DHCP_BROADCAST_FLAG 15
198 #define DHCP_BROADCAST_MASK (1 << DHCP_FLAG_BROADCAST)
201 #define DHCP_OPTION_PAD 0
202 #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */
203 #define DHCP_OPTION_ROUTER 3
204 #define DHCP_OPTION_DNS_SERVER 6
205 #define DHCP_OPTION_HOSTNAME 12
206 #define DHCP_OPTION_IP_TTL 23
207 #define DHCP_OPTION_MTU 26
208 #define DHCP_OPTION_BROADCAST 28
209 #define DHCP_OPTION_TCP_TTL 37
210 #define DHCP_OPTION_END 255
213 #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */
214 #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
215 #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */
217 #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */
218 #define DHCP_OPTION_MESSAGE_TYPE_LEN 1
221 #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */
222 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */
224 #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */
225 #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2
227 #define DHCP_OPTION_T1 58 /* T1 renewal time */
228 #define DHCP_OPTION_T2 59 /* T2 rebinding time */
229 #define DHCP_OPTION_US 60
230 #define DHCP_OPTION_CLIENT_ID 61
231 #define DHCP_OPTION_TFTP_SERVERNAME 66
232 #define DHCP_OPTION_BOOTFILE 67
234 /** possible combinations of overloading the file and sname fields with options */
235 #define DHCP_OVERLOAD_NONE 0
236 #define DHCP_OVERLOAD_FILE 1
237 #define DHCP_OVERLOAD_SNAME 2
238 #define DHCP_OVERLOAD_SNAME_FILE 3
244 #endif /* LWIP_DHCP */
246 #endif /*__LWIP_DHCP_H__*/