Device mode class driver callbacks are now fired before the control request status...
[pub/USBasp.git] / Demos / Device / LowLevel / RNDISEthernet / Lib / TCP.c
index b7b225a..74d3a78 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -38,7 +38,6 @@
 #define  INCLUDE_FROM_TCP_C\r
 #include "TCP.h"\r
 \r
 #define  INCLUDE_FROM_TCP_C\r
 #include "TCP.h"\r
 \r
-/* Global Variables: */\r
 /** Port state table array. This contains the current status of TCP ports in the device. To save on space, only open ports are\r
  *  stored - closed ports may be overwritten at any time, and the system will assume any ports not present in the array are closed. This\r
  *  allows for MAX_OPEN_TCP_PORTS to be less than the number of ports used by the application if desired.\r
 /** Port state table array. This contains the current status of TCP ports in the device. To save on space, only open ports are\r
  *  stored - closed ports may be overwritten at any time, and the system will assume any ports not present in the array are closed. This\r
  *  allows for MAX_OPEN_TCP_PORTS to be less than the number of ports used by the application if desired.\r
@@ -58,19 +57,18 @@ TCP_ConnectionState_t  ConnectionStateTable[MAX_TCP_CONNECTIONS];
  */\r
 void TCP_Task(void)\r
 {\r
  */\r
 void TCP_Task(void)\r
 {\r
-       /* Task to hand off TCP packets to and from the listening applications. */\r
-\r
        /* Run each application in sequence, to process incoming and generate outgoing packets */\r
        for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)\r
        {\r
                /* Find the corresponding port entry in the port table */\r
        /* Run each application in sequence, to process incoming and generate outgoing packets */\r
        for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++)\r
        {\r
                /* Find the corresponding port entry in the port table */\r
-               for (uint8_t PTableEntry = 0; PTableEntry < MAX_TCP_CONNECTIONS; PTableEntry++)\r
+               for (uint8_t PTableEntry = 0; PTableEntry < MAX_OPEN_TCP_PORTS; PTableEntry++)\r
                {\r
                        /* Run the application handler for the port */\r
                        if ((PortStateTable[PTableEntry].Port  == ConnectionStateTable[CSTableEntry].Port) && \r
                            (PortStateTable[PTableEntry].State == TCP_Port_Open))\r
                        {\r
                {\r
                        /* Run the application handler for the port */\r
                        if ((PortStateTable[PTableEntry].Port  == ConnectionStateTable[CSTableEntry].Port) && \r
                            (PortStateTable[PTableEntry].State == TCP_Port_Open))\r
                        {\r
-                               PortStateTable[PTableEntry].ApplicationHandler(&ConnectionStateTable[CSTableEntry], &ConnectionStateTable[CSTableEntry].Info.Buffer);\r
+                               PortStateTable[PTableEntry].ApplicationHandler(&ConnectionStateTable[CSTableEntry],\r
+                                                                              &ConnectionStateTable[CSTableEntry].Info.Buffer);\r
                        }\r
                }\r
        }\r
                        }\r
                }\r
        }\r
@@ -89,7 +87,7 @@ void TCP_Task(void)
                        Ethernet_Frame_Header_t* FrameOUTHeader = (Ethernet_Frame_Header_t*)&FrameOUT.FrameData;\r
                        IP_Header_t*             IPHeaderOUT    = (IP_Header_t*)&FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t)];\r
                        TCP_Header_t*            TCPHeaderOUT   = (TCP_Header_t*)&FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t) +\r
                        Ethernet_Frame_Header_t* FrameOUTHeader = (Ethernet_Frame_Header_t*)&FrameOUT.FrameData;\r
                        IP_Header_t*             IPHeaderOUT    = (IP_Header_t*)&FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t)];\r
                        TCP_Header_t*            TCPHeaderOUT   = (TCP_Header_t*)&FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t) +\r
-                                                                                                    sizeof(IP_Header_t)];                                              \r
+                                                                                                    sizeof(IP_Header_t)];\r
                        void*                    TCPDataOUT     = &FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t) +\r
                                                                                      sizeof(IP_Header_t) +\r
                                                                                      sizeof(TCP_Header_t)];\r
                        void*                    TCPDataOUT     = &FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t) +\r
                                                                                      sizeof(IP_Header_t) +\r
                                                                                      sizeof(TCP_Header_t)];\r
@@ -171,9 +169,9 @@ void TCP_Init(void)
 \r
 /** Sets the state and callback handler of the given port, specified in big endian to the given state.\r
  *\r
 \r
 /** Sets the state and callback handler of the given port, specified in big endian to the given state.\r
  *\r
- *  \param Port     Port whose state and callback function to set, specified in big endian\r
- *  \param State    New state of the port, a value from the TCP_PortStates_t enum\r
- *  \param Handler  Application callback handler for the port\r
+ *  \param[in] Port     Port whose state and callback function to set, specified in big endian\r
+ *  \param[in] State    New state of the port, a value from the TCP_PortStates_t enum\r
+ *  \param[in] Handler  Application callback handler for the port\r
  *\r
  *  \return Boolean true if the port state was set, false otherwise (no more space in the port state table)\r
  */\r
  *\r
  *  \return Boolean true if the port state was set, false otherwise (no more space in the port state table)\r
  */\r
