Skip to content

Commit

Permalink
switched from IO::Socket::IP to IO::Socket::INET6 for IPv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 30, 2012
1 parent 0864002 commit 67dc265
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 49 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

2.98 2012-05-30
- Switched from IO::Socket::IP to IO::Socket::INET6 for IPv6 support.
- Improved IPv6 exception handling in Mojo::IOLoop::Client.
- Improved documentation.
- Improved tests.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -346,9 +346,9 @@ L<Mojo::IOLoop> is a very minimalistic reactor based on L<Mojo::Reactor>, it
has been reduced to the absolute minimal feature set required to build solid
and scalable non-blocking TCP clients and servers.
Optional modules L<EV>, L<IO::Socket::IP> and L<IO::Socket::SSL> are supported
transparently and used if installed. Individual features can also be disabled
with the C<MOJO_NO_IPV6> and C<MOJO_NO_TLS> environment variables.
Optional modules L<EV>, L<IO::Socket::INET6> and L<IO::Socket::SSL> are
supported transparently and used if installed. Individual features can also be
disabled with the C<MOJO_NO_IPV6> and C<MOJO_NO_TLS> environment variables.
A TLS certificate and key are also built right in to make writing test servers
as easy as possible. Also note that for convenience the C<PIPE> signal will be
Expand Down
21 changes: 7 additions & 14 deletions lib/Mojo/IOLoop/Client.pm
Expand Up @@ -5,21 +5,18 @@ use IO::Socket::INET;
use Scalar::Util 'weaken';
use Socket qw(IPPROTO_TCP SO_ERROR TCP_NODELAY);

# IPv6 support requires IO::Socket::IP
# IPv6 support requires IO::Socket::INET6
use constant IPV6 => $ENV{MOJO_NO_IPV6}
? 0
: eval 'use IO::Socket::IP 0.06 (); 1';
: eval 'use IO::Socket::INET6 2.69 (); 1';

# TLS support requires IO::Socket::SSL
use constant TLS => $ENV{MOJO_NO_TLS}
? 0
: eval 'use IO::Socket::SSL 1.37 "inet4"; 1';
use constant TLS => $ENV{MOJO_NO_TLS} ? 0
: eval(IPV6 ? 'use IO::Socket::SSL 1.37 (); 1'
: 'use IO::Socket::SSL 1.37 "inet4"; 1');
use constant TLS_READ => TLS ? IO::Socket::SSL::SSL_WANT_READ() : 0;
use constant TLS_WRITE => TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;

# Fix IO::Socket::SSL to work with IO::Socket::IP
$IO::Socket::SSL::ISA[0] = 'IO::Socket::IP' if IPV6 && TLS;

