3

I am trying to debug a scenario, and for that I want the http server to close the connection via RST. Right now it is doing a graceful close with fin/ack.

Is there any way I can manually send a RST packet to close the connection as part of the current stream? may be a simple custom server?

thanks in advance for your help.

ahamed101
  • 110
  • 2
  • 9

2 Answers2

6

Assuming it is your own code, call setsockopt() with option=SO_LINGER and the linger structure set to l_onoff=1 and l_linger=0, and then close the socket. Any outbound data that is still buffered will be lost, which includes data already sent but not acknowledged.

Use this only for testing. It is insecure and unkind.

If it isn't your code in the server, write a client that does a GET of a large resource and closes the connection without reading any of the response.

Source: W.R. Stevens et al., Unix Network Programming, vol 1, 3rd edition, p.202.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Setting `l_onoff = 0` with any value for `l_linger` would do as well, wouldn't it? – Asblarf Mar 20 '14 at 05:27
  • @Asblarf No it wouldn't. There are three behaviours. This is the only one that causes an RST. What you suggest is the default behaviour, an asynchronous orderly close. – user207421 Mar 20 '14 at 06:02
  • OK, thanks (just had a look at `net/ipv4/tcp.c` in the Linux Kernel and it does what you say. It all checks.) – Asblarf Mar 20 '14 at 17:01
  • Thanks a lot @EJP, that was what I was looking for. Appreciate your quick response. cheers! – ahamed101 Mar 20 '14 at 17:43
1

For reference, code snippet for the setsockopt() with linger. Thanks to @EJB for the help. With this option set, the server closes the connection with RST.

...
     struct linger so_linger;

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
        error("ERROR opening socket");

     so_linger.l_onoff = 1;
     so_linger.l_linger = 0;
     setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(so_linger));
...
ahamed101
  • 110
  • 2
  • 9