MeterLogger
dhcp.h
Go to the documentation of this file.
1 /** @file
2  */
3 
4 #ifndef __LWIP_DHCP_H__
5 #define __LWIP_DHCP_H__
6 
7 #include "lwip/opt.h"
8 
9 #if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
10 
11 #include "lwip/netif.h"
12 #include "lwip/udp.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
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
24 
25 #define DHCP_CHADDR_LEN 16U
26 #define DHCP_SNAME_LEN 64U
27 #define DHCP_FILE_LEN 128U
28 
29 struct dhcp
30 {
31  /** transaction identifier of last sent request */
32  u32_t xid;
33  /** our connection to the DHCP server */
34  struct udp_pcb *pcb;
35  /** incoming msg */
36  struct dhcp_msg *msg_in;
37  /** current DHCP state machine state */
38  u8_t state;
39  /** retries of current request */
40  u8_t tries;
41 #if LWIP_DHCP_AUTOIP_COOP
42  u8_t autoip_coop_state;
43 #endif
44  u8_t subnet_mask_given;
45 
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;
56 
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 #define LWIP_DHCP_BOOTP_FILE 0
63 #if LWIP_DHCP_BOOTP_FILE
64  ip_addr_t offered_si_addr;
65  char boot_file_name[DHCP_FILE_LEN];
66 #endif /* LWIP_DHCP_BOOTPFILE */
67 };
68 
69 /* MUST be compiled with "pack structs" or equivalent! */
70 #ifdef PACK_STRUCT_USE_INCLUDES
71 # include "arch/bpstruct.h"
72 #endif
74 /** minimum set of fields of any DHCP message */
75 struct dhcp_msg
76 {
78  PACK_STRUCT_FIELD(u8_t htype);
79  PACK_STRUCT_FIELD(u8_t hlen);
80  PACK_STRUCT_FIELD(u8_t hops);
83  PACK_STRUCT_FIELD(u16_t flags);
88  PACK_STRUCT_FIELD(u8_t chaddr[DHCP_CHADDR_LEN]);
89  PACK_STRUCT_FIELD(u8_t sname[DHCP_SNAME_LEN]);
90  PACK_STRUCT_FIELD(u8_t file[DHCP_FILE_LEN]);
91  PACK_STRUCT_FIELD(u32_t cookie);
92 #define DHCP_MIN_OPTIONS_LEN 68U
93 /** make sure user does not configure this too small */
94 #if ((defined(DHCP_OPTIONS_LEN)) && (DHCP_OPTIONS_LEN < DHCP_MIN_OPTIONS_LEN))
95 # undef DHCP_OPTIONS_LEN
96 #endif
97 /** allow this to be configured in lwipopts.h, but not too small */
98 #if (!defined(DHCP_OPTIONS_LEN))
99 /** set this to be sufficient for your options in outgoing DHCP msgs */
100 # define DHCP_OPTIONS_LEN DHCP_MIN_OPTIONS_LEN
101 #endif
102  PACK_STRUCT_FIELD(u8_t options[DHCP_OPTIONS_LEN]);
105 #ifdef PACK_STRUCT_USE_INCLUDES
106 # include "arch/epstruct.h"
107 #endif
108 
109 void dhcp_set_struct(struct netif *netif, struct dhcp *dhcp);
110 /** Remove a struct dhcp previously set to the netif using dhcp_set_struct() */
111 #define dhcp_remove_struct(netif) do { (netif)->dhcp = NULL; } while(0)
112 void dhcp_cleanup(struct netif *netif);
113 /** start DHCP configuration */
114 err_t dhcp_start(struct netif *netif);
115 /** enforce early lease renewal (not needed normally)*/
116 err_t dhcp_renew(struct netif *netif);
117 /** release the DHCP lease, usually called before dhcp_stop()*/
118 err_t dhcp_release(struct netif *netif);
119 /** stop DHCP configuration */
120 void dhcp_stop(struct netif *netif);
121 /** inform server of our manual IP address */
122 void dhcp_inform(struct netif *netif);
123 /** Handle a possible change in the network configuration */
124 void dhcp_network_changed(struct netif *netif);
125 
126 /** if enabled, check whether the offered IP address is not in use, using ARP */
127 #if DHCP_DOES_ARP_CHECK
128 void dhcp_arp_reply(struct netif *netif, ip_addr_t *addr);
129 #endif
130 
131 /** to be called every minute */
132 void dhcp_coarse_tmr(void);
133 /** to be called every half second */
134 void dhcp_fine_tmr(void);
135 
136 /** DHCP message item offsets and length */
137 #define DHCP_OP_OFS 0
138 #define DHCP_HTYPE_OFS 1
139 #define DHCP_HLEN_OFS 2
140 #define DHCP_HOPS_OFS 3
141 #define DHCP_XID_OFS 4
142 #define DHCP_SECS_OFS 8
143 #define DHCP_FLAGS_OFS 10
144 #define DHCP_CIADDR_OFS 12
145 #define DHCP_YIADDR_OFS 16
146 #define DHCP_SIADDR_OFS 20
147 #define DHCP_GIADDR_OFS 24
148 #define DHCP_CHADDR_OFS 28
149 #define DHCP_SNAME_OFS 44
150 #define DHCP_FILE_OFS 108
151 #define DHCP_MSG_LEN 236
152 
153 #define DHCP_COOKIE_OFS DHCP_MSG_LEN
154 #define DHCP_OPTIONS_OFS (DHCP_MSG_LEN + 4)
155 
156 #define DHCP_CLIENT_PORT 68
157 #define DHCP_SERVER_PORT 67
158 
159 /** DHCP client states */
160 #define DHCP_OFF 0
161 #define DHCP_REQUESTING 1
162 #define DHCP_INIT 2
163 #define DHCP_REBOOTING 3
164 #define DHCP_REBINDING 4
165 #define DHCP_RENEWING 5
166 #define DHCP_SELECTING 6
167 #define DHCP_INFORMING 7
168 #define DHCP_CHECKING 8
169 #define DHCP_PERMANENT 9
170 #define DHCP_BOUND 10
171 /** not yet implemented #define DHCP_RELEASING 11 */
172 #define DHCP_BACKING_OFF 12
173 
174 /** AUTOIP cooperatation flags */
175 #define DHCP_AUTOIP_COOP_STATE_OFF 0
176 #define DHCP_AUTOIP_COOP_STATE_ON 1
177 
178 #define DHCP_BOOTREQUEST 1
179 #define DHCP_BOOTREPLY 2
180 
181 /** DHCP message types */
182 #define DHCP_DISCOVER 1
183 #define DHCP_OFFER 2
184 #define DHCP_REQUEST 3
185 #define DHCP_DECLINE 4
186 #define DHCP_ACK 5
187 #define DHCP_NAK 6
188 #define DHCP_RELEASE 7
189 #define DHCP_INFORM 8
190 
191 /** DHCP hardware type, currently only ethernet is supported */
192 #define DHCP_HTYPE_ETH 1
193 
194 #define DHCP_MAGIC_COOKIE 0x63825363UL
195 
196 /* This is a list of options for BOOTP and DHCP, see RFC 2132 for descriptions */
197 
198 /** BootP options */
199 #define DHCP_OPTION_PAD 0
200 #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */
201 #define DHCP_OPTION_ROUTER 3
202 #define DHCP_OPTION_DNS_SERVER 6
203 #define DHCP_OPTION_HOSTNAME 12
204 #define DHCP_OPTION_IP_TTL 23
205 #define DHCP_OPTION_MTU 26
206 #define DHCP_OPTION_BROADCAST 28
207 #define DHCP_OPTION_TCP_TTL 37
208 #define DHCP_OPTION_END 255
209 
210 /**add options for support more router by liuHan**/
211 #define DHCP_OPTION_DOMAIN_NAME 15
212 #define DHCP_OPTION_PRD 31
213 #define DHCP_OPTION_STATIC_ROUTER 33
214 #define DHCP_OPTION_VSN 43
215 #define DHCP_OPTION_NB_TINS 44
216 #define DHCP_OPTION_NB_TINT 46
217 #define DHCP_OPTION_NB_TIS 47
218 #define DHCP_OPTION_CLASSLESS_STATIC_ROUTER 121
219 /** DHCP options */
220 #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */
221 #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
222 #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */
223 
224 #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */
225 #define DHCP_OPTION_MESSAGE_TYPE_LEN 1
226 
227 #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */
228 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */
229 
230 #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */
231 #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2
232 
233 #define DHCP_OPTION_T1 58 /* T1 renewal time */
234 #define DHCP_OPTION_T2 59 /* T2 rebinding time */
235 #define DHCP_OPTION_US 60
236 #define DHCP_OPTION_CLIENT_ID 61
237 #define DHCP_OPTION_TFTP_SERVERNAME 66
238 #define DHCP_OPTION_BOOTFILE 67
239 
240 /** possible combinations of overloading the file and sname fields with options */
241 #define DHCP_OVERLOAD_NONE 0
242 #define DHCP_OVERLOAD_FILE 1
243 #define DHCP_OVERLOAD_SNAME 2
244 #define DHCP_OVERLOAD_SNAME_FILE 3
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif /* LWIP_DHCP */
251 
252 #endif /*__LWIP_DHCP_H__*/
unsigned long u32_t
Definition: cc.h:56
static state_t * state
Definition: aes.c:67
Definition: pbuf.h:76
#define PACK_STRUCT_STRUCT
Definition: cc.h:72
s8_t err_t
Definition: err.h:47
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
Definition: netif.h:139
#define PACK_STRUCT_BEGIN
Definition: cc.h:73
#define PACK_STRUCT_END
Definition: cc.h:74
unsigned char u8_t
Definition: cc.h:52
unsigned short u16_t
Definition: cc.h:54
#define PACK_STRUCT_FIELD(x)
Definition: cc.h:71