4 #ifndef __LWIP_DHCP_H__
5 #define __LWIP_DHCP_H__
8 #include "lwip/netif.h"
11 /** period (in seconds) of the application calling dhcp_coarse_tmr() */
12 #define DHCP_COARSE_TIMER_SECS 60
13 /** period (in milliseconds) of the application calling dhcp_fine_tmr() */
14 #define DHCP_FINE_TIMER_MSECS 500
18 /** current DHCP state machine state */
20 /** retries of current request */
22 /** transaction identifier of last sent request */
24 /** our connection to the DHCP server */
26 /** (first) pbuf of incoming msg */
29 struct dhcp_msg *msg_in;
30 /** incoming msg options */
31 struct dhcp_msg *options_in;
32 /** ingoing msg options length */
35 struct pbuf *p_out; /* pbuf of outcoming msg */
36 struct dhcp_msg *msg_out; /* outgoing msg */
37 u16_t options_out_len; /* outgoing msg options length */
38 u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
39 u16_t t1_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
40 u16_t t2_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
41 struct ip_addr server_ip_addr; /* dhcp server address that offered this lease */
42 struct ip_addr offered_ip_addr;
43 struct ip_addr offered_sn_mask;
44 struct ip_addr offered_gw_addr;
45 struct ip_addr offered_bc_addr;
46 u32_t offered_t0_lease; /* lease period (in seconds) */
47 u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
48 u32_t offered_t2_rebind; /* recommended rebind time (usually 66% of lease period) */
50 * TODO: See dhcp.c "TODO"s
53 struct ip_addr offered_si_addr;
58 /* MUST be compiled with "pack structs" or equivalent! */
59 #ifdef PACK_STRUCT_USE_INCLUDES
60 # include "arch/bpstruct.h"
63 /** minimum set of fields of any DHCP message */
66 PACK_STRUCT_FIELD(u8_t op);
67 PACK_STRUCT_FIELD(u8_t htype);
68 PACK_STRUCT_FIELD(u8_t hlen);
69 PACK_STRUCT_FIELD(u8_t hops);
70 PACK_STRUCT_FIELD(u32_t xid);
71 PACK_STRUCT_FIELD(u16_t secs);
72 PACK_STRUCT_FIELD(u16_t flags);
73 PACK_STRUCT_FIELD(struct ip_addr ciaddr);
74 PACK_STRUCT_FIELD(struct ip_addr yiaddr);
75 PACK_STRUCT_FIELD(struct ip_addr siaddr);
76 PACK_STRUCT_FIELD(struct ip_addr giaddr);
77 #define DHCP_CHADDR_LEN 16U
78 PACK_STRUCT_FIELD(u8_t chaddr[DHCP_CHADDR_LEN]);
79 #define DHCP_SNAME_LEN 64U
80 PACK_STRUCT_FIELD(u8_t sname[DHCP_SNAME_LEN]);
81 #define DHCP_FILE_LEN 128U
82 PACK_STRUCT_FIELD(u8_t file[DHCP_FILE_LEN]);
83 PACK_STRUCT_FIELD(u32_t cookie);
84 #define DHCP_MIN_OPTIONS_LEN 68U
85 /** allow this to be configured in lwipopts.h, but not too small */
86 #if ((!defined(DHCP_OPTIONS_LEN)) || (DHCP_OPTIONS_LEN < DHCP_MIN_OPTIONS_LEN))
87 /** set this to be sufficient for your options in outgoing DHCP msgs */
88 # define DHCP_OPTIONS_LEN DHCP_MIN_OPTIONS_LEN
90 PACK_STRUCT_FIELD(u8_t options[DHCP_OPTIONS_LEN]);
93 #ifdef PACK_STRUCT_USE_INCLUDES
94 # include "arch/epstruct.h"
97 /** start DHCP configuration */
98 err_t dhcp_start(struct netif *netif);
99 /** stop DHCP configuration */
100 void dhcp_stop(struct netif *netif);
101 /** enforce lease renewal */
102 err_t dhcp_renew(struct netif *netif);
103 /** inform server of our IP address */
104 void dhcp_inform(struct netif *netif);
106 /** if enabled, check whether the offered IP address is not in use, using ARP */
107 #if DHCP_DOES_ARP_CHECK
108 void dhcp_arp_reply(struct netif *netif, struct ip_addr *addr);
111 /** to be called every minute */
112 void dhcp_coarse_tmr(void);
113 /** to be called every half second */
114 void dhcp_fine_tmr(void);
116 /** DHCP message item offsets and length */
117 #define DHCP_MSG_OFS (UDP_DATA_OFS)
118 #define DHCP_OP_OFS (DHCP_MSG_OFS + 0)
119 #define DHCP_HTYPE_OFS (DHCP_MSG_OFS + 1)
120 #define DHCP_HLEN_OFS (DHCP_MSG_OFS + 2)
121 #define DHCP_HOPS_OFS (DHCP_MSG_OFS + 3)
122 #define DHCP_XID_OFS (DHCP_MSG_OFS + 4)
123 #define DHCP_SECS_OFS (DHCP_MSG_OFS + 8)
124 #define DHCP_FLAGS_OFS (DHCP_MSG_OFS + 10)
125 #define DHCP_CIADDR_OFS (DHCP_MSG_OFS + 12)
126 #define DHCP_YIADDR_OFS (DHCP_MSG_OFS + 16)
127 #define DHCP_SIADDR_OFS (DHCP_MSG_OFS + 20)
128 #define DHCP_GIADDR_OFS (DHCP_MSG_OFS + 24)
129 #define DHCP_CHADDR_OFS (DHCP_MSG_OFS + 28)
130 #define DHCP_SNAME_OFS (DHCP_MSG_OFS + 44)
131 #define DHCP_FILE_OFS (DHCP_MSG_OFS + 108)
132 #define DHCP_MSG_LEN 236
134 #define DHCP_COOKIE_OFS (DHCP_MSG_OFS + DHCP_MSG_LEN)
135 #define DHCP_OPTIONS_OFS (DHCP_MSG_OFS + DHCP_MSG_LEN + 4)
137 #define DHCP_CLIENT_PORT 68
138 #define DHCP_SERVER_PORT 67
140 /** DHCP client states */
141 #define DHCP_REQUESTING 1
143 #define DHCP_REBOOTING 3
144 #define DHCP_REBINDING 4
145 #define DHCP_RENEWING 5
146 #define DHCP_SELECTING 6
147 #define DHCP_INFORMING 7
148 #define DHCP_CHECKING 8
149 #define DHCP_PERMANENT 9
150 #define DHCP_BOUND 10
151 /** not yet implemented #define DHCP_RELEASING 11 */
152 #define DHCP_BACKING_OFF 12
155 #define DHCP_BOOTREQUEST 1
156 #define DHCP_BOOTREPLY 2
158 #define DHCP_DISCOVER 1
160 #define DHCP_REQUEST 3
161 #define DHCP_DECLINE 4
164 #define DHCP_RELEASE 7
165 #define DHCP_INFORM 8
167 #define DHCP_HTYPE_ETH 1
169 #define DHCP_HLEN_ETH 6
171 #define DHCP_BROADCAST_FLAG 15
172 #define DHCP_BROADCAST_MASK (1 << DHCP_FLAG_BROADCAST)
175 #define DHCP_OPTION_PAD 0
176 #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */
177 #define DHCP_OPTION_ROUTER 3
178 #define DHCP_OPTION_HOSTNAME 12
179 #define DHCP_OPTION_IP_TTL 23
180 #define DHCP_OPTION_MTU 26
181 #define DHCP_OPTION_BROADCAST 28
182 #define DHCP_OPTION_TCP_TTL 37
183 #define DHCP_OPTION_END 255
186 #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */
187 #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
188 #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */
190 #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */
191 #define DHCP_OPTION_MESSAGE_TYPE_LEN 1
194 #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */
195 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */
197 #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */
198 #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2
200 #define DHCP_OPTION_T1 58 /* T1 renewal time */
201 #define DHCP_OPTION_T2 59 /* T2 rebinding time */
202 #define DHCP_OPTION_CLIENT_ID 61
203 #define DHCP_OPTION_TFTP_SERVERNAME 66
204 #define DHCP_OPTION_BOOTFILE 67
206 /** possible combinations of overloading the file and sname fields with options */
207 #define DHCP_OVERLOAD_NONE 0
208 #define DHCP_OVERLOAD_FILE 1
209 #define DHCP_OVERLOAD_SNAME 2
210 #define DHCP_OVERLOAD_SNAME_FILE 3
212 #endif /*__LWIP_DHCP_H__*/