Database/Rir.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Database/Ripe.pm ↑ Summary Database/Sinfp3.pm »
1 #
2 # $Id$
3 #
4 # database::rir Brik
5 #
6 package Metabrik::Database::Rir;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Some history:
11 # http://www.apnic.net/about-APNIC/organization/history-of-apnic/history-of-the-regional-internet-registries
12  
13 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub brik_properties {
16 return {
17 revision => '$Revision$',
18 tags => [ qw(unstable as country subnet) ],
19 author => 'GomoR <GomoR[at]metabrik.org>',
20 license => 'http://opensource.org/licenses/BSD-3-Clause',
21 attributes => {
22 datadir => [ qw(datadir) ],
23 input => [ qw(input.rir) ],
24 _read => [ qw(INTERNAL) ],
25 },
26 attributes_default => {
27 input => 'input.rir',
28 },
29 commands => {
30 update => [ ],
31 next_record => [ qw(input|OPTIONAL) ],
32 ip_to_asn => [ qw(ipv4_address) ],
33 },
34 require_modules => {
35 'Metabrik::Client::Www' => [ ],
36 'Metabrik::File::Read' => [ ],
37 'Metabrik::Network::Address' => [ ],
38 },
39 };
40 }
41  
42 sub update {
43 my $self = shift;
44  
45 my @urls = qw(
46 ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest
47 ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-extended-latest
48 ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-extended-latest
49 ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-extended-latest
50 ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-extended-latest
51 );
52  
53 my $datadir = $self->datadir;
54  
55 my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
56  
57 my @fetched = ();
58 for my $url (@urls) {
59 $self->log->verbose("update: fetching url [$url]");
60  
61 (my $filename = $url) =~ s/^.*\/(.*?)$/$1/;
62  
63 my $output = $datadir.'/'.$filename;
64 my $r = $cw->mirror($url, $filename, $datadir);
65 if (! defined($r)) {
66 $self->log->warning("update: can't fetch url [$url]");
67 next;
68 }
69 if (@$r == 0) { # Nothing new
70 next;
71 }
72 push @fetched, $output;
73 }
74  
75 return \@fetched;
76 }
77  
78 sub next_record {
79 my $self = shift;
80 my ($input) = @_;
81  
82 my $fr = $self->_read;
83 if (! defined($fr)) {
84 $input ||= $self->datadir.'/'.$self->input;
85 $self->brik_help_run_file_not_found('next_record', $input) or return;
86  
87 $fr = Metabrik::File::Read->new_from_brik_init($self) or return;
88 $fr->encoding('ascii');
89 $fr->input($input);
90 $fr->as_array(0);
91 $fr->open or return;
92 $self->_read($fr);
93 }
94  
95 my $na = Metabrik::Network::Address->new_from_brik_init($self) or return;
96  
97 # 2|afrinic|20150119|4180|00000000|20150119|00000
98 # afrinic|*|asn|*|1146|summary
99 # afrinic|*|ipv4|*|2586|summary
100 # afrinic|*|ipv6|*|448|summary
101 # afrinic|ZA|asn|1228|1|19910301|allocated
102 # arin|US|ipv4|13.128.0.0|524288|19860425|assigned|efe0f73dfd0d72364bf64f417b803f18
103  
104 my $line;
105 while ($line = $fr->read_line) {
106 next if $line =~ /^\s*#/; # Skip comments
107  
108 chomp($line);
109  
110 $self->log->debug("next_record: line[$line]");
111  
112 my @t = split(/\|/, $line);
113  
114 my $cc = $t[1];
115 if (! defined($cc)) {
116 $self->log->verbose("next_record: skipping line [$line]");
117 next;
118 }
119 next if ($cc eq '*');
120  
121 my $type = $t[2];
122 if (! defined($type)) {
123 $self->log->verbose("next_record: skipping line [$line]");
124 next;
125 }
126 next if ($type ne 'asn' && $type ne 'ipv4' && $type ne 'ipv6');
127  
128 my $source = $t[0];
129 my $value = $t[3];
130 my $count = $t[4];
131 my $date = $t[5];
132 my $status = $t[6];
133  
134 if ($date !~ /^\d{8}$/ && $date ne '') {
135 $self->log->warning("next_record: invalid date [$date] for line [$line]");
136 $date = '1970-01-01';
137 }
138 else {
139 $date =~ s/^(\d{4})(\d{2})(\d{2})$/$1-$2-$3/;
140 }
141  
142 my $from = 'undef';
143 my $to = 'undef';
144 my $subnet = 'undef';
145 if ($type eq 'ipv4') {
146 $from = $value;
147 my $integer = $na->ipv4_to_integer($from);
148 if (! defined($integer)) {
149 $self->log->warning("next_record: unable to convert IPv4 [$from]");
150 next;
151 }
152 $to = $na->integer_to_ipv4($integer + $count - 1);
153 if (! defined($to)) {
154 $self->log->warning("next_record: unable to convert integer [".$integer + $count."]");
155 next;
156 }
157 $subnet = $na->range_to_cidr($from, $to);
158 if (! defined($subnet)) {
159 $self->log->warning("next_record: unable to get subnet with [$from] [$to]");
160 next;
161 }
162 $subnet = join('|', @$subnet);
163 }
164  
165 my $h = {
166 raw => $line,
167 source => uc($source),
168 cc => uc($cc),
169 type => $type,
170 value => $value,
171 count => $count,
172 date => $date,
173 status => $status,
174 subnet => $subnet,
175 from => $value,
176 to => $to,
177 };
178  
179 return $h;
180 }
181  
182 return;
183 }
184  
185 sub ip_to_asn {
186 my $self = shift;
187 my ($ip) = @_;
188  
189 return $self;
190 }
191  
192 1;
193  
194 __END__
« Database/Ripe.pm ↑ Summary Database/Sinfp3.pm »