Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Offline algorithm to turn an IP4 address into a country name

  1. #1
    Very good friend of the forum Virchanza's Avatar
    Join Date
    Jan 2010
    Posts
    863

    Default Offline algorithm to turn an IP4 address into a country name

    I'm currently writing a program that will have no access to the internet.

    I need to be able to turn IP4 addresses into country names. (It doesn't have to be 100% accurate). So the code would be something like:

    Code:
    if ( 62 == ip_address.first_octet )
        return "Ireland";
    
    else if ( 41 == ip_address.first_octet )
        return "Nigeria";
    
    else if ( 158 == ip_address.first_octet )
        return "Chile";
    
    else
        return 0;
    Does anyone have such code available? I'd really appreciate if you could post it here (it can be in any programming language at all).
    Ask questions on the open forums, that way everybody benefits from the solution, and everybody can be corrected when they make mistakes. Don't send me private messages asking questions that should be asked on the open forums, I won't respond. I decline all "Friend Requests".

  2. #2
    Senior Member
    Join Date
    Jul 2011
    Posts
    236

    Default Re: Offline algorithm to turn an IP4 address into a country name

    Do you have a list of all the country codes? I could find them I'm sure online, but too lazy for such things. You give me a list and ill write ya something in bash
    V/r,
    Snafu
    Pffbt..[quote]I made a discovery today. I found a computer. Wait a second, this is cool. It does what I want it to. If it makes a mistake, it's because I screwed it up. Not because it doesn't like me... Or feels threatened by me.. Or thinks I'm a smart ass.. [/quote]

  3. #3
    Very good friend of the forum Virchanza's Avatar
    Join Date
    Jan 2010
    Posts
    863

    Default Re: Offline algorithm to turn an IP4 address into a country name

    It's grand, I've got all the data I need, now I just have to parse it.

    I'll post it here when I've got it done.
    Ask questions on the open forums, that way everybody benefits from the solution, and everybody can be corrected when they make mistakes. Don't send me private messages asking questions that should be asked on the open forums, I won't respond. I decline all "Friend Requests".

  4. #4
    Member shadowzero's Avatar
    Join Date
    Jun 2011
    Location
    ${HOME}
    Posts
    94

    Default Re: Offline algorithm to turn an IP4 address into a country name


  5. #5
    Very good friend of the forum Virchanza's Avatar
    Join Date
    Jan 2010
    Posts
    863

    Default Re: Offline algorithm to turn an IP4 address into a country name

    Here's the C code. It turns 32-Bit IP address numbers into a two-letter country code:

    http://virjacode.com/ip_address_c_code.txt
    Last edited by Virchanza; 10-30-2011 at 11:20 PM.

  6. #6
    Very good friend of the forum Gitsnik's Avatar
    Join Date
    Jan 2010
    Location
    The Crystal Wind
    Posts
    851

    Default Re: Offline algorithm to turn an IP4 address into a country name

    Sorry Virchanza I only just saw this. I am the first to admit this isn't running from my BT system, but it might make things easier for you
    Code:
    # geoiplookup 173.245.60.120 | awk -F:\  '{print $2}' | sed 's/,.*//'
    It used to be that I would download a copy of the lookup tables daily from a particular site (via a crontab), but it looks like the latest version has a program built to let you update it when you are online anyway.

    You have the benefit then of looking up the information from a data file, which is updateable and modifiable, AND you should have a lot less code than a sequence of if statements like that. Would save you a lot of time.

    For those following along at home, there are even plugins to Apache, lighthttpd, pecl, rubnet and ruby gems for GeoIP lookups. I used to use it to plot IP addresses to approximate GPS coordinates on a world map (a nifty way of using an IRC bot really, nothing more).
    Still not underestimating the power...

    There is no such thing as bad information - There is truth in the data, so you sift it all, even the crap stuff.

  7. #7
    Very good friend of the forum Virchanza's Avatar
    Join Date
    Jan 2010
    Posts
    863

    Default Re: Offline algorithm to turn an IP4 address into a country name

    Is anyone here handy with Java and Eclipse? (I don't have much experience with them at all, I stick to C and C++).

    I have to do an Interactive Media Design project for college, and the lecturer is using a Java library so I have to try put something together in Java.

    So I took my C code and tried to turn it into Java code. Here's the contents of "IP_Converter.java":

    http://virjacode.com/IP_Converter.txt (This file is 1.8 megabytes)

    When I try to build this in Eclipse, I get:

    Too many constants, the constant pool for IP_Converter would exceed 65536 entries IP_Converter.java /processing_tesing/src line 2 Java Problem


    Is there anything I can do to get around this?
    Ask questions on the open forums, that way everybody benefits from the solution, and everybody can be corrected when they make mistakes. Don't send me private messages asking questions that should be asked on the open forums, I won't respond. I decline all "Friend Requests".

  8. #8
    Very good friend of the forum Virchanza's Avatar
    Join Date
    Jan 2010
    Posts
    863

    Default Re: Offline algorithm to turn an IP4 address into a country name

    OK I've decided to go with a different strategy. Instead of having a boat load of IF statements, I'm gonna have a class that loads a CSV file into a vector.

    Anyway, first of all, here's the format of the CSV file:

    Code:
    Two-letter country code,starting ip,ending ip
    So here's the first few entries of the CSV file:
    Code:
    ad,3265150976,3265159167
    ad,1432264704,1432272895
    ad,1538998272,1539006463
    ae,3265921024,3265986559
    ae,3286564864,3286630399
    ae,3561742336,3561750527
    ae,3582205952,3582214143
    ae,3576299520,3576365055
    ae,3651403776,3651534847
    ae,1357053952,1357119487
    Now I actually have to code this in Java..... but since I'm shite at Java I decided to write it in C++ first, get it working, and then see about bending and squeezing it into Java.
    Anyway here's what I've got:

    Code:
    #include <fstream>   // ifstream
    #include <vector>    // vector
    #include <string>    // string
    #include <cstdlib>   // strtoul
    #include <iostream>  // ostream
    #include <stdint.h>  // uint_fast32_t
    
    struct RangeEntry {
    
        uint_fast32_t a, z;
    
        char cty_code[3];
    
    };
    
    class IP_Tracer {
    
    protected:
    
        std::ifstream csv_file;
    
        typedef std::vector<RangeEntry> ContainerType;
        
        ContainerType ranges;
    
    public:
    
        IP_Tracer(char const *const arg_filename) : csv_file(arg_filename)
        {
            if ( !csv_file.is_open() )
                throw -1;
    
            std::string str;
    
            while ( std::getline(csv_file,str) )
            {
                if ( str.length() < 18 )  // Start off with aa,1.1.1.1,1.1.1.1
                    continue;
    
                RangeEntry re;
    
                char const *const p = str.c_str();
    
                re.cty_code[0] = p[0];
                re.cty_code[1] = p[1];
                re.cty_code[2] = '\0';
    
                str.erase(0,3); // Leaves us with 1.1.1.1,1.1.1.1
    
                std::string const &a = str.substr(  0, str.find_first_of(',')  );
                
                std::string const &z = str.substr(  str.find_first_of(',') + 1  );
    
                re.a = std::strtoul( a.c_str(), 0, 10 );
     
                re.z = std::strtoul( z.c_str(), 0, 10 );
    
                ranges.push_back(re);
            }
        }
    
        void Display_All_Ranges(std::ostream &os) const
        {
            for ( ContainerType::const_iterator p = ranges.begin(); p != ranges.end(); ++p )
            {
                os << p->cty_code << " / " << p->a << " / " << p->z << '\n';
            }        
        }
    
        char const *IP_to_Country( uint_fast32_t const ip ) const
        {
            // Return value is valid until IP_Tracer object is destroyed
    
            for ( ContainerType::const_iterator p = ranges.begin(); p != ranges.end(); ++p )
            {
                if ( ip >= p->a && ip <= p->z )
                    return p->cty_code;
            }
    
            return 0;
        }
    };
    Seeing as how my CSV file is 1.8 MB, containing over 28 thousand lines, I thought there would be a delay of a few seconds when I create an object of this class.... but funnily enough it's pretty much instantaneous on my laptop.

    Now I just have to try turn the above class into Java code I tried playing around with Eclipse, but when I hit Build, it seems like it's actually running the program. It's giving me errors about unhandled exceptions...... so either it's running the program and exceptions are being thrown...... or the Java compiler is telling me that every exception needs to be caught in Java...... I dunno I'm not a Java guy.
    Last edited by Virchanza; 10-31-2011 at 05:07 PM.
    Ask questions on the open forums, that way everybody benefits from the solution, and everybody can be corrected when they make mistakes. Don't send me private messages asking questions that should be asked on the open forums, I won't respond. I decline all "Friend Requests".

  9. #9
    Very good friend of the forum Virchanza's Avatar
    Join Date
    Jan 2010
    Posts
    863

    Default Re: Offline algorithm to turn an IP4 address into a country name

    OK I'm trying to turn the above C++ code in Java code. Could someone give me a hand please? I'm hopeless at Java. Here's what I have so far:

    Code:
    import java.io.*;
    import java.util.*;
    
    class RangeEntry {
    
        long a, z;
    
        String cty_code;
    
    };
    
    public class IP_Tracer {
    
        protected Vector<RangeEntry> ranges;
    
        public IP_Tracer(String arg_filename)
        {
            FileInputStream fis;
            DataInputStream dis;
            BufferedReader br;
            
            fis = new FileInputStream(arg_filename);
            
            dis = new DataInputStream(fis);
            
            br = new BufferedReader( new InputStreamReader(dis) );
            
            String str;
    
            while ( (str = br.readLine()) != null )
            {
                if ( str.length() < 18 )  // Start off with aa,1.1.1.1,1.1.1.1
                    continue;
    
                RangeEntry re = new RangeEntry();
    
                re.cty_code = str.substring(0,1);
                
                str = str.substring(3); // Leaves us with 1.1.1.1,1.1.1.1
    
                String a = str.substring(  0, str.indexOf(',') - 1 );
                
                String z = str.substring(  str.indexOf(',') + 1  );
    
                re.a = Long.valueOf(a);
     
                re.z = Long.valueOf(z);
                
                ranges.add(re);
            }
        }
    
        String IP_to_Country( long ip )
        {
            for ( RangeEntry re : ranges )
            {
                if ( ip >= re.a && ip <= re.z )
                    return re.cty_code;
            }
    
            return null;
        }
    };
    Is that looking alright?

    When I hit Build, it gave me:

    Code:
    Description    Resource    Path    Location    Type
    Unhandled exception type FileNotFoundException    IP_Tracer.java    /src    line 22    Java Problem
    Unhandled exception type IOException    IP_Tracer.java    /src    line 30    Java Problem
    Why is it moaning about unhandled exceptions? Do you need to have a try-catch block for every exception in Java?!
    Ask questions on the open forums, that way everybody benefits from the solution, and everybody can be corrected when they make mistakes. Don't send me private messages asking questions that should be asked on the open forums, I won't respond. I decline all "Friend Requests".

  10. #10
    Very good friend of the forum Gitsnik's Avatar
    Join Date
    Jan 2010
    Location
    The Crystal Wind
    Posts
    851

    Default Re: Offline algorithm to turn an IP4 address into a country name

    Quote Originally Posted by Virchanza View Post
    Why is it moaning about unhandled exceptions? Do you need to have a try-catch block for every exception in Java?!
    Just around the statements on line 22 and 30.

    I usually cheat (though it's been a while):
    Code:
    try {
    blah
    } catch exception (e) {
    meh
    }
    Saves having to argue with IOExceptions and all the rest. You'll have to look up the proper format, it has been a few years.
    Still not underestimating the power...

    There is no such thing as bad information - There is truth in the data, so you sift it all, even the crap stuff.

Page 1 of 2 12 LastLast

Similar Threads

  1. Crypto algorithm
    By digitalfriction in forum BackTrack 5 General Topics
    Replies: 2
    Last Post: 05-13-2011, 01:30 PM
  2. Gateway Offline
    By Maverikc in forum Beginners Forum
    Replies: 1
    Last Post: 05-26-2010, 09:43 AM
  3. Sky router WPA algorithm available
    By letmein in forum OLD General IT Discussion
    Replies: 21
    Last Post: 12-14-2009, 06:29 PM
  4. Algorithm Cracker\Imitator
    By Ouassar in forum OLD Specialist Topics
    Replies: 0
    Last Post: 11-30-2009, 01:48 PM
  5. WPA-PSK Algorithm question
    By kotarel in forum OLD Wireless
    Replies: 0
    Last Post: 11-15-2009, 08:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •