How do I set the Time-To-Live
socket option in perl?
What are the parameters to setsockopt()
, or is there a better way to do it?
How do I set the Time-To-Live
socket option in perl?
What are the parameters to setsockopt()
, or is there a better way to do it?
The following works for me, on Linux:
IPv4
use Socket;
...
socket (SOCKET, ...) or die "Can't create socket: $!";
my $ip = getprotobyname ('ip'); # just in case it ever changes ;-)
my $opname = IP_TTL;
my $opval = int $ttl; # if $ttl is a string, convert to int
setsockopt (SOCKET, $ip, $opname, $opval) or warn "setsockopt failed: $!";
IPv6
use Socket("SOCK_STREAM", "IPV6_UNICAST_HOPS");
...
my $ipv6 = getprotobyname('ipv6');
my $opname = IPV6_UNICAST_HOPS;
my $opval = int $ttl;
setsockopt (SOCKET,$ipv6,$opname,$opval) or warn "setsockopt failed: $!";