Skip to content

Commit

Permalink
RT#77458 re-work IPv6 support
Browse files Browse the repository at this point in the history
Do not require a special parameter for IPv6 support but try to to do it
automatically.
Use parameters inet4 => N / inet6 => N to force a specific protocol.
  • Loading branch information
marschap committed Sep 5, 2012
1 parent 6144910 commit fc4bea5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
24 changes: 17 additions & 7 deletions lib/Net/LDAP.pm
Expand Up @@ -5,6 +5,7 @@
package Net::LDAP;

use strict;
use Socket qw(AF_INET AF_INET6 AF_UNSPEC);
use IO::Socket;
use IO::Select;
use Tie::Hash;
Expand All @@ -28,6 +29,8 @@ use Net::LDAP::Constant qw(LDAP_SUCCESS
LDAP_UNAVAILABLE
);

use constant CAN_IPV6 => eval { require IO::Socket::INET6 } ? 1 : 0;

$VERSION = "0.44";
@ISA = qw(Tie::StdHash Net::LDAP::Extra);
$LDAP_VERSION = 3; # default LDAP protocol version
Expand Down Expand Up @@ -135,21 +138,23 @@ sub new {
sub connect_ldap {
my ($ldap, $host, $arg) = @_;
my $port = $arg->{port} || 389;
my $class = 'IO::Socket::INET';
my $class = (CAN_IPV6) ? 'IO::Socket::INET6' : 'IO::Socket::INET';
my $domain = $arg->{inet4} ? AF_INET : ($arg->{inet6} ? AF_INET6 : AF_UNSPEC);

# separate port from host overwriting given/default port
$host =~ s/^([^:]+|\[.*\]):(\d+)$/$1/ and $port = $2;

if ($arg->{inet6}) {
require IO::Socket::INET6;
$class = 'IO::Socket::INET6';
}
if ($arg->{inet6} && !CAN_IPV6) {
$@ = 'unable to load IO::Socket::INET6; no IPv6 support';
return undef;
}

$ldap->{net_ldap_socket} = $class->new(
PeerAddr => $host,
PeerPort => $port,
LocalAddr => $arg->{localaddr} || undef,
Proto => 'tcp',
Domain => $domain,
MultiHomed => $arg->{multihomed},
Timeout => defined $arg->{timeout}
? $arg->{timeout}
Expand All @@ -167,10 +172,14 @@ my %ssl_verify = qw(none 0 optional 1 require 3);
sub connect_ldaps {
my ($ldap, $host, $arg) = @_;
my $port = $arg->{port} || 636;
my $domain = $arg->{inet4} ? AF_INET : ($arg->{inet6} ? AF_INET6 : AF_UNSPEC);

if ($arg->{inet6} && !CAN_IPV6) {
$@ = 'unable to load IO::Socket::INET6; no IPv6 support';
return undef;
}

require IO::Socket::INET6 if ($arg->{inet6});
require IO::Socket::SSL;
IO::Socket::SSL->import(qw/inet6/) if ($arg->{inet6});

# separate port from host overwriting given/default port
$host =~ s/^([^:]+|\[.*\]):(\d+)$/$1/ and $port = $2;
Expand All @@ -180,6 +189,7 @@ sub connect_ldaps {
PeerPort => $port,
LocalAddr => $arg->{localaddr} || undef,
Proto => 'tcp',
Domain => $domain,
Timeout => defined $arg->{'timeout'} ? $arg->{'timeout'} : 120,
_SSL_context_init_args($arg)
) or return undef;
Expand Down
12 changes: 5 additions & 7 deletions lib/Net/LDAP.pod
Expand Up @@ -161,16 +161,14 @@ perl-ldap.

Example: raw =E<gt> qr/(?i:^jpegPhoto|;binary)/

=item inet4 =E<gt> N
=item inet6 =E<gt> N

Try to connect to the server using IPv6 if C<HOST> resolves to an
IPv6 target address.
If it resolves to an IPv4 address, the connection is tried using IPv4,
the same way as if this option was not given.
Try to connect to the server using the specified IP protocol only,
i.e. either IPv4 or IPv6.
If the protocol selected is not supported, iconnecting will fail.

Please note that IPv6 support is considered experimental in
IO::Socket::SSL, which is used of SSL/TLS support, and there are a few
issues to take care of. See L<IO::Socket::SSL/IPv6> for details.
The default is to use any of the two protocols.

=back

Expand Down

0 comments on commit fc4bea5

Please sign in to comment.