3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   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  *  Transmission Control Protocol (TCP) packet handling routines. This protocol handles the reliable in-order transmission 
  34  *  and reception of packets to and from devices on a network, to "ports" on the device. It is used in situations where data 
  35  *  delivery must be reliable and correct, e.g. HTTP, TELNET and most other non-streaming protocols. 
  38 #define  INCLUDE_FROM_TCP_C 
  41 /* Global Variables: */ 
  42 /** Port state table array. This contains the current status of TCP ports in the device. To save on space, only open ports are 
  43  *  stored - closed ports may be overwritten at any time, and the system will assume any ports not present in the array are closed. This 
  44  *  allows for MAX_OPEN_TCP_PORTS to be less than the number of ports used by the application if desired. 
  46 TCP_PortState_t        PortStateTable
[MAX_OPEN_TCP_PORTS
]; 
  48 /** Connection state table array. This contains the current status of TCP connections in the device. To save on space, only active 
  49  *  (non-closed) connections are stored - closed connections may be overwritten at any time, and the system will assume any connections 
  50  *  not present in the array are closed. 
  52 TCP_ConnectionState_t  ConnectionStateTable
[MAX_TCP_CONNECTIONS
]; 
  55 /** Task to handle the calling of each registered application's callback function, to process and generate TCP packets at the application 
  56  *  level. If an application produces a response, this task constructs the appropriate Ethernet frame and places it into the Ethernet OUT 
  57  *  buffer for later transmission. 
  61         /* Task to hand off TCP packets to and from the listening applications. */ 
  63         /* Run each application in sequence, to process incoming and generate outgoing packets */ 
  64         for (uint8_t CSTableEntry 
= 0; CSTableEntry 
< MAX_TCP_CONNECTIONS
; CSTableEntry
++) 
  66                 /* Find the corresponding port entry in the port table */ 
  67                 for (uint8_t PTableEntry 
= 0; PTableEntry 
< MAX_TCP_CONNECTIONS
; PTableEntry
++) 
  69                         /* Run the application handler for the port */ 
  70                         if ((PortStateTable
[PTableEntry
].Port  
== ConnectionStateTable
[CSTableEntry
].Port
) &&  
  71                             (PortStateTable
[PTableEntry
].State 
== TCP_Port_Open
)) 
  73                                 PortStateTable
[PTableEntry
].ApplicationHandler(&ConnectionStateTable
[CSTableEntry
], &ConnectionStateTable
[CSTableEntry
].Info
.Buffer
); 
  78         /* Bail out early if there is already a frame waiting to be sent in the Ethernet OUT buffer */ 
  79         if (FrameOUT
.FrameInBuffer
) 
  82         /* Send response packets from each application as the TCP packet buffers are filled by the applications */ 
  83         for (uint8_t CSTableEntry 
= 0; CSTableEntry 
< MAX_TCP_CONNECTIONS
; CSTableEntry
++) 
  85                 /* For each completely received packet, pass it along to the listening application */ 
  86                 if ((ConnectionStateTable
[CSTableEntry
].Info
.Buffer
.Direction 
== TCP_PACKETDIR_OUT
) && 
  87                     (ConnectionStateTable
[CSTableEntry
].Info
.Buffer
.Ready
)) 
  89                         Ethernet_Frame_Header_t
* FrameOUTHeader 
= (Ethernet_Frame_Header_t
*)&FrameOUT
.FrameData
; 
  90                         IP_Header_t
*             IPHeaderOUT    
= (IP_Header_t
*)&FrameOUT
.FrameData
[sizeof(Ethernet_Frame_Header_t
)]; 
  91                         TCP_Header_t
*            TCPHeaderOUT   
= (TCP_Header_t
*)&FrameOUT
.FrameData
[sizeof(Ethernet_Frame_Header_t
) + 
  93                         void*                    TCPDataOUT     
= &FrameOUT
.FrameData
[sizeof(Ethernet_Frame_Header_t
) + 
  95                                                                                       sizeof(TCP_Header_t
)]; 
  97                         uint16_t PacketSize 
= ConnectionStateTable
[CSTableEntry
].Info
.Buffer
.Length
; 
  99                         /* Fill out the TCP data */ 
 100                         TCPHeaderOUT
->SourcePort           
= ConnectionStateTable
[CSTableEntry
].Port
; 
 101                         TCPHeaderOUT
->DestinationPort      
= ConnectionStateTable
[CSTableEntry
].RemotePort
; 
 102                         TCPHeaderOUT
->SequenceNumber       
= SwapEndian_32(ConnectionStateTable
[CSTableEntry
].Info
.SequenceNumberOut
); 
 103                         TCPHeaderOUT
->AcknowledgmentNumber 
= SwapEndian_32(ConnectionStateTable
[CSTableEntry
].Info
.SequenceNumberIn
); 
 104                         TCPHeaderOUT
->DataOffset           
= (sizeof(TCP_Header_t
) / sizeof(uint32_t)); 
 105                         TCPHeaderOUT
->WindowSize           
= SwapEndian_16(TCP_WINDOW_SIZE
); 
 107                         TCPHeaderOUT
->Flags                
= TCP_FLAG_ACK
; 
 108                         TCPHeaderOUT
->UrgentPointer        
= 0; 
 109                         TCPHeaderOUT
->Checksum             
= 0; 
 110                         TCPHeaderOUT
->Reserved             
= 0; 
 112                         memcpy(TCPDataOUT
, ConnectionStateTable
[CSTableEntry
].Info
.Buffer
.Data
, PacketSize
); 
 114                         ConnectionStateTable
[CSTableEntry
].Info
.SequenceNumberOut 
+= PacketSize
; 
 116                         TCPHeaderOUT
->Checksum             
= TCP_Checksum16(TCPHeaderOUT
, ServerIPAddress
, 
 117                                                                             ConnectionStateTable
[CSTableEntry
].RemoteAddress
, 
 118                                                                             (sizeof(TCP_Header_t
) + PacketSize
)); 
 120                         PacketSize 
+= sizeof(TCP_Header_t
); 
 122                         /* Fill out the response IP header */ 
 123                         IPHeaderOUT
->TotalLength        
= SwapEndian_16(sizeof(IP_Header_t
) + PacketSize
); 
 124                         IPHeaderOUT
->TypeOfService      
= 0; 
 125                         IPHeaderOUT
->HeaderLength       
= (sizeof(IP_Header_t
) / sizeof(uint32_t)); 
 126                         IPHeaderOUT
->Version            
= 4; 
 127                         IPHeaderOUT
->Flags              
= 0; 
 128                         IPHeaderOUT
->FragmentOffset     
= 0; 
 129                         IPHeaderOUT
->Identification     
= 0; 
 130                         IPHeaderOUT
->HeaderChecksum     
= 0; 
 131                         IPHeaderOUT
->Protocol           
= PROTOCOL_TCP
; 
 132                         IPHeaderOUT
->TTL                
= DEFAULT_TTL
; 
 133                         IPHeaderOUT
->SourceAddress      
= ServerIPAddress
; 
 134                         IPHeaderOUT
->DestinationAddress 
= ConnectionStateTable
[CSTableEntry
].RemoteAddress
; 
 136                         IPHeaderOUT
->HeaderChecksum     
= Ethernet_Checksum16(IPHeaderOUT
, sizeof(IP_Header_t
)); 
 138                         PacketSize 
+= sizeof(IP_Header_t
); 
 140                         /* Fill out the response Ethernet frame header */ 
 141                         FrameOUTHeader
->Source          
= ServerMACAddress
; 
 142                         FrameOUTHeader
->Destination     
= (MAC_Address_t
){{0x02, 0x00, 0x02, 0x00, 0x02, 0x00}}; 
 143                         FrameOUTHeader
->EtherType       
= SwapEndian_16(ETHERTYPE_IPV4
); 
 145                         PacketSize 
+= sizeof(Ethernet_Frame_Header_t
); 
 147                         /* Set the response length in the buffer and indicate that a response is ready to be sent */ 
 148                         FrameOUT
.FrameLength            
= PacketSize
; 
 149                         FrameOUT
.FrameInBuffer          
= true; 
 151                         ConnectionStateTable
[CSTableEntry
].Info
.Buffer
.Ready 
= false; 
 158 /** Initializes the TCP protocol handler, clearing the port and connection state tables. This must be called before TCP packets are 
 163         /* Initialize the port state table with all CLOSED entries */ 
 164         for (uint8_t PTableEntry 
= 0; PTableEntry 
< MAX_OPEN_TCP_PORTS
; PTableEntry
++) 
 165           PortStateTable
[PTableEntry
].State 
= TCP_Port_Closed
; 
 167         /* Initialize the connection table with all CLOSED entries */ 
 168         for (uint8_t CSTableEntry 
= 0; CSTableEntry 
< MAX_TCP_CONNECTIONS
; CSTableEntry
++) 
 169           ConnectionStateTable
[CSTableEntry
].State 
= TCP_Connection_Closed
; 
 172 /** Sets the state and callback handler of the given port, specified in big endian to the given state. 
 174  *  \param Port     Port whose state and callback function to set, specified in big endian 
 175  *  \param State    New state of the port, a value from the TCP_PortStates_t enum 
 176  *  \param Handler  Application callback handler for the port 
 178  *  \return Boolean true if the port state was set, false otherwise (no more space in the port state table) 
 180 bool TCP_SetPortState(uint16_t Port
, uint8_t State
, void (*Handler
)(TCP_ConnectionState_t
*, TCP_ConnectionBuffer_t
*)) 
 182         /* Note, Port number should be specified in BIG endian to simplify network code */ 
 184         /* Check to see if the port entry is already in the port state table */ 
 185         for (uint8_t PTableEntry 
= 0; PTableEntry 
< MAX_TCP_CONNECTIONS
; PTableEntry
++) 
 187                 /* Find existing entry for the port in the table, update it if found */ 
 188                 if (PortStateTable
[PTableEntry
].Port 
== Port
) 
 190                         PortStateTable
[PTableEntry
].State 
= State
; 
 191                         PortStateTable
[PTableEntry
].ApplicationHandler 
= Handler
; 
 196         /* Check if trying to open the port -- if so we need to find an unused (closed) entry and replace it */ 
 197         if (State 
== TCP_Port_Open
) 
 199                 for (uint8_t PTableEntry 
= 0; PTableEntry 
< MAX_TCP_CONNECTIONS
; PTableEntry
++) 
 201                         /* Find a closed port entry in the table, change it to the given port and state */ 
 202                         if (PortStateTable
[PTableEntry
].State 
== TCP_Port_Closed
) 
 204                                 PortStateTable
[PTableEntry
].Port  
= Port
; 
 205                                 PortStateTable
[PTableEntry
].State 
= State
; 
 206                                 PortStateTable
[PTableEntry
].ApplicationHandler 
= Handler
; 
 211                 /* Port not in table and no room to add it, return failure */ 
 216                 /* Port not in table but trying to close it, so operation successful */ 
 221 /** Retrieves the current state of a given TCP port, specified in big endian. 
 223  *  \param Port  TCP port whose state is to be retrieved, given in big-endian 
 225  *  \return A value from the TCP_PortStates_t enum 
 227 uint8_t TCP_GetPortState(uint16_t Port
) 
 229         /* Note, Port number should be specified in BIG endian to simplify network code */ 
 231         for (uint8_t PTableEntry 
= 0; PTableEntry 
< MAX_TCP_CONNECTIONS
; PTableEntry
++) 
 233                 /* Find existing entry for the port in the table, return the port status if found */ 
 234                 if (PortStateTable
[PTableEntry
].Port 
== Port
) 
 235                   return PortStateTable
[PTableEntry
].State
; 
 238         /* Port not in table, assume closed */ 
 239         return TCP_Port_Closed
; 
 242 /** Sets the connection state of the given port, remote address and remote port to the given TCP connection state. If the 
 243  *  connection exists in the connection state table it is updated, otherwise it is created if possible. 
 245  *  \param Port           TCP port of the connection on the device, specified in big endian 
 246  *  \param RemoteAddress  Remote protocol IP address of the connected device 
 247  *  \param RemotePort     TCP port of the remote device in the connection, specified in big endian 
 248  *  \param State          TCP connection state, a value from the TCP_ConnectionStates_t enum 
 250  *  \return Boolean true if the connection was updated or created, false otherwise (no more space in the connection state table) 
 252 bool TCP_SetConnectionState(uint16_t Port
, IP_Address_t RemoteAddress
, uint16_t RemotePort
, uint8_t State
) 
 254         /* Note, Port number should be specified in BIG endian to simplify network code */ 
 256         for (uint8_t CSTableEntry 
= 0; CSTableEntry 
< MAX_TCP_CONNECTIONS
; CSTableEntry
++) 
 258                 /* Find port entry in the table */ 
 259                 if ((ConnectionStateTable
[CSTableEntry
].Port 
== Port
) && 
 260                      IP_COMPARE(&ConnectionStateTable
[CSTableEntry
].RemoteAddress
, &RemoteAddress
) && 
 261                          ConnectionStateTable
[CSTableEntry
].RemotePort 
== RemotePort
) 
 263                         ConnectionStateTable
[CSTableEntry
].State 
= State
; 
 268         for (uint8_t CSTableEntry 
= 0; CSTableEntry 
< MAX_TCP_CONNECTIONS
; CSTableEntry
++) 
 270                 /* Find empty entry in the table */ 
 271                 if (ConnectionStateTable
[CSTableEntry
].State 
== TCP_Connection_Closed
) 
 273                         ConnectionStateTable
[CSTableEntry
].Port          
= Port
; 
 274                         ConnectionStateTable
[CSTableEntry
].RemoteAddress 
= RemoteAddress
;                        
 275                         ConnectionStateTable
[CSTableEntry
].RemotePort    
= RemotePort
; 
 276                         ConnectionStateTable
[CSTableEntry
].State         
= State
; 
 284 /** Retrieves the current state of a given TCP connection to a host. 
 286  *  \param Port           TCP port on the device in the connection, specified in big endian 
 287  *  \param RemoteAddress  Remote protocol IP address of the connected host 
 288  *  \param RemotePort     Remote TCP port of the connected host, specified in big endian 
 290  *  \return A value from the TCP_ConnectionStates_t enum 
 292 uint8_t TCP_GetConnectionState(uint16_t Port
, IP_Address_t RemoteAddress
, uint16_t RemotePort
) 
 294         /* Note, Port number should be specified in BIG endian to simplify network code */ 
 296         for (uint8_t CSTableEntry 
= 0; CSTableEntry 
< MAX_TCP_CONNECTIONS
; CSTableEntry
++) 
 298                 /* Find port entry in the table */ 
 299                 if ((ConnectionStateTable
[CSTableEntry
].Port 
== Port
) && 
 300                      IP_COMPARE(&ConnectionStateTable
[CSTableEntry
].RemoteAddress
, &RemoteAddress
) && 
 301                          ConnectionStateTable
[CSTableEntry
].RemotePort 
== RemotePort
) 
 304                         return ConnectionStateTable
[CSTableEntry
].State
; 
 308         return TCP_Connection_Closed
; 
 311 /** Retrieves the connection info structure of a given connection to a host. 
 313  *  \param Port           TCP port on the device in the connection, specified in big endian 
 314  *  \param RemoteAddress  Remote protocol IP address of the connected host 
 315  *  \param RemotePort     Remote TCP port of the connected host, specified in big endian 
 317  *  \return ConnectionInfo structure of the connection if found, NULL otherwise 
 319 TCP_ConnectionInfo_t
* TCP_GetConnectionInfo(uint16_t Port
, IP_Address_t RemoteAddress
, uint16_t RemotePort
) 
 321         /* Note, Port number should be specified in BIG endian to simplify network code */ 
 323         for (uint8_t CSTableEntry 
= 0; CSTableEntry 
< MAX_TCP_CONNECTIONS
; CSTableEntry
++) 
 325                 /* Find port entry in the table */ 
 326                 if ((ConnectionStateTable
[CSTableEntry
].Port 
== Port
) && 
 327                      IP_COMPARE(&ConnectionStateTable
[CSTableEntry
].RemoteAddress
, &RemoteAddress
) && 
 328                          ConnectionStateTable
[CSTableEntry
].RemotePort 
== RemotePort
) 
 330                         return &ConnectionStateTable
[CSTableEntry
].Info
; 
 337 /** Processes a TCP packet inside an Ethernet frame, and writes the appropriate response 
 338  *  to the output Ethernet frame if one is created by a application handler. 
 340  *  \param IPHeaderInStart    Pointer to the start of the incoming packet's IP header 
 341  *  \param TCPHeaderInStart   Pointer to the start of the incoming packet's TCP header 
 342  *  \param TCPHeaderOutStart  Pointer to the start of the outgoing packet's TCP header 
 344  *  \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE if no 
 345  *           response was generated, NO_PROCESS if the packet processing was deferred until the 
 346  *           next Ethernet packet handler iteration 
 348 int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart
, void* TCPHeaderInStart
, void* TCPHeaderOutStart
) 
 350         IP_Header_t
*  IPHeaderIN   
= (IP_Header_t
*)IPHeaderInStart
; 
 351         TCP_Header_t
* TCPHeaderIN  
= (TCP_Header_t
*)TCPHeaderInStart
; 
 352         TCP_Header_t
* TCPHeaderOUT 
= (TCP_Header_t
*)TCPHeaderOutStart
; 
 354         TCP_ConnectionInfo_t
* ConnectionInfo
; 
 356         DecodeTCPHeader(TCPHeaderInStart
); 
 358         bool PacketResponse 
= false; 
 360         /* Check if the destination port is open and allows incoming connections */ 
 361         if (TCP_GetPortState(TCPHeaderIN
->DestinationPort
) == TCP_Port_Open
) 
 363                 /* Detect SYN from host to start a connection */ 
 364                 if (TCPHeaderIN
->Flags 
& TCP_FLAG_SYN
) 
 365                   TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, TCPHeaderIN
->SourcePort
, TCP_Connection_Listen
); 
 367                 /* Detect RST from host to abort existing connection */ 
 368                 if (TCPHeaderIN
->Flags 
& TCP_FLAG_RST
) 
 370                         TCPHeaderOUT
->Flags 
= (TCP_FLAG_RST 
| TCP_FLAG_ACK
);                             
 371                         PacketResponse 
= true; 
 373                         TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 374                                                TCPHeaderIN
->SourcePort
, TCP_Connection_Closed
);                  
 378                         /* Process the incoming TCP packet based on the current connection state for the sender and port */ 
 379                         switch (TCP_GetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, TCPHeaderIN
->SourcePort
)) 
 381                                 case TCP_Connection_Listen
: 
 382                                         if (TCPHeaderIN
->Flags 
== TCP_FLAG_SYN
) 
 384                                                 /* SYN connection when closed starts a connection with a peer */ 
 386                                                 TCPHeaderOUT
->Flags 
= (TCP_FLAG_SYN 
| TCP_FLAG_ACK
);                             
 387                                                 PacketResponse      
= true; 
 389                                                 TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, TCPHeaderIN
->SourcePort
, 
 390                                                                                            TCP_Connection_SYNReceived
); 
 392                                                 ConnectionInfo 
= TCP_GetConnectionInfo(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, TCPHeaderIN
->SourcePort
); 
 394                                                 ConnectionInfo
->SequenceNumberIn  
= (SwapEndian_32(TCPHeaderIN
->SequenceNumber
) + 1); 
 395                                                 ConnectionInfo