# "It's like my dad always said: eventually, everybody gets shot."
has reactor => sub {
require Mojo::IOLoop;
Expand Down Expand Up @@ -60,14 +57,10 @@ sub _connect {
);
$options{LocalAddr} = $args->{local_address} if $args->{local_address};
$options{PeerAddr} =~ s/[\[\]]//g if $options{PeerAddr};
my $class = IPV6 ? 'IO::Socket::IP' : 'IO::Socket::INET';
my $class = IPV6 ? 'IO::Socket::INET6' : 'IO::Socket::INET';
return $self->emit_safe(error => "Couldn't connect.")
unless $handle = $class->new(%options);

# IPv6 needs an early start
return $self->emit_safe(error => "Couldn't connect.")
if IPV6 && !defined($handle->connect);

# Timer
$self->{timer} = $reactor->timer($args->{timeout} || 10,
sub { $self->emit_safe(error => 'Connect timeout.') });
Expand Down Expand Up @@ -206,7 +199,7 @@ implements the following new ones.
$client->connect(address => '127.0.0.1', port => 3000);
Open a socket connection to a remote host. Note that TLS support depends on
L<IO::Socket::SSL> and IPv6 support on L<IO::Socket::IP>.
L<IO::Socket::SSL> and IPv6 support on L<IO::Socket::INET6>.
These options are currently available:
Expand Down
17 changes: 7 additions & 10 deletions lib/Mojo/IOLoop/Server.pm
Expand Up @@ -8,21 +8,18 @@ use IO::Socket::INET;
use Scalar::Util 'weaken';
use Socket qw(IPPROTO_TCP TCP_NODELAY);

# IPv6 support requires IO::Socket::IP
# IPv6 support requires IO::Socket::INET6
use constant IPV6 => $ENV{MOJO_NO_IPV6}
? 0
: eval 'use IO::Socket::IP 0.06 (); 1';
: eval 'use IO::Socket::INET6 2.69 (); 1';

# TLS support requires IO::Socket::SSL
use constant TLS => $ENV{MOJO_NO_TLS}
? 0
: eval 'use IO::Socket::SSL 1.37 "inet4"; 1';
use constant TLS => $ENV{MOJO_NO_TLS} ? 0
: eval(IPV6 ? 'use IO::Socket::SSL 1.37 (); 1'
: 'use IO::Socket::SSL 1.37 "inet4"; 1');
use constant TLS_READ => TLS ? IO::Socket::SSL::SSL_WANT_READ() : 0;
use constant TLS_WRITE => TLS ? IO::Socket::SSL::SSL_WANT_WRITE() : 0;

# Fix IO::Socket::SSL to work with IO::Socket::IP
$IO::Socket::SSL::ISA[0] = 'IO::Socket::IP' if IPV6 && TLS;

# To regenerate the certificate run this command (18.04.2012)
# openssl req -new -x509 -keyout server.key -out server.crt -nodes -days 7300
my $CERT = catfile dirname(__FILE__), 'server.crt';
Expand Down Expand Up @@ -62,7 +59,7 @@ sub listen {

# Reuse file descriptor
my $handle;
my $class = IPV6 ? 'IO::Socket::IP' : 'IO::Socket::INET';
my $class = IPV6 ? 'IO::Socket::INET6' : 'IO::Socket::INET';
if (defined $fd) {
$handle = $class->new;
$handle->fdopen($fd, 'r') or croak "Can't open file descriptor $fd: $!";
Expand Down Expand Up @@ -230,7 +227,7 @@ implements the following new ones.
$server->listen(port => 3000);
Create a new listen socket. Note that TLS support depends on
L<IO::Socket::SSL> and IPv6 support on L<IO::Socket::IP>.
L<IO::Socket::SSL> and IPv6 support on L<IO::Socket::INET6>.
These options are currently available:
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -298,10 +298,9 @@ Mojo::Server::Daemon - Non-blocking I/O HTTP 1.1 and WebSocket server
L<Mojo::Server::Daemon> is a full featured non-blocking I/O HTTP 1.1 and
WebSocket server with C<IPv6>, C<TLS> and C<libev> support.
Optional modules L<EV>, L<IO::Socket::IP>, L<IO::Socket::SSL> and
L<Net::Rendezvous::Publish> are supported transparently and used if installed.
Individual features can also be disabled with the C<MOJO_NO_IPV6> and
C<MOJO_NO_TLS> environment variables.
Optional modules L<EV>, L<IO::Socket::INET6> and L<IO::Socket::SSL> are
supported transparently and used if installed. Individual features can also be
disabled with the C<MOJO_NO_IPV6> and C<MOJO_NO_TLS> environment variables.
See L<Mojolicious::Guides::Cookbook> for more.
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -382,10 +382,9 @@ You can run the exact same command again for automatic hot deployment.
For L<Mojolicious> and L<Mojolicious::Lite> applications it will default to
C<production> mode.
Optional modules L<EV>, L<IO::Socket::IP>, L<IO::Socket::SSL> and
L<Net::Rendezvous::Publish> are supported transparently and used if installed.
Individual features can also be disabled with the C<MOJO_NO_IPV6> and
C<MOJO_NO_TLS> environment variables.
Optional modules L<EV>, L<IO::Socket::INET6> and L<IO::Socket::SSL> are
supported transparently and used if installed. Individual features can also be
disabled with the C<MOJO_NO_IPV6> and C<MOJO_NO_TLS> environment variables.
See L<Mojolicious::Guides::Cookbook> for more.
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Server/Morbo.pm
Expand Up @@ -130,10 +130,9 @@ To start applications with it you can use the L<morbo> script.
$ morbo myapp.pl
Server available at http://127.0.0.1:3000.
Optional modules L<EV>, L<IO::Socket::IP>, L<IO::Socket::SSL> and
L<Net::Rendezvous::Publish> are supported transparently and used if installed.
Individual features can also be disabled with the C<MOJO_NO_IPV6> and
C<MOJO_NO_TLS> environment variables.
Optional modules L<EV>, L<IO::Socket::INET6> and L<IO::Socket::SSL> are
supported transparently and used if installed. Individual features can also be
disabled with the C<MOJO_NO_IPV6> and C<MOJO_NO_TLS> environment variables.
See L<Mojolicious::Guides::Cookbook> for more.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -607,9 +607,9 @@ Mojo::UserAgent - Non-blocking I/O HTTP 1.1 and WebSocket user agent
L<Mojo::UserAgent> is a full featured non-blocking I/O HTTP 1.1 and WebSocket
user agent with C<IPv6>, C<TLS> and C<libev> support.
Optional modules L<EV>, L<IO::Socket::IP> and L<IO::Socket::SSL> are supported
transparently and used if installed. Individual features can also be disabled
with the C<MOJO_NO_IPV6> and C<MOJO_NO_TLS> environment variables.
Optional modules L<EV>, L<IO::Socket::INET6> and L<IO::Socket::SSL> are
supported transparently and used if installed. Individual features can also be
disabled with the C<MOJO_NO_IPV6> and C<MOJO_NO_TLS> environment variables.
See L<Mojolicious::Guides::Cookbook> for more.
Expand Down
6 changes: 4 additions & 2 deletions lib/Mojolicious/Command/version.pm
Expand Up @@ -18,7 +18,9 @@ sub run {

# IPv6
my $ipv6
= Mojo::IOLoop::Server::IPV6 ? $IO::Socket::IP::VERSION : 'not installed';
= Mojo::IOLoop::Server::IPV6
? $IO::Socket::INET6::VERSION
: 'not installed';

# TLS
my $tls
Expand All @@ -31,7 +33,7 @@ CORE
OPTIONAL
EV ($ev)
IO::Socket::IP ($ipv6)
IO::Socket::INET6 ($ipv6)
IO::Socket::SSL ($tls)
EOF
Expand Down
5 changes: 2 additions & 3 deletions lib/Mojolicious/Guides/FAQ.pod
Expand Up @@ -30,9 +30,8 @@ without compromises. While there are no rules in
L<Mojolicious::Guides::CodingGuidelines> that forbid dependencies, we do
currently discourage adding non-optional ones in favor of a faster and more
painless installation process. And we do in fact already use several optional
CPAN modules such as L<EV>, L<IO::Socket::IP>, L<IO::Socket::SSL>,
L<Net::Rendezvous::Publish> and L<Plack> to provide advanced functionality if
they are installed.
CPAN modules such as L<EV>, L<IO::Socket::INET6>, L<IO::Socket::SSL> and
L<Plack> to provide advanced functionality if they are installed.

=head2 Why reinvent wheels?

Expand Down
2 changes: 1 addition & 1 deletion t/mojo/ioloop_ipv6.t
Expand Up @@ -7,7 +7,7 @@ use Test::More;
use Mojo::IOLoop::Server;
plan skip_all => 'set TEST_IPV6 to enable this test (developer only!)'
unless $ENV{TEST_IPV6};
plan skip_all => 'IO::Socket::IP 0.06 required for this test!'
plan skip_all => 'IO::Socket::INET6 2.69 required for this test!'
unless Mojo::IOLoop::Server::IPV6;
plan tests => 2;

Expand Down
2 changes: 1 addition & 1 deletion t/mojo/user_agent_online.t
Expand Up @@ -10,7 +10,7 @@ use Test::More;
use Mojo::IOLoop::Server;
plan skip_all => 'set TEST_ONLINE to enable this test (developer only!)'
unless $ENV{TEST_ONLINE};
plan skip_all => 'IO::Socket::IP 0.06 required for this test!'
plan skip_all => 'IO::Socket::INET6 2.69 required for this test!'
unless Mojo::IOLoop::Server::IPV6;
plan skip_all => 'IO::Socket::SSL 1.37 required for this test!'
unless Mojo::IOLoop::Server::TLS;
Expand Down

0 comments on commit 67dc265

Please sign in to comment.