Version 5 of Basic GeoIP with Tcl

Updated 2004-09-24 13:23:41 by kroc

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)"
    }