chiark / gitweb /
Default "overlapping" to relevant ones when you view a record or pick a network.
[bcp5-registry.git] / networks.pl
1 # Network/range syntax and semantics handling.
2 #
3 # Copyright (C) 1999 Ian Jackson <ijackson@chiark.greenend.org.uk>
4 #
5 # This is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2,
8 # or (at your option) any later version.
9 #
10 # This is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public
16 # License along with this file; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 sub parse_netrange ($) {
20     my ($net) = @_;
21     my ($prefix,$network,@b,$b,$val,$mask);
22
23     length $net or finish_error('nonet');
24 print DEBUG "got $net\n";
25     $net =~ s,/(\d+)$,, or finish_error("badnet");
26 print DEBUG "prefix $1\n";
27     $prefix= $1+0;  ($prefix >= 0 && $prefix <= 32) or finish_error("badnet");
28 print DEBUG "prefix $1 $net\n";
29     $network= ''; @b= split(/\./,$net);
30     @b<=4 or finish_error("badnet");
31 print DEBUG "big enough\n";
32     @b*8 >= $prefix or finish_error("badnet");
33     while (@b<4) { push @b,0; }
34 print DEBUG "@b\n";
35     foreach $b (@b) {
36         $b>=0 && $b<=255 or finish_error("badnet");
37         $network .= sprintf("%02x",$b);
38     }
39     ($val,$mask) = net_valuemask($network,$prefix);
40 printf DEBUG "%08x %08x %08x\n", $val,$mask,~$mask;
41     !($val & ~$mask) or finish_error("badnet");
42 print DEBUG "ok\n";
43     return ($network,$prefix,$val,$mask);
44 }
45
46 sub net_subset ($$$$) {
47     my ($smln,$smlp, $bign,$bigp) = @_;
48     return 0 unless $smlp >= $bigp;
49     ($bigv,$bigm) = net_valuemask($bign,$bigp);
50     ($smlv,$smlm) = net_valuemask($bign,$bigp);
51     return 0 unless ($smlv & $bigm) == $bigv;
52     return 1;
53 }
54
55 sub get_mask ($) {
56     my ($prefix) = @_;
57     my ($m, $sh);
58     $m= 0xffffffff;
59     $sh= 32-$prefix;
60     $sh2= 0;
61     if ($sh>=16) { $sh2 += 16; $sh -= 16; }
62     $m <<= $sh;
63     $m <<= $sh2;
64     return $m;
65 };
66
67 sub net_valuemask ($$) {
68     my ($network,$prefix) = @_;
69     return (hex($network), get_mask($prefix));
70 }
71
72 sub display_net ($$) {
73     my ($network,$prefix) = @_;
74     return join('.', unpack("C4",pack("H8",$network)))."/".$prefix;
75 }
76
77 1;