Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Setting Time-To-Live (TTL) for outbound connections using Winsock2

4 views
Skip to first unread message

Paperboy

unread,
Oct 26, 2009, 6:59:52 AM10/26/09
to

Hi,

Anyone done this successfully using default WinSock2 options?

I set the TTL using (code short version):

hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
getaddrinfo(serveraddress, serverport, &hints, &result);
hSocket = socket(result->ai_family, SOCK_STREAM, result->ai_protocol);
int ttl = 20;
setsockopt(hSocket, IPPROTO_IP, IP_TTL, (char*) &ttl, sizeof(ttl))

// No SOCKET_ERROR returned and I can check that the value was
successfully set using:

getsockopt(hSocket, IPPROTO_IP, IP_TTL, (char*) &ttl, &ttllen)

// Which returns ttl value of 20.
// However when I start outbound connection

connect(hSocket, result->ai_addr, sizeof(*result->ai_addr));
send(hSocket, SENDDATA, sizeof(SENDDATA)-1, 0);

I can see with network monitoring software that TTL for the packet is
128 (Which is the default for Windows XP set in registry DefaultTTL).

Anyone knows how this should work? Little help appreciated.

Remy Lebeau

unread,
Oct 26, 2009, 4:16:21 PM10/26/09
to

"Paperboy" <tero....@gmail.com> wrote in message
news:153767c3-6e5f-4eab...@x15g2000vbr.googlegroups.com...

> hints.ai_family = AF_UNSPEC;

You are not checking the actual family that you are passing to socket(). If
an IPv6 socket gets created, then your call to setsockopt() needs to be this
instead:

setsockopt(hSocket, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));

> connect(hSocket, result->ai_addr, sizeof(*result->ai_addr));

Likewise, you need to use the result->ai_addrlen value instead of sizeof(),
since IPv4 and IPv6 addresses are different sizes, ie:

connect(hSocket, result->ai_addr, result->ai_addrlen);

--
Remy Lebeau (TeamB)


0 new messages