[Include] CIDR - Check if an IP is in a CIDR block
#1

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
Reply
#2

Very nice !
Reply
#3

2nd cool
Reply
#4

Ah, nice job. Kaisersource released a bunch of CIDR ranges that didn't have a practical use up until now.
Reply
#5

nice
Reply
#6

okay this is wonderfull gj
Reply
#7

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?
Reply
#8

- REMOVED
Sry for double post, didnt mean to do it.
Reply
#9

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.
Reply
#10

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!
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)