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
25 #define DHCP_CHADDR_LEN 16U
26 #define DHCP_SNAME_LEN 64U
27 #define DHCP_FILE_LEN 128U
31 /** transaction identifier of last sent request */
33 /** our connection to the DHCP server */
36 struct dhcp_msg
*msg_in
;
37 /** current DHCP state machine state */
39 /** retries of current request */
41 #if LWIP_DHCP_AUTOIP_COOP
42 u8_t autoip_coop_state
;
44 u8_t subnet_mask_given
;
46 struct pbuf
*p_out
; /* pbuf of outcoming msg */
47 struct dhcp_msg
*msg_out
; /* outgoing msg */
48 u16_t options_out_len
; /* outgoing msg options length */
49 u16_t request_timeout
; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
50 u16_t t1_timeout
; /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
51 u16_t t2_timeout
; /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
52 ip_addr_t server_ip_addr
; /* dhcp server address that offered this lease */
53 ip_addr_t offered_ip_addr
;
54 ip_addr_t offered_sn_mask
;
55 ip_addr_t offered_gw_addr
;
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 /* @todo: LWIP_DHCP_BOOTP_FILE configuration option?
61 integrate with possible TFTP-client for booting? */
62 #if LWIP_DHCP_BOOTP_FILE
63 ip_addr_t offered_si_addr
;
64 char boot_file_name
[DHCP_FILE_LEN
];
65 #endif /* LWIP_DHCP_BOOTPFILE */
68 /* MUST be compiled with "pack structs" or equivalent! */
69 #ifdef PACK_STRUCT_USE_INCLUDES
70 # include "arch/bpstruct.h"
73 /** minimum set of fields of any DHCP message */
76 PACK_STRUCT_FIELD(u8_t op
);
77 PACK_STRUCT_FIELD(u8_t htype
);
78 PACK_STRUCT_FIELD(u8_t hlen
);
79 PACK_STRUCT_FIELD(u8_t hops
);
80 PACK_STRUCT_FIELD(u32_t xid
);
81 PACK_STRUCT_FIELD(u16_t secs
);
82 PACK_STRUCT_FIELD(u16_t flags
);
83 PACK_STRUCT_FIELD(ip_addr_p_t ciaddr
);
84 PACK_STRUCT_FIELD(ip_addr_p_t yiaddr
);
85 PACK_STRUCT_FIELD(ip_addr_p_t siaddr
);
86 PACK_STRUCT_FIELD(ip_addr_p_t giaddr
);
87 PACK_STRUCT_FIELD(u8_t chaddr
[DHCP_CHADDR_LEN
]);
88 PACK_STRUCT_FIELD(u8_t sname
[DHCP_SNAME_LEN
]);
89 PACK_STRUCT_FIELD(u8_t file
[DHCP_FILE_LEN
]);
90 PACK_STRUCT_FIELD(u32_t cookie
);
91 #define DHCP_MIN_OPTIONS_LEN 68U
92 /** make sure user does not configure this too small */
93 #if ((defined(DHCP_OPTIONS_LEN)) && (DHCP_OPTIONS_LEN < DHCP_MIN_OPTIONS_LEN))
94 # undef DHCP_OPTIONS_LEN
96 /** allow this to be configured in lwipopts.h, but not too small */
97 #if (!defined(DHCP_OPTIONS_LEN))
98 /** set this to be sufficient for your options in outgoing DHCP msgs */
99 # define DHCP_OPTIONS_LEN DHCP_MIN_OPTIONS_LEN
101 PACK_STRUCT_FIELD(u8_t options
[DHCP_OPTIONS_LEN
]);
102 } PACK_STRUCT_STRUCT
;
104 #ifdef PACK_STRUCT_USE_INCLUDES
105 # include "arch/epstruct.h"
108 void dhcp_set_struct(struct netif
*netif
, struct dhcp
*dhcp
);
109 /** start DHCP configuration */
110 err_t
dhcp_start(struct netif
*netif
);
111 /** enforce early lease renewal (not needed normally)*/
112 err_t
dhcp_renew(struct netif
*netif
);
113 /** release the DHCP lease, usually called before dhcp_stop()*/
114 err_t
dhcp_release(struct netif
*netif
);
115 /** stop DHCP configuration */
116 void dhcp_stop(struct netif
*netif
);
117 /** inform server of our manual IP address */
118 void dhcp_inform(struct netif
*netif
);
119 /** Handle a possible change in the network configuration */
120 void dhcp_network_changed(struct netif
*netif
);
122 /** if enabled, check whether the offered IP address is not in use, using ARP */
123 #if DHCP_DOES_ARP_CHECK
124 void dhcp_arp_reply(struct netif
*netif
, ip_addr_t
*addr
);
127 /** to be called every minute */
128 void dhcp_coarse_tmr(void);
129 /** to be called every half second */
130 void dhcp_fine_tmr(void);
132 /** DHCP message item offsets and length */
133 #define DHCP_OP_OFS 0
134 #define DHCP_HTYPE_OFS 1
135 #define DHCP_HLEN_OFS 2
136 #define DHCP_HOPS_OFS 3
137 #define DHCP_XID_OFS 4
138 #define DHCP_SECS_OFS 8
139 #define DHCP_FLAGS_OFS 10
140 #define DHCP_CIADDR_OFS 12
141 #define DHCP_YIADDR_OFS 16
142 #define DHCP_SIADDR_OFS 20
143 #define DHCP_GIADDR_OFS 24
144 #define DHCP_CHADDR_OFS 28
145 #define DHCP_SNAME_OFS 44
146 #define DHCP_FILE_OFS 108
147 #define DHCP_MSG_LEN 236
149 #define DHCP_COOKIE_OFS DHCP_MSG_LEN
150 #define DHCP_OPTIONS_OFS (DHCP_MSG_LEN + 4)
152 #define DHCP_CLIENT_PORT 68
153 #define DHCP_SERVER_PORT 67
155 /** DHCP client states */
157 #define DHCP_REQUESTING 1
159 #define DHCP_REBOOTING 3
160 #define DHCP_REBINDING 4
161 #define DHCP_RENEWING 5
162 #define DHCP_SELECTING 6
163 #define DHCP_INFORMING 7
164 #define DHCP_CHECKING 8
165 #define DHCP_PERMANENT 9
166 #define DHCP_BOUND 10
167 /** not yet implemented #define DHCP_RELEASING 11 */
168 #define DHCP_BACKING_OFF 12
170 /** AUTOIP cooperatation flags */
171 #define DHCP_AUTOIP_COOP_STATE_OFF 0
172 #define DHCP_AUTOIP_COOP_STATE_ON 1
174 #define DHCP_BOOTREQUEST 1
175 #define DHCP_BOOTREPLY 2
177 /** DHCP message types */
178 #define DHCP_DISCOVER 1
180 #define DHCP_REQUEST 3
181 #define DHCP_DECLINE 4
184 #define DHCP_RELEASE 7
185 #define DHCP_INFORM 8
187 /** DHCP hardware type, currently only ethernet is supported */
188 #define DHCP_HTYPE_ETH 1
190 #define DHCP_MAGIC_COOKIE 0x63825363UL
192 /* This is a list of options for BOOTP and DHCP, see RFC 2132 for descriptions */
195 #define DHCP_OPTION_PAD 0
196 #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */
197 #define DHCP_OPTION_ROUTER 3
198 #define DHCP_OPTION_DNS_SERVER 6
199 #define DHCP_OPTION_HOSTNAME 12
200 #define DHCP_OPTION_IP_TTL 23
201 #define DHCP_OPTION_MTU 26
202 #define DHCP_OPTION_BROADCAST 28
203 #define DHCP_OPTION_TCP_TTL 37
204 #define DHCP_OPTION_END 255
207 #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */
208 #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
209 #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */
211 #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */
212 #define DHCP_OPTION_MESSAGE_TYPE_LEN 1
214 #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */
215 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */
217 #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */
218 #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2
220 #define DHCP_OPTION_T1 58 /* T1 renewal time */
221 #define DHCP_OPTION_T2 59 /* T2 rebinding time */
222 #define DHCP_OPTION_US 60
223 #define DHCP_OPTION_CLIENT_ID 61
224 #define DHCP_OPTION_TFTP_SERVERNAME 66
225 #define DHCP_OPTION_BOOTFILE 67
227 /** possible combinations of overloading the file and sname fields with options */
228 #define DHCP_OVERLOAD_NONE 0
229 #define DHCP_OVERLOAD_FILE 1
230 #define DHCP_OVERLOAD_SNAME 2
231 #define DHCP_OVERLOAD_SNAME_FILE 3
237 #endif /* LWIP_DHCP */
239 #endif /*__LWIP_DHCP_H__*/