6a2f029387cc6048be94167371318b33f8bfef68
[pub/USBasp.git] / Projects / Webserver / Lib / DHCPClientApp.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 #if defined(ENABLE_DHCP_CLIENT) || defined(__DOXYGEN__)
32
33 /** \file
34 *
35 * DHCP Client Application. When connected to the uIP stack, this will retrieve IP configuration settings from the
36 * DHCP server on the network.
37 */
38
39 #define INCLUDE_FROM_DHCPCLIENTAPP_C
40 #include "DHCPClientApp.h"
41
42 /** Initialization function for the DHCP client. */
43 void DHCPClientApp_Init(void)
44 {
45 /* Create a new UDP connection to the DHCP server port for the DHCP solicitation */
46 struct uip_udp_conn* Connection = uip_udp_new(&uip_broadcast_addr, HTONS(DHCP_SERVER_PORT));
47
48 /* If the connection was successfully created, bind it to the local DHCP client port */
49 if (Connection != NULL)
50 {
51 uip_udp_appstate_t* const AppState = &Connection->appstate;
52 uip_udp_bind(Connection, HTONS(DHCP_CLIENT_PORT));
53
54 /* Set the initial client state */
55 AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover;
56
57 /* Set timeout period to half a second for a DHCP server to respond */
58 timer_set(&AppState->DHCPClient.Timeout, CLOCK_SECOND / 2);
59 }
60 }
61
62 /** uIP stack application callback for the DHCP client. This function must be called each time the TCP/IP stack
63 * needs a UDP packet to be processed.
64 */
65 void DHCPClientApp_Callback(void)
66 {
67 uip_udp_appstate_t* const AppState = &uip_udp_conn->appstate;
68 DHCP_Header_t* const AppData = (DHCP_Header_t*)uip_appdata;
69 uint16_t AppDataSize = 0;
70
71 switch (AppState->DHCPClient.CurrentState)
72 {
73 case DHCP_STATE_SendDiscover:
74 /* Clear all DHCP settings, reset client IP address */
75 memset(&AppState->DHCPClient.DHCPOffer_Data, 0x00, sizeof(AppState->DHCPClient.DHCPOffer_Data));
76 uip_sethostaddr((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.AllocatedIP);
77
78 /* Fill out the DHCP response header */
79 AppDataSize += DHCPClientApp_FillDHCPHeader(AppData, DHCP_DISCOVER, AppState);
80
81 /* Add the required DHCP options list to the packet */
82 uint8_t RequiredOptionList[] = {DHCP_OPTION_SUBNET_MASK, DHCP_OPTION_ROUTER, DHCP_OPTION_DNS_SERVER};
83 AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_REQ_LIST, sizeof(RequiredOptionList),
84 RequiredOptionList);
85
86 /* Send the DHCP DISCOVER packet */
87 uip_udp_send(AppDataSize);
88
89 /* Reset the timeout timer, progress to next state */
90 timer_reset(&AppState->DHCPClient.Timeout);
91 AppState->DHCPClient.CurrentState = DHCP_STATE_WaitForOffer;
92
93 break;
94 case DHCP_STATE_WaitForOffer:
95 if (!(uip_newdata()))
96 {
97 /* Check if the DHCP timeout period has expired while waiting for a response */
98 if (timer_expired(&AppState->DHCPClient.Timeout))
99 AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover;
100
101 break;
102 }
103
104 uint8_t OfferResponse_MessageType;
105 if ((AppData->TransactionID == DHCP_TRANSACTION_ID) &&
106 DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_MSG_TYPE, &OfferResponse_MessageType) &&
107 (OfferResponse_MessageType == DHCP_OFFER))
108 {
109 /* Received a DHCP offer for an IP address, copy over values for later request */
110 memcpy(&AppState->DHCPClient.DHCPOffer_Data.AllocatedIP, &AppData->YourIP, sizeof(uip_ipaddr_t));
111 DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_SUBNET_MASK, &AppState->DHCPClient.DHCPOffer_Data.Netmask);
112 DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_ROUTER, &AppState->DHCPClient.DHCPOffer_Data.GatewayIP);
113 DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_SERVER_ID, &AppState->DHCPClient.DHCPOffer_Data.ServerIP);
114
115 timer_reset(&AppState->DHCPClient.Timeout);
116 AppState->DHCPClient.CurrentState = DHCP_STATE_SendRequest;
117 }
118
119 break;
120 case DHCP_STATE_SendRequest:
121 /* Fill out the DHCP response header */
122 AppDataSize += DHCPClientApp_FillDHCPHeader(AppData, DHCP_REQUEST, AppState);
123
124 /* Add the DHCP REQUESTED IP ADDRESS option to the packet */
125 AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_REQ_IPADDR, sizeof(uip_ipaddr_t),
126 &AppState->DHCPClient.DHCPOffer_Data.AllocatedIP);
127
128 /* Add the DHCP SERVER IP ADDRESS option to the packet */
129 AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_SERVER_ID, sizeof(uip_ipaddr_t),
130 &AppState->DHCPClient.DHCPOffer_Data.ServerIP);
131
132 /* Send the DHCP REQUEST packet */
133 uip_udp_send(AppDataSize);
134
135 /* Reset the timeout timer, progress to next state */
136 timer_reset(&AppState->DHCPClient.Timeout);
137 AppState->DHCPClient.CurrentState = DHCP_STATE_WaitForACK;
138
139 break;
140 case DHCP_STATE_WaitForACK:
141 if (!(uip_newdata()))
142 {
143 /* Check if the DHCP timeout period has expired while waiting for a response */
144 if (timer_expired(&AppState->DHCPClient.Timeout))
145 AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover;
146
147 break;
148 }
149
150 uint8_t RequestResponse_MessageType;
151 if ((AppData->TransactionID == DHCP_TRANSACTION_ID) &&
152 DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_MSG_TYPE, &RequestResponse_MessageType) &&
153 (RequestResponse_MessageType == DHCP_ACK))
154 {
155 /* Set the new network parameters from the DHCP server */
156 uip_sethostaddr((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.AllocatedIP);
157 uip_setnetmask((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.Netmask);
158 uip_setdraddr((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.GatewayIP);
159
160 /* Indicate to the user that we now have a valid IP configuration */
161 HaveIPConfiguration = true;
162
163 AppState->DHCPClient.CurrentState = DHCP_STATE_AddressLeased;
164 }
165
166 break;
167 }
168 }
169
170 /** Fills the DHCP packet response with the appropriate BOOTP header for DHCP. This fills out all the required
171 * fields, leaving only the additional DHCP options to be added to the packet before it is sent to the DHCP server.
172 *
173 * \param[out] DHCPHeader Location in the packet buffer where the BOOTP header should be written to
174 * \param[in] DHCPMessageType DHCP Message type, such as DHCP_DISCOVER
175 * \param[in] AppState Application state of the current UDP connection
176 *
177 * \return Size in bytes of the created DHCP packet
178 */
179 static uint16_t DHCPClientApp_FillDHCPHeader(DHCP_Header_t* const DHCPHeader,
180 const uint8_t DHCPMessageType,
181 uip_udp_appstate_t* const AppState)
182 {
183 /* Erase existing packet data so that we start will all 0x00 DHCP header data */
184 memset(DHCPHeader, 0, sizeof(DHCP_Header_t));
185
186 /* Fill out the DHCP packet header */
187 DHCPHeader->Operation = DHCP_OP_BOOTREQUEST;
188 DHCPHeader->HardwareType = DHCP_HTYPE_ETHERNET;
189 DHCPHeader->HardwareAddressLength = sizeof(MACAddress);
190 DHCPHeader->Hops = 0;
191 DHCPHeader->TransactionID = DHCP_TRANSACTION_ID;
192 DHCPHeader->ElapsedSeconds = 0;
193 DHCPHeader->Flags = HTONS(BOOTP_BROADCAST);
194 memcpy(&DHCPHeader->ClientIP, &uip_hostaddr, sizeof(uip_ipaddr_t));
195 memcpy(&DHCPHeader->YourIP, &AppState->DHCPClient.DHCPOffer_Data.AllocatedIP, sizeof(uip_ipaddr_t));
196 memcpy(&DHCPHeader->NextServerIP, &AppState->DHCPClient.DHCPOffer_Data.ServerIP, sizeof(uip_ipaddr_t));
197 memcpy(&DHCPHeader->ClientHardwareAddress, &MACAddress, sizeof(struct uip_eth_addr));
198 DHCPHeader->Cookie = DHCP_MAGIC_COOKIE;
199
200 /* Add a DHCP message type and terminator options to the start of the DHCP options field */
201 DHCPHeader->Options[0] = DHCP_OPTION_MSG_TYPE;
202 DHCPHeader->Options[1] = 1;
203 DHCPHeader->Options[2] = DHCPMessageType;
204 DHCPHeader->Options[3] = DHCP_OPTION_END;
205
206 /* Calculate the total number of bytes added to the outgoing packet */
207 return (sizeof(DHCP_Header_t) + 4);
208 }
209
210 #endif
211