3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  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 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 
  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] 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 
  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 
  49 int16_t IP_ProcessIPPacket(void* InDataStart
, 
  52         DecodeIPHeader(InDataStart
); 
  54         IP_Header_t
* IPHeaderIN  
= (IP_Header_t
*)InDataStart
; 
  55         IP_Header_t
* IPHeaderOUT 
= (IP_Header_t
*)OutDataStart
; 
  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)); 
  60         int16_t  RetSize 
= NO_RESPONSE
; 
  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
))) 
  69         /* Pass off the IP payload to the appropriate protocol processing routine */ 
  70         switch (IPHeaderIN
->Protocol
) 
  73                         RetSize 
= ICMP_ProcessICMPPacket(&((uint8_t*)InDataStart
)[HeaderLengthBytes
], 
  74                                                          &((uint8_t*)OutDataStart
)[sizeof(IP_Header_t
)]); 
  77                         RetSize 
= TCP_ProcessTCPPacket(InDataStart
, 
  78                                                        &((uint8_t*)InDataStart
)[HeaderLengthBytes
], 
  79                                                        &((uint8_t*)OutDataStart
)[sizeof(IP_Header_t
)]); 
  82                         RetSize 
= UDP_ProcessUDPPacket(InDataStart
, 
  83                                                        &((uint8_t*)InDataStart
)[HeaderLengthBytes
], 
  84                                                        &((uint8_t*)OutDataStart
)[sizeof(IP_Header_t
)]); 
  88         /* Check to see if the protocol processing routine has filled out a response */ 
  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
; 
 105                 IPHeaderOUT
->HeaderChecksum     
= Ethernet_Checksum16(IPHeaderOUT
, sizeof(IP_Header_t
)); 
 107                 /* Return the size of the response so far */ 
 108                 return (sizeof(IP_Header_t
) + RetSize
);