java.lang.Object
net.i2p.router.transport.GeoIPv6

public class GeoIPv6extends Object
Generate compressed geoipv6.dat.gz file, and lookup entries in it.

Public only for command line use, not a public API, not for external use.

  • Field Details

  • Constructor Details

    • GeoIPv6

      private GeoIPv6()
      Utility class.
  • Method Details

    • readGeoIPFile

      public static String[] readGeoIPFile(I2PAppContext context, Long[] search, Map<String,String> codeCache)
      Lookup search items in the geoip file. See below for format.
      Parameters:
      context - the I2P app context
      search - a sorted array of IPs to search
      codeCache - the country code cache
      Returns:
      an array of country codes, same order as the search param, or a zero-length array on total failure. Individual array elements will be null for lookup failure of that item.
    • readGeoIPFile

      private static String[] readGeoIPFile(I2PAppContext context, File geoFile, Long[] search, Map<String,String> codeCache, Log log)
      Lookup search items in the geoip file. See below for format.
      Parameters:
      context - the application context, notified of the data file version
      geoFile - the gzip-compressed IPv6 geoip data file to read
      search - a sorted array of IPs to search
      codeCache - reuses one String per lower-case country code
      log - where to log read errors and timing
      Returns:
      an array of country codes, same order as the search param, or a zero-length array on total failure. Individual array elements will be null for lookup failure of that item.
    • compressGeoIPv6CSVFiles

      private static boolean compressGeoIPv6CSVFiles(List<File> inFiles, File outFile)
      Read in and parse multiple IPv6 geoip CSV files, merge them, and write out a gzipped binary IPv6 geoip file.

      Acceptable input formats (IPv6 only):

      #comment (# must be in column 1)
      "text IP", "text IP", "bigint IP", "bigint IP", "country code", "country name"
      
      Quotes and spaces optional. Sorting not required. Country code case-insensitive. Fields 1, 2, and 5 are used; fields 3, 4, and 6 are ignored. This is identical to the format of the MaxMind GeoLite IPv6 file.

      Example:

      "2001:200::", "2001:200:ffff:ffff:ffff:ffff:ffff:ffff", "42540528726795050063891204319802818560", "42540528806023212578155541913346768895", "JP", "Japan"
      
      Output format:
      Bytes 0-9: Magic number "I2PGeoIPv6"
      Bytes 10-11: version (0x0001)
      Bytes 12-15 flags (0)
      Bytes 16-23: Date (long)
      Bytes 24-xx: Comment (UTF-8)
      Bytes xx-255: null padding
      Bytes 256-: 18 byte records:
            8 byte from (/64)
            8 byte to (/64)
            2 byte country code LOWER case (ASCII)
      Data must be sorted (SIGNED twos complement), no overlap
      

      SLOW. For preprocessing only!

      Parameters:
      inFiles - the CSV files to merge, each plain or gzip-compressed
      outFile - the merged binary database to write, gzip-compressed
      Returns:
      true if the merged database was written, false on an I/O error
    • toLong

      private static long toLong(byte[] ip)
      Converts an address to a long.
      Parameters:
      ip - the address bytes
      Returns:
      the address as a long
    • writeLong

      private static void writeLong(OutputStream rawStream, long value) throws IOException
      Like DataHelper.writeLong(OutputStream, int, long) but allows negative values.
      Parameters:
      rawStream - where to write the eight big-endian bytes
      value - the value to write, negative allowed
      Throws:
      IOException - if the stream write fails
    • readLong

      private static long readLong(byte[] src, int offset)
      Like DataHelper.readLong(InputStream, int) but allows negative values.
      Parameters:
      src - the bytes to read eight big-endian bytes from
      offset - index of the first byte to read
      Returns:
      the decoded value, negative when the high bit is set
    • main

      public static void main(String[] args)
      Merge and compress CSV files to I2P compressed format

      GeoIPv6 infile1.csv[.gz] [infile2.csv[.gz]...] outfile.dat.gz

      Used to create the file for distribution, do not comment out.

      Parameters:
      args - command line arguments