3 Copyright (C) Dean Camera, 2014.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
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.
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
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
50 int16_t IP_ProcessIPPacket(Ethernet_Frame_Info_t* const FrameIN,
54 DecodeIPHeader(InDataStart);
56 IP_Header_t* IPHeaderIN = (IP_Header_t*)InDataStart;
57 IP_Header_t* IPHeaderOUT = (IP_Header_t*)OutDataStart;
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));
62 int16_t RetSize = NO_RESPONSE;
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)))
71 /* Pass off the IP payload to the appropriate protocol processing routine */
72 switch (IPHeaderIN->Protocol)
75 RetSize = ICMP_ProcessICMPPacket(FrameIN,
76 &((uint8_t*)InDataStart)[HeaderLengthBytes],
77 &((uint8_t*)OutDataStart)[sizeof(IP_Header_t)]);
80 RetSize = TCP_ProcessTCPPacket(InDataStart,
81 &((uint8_t*)InDataStart)[HeaderLengthBytes],
82 &((uint8_t*)OutDataStart)[sizeof(IP_Header_t)]);
85 RetSize = UDP_ProcessUDPPacket(InDataStart,
86 &((uint8_t*)InDataStart)[HeaderLengthBytes],
87 &((uint8_t*)OutDataStart)[sizeof(IP_Header_t)]);
91 /* Check to see if the protocol processing routine has filled out a response */
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;
108 IPHeaderOUT->HeaderChecksum = Ethernet_Checksum16(IPHeaderOUT, sizeof(IP_Header_t));
110 /* Return the size of the response so far */
111 return (sizeof(IP_Header_t) + RetSize);