@@ -220,7 +218,7 @@ bool TCP_SetPortState(uint16_t Port, uint8_t State, void (*Handler)(TCP_Connecti
 \r
 /** Retrieves the current state of a given TCP port, specified in big endian.\r
  *\r
 \r
 /** Retrieves the current state of a given TCP port, specified in big endian.\r
  *\r
- *  \param Port  TCP port whose state is to be retrieved, given in big-endian\r
+ *  \param[in] Port  TCP port whose state is to be retrieved, given in big-endian\r
  *\r
  *  \return A value from the TCP_PortStates_t enum\r
  */\r
  *\r
  *  \return A value from the TCP_PortStates_t enum\r
  */\r
@@ -242,10 +240,10 @@ uint8_t TCP_GetPortState(uint16_t Port)
 /** Sets the connection state of the given port, remote address and remote port to the given TCP connection state. If the\r
  *  connection exists in the connection state table it is updated, otherwise it is created if possible.\r
  *\r
 /** Sets the connection state of the given port, remote address and remote port to the given TCP connection state. If the\r
  *  connection exists in the connection state table it is updated, otherwise it is created if possible.\r
  *\r
- *  \param Port           TCP port of the connection on the device, specified in big endian\r
- *  \param RemoteAddress  Remote protocol IP address of the connected device\r
- *  \param RemotePort     TCP port of the remote device in the connection, specified in big endian\r
- *  \param State          TCP connection state, a value from the TCP_ConnectionStates_t enum\r
+ *  \param[in] Port           TCP port of the connection on the device, specified in big endian\r
+ *  \param[in] RemoteAddress  Remote protocol IP address of the connected device\r
+ *  \param[in] RemotePort     TCP port of the remote device in the connection, specified in big endian\r
+ *  \param[in] State          TCP connection state, a value from the TCP_ConnectionStates_t enum\r
  *\r
  *  \return Boolean true if the connection was updated or created, false otherwise (no more space in the connection state table)\r
  */\r
  *\r
  *  \return Boolean true if the connection was updated or created, false otherwise (no more space in the connection state table)\r
  */\r
@@ -283,9 +281,9 @@ bool TCP_SetConnectionState(uint16_t Port, IP_Address_t RemoteAddress, uint16_t
 \r
 /** Retrieves the current state of a given TCP connection to a host.\r
  *\r
 \r
 /** Retrieves the current state of a given TCP connection to a host.\r
  *\r
- *  \param Port           TCP port on the device in the connection, specified in big endian\r
- *  \param RemoteAddress  Remote protocol IP address of the connected host\r
- *  \param RemotePort     Remote TCP port of the connected host, specified in big endian\r
+ *  \param[in] Port           TCP port on the device in the connection, specified in big endian\r
+ *  \param[in] RemoteAddress  Remote protocol IP address of the connected host\r
+ *  \param[in] RemotePort     Remote TCP port of the connected host, specified in big endian\r
  *\r
  *  \return A value from the TCP_ConnectionStates_t enum\r
  */\r
  *\r
  *  \return A value from the TCP_ConnectionStates_t enum\r
  */\r
@@ -310,9 +308,9 @@ uint8_t TCP_GetConnectionState(uint16_t Port, IP_Address_t RemoteAddress, uint16
 \r
 /** Retrieves the connection info structure of a given connection to a host.\r
  *\r
 \r
 /** Retrieves the connection info structure of a given connection to a host.\r
  *\r
- *  \param Port           TCP port on the device in the connection, specified in big endian\r
- *  \param RemoteAddress  Remote protocol IP address of the connected host\r
- *  \param RemotePort     Remote TCP port of the connected host, specified in big endian\r
+ *  \param[in] Port           TCP port on the device in the connection, specified in big endian\r
+ *  \param[in] RemoteAddress  Remote protocol IP address of the connected host\r
+ *  \param[in] RemotePort     Remote TCP port of the connected host, specified in big endian\r
  *\r
  *  \return ConnectionInfo structure of the connection if found, NULL otherwise\r
  */\r
  *\r
  *  \return ConnectionInfo structure of the connection if found, NULL otherwise\r
  */\r
@@ -337,9 +335,9 @@ TCP_ConnectionInfo_t* TCP_GetConnectionInfo(uint16_t Port, IP_Address_t RemoteAd
 /** Processes a TCP packet inside an Ethernet frame, and writes the appropriate response\r
  *  to the output Ethernet frame if one is created by a application handler.\r
  *\r
 /** Processes a TCP packet inside an Ethernet frame, and writes the appropriate response\r
  *  to the output Ethernet frame if one is created by a application handler.\r
  *\r
- *  \param IPHeaderInStart    Pointer to the start of the incoming packet's IP header\r
- *  \param TCPHeaderInStart   Pointer to the start of the incoming packet's TCP header\r
- *  \param TCPHeaderOutStart  Pointer to the start of the outgoing packet's TCP header\r
+ *  \param[in] IPHeaderInStart     Pointer to the start of the incoming packet's IP header\r
+ *  \param[in] TCPHeaderInStart    Pointer to the start of the incoming packet's TCP header\r
+ *  \param[out] TCPHeaderOutStart  Pointer to the start of the outgoing packet's TCP header\r
  *\r
  *  \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE if no\r
  *           response was generated, NO_PROCESS if the packet processing was deferred until the\r
  *\r
  *  \return The number of bytes written to the out Ethernet frame if any, NO_RESPONSE if no\r
  *           response was generated, NO_PROCESS if the packet processing was deferred until the\r
@@ -367,11 +365,12 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart, void* TCPHeaderInStart, void
                /* Detect RST from host to abort existing connection */\r
                if (TCPHeaderIN->Flags & TCP_FLAG_RST)\r
                {\r
                /* Detect RST from host to abort existing connection */\r
                if (TCPHeaderIN->Flags & TCP_FLAG_RST)\r
                {\r
-                       TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK);                            \r
-                       PacketResponse = true;\r
-                       \r
-                       TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,\r
-                                              TCPHeaderIN->SourcePort, TCP_Connection_Closed);                 \r
+                       if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,\r
+                                                  TCPHeaderIN->SourcePort, TCP_Connection_Closed))\r
+                       {\r
+                               TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK);                            \r
+                               PacketResponse = true;                  \r
+                       }\r
                }\r
                else\r
                {\r
                }\r
                else\r
                {\r
@@ -381,19 +380,24 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart, void* TCPHeaderInStart, void
                                case TCP_Connection_Listen:\r
                                        if (TCPHeaderIN->Flags == TCP_FLAG_SYN)\r
                                        {\r
                                case TCP_Connection_Listen:\r
                                        if (TCPHeaderIN->Flags == TCP_FLAG_SYN)\r
                                        {\r
-                                               /* SYN connection when closed starts a connection with a peer */\r
+                                               /* SYN connection starts a connection with a peer */\r
+                                               if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress,\r
+                                                                          TCPHeaderIN->SourcePort, TCP_Connection_SYNReceived))\r
+                                               {\r
+                                                       TCPHeaderOUT->Flags = (TCP_FLAG_SYN | TCP_FLAG_ACK);                                            \r
 \r
 \r
-                                               TCPHeaderOUT->Flags = (TCP_FLAG_SYN | TCP_FLAG_ACK);                            \r
-                                               PacketResponse      = true;\r
-                                                                       \r
-                                               TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort,\r
-                                                                                          TCP_Connection_SYNReceived);\r
-                                                                                          \r
-                                               ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort);\r
+                                                       ConnectionInfo = TCP_GetConnectionInfo(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort);\r
 \r
 \r
-                                               ConnectionInfo->SequenceNumberIn  = (SwapEndian_32(TCPHeaderIN->SequenceNumber) + 1);\r
-                                               ConnectionInfo->SequenceNumberOut = 0;\r
-                                               ConnectionInfo->Buffer.InUse      = false;\r
+                                                       ConnectionInfo->SequenceNumberIn  = (SwapEndian_32(TCPHeaderIN->SequenceNumber) + 1);\r
+                                                       ConnectionInfo->SequenceNumberOut = 0;\r
+                                                       ConnectionInfo->Buffer.InUse      = false;\r
+                                               }\r
+                                               else\r
+                                               {\r
+                                                       TCPHeaderOUT->Flags = TCP_FLAG_RST;\r
+                                               }\r
+                                                                                          \r
+                                               PacketResponse      = true;\r
                                        }\r
                                        \r
                                        break;\r
                                        }\r
                                        \r
                                        break;\r
@@ -579,10 +583,10 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart, void* TCPHeaderInStart, void
 /** Calculates the appropriate TCP checksum, consisting of the addition of the one's compliment of each word,\r
  *  complimented.\r
  *\r
 /** Calculates the appropriate TCP checksum, consisting of the addition of the one's compliment of each word,\r
  *  complimented.\r
  *\r
- *  \param TCPHeaderOutStart  Pointer to the start of the packet's outgoing TCP header\r
- *  \param SourceAddress      Source protocol IP address of the outgoing IP header\r
- *  \param DestinationAddress Destination protocol IP address of the outgoing IP header\r
- *  \param TCPOutSize         Size in bytes of the TCP data header and payload\r
+ *  \param[in] TCPHeaderOutStart   Pointer to the start of the packet's outgoing TCP header\r
+ *  \param[in] SourceAddress       Source protocol IP address of the outgoing IP header\r
+ *  \param[in] DestinationAddress  Destination protocol IP address of the outgoing IP header\r
+ *  \param[in] TCPOutSize          Size in bytes of the TCP data header and payload\r
  *\r
  *  \return A 16-bit TCP checksum value\r
  */\r
  *\r
  *  \return A 16-bit TCP checksum value\r
  */\r