• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * DHCP client API
4  */
5 
6 /*
7  * Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
8  * Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Leon Woestenberg <leon.woestenberg@gmx.net>
36  *
37  */
38 #ifndef LWIP_HDR_DHCP_H
39 #define LWIP_HDR_DHCP_H
40 
41 #include "lwip/opt.h"
42 
43 #if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
44 
45 #include "lwip/netif.h"
46 #include "lwip/udp.h"
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /** period (in seconds) of the application calling dhcp_coarse_tmr() */
53 #define DHCP_COARSE_TIMER_SECS  60
54 /** period (in milliseconds) of the application calling dhcp_coarse_tmr() */
55 #define DHCP_COARSE_TIMER_MSECS (DHCP_COARSE_TIMER_SECS * 1000UL)
56 /** period (in milliseconds) of the application calling dhcp_fine_tmr() */
57 #define DHCP_FINE_TIMER_MSECS   500
58 
59 #define DHCP_BOOT_FILE_LEN      128U
60 
61 #if LWIP_DNS && LWIP_DHCP_MAX_DNS_SERVERS
62 #if DNS_MAX_SERVERS > LWIP_DHCP_MAX_DNS_SERVERS
63 #define LWIP_DHCP_PROVIDE_DNS_SERVERS LWIP_DHCP_MAX_DNS_SERVERS
64 #else
65 #define LWIP_DHCP_PROVIDE_DNS_SERVERS DNS_MAX_SERVERS
66 #endif
67 #else
68 #define LWIP_DHCP_PROVIDE_DNS_SERVERS 0
69 #endif
70 
71 /** Option handling: options are parsed in dhcp_parse_reply
72  * and saved in an array where other functions can load them from.
73  * This might be moved into the struct dhcp (not necessarily since
74  * lwIP is single-threaded and the array is only used while in recv
75  * callback). */
76 enum dhcp_option_idx {
77   DHCP_OPTION_IDX_OVERLOAD = 0,
78   DHCP_OPTION_IDX_MSG_TYPE,
79   DHCP_OPTION_IDX_SERVER_ID,
80   DHCP_OPTION_IDX_LEASE_TIME,
81   DHCP_OPTION_IDX_T1,
82   DHCP_OPTION_IDX_T2,
83   DHCP_OPTION_IDX_SUBNET_MASK,
84   DHCP_OPTION_IDX_ROUTER,
85 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
86   DHCP_OPTION_IDX_DNS_SERVER,
87   DHCP_OPTION_IDX_DNS_SERVER_LAST = DHCP_OPTION_IDX_DNS_SERVER + LWIP_DHCP_PROVIDE_DNS_SERVERS - 1,
88 #endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
89 #if LWIP_DHCP_GET_NTP_SRV
90   DHCP_OPTION_IDX_NTP_SERVER,
91   DHCP_OPTION_IDX_NTP_SERVER_LAST = DHCP_OPTION_IDX_NTP_SERVER + LWIP_DHCP_MAX_NTP_SERVERS - 1,
92 #endif /* LWIP_DHCP_GET_NTP_SRV */
93   DHCP_OPTION_IDX_MAX
94 };
95 
96 /* AutoIP cooperation flags (struct dhcp.autoip_coop_state) */
97 typedef enum {
98   DHCP_AUTOIP_COOP_STATE_OFF  = 0,
99   DHCP_AUTOIP_COOP_STATE_ON   = 1
100 } dhcp_autoip_coop_state_enum_t;
101 
102 struct dhcp
103 {
104   /** transaction identifier of last sent request */
105   u32_t xid;
106   /** track PCB allocation state */
107   u8_t pcb_allocated;
108   /** current DHCP state machine state */
109   u8_t state;
110   /** retries of current request */
111   u8_t tries;
112 #if LWIP_DHCP_AUTOIP_COOP
113   u8_t autoip_coop_state;
114 #endif
115   u8_t subnet_mask_given;
116 
117   u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
118   u16_t t1_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
119   u16_t t2_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
120   u16_t t1_renew_time;  /* #ticks with period DHCP_COARSE_TIMER_SECS until next renew try */
121   u16_t t2_rebind_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next rebind try */
122   u16_t lease_used; /* #ticks with period DHCP_COARSE_TIMER_SECS since last received DHCP ack */
123   u16_t t0_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for lease time */
124   ip_addr_t server_ip_addr; /* dhcp server address that offered this lease (ip_addr_t because passed to UDP) */
125   ip4_addr_t offered_ip_addr;
126   ip4_addr_t offered_sn_mask;
127   ip4_addr_t offered_gw_addr;
128 
129   u32_t offered_t0_lease; /* lease period (in seconds) */
130   u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
131   u32_t offered_t2_rebind; /* recommended rebind time (usually 87.5 of lease period)  */
132 #if LWIP_DHCP_BOOTP_FILE
133   ip4_addr_t offered_si_addr;
134   char boot_file_name[DHCP_BOOT_FILE_LEN];
135 #endif /* LWIP_DHCP_BOOTPFILE */
136   /** Holds the decoded option values, only valid while in dhcp_recv. */
137   u32_t rx_options_val[DHCP_OPTION_IDX_MAX];
138   /** Holds a flag which option was received and is contained in dhcp_rx_options_val, only valid while in dhcp_recv. */
139   u8_t rx_options_given[DHCP_OPTION_IDX_MAX];
140 };
141 
142 
143 void dhcp_set_struct(struct netif *netif, struct dhcp *dhcp);
144 /** Remove a struct dhcp previously set to the netif using dhcp_set_struct() */
145 #define dhcp_remove_struct(netif) netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, NULL)
146 void dhcp_cleanup(struct netif *netif);
147 err_t dhcp_start(struct netif *netif);
148 err_t dhcp_renew(struct netif *netif);
149 err_t dhcp_release(struct netif *netif);
150 void dhcp_stop(struct netif *netif);
151 void dhcp_release_and_stop(struct netif *netif);
152 void dhcp_inform(struct netif *netif);
153 void dhcp_network_changed(struct netif *netif);
154 #if DHCP_DOES_ARP_CHECK
155 void dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr);
156 #endif
157 u8_t dhcp_supplied_address(const struct netif *netif);
158 /* to be called every minute */
159 void dhcp_coarse_tmr(void);
160 /* to be called every half second */
161 void dhcp_fine_tmr(void);
162 
163 #if LWIP_LOWPOWER
164 u32_t dhcp_coarse_tmr_tick(void);
165 u32_t dhcp_fine_tmr_tick(void);
166 #endif
167 
168 #if LWIP_DHCP_GET_NTP_SRV
169 /** This function must exist, in other to add offered NTP servers to
170  * the NTP (or SNTP) engine.
171  * See LWIP_DHCP_MAX_NTP_SERVERS */
172 extern void dhcp_set_ntp_servers(u8_t num_ntp_servers, const ip4_addr_t* ntp_server_addrs);
173 #endif /* LWIP_DHCP_GET_NTP_SRV */
174 
175 #define netif_dhcp_data(netif) ((struct dhcp*)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP))
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif /* LWIP_DHCP */
182 
183 #endif /*LWIP_HDR_DHCP_H*/
184