How can i simulate a connect from a non-blocking client to experience a EINPROGRESS error ? What can i do from the server to return such error to the client (i.e tuning some tcp timers, raw socket server etc..)
2 Answers
When connecting to localhost TCP always connects immediately, hence connect()
doesn't return EINPROGRESS
.
You can simulate that in client by "pretending" that connect()
returned EINPROGRESS
and waiting till the client socket becomes ready for write (as if connected after EINPROGRESS
). In fact, some libraries do just that for non-blocking connect()
to avoid two different code paths for immediate and for slow connect.
I had to do something similar recently and I came across this thread. I used iptable rules to solve my problem. EINPROGRESS is not something that is returned by the server. If a connect can't complete immediately, the return code from connect is -1 and the errorno is set to EINPROGRESS. Once connect sends out the SYN packet, your connect will complete (given that you are connecting to a valid IP address, and everything else is okay with the network).
If you can change iptables on the client side, then the easiest way to simulate EINPROGRESS is to create an iptable rule to drop outgoing SYN packets to the server's IP address and port.
iptables -A OUTPUT -p tcp --tcp-flags SYN,ACK,FIN,RST SYN -d serverip --dport serverport -j DROP
Keep in mind that this only works if you don't want the connect to ever complete.