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.
> 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)