#!/usr/bin/perl -w

#
#  census_to_intermediate_map
#  Copyright (C) 2010 by USC/ISI
#  $Id$
#  
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License,
#  version 2, as published by the Free Software Foundation.
#  
#  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.
#  
#  
#  This code was originally written by Xun Fan <xunfan@isi.edu>.
#

# only extract 128.0.0.0/9 addresses
#my $match_prefix_mask = ~(2**(32-9)-1); # mask length 9 
#my $match_prefix_net = $match_prefix_mask & hex(substr("80". "00000000", 0, 8)); #  128.0.0.0/9

# /24 addresses
#my $c_prefix_mask = ~(2**8 - 1);
my $ICMP_code = "00";
my $ICMP_reply = "00";

while(<>){
	my($ip, $type, $code) = split;
#	
#	my $ip_n = inet_aton($ip); //this is bullshit, inet_aton() is only used to do DNS query in Perl!!!!!!!!
#	my ($ip1, $ip2, $ip3, $ip4) = split(/\./, $ip);

	my $ip_hex = hex($ip);        # int($ip1)*(2**24)+int($ip2)*(2**16)+int($ip3)*(2**8)+int($ip4);

	# not in the range(128.0.0.0/9), drop it
	#next if (($ip_hex & $match_prefix_mask) !=  $match_prefix_net);

	# skip nonsense records
	next if ($ip_hex == 0);

	# not echo reply, drop it
	next if ($type ne $ICMP_reply);
	next if ($code ne $ICMP_code);

	# get prefix
#	my $prefix = $ip_hex & $c_prefix_mask;
#  	my $prefix_print = sprintf("%08x", $prefix);
	
	print "$ip\n";
}
