Jump to content

IP to Country [C++]


Recommended Posts

A little while ago I decided to make an program that would trace an IP to the country it originated from. After finding a CSV file of IP Number ranges and their corresponding countries I got to work, with out much luck. After talking to TomB about it for a while, he decided to give me a pretty in-depth (at least for me) tutorial on aspects of C and C++ like linked lists and text phrasing.

The result of this tutorial is this app: Download Source Code

sorry about the lack of comments, everything I need is in chat logs. Its not too hard to work out though.

The code isn't really mine, so as far as I am concerned do as you will unless TomB says otherwise.

Link to comment
Share on other sites

I recently compiled the code in Dev-C++. Execution was fine up until EmptyLinkedList(list); which causes the error "ip2na.exe has encountered a problem and needs to close. We are sorry for the inconvenience... blah blah blah" Removing the call to EmptyLinkedList solves the problem, however this isn't really a solution. Anyway I have made two changes to the original code.

Firstly instead of prompting for an IP Address I have passed it as an argument and retrieved it from char** argv so on the command line you can do: ip2na w.x.y.z

Secondly when doing something like C:Documents and SettingsArran>G:ip2na.exe w.x.y.z it trys to open ip-to-country.txt in C:Documents and SettingsArran when its actually in G:. I have therefore added code to get the path of ip2na.exe from argv[0] and store it in char path[MAX_LENGTH] to which I strcat(path, "ip-to-country.txt").

One last thing, the size of ip-to-country.txt is quite large, one way of making it smaller would be storing the IP addresses as four bytes and not a string of digits. For example storing 33996344 as ASCII uses 8 bytes whereas it could be stored as 4 bytes as 2, 6, 190 and 56.

The modified source code can be found at http://websiteat.googlepages.com/ip2na.zip

Link to comment
Share on other sites

Thanks for adding the command line argument, that makes it a lot nicer.

I never got a problem with EmptyLinkedList() when I compiled with minGW on windows, or gcc on linux but I'll take another look. I know ip-to-country.txt is pretty big, but since I already had it in an (almost) usable format (TomB had to make some changes) I thought I would just use it.

Basically like I said, this application was made purely for learning purposes, but thanks for the changes, you taught me some stuff.

Link to comment
Share on other sites

Unfortunately, there is a buffer overflow in your code at this point:

strcpy(path, argv[0]);

It is fixed by doing:

strncpy(path, argv[0], MAX_PATH);

Yes it was my modified code that introduced the buffer overflow, anyway thanks for pointing that out.

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...
This is a cool app... sorry I can't help with the C++ but if you get a chance a link to that CSV file would be nice. I'd like to write my own app in PHP maybe.

actually I just did a quick google and found one here:


thanks anyway. keep up the good work.

Yeh, we used the CSV from that site, like I said, TomB modified it a lot, if you download the package you can see what he did. If you write one in PHP, I wouldn't mind taking a look.



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...