Codebase list libnet-irr-perl / 280d2c7
Import original source of Net-IRR 0.08 Carlos Vicente 12 years ago
7 changed file(s) with 495 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 Revision history for Perl extension Net::IRR.
1
2 0.08 Mar 30 2010
3 - Changed dependency from Net::TCP to IO::Socket::INET.
4
5 0.07 Nov 5 2009
6 - Added $whois->get_ipv6_routes_by_origin() - Greg Skinner [RT #48316]
7 - Fixed connect bug - Greg Skinner [RT #51154]
8
9 0.06 Tue Aug 10 15:20:54 2004
10 - $whois->sources() now uses wantarray to return a scalar or list
11 - fixed a couple of typos in t/01_methods.t (reported by Joshua Keroes)
12 - the synopsis compiles now
13
14 0.05 Tue Aug 10 08:07:10 2004
15 - seperated $whois->get_route_search() from $whois->get_as_search()
16 - cleaned up warnings
17 - refactored $whois->route_search() internals
18 - fixed a croak() typo in $whois->update()
19
20 0.04 Mon Aug 9 08:07:10 2004
21 - added $whois->get_route_set() (suggested by Peter Mills)
22 - fixed a usage message in $whois->get_as_set()
23
24 0.03 Wed Jul 21 09:37:58 2004
25 - more documentation cleanup
26 - better error handling for parsing server responses (bug reported by Peter Mills)
27
28 0.02 Thu Jun 24 09:13:47 2004
29 - updated documentation
30 - fixed required parameter checking
31 - now using croak() instead of die()
32
33 0.01 Mon Sep 22 09:02:33 2003
34 - renamed from IRR to Net::IRR
35 - added more tests
36
0 Changes
1 lib/Net/IRR.pm
2 Makefile.PL
3 MANIFEST
4 MANIFEST.SKIP
5 t/01_methods.t
6 META.yml Module meta-data (added by MakeMaker)
0 CVS/.*
1 \.bak$
2 \.tar$
3 \.tgz$
4 \.tar\.gz$
5 ^blib/
6 ^Makefile$
7 ^Makefile\.[a-z]+$
8 ^pm_to_blib$
9 ~$
10 ^_build
11 ^Build$
0 --- #YAML:1.0
1 name: Net-IRR
2 version: 0.08
3 abstract: Perl interface to the Internet Route Registry daemon
4 author:
5 - Todd Caine <todd.caine@gmail.com>
6 license: unknown
7 distribution_type: module
8 configure_requires:
9 ExtUtils::MakeMaker: 0
10 build_requires:
11 ExtUtils::MakeMaker: 0
12 requires:
13 IO::Socket::INET: 0
14 Test::More: 0
15 no_index:
16 directory:
17 - t
18 - inc
19 generated_by: ExtUtils::MakeMaker version 6.55_02
20 meta-spec:
21 url: http://module-build.sourceforge.net/META-spec-v1.4.html
22 version: 1.4
0 use ExtUtils::MakeMaker;
1
2 WriteMakefile(
3 'NAME' => 'Net::IRR',
4 'VERSION_FROM' => 'lib/Net/IRR.pm',
5 'PREREQ_PM' => {
6 Test::More => 0,
7 IO::Socket::INET => 0,
8 },
9 (($] ge '5.005') ?
10 ( 'AUTHOR' => 'Todd Caine <todd.caine@gmail.com>',
11 'ABSTRACT' => 'Perl interface to the Internet Route Registry daemon',
12 ) : (),
13 ),
14 );
0 package Net::IRR;
1
2 use strict;
3 use warnings;
4
5 use Carp;
6 use IO::Socket::INET;
7
8 use vars qw/ @ISA %EXPORT_TAGS @EXPORT_OK $VERSION /;
9
10 $VERSION = '0.08';
11
12 # used for route searches
13 use constant EXACT_MATCH => 'o';
14 use constant ONE_LEVEL => 'l';
15 use constant LESS_SPECIFIC => 'L';
16 use constant MORE_SPECIFIC => 'M';
17
18 require Exporter;
19 @ISA = qw(Exporter);
20 @EXPORT_OK = qw( EXACT_MATCH ONE_LEVEL LESS_SPECIFIC MORE_SPECIFIC );
21 %EXPORT_TAGS = (
22 'all' => \@EXPORT_OK,
23 'route' => \@EXPORT_OK,
24 );
25
26 # constructor
27 sub connect {
28 my ($class, %args) = @_;
29 my $self = bless {}, ref($class) || $class;
30 $self->{host} = $args{host} || '127.0.0.1';
31 $self->{port} = $args{port} || 43;
32 $self->{tcp} = IO::Socket::INET->new(
33 PeerAddr => $self->{host},
34 PeerPort => $self->{port},
35 Proto => 'tcp'
36 );
37
38 unless ($self->{tcp}) {
39 $self->{errstr} = "cannot create socket: $@";
40 return;
41 }
42
43 return undef if $self->error();
44 $self->_multi_mode();
45 $self->_identify();
46 return $self;
47 }
48
49 sub get_routes_by_origin {
50 my ($self, $as) = @_;
51 croak 'usage: $whois->get_routes_by_origin( $as_number )'
52 unless @_ == 2;
53 $as = 'as'.$as unless $as =~ /^as/i;
54 $self->{tcp}->send("!g${as}\n");
55 if (my $data = $self->_response()) {
56 return wantarray ? split(" ", $data) : $data;
57 }
58 return ();
59 }
60
61 # RIPE-181 Only
62 sub get_routes_by_community {
63 my ($self, $community) = @_;
64 croak 'usage: $whois->get_routes_by_community( $community )'
65 unless @_ == 2;
66 $self->{tcp}->send("!h${community}\n");
67 if (my $data = $self->_response()) {
68 return wantarray ? split(" ", $data) : $data;
69 }
70 return ();
71 }
72
73 sub get_ipv6_routes_by_origin {
74 my ($self, $as) = @_;
75 croak 'usage: $whois->get_ipv6_routes_by_origin( $as_number )'
76 unless @_ == 2;
77 $as = 'as'.$as unless $as =~ /^as/i;
78 $self->{tcp}->send("!6${as}\n");
79 if (my $data = $self->_response()) {
80 return wantarray ? split(" ", $data) : $data;
81 }
82 return ();
83 }
84
85 sub get_sync_info {
86 my ($self, @dbs) = @_;
87 my $dbs = (@dbs) ? join(",",@dbs) : '-*';
88 $self->{tcp}->send("!j${dbs}\n");
89 return $self->_response();
90 }
91
92 sub get_as_set {
93 my ($self, $as_set, $expand) = @_;
94 croak 'usage: $whois->get_as_set( $as_set )'
95 unless @_ >= 2 && @_ <= 3;
96 $expand = ($expand) ? ',1' : '';
97 $self->{tcp}->send("!i${as_set}${expand}\n");
98 if (my $data = $self->_response()) {
99 return wantarray ? split(" ", $data) : $data;
100 }
101 return ();
102 }
103
104 sub get_route_set { my ($self, $route_set, $expand) = @_;
105 croak 'usage: $whois->get_route_set( $route_set )'
106 unless @_ >= 2 && @_ <= 3;
107 $expand = ($expand) ? ',1' : '';
108 $self->{tcp}->send("!i${route_set}${expand}\n");
109 if (my $data = $self->_response()) {
110 return wantarray ? split(" ", $data) : $data;
111 }
112 return ();
113 }
114
115 sub match {
116 my ($self, $type, $key) = @_;
117 croak 'usage: $whois->match( $object_type, $key )'
118 unless @_ == 3;
119 $self->{tcp}->send("!m${type},${key}\n");
120 return $self->_response();
121 }
122
123 *disconnect = \&quit;
124 sub quit {
125 my $self = shift;
126 $self->{tcp}->send("!q\n");
127 }
128
129 sub _identify {
130 my ($self) = @_;
131 $self->{tcp}->send("!nNet::IRR\n");
132 return $self->_response();
133 }
134
135 sub _multi_mode {
136 my ($self) = @_;
137 $self->{tcp}->send("!!\n");
138 return 1;
139 }
140
141 sub get_irrd_version {
142 my ($self) = @_;
143 $self->{tcp}->send("!v\n");
144 return $self->_response();
145 }
146
147 sub route_search {
148 my ($self, $route, $specific) = @_;
149 croak 'usage: $whois->route_search( $route )'
150 unless @_ >= 2 && @_ <= 3;
151 $specific = ($specific) ? ",$specific" : '';
152 $self->{tcp}->send("!r${route}${specific}\n");
153 my $response = $self->_response();
154 chomp($response) if $response;
155 $response =~ s/\s*$// if $response;
156 return $response;
157 }
158
159 sub sources {
160 my ($self, @sources) = @_;
161 my $source = (@sources) ? join(",", @sources) : '-lc';
162 $self->{tcp}->send("!s${source}\n");
163 my $response = $self->_response();
164 chomp($response) if $response;
165 return wantarray ? split(',', $response) : $response;
166 }
167
168 sub update {
169 my ($self, $db, $action, $object) = @_;
170 croak 'usage: $whois->update( $db, "ADD|DEL", $object )'
171 unless @_ == 4;
172 croak 'second argument to $whois->update() must be either ADD or DEL'
173 unless $action eq 'ADD' || $action eq 'DEL';
174 $self->{tcp}->send( sprintf("!us%s\n%s\n\n%s\n\n!ue\n", $db, $action, $object) );
175 return $self->_response();
176 }
177
178 sub _response {
179 my $self = shift;
180 my $t = $self->{tcp};
181 my $header = $t->getline();
182 my $error_prefix = 'Net::IRR read error';
183 if (not defined $header) {
184 $self->{errstr} = sprintf("%s: no data read from %s:%d\n", $error_prefix, $self->{host}, $self->{port});
185 return ();
186 }
187 return () if ($header =~ /^[CDEF].*$/);
188 my($data_length) = $header =~ /^A(.*)$/;
189 my $data = '';
190 while($data_length != length($data)) {
191 $data .= $t->getline();
192 }
193 carp sprintf("%s: only received %d out of %d bytes from %s:%d\n", $error_prefix, length($data), $data_length, $self->{host}, $self->{port})
194 if $data_length != length($data);
195 my $footer = $t->getline();
196 return $data;
197 }
198
199 sub error {
200 my $self = shift;
201 return $self->{errstr};
202 }
203
204 1;
205 __END__
206
207 =pod
208
209 =head1 NAME
210
211 Net::IRR - Perl interface to the Internet Route Registry Daemon
212
213 =head1 SYNOPSIS
214
215 use Net::IRR;
216
217 my $host = 'whois.radb.net';
218
219 my $i = Net::IRR->connect( host => $host )
220 or die "can't connect to $host\n";
221
222 my $version = $i->get_irrd_version();
223 print "IRRd version: $version\n" unless $i->error();
224
225 print "routes by origin AS5650\n";
226 my @routes = $i->get_routes_by_origin("AS5650");
227 print "found " . scalar(@routes) . " routes\n";
228
229 print "AS-SET for AS5650\n";
230 if (my @ases = $i->get_as_set("AS-ELI", 1)) {
231 print "found " . scalar(@ases) . " AS's\n";
232 print "@ases\n";
233 }
234 else {
235 print "none found\n";
236 }
237
238 my $aut_num = $i->match("aut-num","as5650")
239 or warn("can't find object: " . $i->error . "\n");
240
241 print $i->route_search("208.186.0.0/15", Net::IRR::EXACT_MATCH)
242 . " originates 208.186.0.0/15\n";
243
244 print $i->get_sync_info(), "\n";
245
246 $i->disconnect();
247
248 =head1 DESCRIPTION
249
250 This module provides an object oriented perl interface to the Internet Route Registry. The interface uses the RIPE/RPSL Tool Query Language as defined in Appendix B of the IRRd User Guide. The guide can be found at http://www.irrd.net/, however an understanding of the query language is not required to use this module.
251
252 Net::IRR supports IRRd's multiple-command mode. Multiple-command mode is good for intensive queries since only one TCP connection needs to be made for multiple queries. The interface also allows for additional queries that aren't supported by standard UNIX I<whois> utitilies.
253
254 Hopefully this module will stimulate development of new Route Registry tools written in Perl. An example of Route Registry tools can be found by googling for RAToolset which is now known as the IRRToolset. The RAToolset was originally developed by ISI, http://www.isi.edu/, and is now maintained by RIPE, http://www.ripe.net/.
255
256 =head1 METHODS
257
258 =over 4
259
260 =item Net::IRR->connect( host => $hostname, port => $port_number )
261
262 This class method is used to connect to a route registry server. Net::IRR->connect() is also the constructor for the Net::IRR class. The constructor returns a Net::IRR object upon connection to the IRR server or undef upon failure.
263
264 =item $whois->disconnect()
265
266 This method closes the connection to the route registry server.
267
268 =item $whois->quit()
269
270 Same as $whois->disconnect().
271
272 =item $whois->get_routes_by_origin('AS5650')
273
274 Get routes with a specified origin AS. This method takes an autonomous system number and returns the set of routes it originates. Upon success this method returns a list of routes in list context or a string of space separated routes. undef is returned upon failure.
275
276 =item $whois->get_ipv6_routes_by_origin('AS5650')
277
278 Same as $whois->get_routes_by_origin(), but returns IPv6 instead of IPv4 routes.
279
280 =item $whois->get_routes_by_community($community_name)
281
282 This method is for RIPE-181 only. It is not supported by RPSL. This method takes a community object name and returns the set of routes it originates. Upon success this method returns a list of routes in list context or a string of space separated routes. undef is returned upon failure.
283
284 =item $whois->get_sync_info()
285
286 This method provides database synchronization information. This makes it possible to view the mirror status of a database. This method optionally takes the name of a database such as RADB. If no argument is given the method will return information about all databases originating from and mirrored by the registry server. If the optional argument is given the database specified will be checked and it's status returned. This method returns undef if no database exists or if access is denied.
287
288 =item $whois->get_as_set("AS-ELI", 1)
289
290 This method takes an AS-SET object name and returns the ASNs registered for the AS-SET object. The method takes an optional second argument which enables AS-SET key expansion since an AS-SET can contain both ASNs and AS-SET keys. undef is returned upon failure.
291
292 =item $whois->get_route_set("ROUTES-ELI", 1)
293
294 This method takes an ROUTE-SET object name and returns the ROUTEs registered for the ROUTE-SET object. The method takes an optional second argument which enables ROUTE-SET key expansion since a ROUTE-SET can contain both ROUTEs and ROUTE-SET keys. undef is returned upon failure.
295
296 =item $whois->match('aut-num', 'AS5650'); - get RPSL objects registered in the database
297
298 The example above will retrieve the aut-num object with the key AS5650. This method will return the first RPSL object matching the object type and name specified as parameters to $whois->match(). undef is returned upon failure.
299
300 =item $whois->get_irrd_version()
301
302 This methods takes no arguments and returns the version of the IRRd server that was specified as the hostname to the connect() method.
303
304 =item $whois->route_search("208.186.0.0/15", EXACT_MATCH)
305
306 The method is used to search for route objects. The method takes two arguments, a route and an optional flag. The flag can be one of four values: EXACT_MATCH, LEVEL_ONE, LESS_SPECIFIC, MORE_SPECIFIC. These constants can be imported into your namespace by using the :all or :route export tag when importing the Net::IRR module.
307
308 use Net::IRR qw( :route );
309
310 print "EXACT_MATCH = " . EXACT_MATCH . "\n";
311
312 =item $whois->sources()
313
314 This method is used to both get and set the databases used for Internet Route Registry queries. The $whois->sources() method accepts a list of databases in the order they should be searched. If no arguments are given the method will return a list of all the databases mirrored by the route registry you are connected to.
315
316 =item $whois->update($database, 'ADD', $rpsl_rr_object)
317
318 This method is used to add or delete a database object. This method takes three arguments. The first argument is the database to update. The second argument is the action which can be either "ADD" or "DEL". The third and final required argument is a route object in RPSL format.
319
320 =item $whois->error()
321
322 Most Net::IRR methods set an error message when errors occur. These errors can only be accessed by using the error() method.
323
324 =back
325
326 =head1 AUTHOR
327
328 Todd Caine <todd.caine@gmail.com>
329
330 =head1 SEE ALSO
331
332 Main IRRd Site
333
334 http://www.irrd.net/
335
336 RIPE/RPSL Tool Query Language
337
338 http://www.irrd.net/irrd-user.pdf, Appendix B
339
340 =head1 COPYRIGHT
341
342 Copyright 2002-2010 by Todd Caine. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
343
344 =cut
0
1 use Test::More tests => 21;
2
3 BEGIN{ use_ok('Net::IRR') }
4
5 my $host = 'whois.radb.net';
6 can_ok("Net::IRR", "connect");
7 my $i = Net::IRR->connect( host => $host );
8 ok($i, "connected to $host");
9
10 can_ok($i, "get_irrd_version");
11 ok ($i->get_irrd_version, 'IRRd version number found');
12
13 can_ok($i, "get_routes_by_origin");
14 my @routes = $i->get_routes_by_origin("AS5650");
15 my $found = scalar @routes;
16 ok ($found, "found $found routes for AS5650");
17
18 can_ok($i, "get_ipv6_routes_by_origin");
19
20 can_ok($i, "get_as_set");
21 if (my @ases = $i->get_as_set("AS-ELI", 1)) {
22 my $found = scalar @ases;
23 ok ($found, "found $found ASNs in the AS-ELI AS set. (1)");
24 }
25 else {
26 fail('no ASNs found in the AS-ELI AS set (1)');
27 }
28
29 can_ok($i, "get_route_set");
30 if (my @ases = $i->get_route_set("AS-ELI", 1)) {
31 my $found = scalar @ases;
32 ok ($found, "found $found ASNs in the AS-ELI AS set (2).");
33 }
34 else {
35 fail('no ASNs found in the AS-ELI AS set (2)');
36 }
37
38 can_ok($i, "match");
39 my $person = $i->match("aut-num","as5650");
40 ok($person, "found an aut-num object for AS5650");
41
42 can_ok($i, "route_search");
43 my $origin = $i->route_search("208.186.0.0/15", 'o');
44 ok( $origin, "$origin originates 208.186.0.0/15" );
45
46 my $origin1 = $i->route_search("10.0.0.0/8", 'o');
47 ok( not(defined($i->error())), "10.0.0.0/8 was not found" );
48
49 can_ok($i, "get_sync_info");
50 my $info = $i->get_sync_info();
51 ok($info, 'found synchronization information');
52
53 can_ok($i, "disconnect");
54 ok($i->disconnect(), 'disconnect was successful');
55