Version 6 of Basic GeoIP with Tcl

Updated 2004-09-24 13:56:56 by rmax

Kroc - 24 Sep 2004 - This is basic GeoIP (see http://www.maxmind.com for details) that return country code for a given IP adress.

    ################################################################################
    #
    # Basic GeoIP for tcl.
    #
    # Copyright © 2004 - David Zolli - http://www.kroc.tk
    #
    # This script is under NOL : http://wiki.tcl.tk/nol
    #
    # Version 1.0 - 24 Sep 2004
    #
    ################################################################################

    # This needs Maxmind CVS database available here :
    # http://www.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip
    proc GeoIP  { IP {cvsfile GeoIPCountryWhois.csv} } {
        foreach "a b c d" [split $IP .] {
            set V [expr 300+$a][expr 300+$b][expr 300+$c][expr 300+$d]
        }
        set fin [open $cvsfile r]
        while {![eof $fin]} {
            foreach "1 2 3 4 5 6" [split [gets $fin] ,] {
                foreach "a b c d" [split [lindex $1 0] .] {
                    set min [expr 300+$a][expr 300+$b][expr 300+$c][expr 300+$d]
                }
                foreach "a b c d" [split [lindex $2 0] .] {
                    set max [expr 300+$a][expr 300+$b][expr 300+$c][expr 300+$d]
                }
                if { $V >= $min && $V <= $max } {
                    close $fin
                    return "[lindex $5 0] ([lindex $6 0])"
                }
            }
        }
        close $fin
        return "?? (unknow)"
    }

rmax - 24 Sep 2004. This variant uses a modified file with fixed line length and makes a binary search over it. A conversion proc for the csv file is also included.

 proc convert {} {
    set fd [open GeoIPCountryWhois.csv]
    set data [split [read -nonewline $fd] \n]
    close $fd
    foreach line [string map {\" ""} $data] {
        foreach {a b c d e f} [split $line ,] break
        puts [format "%ld % %s" [expr {$d - $c + 1}] $c $d $e ]

    }
 }

 proc main {ip} {
    foreach {a b c d} [split $ip .] break
    set find [expr {$a<<24 | $b<<16 | $c<<8 | $d}]
    set fd [open bar.ssv]
    set size [file size bar.ssv]
    set lines [expr {$size/21}]
    set a 0; set b $lines; set found 0
    while {!$found && $a != $b} {
        set point [expr {($b+$a)/2}]
        seek $fd [expr {$point * 21}]
        foreach {start end country} [gets $fd] break
        if "$find >= 0x$start" {
            if "$find <= 0x$end" {
                puts $ip $country
                set found 1
            } else {
                set a $point
            }
        } else {
            set b $point
        }
    }
 }

 foreach ip $argv {
    puts [time {main $ip}]
 }