NAME
IP::Registry - lookup country codes by IP address
SYNOPSIS
use IP::Registry;
DESCRIPTION
Finding the home country of a client using only the IP address can be difficult. Looking up the domain name associated with that address can provide some help, but many IP address are not reverse mapped to any useful domain, and the most common domain (.com) offers no help when looking for country.
This module comes bundled with a database of countries where various IP addresses have been assigned. Although the country of assignment will probably be the country associated with a large ISP rather than the client herself, this is probably good enough for most log analysis applications.
This module will probably be most useful when used after domain lookup has failed, or when it has returned a non-useful TLD (.com, .net, etc.).
CONSTRUCTOR
The constructor takes no arguments.
use IP::Registry;
my $reg = IP::Registry->new();
OBJECT METHODS
All object methods are designed to be used in an object-oriented fashion.
$result = $object->foo_method($bar,$baz);
Using the module in a procedural fashion (without the arrow syntax) won't work.
- $cc = $reg->inet_atocc(HOSTNAME)
-
Takes a string giving the name of a host, and translates that to an two-letter country code. Takes arguments of both the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name cannot be resolved, returns undef. If the resolved IP address is not contained within the database, returns undef. For multi-homed hosts (hosts with more than one address), the first address found is returned.
If domain names are submitted to inet_atocc that end with a two-letter top-level domain, this is upper-cased and returned without further effort. If you don't like this behaviour, call Socket::inet_aton() on the hostname and pass it to IP::Registry::inet_ntocc() rather than this method.
- $cc = $reg->inet_ntocc(IP_ADDRESS)
-
Takes a string (an opaque string as returned by Socket::inet_aton()) and translates it into a two-letter country code. If the IP address is not contained within the database, returns undef.
PERFORMANCE
With a random selection of 65,000 IP addresses, the module can look up about 10,000 IP addresses per second. This is on 730MHz Pentium III (Coppermine) with 512MB RAM. Out of this random selection of IP addresses (many of which will not exist), 40% had an associated country code.
BUGS/LIMITATIONS
A few weird IP ranges have been assigned by the registries. By weird, I mean ranges that are not powers of two. I've made a compromise with these ranges for the sake of performance. Any weird ranges are rounded down to the nearest power of two. For example, the range beginning at 24.24.0.0 has a range of 393216 IPs, but I have rounded this down to a range of 262144 (2**18), which means that one third of the IPs in this range will be missed. Sorry, such is life! IP to CC tanslation isn't an exact science.
Only works with IPv4 addresses.
SEE ALSO
IP::Country - slower, but more accurate. Uses reverse hostname lookups before consulting this database.
Geo::IP - wrapper around the geoip C libraries. Faster, but less portable.
www.apnic.net - Asia pacific
www.ripe.net - Europe
www.arin.net - North America
www.lacnic.net - Latin America (soon)
COPYRIGHT
Copyright (C) 2002 Nigel Wetters. All Rights Reserved.
NO WARRANTY. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Some parts of this software distribution are derived from the APNIC, ARIN and RIPE databases (copyright details below). The author of this module makes no claims of ownership on those parts.
APNIC conditions of use
The files are freely available for download and use on the condition that APNIC will not be held responsible for any loss or damage arising from the application of the information contained in these reports.
APNIC endeavours to the best of its ability to ensure the accuracy of these reports; however, APNIC makes no guarantee in this regard.
In particular, it should be noted that these reports seek to indicate the country where resources were first allocated or assigned. It is not intended that these reports be considered as an authoritative statement of the location in which any specific resource may currently be in use.
ARIN database copyright
Copyright (c) American Registry for Internet Numbers. All rights reserved.
RIPE database copyright
The information in the RIPE Database is available to the public for agreed Internet operation purposes, but is under copyright. The copyright statement is:
"Except for agreed Internet operational purposes, no part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, recording, or otherwise, without prior permission of the RIPE NCC on behalf of the copyright holders. Any use of this material to target advertising or similar activities is explicitly forbidden and may be prosecuted. The RIPE NCC requests to be notified of any such activities or suspicions thereof."