->SequenceNumberOut 
= 0; 
 396                                                 ConnectionInfo
->Buffer
.InUse      
= false; 
 400                                 case TCP_Connection_SYNReceived
: 
 401                                         if (TCPHeaderIN
->Flags 
== TCP_FLAG_ACK
) 
 403                                                 /* ACK during the connection process completes the connection to a peer */ 
 405                                                 TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 406                                                                                            TCPHeaderIN
->SourcePort
, TCP_Connection_Established
); 
 408                                                 ConnectionInfo 
= TCP_GetConnectionInfo(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 409                                                                                                                            TCPHeaderIN
->SourcePort
); 
 411                                                 ConnectionInfo
->SequenceNumberOut
++; 
 415                                 case TCP_Connection_Established
: 
 416                                         if (TCPHeaderIN
->Flags 
== (TCP_FLAG_FIN 
| TCP_FLAG_ACK
)) 
 418                                                 /* FIN ACK when connected to a peer starts the finalization process */ 
 420                                                 TCPHeaderOUT
->Flags 
= (TCP_FLAG_FIN 
| TCP_FLAG_ACK
);                             
 421                                                 PacketResponse      
= true; 
 423                                                 TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 424                                                                                            TCPHeaderIN
->SourcePort
, TCP_Connection_CloseWait
); 
 426                                                 ConnectionInfo 
= TCP_GetConnectionInfo(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 427                                                                                                                            TCPHeaderIN
->SourcePort
); 
 429                                                 ConnectionInfo
->SequenceNumberIn
++; 
 430                                                 ConnectionInfo
->SequenceNumberOut
++; 
 432                                         else if ((TCPHeaderIN
->Flags 
== TCP_FLAG_ACK
) || (TCPHeaderIN
->Flags 
== (TCP_FLAG_ACK 
| TCP_FLAG_PSH
))) 
 434                                                 ConnectionInfo 
= TCP_GetConnectionInfo(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 435                                                                                                                            TCPHeaderIN
->SourcePort
); 
 437                                                 /* Check if the buffer is currently in use either by a buffered data to send, or receive */              
 438                                                 if ((ConnectionInfo
->Buffer
.InUse 
== false) && (ConnectionInfo
->Buffer
.Ready 
== false)) 
 440                                                         ConnectionInfo
->Buffer
.Direction 
= TCP_PACKETDIR_IN
; 
 441                                                         ConnectionInfo
->Buffer
.InUse     
= true; 
 442                                                         ConnectionInfo
->Buffer
.Length    
= 0; 
 445                                                 /* Check if the buffer has been claimed by us to read in data from the peer */ 
 446                                                 if ((ConnectionInfo
->Buffer
.Direction 
== TCP_PACKETDIR_IN
) && 
 447                                                         (ConnectionInfo
->Buffer
.Length 
!= TCP_WINDOW_SIZE
)) 
 449                                                         uint16_t IPOffset   
= (IPHeaderIN
->HeaderLength 
* sizeof(uint32_t)); 
 450                                                         uint16_t TCPOffset  
= (TCPHeaderIN
->DataOffset 
* sizeof(uint32_t)); 
 451                                                         uint16_t DataLength 
= (SwapEndian_16(IPHeaderIN
->TotalLength
) - IPOffset 
- TCPOffset
); 
 453                                                         /* Copy the packet data into the buffer */ 
 454                                                         memcpy(&ConnectionInfo
->Buffer
.Data
[ConnectionInfo
->Buffer
.Length
], 
 455                                                                    &((uint8_t*)TCPHeaderInStart
)[TCPOffset
], 
 458                                                         ConnectionInfo
->SequenceNumberIn 
+= DataLength
; 
 459                                                         ConnectionInfo
->Buffer
.Length    
+= DataLength
; 
 461                                                         /* Check if the buffer is full or if the PSH flag is set, if so indicate buffer ready */ 
 462                                                         if ((!(TCP_WINDOW_SIZE 
- ConnectionInfo
->Buffer
.Length
)) || (TCPHeaderIN
->Flags 
& TCP_FLAG_PSH
)) 
 464                                                                 ConnectionInfo
->Buffer
.InUse 
= false; 
 465                                                                 ConnectionInfo
->Buffer
.Ready 
= true; 
 467                                                                 TCPHeaderOUT
->Flags 
= TCP_FLAG_ACK
; 
 468                                                                 PacketResponse      
= true; 
 473                                                         /* Buffer is currently in use by the application, defer processing of the incoming packet */ 
 479                                 case TCP_Connection_Closing
: 
 480                                                 ConnectionInfo 
= TCP_GetConnectionInfo(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 481                                                                                                                            TCPHeaderIN
->SourcePort
); 
 483                                                 TCPHeaderOUT
->Flags 
= (TCP_FLAG_ACK 
| TCP_FLAG_FIN
); 
 484                                                 PacketResponse      
= true; 
 486                                                 ConnectionInfo
->Buffer
.InUse 
= false; 
 488                                                 TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 489                                                                                            TCPHeaderIN
->SourcePort
, TCP_Connection_FINWait1
); 
 492                                 case TCP_Connection_FINWait1
: 
 493                                         if (TCPHeaderIN
->Flags 
== (TCP_FLAG_FIN 
| TCP_FLAG_ACK
)) 
 495                                                 ConnectionInfo 
= TCP_GetConnectionInfo(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 496                                                                                                                            TCPHeaderIN
->SourcePort
); 
 498                                                 TCPHeaderOUT
->Flags 
= TCP_FLAG_ACK
; 
 499                                                 PacketResponse      
= true; 
 501                                                 ConnectionInfo
->SequenceNumberIn
++; 
 502                                                 ConnectionInfo
->SequenceNumberOut
++; 
 504                                                 TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 505                                                                                            TCPHeaderIN
->SourcePort
, TCP_Connection_Closed
); 
 507                                         else if (TCPHeaderIN
->Flags 
== TCP_FLAG_ACK
) 
 509                                                 TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 510                                                                                            TCPHeaderIN
->SourcePort
, TCP_Connection_FINWait2
); 
 514                                 case TCP_Connection_FINWait2
: 
 515                                         if (TCPHeaderIN
->Flags 
== (TCP_FLAG_FIN 
| TCP_FLAG_ACK
)) 
 517                                                 ConnectionInfo 
= TCP_GetConnectionInfo(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 518                                                                                                                            TCPHeaderIN
->SourcePort
); 
 520                                                 TCPHeaderOUT
->Flags 
= TCP_FLAG_ACK
; 
 521                                                 PacketResponse      
= true; 
 523                                                 ConnectionInfo
->SequenceNumberIn
++; 
 524                                                 ConnectionInfo
->SequenceNumberOut
++; 
 526                                                 TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 527                                                                                            TCPHeaderIN
->SourcePort
, TCP_Connection_Closed
); 
 531                                 case TCP_Connection_CloseWait
: 
 532                                         if (TCPHeaderIN
->Flags 
== TCP_FLAG_ACK
) 
 534                                                 TCP_SetConnectionState(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 535                                                                                            TCPHeaderIN
->SourcePort
, TCP_Connection_Closed
); 
 544                 /* Port is not open, indicate via a RST/ACK response to the sender */ 
 545                 TCPHeaderOUT
->Flags 
= (TCP_FLAG_RST 
| TCP_FLAG_ACK
);                             
 546                 PacketResponse      
= true; 
 549         /* Check if we need to respond to the sent packet */ 
 552                 ConnectionInfo 
= TCP_GetConnectionInfo(TCPHeaderIN
->DestinationPort
, IPHeaderIN
->SourceAddress
, 
 553                                                        TCPHeaderIN
->SourcePort
); 
 555                 TCPHeaderOUT
->SourcePort           
= TCPHeaderIN
->DestinationPort
; 
 556                 TCPHeaderOUT
->DestinationPort      
= TCPHeaderIN
->SourcePort
; 
 557                 TCPHeaderOUT
->SequenceNumber       
= SwapEndian_32(ConnectionInfo
->SequenceNumberOut
); 
 558                 TCPHeaderOUT
->AcknowledgmentNumber 
= SwapEndian_32(ConnectionInfo
->SequenceNumberIn
); 
 559                 TCPHeaderOUT
->DataOffset           
= (sizeof(TCP_Header_t
) / sizeof(uint32_t)); 
 561                 if (!(ConnectionInfo
->Buffer
.InUse
)) 
 562                   TCPHeaderOUT
->WindowSize         
= SwapEndian_16(TCP_WINDOW_SIZE
); 
 564                   TCPHeaderOUT
->WindowSize         
= SwapEndian_16(TCP_WINDOW_SIZE 
- ConnectionInfo
->Buffer
.Length
); 
 566                 TCPHeaderOUT
->UrgentPointer        
= 0; 
 567                 TCPHeaderOUT
->Checksum             
= 0; 
 568                 TCPHeaderOUT
->Reserved             
= 0; 
 570                 TCPHeaderOUT
->Checksum             
= TCP_Checksum16(TCPHeaderOUT
, IPHeaderIN
->DestinationAddress
, 
 571                                                                     IPHeaderIN
->SourceAddress
, sizeof(TCP_Header_t
));                                    
 573                 return sizeof(TCP_Header_t
);     
 579 /** Calculates the appropriate TCP checksum, consisting of the addition of the one's compliment of each word, 
 582  *  \param TCPHeaderOutStart  Pointer to the start of the packet's outgoing TCP header 
 583  *  \param SourceAddress      Source protocol IP address of the outgoing IP header 
 584  *  \param SourceAddress      DestinationAddress protocol IP address of the outgoing IP header 
 585  *  \param TCPOutSize         Size in bytes of the TCP data header and payload 
 587  *  \return A 16-bit TCP checksum value 
 589 static uint16_t TCP_Checksum16(void* TCPHeaderOutStart
, IP_Address_t SourceAddress
, 
 590                                IP_Address_t DestinationAddress
, uint16_t TCPOutSize
) 
 592         uint32_t Checksum 
= 0; 
 594         /* TCP/IP checksums are the addition of the one's compliment of each word including the IP pseudo-header, 
 597         Checksum 
+= ((uint16_t*)&SourceAddress
)[0]; 
 598         Checksum 
+= ((uint16_t*)&SourceAddress
)[1]; 
 599         Checksum 
+= ((uint16_t*)&DestinationAddress
)[0]; 
 600         Checksum 
+= ((uint16_t*)&DestinationAddress
)[1]; 
 601         Checksum 
+= SwapEndian_16(PROTOCOL_TCP
); 
 602         Checksum 
+= SwapEndian_16(TCPOutSize
); 
 604         for (uint8_t CurrWord 
= 0; CurrWord 
< (TCPOutSize 
>> 1); CurrWord
++) 
 605           Checksum 
+= ((uint16_t*)TCPHeaderOutStart
)[CurrWord
]; 
 607         if (TCPOutSize 
& 0x01) 
 608           Checksum 
+= (((uint16_t*)TCPHeaderOutStart
)[TCPOutSize 
>> 1] & 0x00FF); 
 610         while (Checksum 
& 0xFFFF0000) 
 611           Checksum 
= ((Checksum 
& 0xFFFF) + (Checksum 
>> 16));