File: netaddr.t

package info (click to toggle)
libnetaddr-ip-perl 4.079%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 1,580 kB
  • ctags: 251
  • sloc: perl: 1,417; cpp: 67; sh: 51; makefile: 9
file content (208 lines) | stat: -rw-r--r-- 5,704 bytes parent folder | download | duplicates (5)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..35\n"; }
END {print "not ok 1\n" unless $loaded;}

#use diagnostics;
use Data::Dumper;
use NetAddr::IP::Lite;

$loaded = 1;
print "ok 1\n";
######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):

=pod

$rv=list2NetAddr(\@inlist,\@NAobject);

Build of NetAddr object structure from a list of IPv4 addresses or address
ranges. This object is passed to B<matchNetAddr> to check if a given IP
address is contained in the list.

  input:	array reference pointer
		to a list of addresses

  i.e.		11.22.33.44
		11.22.33.0/24
		11.22.33.0/255.255.255.0
		11.22.33.20-11.22.33.46
		11.22.33.20 - 11.22.33.46

  output:	Number of objects created
		or undef on error

The NAobject array is filled with NetAddr::IP::Lite object references.

=cut

sub list2NetAddr {
  my($inref,$outref) = @_;
  return undef
	unless ref $inref eq 'ARRAY'
	&& ref $outref eq 'ARRAY';
  unless ($SKIP_NetAddrIP) {
    require NetAddr::IP::Lite;
    $SKIP_NetAddrIP = 1;
  }
  @$outref = ();
  my $IP;
  no strict;
  foreach $IP (@$inref) {
    $IP =~ s/\s//g;
	# 11.22.33.44
    if ($IP =~ /^\d+\.\d+\.\d+\.\d+$/o) {
      push @$outref, NetAddr::IP::Lite->new($IP), 0;
    }
	# 11.22.33.44 - 11.22.33.49
    elsif ($IP =~ /^(\d+\.\d+\.\d+\.\d+)\s*\-\s*(\d+\.\d+\.\d+\.\d+)$/o) {
      push @$outref, NetAddr::IP::Lite->new($1), NetAddr::IP::Lite->new($2);
    }
	# 11.22.33.44/63
    elsif ($IP =~ m|^\d+\.\d+\.\d+\.\d+/\d+$|) {
      push @$outref, NetAddr::IP::Lite->new($IP), 0;
    }
	# 11.22.33.44/255.255.255.224
    elsif ($IP =~ m|^\d+\.\d+\.\d+\.\d+/\d+\.\d+\.\d+\.\d+$|o) {
      push @$outref, NetAddr::IP::Lite->new($IP), 0;
    }
# ignore un-matched IP patterns
  }
  return (scalar @$outref)/2;
}

=pod

$rv = matchNetAddr($ip,\@NAobject);

Check if an IP address appears in a list of NetAddr objects.

  input:	dot quad IP address,
		reference to NetAddr objects
  output:	true if match else false

=cut

sub matchNetAddr {
  my($ip,$naref) = @_;
  return 0 unless $ip && $ip =~ /\d+\.\d+\.\d+\.\d+/;
  $ip =~ s/\s//g;
  $ip = new NetAddr::IP::Lite($ip);
  my $i;
  for($i=0; $i <= $#{$naref}; $i += 2) {
    my $beg = $naref->[$i];
    my $end = $naref->[$i+1];
    if ($end) {
      return 1  if $ip >= $beg && $ip <= $end;
    } else {
      return 1 if $ip->within($beg);
    }
  }
  return 0;
}



$test = 2;

sub ok {
  print "ok $test\n";
  ++$test;
}

## test 2	instantiate netaddr array
#
# A multi-formated array of IP address that will never be tarpitted.
#
# WARNING: if you are using a private network, then you should include the
# address description for the net/subnets that you are using or you might
# find your DMZ or internal mail servers blocked since many DNSBLS list the
# private network addresses as BLACKLISTED
#
#       127./8, 10./8, 172.16/12, 192.168/16
#
#       class A         xxx.0.0.0/8
#       class B         xxx.xxx.0.0/16
#       class C         xxx.xxx.xxx.0/24	0
#       128 subnet      xxx.xxx.xxx.xxx/25	128
#        64 subnet      xxx.xxx.xxx.xxx/26	192
#        32 subnet      xxx.xxx.xxx.xxx/27	224
#        16 subnet      xxx.xxx.xxx.xxx/28	240
#         8 subnet      xxx.xxx.xxx.xxx/29	248
#         4 subnet      xxx.xxx.xxx.xxx/30	252
#         2 subnet      xxx.xxx.xxx.xxx/31	254
#       single address  xxx.xxx.xxx.xxx/32	255
#
@tstrng = (
	    # a single address
	'11.22.33.44',
	    # a range of ip's, ONLY VALID WITHIN THE SAME CLASS 'C'
	'22.33.44.55 - 22.33.44.65',
	'45.67.89.10-45.67.89.32',
	    # a CIDR range
	'5.6.7.16/28',
	    # a range specified with a netmask
	'7.8.9.128/255.255.255.240',
	    # this should ALWAYS be here
	'127.0.0.0/8',  # ignore all test entries and localhost
);
my @NAobject;
my $rv = list2NetAddr(\@tstrng,\@NAobject);
print "wrong number of NA objects\ngot: $rv, exp: 6\nnot "
	unless $rv == 6;
&ok;

## test 3	check disallowed terms
print "accepted null parameter\nnot "
	if matchNetAddr();
&ok;

## test 4	check disallowed parm
print "accepted non-numeric parameter\nnot "
	if matchNetAddr('junk');
&ok;

##test 5	check non-ip short
print "accepted short ip segment\nnot "
	if matchNetAddr('1.2.3');
&ok;

# yeah, it will accept a long one, but that's tough!

## test 6-35	bracket NA objects
#
my @chkary =	# 5 x 6 tests
    #	out left	in left		middle		in right	out right
qw(	11.22.33.43	11.22.33.44	11.22.33.44	11.22.33.44	11.22.33.45
	22.33.44.54	22.33.44.55	22.33.44.60	22.33.44.65	22.33.44.66
	45.67.89.9	45.67.89.10	45.67.89.20	45.67.89.32	45.67.89.33
	5.6.7.15	5.6.7.16	5.6.7.20	5.6.7.31	5.6.7.32
	7.8.9.127	7.8.9.128	7.8.9.138	7.8.9.143	7.8.9.144
	126.255.255.255	127.0.0.0	127.128.128.128	127.255.255.255	128.0.0.0
);

for(my $i=0;$i <= $#chkary; $i+=5) {
  print "accepted outside left bound $chkary[$i]\nnot "
	if matchNetAddr($chkary[$i],\@NAobject);
  &ok;
  print "rejected inside left bound $chkary[$i+1]\nnot "
	unless matchNetAddr($chkary[$i+1],\@NAobject);
  &ok;
  print "rejected inside middle bound $chkary[$i+2]\nnot "
	unless matchNetAddr($chkary[$i+2],\@NAobject);
  &ok;
  print "rejected inside right bound $chkary[$i+3]\nnot "
	unless matchNetAddr($chkary[$i+3],\@NAobject);
  &ok;
  print "accepted outside right bound $chkary[$i+4]\nnot "
	if matchNetAddr($chkary[$i+4],\@NAobject);
  &ok;
}