Tuesday, April 28, 2009

Java: IP Analyzer Update #2

Previously this program relied upon a brute strength method of comparing a given IP with a list of previously provided IPs. Some of these IPs were classes and large ranges and were converted to a longer list of single IPs before the comparison took place. As one can imagine this is not the ideal way to go about the process as it can take minutes to compare a Class B range and Class C ranges actually crash the program itself.

Recently it became necessary to add a Class B range to the list of IPs to be compared to, so a solution to this problem was needed to allow the program to remain viable. So the entire block of code regarding comparison of IPs was rewritten, with very little of it being reused. Comparison of Ips would be done in steps where each octet was compared to each other rather than the entire IP at once. The input and list IPs were split up into arrays of integers, one slot for each octet of the IP. For those that were classes or ranges there was one array for the beginning of the IP range and one array for the end.

Once the IPs were stored in this manner their first octets were compared and if the given IP range fell within the list's IP range then a boolean variable was set to true, signifying that a match was found.

if(beginUserIP[0] > endCompareIP[0] || endUserIP[0] <>
match1 = false;
else
match1 = true;

This was done for all four octets. Then if all of the variable were true the given IP matched, if not then it did not match the list IP range and the next IP range on the list was taken and the process repeated all over again.

As it turns out this process only requires at most a second to run through the entire restricted IP list, and this includes having some Class A ranges in the list. Also helps that this reduced the amount of code in the program by several dozen lines, considering how small the program is that is rather nice. I don't see any additional changes that need to be made to this program, it is very basic and now all of the bugs are worked out of it so no new updates should be forthcoming.

No comments: