IP-Country-DB_File

 view release on metacpan or  search on metacpan

lib/IP/Country/DB_File.pm  view on Meta::CPAN

        or return undef;
    # Verify that key starts with '4' and isn't from IPv6 range.
    return undef if ord($key) != 52;

    my $start = substr($data, 0, 4);
    my $cc    = substr($data, 4, 2);

    return $addr ge $start ? $cc : undef;
}

sub inet6_ntocc {
    my ($this, $addr) = @_;

    $addr = substr($addr, 0, 8);

    my ($key, $data);
    $this->{db}->seq($key = "6$addr", $data, DB_File::R_CURSOR()) == 0
        or return undef;
    my $start = substr($data, 0, 4);
    my $cc    = substr($data, 4, 2);

    return $addr ge $start ? $cc : undef;
}

sub inet6_atocc {
    my ($this, $host) = @_;

    my ($err, $result) = Socket::getaddrinfo($host, undef, {
        family   => Socket::AF_INET6,
        socktype => Socket::SOCK_STREAM,
    });
    return undef if $err || !$result;
    my (undef, $addr) = Socket::unpack_sockaddr_in6($result->{addr});

    $addr = substr($addr, 0, 8);

lib/IP/Country/DB_File.pm  view on Meta::CPAN


version 3.03

=head1 SYNOPSIS

    use IP::Country::DB_File;

    my $ipcc = IP::Country::DB_File->new();
    my $cc = $ipcc->inet_atocc('1.2.3.4');
    my $cc = $ipcc->inet_atocc('host.example.com');
    my $cc = $ipcc->inet6_atocc('1a00:300::');
    my $cc = $ipcc->inet6_atocc('ipv6.example.com');

=head1 DESCRIPTION

IP::Country::DB_File is a light-weight module for fast IP address to country
translation based on L<DB_File>. The country code database is stored in a
Berkeley DB file. You have to build the database using C<build_ipcc.pl> or
L<IP::Country::DB_File::Builder> before you can lookup country codes.

This module tries to be API compatible with the other L<IP::Country> modules.
The installation of L<IP::Country> is not required.

lib/IP/Country/DB_File.pm  view on Meta::CPAN


Returns undef if there's no country code listed for the IP address, the DNS
lookup fails, or the host string is invalid.

=head2 inet_ntocc

    my $cc = $ipcc->inet_ntocc($packed_address);

Like I<inet_atocc> but works with a packed IPv4 address.

=head2 inet6_atocc

    my $cc = $ipcc->inet6_atocc($host);

Like I<inet_atocc> but works with IPv6 addresses or hosts.

=head2 inet6_ntocc

    my $cc = $ipcc->inet6_ntocc($packed_address);

Like I<inet_ntocc> but works with a packed IPv6 address.

=head2 db_time

    my $time = $ipcc->db_time();

Returns the mtime of the DB file.

=head1 SEE ALSO

t/01-basic.t  view on Meta::CPAN

    fe00::                                  ?
    ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff ?
);

SKIP: {
    skip("Socket doesn't support IPv6", 18)
        if !$ipv6_supported;

    for (my $i = 0; $i < @tests_v6; $i += 2) {
        my ($ip, $test_cc) = ($tests_v6[$i], $tests_v6[$i+1]);
        my $cc = $ipcc->inet6_atocc($ip);
        $cc = '?' unless defined($cc);
        ok($cc eq $test_cc, "lookup $ip, got $cc, expected $test_cc");
    }
}

unlink($filename);



( run in 1.527 second using v1.01-cache-2.11-cpan-87723dcf8b7 )