Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a reset in TCP/IP Socket connection

Tags:

I am using python’s socket.py to create a connection to an ftp-server. Now I want to reset the connection (send a RST Flag) and listen to the response of the ftp-server. (FYI using socket.send('','R') does not work as the OS sends FIN flag instead of RST.)

like image 807
Anvay Avatar asked Jun 22 '11 12:06

Anvay


People also ask

Why does client send TCP reset?

When one TCP peer is sending out TCP packets for which there's no response received from the other end, the TCP peer would end up retransmitting the data and when there's no response received, it would end the session by sending an ACK RESET (thisACK RESET means that the application acknowledges whatever data is ...

What happens when TCP reset?

A TCP reset (RST) closes a connection between a sender device and recipient device, and informs the sender to create another connection and resend the traffic.

Who sends TCP reset?

The server will send a reset to the client. SYN matches the existing TCP endpoint: The client sends SYN to an existing TCP endpoint, which means the same 5-tuple. The server will send a reset to the client.


2 Answers

Turn the SO_LINGER socket option on and set the linger time to 0 seconds. This will cause TCP to abort the connection when it is closed, flush the data and send a RST. See section 7.5 and example 15.21 in UNP.

In python:

def client(host, port):     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)     s.connect((host, port))     l_onoff = 1                                                                                                                                                                l_linger = 0                                                                                                                                                               s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,                                                                                                                                       struct.pack('ii', l_onoff, l_linger))     # send data here     s.close() 
like image 101
crowbent Avatar answered Nov 06 '22 01:11

crowbent


If you want to implement your own behavior over connections I think you should try using Scapy. It is a really useful library/tool. It lets you play with IP/TCP/UDP/ICMP packages.

like image 33
Santiago Alessandri Avatar answered Nov 06 '22 03:11

Santiago Alessandri