#!/usr/bin/perl -w
#
# Copyright (C) 2007, Joshua D. Abraham (jabra@spl0it.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# valid_iplist.pl - validates IPs from a list
# 
# ex: ./valid_iplist.pl -f iplist.txt -e              Print Valid External IPs
# ex: ./valid_iplist.pl -f iplist.txt -i              Print Valid Internal IPs
# ex: ./valid_iplist.pl -f iplist.txt -l              Print Valid Loopback IPs
#
use strict;

use Getopt::Long;
use Data::Validate::IP;

use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//;    # Truncate calling path from the prog name

my $AUTH    = 'Joshua D. Abraham';  # author
my $EMAIL    = 'jabra@spl0it.org';  # email
my $VERSION = '1.01';               # version

my %options;
my $type;
my $file;
my $v = Data::Validate::IP->new();

#
# help ->
# display help information
# side effect:  exits program
#
sub help {
    print "Usage: $PROG [Input Type] [General Options]

   -f  --file [iplist-file]     List of IPs ( one per line )

   -i  --internal               Internal IP validation
   -e  --external               External IP validation
   -l  --loop                   Loopback IP validation

   -v  --version                Display version
   -h  --help                   Display this information

Send Comments to $AUTH ( $EMAIL )\n";
    exit;
}


#
# print_version ->
# displays version
# side effect: exits program
#
sub print_version {
    print "$PROG version $VERSION by $AUTH ( $EMAIL )\n";
    exit;
}


GetOptions(
    \%options,
    'file|f=s',
    'external|e' => sub { $type = 'external' },
    'internal|i' => sub { $type = 'internal' },
    'loopback|l' => sub { $type = 'loopback' },
    'help|h'    => sub { help(); },
    'version|v' => sub { print_version(); },
    )
    or exit 1;

if ( $options{file} ) {
    if ( -r $options{file} ) {
        $file = $options{file};
    }
    else {
        print "Error: Input file doesn't exist or isnt readable\n";
        exit;
    }
}
else {
    help();
}

open(INPUT, "<$file");
while( <INPUT> ) {
    chomp;
    my $out;
    if ( /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) {
        my $ip = $1;
        if ($type eq 'internal') {
            $out = $v->is_private_ipv4($ip);    
        }
        elsif ($type eq 'external') {
            $out = $v->is_public_ipv4($ip);    
        }
        else {
            $out = $v->is_loopback_ipv4($ip); 
        }
        if (defined($out)) {
            print "$out\n";
        }
    }
}
