69 |
} |
} |
70 |
} |
} |
71 |
} |
} |
72 |
return 1; |
return 0; |
73 |
} |
} |
74 |
|
|
75 |
sub match_host ($$$) { |
sub match_host ($$$) { |
97 |
$addr =~ /([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/; |
$addr =~ /([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/; |
98 |
$addr = pack 'C4', $1, $2, $3, $4; |
$addr = pack 'C4', $1, $2, $3, $4; |
99 |
} |
} |
100 |
|
my $mask = pack 'C4', 255, 255, 255, 255; |
101 |
if (length ($pattern) != 4) { |
if (length ($pattern) != 4) { |
102 |
$pattern =~ m!([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)(?:/([0-9]+))?!; |
$pattern =~ m!([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)(?:/([0-9]+))?!; |
103 |
$pattern = pack 'C4', $1, $2, $3, $4; |
$pattern = pack 'C4', $1, $2, $3, $4; |
104 |
my $m = $5; $m %= 33; |
my $m = $5; $m = 32 if $m > 32; |
105 |
my $mask = pack 'C4', (($m > 24) ? (2**($m-24)-1, 255, 255, 255) : |
if (defined $m) { |
106 |
($m > 16) ? (0, 2**($m-16)-1, 255, 255) : |
$mask = pack 'C4', (($m > 24) ? (255, 255, 255, (2**($m-24)-1) << (32-$m)) : |
107 |
($m > 8) ? (0, 0, 2**($m-8)-1, 255) : |
($m > 16) ? (255, 255, (2**($m-16)-1) << (24-$m), 0) : |
108 |
(0, 0, 0, 2**$m)); |
($m > 8) ? (255, (2**($m-8)-1) << (16-$m), 0, 0) : |
109 |
$pattern |= $mask; |
((2**$m-1) << (8-$m), 0, 0, 0)); |
110 |
|
} |
111 |
|
$pattern &= $mask; |
112 |
|
#printf '[%vd] %vd (%s) %vd (%vd) %d', $mask, $pattern, $_[1], ($addr & $mask), $addr, (($addr & $mask) eq $pattern); |
113 |
} |
} |
114 |
return (($addr & $pattern) eq $addr) ? 1 : 0; |
return (($addr & $mask) eq $pattern) ? 1 : 0; |
115 |
} |
} |
116 |
|
|
117 |
## TODO: IPv6 support |
## TODO: IPv6 support |