Fix "grep: Version.h: No such file or directory" error when including the core LUFA...
[pub/USBasp.git] / Demos / Device / ClassDriver / RNDISEthernet / Lib / TCP.c
index 5889c45..44b5d95 100644 (file)
@@ -1,24 +1,24 @@
 /*
              LUFA Library
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2010.
-              
+     Copyright (C) Dean Camera, 2013.
+
   dean [at] fourwalledcubicle [dot] com
   dean [at] fourwalledcubicle [dot] com
-      www.fourwalledcubicle.com
+           www.lufa-lib.org
 */
 
 /*
 */
 
 /*
-  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
 
-  Permission to use, copy, modify, distribute, and sell this 
+  Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
   software and its documentation for any purpose is hereby granted
-  without fee, provided that the above copyright notice appear in 
+  without fee, provided that the above copyright notice appear in
   all copies and that both that the copyright notice and this
   all copies and that both that the copyright notice and this
-  permission notice and warranty disclaimer appear in supporting 
-  documentation, and that the name of the author not be used in 
-  advertising or publicity pertaining to distribution of the 
+  permission notice and warranty disclaimer appear in supporting
+  documentation, and that the name of the author not be used in
+  advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
   software without specific, written prior permission.
 
-  The author disclaim all warranties with regard to this
+  The author disclaims all warranties with regard to this
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
@@ -34,7 +34,7 @@
  *  and reception of packets to and from devices on a network, to "ports" on the device. It is used in situations where data
  *  delivery must be reliable and correct, e.g. HTTP, TELNET and most other non-streaming protocols.
  */
  *  and reception of packets to and from devices on a network, to "ports" on the device. It is used in situations where data
  *  delivery must be reliable and correct, e.g. HTTP, TELNET and most other non-streaming protocols.
  */
+
 #define  INCLUDE_FROM_TCP_C
 #include "TCP.h"
 
 #define  INCLUDE_FROM_TCP_C
 #include "TCP.h"
 
@@ -55,7 +55,8 @@ TCP_ConnectionState_t  ConnectionStateTable[MAX_TCP_CONNECTIONS];
  *  level. If an application produces a response, this task constructs the appropriate Ethernet frame and places it into the Ethernet OUT
  *  buffer for later transmission.
  */
  *  level. If an application produces a response, this task constructs the appropriate Ethernet frame and places it into the Ethernet OUT
  *  buffer for later transmission.
  */
-void TCP_TCPTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
+void TCP_TCPTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo,
+                        Ethernet_Frame_Info_t* const FrameOUT)
 {
        /* Run each application in sequence, to process incoming and generate outgoing packets */
        for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
 {
        /* Run each application in sequence, to process incoming and generate outgoing packets */
        for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
@@ -64,7 +65,7 @@ void TCP_TCPTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
                for (uint8_t PTableEntry = 0; PTableEntry < MAX_OPEN_TCP_PORTS; PTableEntry++)
                {
                        /* Run the application handler for the port */
                for (uint8_t PTableEntry = 0; PTableEntry < MAX_OPEN_TCP_PORTS; PTableEntry++)
                {
                        /* Run the application handler for the port */
-                       if ((PortStateTable[PTableEntry].Port  == ConnectionStateTable[CSTableEntry].Port) && 
+                       if ((PortStateTable[PTableEntry].Port  == ConnectionStateTable[CSTableEntry].Port) &&
                            (PortStateTable[PTableEntry].State == TCP_Port_Open))
                        {
                                PortStateTable[PTableEntry].ApplicationHandler(&ConnectionStateTable[CSTableEntry],
                            (PortStateTable[PTableEntry].State == TCP_Port_Open))
                        {
                                PortStateTable[PTableEntry].ApplicationHandler(&ConnectionStateTable[CSTableEntry],
@@ -72,14 +73,11 @@ void TCP_TCPTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
                        }
                }
        }
                        }
                }
        }
-       
-       /* Get pointer to the output frame info struct for convenience */
-       Ethernet_Frame_Info_t* FrameOUT = &RNDISInterfaceInfo->State.FrameOUT;
-       
+
        /* Bail out early if there is already a frame waiting to be sent in the Ethernet OUT buffer */
        /* Bail out early if there is already a frame waiting to be sent in the Ethernet OUT buffer */
-       if (FrameOUT->FrameInBuffer)
+       if (FrameOUT->FrameLength)
          return;
          return;
-       
+
        /* Send response packets from each application as the TCP packet buffers are filled by the applications */
        for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
        {
        /* Send response packets from each application as the TCP packet buffers are filled by the applications */
        for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
        {
@@ -111,11 +109,11 @@ void TCP_TCPTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
                        TCPHeaderOUT->Reserved             = 0;
 
                        memcpy(TCPDataOUT, ConnectionStateTable[CSTableEntry].Info.Buffer.Data, PacketSize);
                        TCPHeaderOUT->Reserved             = 0;
 
                        memcpy(TCPDataOUT, ConnectionStateTable[CSTableEntry].Info.Buffer.Data, PacketSize);
-                       
+
                        ConnectionStateTable[CSTableEntry].Info.SequenceNumberOut += PacketSize;
 
                        ConnectionStateTable[CSTableEntry].Info.SequenceNumberOut += PacketSize;
 
-                       TCPHeaderOUT->Checksum             = TCP_Checksum16(TCPHeaderOUT, ServerIPAddress,
-                                                                           ConnectionStateTable[CSTableEntry].RemoteAddress,
+                       TCPHeaderOUT->Checksum             = TCP_Checksum16(TCPHeaderOUT, &ServerIPAddress,
+                                                                           &ConnectionStateTable[CSTableEntry].RemoteAddress,
                                                                            (sizeof(TCP_Header_t) + PacketSize));
 
                        PacketSize += sizeof(TCP_Header_t);
                                                                            (sizeof(TCP_Header_t) + PacketSize));
 
                        PacketSize += sizeof(TCP_Header_t);
@@ -133,11 +131,11 @@ void TCP_TCPTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
                        IPHeaderOUT->TTL                = DEFAULT_TTL;
                        IPHeaderOUT->SourceAddress      = ServerIPAddress;
                        IPHeaderOUT->DestinationAddress = ConnectionStateTable[CSTableEntry].RemoteAddress;
                        IPHeaderOUT->TTL                = DEFAULT_TTL;
                        IPHeaderOUT->SourceAddress      = ServerIPAddress;
                        IPHeaderOUT->DestinationAddress = ConnectionStateTable[CSTableEntry].RemoteAddress;
-                       
+
                        IPHeaderOUT->HeaderChecksum     = Ethernet_Checksum16(IPHeaderOUT, sizeof(IP_Header_t));
                        IPHeaderOUT->HeaderChecksum     = Ethernet_Checksum16(IPHeaderOUT, sizeof(IP_Header_t));
-               
+
                        PacketSize += sizeof(IP_Header_t);
                        PacketSize += sizeof(IP_Header_t);
-               
+
                        /* Fill out the response Ethernet frame header */
                        FrameOUTHeader->Source          = ServerMACAddress;
                        FrameOUTHeader->Destination     = (MAC_Address_t){{0x02, 0x00, 0x02, 0x00, 0x02, 0x00}};
                        /* Fill out the response Ethernet frame header */
                        FrameOUTHeader->Source          = ServerMACAddress;
                        FrameOUTHeader->Destination     = (MAC_Address_t){{0x02, 0x00, 0x02, 0x00, 0x02, 0x00}};
@@ -147,16 +145,15 @@ void TCP_TCPTask(USB_ClassInfo_RNDIS_Device_t* const RNDISInterfaceInfo)
 
                        /* Set the response length in the buffer and indicate that a response is ready to be sent */
                        FrameOUT->FrameLength           = PacketSize;
 
                        /* Set the response length in the buffer and indicate that a response is ready to be sent */
                        FrameOUT->FrameLength           = PacketSize;
-                       FrameOUT->FrameInBuffer         = true;
-                       
+
                        ConnectionStateTable[CSTableEntry].Info.Buffer.Ready = false;
                        ConnectionStateTable[CSTableEntry].Info.Buffer.Ready = false;
-                       
+
                        break;
                }
        }
 }
 
                        break;
                }
        }
 }
 
-/** Initialises the TCP protocol handler, clearing the port and connection state tables. This must be called before TCP packets are
+/** Initializes the TCP protocol handler, clearing the port and connection state tables. This must be called before TCP packets are
  *  processed.
  */
 void TCP_Init(void)
  *  processed.
  */
 void TCP_Init(void)
@@ -173,7 +170,7 @@ void TCP_Init(void)
 /** Sets the state and callback handler of the given port, specified in big endian to the given state.
  *
  *  \param[in] Port     Port whose state and callback function to set, specified in big endian
 /** Sets the state and callback handler of the given port, specified in big endian to the given state.
  *
  *  \param[in] Port     Port whose state and callback function to set, specified in big endian
- *  \param[in] State    New state of the port, a value from the TCP_PortStates_t enum
+ *  \param[in] State    New state of the port, a value from the \ref TCP_PortStates_t enum
  *  \param[in] Handler  Application callback handler for the port
  *
  *  \return Boolean true if the port state was set, false otherwise (no more space in the port state table)
  *  \param[in] Handler  Application callback handler for the port
  *
  *  \return Boolean true if the port state was set, false otherwise (no more space in the port state table)
@@ -185,7 +182,7 @@ bool TCP_SetPortState(const uint16_t Port,
        /* Note, Port number should be specified in BIG endian to simplify network code */
 
        /* Check to see if the port entry is already in the port state table */
        /* Note, Port number should be specified in BIG endian to simplify network code */
 
        /* Check to see if the port entry is already in the port state table */
-       for (uint8_t PTableEntry = 0; PTableEntry < MAX_TCP_CONNECTIONS; PTableEntry++)
+       for (uint8_t PTableEntry = 0; PTableEntry < MAX_OPEN_TCP_PORTS; PTableEntry++)
        {
                /* Find existing entry for the port in the table, update it if found */
                if (PortStateTable[PTableEntry].Port == Port)
        {
                /* Find existing entry for the port in the table, update it if found */
                if (PortStateTable[PTableEntry].Port == Port)
@@ -199,7 +196,7 @@ bool TCP_SetPortState(const uint16_t Port,
        /* Check if trying to open the port -- if so we need to find an unused (closed) entry and replace it */
        if (State == TCP_Port_Open)
        {
        /* Check if trying to open the port -- if so we need to find an unused (closed) entry and replace it */
        if (State == TCP_Port_Open)
        {
-               for (uint8_t PTableEntry = 0; PTableEntry < MAX_TCP_CONNECTIONS; PTableEntry++)
+               for (uint8_t PTableEntry = 0; PTableEntry < MAX_OPEN_TCP_PORTS; PTableEntry++)
                {
                        /* Find a closed port entry in the table, change it to the given port and state */
                        if (PortStateTable[PTableEntry].State == TCP_Port_Closed)
                {
                        /* Find a closed port entry in the table, change it to the given port and state */
                        if (PortStateTable[PTableEntry].State == TCP_Port_Closed)
@@ -210,7 +207,7 @@ bool TCP_SetPortState(const uint16_t Port,
                                return true;
                        }
                }
                                return true;
                        }
                }
-               
+
                /* Port not in table and no room to add it, return failure */
                return false;
        }
                /* Port not in table and no room to add it, return failure */
                return false;
        }
@@ -225,19 +222,19 @@ bool TCP_SetPortState(const uint16_t Port,
  *
  *  \param[in] Port  TCP port whose state is to be retrieved, given in big-endian
  *
  *
  *  \param[in] Port  TCP port whose state is to be retrieved, given in big-endian
  *
- *  \return A value from the TCP_PortStates_t enum
+ *  \return A value from the \ref TCP_PortStates_t enum
  */
 uint8_t TCP_GetPortState(const uint16_t Port)
 {
        /* Note, Port number should be specified in BIG endian to simplify network code */
 
  */
 uint8_t TCP_GetPortState(const uint16_t Port)
 {
        /* Note, Port number should be specified in BIG endian to simplify network code */
 
-       for (uint8_t PTableEntry = 0; PTableEntry < MAX_TCP_CONNECTIONS; PTableEntry++)
+       for (uint8_t PTableEntry = 0; PTableEntry < MAX_OPEN_TCP_PORTS; PTableEntry++)
        {
                /* Find existing entry for the port in the table, return the port status if found */
                if (PortStateTable[PTableEntry].Port == Port)
                  return PortStateTable[PTableEntry].State;
        }
        {
                /* Find existing entry for the port in the table, return the port status if found */
                if (PortStateTable[PTableEntry].Port == Port)
                  return PortStateTable[PTableEntry].State;
        }
-       
+
        /* Port not in table, assume closed */
        return TCP_Port_Closed;
 }
        /* Port not in table, assume closed */
        return TCP_Port_Closed;
 }
@@ -248,12 +245,12 @@ uint8_t TCP_GetPortState(const uint16_t Port)
  *  \param[in] Port           TCP port of the connection on the device, specified in big endian
  *  \param[in] RemoteAddress  Remote protocol IP address of the connected device
  *  \param[in] RemotePort     TCP port of the remote device in the connection, specified in big endian
  *  \param[in] Port           TCP port of the connection on the device, specified in big endian
  *  \param[in] RemoteAddress  Remote protocol IP address of the connected device
  *  \param[in] RemotePort     TCP port of the remote device in the connection, specified in big endian
- *  \param[in] State          TCP connection state, a value from the TCP_ConnectionStates_t enum
+ *  \param[in] State          TCP connection state, a value from the \ref TCP_ConnectionStates_t enum
  *
  *  \return Boolean true if the connection was updated or created, false otherwise (no more space in the connection state table)
  */
 bool TCP_SetConnectionState(const uint16_t Port,
  *
  *  \return Boolean true if the connection was updated or created, false otherwise (no more space in the connection state table)
  */
 bool TCP_SetConnectionState(const uint16_t Port,
-                            const IP_Address_t RemoteAddress,
+                            const IP_Address_t* RemoteAddress,
                             const uint16_t RemotePort,
                             const uint8_t State)
 {
                             const uint16_t RemotePort,
                             const uint8_t State)
 {
@@ -263,27 +260,27 @@ bool TCP_SetConnectionState(const uint16_t Port,
        {
                /* Find port entry in the table */
                if ((ConnectionStateTable[CSTableEntry].Port == Port) &&
        {
                /* Find port entry in the table */
                if ((ConnectionStateTable[CSTableEntry].Port == Port) &&
-                    IP_COMPARE(&ConnectionStateTable[CSTableEntry].RemoteAddress, &RemoteAddress) &&
+                    IP_COMPARE(&ConnectionStateTable[CSTableEntry].RemoteAddress, RemoteAddress) &&
                         ConnectionStateTable[CSTableEntry].RemotePort == RemotePort)
                {
                        ConnectionStateTable[CSTableEntry].State = State;
                        return true;
                }
        }
                         ConnectionStateTable[CSTableEntry].RemotePort == RemotePort)
                {
                        ConnectionStateTable[CSTableEntry].State = State;
                        return true;
                }
        }
-       
+
        for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
        {
                /* Find empty entry in the table */
                if (ConnectionStateTable[CSTableEntry].State == TCP_Connection_Closed)
                {
                        ConnectionStateTable[CSTableEntry].Port          = Port;
        for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)
        {
                /* Find empty entry in the table */
                if (ConnectionStateTable[CSTableEntry].State == TCP_Connection_Closed)
                {
                        ConnectionStateTable[CSTableEntry].Port          = Port;
-                       ConnectionStateTable[CSTableEntry].RemoteAddress = RemoteAddress;                       
+                       ConnectionStateTable[CSTableEntry].RemoteAddress = *RemoteAddress;
                        ConnectionStateTable[CSTableEntry].RemotePort    = RemotePort;
                        ConnectionStateTable[CSTableEntry].State         = State;
                        return true;
                }
        }
                        ConnectionStateTable[CSTableEntry].RemotePort    = RemotePort;
                        ConnectionStateTable[CSTableEntry].State         = State;
                        return true;
                }
        }
-       
+
        return false;
 }
 
        return false;
 }
 
@@ -293,10 +290,10 @@ bool TCP_SetConnectionState(const uint16_t Port,
  *  \param[in] RemoteAddress  Remote protocol IP address of the connected host
  *  \param[in] RemotePort     Remote TCP port of the connected host, specified in big endian
  *
  *  \param[in] RemoteAddress  Remote protocol IP address of the connected host
  *  \param[in] RemotePort     Remote TCP port of the connected host, specified in big endian
  *
- *  \return A value from the TCP_ConnectionStates_t enum
+ *  \return A value from the \ref TCP_ConnectionStates_t enum
  */
 uint8_t TCP_GetConnectionState(const uint16_t Port,
  */
 uint8_t TCP_GetConnectionState(const uint16_t Port,
-                               const IP_Address_t RemoteAddress,
+                               const IP_Address_t* RemoteAddress,
                                const uint16_t RemotePort)
 {
        /* Note, Port number should be specified in BIG endian to simplify network code */
                                const uint16_t RemotePort)
 {
        /* Note, Port number should be specified in BIG endian to simplify network code */
@@ -305,14 +302,14 @@ uint8_t TCP_GetConnectionState(const uint16_t Port,
        {
                /* Find port entry in the table */
                if ((ConnectionStateTable[CSTableEntry].Port == Port) &&
        {
                /* Find port entry in the table */
                if ((ConnectionStateTable[CSTableEntry].Port == Port) &&
-                    IP_COMPARE(&ConnectionStateTable[CSTableEntry].RemoteAddress, &RemoteAddress) &&
+                    IP_COMPARE(&ConnectionStateTable[CSTableEntry].RemoteAddress, RemoteAddress) &&
                         ConnectionStateTable[CSTableEntry].RemotePort == RemotePort)
                         ConnectionStateTable[CSTableEntry].RemotePort == RemotePort)
-                        
+
                {
                        return ConnectionStateTable[CSTableEntry].State;
                }
        }
                {
                        return ConnectionStateTable[CSTableEntry].State;
                }
        }
-       
+
        return TCP_Connection_Closed;
 }
 
        return TCP_Connection_Closed;
 }
 
@@ -325,7 +322,7 @@ uint8_t TCP_GetConnectionState(const uint16_t Port,
  *  \return ConnectionInfo structure of the connection if found, NULL otherwise
  */
 TCP_ConnectionInfo_t* TCP_GetConnectionInfo(const uint16_t Port,
  *  \return ConnectionInfo structure of the connection if found, NULL otherwise
  */
 TCP_ConnectionInfo_t* TCP_GetConnectionInfo(const uint16_t Port,
-                                            const IP_Address_t RemoteAddress,
+                                            const IP_Address_t* RemoteAddress,
                                             const uint16_t RemotePort)
 {
        /* Note, Port number should be specified in BIG endian to simplify network code */
                                             const uint16_t RemotePort)
 {
        /* Note, Port number should be specified in BIG endian to simplify network code */
@@ -334,13 +331,13 @@ TCP_ConnectionInfo_t* TCP_GetConnectionInfo(const uint16_t Port,
        {
                /* Find port entry in the table */
                if ((ConnectionStateTable[CSTableEntry].Port == Port) &&
        {
                /* Find port entry in the table */
                if ((ConnectionStateTable[CSTableEntry].Port == Port) &&
-                    IP_COMPARE(&ConnectionStateTable[CSTableEntry].RemoteAddress, &RemoteAddress) &&
+                    IP_COMPARE(&ConnectionStateTable[CSTableEntry].RemoteAddress, RemoteAddress) &&
                         ConnectionStateTable[CSTableEntry].RemotePort == RemotePort)
                {
                        return &ConnectionStateTable[CSTableEntry].Info;
                }
        }
                         ConnectionStateTable[CSTableEntry].RemotePort == RemotePort)
                {
                        return &ConnectionStateTable[CSTableEntry].Info;
                }
        }
-       
+
        return NULL;
 }
 
        return NULL;
 }
 
@@ -364,43 +361,43 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
        TCP_Header_t* TCPHeaderOUT = (TCP_Header_t*)TCPHeaderOutStart;
 
        TCP_ConnectionInfo_t* ConnectionInfo;
        TCP_Header_t* TCPHeaderOUT = (TCP_Header_t*)TCPHeaderOutStart;
 
        TCP_ConnectionInfo_t* ConnectionInfo;
-       
+
        DecodeTCPHeader(TCPHeaderInStart);
 
        bool PacketResponse = false;
        DecodeTCPHeader(TCPHeaderInStart);
 
        bool PacketResponse = false;
-               
+
        /* Check if the destination port is open and allows incoming connections */
        if (TCP_GetPortState(TCPHeaderIN->DestinationPort) == TCP_Port_Open)
        {
                /* Detect SYN from host to start a connection */
                if (TCPHeaderIN->Flags & TCP_FLAG_SYN)
        /* Check if the destination port is open and allows incoming connections */
        if (TCP_GetPortState(TCPHeaderIN->DestinationPort) == TCP_Port_Open)
        {
                /* Detect SYN from host to start a connection */
                if (TCPHeaderIN->Flags & TCP_FLAG_SYN)
-                 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort, TCP_Connection_Listen);
+                 TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort, TCP_Connection_Listen);
 
                /* Detect RST from host to abort existing connection */
                if (TCPHeaderIN->Flags & TCP_FLAG_RST)
                {
 
                /* Detect RST from host to abort existing connection */
                if (TCPHeaderIN->Flags & TCP_FLAG_RST)
                {
-                       if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                       if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                   TCPHeaderIN->SourcePort, TCP_Connection_Closed))
                        {
                                                   TCPHeaderIN->SourcePort, TCP_Connection_Closed))
                        {
-                               TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK);                            
-                               PacketResponse = true;                  
+                               TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK);
+                               PacketResponse = true;
                        }
                }
                else
                {
                        /* Process the incoming TCP packet based on the current connection state for the sender and port */
                        }
                }
                else
                {
                        /* Process the incoming TCP packet based on the current connection state for the sender and port */
-                       switch (TCP_GetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort))
+                       switch (TCP_GetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort))
                        {
                                case TCP_Connection_Listen:
                                        if (TCPHeaderIN->Flags == TCP_FLAG_SYN)
                                        {
                                                /* SYN connection starts a connection with a peer */
                        {
                                case TCP_Connection_Listen:
                                        if (TCPHeaderIN->Flags == TCP_FLAG_SYN)
                                        {
                                                /* SYN connection starts a connection with a peer */
-                                               if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                           TCPHeaderIN->SourcePort, TCP_Connection_SYNReceived))
                                                {
                                                                           TCPHeaderIN->SourcePort, TCP_Connection_SYNReceived))
                                                {
-                                                       TCPHeaderOUT->Flags = (TCP_FLAG_SYN | TCP_FLAG_ACK);                                            
+                                                       TCPHeaderOUT->Flags = (TCP_FLAG_SYN | TCP_FLAG_ACK);
 
 
-                                                       ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort);
+                                                       ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort);
 
                                                        ConnectionInfo->SequenceNumberIn  = (SwapEndian_32(TCPHeaderIN->SequenceNumber) + 1);
                                                        ConnectionInfo->SequenceNumberOut = 0;
 
                                                        ConnectionInfo->SequenceNumberIn  = (SwapEndian_32(TCPHeaderIN->SequenceNumber) + 1);
                                                        ConnectionInfo->SequenceNumberOut = 0;
@@ -410,38 +407,38 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
                                                {
                                                        TCPHeaderOUT->Flags = TCP_FLAG_RST;
                                                }
                                                {
                                                        TCPHeaderOUT->Flags = TCP_FLAG_RST;
                                                }
-                                                                                          
+
                                                PacketResponse      = true;
                                        }
                                                PacketResponse      = true;
                                        }
-                                       
+
                                        break;
                                case TCP_Connection_SYNReceived:
                                        if (TCPHeaderIN->Flags == TCP_FLAG_ACK)
                                        {
                                                /* ACK during the connection process completes the connection to a peer */
 
                                        break;
                                case TCP_Connection_SYNReceived:
                                        if (TCPHeaderIN->Flags == TCP_FLAG_ACK)
                                        {
                                                /* ACK during the connection process completes the connection to a peer */
 
-                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_Established);
 
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_Established);
 
-                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                                                           TCPHeaderIN->SourcePort);
                                                                                                                           TCPHeaderIN->SourcePort);
-                                                                                                                          
+
                                                ConnectionInfo->SequenceNumberOut++;
                                        }
                                                ConnectionInfo->SequenceNumberOut++;
                                        }
-                                       
+
                                        break;
                                case TCP_Connection_Established:
                                        if (TCPHeaderIN->Flags == (TCP_FLAG_FIN | TCP_FLAG_ACK))
                                        {
                                                /* FIN ACK when connected to a peer starts the finalization process */
                                        break;
                                case TCP_Connection_Established:
                                        if (TCPHeaderIN->Flags == (TCP_FLAG_FIN | TCP_FLAG_ACK))
                                        {
                                                /* FIN ACK when connected to a peer starts the finalization process */
-                                       
-                                               TCPHeaderOUT->Flags = (TCP_FLAG_FIN | TCP_FLAG_ACK);                            
+
+                                               TCPHeaderOUT->Flags = (TCP_FLAG_FIN | TCP_FLAG_ACK);
                                                PacketResponse      = true;
                                                PacketResponse      = true;
-                                               
-                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+
+                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_CloseWait);
 
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_CloseWait);
 
-                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                                                           TCPHeaderIN->SourcePort);
 
                                                ConnectionInfo->SequenceNumberIn++;
                                                                                                                           TCPHeaderIN->SourcePort);
 
                                                ConnectionInfo->SequenceNumberIn++;
@@ -449,17 +446,17 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
                                        }
                                        else if ((TCPHeaderIN->Flags == TCP_FLAG_ACK) || (TCPHeaderIN->Flags == (TCP_FLAG_ACK | TCP_FLAG_PSH)))
                                        {
                                        }
                                        else if ((TCPHeaderIN->Flags == TCP_FLAG_ACK) || (TCPHeaderIN->Flags == (TCP_FLAG_ACK | TCP_FLAG_PSH)))
                                        {
-                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                                                           TCPHeaderIN->SourcePort);
 
                                                                                                                           TCPHeaderIN->SourcePort);
 
-                                               /* Check if the buffer is currently in use either by a buffered data to send, or receive */             
+                                               /* Check if the buffer is currently in use either by a buffered data to send, or receive */
                                                if ((ConnectionInfo->Buffer.InUse == false) && (ConnectionInfo->Buffer.Ready == false))
                                                if ((ConnectionInfo->Buffer.InUse == false) && (ConnectionInfo->Buffer.Ready == false))
-                                               {                                               
+                                               {
                                                        ConnectionInfo->Buffer.Direction = TCP_PACKETDIR_IN;
                                                        ConnectionInfo->Buffer.InUse     = true;
                                                        ConnectionInfo->Buffer.Length    = 0;
                                                }
                                                        ConnectionInfo->Buffer.Direction = TCP_PACKETDIR_IN;
                                                        ConnectionInfo->Buffer.InUse     = true;
                                                        ConnectionInfo->Buffer.Length    = 0;
                                                }
-                                               
+
                                                /* Check if the buffer has been claimed by us to read in data from the peer */
                                                if ((ConnectionInfo->Buffer.Direction == TCP_PACKETDIR_IN) &&
                                                        (ConnectionInfo->Buffer.Length != TCP_WINDOW_SIZE))
                                                /* Check if the buffer has been claimed by us to read in data from the peer */
                                                if ((ConnectionInfo->Buffer.Direction == TCP_PACKETDIR_IN) &&
                                                        (ConnectionInfo->Buffer.Length != TCP_WINDOW_SIZE))
@@ -475,7 +472,7 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
 
                                                        ConnectionInfo->SequenceNumberIn += DataLength;
                                                        ConnectionInfo->Buffer.Length    += DataLength;
 
                                                        ConnectionInfo->SequenceNumberIn += DataLength;
                                                        ConnectionInfo->Buffer.Length    += DataLength;
-                                                       
+
                                                        /* Check if the buffer is full or if the PSH flag is set, if so indicate buffer ready */
                                                        if ((!(TCP_WINDOW_SIZE - ConnectionInfo->Buffer.Length)) || (TCPHeaderIN->Flags & TCP_FLAG_PSH))
                                                        {
                                                        /* Check if the buffer is full or if the PSH flag is set, if so indicate buffer ready */
                                                        if ((!(TCP_WINDOW_SIZE - ConnectionInfo->Buffer.Length)) || (TCPHeaderIN->Flags & TCP_FLAG_PSH))
                                                        {
@@ -492,25 +489,25 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
                                                        return NO_PROCESS;
                                                }
                                        }
                                                        return NO_PROCESS;
                                                }
                                        }
-                                       
+
                                        break;
                                case TCP_Connection_Closing:
                                        break;
                                case TCP_Connection_Closing:
-                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                                                           TCPHeaderIN->SourcePort);
 
                                                TCPHeaderOUT->Flags = (TCP_FLAG_ACK | TCP_FLAG_FIN);
                                                PacketResponse      = true;
                                                                                                                           TCPHeaderIN->SourcePort);
 
                                                TCPHeaderOUT->Flags = (TCP_FLAG_ACK | TCP_FLAG_FIN);
                                                PacketResponse      = true;
-                                               
+
                                                ConnectionInfo->Buffer.InUse = false;
                                                ConnectionInfo->Buffer.InUse = false;
-                                               
-                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+
+                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_FINWait1);
 
                                        break;
                                case TCP_Connection_FINWait1:
                                        if (TCPHeaderIN->Flags == (TCP_FLAG_FIN | TCP_FLAG_ACK))
                                        {
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_FINWait1);
 
                                        break;
                                case TCP_Connection_FINWait1:
                                        if (TCPHeaderIN->Flags == (TCP_FLAG_FIN | TCP_FLAG_ACK))
                                        {
-                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                                                           TCPHeaderIN->SourcePort);
 
                                                TCPHeaderOUT->Flags = TCP_FLAG_ACK;
                                                                                                                           TCPHeaderIN->SourcePort);
 
                                                TCPHeaderOUT->Flags = TCP_FLAG_ACK;
@@ -518,21 +515,21 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
 
                                                ConnectionInfo->SequenceNumberIn++;
                                                ConnectionInfo->SequenceNumberOut++;
 
                                                ConnectionInfo->SequenceNumberIn++;
                                                ConnectionInfo->SequenceNumberOut++;
-                                               
-                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+
+                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_Closed);
                                        }
                                        else if (TCPHeaderIN->Flags == TCP_FLAG_ACK)
                                        {
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_Closed);
                                        }
                                        else if (TCPHeaderIN->Flags == TCP_FLAG_ACK)
                                        {
-                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_FINWait2);
                                        }
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_FINWait2);
                                        }
-                                       
+
                                        break;
                                case TCP_Connection_FINWait2:
                                        if (TCPHeaderIN->Flags == (TCP_FLAG_FIN | TCP_FLAG_ACK))
                                        {
                                        break;
                                case TCP_Connection_FINWait2:
                                        if (TCPHeaderIN->Flags == (TCP_FLAG_FIN | TCP_FLAG_ACK))
                                        {
-                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                                                           TCPHeaderIN->SourcePort);
 
                                                TCPHeaderOUT->Flags = TCP_FLAG_ACK;
                                                                                                                           TCPHeaderIN->SourcePort);
 
                                                TCPHeaderOUT->Flags = TCP_FLAG_ACK;
@@ -540,19 +537,19 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
 
                                                ConnectionInfo->SequenceNumberIn++;
                                                ConnectionInfo->SequenceNumberOut++;
 
                                                ConnectionInfo->SequenceNumberIn++;
                                                ConnectionInfo->SequenceNumberOut++;
-                                               
-                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+
+                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_Closed);
                                        }
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_Closed);
                                        }
-                               
+
                                        break;
                                case TCP_Connection_CloseWait:
                                        if (TCPHeaderIN->Flags == TCP_FLAG_ACK)
                                        {
                                        break;
                                case TCP_Connection_CloseWait:
                                        if (TCPHeaderIN->Flags == TCP_FLAG_ACK)
                                        {
-                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_Closed);
                                        }
                                                                                           TCPHeaderIN->SourcePort, TCP_Connection_Closed);
                                        }
-                                       
+
                                        break;
                        }
                }
                                        break;
                        }
                }
@@ -560,14 +557,14 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
        else
        {
                /* Port is not open, indicate via a RST/ACK response to the sender */
        else
        {
                /* Port is not open, indicate via a RST/ACK response to the sender */
-               TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK);                            
+               TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK);
                PacketResponse      = true;
        }
                PacketResponse      = true;
        }
-       
+
        /* Check if we need to respond to the sent packet */
        if (PacketResponse)
        {
        /* Check if we need to respond to the sent packet */
        if (PacketResponse)
        {
-               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,
+               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, &IPHeaderIN->SourceAddress,
                                                       TCPHeaderIN->SourcePort);
 
                TCPHeaderOUT->SourcePort           = TCPHeaderIN->DestinationPort;
                                                       TCPHeaderIN->SourcePort);
 
                TCPHeaderOUT->SourcePort           = TCPHeaderIN->DestinationPort;
