Added const modifiers to device mode class drivers.
[pub/USBasp.git] / Demos / Device / ClassDriver / RNDISEthernet / Lib / UDP.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
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 * User Datagram Protocol (UDP) packet handling routines. This protocol handles high throughput, low
34 * reliability packets which are typically used to encapsulate streaming data.
35 */
36
37 #define INCLUDE_FROM_UDP_C
38 #include "UDP.h"
39
40 /** Processes a UDP packet inside an Ethernet frame, and writes the appropriate response
41 * to the output Ethernet frame if a subprotocol handler has created a response packet.
42 *
43 * \param[in] IPHeaderInStart Pointer to the start of the incoming packet's IP header
44 * \param[in] UDPHeaderInStart Pointer to the start of the incoming packet's UDP header
45 * \param[out] UDPHeaderOutStart Pointer to the start of the outgoing packet's UDP header
46 *
47 * \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE otherwise
48 */
49 int16_t UDP_ProcessUDPPacket(void* IPHeaderInStart, void* UDPHeaderInStart, void* UDPHeaderOutStart)
50 {
51 UDP_Header_t* UDPHeaderIN = (UDP_Header_t*)UDPHeaderInStart;
52 UDP_Header_t* UDPHeaderOUT = (UDP_Header_t*)UDPHeaderOutStart;
53
54 int16_t RetSize = NO_RESPONSE;
55
56 DecodeUDPHeader(UDPHeaderInStart);
57
58 /* Check to see if the UDP packet is a DHCP packet */
59 if (SwapEndian_16(UDPHeaderIN->DestinationPort) == UDP_PORT_DHCP_REQUEST)
60 {
61 RetSize = DHCP_ProcessDHCPPacket(IPHeaderInStart,
62 &((uint8_t*)UDPHeaderInStart)[sizeof(UDP_Header_t)],
63 &((uint8_t*)UDPHeaderOutStart)[sizeof(UDP_Header_t)]);
64 }
65
66 /* Check to see if the protocol processing routine has filled out a response */
67 if (RetSize > 0)
68 {
69 /* Fill out the response UDP packet header */
70 UDPHeaderOUT->SourcePort = UDPHeaderIN->DestinationPort;
71 UDPHeaderOUT->DestinationPort = UDPHeaderIN->SourcePort;
72 UDPHeaderOUT->Checksum = 0;
73 UDPHeaderOUT->Length = SwapEndian_16(sizeof(UDP_Header_t) + RetSize);
74
75 /* Return the size of the response so far */
76 return (sizeof(UDP_Header_t) + RetSize);
77 }
78
79 return NO_RESPONSE;
80 }