3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * DHCP Client Application. When connected to the uIP stack, this will retrieve IP configuration settings from the
34 * DHCP server on the network.
37 #include "DHCPClientApp.h"
39 #if defined(ENABLE_DHCP_CLIENT) || defined(__DOXYGEN__)
41 /** Initialization function for the DHCP client. */
42 void DHCPClientApp_Init(void)
44 /* Create an IP address to the broadcast network address */
45 uip_ipaddr_t DHCPServerIPAddress
;
46 uip_ipaddr(&DHCPServerIPAddress
, 255, 255, 255, 255);
48 /* Create a new UDP connection to the DHCP server port for the DHCP solicitation */
49 struct uip_udp_conn
* Connection
= uip_udp_new(&DHCPServerIPAddress
, HTONS(DHCPC_SERVER_PORT
));
51 /* If the connection was successfully created, bind it to the local DHCP client port */
52 if (Connection
!= NULL
)
54 uip_udp_appstate_t
* const AppState
= &Connection
->appstate
;
55 uip_udp_bind(Connection
, HTONS(DHCPC_CLIENT_PORT
));
57 /* Set the initial client state */
58 AppState
->DHCPClient
.CurrentState
= DHCP_STATE_SendDiscover
;
60 /* Set timeout period to half a second for a DHCP server to respond */
61 timer_set(&AppState
->DHCPClient
.Timeout
, CLOCK_SECOND
/ 2);
65 /** uIP stack application callback for the DHCP client. This function must be called each time the TCP/IP stack
66 * needs a UDP packet to be processed.
68 void DHCPClientApp_Callback(void)
70 uip_udp_appstate_t
* const AppState
= &uip_udp_conn
->appstate
;
71 DHCP_Header_t
* const AppData
= (DHCP_Header_t
*)uip_appdata
;
72 uint16_t AppDataSize
= 0;
74 switch (AppState
->DHCPClient
.CurrentState
)
76 case DHCP_STATE_SendDiscover
:
77 /* Clear all DHCP settings, reset client IP address */
78 memset(&AppState
->DHCPClient
.DHCPOffer_Data
, 0x00, sizeof(AppState
->DHCPClient
.DHCPOffer_Data
));
79 uip_sethostaddr((uip_ipaddr_t
*)&AppState
->DHCPClient
.DHCPOffer_Data
.AllocatedIP
);
81 /* Fill out the DHCP response header */
82 AppDataSize
+= DHCPClientApp_FillDHCPHeader(AppData
, DHCP_DISCOVER
, AppState
);
84 /* Add the required DHCP options list to the packet */
85 uint8_t RequiredOptionList
[] = {DHCP_OPTION_SUBNET_MASK
, DHCP_OPTION_ROUTER
, DHCP_OPTION_DNS_SERVER
};
86 AppDataSize
+= DHCPClientApp_SetOption(AppData
->Options
, DHCP_OPTION_REQ_LIST
, sizeof(RequiredOptionList
),
89 /* Send the DHCP DISCOVER packet */
90 uip_udp_send(AppDataSize
);
92 /* Reset the timeout timer, progress to next state */
93 timer_reset(&AppState
->DHCPClient
.Timeout
);
94 AppState
->DHCPClient
.CurrentState
= DHCP_STATE_WaitForOffer
;
97 case DHCP_STATE_WaitForOffer
:
100 /* Check if the DHCP timeout period has expired while waiting for a response */
101 if (timer_expired(&AppState
->DHCPClient
.Timeout
))
102 AppState
->DHCPClient
.CurrentState
= DHCP_STATE_SendDiscover
;
107 uint8_t OfferResponse_MessageType
;
108 if ((AppData
->TransactionID
== DHCP_TRANSACTION_ID
) &&
109 DHCPClientApp_GetOption(AppData
->Options
, DHCP_OPTION_MSG_TYPE
, &OfferResponse_MessageType
) &&
110 (OfferResponse_MessageType
== DHCP_OFFER
))
112 /* Received a DHCP offer for an IP address, copy over values for later request */
113 memcpy(&AppState
->DHCPClient
.DHCPOffer_Data
.AllocatedIP
, &AppData
->YourIP
, sizeof(uip_ipaddr_t
));
114 DHCPClientApp_GetOption(AppData
->Options
, DHCP_OPTION_SUBNET_MASK
, &AppState
->DHCPClient
.DHCPOffer_Data
.Netmask
);
115 DHCPClientApp_GetOption(AppData
->Options
, DHCP_OPTION_ROUTER
, &AppState
->DHCPClient
.DHCPOffer_Data
.GatewayIP
);
116 DHCPClientApp_GetOption(AppData
->Options
, DHCP_OPTION_SERVER_ID
, &AppState
->DHCPClient
.DHCPOffer_Data
.ServerIP
);
118 timer_reset(&AppState
->DHCPClient
.Timeout
);
119 AppState
->DHCPClient
.CurrentState
= DHCP_STATE_SendRequest
;
123 case DHCP_STATE_SendRequest
:
124 /* Fill out the DHCP response header */
125 AppDataSize
+= DHCPClientApp_FillDHCPHeader(AppData
, DHCP_REQUEST
, AppState
);
127 /* Add the DHCP REQUESTED IP ADDRESS option to the packet */
128 AppDataSize
+= DHCPClientApp_SetOption(AppData
->Options
, DHCP_OPTION_REQ_IPADDR
, sizeof(uip_ipaddr_t
),
129 &AppState
->DHCPClient
.DHCPOffer_Data
.AllocatedIP
);
131 /* Add the DHCP SERVER IP ADDRESS option to the packet */
132 AppDataSize
+= DHCPClientApp_SetOption(AppData
->Options
, DHCP_OPTION_SERVER_ID
, sizeof(uip_ipaddr_t
),
133 &AppState
->DHCPClient
.DHCPOffer_Data
.ServerIP
);
135 /* Send the DHCP REQUEST packet */
136 uip_udp_send(AppDataSize
);
138 /* Reset the timeout timer, progress to next state */
139 timer_reset(&AppState
->DHCPClient
.Timeout
);
140 AppState
->DHCPClient
.CurrentState
= DHCP_STATE_WaitForACK
;
143 case DHCP_STATE_WaitForACK
:
144 if (!(uip_newdata()))
146 /* Check if the DHCP timeout period has expired while waiting for a response */
147 if (timer_expired(&AppState
->DHCPClient
.Timeout
))
148 AppState
->DHCPClient
.CurrentState
= DHCP_STATE_SendDiscover
;
153 uint8_t RequestResponse_MessageType
;
154 if ((AppData
->TransactionID
== DHCP_TRANSACTION_ID
) &&
155 DHCPClientApp_GetOption(AppData
->Options
, DHCP_OPTION_MSG_TYPE
, &RequestResponse_MessageType
) &&
156 (RequestResponse_MessageType
== DHCP_ACK
))
158 /* Set the new network parameters from the DHCP server */
159 uip_sethostaddr((uip_ipaddr_t
*)&AppState
->DHCPClient
.DHCPOffer_Data
.AllocatedIP
);
160 uip_setnetmask((uip_ipaddr_t
*)&AppState
->DHCPClient
.DHCPOffer_Data
.Netmask
);
161 uip_setdraddr((uip_ipaddr_t
*)&AppState
->DHCPClient
.DHCPOffer_Data
.GatewayIP
);
163 AppState
->DHCPClient
.CurrentState
= DHCP_STATE_AddressLeased
;
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.
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
177 * \return Size in bytes of the created DHCP packet
179 uint16_t DHCPClientApp_FillDHCPHeader(DHCP_Header_t
* DHCPHeader
, uint8_t DHCPMessageType
, uip_udp_appstate_t
* AppState
)
181 /* Erase existing packet data so that we start will all 0x00 DHCP header data */
182 memset(DHCPHeader
, 0, sizeof(DHCP_Header_t
));
184 /* Fill out the DHCP packet header */
185 DHCPHeader
->Operation
= DHCP_OP_BOOTREQUEST
;
186 DHCPHeader
->HardwareType
= DHCP_HTYPE_ETHERNET
;
187 DHCPHeader
->HardwareAddressLength
= sizeof(MACAddress
);
188 DHCPHeader
->Hops
= 0;
189 DHCPHeader
->TransactionID
= DHCP_TRANSACTION_ID
;
190 DHCPHeader
->ElapsedSeconds
= 0;
191 DHCPHeader
->Flags
= HTONS(BOOTP_BROADCAST
);
192 memcpy(&DHCPHeader
->ClientIP
, &uip_hostaddr
, sizeof(uip_ipaddr_t
));
193 memcpy(&DHCPHeader
->YourIP
, &AppState
->DHCPClient
.DHCPOffer_Data
.AllocatedIP
, sizeof(uip_ipaddr_t
));
194 memcpy(&DHCPHeader
->NextServerIP
, &AppState
->DHCPClient
.DHCPOffer_Data
.ServerIP
, sizeof(uip_ipaddr_t
));
195 memcpy(&DHCPHeader
->ClientHardwareAddress
, &MACAddress
, sizeof(struct uip_eth_addr
));
196 DHCPHeader
->Cookie
= DHCP_MAGIC_COOKIE
;
198 /* Add a DHCP message type and terminator options to the start of the DHCP options field */
199 DHCPHeader
->Options
[0] = DHCP_OPTION_MSG_TYPE
;
200 DHCPHeader
->Options
[1] = 1;
201 DHCPHeader
->Options
[2] = DHCPMessageType
;
202 DHCPHeader
->Options
[3] = DHCP_OPTION_END
;
204 /* Calculate the total number of bytes added to the outgoing packet */
205 return (sizeof(DHCP_Header_t
) + 4);
208 /** Sets the given DHCP option in the DHCP packet's option list. This automatically moves the
209 * end of options terminator past the new option in the options list.
211 * \param[in,out] DHCPOptionList Pointer to the start of the DHCP packet's options list
212 * \param[in] Option DHCP option to add to the list
213 * \param[in] DataLen Size in bytes of the option data to add
214 * \param[in] OptionData Buffer where the option's data is to be sourced from
216 * \return Number of bytes added to the DHCP packet
218 uint8_t DHCPClientApp_SetOption(uint8_t* DHCPOptionList
, uint8_t Option
, uint8_t DataLen
, void* OptionData
)
220 /* Skip through the DHCP options list until the terminator option is found */
221 while (*DHCPOptionList
!= DHCP_OPTION_END
)
222 DHCPOptionList
+= (DHCPOptionList
[1] + 2);
224 /* Overwrite the existing terminator with the new option, add a new terminator at the end of the list */
225 DHCPOptionList
[0] = Option
;
226 DHCPOptionList
[1] = DataLen
;
227 memcpy(&DHCPOptionList
[2], OptionData
, DataLen
);
228 DHCPOptionList
[2 + DataLen
] = DHCP_OPTION_END
;
230 /* Calculate the total number of bytes added to the outgoing packet */
231 return (2 + DataLen
);
234 /** Retrieves the given option's data (if present) from the DHCP packet's options list.
236 * \param[in,out] DHCPOptionList Pointer to the start of the DHCP packet's options list
237 * \param[in] Option DHCP option to retrieve to the list
238 * \param[out] Destination Buffer where the option's data is to be written to if found
240 * \return Boolean true if the option was found in the DHCP packet's options list, false otherwise
242 bool DHCPClientApp_GetOption(uint8_t* DHCPOptionList
, uint8_t Option
, void* Destination
)
244 /* Look through the incoming DHCP packet's options list for the requested option */
245 while (*DHCPOptionList
!= DHCP_OPTION_END
)
247 /* Check if the current DHCP option in the packet is the one requested */
248 if (DHCPOptionList
[0] == Option
)
250 /* Copy request option's data to the destination buffer */
251 memcpy(Destination
, &DHCPOptionList
[2], DHCPOptionList
[1]);
253 /* Indicate that the requested option data was successfully retrieved */
257 /* Skip to next DHCP option in the options list */
258 DHCPOptionList
+= (DHCPOptionList
[1] + 2);
261 /* Requested option not found in the incoming packet's DHCP options list */