Use it like this to ban ranges:
PHP Code:
init() //setup ip range bans
{
banned = [];
banned[banned.size] = "1.2.0.0/16"; //a /16 ban bans any ip that matches the first 16 bits of this address, so it effectively bans 65536 ips (1.2.0.0 - 1.2.255.255)
banned[banned.size] = "3.4.5.0/24"; //a /24 ban bans any ip that matches the first 24 bits of this address, so it effectively bans 256 ips (3.4.5.0 - 3.4.5.255)
banned[banned.size] = "6.7.8.9"; //an ip without range is the same as a /32 ban, matching the first 32 bits, which is the entire address. This only bans the ip in question (6.7.8.9)
level.banned_ranges = createbans(banned);
}
isbanned()
{
ip = self getip();
ip_int = parse_ip(ip);
for(i = 0; i < level.banned_ranges.size; i++)
{
if((ip_int>>(32 - level.banned_ranges[i].range)) == (level.banned_ranges[i].ip>>(32 - level.banned_ranges[i].range)) //bitshift ftw, compare the first N bits of the ip with the first N bits of the banned range
return true;
}
return false;
}
createbans(ip_ranges)
{
result = [];
for(i = 0; i < ip_ranges.size; i++)
{
split = strtok(ip_ranges[i], "/");
if(!split.size || split.size > 2)
return; //error occured parsing the ips
if(split.size == 1)
range = 32;
else
range = int(split[1]);
ip = parse_ip(split[0]);
result[result.size] = spawnstruct();
result[result.size - 1].ip = ip;
result[result.size - 1].range = range;
}
return result;
}
parse_ip(ip)
{
ip_parts = [];
part = 0;
ip_parts[part] = "";
prev_found = 0;
for(i = 0; i < ip.size; i++)
{
if(ip[i] != ".")
ip_parts[part] += ip[i];
else
{
part++;
ip_parts[part] = "";
}
}
multi = 1;
num = 0;
for(i = ip_parts.size - 1; i >= 0; i--)
{
num += int(ip_parts[i]) * multi;
multi *= 256;
}
return num;
}