Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: IP range

  1. #1
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts

    IP range

    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

  2. #2
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    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)
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  3. #3
    Private
    Join Date
    Jun 2013
    Posts
    70
    Thanks
    20
    Thanked 32 Times in 26 Posts
    hi
    u can split the ips with strtok and then compare it like this

    PHP Code:
    IsOk(testIp,ranges,index)
    {
        
    strtok(ranges,".");
        
    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); 

  4. The Following User Says Thank You to vanfreddy For This Useful Post:

    Ni3ls (8th October 2015)

  5. #4
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    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(
    0level.banned_ranges.sizei++)
        {
            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(
    0ip_ranges.sizei++)
        {
            
    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(
    0ip.sizei++)
        {
            if(
    ip[i] != ".")
                
    ip_parts[part] += ip[i];
            else
            {
                
    part++;
                
    ip_parts[part] = "";
            }
        }
        
    multi 1;
        
    num 0;
        for(
    ip_parts.size 1>= 0i--)
        {
            
    num += int(ip_parts[i]) * multi;
            
    multi *= 256;
        }
        return 
    num;

    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  6. The Following User Says Thank You to IzNoGoD For This Useful Post:

    Ni3ls (8th October 2015)

  7. #5
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    How does this script know which IP's are banned? I save them in scriptdata/bans and then ip.cfg

  8. #6
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    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);

    10characters
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  9. #7
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    So i have to setup all bans manually? I cant use the ip's stored in a scriptdata folder?

  10. #8
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    Just read the damn scriptdata files INTO that array....
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  11. #9
    Brigadier General
    Join Date
    Dec 2012
    Posts
    1,012
    Thanks
    440
    Thanked 171 Times in 132 Posts
    How can I do that? There are all seperated files with the name as IP.cfg

  12. #10
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    listdir() maybe?

    Libcod functions...
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •