3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 * Dynamic Host Configuration Protocol (DHCP) packet handling routines. This protocol
34 * handles the automatic IP negotiation to the host, so that the host will use the provided
35 * IP address given to it by the device.
40 /** Processes a DHCP packet inside an Ethernet frame, and writes the appropriate response
41 * to the output Ethernet frame if the host is requesting or accepting an IP address.
43 * \param IPHeaderInStart Pointer to the start of the incoming packet's IP header
44 * \param DHCPHeaderInStart Pointer to the start of the incoming packet's DHCP header
45 * \param DHCPHeaderOutStart Pointer to the start of the outgoing packet's DHCP header
47 * \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE otherwise
49 int16_t DHCP_ProcessDHCPPacket(void* IPHeaderInStart
, void* DHCPHeaderInStart
, void* DHCPHeaderOutStart
)
51 IP_Header_t
* IPHeaderIN
= (IP_Header_t
*)IPHeaderInStart
;
52 DHCP_Header_t
* DHCPHeaderIN
= (DHCP_Header_t
*)DHCPHeaderInStart
;
53 DHCP_Header_t
* DHCPHeaderOUT
= (DHCP_Header_t
*)DHCPHeaderOutStart
;
55 uint8_t* DHCPOptionsINStart
= (uint8_t*)(DHCPHeaderInStart
+ sizeof(DHCP_Header_t
));
56 uint8_t* DHCPOptionsOUTStart
= (uint8_t*)(DHCPHeaderOutStart
+ sizeof(DHCP_Header_t
));
58 DecodeDHCPHeader(DHCPHeaderInStart
);
60 /* Zero out the response DHCP packet, as much of it legacy and left at 0 */
61 memset(DHCPHeaderOUT
, 0, sizeof(DHCP_Header_t
));
63 /* Fill out the response DHCP packet */
64 DHCPHeaderOUT
->HardwareType
= DHCPHeaderIN
->HardwareType
;
65 DHCPHeaderOUT
->Operation
= DHCP_OP_BOOTREPLY
;
66 DHCPHeaderOUT
->HardwareAddressLength
= DHCPHeaderIN
->HardwareAddressLength
;
67 DHCPHeaderOUT
->Hops
= 0;
68 DHCPHeaderOUT
->TransactionID
= DHCPHeaderIN
->TransactionID
;
69 DHCPHeaderOUT
->ElapsedSeconds
= 0;
70 DHCPHeaderOUT
->Flags
= DHCPHeaderIN
->Flags
;
71 DHCPHeaderOUT
->YourIP
= ClientIPAddress
;
72 memcpy(&DHCPHeaderOUT
->ClientHardwareAddress
, &DHCPHeaderIN
->ClientHardwareAddress
, sizeof(MAC_Address_t
));
73 DHCPHeaderOUT
->Cookie
= SwapEndian_32(DHCP_MAGIC_COOKIE
);
75 /* Alter the incoming IP packet header so that the corrected IP source and destinations are used - this means that
76 when the response IP header is generated, it will use the corrected addresses and not the null/broatcast addresses */
77 IPHeaderIN
->SourceAddress
= ClientIPAddress
;
78 IPHeaderIN
->DestinationAddress
= ServerIPAddress
;
80 /* Process the incoming DHCP packet options */
81 while (DHCPOptionsINStart
[0] != DHCP_OPTION_END
)
83 /* Find the Message Type DHCP option, to determine the type of DHCP packet */
84 if (DHCPOptionsINStart
[0] == DHCP_OPTION_MESSAGETYPE
)
86 if ((DHCPOptionsINStart
[2] == DHCP_MESSAGETYPE_DISCOVER
) || (DHCPOptionsINStart
[2] == DHCP_MESSAGETYPE_REQUEST
))
88 /* Fill out the response DHCP packet options for a DHCP OFFER or ACK response */
90 *(DHCPOptionsOUTStart
++) = DHCP_OPTION_MESSAGETYPE
;
91 *(DHCPOptionsOUTStart
++) = 1;
92 *(DHCPOptionsOUTStart
++) = (DHCPOptionsINStart
[2] == DHCP_MESSAGETYPE_DISCOVER
) ? DHCP_MESSAGETYPE_OFFER
93 : DHCP_MESSAGETYPE_ACK
;
95 *(DHCPOptionsOUTStart
++) = DHCP_OPTION_SUBNETMASK
;
96 *(DHCPOptionsOUTStart
++) = 4;
97 *(DHCPOptionsOUTStart
++) = 0xFF;
98 *(DHCPOptionsOUTStart
++) = 0xFF;
99 *(DHCPOptionsOUTStart
++) = 0xFF;
100 *(DHCPOptionsOUTStart
++) = 0x00;
102 *(DHCPOptionsOUTStart
++) = DHCP_OPTION_DHCPSERVER
;
103 *(DHCPOptionsOUTStart
++) = sizeof(IP_Address_t
);
104 memcpy(DHCPOptionsOUTStart
, &ServerIPAddress
, sizeof(IP_Address_t
));
105 DHCPOptionsOUTStart
+= sizeof(IP_Address_t
);
107 *(DHCPOptionsOUTStart
++) = DHCP_OPTION_END
;
109 return (sizeof(DHCP_Header_t
) + 12 + sizeof(IP_Address_t
));
113 /* Go to the next DHCP option - skip one byte if option is a padding byte, else skip the complete option's size */
114 DHCPOptionsINStart
+= ((DHCPOptionsINStart
[0] == DHCP_OPTION_PAD
) ?
1 : (DHCPOptionsINStart
[1] + 2));