@@ -575,7 +572,7 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
                TCPHeaderOUT->SequenceNumber       = SwapEndian_32(ConnectionInfo->SequenceNumberOut);
                TCPHeaderOUT->AcknowledgmentNumber = SwapEndian_32(ConnectionInfo->SequenceNumberIn);
                TCPHeaderOUT->DataOffset           = (sizeof(TCP_Header_t) / sizeof(uint32_t));
                TCPHeaderOUT->SequenceNumber       = SwapEndian_32(ConnectionInfo->SequenceNumberOut);
                TCPHeaderOUT->AcknowledgmentNumber = SwapEndian_32(ConnectionInfo->SequenceNumberIn);
                TCPHeaderOUT->DataOffset           = (sizeof(TCP_Header_t) / sizeof(uint32_t));
-               
+
                if (!(ConnectionInfo->Buffer.InUse))
                  TCPHeaderOUT->WindowSize         = SwapEndian_16(TCP_WINDOW_SIZE);
                else
                if (!(ConnectionInfo->Buffer.InUse))
                  TCPHeaderOUT->WindowSize         = SwapEndian_16(TCP_WINDOW_SIZE);
                else
@@ -584,11 +581,11 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
                TCPHeaderOUT->UrgentPointer        = 0;
                TCPHeaderOUT->Checksum             = 0;
                TCPHeaderOUT->Reserved             = 0;
                TCPHeaderOUT->UrgentPointer        = 0;
                TCPHeaderOUT->Checksum             = 0;
                TCPHeaderOUT->Reserved             = 0;
-               
-               TCPHeaderOUT->Checksum             = TCP_Checksum16(TCPHeaderOUT, IPHeaderIN->DestinationAddress,
-                                                                   IPHeaderIN->SourceAddress, sizeof(TCP_Header_t));                                   
 
 
-               return sizeof(TCP_Header_t);    
+               TCPHeaderOUT->Checksum             = TCP_Checksum16(TCPHeaderOUT, &IPHeaderIN->DestinationAddress,
+                                                                   &IPHeaderIN->SourceAddress, sizeof(TCP_Header_t));
+
+               return sizeof(TCP_Header_t);
        }
 
        return NO_RESPONSE;
        }
 
        return NO_RESPONSE;
@@ -605,30 +602,31 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart,
  *  \return A 16-bit TCP checksum value
  */
 static uint16_t TCP_Checksum16(void* TCPHeaderOutStart,
  *  \return A 16-bit TCP checksum value
  */
 static uint16_t TCP_Checksum16(void* TCPHeaderOutStart,
-                               const IP_Address_t SourceAddress,
-                               const IP_Address_t DestinationAddress,
-                               const uint16_t TCPOutSize)
+                               const IP_Address_t* SourceAddress,
+                               const IP_Address_t* DestinationAddress,
+                               uint16_t TCPOutSize)
 {
        uint32_t Checksum = 0;
 {
        uint32_t Checksum = 0;
-       
+
        /* TCP/IP checksums are the addition of the one's compliment of each word including the IP pseudo-header,
           complimented */
        /* TCP/IP checksums are the addition of the one's compliment of each word including the IP pseudo-header,
           complimented */
-       
-       Checksum += ((uint16_t*)&SourceAddress)[0];
-       Checksum += ((uint16_t*)&SourceAddress)[1];
-       Checksum += ((uint16_t*)&DestinationAddress)[0];
-       Checksum += ((uint16_t*)&DestinationAddress)[1];
+
+       Checksum += ((uint16_t*)SourceAddress)[0];
+       Checksum += ((uint16_t*)SourceAddress)[1];
+       Checksum += ((uint16_t*)DestinationAddress)[0];
+       Checksum += ((uint16_t*)DestinationAddress)[1];
        Checksum += SwapEndian_16(PROTOCOL_TCP);
        Checksum += SwapEndian_16(TCPOutSize);
 
        for (uint16_t CurrWord = 0; CurrWord < (TCPOutSize >> 1); CurrWord++)
          Checksum += ((uint16_t*)TCPHeaderOutStart)[CurrWord];
        Checksum += SwapEndian_16(PROTOCOL_TCP);
        Checksum += SwapEndian_16(TCPOutSize);
 
        for (uint16_t CurrWord = 0; CurrWord < (TCPOutSize >> 1); CurrWord++)
          Checksum += ((uint16_t*)TCPHeaderOutStart)[CurrWord];
-       
+
        if (TCPOutSize & 0x01)
          Checksum += (((uint16_t*)TCPHeaderOutStart)[TCPOutSize >> 1] & 0x00FF);
        if (TCPOutSize & 0x01)
          Checksum += (((uint16_t*)TCPHeaderOutStart)[TCPOutSize >> 1] & 0x00FF);
-         
+
        while (Checksum & 0xFFFF0000)
          Checksum = ((Checksum & 0xFFFF) + (Checksum >> 16));
        while (Checksum & 0xFFFF0000)
          Checksum = ((Checksum & 0xFFFF) + (Checksum >> 16));
-       
+
        return ~Checksum;
 }
        return ~Checksum;
 }
+