#!/usr/bin/perl use strict; use warnings; # Class package ip2country; # Constructor sub new { my ($class) = @_; my $self = {}; bless ($self, $class); $self->{ips} = (); $self; } # Has another node sub hasNext { my $self = shift; if ($self->{ips} [$self->{pos}] ) { $self->{pos}++; return 1; } else { return 0; } } # Get the next node sub getNext { my $self = shift; return $self->{ips} [$self->{pos}]; } # Populate range list sub load { my $self = shift; my $f; open $f, "{ips}}, { start => $start, end => $end, country => $country }; } close $f; } # Convert dotted quad IP to integer sub ip2long { my ($self, $ip) = @_; my @parts = split /\./, $ip; ($parts[0] * (256 ** 3)) + ($parts[1] * (256 ** 2)) + ($parts[2] * 256) + $parts[3]; } # Use above functions to translate an ip address to country sub getIP { my ($self, $ip) = @_; my $ipn = $self->ip2long($ip); # Reset position counter $self->{pos} = 0; # Iterate through list of ip ranges while ($self->hasNext) { # Snag this one my $cur = $self->getNext; # If it isn't real, fuck it last unless $cur; # If it matches, stop list and return it if ($ipn > $cur->{start} && $ipn < $cur->{end}) { return $cur->{country}; last; # redundancy } } 'idk'; } # Code ran when file is run directly package main; my $it = ip2country->new; $it->load; print $it->getIP('65.34.27.38'); print "\n";