Subject: | IPv6 Support - with patch! |
The current version of Net::Telnet (3.03) does not support IPv6 and uses
IPv4 only Socket routines.
The attached patch adds IPv6 support by using IO::Socket::IP in lieu of
IO::Socket::INET and getaddrinfo() in lieu of inet_aton() and
sockaddr_in().
Subject: | Telnet.patch |
--- lib\Net\Telnet.pm Tue Jul 16 20:30:42 2002
+++ lib\Net\Telnet.pm Tue Oct 02 08:55:26 2012
@@ -29,14 +29,14 @@
## Module import.
use Exporter ();
-use Socket qw(AF_INET SOCK_STREAM inet_aton sockaddr_in);
+use Socket qw(AF_INET AF_INET6 SOCK_STREAM :addrinfo);
use Symbol qw(qualify);
## Base classes.
use vars qw(@ISA);
@ISA = qw(Exporter);
if (&_io_socket_include) { # successfully required module IO::Socket
- push @ISA, "IO::Socket::INET";
+ push @ISA, "IO::Socket::IP";
}
else { # perl version < 5.004
require FileHandle;
@@ -63,6 +63,7 @@
my ($class) = @_;
my (
$errmode,
+ $family,
$fh_open,
$host,
$self,
@@ -82,6 +83,7 @@
eofile => 1,
errormode => "die",
errormsg => "",
+ family => AF_INET,
fdmask => '',
host => "localhost",
inputlog => '',
@@ -140,6 +142,9 @@
elsif (/^-?errmode$/i) {
$errmode = $args{$_};
}
+ elsif (/^-?family$/i) {
+ $family = $args{$_}
+ }
elsif (/^-?fhopen$/i) {
$fh_open = $args{$_};
}
@@ -190,6 +195,9 @@
}
elsif (defined $host) { # user wants us to open a connection to host
$self->host($host);
+ if (defined($family)) {
+ $self->family($family)
+ }
$self->open
or return;
}
@@ -885,6 +893,34 @@
@lines;
} # end sub getlines
+sub family {
+ my ($self, $family) = @_;
+ my (
+ $prev,
+ $s,
+ );
+
+ $s = *$self->{net_telnet};
+ $prev = $s->{family};
+
+ if (@_ >= 2) {
+ unless (defined $family) {
+ $family = "";
+ }
+ if ($family =~ /^[46]$/) {
+ if ($family == 4) {
+ $s->{family} = AF_INET
+ } else {
+ $s->{family} = AF_INET6
+ }
+ } else {
+ &_croak($self, "bad family \"$family\" given " .
+ "to " . ref($self) . "::family()");
+ }
+ }
+
+ $prev;
+} # end sub family
sub host {
my ($self, $host) = @_;
@@ -1200,6 +1236,7 @@
my (
$errmode,
$errno,
+ $family,
$host,
$ip_addr,
$port,
@@ -1233,6 +1270,10 @@
$self->port($args{$_})
or return;
}
+ elsif (/^-?family$/i) {
+ $self->family($args{$_})
+ or return;
+ }
elsif (/^-?timeout$/i) {
$timeout = &_parse_timeout($self, $args{$_});
}
@@ -1250,6 +1291,7 @@
## Get host and port.
$host = $self->host;
$port = $self->port;
+ $family = $self->family;
## Ensure we're already closed.
$self->close;
@@ -1275,15 +1317,20 @@
alarm $timeout;
## Lookup server's IP address.
- $ip_addr = inet_aton $host
- or die "unknown remote host: $host\n";
+ my ($err, @res) = getaddrinfo($host, $port, {socktype => SOCK_STREAM, family => $family})
+ or die "error - getaddrinfo(): $host\n";
+ if (defined($res[0])) {
+ $ip_addr = $res[0]->{addr}
+ } else {
+ die "unknown remote host: $host\n";
+ }
## Create a socket and attach the filehandle to it.
- socket $self, AF_INET, SOCK_STREAM, 0
+ socket $self, $family, SOCK_STREAM, 0
or die "problem creating socket: $!\n";
## Open connection to server.
- connect $self, sockaddr_in($port, $ip_addr)
+ connect $self, $ip_addr
or die "problem connecting to \"$host\", port $port: $!\n";
};
alarm 0;
@@ -1311,15 +1358,20 @@
$timeout = undef;
## Lookup server's IP address.
- $ip_addr = inet_aton $host
- or return $self->error("unknown remote host: $host");
+ my ($err, @res) = getaddrinfo($host, $port, {socktype => SOCK_STREAM, family => $family})
+ or return $self->error("error - getaddrinfo(): $host");
+ if (defined($res[0])) {
+ $ip_addr = $res[0]->{addr}
+ } else {
+ return $self->error("unknown remote host: $host");
+ }
## Create a socket and attach the filehandle to it.
- socket $self, AF_INET, SOCK_STREAM, 0
+ socket $self, $family, SOCK_STREAM, 0
or return $self->error("problem creating socket: $!");
## Open connection to server.
- connect $self, sockaddr_in($port, $ip_addr)
+ connect $self, $ip_addr
or do {
$errno = "$!";
$self->close;
@@ -2377,7 +2429,7 @@
sub _io_socket_include {
local $SIG{"__DIE__"} = "DEFAULT";
- eval "require IO::Socket";
+ eval "require IO::Socket::IP";
} # end sub io_socket_include
@@ -3936,6 +3988,26 @@
This method is primarily used by this class or a sub-class to perform
the user requested action when an error is encountered.
+
+=back
+
+
+=over 4
+
+=item B<family> - address family
+
+ $family = $obj->family;
+
+ $prev = $obj->family($family);
+
+This method designates the address family for C<open()>. With no
+argument it returns the current address family set in the object. With an
+argument it sets the current address family to I<$family> and returns the
+previous address family. You may indicate the address family using either 4
+for IPv4 or 6 for IPv6.
+
+The default value is C<4>. It may also be set by C<open()>
+or C<new()>.
=back