Added standard method for matching partial and *'d IPs (fuzzy ip matching).

This commit is contained in:
Steven Lawson
2012-11-22 21:32:55 -05:00
parent ed82272f5c
commit 502b055265
3 changed files with 90 additions and 73 deletions

View File

@ -888,7 +888,6 @@ public class TFM_Util
}
}
}
public static String DATE_STORAGE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
public static String dateToString(Date date)
@ -913,13 +912,55 @@ public class TFM_Util
return restricted_senders.contains(sender_name.toLowerCase());
}
public static List<String> removeDuplicates(List<String> list)
public static List<String> removeDuplicates(List<String> old_list)
{
HashSet<String> hash = new HashSet<String>();
hash.addAll(list);
list.clear();
list.addAll(hash);
return list;
List<String> new_list = new ArrayList<String>();
for (String entry : old_list)
{
if (!new_list.contains(entry))
{
new_list.add(entry);
}
}
return new_list;
}
public static boolean fuzzyIpMatch(String a, String b, int required_octets)
{
boolean is_match = true;
String[] a_parts = StringUtils.split(a, '.');
String[] b_parts = StringUtils.split(b, '.');
if (a_parts.length != 4 || b_parts.length != 4)
{
return false;
}
if (required_octets > 4)
{
required_octets = 4;
}
else if (required_octets < 1)
{
required_octets = 1;
}
for (int i = 0; i < required_octets && i < 4; i++)
{
if (a_parts[i].equals("*") || b_parts[i].equals("*"))
{
continue;
}
if (!a_parts[i].equals(b_parts[i]))
{
is_match = false;
break;
}
}
return is_match;
}
// I wrote all this before i discovered getTargetBlock >.> - might come in handy some day...
// public static final double LOOKAT_VIEW_HEIGHT = 1.65;