Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Convert IP address to country name and code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Convert IP address to country name and code

    This simple little utility allows you to specify any IP address (eg. www.powerbasic.com = 66.209.38.219 = UNITED STATES (US). When you've determined the country of the IP address you can then target that person specifically. For example, if you ran this program as a CGI on your webserver, you could determine which visitors are from the US, and then only give them US-specific ads. Or, you could just keep track of demographics, to see which countries visit your site the most. Etc. etc.

    It is based on a CSV database that is freely available at http://www.ip-to-country.com
    It simply contains a list of IP ranges, their country code and country names. For example, one line is: "406388736","406437887","CA","CANADA"
    So, IPs 406388736 - 406437887 are assigned to Canada (there's a simple function in this demo to convert dotted IP addresses to DWORD addresses). As this method is based on IP ranges, it's fast (especially if the database is already loaded into memory - thats the slowest bit, but still only takes a second from a local hdd) and fairly accurate. The database is updated monthly, according to their website.

    To install, just go to http://www.ip-to-country.com and download the latest IP-to-Country database. Save it as COUNTRY.DAT in the same directory you compile this program.

    Code:
    #COMPILE EXE "ip2country.exe"
    #INCLUDE "win32api.inc"
     
    TYPE C2IPData
     dwFrom AS DWORD
     dwTo   AS DWORD
     dwCode AS STRING * 2
     dwName AS STRING * 50
    END TYPE
     
    GLOBAL C2IPinf() AS C2IPData, C2IPmax AS DWORD
     
    FUNCTION AppPath() EXPORT AS STRING
        LOCAL zTmp AS ASCIIZ * 256
        LOCAL sTmp AS STRING
        LenExeName& = GetModuleFileName(BYVAL %NULL, zTmp, SIZEOF(zTmp))
        IF LenExeName& THEN
           LenExeName& = MIN&(LenExeName&, SIZEOF(zTmp))
           sTmp = LEFT$(zTmp, LenExeName&)
           sTmp = LEFT$(sTmp,INSTR(-1,sTmp,"\"))
           FUNCTION = sTmp
        END IF
    END FUNCTION
     
    FUNCTION IPAddress(sIP AS STRING) AS QUAD
    DIM ipBit() AS INTEGER, I AS INTEGER, dwResult AS QUAD
    REDIM ipBit(4) AS INTEGER
    FOR I = 1 TO 4: ipBit(i) = VAL(PARSE$(sIP, ".", i)): NEXT
    'There are probably several ways to do the calculation, here's the manual way.
    dwResult = 0
    dwResult = dwResult + (ipBit(1) * 16777216)
    dwResult = dwResult + (ipBit(2) * 65536)
    dwResult = dwResult + (ipBit(3) * 256)
    dwResult = dwResult + (ipBit(4))
    FUNCTION = dwResult
    END FUNCTION
     
    SUB C2IP_Init()
    DIM sDat AS STRING, sMid AS STRING, i1 AS LONG, i2 AS LONG, lMax AS LONG, i AS LONG
    REDIM C2IPinf(30000) AS C2IPData  'IMPORTANT: may need to increase this
    OPEN AppPath & "\country.dat" FOR BINARY ACCESS READ LOCK SHARED AS #1
     lMax = LOF(1)
     GET$ #1, lMax, sDat
    CLOSE #1
    i1 = 1
    DIM b$(4)
    DO
     i2 = INSTR(i1, sDat, CHR$(10))
     sMid = MID$(sDat, i1, i2 - i1 - 1)
     PARSE sMid, b$()
     C2IPinf(i).dwFrom = VAL(b$(0))
     C2IPinf(i).dwTo = VAL(b$(1))
     C2IPinf(i).dwCode = b$(2)
     C2IPinf(i).dwName = b$(3)
     i1 = i2 + 1
     IF i1 => lMax THEN EXIT DO
     i = i + 1
    LOOP
    C2IPmax = i
    END SUB
     
    SUB ShowIPinfo(sIP AS STRING)
    DIM iIP AS QUAD, I AS DWORD
    iIP = IPAddress(sIP)
    'STDOUT "Your IP = " & STR$(iIP)
    FOR I = 0 TO C2IPmax
     IF iIP => C2IPInf(i).dwFrom AND iIP <= C2IPInf(i).dwTo THEN
         STDOUT "Country = " & C2IPinf(i).dwCode & " (" & RTRIM$(C2IPinf(i).dwName) & ")"
         EXIT SUB
     END IF
    NEXT I
    STDOUT "Unable to identify country from this IP."
    END SUB
     
    FUNCTION PBMAIN() AS LONG
    ON ERROR RESUME NEXT
    DIM sIP AS STRING
    C2IP_Init  'load database
    STDOUT "IP Address: ";
    STDIN LINE sIp
    ShowIPinfo(sIP)
    STDOUT "Press any key to continue . . .";
    WAITKEY$
    END FUNCTION
    ------------------
    The PowerBASIC Crypto Archives - My Email - What's mine is yours...



    [This message has been edited by Wayne Diamond (edited May 24, 2003).]
    -

  • #2
    Wayne,

    You gotta fix this

    REDIM C2IPinf(20000) AS C2IPData

    the new file seems to be quite a bit bigger (may 2003)

    was getting GPFs till I fixed it

    ------------------
    Paul Dwyer
    Network Engineer
    Aussie in Tokyo

    Comment

    Working...
    X