Thursday, 22 August 2013

Reading SIO_KEEPALIVE_VALS fields on a Windows socket (for keepalive idle and interval times)

Reading SIO_KEEPALIVE_VALS fields on a Windows socket (for keepalive idle
and interval times)

Given a Windows socket, I want to determine which values it is using for
the TCP keepalive idle time and the TCP keepalive interval time (roughly
equivalent to the TCP_KEEPIDLE and TCP_KEEPINTVL settings on Berkeley
sockets).
I see that you can set these values using a WSAIoctl call (see
http://msdn.microsoft.com/en-us/library/windows/desktop/dd877220%28v=vs.85%29.aspx
). However, there does not appear to be any API for reading their current
values. I tried calling WSAIoctl with a populated output parameter but
NULL input parameter, like this:
DWORD bytes_returned;
struct tcp_keepalive keepalive_opts;
int rv = WSAIoctl(socket, SIO_KEEPALIVE_VALS, NULL, 0, &keepalive_opts,
sizeof(keepalive_opts), &bytes_returned, NULL, NULL);
But this returns me a WSAEFAULT ("The system detected an invalid pointer
address in attempting to use a pointer argument in a call.").
I could call WSAIoctl with both an input and an output parameter, but I
don't want to set the values, I just want to read them. And as far as I
can tell, providing any non-NULL input parameter would cause the
parameters to be set to whatever values happen to be in that memory space
(defined by the struct tcp_keepalive; again see
http://msdn.microsoft.com/en-us/library/windows/desktop/dd877220%28v=vs.85%29.aspx
).
The above also highlights another problem with not knowing what the
current values are: I can't set just one of the keepalive idle time or the
keepalive interval time - I must blow away both (unknown) values at the
same time since they're both members of the struct I'm required to
provide.
I know that I could assume things about what values are set based on
Windows documentation, but I'd rather not assume. I see that
http://technet.microsoft.com/en-us/library/bb726981.aspx#EDAA defines
KeepAliveInterval and KeepAliveTime default values. However, the
Parameters folder in my Windows 7 registry does not contain either of
those keys, so I really have to rely on the documentation being 100%
correct here (to know the default values a socket will receive), which is
much worse than programmatically retrieving them (even retrieving them
from the registry might be ok, but the above experience shows I can't).
Is there any way to get the current TCP keepalive idle time and the TCP
keepalive interval time values for a Windows socket?

No comments:

Post a Comment