dhcp_def.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef __ETHERNET_COMMON_DHCP_DEF_H__
  2. #define __ETHERNET_COMMON_DHCP_DEF_H__
  3. #include "luat_base.h"
  4. #ifdef LUAT_USE_DHCP
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #define DHCP_CLIENT_PORT 68
  9. #define DHCP_SERVER_PORT 67
  10. /* DHCP message item offsets and length */
  11. #define DHCP_CHADDR_LEN 16U
  12. #define DHCP_SNAME_OFS 44U
  13. #define DHCP_SNAME_LEN 64U
  14. #define DHCP_FILE_OFS 108U
  15. #define DHCP_FILE_LEN 128U
  16. #define DHCP_MSG_LEN 236U
  17. #define DHCP_OPTIONS_OFS (DHCP_MSG_LEN + 4U) /* 4 byte: cookie */
  18. #define DHCP_MIN_OPTIONS_LEN 68U
  19. typedef struct
  20. {
  21. uint64_t lease_end_time;
  22. uint64_t lease_p1_time;
  23. uint64_t lease_p2_time;
  24. uint64_t last_tx_time;
  25. uint32_t server_ip;
  26. uint32_t temp_ip; //服务器给的动态IP
  27. uint32_t submask;
  28. uint32_t gateway;
  29. uint32_t ip; //当前的动态IP
  30. uint32_t dns_server[2];
  31. uint32_t xid;
  32. uint32_t lease_time;
  33. char name[32];
  34. uint8_t mac[6];
  35. uint8_t state;
  36. uint8_t discover_cnt;
  37. }dhcp_client_info_t;
  38. typedef struct
  39. {
  40. uint8_t *p;
  41. uint16_t len;
  42. uint8_t id;
  43. uint8_t unuse;
  44. }dhcp_option_t;
  45. /* DHCP client states */
  46. enum {
  47. DHCP_STATE_NOT_WORK = 0,
  48. DHCP_STATE_WAIT_LEASE_P1,
  49. DHCP_STATE_WAIT_LEASE_P1_ACK,
  50. DHCP_STATE_WAIT_LEASE_P2,
  51. DHCP_STATE_WAIT_LEASE_P2_ACK,
  52. DHCP_STATE_WAIT_LEASE_END,
  53. DHCP_STATE_DISCOVER,
  54. DHCP_STATE_WAIT_OFFER,
  55. DHCP_STATE_SELECT,
  56. DHCP_STATE_WAIT_SELECT_ACK,
  57. DHCP_STATE_REQUIRE,
  58. // DHCP_STATE_WAIT_REQUIRE_ACK,
  59. DHCP_STATE_DECLINE,
  60. DHCP_STATE_CHECK,
  61. };
  62. /* DHCP op codes */
  63. #define DHCP_BOOTREQUEST 1
  64. #define DHCP_BOOTREPLY 2
  65. /* DHCP message types */
  66. #define DHCP_DISCOVER 1
  67. #define DHCP_OFFER 2
  68. #define DHCP_REQUEST 3
  69. #define DHCP_DECLINE 4
  70. #define DHCP_ACK 5
  71. #define DHCP_NAK 6
  72. #define DHCP_RELEASE 7
  73. #define DHCP_INFORM 8
  74. /** DHCP hardware type, currently only ethernet is supported */
  75. #define DHCP_HTYPE_10MB 1
  76. #define DHCP_HTYPE_100MB 2
  77. #define DHCP_HTYPE_ETH DHCP_HTYPE_10MB
  78. #define DHCP_MAGIC_COOKIE 0x63825363UL
  79. /* This is a list of options for BOOTP and DHCP, see RFC 2132 for descriptions */
  80. /* BootP options */
  81. #define DHCP_OPTION_PAD 0
  82. #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */
  83. #define DHCP_OPTION_ROUTER 3
  84. #define DHCP_OPTION_DNS_SERVER 6
  85. #define DHCP_OPTION_HOSTNAME 12
  86. #define DHCP_OPTION_IP_TTL 23
  87. #define DHCP_OPTION_MTU 26
  88. #define DHCP_OPTION_BROADCAST 28
  89. #define DHCP_OPTION_TCP_TTL 37
  90. #define DHCP_OPTION_NTP 42
  91. #define DHCP_OPTION_END 255
  92. /* DHCP options */
  93. #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */
  94. #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
  95. #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */
  96. #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */
  97. #define DHCP_OPTION_MESSAGE_TYPE_LEN 1
  98. #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */
  99. #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */
  100. #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */
  101. #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2
  102. #define DHCP_OPTION_T1 58 /* T1 renewal time */
  103. #define DHCP_OPTION_T2 59 /* T2 rebinding time */
  104. #define DHCP_OPTION_US 60
  105. #define DHCP_OPTION_CLIENT_ID 61
  106. #define DHCP_OPTION_TFTP_SERVERNAME 66
  107. #define DHCP_OPTION_BOOTFILE 67
  108. /* possible combinations of overloading the file and sname fields with options */
  109. #define DHCP_OVERLOAD_NONE 0
  110. #define DHCP_OVERLOAD_FILE 1
  111. #define DHCP_OVERLOAD_SNAME 2
  112. #define DHCP_OVERLOAD_SNAME_FILE 3
  113. void make_ip4_dhcp_msg_base(dhcp_client_info_t *dhcp, uint16_t flag, Buffer_Struct *out);
  114. void ip4_dhcp_msg_add_bytes_option(uint8_t id, uint8_t *data, uint8_t len, Buffer_Struct *out);
  115. void ip4_dhcp_msg_add_ip_option(uint8_t id, uint32_t ip, Buffer_Struct *out);
  116. void ip4_dhcp_msg_add_integer_option(uint8_t id, uint8_t len, uint32_t value, Buffer_Struct *out);
  117. void make_ip4_dhcp_discover_msg(dhcp_client_info_t *dhcp, Buffer_Struct *out);
  118. void make_ip4_dhcp_select_msg(dhcp_client_info_t *dhcp, uint16_t flag, Buffer_Struct *out);
  119. void make_ip4_dhcp_decline_msg(dhcp_client_info_t *dhcp, Buffer_Struct *out);
  120. int analyze_ip4_dhcp(dhcp_client_info_t *dhcp, Buffer_Struct *in);
  121. int ip4_dhcp_run(dhcp_client_info_t *dhcp, Buffer_Struct *in, Buffer_Struct *out, uint32_t *remote_ip);
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif
  126. #endif