9e9faaa363e869246844d0bc69d330caae5779e6
[pub/USBasp.git] / Projects / Webserver / Lib / DHCPApp.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Header file for DHCPApp.c.
34 */
35
36 #ifndef _DHCP_APP_H_
37 #define _DHCP_APP_H_
38
39 /* Includes: */
40 #include <stdio.h>
41
42 #include <uip.h>
43
44 #include "../Webserver.h"
45
46 /* Macros: */
47 #define DHCPC_SERVER_PORT 67
48 #define DHCPC_CLIENT_PORT 68
49
50 #define DHCP_OP_BOOTREQUEST 0x01
51 #define DHCP_OP_BOOTREPLY 0x02
52
53 #define BOOTP_BROADCAST 0x8000
54
55 #define DHCP_MAGIC_COOKIE 0x63538263
56
57 #define DHCP_TRANSACTION_ID 0x13245466
58
59 #define DHCP_DISCOVER 1
60 #define DHCP_OFFER 2
61 #define DHCP_REQUEST 3
62 #define DHCP_DECLINE 4
63 #define DHCP_ACK 5
64 #define DHCP_NAK 6
65 #define DHCP_RELEASE 7
66
67 #define DHCP_HTYPE_ETHERNET 1
68
69 #define DHCP_OPTION_SUBNET_MASK 1
70 #define DHCP_OPTION_ROUTER 3
71 #define DHCP_OPTION_DNS_SERVER 6
72 #define DHCP_OPTION_REQ_IPADDR 50
73 #define DHCP_OPTION_LEASE_TIME 51
74 #define DHCP_OPTION_MSG_TYPE 53
75 #define DHCP_OPTION_SERVER_ID 54
76 #define DHCP_OPTION_REQ_LIST 55
77 #define DHCP_OPTION_END 255
78
79 /* Type Defines: */
80 /** Type define for a DHCP packet inside an Ethernet frame. */
81 typedef struct
82 {
83 uint8_t Operation; /**< DHCP operation, either DHCP_OP_BOOTREQUEST or DHCP_OP_BOOTREPLY */
84 uint8_t HardwareType; /**< Hardware carrier type constant */
85 uint8_t HardwareAddressLength; /**< Length in bytes of a hardware (MAC) address on the network */
86 uint8_t Hops; /**< Number of hops required to reach the server, unused */
87
88 uint32_t TransactionID; /**< Unique ID of the DHCP packet, for positive matching between sent and received packets */
89
90 uint16_t ElapsedSeconds; /**< Elapsed seconds since the request was made */
91 uint16_t Flags; /**< BOOTP packet flags */
92
93 uip_ipaddr_t ClientIP; /**< Client IP address, if already leased an IP */
94 uip_ipaddr_t YourIP; /**< Client IP address */
95 uip_ipaddr_t NextServerIP; /**< Legacy BOOTP protocol field, unused for DHCP */
96 uip_ipaddr_t RelayAgentIP; /**< Legacy BOOTP protocol field, unused for DHCP */
97
98 uint8_t ClientHardwareAddress[16]; /**< Hardware (MAC) address of the client making a request to the DHCP server */
99 uint8_t ServerHostnameString[64]; /**< Legacy BOOTP protocol field, unused for DHCP */
100 uint8_t BootFileName[128]; /**< Legacy BOOTP protocol field, unused for DHCP */
101
102 uint32_t Cookie; /**< Magic BOOTP protocol cookie to indicate a valid packet */
103
104 uint8_t Options[]; /** DHCP message options */
105 } DHCP_Header_t;
106
107 /* Enums: */
108 enum DHCP_States_t
109 {
110 DHCP_STATE_SendDiscover,
111 DHCP_STATE_WaitForResponse,
112 DHCP_STATE_SendRequest,
113 DHCP_STATE_WaitForACK,
114 DHCP_STATE_AddressLeased,
115 };
116
117 /* Function Prototypes: */
118 void DHCPApp_Init(void);
119 void DHCPApp_Callback(void);
120
121 uint16_t DHCPApp_FillDHCPHeader(DHCP_Header_t* DHCPHeader, uint8_t DHCPMessageType, uip_udp_appstate_t* AppState);
122 uint8_t DHCPApp_SetOption(uint8_t* DHCPOptionList, uint8_t Option, uint8_t DataLen, void* Source);
123 bool DHCPApp_GetOption(uint8_t* DHCPOptionList, uint8_t Option, void* Destination);
124
125 #endif