#! /usr/bin/perl
#
# MKREJECT.PL (2007/07/17)
#
# (C) 2007, HIRATA Yasuyuki <yasu@asuka.net>, all rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR (HIRATA YASUYUKI) ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use strict;
use Locale::Country;

my $OUTFILE_FMT = "reject_%s";
Locale::Country::rename_country(tw => "Republic of China (Taiwan)");
Locale::Country::rename_country(cn => "People's Republic of China (Sino)");
Locale::Country::rename_country(kr => "Republic of Korea (South Korea)");

MAIN: {
  my %NETBLOCKS;

  while(<>) {
    chomp;
    my @f = split /\|/;

    next if(/^#/);
    next if(not $f[1] =~ /^[A-Z][A-Z]$/ or
	    not $f[2] eq "ipv4");

    my($cc, $start, $size) = @f[1, 3, 4];
    next if(not $start =~ /\.0$/);

    if($size%16777216 == 0) {
      for(my $i = 0; $i < $size/16777216; $i++) {
	my @octets = split /\./, $start;
	my $decimal = ($octets[0] + $i);
	my $netaddr = sprintf("%d", $decimal);
	push @{$NETBLOCKS{$cc}}, $netaddr;
      }
    } elsif($size%65536 == 0) {
      for(my $i = 0; $i < $size/65536; $i++) {
	my @octets = split /\./, $start;
	my $decimal = ($octets[0]*256 +
		       $octets[1] + $i);
	my $netaddr = sprintf("%d.%d",
			      ($decimal >> 8) & 0xff,
			      ($decimal >> 0) & 0xff);
	push @{$NETBLOCKS{$cc}}, $netaddr;
      }
    } else {
      for(my $i = 0; $i < int($size/256); $i++) {
	my @octets = split /\./, $start;
	my $decimal = ($octets[0]*65536 +
		       $octets[1]*256 +
		       $octets[2] + $i);
	my $netaddr = sprintf("%d.%d.%d",
			      ($decimal >> 16) & 0xff,
			      ($decimal >> 8) & 0xff,
			      ($decimal >> 0) & 0xff);
	push @{$NETBLOCKS{$cc}}, $netaddr;
      }
    }
  }

  foreach my $c (keys %NETBLOCKS) {
    my $country = code2country($c);
    if($country) {
      $country =~ s/(.*),\s*(.*)/$2 $1/;
    } else {
      $country = "Asia Pacific Region" if($c eq "AP");
    }

    my $outfile = sprintf $OUTFILE_FMT, lc $c;
    open my $fh, "> $outfile" or die;
    foreach my $n (@{$NETBLOCKS{$c}}) {
      print $fh "$n\t\tREJECT $country rejected\n";
    }
    close $fh;
    system "postmap $outfile";
  }
}
