dhcp.h 7.9 KB

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