[Mimedefang] RE: What to do w/ SPAM? (ip address regex)

Matthew.van.Eerde at hbinc.com Matthew.van.Eerde at hbinc.com
Mon Sep 23 19:23:01 EDT 2002


Here you go:
use strict;

# call as
# if (address_in_network($test_ip, '10.0.0.0/24')
sub address_in_network($$);


sub address_in_network($$)
{	my $test_ip = shift;
	my $network = shift;
	my $network_start_byte;
	my $test_ip_byte;
	my $relevant_bits;
	my $in_network;

	$network =~
m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/(\d{1,2})$/
		or die "invalid network address: $network, died";

	$network_start_byte =
			$1 * 256 * 256 * 256 +
			$2 * 256 * 256 +
			$3 * 256 +
			$4;
	$relevant_bits = $5;

	$test_ip =~ m/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
		or die "invalid test IP address: $test_ip, died";

	$test_ip_byte = 
		$1 * 256 * 256 * 256 +
		$2 * 256 * 256 +
		$3 * 256 +
		$4;

	$in_network =
		(	($test_ip_byte >> (32 - $relevant_bits)) ==
			($network_start_byte >> (32 - $relevant_bits))
		);

	return $in_network;
}

> -----Original Message-----
> From: Ashley M. Kirchner [mailto:ashley at pcraft.com]
> Sent: Monday, September 23, 2002 13:02
> To: mimedefang at lists.roaringpenguin.com
> Subject: Re: [Mimedefang] RE: What to do w/ SPAM? (ip address regex)
> 
> 
> "David F. Skoll" wrote:
> 
> > Hovever, it might be worth adding some Perl functions which check if
> > an address is within a network.  For example:
> >
> >    if (address_in_network($RelayAddr, "192.168.128.0/25")) {
> >         #...
> >    }
> >
> > could be a useful thing to do.  (Actually, I bet there's a 
> CPAN module
> > out there that already does this. :-))
> 
>     This would be usefull, ya!
> 
> --
> W | I haven't lost my mind; it's backed up on tape somewhere.
>   
> +--------------------------------------------------------------------
>   Ashley M. Kirchner <mailto:ashley at pcraft.com>   .   
> 303.442.6410 x130
>   IT Director / SysAdmin / WebSmith             .     
> 800.441.3873 x130
>   Photo Craft Laboratories, Inc.            .     3550 
> Arapahoe Ave. #6
>   http://www.pcraft.com ..... .  .    .       Boulder, CO 
> 80303, U.S.A.
> 
> 
> 
> _______________________________________________
> MIMEDefang mailing list
> MIMEDefang at lists.roaringpenguin.com
> http://lists.roaringpenguin.com/mailman/listinfo/mimedefang
> 



More information about the MIMEDefang mailing list