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
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:
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).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;
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".
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]
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".
Found a sample actually: http://www.codeproject.com/KB/aspnet/aspxcode_net.aspx
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.
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 youIt 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.Code:# geoiplookup 173.245.60.120 | awk -F:\ '{print $2}' | sed 's/,.*//'
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.
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".
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:
So here's the first few entries of the CSV file:Code:Two-letter country code,starting ip,ending ip
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.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
Anyway here's what I've got:
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.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; } };
Now I just have to try turn the above class into Java codeI 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".
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:
Is that looking alright?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; } };
When I hit Build, it gave me:
Why is it moaning about unhandled exceptions? Do you need to have a try-catch block for every exception in Java?!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
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".
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.