Add DHCP server to the Webserver demo for automatic network configuration. Correct...
[pub/lufa.git] / Projects / Webserver / Lib / DHCPApp.h
diff --git a/Projects/Webserver/Lib/DHCPApp.h b/Projects/Webserver/Lib/DHCPApp.h
new file mode 100644 (file)
index 0000000..9e9faaa
--- /dev/null
@@ -0,0 +1,125 @@
+/*\r
+             LUFA Library\r
+     Copyright (C) Dean Camera, 2010.\r
+              \r
+  dean [at] fourwalledcubicle [dot] com\r
+      www.fourwalledcubicle.com\r
+*/\r
+\r
+/*\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
+  software without specific, written prior permission.\r
+\r
+  The author disclaim all warranties with regard to this\r
+  software, including all implied warranties of merchantability\r
+  and fitness.  In no event shall the author be liable for any\r
+  special, indirect or consequential damages or any damages\r
+  whatsoever resulting from loss of use, data or profits, whether\r
+  in an action of contract, negligence or other tortious action,\r
+  arising out of or in connection with the use or performance of\r
+  this software.\r
+*/\r
+\r
+/** \file\r
+ *\r
+ *  Header file for DHCPApp.c.\r
+ */\r
+\r
+#ifndef _DHCP_APP_H_\r
+#define _DHCP_APP_H_\r
+\r
+       /* Includes: */\r
+               #include <stdio.h>\r
+               \r
+               #include <uip.h>\r
+               \r
+               #include "../Webserver.h"\r
+               \r
+       /* Macros: */\r
+               #define DHCPC_SERVER_PORT         67\r
+               #define DHCPC_CLIENT_PORT         68\r
+\r
+               #define DHCP_OP_BOOTREQUEST       0x01\r
+               #define DHCP_OP_BOOTREPLY         0x02\r
+\r
+               #define BOOTP_BROADCAST           0x8000\r
+               \r
+               #define DHCP_MAGIC_COOKIE         0x63538263\r
+               \r
+               #define DHCP_TRANSACTION_ID       0x13245466\r
+\r
+               #define DHCP_DISCOVER             1\r
+               #define DHCP_OFFER                2\r
+               #define DHCP_REQUEST              3\r
+               #define DHCP_DECLINE              4\r
+               #define DHCP_ACK                  5\r
+               #define DHCP_NAK                  6\r
+               #define DHCP_RELEASE              7\r
+\r
+               #define DHCP_HTYPE_ETHERNET       1\r
+\r
+               #define DHCP_OPTION_SUBNET_MASK   1\r
+               #define DHCP_OPTION_ROUTER        3\r
+               #define DHCP_OPTION_DNS_SERVER    6\r
+               #define DHCP_OPTION_REQ_IPADDR    50\r
+               #define DHCP_OPTION_LEASE_TIME    51\r
+               #define DHCP_OPTION_MSG_TYPE      53\r
+               #define DHCP_OPTION_SERVER_ID     54\r
+               #define DHCP_OPTION_REQ_LIST      55\r
+               #define DHCP_OPTION_END           255\r
+\r
+       /* Type Defines: */\r
+               /** Type define for a DHCP packet inside an Ethernet frame. */\r
+               typedef struct\r
+               {\r
+                       uint8_t  Operation; /**< DHCP operation, either DHCP_OP_BOOTREQUEST or DHCP_OP_BOOTREPLY */\r
+                       uint8_t  HardwareType; /**< Hardware carrier type constant */\r
+                       uint8_t  HardwareAddressLength;  /**< Length in bytes of a hardware (MAC) address on the network */\r
+                       uint8_t  Hops; /**< Number of hops required to reach the server, unused */\r
+\r
+                       uint32_t TransactionID; /**< Unique ID of the DHCP packet, for positive matching between sent and received packets */\r
+\r
+                       uint16_t ElapsedSeconds; /**< Elapsed seconds since the request was made */\r
+                       uint16_t Flags; /**< BOOTP packet flags */\r
+                       \r
+                       uip_ipaddr_t ClientIP; /**< Client IP address, if already leased an IP */\r
+                       uip_ipaddr_t YourIP; /**< Client IP address */\r
+                       uip_ipaddr_t NextServerIP; /**< Legacy BOOTP protocol field, unused for DHCP */\r
+                       uip_ipaddr_t RelayAgentIP; /**< Legacy BOOTP protocol field, unused for DHCP */\r
+                       \r
+                       uint8_t ClientHardwareAddress[16]; /**< Hardware (MAC) address of the client making a request to the DHCP server */\r
+                       uint8_t ServerHostnameString[64]; /**< Legacy BOOTP protocol field, unused for DHCP */\r
+                       uint8_t BootFileName[128]; /**< Legacy BOOTP protocol field, unused for DHCP */\r
+                       \r
+                       uint32_t Cookie; /**< Magic BOOTP protocol cookie to indicate a valid packet */\r
+                       \r
+                       uint8_t  Options[]; /** DHCP message options */\r
+               } DHCP_Header_t;\r
+\r
+       /* Enums: */\r
+               enum DHCP_States_t\r
+               {\r
+                       DHCP_STATE_SendDiscover,\r
+                       DHCP_STATE_WaitForResponse,\r
+                       DHCP_STATE_SendRequest,\r
+                       DHCP_STATE_WaitForACK,\r
+                       DHCP_STATE_AddressLeased,\r
+               };\r
+\r
+       /* Function Prototypes: */\r
+               void DHCPApp_Init(void);\r
+               void DHCPApp_Callback(void);\r
+               \r
+               uint16_t DHCPApp_FillDHCPHeader(DHCP_Header_t* DHCPHeader, uint8_t DHCPMessageType, uip_udp_appstate_t* AppState);\r
+               uint8_t  DHCPApp_SetOption(uint8_t* DHCPOptionList, uint8_t Option, uint8_t DataLen, void* Source);\r
+               bool     DHCPApp_GetOption(uint8_t* DHCPOptionList, uint8_t Option, void* Destination);\r
+               \r
+#endif\r