1 /** @file 2 */ 3 4 #ifndef __LWIP_DHCP_H__ 5 #define __LWIP_DHCP_H__ 6 7 #include "lwip/opt.h" 8 #include "lwip/netif.h" 9 #include "lwip/udp.h" 10 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 15 16 struct dhcp 17 { 18 /** current DHCP state machine state */ 19 u8_t state; 20 /** retries of current request */ 21 u8_t tries; 22 /** transaction identifier of last sent request */ 23 u32_t xid; 24 /** our connection to the DHCP server */ 25 struct udp_pcb *pcb; 26 /** (first) pbuf of incoming msg */ 27 struct pbuf *p; 28 /** incoming msg */ 29 struct dhcp_msg *msg_in; 30 /** incoming msg options */ 31 struct dhcp_msg *options_in; 32 /** ingoing msg options length */ 33 u16_t options_in_len; 34 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 #define DHCP_MAX_DNS 2 47 u32_t dns_count; /* actual number of DNS servers obtained */ 48 struct ip_addr offered_dns_addr[DHCP_MAX_DNS]; /* DNS server addresses */ 49 50 u32_t offered_t0_lease; /* lease period (in seconds) */ 51 u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */ 52 u32_t offered_t2_rebind; /* recommended rebind time (usually 66% of lease period) */ 53 /** Patch #1308 54 * TODO: See dhcp.c "TODO"s 55 */ 56 #if 0 57 struct ip_addr offered_si_addr; 58 u8_t *boot_file_name; 59 #endif 60 }; 61 62 /* MUST be compiled with "pack structs" or equivalent! */ 63 #ifdef PACK_STRUCT_USE_INCLUDES 64 # include "arch/bpstruct.h" 65 #endif 66 PACK_STRUCT_BEGIN 67 /** minimum set of fields of any DHCP message */ 68 struct dhcp_msg 69 { 70 PACK_STRUCT_FIELD(u8_t op); 71 PACK_STRUCT_FIELD(u8_t htype); 72 PACK_STRUCT_FIELD(u8_t hlen); 73 PACK_STRUCT_FIELD(u8_t hops); 74 PACK_STRUCT_FIELD(u32_t xid); 75 PACK_STRUCT_FIELD(u16_t secs); 76 PACK_STRUCT_FIELD(u16_t flags); 77 PACK_STRUCT_FIELD(struct ip_addr ciaddr); 78 PACK_STRUCT_FIELD(struct ip_addr yiaddr); 79 PACK_STRUCT_FIELD(struct ip_addr siaddr); 80 PACK_STRUCT_FIELD(struct ip_addr giaddr); 81 #define DHCP_CHADDR_LEN 16U 82 PACK_STRUCT_FIELD(u8_t chaddr[DHCP_CHADDR_LEN]); 83 #define DHCP_SNAME_LEN 64U 84 PACK_STRUCT_FIELD(u8_t sname[DHCP_SNAME_LEN]); 85 #define DHCP_FILE_LEN 128U 86 PACK_STRUCT_FIELD(u8_t file[DHCP_FILE_LEN]); 87 PACK_STRUCT_FIELD(u32_t cookie); 88 #define DHCP_MIN_OPTIONS_LEN 68U 89 /** make sure user does not configure this too small */ 90 #if ((defined(DHCP_OPTIONS_LEN)) && (DHCP_OPTIONS_LEN < DHCP_MIN_OPTIONS_LEN)) 91 # undef DHCP_OPTIONS_LEN 92 #endif 93 /** allow this to be configured in lwipopts.h, but not too small */ 94 #if (!defined(DHCP_OPTIONS_LEN)) 95 /** set this to be sufficient for your options in outgoing DHCP msgs */ 96 # define DHCP_OPTIONS_LEN DHCP_MIN_OPTIONS_LEN 97 #endif 98 PACK_STRUCT_FIELD(u8_t options[DHCP_OPTIONS_LEN]); 99 } PACK_STRUCT_STRUCT; 100 PACK_STRUCT_END 101 #ifdef PACK_STRUCT_USE_INCLUDES 102 # include "arch/epstruct.h" 103 #endif 104 105 /** start DHCP configuration */ 106 err_t dhcp_start(struct netif *netif); 107 /** enforce early lease renewal (not needed normally)*/ 108 err_t dhcp_renew(struct netif *netif); 109 /** release the DHCP lease, usually called before dhcp_stop()*/ 110 err_t dhcp_release(struct netif *netif); 111 /** stop DHCP configuration */ 112 void dhcp_stop(struct netif *netif); 113 /** inform server of our manual IP address */ 114 void dhcp_inform(struct netif *netif); 115 116 /** if enabled, check whether the offered IP address is not in use, using ARP */ 117 #if DHCP_DOES_ARP_CHECK 118 void dhcp_arp_reply(struct netif *netif, struct ip_addr *addr); 119 #endif 120 121 /** to be called every minute */ 122 void dhcp_coarse_tmr(void); 123 /** to be called every half second */ 124 void dhcp_fine_tmr(void); 125 126 /** DHCP message item offsets and length */ 127 #define DHCP_MSG_OFS (UDP_DATA_OFS) 128 #define DHCP_OP_OFS (DHCP_MSG_OFS + 0) 129 #define DHCP_HTYPE_OFS (DHCP_MSG_OFS + 1) 130 #define DHCP_HLEN_OFS (DHCP_MSG_OFS + 2) 131 #define DHCP_HOPS_OFS (DHCP_MSG_OFS + 3) 132 #define DHCP_XID_OFS (DHCP_MSG_OFS + 4) 133 #define DHCP_SECS_OFS (DHCP_MSG_OFS + 8) 134 #define DHCP_FLAGS_OFS (DHCP_MSG_OFS + 10) 135 #define DHCP_CIADDR_OFS (DHCP_MSG_OFS + 12) 136 #define DHCP_YIADDR_OFS (DHCP_MSG_OFS + 16) 137 #define DHCP_SIADDR_OFS (DHCP_MSG_OFS + 20) 138 #define DHCP_GIADDR_OFS (DHCP_MSG_OFS + 24) 139 #define DHCP_CHADDR_OFS (DHCP_MSG_OFS + 28) 140 #define DHCP_SNAME_OFS (DHCP_MSG_OFS + 44) 141 #define DHCP_FILE_OFS (DHCP_MSG_OFS + 108) 142 #define DHCP_MSG_LEN 236 143 144 #define DHCP_COOKIE_OFS (DHCP_MSG_OFS + DHCP_MSG_LEN) 145 #define DHCP_OPTIONS_OFS (DHCP_MSG_OFS + DHCP_MSG_LEN + 4) 146 147 #define DHCP_CLIENT_PORT 68 148 #define DHCP_SERVER_PORT 67 149 150 /** DHCP client states */ 151 #define DHCP_REQUESTING 1 152 #define DHCP_INIT 2 153 #define DHCP_REBOOTING 3 154 #define DHCP_REBINDING 4 155 #define DHCP_RENEWING 5 156 #define DHCP_SELECTING 6 157 #define DHCP_INFORMING 7 158 #define DHCP_CHECKING 8 159 #define DHCP_PERMANENT 9 160 #define DHCP_BOUND 10 161 /** not yet implemented #define DHCP_RELEASING 11 */ 162 #define DHCP_BACKING_OFF 12 163 #define DHCP_OFF 13 164 165 #define DHCP_BOOTREQUEST 1 166 #define DHCP_BOOTREPLY 2 167 168 #define DHCP_DISCOVER 1 169 #define DHCP_OFFER 2 170 #define DHCP_REQUEST 3 171 #define DHCP_DECLINE 4 172 #define DHCP_ACK 5 173 #define DHCP_NAK 6 174 #define DHCP_RELEASE 7 175 #define DHCP_INFORM 8 176 177 #define DHCP_HTYPE_ETH 1 178 179 #define DHCP_HLEN_ETH 6 180 181 #define DHCP_BROADCAST_FLAG 15 182 #define DHCP_BROADCAST_MASK (1 << DHCP_FLAG_BROADCAST) 183 184 /** BootP options */ 185 #define DHCP_OPTION_PAD 0 186 #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */ 187 #define DHCP_OPTION_ROUTER 3 188 #define DHCP_OPTION_DNS_SERVER 6 189 #define DHCP_OPTION_HOSTNAME 12 190 #define DHCP_OPTION_IP_TTL 23 191 #define DHCP_OPTION_MTU 26 192 #define DHCP_OPTION_BROADCAST 28 193 #define DHCP_OPTION_TCP_TTL 37 194 #define DHCP_OPTION_END 255 195 196 /** DHCP options */ 197 #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */ 198 #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */ 199 #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */ 200 201 #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */ 202 #define DHCP_OPTION_MESSAGE_TYPE_LEN 1 203 204 #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */ 205 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */ 206 207 #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */ 208 #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2 209 210 #define DHCP_OPTION_T1 58 /* T1 renewal time */ 211 #define DHCP_OPTION_T2 59 /* T2 rebinding time */ 212 #define DHCP_OPTION_CLIENT_ID 61 213 #define DHCP_OPTION_TFTP_SERVERNAME 66 214 #define DHCP_OPTION_BOOTFILE 67 215 216 /** possible combinations of overloading the file and sname fields with options */ 217 #define DHCP_OVERLOAD_NONE 0 218 #define DHCP_OVERLOAD_FILE 1 219 #define DHCP_OVERLOAD_SNAME 2 220 #define DHCP_OVERLOAD_SNAME_FILE 3 221 222 #endif /*__LWIP_DHCP_H__*/ 223