[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


Messages In This Thread

Forum Jump:


Users browsing this thread: 3 Guest(s)