PDA

View Full Version : IP range



Ni3ls
7th October 2015, 14:31
Hi all,

I made an IP ban command, which saves the IP of the player in a file and when a player connects, his IP is checked against that list. However, for example, if your IP is 123.45.67.10, you can connect again if your IP is 123.45.67.11. Is it possible to make some kind of IP range that checks ur IP against 123.45.67.i with i from 0 till 150 for example?

EDIT:

Or can be a isSubStr(PlayerIP, bannedIP) used? No idea how to set that up though

IzNoGoD
7th October 2015, 15:08
I'd go with ip ranges entirely, and not just /8 /16 or /24, but anything in between.

Basically you have to convert an ip to a 32 bit integer, then compare the first /N bits to your ban. You can ban the player's entire ASN to ensure he wont be able to come back that way (ASNs might have multiple ranges, google is your friend here)

vanfreddy
7th October 2015, 20:16
hi
u can split the ips with strtok and then compare it like this


IsOk(testIp,ranges,index)
{
r = strtok(ranges,".");
t = strtok(testIp,".");
if(!isdefined(t[index-1])) return false;
if(int(t[index-1]) > int(r[index-1])) return false;
return true;
}

iprangeissmaller = IsOk("123.123.123.10","123.123.123.150",4);

IzNoGoD
7th October 2015, 22:12
Use it like this to ban ranges:


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;
}

Ni3ls
27th October 2015, 11:50
How does this script know which IP's are banned? I save them in scriptdata/bans and then ip.cfg

IzNoGoD
27th October 2015, 12:34
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);
}


10characters

Ni3ls
27th October 2015, 13:36
So i have to setup all bans manually? I cant use the ip's stored in a scriptdata folder?

IzNoGoD
27th October 2015, 15:49
Just read the damn scriptdata files INTO that array....

Ni3ls
27th October 2015, 16:09
How can I do that? There are all seperated files with the name as IP.cfg

IzNoGoD
27th October 2015, 17:59
listdir() maybe?

Libcod functions...

Ni3ls
27th October 2015, 19:15
I use this for reference
https://znation.nl/cod4script/

But I cant find that function in there. Where can I see all functions?

IzNoGoD
27th October 2015, 21:50
https://github.com/kungfooman/libcod/blob/master/gsc.cpp

line 203 and below

kung foo man
28th October 2015, 03:35
You can click on the line number to get the anker link: https://github.com/kungfooman/libcod/blob/master/gsc.cpp#L203

Ni3ls
30th October 2015, 14:48
And is there any docs how to use them?

IzNoGoD
30th October 2015, 15:59
For most: yes, look at the link you posted yourself