1 2 #ifndef __DHCPS_H__ 3 #define __DHCPS_H__ 4 5 #include "lwip/arch.h" 6 #include "lwip/netif.h" 7 #include "lwip/udp.h" 8 #include "lwip/stats.h" 9 #include "lwip/sys.h" 10 11 #define CONFIG_DHCPS_KEPT_CLIENT_INFO 12 13 #define DHCP_POOL_START 100 14 #define DHCP_POOL_END 200 15 16 #define DHCPS_MAX_CLIENT_NUM (DHCP_POOL_END-DHCP_POOL_START+1) 17 18 #define IS_USE_FIXED_IP 0 19 #define debug_dhcps 1 20 21 /* dhcp server states */ 22 #define DHCP_SERVER_STATE_OFFER (1) 23 #define DHCP_SERVER_STATE_DECLINE (2) 24 #define DHCP_SERVER_STATE_ACK (3) 25 #define DHCP_SERVER_STATE_NAK (4) 26 #define DHCP_SERVER_STATE_IDLE (5) 27 28 29 #define BOOTP_BROADCAST (0x8000) 30 31 #define DHCP_MESSAGE_OP_REQUEST (1) 32 #define DHCP_MESSAGE_OP_REPLY (2) 33 34 #define DHCP_MESSAGE_HTYPE (1) 35 #define DHCP_MESSAGE_HLEN (6) 36 37 #define DHCP_SERVER_PORT (67) 38 #define DHCP_CLIENT_PORT (68) 39 40 #define DHCP_MESSAGE_TYPE_DISCOVER (1) 41 #define DHCP_MESSAGE_TYPE_OFFER (2) 42 #define DHCP_MESSAGE_TYPE_REQUEST (3) 43 #define DHCP_MESSAGE_TYPE_DECLINE (4) 44 #define DHCP_MESSAGE_TYPE_ACK (5) 45 #define DHCP_MESSAGE_TYPE_NAK (6) 46 #define DHCP_MESSAGE_TYPE_RELEASE (7) 47 48 #define DHCP_OPTION_LENGTH_ONE (1) 49 #define DHCP_OPTION_LENGTH_TWO (2) 50 #define DHCP_OPTION_LENGTH_THREE (3) 51 #define DHCP_OPTION_LENGTH_FOUR (4) 52 #ifndef DHCP_MSG_LEN 53 #define DHCP_MSG_LEN 236 54 #endif 55 #define DHCP_OFFER_OPTION_TOTAL_LENGTH_MAX 312 //(51)= 4(magic)+3(type)+44(option code: 1,3,6,51,54,28,26,32,end) 56 57 #define DHCP_OPTION_CODE_SUBNET_MASK (1) 58 #define DHCP_OPTION_CODE_ROUTER (3) 59 #define DHCP_OPTION_CODE_DNS_SERVER (6) 60 #define DHCP_OPTION_CODE_INTERFACE_MTU (26) 61 #define DHCP_OPTION_CODE_BROADCAST_ADDRESS (28) 62 #define DHCP_OPTION_CODE_PERFORM_ROUTER_DISCOVERY (31) 63 #define DHCP_OPTION_CODE_REQUEST_IP_ADDRESS (50) 64 #define DHCP_OPTION_CODE_LEASE_TIME (51) 65 #define DHCP_OPTION_CODE_MSG_TYPE (53) 66 #define DHCP_OPTION_CODE_SERVER_ID (54) 67 #define DHCP_OPTION_CODE_REQ_LIST (55) 68 #define DHCP_OPTION_CODE_END (255) 69 70 #define IP_FREE_TO_USE (1) 71 #define IP_ALREADY_IN_USE (0) 72 73 #define HW_ADDRESS_LENGTH (6) 74 75 /* Reference by RFC 2131 */ 76 struct dhcp_msg { 77 uint8_t op; /* Message op code/message type. 1 = BOOTREQUEST, 2 = BOOTREPLY */ 78 uint8_t htype; /* Hardware address type */ 79 uint8_t hlen; /* Hardware address length */ 80 uint8_t hops; /* Client sets to zero, optionally used by relay agents 81 when booting via a relay agent */ 82 uint8_t xid[4]; /* Transaction ID, a random number chosen by the client, 83 used by the client and server to associate messages and 84 responses between a client and a server */ 85 uint16_t secs; /* Filled in by client, seconds elapsed since client began address 86 acquisition or renewal process.*/ 87 uint16_t flags; /* bit 0: Broadcast flag, bit 1~15:MBZ must 0*/ 88 uint8_t ciaddr[4]; /* Client IP address; only filled in if client is in BOUND, 89 RENEW or REBINDING state and can respond to ARP requests. */ 90 uint8_t yiaddr[4]; /* 'your' (client) IP address */ 91 uint8_t siaddr[4]; /* IP address of next server to use in bootstrap; 92 returned in DHCPOFFER, DHCPACK by server. */ 93 uint8_t giaddr[4]; /* Relay agent IP address, used in booting via a relay agent.*/ 94 uint8_t chaddr[16]; /* Client hardware address */ 95 uint8_t sname[64]; /* Optional server host name, null terminated string.*/ 96 uint8_t file[128]; /* Boot file name, null terminated string; "generic" name or 97 null in DHCPDISCOVER, fully qualified directory-path name in DHCPOFFER.*/ 98 uint8_t options[312]; /* Optional parameters field. reference the RFC 2132 */ 99 }; 100 101 /* use this to check whether the message is dhcp related or not */ 102 static const uint8_t dhcp_magic_cookie[4] = {99, 130, 83, 99}; 103 static const uint8_t dhcp_option_lease_time[] = {0x00, 0x00, 0x1c, 0x20}; //1 day 104 //static const uint8_t dhcp_option_lease_time[] = {0x00, 0x00, 0x0e, 0x10}; // one hour 105 //static const uint8_t dhcp_option_interface_mtu_576[] = {0x02, 0x40}; 106 static const uint8_t dhcp_option_interface_mtu[] = {0x05, 0xDC}; 107 108 struct table { 109 uint32_t ip_range[8]; 110 #ifdef CONFIG_DHCPS_KEPT_CLIENT_INFO 111 uint8_t client_mac[256][6]; 112 #endif 113 }; 114 115 struct address_pool{ 116 uint32_t start; 117 uint32_t end; 118 }; 119 120 /* 01~32 */ 121 #define MARK_RANGE1_IP_BIT(table, ip) ((table.ip_range[0]) | (1 << ((ip) - 1))) 122 /* 33~64 */ 123 #define MARK_RANGE2_IP_BIT(table, ip) ((table.ip_range[1]) | (1 << ((ip) - 1))) 124 /* 65~96 */ 125 #define MARK_RANGE3_IP_BIT(table, ip) ((table.ip_range[2]) | (1 << ((ip) - 1))) 126 /* 97~128 */ 127 #define MARK_RANGE4_IP_BIT(table, ip) ((table.ip_range[3]) | (1 << ((ip) - 1))) 128 /* 129~160 */ 129 #define MARK_RANGE5_IP_BIT(table, ip) ((table.ip_range[4]) | (1 << ((ip) - 1))) 130 /* 161~192 */ 131 #define MARK_RANGE6_IP_BIT(table, ip) ((table.ip_range[5]) | (1 << ((ip) - 1))) 132 /* 193~224 */ 133 #define MARK_RANGE7_IP_BIT(table, ip) ((table.ip_range[6]) | (1 << ((ip) - 1))) 134 /* 225~255 */ 135 #define MARK_RANGE8_IP_BIT(table, ip) ((table.ip_range[7]) | (1 << ((ip) - 1))) 136 137 /* expose API */ 138 void dhcps_set_addr_pool(int addr_pool_set, ip_addr_t * addr_pool_start, ip_addr_t *addr_pool_end); 139 void dhcps_init(struct netif * pnetif); 140 void dhcps_deinit(void); 141 void dhcps_test(void); 142 143 extern struct netif *netif_default; 144 145 #endif 146