STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
dhcp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
3  * Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
26  * OF SUCH DAMAGE.
27  *
28  * This file is part of the lwIP TCP/IP stack.
29  *
30  * Author: Leon Woestenberg <leon.woestenberg@gmx.net>
31  *
32  */
33 #ifndef LWIP_HDR_DHCP_H
34 #define LWIP_HDR_DHCP_H
35 
36 #include "lwip/opt.h"
37 
38 #if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
39 
40 #include "lwip/netif.h"
41 #include "lwip/udp.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
48 #define DHCP_COARSE_TIMER_SECS 60
49 
50 #define DHCP_COARSE_TIMER_MSECS (DHCP_COARSE_TIMER_SECS * 1000UL)
51 
52 #define DHCP_FINE_TIMER_MSECS 500
53 
54 #define DHCP_CHADDR_LEN 16U
55 #define DHCP_SNAME_LEN 64U
56 #define DHCP_FILE_LEN 128U
57 
58 struct dhcp
59 {
61  u32_t xid;
63  struct udp_pcb *pcb;
65  struct dhcp_msg *msg_in;
67  u8_t state;
69  u8_t tries;
70 #if LWIP_DHCP_AUTOIP_COOP
71  u8_t autoip_coop_state;
72 #endif
73  u8_t subnet_mask_given;
74 
75  struct pbuf *p_out; /* pbuf of outcoming msg */
76  struct dhcp_msg *msg_out; /* outgoing msg */
77  u16_t options_out_len; /* outgoing msg options length */
78  u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
79  u16_t t1_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
80  u16_t t2_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
81  u16_t t1_renew_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next renew try */
82  u16_t t2_rebind_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next rebind try */
83  u16_t lease_used; /* #ticks with period DHCP_COARSE_TIMER_SECS since last received DHCP ack */
84  u16_t t0_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for lease time */
85  ip_addr_t server_ip_addr; /* dhcp server address that offered this lease (ip_addr_t because passed to UDP) */
86  ip4_addr_t offered_ip_addr;
87  ip4_addr_t offered_sn_mask;
88  ip4_addr_t offered_gw_addr;
89 
90  u32_t offered_t0_lease; /* lease period (in seconds) */
91  u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
92  u32_t offered_t2_rebind; /* recommended rebind time (usually 87.5 of lease period) */
93 #if LWIP_DHCP_BOOTP_FILE
94  ip_addr_t offered_si_addr;
95  char boot_file_name[DHCP_FILE_LEN];
96 #endif /* LWIP_DHCP_BOOTPFILE */
97 };
98 
99 /* MUST be compiled with "pack structs" or equivalent! */
100 #ifdef PACK_STRUCT_USE_INCLUDES
101 # include "arch/bpstruct.h"
102 #endif
105 struct dhcp_msg
106 {
108  PACK_STRUCT_FLD_8(u8_t htype);
109  PACK_STRUCT_FLD_8(u8_t hlen);
110  PACK_STRUCT_FLD_8(u8_t hops);
112  PACK_STRUCT_FIELD(u16_t secs);
113  PACK_STRUCT_FIELD(u16_t flags);
114  PACK_STRUCT_FLD_S(ip4_addr_p_t ciaddr);
115  PACK_STRUCT_FLD_S(ip4_addr_p_t yiaddr);
116  PACK_STRUCT_FLD_S(ip4_addr_p_t siaddr);
117  PACK_STRUCT_FLD_S(ip4_addr_p_t giaddr);
118  PACK_STRUCT_FLD_8(u8_t chaddr[DHCP_CHADDR_LEN]);
119  PACK_STRUCT_FLD_8(u8_t sname[DHCP_SNAME_LEN]);
120  PACK_STRUCT_FLD_8(u8_t file[DHCP_FILE_LEN]);
121  PACK_STRUCT_FIELD(u32_t cookie);
122 #define DHCP_MIN_OPTIONS_LEN 68U
123 
124 #if ((defined(DHCP_OPTIONS_LEN)) && (DHCP_OPTIONS_LEN < DHCP_MIN_OPTIONS_LEN))
125 # undef DHCP_OPTIONS_LEN
126 #endif
127 
128 #if (!defined(DHCP_OPTIONS_LEN))
129 
130 # define DHCP_OPTIONS_LEN DHCP_MIN_OPTIONS_LEN
131 #endif
132  PACK_STRUCT_FLD_8(u8_t options[DHCP_OPTIONS_LEN]);
135 #ifdef PACK_STRUCT_USE_INCLUDES
136 # include "arch/epstruct.h"
137 #endif
138 
139 void dhcp_set_struct(struct netif *netif, struct dhcp *dhcp);
141 #define dhcp_remove_struct(netif) do { (netif)->dhcp = NULL; } while(0)
142 void dhcp_cleanup(struct netif *netif);
144 err_t dhcp_start(struct netif *netif);
146 err_t dhcp_renew(struct netif *netif);
148 err_t dhcp_release(struct netif *netif);
150 void dhcp_stop(struct netif *netif);
152 void dhcp_inform(struct netif *netif);
154 void dhcp_network_changed(struct netif *netif);
155 
157 #if DHCP_DOES_ARP_CHECK
158 void dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr);
159 #endif
160 
162 u8_t dhcp_supplied_address(struct netif *netif);
163 
165 void dhcp_coarse_tmr(void);
167 void dhcp_fine_tmr(void);
168 
170 #define DHCP_OP_OFS 0
171 #define DHCP_HTYPE_OFS 1
172 #define DHCP_HLEN_OFS 2
173 #define DHCP_HOPS_OFS 3
174 #define DHCP_XID_OFS 4
175 #define DHCP_SECS_OFS 8
176 #define DHCP_FLAGS_OFS 10
177 #define DHCP_CIADDR_OFS 12
178 #define DHCP_YIADDR_OFS 16
179 #define DHCP_SIADDR_OFS 20
180 #define DHCP_GIADDR_OFS 24
181 #define DHCP_CHADDR_OFS 28
182 #define DHCP_SNAME_OFS 44
183 #define DHCP_FILE_OFS 108
184 #define DHCP_MSG_LEN 236
185 
186 #define DHCP_COOKIE_OFS DHCP_MSG_LEN
187 #define DHCP_OPTIONS_OFS (DHCP_MSG_LEN + 4)
188 
189 #define DHCP_CLIENT_PORT 68
190 #define DHCP_SERVER_PORT 67
191 
193 #define DHCP_STATE_OFF 0
194 #define DHCP_STATE_REQUESTING 1
195 #define DHCP_STATE_INIT 2
196 #define DHCP_STATE_REBOOTING 3
197 #define DHCP_STATE_REBINDING 4
198 #define DHCP_STATE_RENEWING 5
199 #define DHCP_STATE_SELECTING 6
200 #define DHCP_STATE_INFORMING 7
201 #define DHCP_STATE_CHECKING 8
202 
203 #define DHCP_STATE_BOUND 10
204 
205 #define DHCP_STATE_BACKING_OFF 12
206 
208 #define DHCP_AUTOIP_COOP_STATE_OFF 0
209 #define DHCP_AUTOIP_COOP_STATE_ON 1
210 
211 #define DHCP_BOOTREQUEST 1
212 #define DHCP_BOOTREPLY 2
213 
215 #define DHCP_DISCOVER 1
216 #define DHCP_OFFER 2
217 #define DHCP_REQUEST 3
218 #define DHCP_DECLINE 4
219 #define DHCP_ACK 5
220 #define DHCP_NAK 6
221 #define DHCP_RELEASE 7
222 #define DHCP_INFORM 8
223 
225 #define DHCP_HTYPE_ETH 1
226 
227 #define DHCP_MAGIC_COOKIE 0x63825363UL
228 
229 /* This is a list of options for BOOTP and DHCP, see RFC 2132 for descriptions */
230 
232 #define DHCP_OPTION_PAD 0
233 #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */
234 #define DHCP_OPTION_ROUTER 3
235 #define DHCP_OPTION_DNS_SERVER 6
236 #define DHCP_OPTION_HOSTNAME 12
237 #define DHCP_OPTION_IP_TTL 23
238 #define DHCP_OPTION_MTU 26
239 #define DHCP_OPTION_BROADCAST 28
240 #define DHCP_OPTION_TCP_TTL 37
241 #define DHCP_OPTION_NTP 42
242 #define DHCP_OPTION_END 255
243 
245 #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */
246 #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
247 #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */
248 
249 #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */
250 #define DHCP_OPTION_MESSAGE_TYPE_LEN 1
251 
252 #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */
253 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */
254 
255 #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */
256 #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2
257 
258 #define DHCP_OPTION_T1 58 /* T1 renewal time */
259 #define DHCP_OPTION_T2 59 /* T2 rebinding time */
260 #define DHCP_OPTION_US 60
261 #define DHCP_OPTION_CLIENT_ID 61
262 #define DHCP_OPTION_TFTP_SERVERNAME 66
263 #define DHCP_OPTION_BOOTFILE 67
264 
266 #define DHCP_OVERLOAD_NONE 0
267 #define DHCP_OVERLOAD_FILE 1
268 #define DHCP_OVERLOAD_SNAME 2
269 #define DHCP_OVERLOAD_SNAME_FILE 3
270 
271 #if LWIP_DHCP_GET_NTP_SRV
272 
275 extern void dhcp_set_ntp_servers(u8_t num_ntp_servers, const ip4_addr_t* ntp_server_addrs);
276 #endif /* LWIP_DHCP_GET_NTP_SRV */
277 
278 #ifdef __cplusplus
279 }
280 #endif
281 
282 #endif /* LWIP_DHCP */
283 
284 #endif /*LWIP_HDR_DHCP_H*/
285 
#define PACK_STRUCT_STRUCT
Definition: arch.h:68
#define PACK_STRUCT_FLD_8(x)
Definition: arch.h:78
#define PACK_STRUCT_FIELD(x)
Definition: arch.h:72
unsigned long u32_t
Definition: cc.h:42
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
Definition: netif.h:182
#define PACK_STRUCT_FLD_S(x)
Definition: arch.h:84
#define PACK_STRUCT_BEGIN
Definition: arch.h:60
unsigned char u8_t
Definition: cc.h:38
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
#define PACK_STRUCT_END
Definition: arch.h:64
unsigned short u16_t
Definition: cc.h:40