diff options
Diffstat (limited to 'lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/IP.c')
| -rw-r--r-- | lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/IP.c | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/IP.c b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/IP.c new file mode 100644 index 000000000..dfa583b85 --- /dev/null +++ b/lib/lufa/Demos/Device/LowLevel/RNDISEthernet/Lib/IP.c | |||
| @@ -0,0 +1,113 @@ | |||
| 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 | * 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] InDataStart Pointer to the start of the incoming packet's IP header | ||
| 43 | * \param[out] OutDataStart Pointer to the start of the outgoing packet's IP header | ||
| 44 | * | ||
| 45 | * \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE if no | ||
| 46 | * response was generated, NO_PROCESS if the packet processing was deferred until the | ||
| 47 | * next Ethernet packet handler iteration | ||
| 48 | */ | ||
| 49 | int16_t IP_ProcessIPPacket(void* InDataStart, | ||
| 50 | void* OutDataStart) | ||
| 51 | { | ||
| 52 | DecodeIPHeader(InDataStart); | ||
| 53 | |||
| 54 | IP_Header_t* IPHeaderIN = (IP_Header_t*)InDataStart; | ||
| 55 | IP_Header_t* IPHeaderOUT = (IP_Header_t*)OutDataStart; | ||
| 56 | |||
| 57 | /* Header length is specified in number of longs in the packet header, convert to bytes */ | ||
| 58 | uint16_t HeaderLengthBytes = (IPHeaderIN->HeaderLength * sizeof(uint32_t)); | ||
| 59 | |||
| 60 | int16_t RetSize = NO_RESPONSE; | ||
| 61 | |||
| 62 | /* Check to ensure the IP packet is addressed to the virtual webserver's IP or the broadcast IP address */ | ||
| 63 | if (!(IP_COMPARE(&IPHeaderIN->DestinationAddress, &ServerIPAddress)) && | ||
| 64 | !(IP_COMPARE(&IPHeaderIN->DestinationAddress, &BroadcastIPAddress))) | ||
| 65 | { | ||
| 66 | return NO_RESPONSE; | ||
| 67 | } | ||
| 68 | |||
| 69 | /* Pass off the IP payload to the appropriate protocol processing routine */ | ||
| 70 | switch (IPHeaderIN->Protocol) | ||
| 71 | { | ||
| 72 | case PROTOCOL_ICMP: | ||
| 73 | RetSize = ICMP_ProcessICMPPacket(&((uint8_t*)InDataStart)[HeaderLengthBytes], | ||
| 74 | &((uint8_t*)OutDataStart)[sizeof(IP_Header_t)]); | ||
| 75 | break; | ||
| 76 | case PROTOCOL_TCP: | ||
| 77 | RetSize = TCP_ProcessTCPPacket(InDataStart, | ||
| 78 | &((uint8_t*)InDataStart)[HeaderLengthBytes], | ||
| 79 | &((uint8_t*)OutDataStart)[sizeof(IP_Header_t)]); | ||
| 80 | break; | ||
| 81 | case PROTOCOL_UDP: | ||
| 82 | RetSize = UDP_ProcessUDPPacket(InDataStart, | ||
| 83 | &((uint8_t*)InDataStart)[HeaderLengthBytes], | ||
| 84 | &((uint8_t*)OutDataStart)[sizeof(IP_Header_t)]); | ||
| 85 | break; | ||
| 86 | } | ||
| 87 | |||
| 88 | /* Check to see if the protocol processing routine has filled out a response */ | ||
| 89 | if (RetSize > 0) | ||
| 90 | { | ||
| 91 | /* Fill out the response IP packet header */ | ||
| 92 | IPHeaderOUT->TotalLength = SwapEndian_16(sizeof(IP_Header_t) + RetSize); | ||
| 93 | IPHeaderOUT->TypeOfService = 0; | ||
| 94 | IPHeaderOUT->HeaderLength = (sizeof(IP_Header_t) / sizeof(uint32_t)); | ||
| 95 | IPHeaderOUT->Version = 4; | ||
| 96 | IPHeaderOUT->Flags = 0; | ||
| 97 | IPHeaderOUT->FragmentOffset = 0; | ||
| 98 | IPHeaderOUT->Identification = 0; | ||
| 99 | IPHeaderOUT->HeaderChecksum = 0; | ||
| 100 | IPHeaderOUT->Protocol = IPHeaderIN->Protocol; | ||
| 101 | IPHeaderOUT->TTL = DEFAULT_TTL; | ||
| 102 | IPHeaderOUT->SourceAddress = IPHeaderIN->DestinationAddress; | ||
| 103 | IPHeaderOUT->DestinationAddress = IPHeaderIN->SourceAddress; | ||
| 104 | |||
| 105 | IPHeaderOUT->HeaderChecksum = Ethernet_Checksum16(IPHeaderOUT, sizeof(IP_Header_t)); | ||
| 106 | |||
| 107 | /* Return the size of the response so far */ | ||
| 108 | return (sizeof(IP_Header_t) + RetSize); | ||
| 109 | } | ||
| 110 | |||
| 111 | return RetSize; | ||
| 112 | } | ||
| 113 | |||
