Question

I have a database that contains a range of IP-addresses and the location of this range. This Range is given as IPs without dots e.g. 25946112 - 25946367 for QUEENSLAND AU.

Now I want to compare some IPs with this database and get the location of them. The IPs I have are in dotted-decimal-notation. My question is: Can I compare these addresses with the ranges just by removing the dots or do I have to add zeros? Like: 123.231.4.11to123231004011. But, as you can see, the database IPs do not have all 12 digits. I also tried to test the sample above (25946112), but it wasn't possible to find the location.

Thanks for your help. Thomas

Was it helpful?

Solution

One way to store IP addresses in a database is storing them as integers. I think that this is the case with your databases. You should not just replace dots and compare. Instead you should convert the IP to integer and then compare. Various RDBMs provide function to ease this operation. For example mysql provides INET_ATON

OTHER TIPS

It's not enough with dot removing, due to there're several possibilities for one IP translation.

In other words and according to your example, when you try to pass from 123.231.4.11 to 123231004011 is possible for that case, but in the opposite way, without zeros, 25946112 can be 2.59.46.112, 25.9.46.112, 25.94.6.112, 25.94.61.12.

So, you need to apply some logic such as if they are valid IP. The can be invalid of course if any octet exceeds from 255 but also if they don't belong to any IP class Subnetting IP address classes: The following four are valid IP's, so you need more geolocalization information.

2.59.46.112
25.9.46.112
25.94.6.112
25.94.61.12

Could you tell us some information for example, which octet have most IP from Queensland for example? The target is go on filtering adding conditions until you have only one possibility, conditions that you should get from other fields and tables in your database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top