]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/Demos/Device/ClassDriver/RNDISEthernet/Lib/UDP.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / Demos / Device / ClassDriver / RNDISEthernet / Lib / UDP.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  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 disclaims 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 sub-protocol 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,
50                              void* UDPHeaderInStart,
51                              void* UDPHeaderOutStart)
52 {
53         UDP_Header_t* UDPHeaderIN  = (UDP_Header_t*)UDPHeaderInStart;
54         UDP_Header_t* UDPHeaderOUT = (UDP_Header_t*)UDPHeaderOutStart;
55
56         int16_t RetSize = NO_RESPONSE;
57
58         DecodeUDPHeader(UDPHeaderInStart);
59
60         switch (SwapEndian_16(UDPHeaderIN->DestinationPort))
61         {
62                 case UDP_PORT_DHCP_REQUEST:
63                         RetSize = DHCP_ProcessDHCPPacket(IPHeaderInStart,
64                                                          &((uint8_t*)UDPHeaderInStart)[sizeof(UDP_Header_t)],
65                                                      &((uint8_t*)UDPHeaderOutStart)[sizeof(UDP_Header_t)]);
66                         break;
67         }
68
69         /* Check to see if the protocol processing routine has filled out a response */
70         if (RetSize > 0)
71         {
72                 /* Fill out the response UDP packet header */
73                 UDPHeaderOUT->SourcePort      = UDPHeaderIN->DestinationPort;
74                 UDPHeaderOUT->DestinationPort = UDPHeaderIN->SourcePort;
75                 UDPHeaderOUT->Checksum        = 0;
76                 UDPHeaderOUT->Length          = SwapEndian_16(sizeof(UDP_Header_t) + RetSize);
77
78                 /* Return the size of the response so far */
79                 return (sizeof(UDP_Header_t) + RetSize);
80         }
81
82         return NO_RESPONSE;
83 }
84