I am looking to simulate a TCP server, where I would want to reject connection with different error codes in ICMP message.
Currently, the issue is even before it reaches handle_accept() in sockets SYN,ACK would have already reached to the server, and I can reject the connection with ICMP errors!
Did anybody ever tried it? Is there any other way to do it?
Thanks in Advance!
There is no way to do this on the level of the TCP socket interface available in Python since the OS kernel does the connection setup already before the applications returns from accept. You would need to handle this outside of the application with firewall rules or use raw sockets or a user space network stack where you are not restricted to how connections are handled in the kernel and what the socket interface offers.
Reject TCP SYN with ICMP error messages in python