[Asterisk-Dev] Feature request: auto-guessing NAT status based on From: IP address

Tilghman Lesher tilghman at mail.jeffandtilghman.com
Wed Apr 16 20:46:00 MST 2003


On Wednesday 16 April 2003 20:14, Eric Wieling wrote:
> Why not let you specify a netblock for nat or nonat.  i.e.
> nat=172.16.0.0/255.255.240.0 or
> nonat=63.173.166.0/255.255.255.0 and then make natdefault=nat
> or natdefault=nonat
>
> Seems to me like a more flexable system than guessing if we
> should nat or not.

I'd go with CIDR blocks instead.  Here's a quick set of code
which can be whipped into C easily (the logic is accurate):

-Tilghman

-----Cut here-----
#!/usr/bin/perl

$address = shift;

foreach ( "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16" ) {
    ($range,$bits) = split '/';

    if ((inet_aton_with_mask($address,$bits) ^ 
inet_aton_with_mask($range,$bits)) == 0) {
        print "Match on $range/$bits\n";
        exit;
    }
}
print "No match\n";

sub inet_aton_with_mask {
    my ($string,$bits) = @_;
    my ($i, at address,$address,$mask);

    if (!defined($bits)) {
        $bits = 32;
    }

    @address = split /\./, $string;

    # Assemble address into integer
    $address = 0;
    for ($i=0;$i<4;$i++) {
        $address |= ((1 << ((3 - $i) * 8)) * $address[$i]);
    }

    $mask = 0;
    for ($i=0;$i<32 - $bits;$i++) {
        $mask |= (1 << $i);
    }
    $mask ^= -1;

    return( $address & $mask );
}
-----Cut here-----



More information about the asterisk-dev mailing list