CIDR - Check if an IP is in a CIDR block -
R@f - 06.06.2013
Hello everyone !
I wrote a small include which allows you to check if an IP address is in a CIDR range. It contains one function (+2 other you can use for your own projects).
What can it be used for ?- Range ban small amont of IPs.
- Allow/Block an ISP or VPN providers.
- Other uses ?
Function: cidr_check(string ip_address, string cidr_block)
Param ip_address: The IP address you want to check (ex. 10.13.37.32).
Param cidr_block: The CIDR block you want to use for the check (refer to
http://en.wikipedia.org/wiki/Classle...Domain_Routing).
Return: 0 if the IP is not in the CIDR block, 1 if it is.
Other functions
ip2long: Convert an IPv4 to an integer.
split: Splits a string.
Example
Код:
#include <a_samp>
#include <cidr>
public OnFilterScriptInit()
{
/* Message will be printed in the server console */
if(cidr_match("10.13.37.5", "10.13.37.0/24"))
print("The IP 10.13.37.5 is in the range of 10.13.37.0/24");
/* Message will be printed in the server console */
if(!cidr_match("10.13.37.5", "10.13.37.128/26"))
print("The IP 10.13.37.5 is not in the range of 10.13.37.128/26");
/* Message will NOT be printed in the server console */
if(!cidr_match("10.13.37.129", "10.13.37.128/26"))
print("The IP 10.13.37.129 is not in the range of 10.13.37.128/26");
return true;
}
Download
V1.0: http://pastebin.com/4QNzpebX
Cheers,
R@f
Re : CIDR - Check if an IP is in a CIDR block -
Hip-hop - 07.06.2013
Very nice !
Re: CIDR - Check if an IP is in a CIDR block -
SwisherSweet - 07.06.2013
2nd cool
Re: CIDR - Check if an IP is in a CIDR block -
Scenario - 07.06.2013
Ah, nice job. Kaisersource released a bunch of CIDR ranges that didn't have a practical use up until now.
Re: CIDR - Check if an IP is in a CIDR block -
Kirollos - 07.06.2013
nice
Re: CIDR - Check if an IP is in a CIDR block -
Niko_boy - 07.06.2013
okay this is wonderfull
gj
Re: CIDR - Check if an IP is in a CIDR block -
DRIFT_HUNTER - 08.06.2013
As from some fast look and reading it looks like a simple range ban :
10.13.37.0/24 will mean ip in range from: 10.13.37.0 to 10.13.37.24 will be caught in a ban. Right?
If its not like that can you please explain it a bit more?
And second question is how do we find out someone's range? The only way i know is a reverse DNS but still it will not return a range that we can ban so how do we find out that?
Re: CIDR - Check if an IP is in a CIDR block -
DRIFT_HUNTER - 08.06.2013
- REMOVED
Sry for double post, didnt mean to do it.
Re: CIDR - Check if an IP is in a CIDR block -
Lightning[SV] - 09.06.2013
Very nice. Useful for those without access to iptables.
You can calculate CIDR ranges with
http://www.subnet-calculator.com/cidr.php
@above 10.13.37.0/24 will mean and ip range from: 10.13.37.0 to 10.13.37.255
IP addresses are allocated using CIDR so you should always be able match a range to a specific isp.
If I want to ban an ip range for a specific isp, I use the geoip isp database to lookup all the ip addresses isp's near the ip I want to range ban, then I can work out the specific CIDR range for the isp.
Re: CIDR - Check if an IP is in a CIDR block -
Gammix - 17.07.2018
I am using this remastered version, i made a repo recently on GitHub:
https://github.com/Agneese-Saini/SA-...clude/cidr.inc
If anyone wanna use it!