muto has asked for the wisdom of the Perl Monks concerning the following question:

I've been looking around, and I can't find any way to modify the Time To Live value of a TCP socket. The only references I've found online refer to IO::Socket::Multicast, which aren't helpful. It also doesn't help that searching for TTL often brings up words such as 'little'. Is this possible to do, or am I can to have to use another module - if so, which would you recommend. Thanks, ~muto Update: Ok, I've been looking at using using Socket. However, when I run my script, I get the following error: Your vendor has not defined Socket macro IP_TTL, used at K:\perl\firewalk.pl line 17 Anyone know what's wrong? I'm on Activeperl 5.10, Windows XP SP 2.
#!/usr/bin/perl -w use Socket; $remote = $ARGV[0]; $port = 80; if($port =~ /\D/) { $port = getservbyname($port, 'tcp') } die "no port specified" unless $port; $iaddr = inet_aton($remote) || die "$remote"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "cannot open socket +: $!"; setsockopt (SOCK,IPPROTO_IP,IP_TTL,1); connect(SOCK, $paddr) || die "cannot connect to $remote: $!"; close(SOCK);

Replies are listed 'Best First'.
Re: IO::Socket TTL?
by monarch (Priest) on May 30, 2008 at 12:13 UTC

    I suspect that the TTL is set by the operating system, and it is unlikely that any will allow you to easily modify, on a per-socket basis, the outgoing time-to-live on TCP packets.

    It might also be helpful to perhaps explain what you're trying to achieve, as the TTL is used for two reasons I can think of: 1) prevent packets living indefinitely in the case of a routing loop, and 2) traceroute which deliberately sets low TTLs in order to trigger ICMP responses.

    If it is a traceroute-like function you're trying to implement then perhaps Net::RawIP or Net::Packet::IPv4 are worth looking at.

Re: IO::Socket TTL?
by Anonymous Monk on May 31, 2008 at 07:52 UTC
      Tried it, no effect. I've found that using TTL rather than IP_TTL stops the vendor error appearing, but has no effect. Searching around found 15 as a possible value for TTL, but that did nothing either.
      setsockopt (SOCK,0,15,4);
      This gives no errors, but Wireshark shows that the TTL is still at 64 :/