[Mimedefang] contribution for mimedefang-filter: is_foreign()

James Ralston qralston+ml.mimedefang at andrew.cmu.edu
Sun Aug 10 04:00:01 EDT 2003


David, would you consider putting this function into the default
mimedefang-filter script that ships with MIMEDefang?

Not many people seem to know about the inet_aton() and address
masking, so code like "if ($RelayAddr =~ /^127\.0\.0\./) then blah"
keep cropping up.  Using inet_aton() to performing masking is not only
more efficient, but it should work with IPv6 addresses with little
effort.

-- 
James Ralston, Information Technology
Software Engineering Institute
Carnegie Mellon University, Pittsburgh, PA, USA

###############################################################################
# %PROCEDURE: is_foreign
# %ARGUMENTS:
#  hostip -- an IP address in numbers-and-dots notation (e.g. "127.0.0.1")
# %RETURNS:
#  1 if the IP address is foreign; 0 if it is local
# %DESCRIPTION:
#  The is_foreign function tests to see whether the supplied IP address is a
#  "foreign" (non-local) address.  The results of this function can be used to
#  make determinations whether to invoke spam checking, virus scanners, et. al.
# 

sub is_foreign ($) {

  use Socket;

  my ($hostip) = @_;
  my $addr = '';
  my $network_string = '';
  my $mask_string = '';

  # Add your local subnets to the local_subnets associative array.
  # Follow the example for the loopback network.

  my %local_subnets = (
      '127.0.0.0',      '255.0.0.0',            # loopback
  );

  $addr = inet_aton $hostip;

  while (($network_string, $mask_string) = each %local_subnets) {
    my $network = inet_aton $network_string;
    my $mask = inet_aton $mask_string;
    if (($addr & $mask) eq $network) {
      return 0;
    }
  }

  return 1;

}




More information about the MIMEDefang mailing list