00001 /*! \file dhcp.h \brief DHCP Protocol Library. */ 00002 //***************************************************************************** 00003 // 00004 // File Name : 'dhcp.h' 00005 // Title : DHCP Protocol Library 00006 // Author : Pascal Stang 00007 // Created : 9/17/2005 00008 // Revised : 9/17/2005 00009 // Version : 0.1 00010 // Target MCU : Atmel AVR series 00011 // Editor Tabs : 4 00012 // 00013 /// \ingroup network 00014 /// \defgroup dhcp DHCP Protocol Library (dhcp.c) 00015 /// \code #include "net/dhcp.h" \endcode 00016 /// \par Description 00017 /// This library provides a limited implementation of DHCP (Dynamic Host 00018 /// Configuration Protocol) as described in RFC2131. DHCP allows a 00019 /// network device to automatically obtain an IP address and other network 00020 /// configuration settings from a DHCP server. 00021 /// 00022 /// \note This code is currently below version 1.0, and therefore is considered 00023 /// to be lacking in some functionality or documentation, or may not be fully 00024 /// tested. Nonetheless, you can expect most functions to work. 00025 /// 00026 // This code is distributed under the GNU Public License 00027 // which can be found at http://www.gnu.org/licenses/gpl.txt 00028 //***************************************************************************** 00029 //@{ 00030 00031 #ifndef DHCP_H 00032 #define DHCP_H 00033 00034 #include "global.h" 00035 #include "net.h" 00036 00037 //#define DHCP_DEBUG_PRINT 00038 //#define DHCP_DEBUG 00039 00040 /// Bootp Header (DHCP is transported by BOOTP/UDP/IP) 00041 struct netBootpHeader 00042 { 00043 uint8_t op; ///< Message op-code / message type 00044 uint8_t htype; ///< Hardware address type (Ethernet=1) 00045 uint8_t hlen; ///< Hardware address length (Ethernet=6 byte MAC addr) 00046 uint8_t hops; ///< hop count (client set to zero) 00047 uint32_t xid; ///< Transaction ID (randomly chosen by client, must remain same) 00048 uint16_t secs; ///< Seconds elapsed since DHCP negotiation began (filled by client) 00049 uint16_t flags; ///< Flags 00050 uint32_t ciaddr; ///< Client IP address (filled only if already bound, renewing, or rebinding) 00051 uint32_t yiaddr; ///< 'Your' IP address (client) 00052 uint32_t siaddr; ///< Server IP address 00053 uint32_t giaddr; ///< Gateway IP address 00054 uint8_t chaddr[16]; ///< Client Hardware Address 00055 uint8_t sname[64]; ///< Server Host Name 00056 uint8_t file[128]; ///< Boot file name (null-term string) 00057 } GNUC_PACKED; 00058 00059 #define BOOTP_HEADER_LEN 236 ///< length of BOOTP header not including options 00060 00061 #define BOOTP_OP_BOOTREQUEST 1 ///< BOOTP Request operation (message from client to server) 00062 #define BOOTP_OP_BOOTREPLY 2 ///< BOOTP Reply operation (message from server to client) 00063 00064 #define BOOTP_HTYPE_ETHERNET 1 00065 #define BOOTP_HLEN_ETHERNET 6 00066 00067 /// DHCP Header 00068 struct netDhcpHeader 00069 { 00070 struct netBootpHeader bootp; ///< BOOTP header 00071 uint32_t cookie; ///< magic cookie value 00072 uint8_t options[]; ///< DHCP options 00073 } GNUC_PACKED; 00074 00075 #define DHCP_HEADER_LEN 240 ///< length of DHCP header not including options 00076 00077 #define DHCP_UDP_SERVER_PORT 67 ///< UDP port where DHCP requests should be sent 00078 #define DHCP_UDP_CLIENT_PORT 68 ///< UDP port clients will receive DHCP replies 00079 00080 00081 #define DHCP_OPT_PAD 0 ///< token padding value (make be skipped) 00082 #define DHCP_OPT_NETMASK 1 ///< subnet mask client should use (4 byte mask) 00083 #define DHCP_OPT_ROUTERS 3 ///< routers client should use (IP addr list) 00084 #define DHCP_OPT_TIMESERVERS 4 ///< time servers client should use (IP addr list) 00085 #define DHCP_OPT_NAMESERVERS 5 ///< name servers client should use (IP addr list) 00086 #define DHCP_OPT_DNSSERVERS 6 ///< DNS servers client should use (IP addr list) 00087 #define DHCP_OPT_HOSTNAME 12 ///< host name client should use (string) 00088 #define DHCP_OPT_DOMAINNAME 15 ///< domain name client should use (string) 00089 #define DHCP_OPT_REQUESTEDIP 50 ///< IP address requested by client (IP address) 00090 #define DHCP_OPT_LEASETIME 51 ///< DHCP Lease Time (uint32 seconds) 00091 #define DHCP_OPT_DHCPMSGTYPE 53 ///< DHCP message type (1 byte) 00092 #define DHCP_OPT_SERVERID 54 ///< Server Identifier (IP address) 00093 #define DHCP_OPT_PARAMREQLIST 55 ///< Paramerter Request List (n OPT codes) 00094 #define DHCP_OPT_RENEWALTIME 58 ///< DHCP Lease Renewal Time (uint32 seconds) 00095 #define DHCP_OPT_REBINDTIME 59 ///< DHCP Lease Rebinding Time (uint32 seconds) 00096 #define DHCP_OPT_END 255 ///< token end value (marks end of options list) 00097 00098 #define DHCP_MSG_DHCPDISCOVER 1 ///< DISCOVER is broadcast by client to solicit OFFER from any/all DHCP servers. 00099 #define DHCP_MSG_DHCPOFFER 2 ///< OFFER(s) are made to client by server to offer IP address and config info. 00100 #define DHCP_MSG_DHCPREQUEST 3 ///< REQUEST is made my client in response to best/favorite OFFER message. 00101 #define DHCP_MSG_DHCPDECLINE 4 ///< DECLINE may be sent by client to server to indicate IP already in use. 00102 #define DHCP_MSG_DHCPACK 5 ///< ACK is sent to client by server in confirmation of REQUEST, contains config and IP. 00103 #define DHCP_MSG_DHCPNAK 6 ///< NAK is sent to client by server to indicate problem with REQUEST. 00104 #define DHCP_MSG_DHCPRELEASE 7 ///< RELEASE is sent by client to server to relinquish DHCP lease on IP address, etc. 00105 #define DHCP_MSG_DHCPINFORM 8 ///< INFORM is sent by client to server to request config info, IP address configured locally. 00106 00107 00108 /*! Initialize DHCP system. 00109 Prepares DHCP for use and initializes lease time to zero. */ 00110 void dhcpInit(void); 00111 00112 /*! Processes incoming DHCP packets from UDP port 68. 00113 This function is to be called by the stack when a DHCP packet 00114 arrives over the network. The DHCP packet will be parsed, handled, 00115 and a response will be generated and sent if needed. When the DHCP 00116 process completes, the IP addressing will be automatically updated. */ 00117 void dhcpIn(unsigned int len, struct netDhcpHeader* packet); 00118 00119 /*! Request DHCP assigned network parameters. 00120 This function begins the DHCP process. The remainder of operations 00121 are handled in dhcpIn(). */ 00122 void dhcpRequest(void); 00123 00124 /*! Release DHCP lease and assigned network parameters. 00125 This function releases the DHCP assigned address and allows the 00126 DHCP server to reallocate it. */ 00127 void dhcpRelease(void); 00128 00129 /*! Periodic DHCP maintenance. 00130 This function is to be called once per second and will 00131 expire the DHCP lease. */ 00132 void dhcpTimer(void); 00133 00134 /*! Get a DHCP option from the option list. 00135 \param options is a pointer to the options field of a DHCP packet. 00136 \param optcode is the desired option number to retrieve. 00137 \param optlen is the maximum data length that should be retrieved (less data will be retrieved if option is shorter). 00138 \param optvalptr is a pointer to where the option value will be stored. 00139 \return actual length of the option data, as stored in the options list. */ 00140 uint8_t dhcpGetOption(uint8_t* options, uint8_t optcode, uint8_t optlen, void* optvalptr); 00141 00142 /*! Set a DHCP option in the option list. 00143 \param options is a pointer to the options field of a DHCP packet. 00144 \param optcode is the option number to write. 00145 \param optlen is the data length of the option value. 00146 \param optvalptr is a pointer to the option data to be read. 00147 \return pointer to write location of the next option. */ 00148 uint8_t* dhcpSetOption(uint8_t* options, uint8_t optcode, uint8_t optlen, void* optvalptr); 00149 00150 /*! Print diagnotic information about BOOTP/DHCP packet. 00151 */ 00152 void dhcpPrintHeader(struct netDhcpHeader* packet); 00153 00154 #endif 00155 //@} 00156