X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/f1076ac4d6e56bff7fb6d2126746af1108211370..31d8ebebc0796873f7c70db80a04acdcbb307ed8:/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.c diff --git a/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.c b/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.c index b4fc12c93..2537286b7 100644 --- a/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.c +++ b/Demos/Device/LowLevel/RNDISEthernet/Lib/TCP.c @@ -58,19 +58,18 @@ TCP_ConnectionState_t ConnectionStateTable[MAX_TCP_CONNECTIONS]; */ void TCP_Task(void) { - /* Task to hand off TCP packets to and from the listening applications. */ - /* Run each application in sequence, to process incoming and generate outgoing packets */ for (uint8_t CSTableEntry = 0; CSTableEntry < MAX_TCP_CONNECTIONS; CSTableEntry++) { /* Find the corresponding port entry in the port table */ - for (uint8_t PTableEntry = 0; PTableEntry < MAX_TCP_CONNECTIONS; PTableEntry++) + 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) && (PortStateTable[PTableEntry].State == TCP_Port_Open)) { - PortStateTable[PTableEntry].ApplicationHandler(&ConnectionStateTable[CSTableEntry], &ConnectionStateTable[CSTableEntry].Info.Buffer); + PortStateTable[PTableEntry].ApplicationHandler(&ConnectionStateTable[CSTableEntry], + &ConnectionStateTable[CSTableEntry].Info.Buffer); } } } @@ -89,7 +88,7 @@ void TCP_Task(void) Ethernet_Frame_Header_t* FrameOUTHeader = (Ethernet_Frame_Header_t*)&FrameOUT.FrameData; IP_Header_t* IPHeaderOUT = (IP_Header_t*)&FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t)]; TCP_Header_t* TCPHeaderOUT = (TCP_Header_t*)&FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t) + - sizeof(IP_Header_t)]; + sizeof(IP_Header_t)]; void* TCPDataOUT = &FrameOUT.FrameData[sizeof(Ethernet_Frame_Header_t) + sizeof(IP_Header_t) + sizeof(TCP_Header_t)]; @@ -367,11 +366,12 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart, void* TCPHeaderInStart, void /* Detect RST from host to abort existing connection */ if (TCPHeaderIN->Flags & TCP_FLAG_RST) { - TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK); - PacketResponse = true; - - TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, - TCPHeaderIN->SourcePort, TCP_Connection_Closed); + if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, + TCPHeaderIN->SourcePort, TCP_Connection_Closed)) + { + TCPHeaderOUT->Flags = (TCP_FLAG_RST | TCP_FLAG_ACK); + PacketResponse = true; + } } else { @@ -381,19 +381,24 @@ int16_t TCP_ProcessTCPPacket(void* IPHeaderInStart, void* TCPHeaderInStart, void case TCP_Connection_Listen: if (TCPHeaderIN->Flags == TCP_FLAG_SYN) { - /* SYN connection when closed starts a connection with a peer */ + /* SYN connection starts a connection with a peer */ + if (TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, + TCPHeaderIN->SourcePort, TCP_Connection_SYNReceived)) + { + TCPHeaderOUT->Flags = (TCP_FLAG_SYN | TCP_FLAG_ACK); - TCPHeaderOUT->Flags = (TCP_FLAG_SYN | TCP_FLAG_ACK); - PacketResponse = true; - - TCP_SetConnectionState(TCPHeaderIN->DestinationPort, IPHeaderIN->SourceAddress, TCPHeaderIN->SourcePort, - TCP_Connection_SYNReceived); - - 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->Buffer.InUse = false; + ConnectionInfo->SequenceNumberIn = (SwapEndian_32(TCPHeaderIN->SequenceNumber) + 1); + ConnectionInfo->SequenceNumberOut = 0; + ConnectionInfo->Buffer.InUse = false; + } + else + { + TCPHeaderOUT->Flags = TCP_FLAG_RST; + } + + PacketResponse = true; } break; @@ -579,10 +584,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, * complimented. * - * \param[in] TCPHeaderOutStart Pointer to the start of the packet's outgoing TCP header - * \param[in] SourceAddress Source protocol IP address of the outgoing IP header - * \param[in] DestinationAddress Destination protocol IP address of the outgoing IP header - * \param[in] TCPOutSize Size in bytes of the TCP data header and payload + * \param[in] TCPHeaderOutStart Pointer to the start of the packet's outgoing TCP header + * \param[in] SourceAddress Source protocol IP address of the outgoing IP header + * \param[in] DestinationAddress Destination protocol IP address of the outgoing IP header + * \param[in] TCPOutSize Size in bytes of the TCP data header and payload * * \return A 16-bit TCP checksum value */