Search code examples
perlipv6lwp

HTTP::Request use IPv6 interface


I have a Linux machine with IPv6 support, local loopback interface: ::1.

I created a perl script that sends an HTTP request to the local virtual host:

#!/usr/bin/perl

use strict;
use warnings;

use Net::INET6Glue::INET_is_INET6;
use LWP;

my $user_agent = LWP::UserAgent->new;
my $url = 'http://lwt/docs/info.php';
my $request = HTTP::Request->new(GET => $url);
my $response = $user_agent->request($request);
if ($response->is_success) {
    print $response->decoded_content;
}
else {
    die($response->status_line);
}

info.php file:

<?php
print $_SERVER['REMOTE_ADDR'];

lwt alias is written in /etc/hosts file like this:

127.0.0.1       lwt

Currently the output is: "127.0.0.1". So, the request is sent via IPv4.

Is it possible to tell Perl to use IPv6 interface instead?


Solution

  • In order to make an IPv6 connection, LWP has to find an IPv6 address for your hostname. Since the address you're providing via /etc/hosts, 127.0.0.1, is an IPv4 address, this doesn't happen. You need a hosts entry like ::1 lwt so that the hostname resolves to the IPv6 loopback address instead.