Search code examples
csocketstcp

STARTED getting RST packet from client socket after I replied SYN packet with SYN + ACK packet


I am creating simple TCP simulation program that can act as a simple TCP server over IP, the problem is in my code I coded when I receive SYN packet, --> I do all the response packet formulation in my program server and creation of response packet and reply with SYN+ACK packet.

But right after I reply with SYN+ACK packet I get RST packet from the client socket. I have no clue whats wrong with my TCP or some headers that making my client socket to just go about reply my SYN+ACK packet with RST packet.

I read on the internet about RST packet that It says RST received on host for two reasons, but can be more reasons

There are a few circumstances in which a TCP packet might not be expected; the two most common are:

    The packet is an initial SYN packet trying to establish a connection to a server port on which no process is listening.
    The packet arrives on a TCP connection that was previously established, but the local application already closed its socket or exited and the OS closed the socket.

So I like to know what is the reason in my case given.

When I received SYN packet on program IP socket

0000   45 00 00 3c 81 37 40 00 40 06 24 11 c0 a8 0a 14
0010   c0 a8 0a 0f ba 50 00 50 15 1a 68 41 00 00 00 00
0020   a0 02 fa f0 a3 ce 00 00 02 04 05 b4 04 02 08 0a
0030   de d7 fc f8 00 00 00 00 01 03 03 07

I reply with SYN+ACK packet. my TCP packet looks like this

0000   45 00 00 28 00 64 40 00 40 06 a4 f8 c0 a8 0a 0f
0010   c0 a8 0a 14 00 50 ba 50 00 01 00 00 15 1a 68 41
0020   50 12 fa f0 e7 70 00 00

So fields in TCP are like above in 20 (IP header)+20 (TCP header) bytes of packet (SYN+ACK) for SYN+ACK replying from my code to client socket. but instead of start of data sending from client, my client start sending RST packets and in my code I get RST packet right after I send SYN + ACK packet from my code to client socket.

But what is the thing in above headers values that I get RST packet (reset packet) instead of getting some data from client or at least my client sockets [connect()] function call gets succeed? How to make this RST packet from my client sending go away so my client finally start sending data and my server code start getting data? What am I doing wrong?


Solution

  • The general reason for sending RST, is that the receiver cannot find a socket, that incomming packet should be attributed to.

    Common cases include:

    • mismatched ports: there is no open socket for the port pair
    • mistmatched sequence numbers: ACK number is incorrect, or ACK number does not fit within sender's window (here sender is the side that has received an ACK, because ACK is a response to sender sending something)

    According to TCP specification, packets with SYN (and also FIN) flags set, are acknowledged with ACK numbers increased by one, i.e., as if these flags occupied one byte of data.


    Considering both received SYN and sent SYNACK, one can see, that sequence number in SYN is 15 1a 68 41 and ACK number in SYNACK is also 15 1a 68 41, which is not SYN + 1 as it should be.

    P.S.: structure of TCP header can be found in e.g., wikipedia. An explanation how to match packet bytes to header fields can be found in this answer. Also, an illustration of sender window can be found here.