]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Device/ClassDriver/RNDISEthernet/Lib/IP.c
Squashed 'tmk_core/' changes from 8da1898..e5f9940
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Device / ClassDriver / RNDISEthernet / Lib / IP.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 * Internet Protocol (IP) packet handling routines. This protocol handles IP packets from the
34 * host which typically encapsulate other protocols such as ICMP, UDP and TCP.
35 */
36
37 #include "IP.h"
38
39 /** Processes an IP packet inside an Ethernet frame, and writes the appropriate response
40 * to the output Ethernet frame if one is created by a sub-protocol handler.
41 *
42 * \param[in] FrameIN Pointer to the incoming Ethernet frame information structure
43 * \param[in] InDataStart Pointer to the start of the incoming packet's IP header
44 * \param[out] OutDataStart Pointer to the start of the outgoing packet's IP header
45 *
46 * \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE if no
47 * response was generated, NO_PROCESS if the packet processing was deferred until the
48 * next Ethernet packet handler iteration
49 */
50 int16_t IP_ProcessIPPacket(Ethernet_Frame_Info_t* const FrameIN,
51 void* InDataStart,
52 void* OutDataStart)
53 {
54 DecodeIPHeader(InDataStart);
55
56 IP_Header_t* IPHeaderIN = (IP_Header_t*)InDataStart;
57 IP_Header_t* IPHeaderOUT = (IP_Header_t*)OutDataStart;
58
59 /* Header length is specified in number of longs in the packet header, convert to bytes */
60 uint16_t HeaderLengthBytes = (IPHeaderIN->HeaderLength * sizeof(uint32_t));
61
62 int16_t RetSize = NO_RESPONSE;
63
64 /* Check to ensure the IP packet is addressed to the virtual webserver's IP or the broadcast IP address */
65 if (!(IP_COMPARE(&IPHeaderIN->DestinationAddress, &ServerIPAddress)) &&
66 !(IP_COMPARE(&IPHeaderIN->DestinationAddress, &BroadcastIPAddress)))
67 {
68 return NO_RESPONSE;
69 }
70
71 /* Pass off the IP payload to the appropriate protocol processing routine */
72 switch (IPHeaderIN->Protocol)
73 {
74 case PROTOCOL_ICMP:
75 RetSize = ICMP_ProcessICMPPacket(FrameIN,
76 &((uint8_t*)InDataStart)[HeaderLengthBytes],
77 &((uint8_t*)OutDataStart)[sizeof(IP_Header_t)]);
78 break;
79 case PROTOCOL_TCP:
80 RetSize = TCP_ProcessTCPPacket(InDataStart,
81 &((uint8_t*)InDataStart)[HeaderLengthBytes],
82 &((uint8_t*)OutDataStart)[sizeof(IP_Header_t)]);
83 break;
84 case PROTOCOL_UDP:
85 RetSize = UDP_ProcessUDPPacket(InDataStart,
86 &((uint8_t*)InDataStart)[HeaderLengthBytes],
87 &((uint8_t*)OutDataStart)[sizeof(IP_Header_t)]);
88 break;
89 }
90
91 /* Check to see if the protocol processing routine has filled out a response */
92 if (RetSize > 0)
93 {
94 /* Fill out the response IP packet header */
95 IPHeaderOUT->TotalLength = SwapEndian_16(sizeof(IP_Header_t) + RetSize);
96 IPHeaderOUT->TypeOfService = 0;
97 IPHeaderOUT->HeaderLength = (sizeof(IP_Header_t) / sizeof(uint32_t));
98 IPHeaderOUT->Version = 4;
99 IPHeaderOUT->Flags = 0;
100 IPHeaderOUT->FragmentOffset = 0;
101 IPHeaderOUT->Identification = 0;
102 IPHeaderOUT->HeaderChecksum = 0;
103 IPHeaderOUT->Protocol = IPHeaderIN->Protocol;
104 IPHeaderOUT->TTL = DEFAULT_TTL;
105 IPHeaderOUT->SourceAddress = IPHeaderIN->DestinationAddress;
106 IPHeaderOUT->DestinationAddress = IPHeaderIN->SourceAddress;
107
108 IPHeaderOUT->HeaderChecksum = Ethernet_Checksum16(IPHeaderOUT, sizeof(IP_Header_t));
109
110 /* Return the size of the response so far */
111 return (sizeof(IP_Header_t) + RetSize);
112 }
113
114 return RetSize;
115 }
116
Imprint / Impressum