#!/usr/local/bin/perl # nnpack.pl ciscobgpfile # net number packing $infname=$ARGV[0]; open(infile,"$infname") || die("Cannot open input file $infname"); while(){ ($flags,$net,@r)=split(':',$_); if($flags =~ "etwork"){ ($netnum,$mask)=split('/',$net); ($a1,$a2,$a3,$a4)=split('\.',$netnum); if(!$mask){ $mask=24; if($a1 < 192){$mask=16;} if($a1 < 128){$mask=8;} } $a4=0; # ignore fourth byte if($mask > 24){$mask=24;} # set granularity $ccnt=2**(24-$mask); # count of Class-C numbers $addr=($a1<<16)+($a2<<8)+$a3; # at proper granularity printf stderr "%x\t%d\t%15s:$mask\t\n",$addr,$ccnt,$netnum; for ($i = $ccnt; $i > 0; $i--) { $naddr=$addr+($ccnt-$i); $abyte=$naddr>>3; # address by byte $bitoff=$naddr&0x07; # offset $ma{$abyte}=$ma{$abyte} | (2**$bitoff); # printf stderr "%x\t%x\t%x\t%x\n",$naddr,$abyte,$bitoff,$ma{$abyte}; $clccnt++; } } } printf stderr "loaded... $clccnt Class-C equivs counted, writing...\n"; printf"P1\n4096 4096\n"; # PBM ASCII format for ($i = 0; $i <= 2097151; $i++) { for ($j = 7; $j >= 0; $j--) { $v=$ma{$i}; $e=($v>>$j)&1; printf"%d ",$e; if($e == 1){$clccnt2++;} } printf"\n"; } printf stderr "Done. $clccnt2 Class-C equivs counted, %d hosts\n"; $clccnt2*256;