#!/usr/bin/php $value ) { $b[$key] = strtoupper( trim( $value )); } if ( $b[2] == "ASN" ) { if ( $b[4] > 1 ) { /* ASN range defined */ $dot = strpos( $b[3], "." ); if ( $dot === FALSE ) { /* Normal integer representation of both 2-byte and 4-byte ASN */ for ( $i = $b[3]; $i < ( $b[3] + $b[4] ); $i++ ) { $data[$b[1]]["ASN"][] = $i; } } else { /* Handle Canonical Textual Form of 4-byte ASN with range defined */ $x = substr( $b[3], 0, $dot ); $y = substr( $b[3], $dot + 1 ); for ( $i = $y; $i < ( $y + $b[4] ); $i++ ) { $data[$b[1]]["ASN"][] = $x.".".$i; } } } else { /* Single ASN (either 2 og 4 byte) */ $data[$b[1]]["ASN"][] = $b[3]; } } elseif ( $b[2] == "IPV4" ) { $suffix = 32 - log( $b[4], 2 ); if ( strstr( $suffix, "." ) === FALSE ) { $data[$b[1]]["IPV4"][] = $b[3]."/".$suffix; } else { $err[$b[3]]["CC"] = $b[1]; $err[$b[3]]["SUFFIX"] = $suffix; } } elseif ( $b[2] == "IPV6" ) { $data[$b[1]]["IPV6"][] = $b[3]."/".$b[4]; } else { /* Unknown type */ $data[$b[1]][$b[2]][] = $b[3]; } } fclose($fp); } /* Simple fault checking */ if ( sizeof( $err ) > 50000 ) { fprintf( STDERR, "More than 50000 faulty prefixes. Somethings funky. Terminating\n" ); exit ( 1 ); } /******** NOTICE ********* The above code detects and stores faulty IPv4 prefixes in the err-array. These are cases where the amount of allocated IP-addresses do not align with a proper network mask. One way to correct any faulty prefixes is by correlating the entries in the err-array with BGP anouncements. There are several free services that may be used for this exact purpose, however I chose not to include the code to do that here, because such services run with limited resources. But please do drop us an e-mail, and we'll happily share more code with you. ********* NOTICE ********/ /* Create a flat file structure of the data */ print( "Storing data in file structure at ".$conf["DBDIR"]."\n" ); @mkdir($conf["DBDIR"]); foreach ( $data as $cc => $d ) { if ( sizeof( $d["ASN"] ) > 0 ) { $list_array = $d["ASN"]; natsort( $list_array ); $list = ""; foreach ( $list_array as $asn ) { $list .= "AS".$asn."\n"; } file_put_contents( $conf["DBDIR"]."/".$cc."_ASN", $list ); } if ( sizeof( $d["IPV4"] ) > 0 ) { $list_array = $d["IPV4"]; natsort( $list_array ); $list = ""; foreach ( $list_array as $prefix ) { $list .= $prefix."\n"; } file_put_contents( $conf["DBDIR"]."/".$cc."_IPV4", $list ); } if ( sizeof( $d["IPV6"] ) > 0 ) { $list_array = $d["IPV6"]; natsort( $list_array ); $list = ""; foreach ( $list_array as $prefix ) { $list .= $prefix."\n"; } file_put_contents( $conf["DBDIR"]."/".$cc."_IPV6", $list ); } } ?>