How to set a connect() timeout (?)

Ronald F. Guilmette rfg at tristatelogic.com
Thu Aug 13 10:11:12 UTC 2020


This is driving me bonkers and I feel like I'm having deja vu all over again.
(In other words I think I may have asked about this before in the very distant
past.)

Setting a programmed time limit for the connect() system call seems to be a
common thing that many people, not just myself, often want to do.  It is not
at all clear if there is or isn't a simple way to do that, e.g. one that
doesn't involve calling select() or poll() or something like that.

On FreeBSD, there is a system-wide sysctl variable that sets the system-wide default
connect timeout.  It's called net.inet.tcp.keepinit.  But I really do want to be
able to programatically set the connect timeout for individual (outbound) sockets.

setsockopt() provides the SO_SNDTIMEO and SO_RCVTIMEO options which allow setting
of timeouts on individual sockets for writing and reading respectively, but it is
not immediately apparent, e.g. from the relevant man page, that either of these
setsockopt() options will have any effect on the connect() timeout period.

In online sources there is some suggestion that one or the other of these two
setsockopt() options can be used, at least on Linux, to programatically control
the per-socket connect timeout period, but I have not tried that on my own Linux
system so I may be reading too much into what I have been reading.

I am forced to wonder why, on FreeBSD there is no such thing as an SO_CONTIMEO
option that can be used with setsockopt() to easily control the connect() timeout
on a per-socket basis.

Can anyone explain to me why there isn't such a thing?  Seems like rather a glaring
oversight to me, and a no-brainer (that such a thing really should exist).


More information about the freebsd-questions mailing list