Raised This Month: $ Target: $400
 0% 

[SOLVED] Avoid banning certain IPs


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 10-28-2010 , 19:13   Re: Avoid banning certain IPs
Reply With Quote #1

try this:
PHP Code:
#define WL_ENTRY_CONTENTS 2
#define IP_MAX_LEN 16

new g_whitelist[2][WL_ENTRY_CONTENTS][IP_MAX_LEN] = {
    { 
"xxx.xxx.133.xxx""255.255.255.0" },
    { 
"xxx.xxx.142.xxx""255.255.255.0" }
}
/*  white list looks like this:
new g_whitelist[ N ][WL_DEFS][IP_MAX_LEN] = {
    { ip_1, mask_1 },
    { ip_2, mask_2 },
    ...
    { ip_N, mask_N }
}
*/
// info:
// 0 in a mask_NUM means that corresponding octet in ip_NUM can have any value 0-255,
// while 255 in a mask_NUM means that it must match with ip passed as an argument to is_ip_protected

// or more in detail:
// every bit in mask set to 1 means that corrsponding bit from whitelist ip must match with input ip

public bool:is_ip_protected(const ip_str[]){
    new 
wipwmaskip ip_to_num(ip_str)
    for(new 
i=0i<sizeof g_whitelisti++){
        
wip ip_to_num(g_whitelist[i][0])
        
wmask ip_to_num(g_whitelist[i][1])
        if(
ip&wmask==wip&wmask)
            return 
true
    
}
    return 
false
}

public 
ip_to_num(const ip[]){
    new 
numbyte
    
for(new i=0ip[i]!='^0'i++){
        if(
ip[i]=='.'){
            
num = (num<<8)+byte
            byte
=0
            
continue
        }
        
byte byte*10+ip[i]-'0'
    
}
    return (
num<<8)+byte

You must replace those "xxx" on whitelist with numbers. If you have trouble understanding how to use it, then I can give you some examples.
__________________
Impossible is Nothing
Sylwester is offline
T-z3P
Senior Member
Join Date: Apr 2008
Location: Iasi, Romania
Old 10-29-2010 , 05:58   Re: Avoid banning certain IPs
Reply With Quote #2

How to use this ?
__________________


T-z3P is offline
Send a message via Yahoo to T-z3P Send a message via Skype™ to T-z3P
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 10-29-2010 , 06:57   Re: Avoid banning certain IPs
Reply With Quote #3

You need to add ip to g_whitelist if you want it to have immunity and then use in ban command:
PHP Code:
//...
new ip[16]
get_user_ip(target_idip151)

if(
is_ip_protected(ip)){
    
// do NOT ban (ip has immunity)
}else{
    
// BAN

Every entry on whitelist contains ip and mask (mask allows you to define range):
PHP Code:
new g_whitelist][WL_DEFS][IP_MAX_LEN] = {
    { 
ip_1mask_1 },  //entry 1
    
ip_2mask_2 },  //entry 2
    
...
    { 
ip_Nmask_N }   //last entry number N

Simple examples of ip and mask:
ip aaa.bbb.ccc.ddd, mask 255.255.255.255 protects only ip aaa.bbb.ccc.ddd
ip aaa.bbb.ccc.ddd, mask 255.255.255.0 protects ip range aaa.bbb.ccc.0 - aaa.bbb.ccc.255
ip aaa.bbb.ccc.ddd, mask 255.255.0.255 protects ip range aaa.bbb.0.ddd - aaa.bbb.255.ddd
ip aaa.bbb.ccc.ddd, mask 255.0.255.255 protects ip range aaa.0.ccc.ddd - aaa.255.ccc.ddd
ip aaa.bbb.ccc.ddd, mask 255.0.0.0 protects ip range aaa.0.0.0 - aaa.255.255.255

You can use ranges other than 0-255:
ip aaa.bbb.ccc.0, mask 255.255.255.128 protects ip range aaa.bbb.ccc.0 - aaa.bbb.ccc.127
ip aaa.bbb.ccc.128, mask 255.255.255.128 protects ip range aaa.bbb.ccc.128 - aaa.bbb.ccc.255
ip aaa.bbb.ccc.64, mask 255.255.255.192 protects ip range aaa.bbb.ccc.64 - aaa.bbb.ccc.127

This gives you a lot of possibilities, but you need to have some knowledge about binary numeral system to be able to use ranges other than 0-255. You can edit the code and make it load whitelist from file, nvault, sql etc. You can cache whitelist ip and mask converted to number to make it faster.
__________________
Impossible is Nothing
Sylwester is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-29-2010 , 13:47   Re: Avoid banning certain IPs
Reply With Quote #4

Ooo, that's super nifty. Now, I'm trying to visualize how the 'other' ip ranges work. I might get there .

EDIT:

If you were to just limit yourself to the first set of examples, it would be possible to represent the IP and the mask in a single IP such as 123.139.200.*, right?
__________________

Last edited by fysiks; 10-29-2010 at 14:12.
fysiks is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 10-29-2010 , 14:38   Re: Avoid banning certain IPs
Reply With Quote #5

Yeah, that's right.
__________________
Impossible is Nothing
Sylwester is offline
T-z3P
Senior Member
Join Date: Apr 2008
Location: Iasi, Romania
Old 10-29-2010 , 15:41   Re: Avoid banning certain IPs
Reply With Quote #6

Thank you for reply . Testing .

LE : It's working flawless . Thanks .
__________________



Last edited by T-z3P; 10-29-2010 at 16:56.
T-z3P is offline
Send a message via Yahoo to T-z3P Send a message via Skype™ to T-z3P
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 00:19.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode