📄 tcp.c
字号:
// TCP A TCP B
// 1. CLOSED LISTEN
//
// 2. SYN-SENT --> <SEQ=100><CTL=SYN> --> SYN-RECEIVED
// 3. ESTABLISHED <-- <SEQ=300><ACK=101><CTL=SYN,ACK> <-- SYN-RECEIVED
// 4. ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK> --> ESTABLISHED
//
// 5. ESTABLISHED --> <SEQ=101><ACK=301><CTL=ACK><DATA> --> ESTABLISHED
// 6. etc
//
//
//
// 7. FIN-WAIT-1 --> <SEQ=100><ACK=300><CTL=FIN,ACK> --> CLOSE-WAIT
// 8. FIN-WAIT-2 <-- <SEQ=300><ACK=101><CTL=ACK> <-- CLOSE-WAIT
// 9. TIME-WAIT <-- <SEQ=300><ACK=101><CTL=FIN,ACK> <-- LAST-ACK
// 10. TIME-WAIT --> <SEQ=101><ACK=301><CTL=ACK> --> CLOSED
// 11. (2 MSL)
// CLOSED
/*
* Copyright (C) 2003-2004 by Clive Moss. Email c.a.m@blueyonder.co.uk All rights reserved.
*
* Help & Contributions from D.J.Armstrong Email heli.pad@ntlworld.com
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY CLIVE MOSS 'AS IS' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.
* IN NO EVENT SHALL CLIVE MOSS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
//#ifdef CPU_eZ8
// #pragma stkck // enable stack checking
//#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
#include "ppp.h"
#include "ip.h"
#include "tcp.h"
#ifdef Debug
flash char tcp_str1[] = " TCPHdr: SrcPrt:";
flash char tcp_str2[] = " DestPrt:";
flash char tcp_str3[] = " SeqNum:";
flash char tcp_str4[] = " AckNum:";
flash char tcp_str5[] = " DataOfs:";
flash char tcp_str6[] = " CBits:";
flash char tcp_str7[] = "-URG";
flash char tcp_str8[] = "-ACK";
flash char tcp_str9[] = "-PSH";
flash char tcp_str10[] = "-RST";
flash char tcp_str11[] = "-SYN";
flash char tcp_str12[] = "-FIN";
flash char tcp_str13[] = " WinSize:";
flash char tcp_str14[] = " Chksum:";
flash char tcp_str15[] = " UrgPtr:";
flash char tcp_str16[] = " option:";
flash char tcp_str17[] = " DataBytes:";
flash char tcp_str18[] = " Conn RESET\n";
flash char tcp_str19[] = " Conn ACCEPTED\n";
flash char tcp_str20[] = " Conn ESTABLISHED\n";
flash char tcp_str21[] = " - BLOCKED\n";
flash char tcp_str22[] = "TCP Skt State: ";
flash char tcp_str23[] = "CLOSED\n";
flash char tcp_str24[] = "LISTEN\n";
flash char tcp_str25[] = "SYN_SENT\n";
flash char tcp_str26[] = "SYN_RECEIVED\n";
flash char tcp_str27[] = "ESTABLISHED\n";
flash char tcp_str28[] = "FIN_WAIT_1\n";
flash char tcp_str29[] = "FIN_WAIT_2\n";
flash char tcp_str30[] = "CLOSE_WAIT\n";
flash char tcp_str31[] = "CLOSING\n";
flash char tcp_str32[] = "LAST_ACK\n";
flash char tcp_str33[] = "TIME_WAIT\n";
flash char tcp_str34[] = "UNKNOWN\n";
flash char tcp_Str35[] = "*** failed to get mem for socket\n";
flash char tcp_str40[] = "*** TCP chksum err\n";
flash char TCP_Str50[] = " Connect Time: ";
flash char TCP_Str51[] = " Last Rx Data: ";
flash char TCP_Str52[] = "Their Max Seg Size: ";
flash char TCP_Str53[] = " Their Seq Num: ";
flash char TCP_Str54[] = " Our Seq Num: ";
flash char TCP_Str55[] = " Their Win Size: ";
flash char TCP_Str56[] = " Last Bytes Sent: ";
flash char TCP_Str57[] = " Their IP: ";
flash char TCP_Str58[] = " Remote Port: ";
flash char TCP_Str59[] = " Local Port: ";
flash char TCP_Str60[] = " Round Trip Time: ";
flash char TCP_Str61[] = " Retries: ";
flash char TCP_Str62[] = " Retry Timer: ";
flash char TCP_Str63[] = " TxBufferRd: ";
flash char TCP_Str64[] = " TxBufferWr: ";
flash char TCP_Str65[] = " RxBufferRd: ";
flash char TCP_Str66[] = " RxBufferWr: ";
#endif
#ifdef CPU_eZ8
near u16 TCP_TmpPort = 0; // this is usedfor deciding what source port to use when making out going tcp connections
#endif
#ifdef CPU_ATmega128
u16 TCP_TmpPort = 0; // "
#endif
#ifdef StaticTCPSocket
T_TCP_Socket TCP_Static_Socket; // static socket
T_TCP_Socket *TCP_Socket = (T_TCP_Socket*)&TCP_Static_Socket; // set the pointer to point to it
#else
T_TCP_Socket *TCP_Socket = NULL; // it'll be dynamically allocated
#endif
T_TCP_Header *TCP_Header = NULL; // points to the tcp header in a tx or rx packet
//*****************************************************************************************************
#ifdef Debug
void TCP_DisplayHeader(int HeaderIdx, int TotalBytes)
{ // display the tcp header details
u8 b, type, len;
int i, j;
u16 w;
u32 dw;
/*
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding | < optional
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
// display the header
if (!SendDebugRStr(tcp_str1)) return;
sprintf((char*)ScratchPad, "%u", ntohs(TCP_Header->SourcePort));
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(tcp_str2)) return;
sprintf((char*)ScratchPad, "%u", ntohs(TCP_Header->DestPort));
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(tcp_str3)) return;
dw = ntohl(TCP_Header->SequenceNum);
sprintf((char*)ScratchPad, "%lu", dw);
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(tcp_str4)) return;
dw = ntohl(TCP_Header->AckNum);
sprintf((char*)ScratchPad, "%lu", dw);
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(tcp_str5)) return;
sprintf((char*)ScratchPad, "%u", TCP_Header->DataOffset >> 4);
if (!SendDebugStr((char*)ScratchPad)) return;
b = TCP_Header->ControlBits;
if (!SendDebugRStr(tcp_str6)) return;
if (b & TCP_SYN) SendDebugRStr(tcp_str11);
if (b & TCP_FIN) SendDebugRStr(tcp_str12);
if (b & TCP_RST) SendDebugRStr(tcp_str10);
if (b & TCP_URG) SendDebugRStr(tcp_str7);
if (b & TCP_ACK) SendDebugRStr(tcp_str8);
if (b & TCP_PSH) SendDebugRStr(tcp_str9);
if (!SendDebugRStr(tcp_str13)) return;
sprintf((char*)ScratchPad, "%u", ntohs(TCP_Header->WindowSize));
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(tcp_str14)) return;
sprintf((char*)ScratchPad, "%04x", ntohs(TCP_Header->Checksum));
if (!SendDebugStr((char*)ScratchPad)) return;
if (!SendDebugRStr(tcp_str15)) return;
sprintf((char*)ScratchPad, "%u", ntohs(TCP_Header->UrgentPointer));
if (!SendDebugStr((char*)ScratchPad)) return;
// display the options - if any
i = HeaderIdx; //
j = (int)(TCP_Header->DataOffset >> 4); // header length in 32-bit dwords
j <<= 2; // ... and now in bytes
j += i; // point to data (immediately follows the TCP header)
i += sizeof(T_TCP_Header); //
while (i < j) //
{ // go thru each IP header option
if (!SendDebugStr("\n")) return; //
//
type = MainBuffer[i++]; // option type
len = 0; //
if (type >= 2) len = MainBuffer[i++]; //
//
if (!SendDebugRStr(tcp_str16)) return; //
//
switch (type) //
{ //
case 0 : //
case 1 : sprintf((char*)ScratchPad, "%u", type); //
break; //
case 2 : w = ntohs(*(u16*)(MainBuffer + i)); // max segment size
sprintf((char*)ScratchPad, "%u len:%u %u", type, len, w); //
if (!SendDebugStr((char*)ScratchPad)) return; //
break; //
default : sprintf((char*)ScratchPad, "%u len:%u", type, len); //
if (!SendDebugStr((char*)ScratchPad)) return; //
break; //
} //
if (type == 0) break; // end of options
if (type == 1) continue; // no length byte
//
if (len < 2) len = 2; //
i += (len - 2); // point to next option
} //
i = j;
if (!SendDebugStr("\n")) return; //
// done
if (!SendDebugRStr(tcp_str17)) return;
sprintf((char*)ScratchPad, "%u\n", TotalBytes - i);
if (!SendDebugStr((char*)ScratchPad)) return;
}
void TCP_DisplaySocketStage(T_TCP_Socket *Socket)
{ // display the tcp socket stage
if (Socket == NULL) return;
if (!SendDebugRStr(tcp_str22)) return;
switch (Socket->Stage)
{
case TCP_CLOSED : SendDebugRStr(tcp_str23);
break;
case TCP_LISTEN : SendDebugRStr(tcp_str24);
break;
case TCP_SYN_SENT : SendDebugRStr(tcp_str25);
break;
case TCP_SYN_RECEIVED : SendDebugRStr(tcp_str26);
break;
case TCP_ESTABLISHED : SendDebugRStr(tcp_str27);
break;
case TCP_FIN_WAIT_1 : SendDebugRStr(tcp_str28);
break;
case TCP_FIN_WAIT_2 : SendDebugRStr(tcp_str29);
break;
case TCP_CLOSE_WAIT : SendDebugRStr(tcp_str30);
break;
case TCP_CLOSING : SendDebugRStr(tcp_str31);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -