Discussion:
wininet and ttl
(too old to reply)
Ted Carter
2004-04-22 01:25:44 UTC
Permalink
Is there any way to specify the time-to-live (TTL) on sockets wininet uses?

Regards, Ted.
Stephen Sulzer
2004-04-22 07:54:35 UTC
Permalink
It may be possible to set the TTL on the socket that WinInet uses for an
HTTP request. However, WinInet really does not like (or support) the
application modifying the socket, so there are no guarantees this will work
(I have not tried this myself). There is no option in the WinInet API to let
the application set the socket's TTL, but it is possible to access the
socket "behind WinInet's back."

Presumably you want to call:
setsockopt(socket, IPPROTO_IP,
IP_TTL,
(const char *)&dwNewTTLValue,
sizeof(DWORD));
on the WinInet socket, correct?

In order to get a hold of the WinInet socket at the right time, the
application needs to set a callback function (via InternetSetStatusCallback)
on the request handle. This should be done after HttpOpenRequest (or
InternetOpenUrl) and before HttpSendRequest[Ex]. WinInet does not create the
socket during HttpOpenRequest, but instead creates the socket "just-in-time"
during HttpSendRequest, so registering a callback function is required to
monitor WinInet during HttpSendRequest. The callback function needs to
handle the INTERNET_STATUS_CONNECTING_TO_SERVER notification. At the time of
this notification, WinInet has created the socket but has not called bind()
on it.

To get the socket handle during the callback, query the HTTP request handle
for the INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO option. This option supplies
the socket in an INTERNET_DIAGNOSTIC_SOCKET_INFO structure; for more
information see the following on-line documentation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internet_diagnostic_socket_info.asp

(Note that using this option requires at least Internet Explorer 5 or
later.)

With the socket in the DIAGNOSTIC_SOCKET_INFO structure, the app can call
setsockopt to set the TTL value. When control returns back to WinInet from
the CONNECTING_TO_SERVER callback, WinInet will then call bind() and
connect() on the socket and then send out the HTTP request.

Now, I don't know off-hand if it is Ok to set the TTL before WinInet
bind()'s the socket, or it must bet set after bind(). Hopefully this is not
an issue. The problem is that you cannot access the socket between bind()
and connect(). The next notification you can receive from WinInet is
INTERNET_STATUS_CONNECTED_TO_SERVER, which WinInet sends to your callback
function after WinInet has connect()'ed the socket. (Perhaps it is Ok to set
the TTL after connect()? I am not sure.)

[Also, note that by default WinInet uses persistent ("keep-alive") socket
connections, and will try to reuse the socket for a subsequent HTTP request
to the same server. If you want to set a different TTL value on a subsequent
request, you will probably need to disable keep-alive. To tell WinInet to
discard the socket after the HTTP request completes, set a "Connection:
Close" request header. ]

Hope that helps. Out of curiosity, why do you want to change the TTL value
on the socket?


Stephen
Post by Ted Carter
Is there any way to specify the time-to-live (TTL) on sockets wininet uses?
Regards, Ted.
Loading...