summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/tweaker/bin/LocalIPCheck.pl
blob: b744fb6c20df9df34a232d4ccb3596e48ca75050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/perl

# Valid private IP ranges

my @LOCAL_IP_RANGES = ("10.0.0.0", "10.255.255.255",
		       "172.16.0.0", "172.31.255.255",
		       "192.168.0.0", "192.168.255.255");

# input: A dotted quad IP address.

# output: 0 if a public (internet) address
#       : 8 if a class A private (LAN) address
#       : 12 if a class B private (LAN) address
#       : 16 if a class C private (LAN) address

sub get_IP_number () {
    my ($dotted_quad) = @_;
    my $IP_number=0;
    
    split(/\./, $dotted_quad);

    for (my $i=0; $i < 4; $i++) {
	$IP_number=$IP_number+@_[3-$i]*(2**(8*$i));
    }
    return $IP_number;
}

while(<>) {
    chop;
    my $IPnumber=&get_IP_number($_);
    my $class=16;

    while (@LOCAL_IP_RANGES) {
	my $highIPnumber = &get_IP_number(pop(@LOCAL_IP_RANGES));
	my $lowIPnumber = &get_IP_number(pop(@LOCAL_IP_RANGES));

	if (($lowIPnumber <= $IPnumber) && ($highIPnumber >= $IPnumber)) {
	    exit($class); # PRIVATE IP
	} else {
	    $class += 4;
	}
    }
}

exit(0); # PUBLIC IP