lib/NOLookup/DAS/DASLookup.pm

filter: Statementstmt 69.1% 47 / 68 Branchbran 16.6% 5 / 30 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 56.5% 65 / 115 CC 17 SCAR 38.8
CC
17
Cov
52%
lookup
101.7
new
5.8
AUTOLOAD
2.1
CRAP 48.5
« lib/NOLookup/Patent/DataLookup.pm ↑ Summary lib/NOLookup/Brreg/DataLookup.pm »
1 package NOLookup::DAS::DASLookup;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IO::Socket::INET6;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 @ISA = qw( Exporter );
10 1 @EXPORT_OK = qw / $DAS_LOOKUP_ERR_NO_CONN
11  
12 $DAS_LOOKUP_ERR_QUOTA_EXCEEDED
13 $DAS_LOOKUP_ERR_NO_ACCESS
14 $DAS_LOOKUP_ERR_REFERRAL_DENIED
15  
16 $DAS_LOOKUP_ERR_OTHER
17 /;
18  
19 # Error codes returned fom the DASLookup module
20 # Ref. the Norid API definition at
21 # https://www.norid.no/en/registrar/system/dokumentasjon/whoisdas-grensesnitt/
22
23 # Connection problems
24 1 our $DAS_LOOKUP_ERR_NO_CONN = 100;
25  
26 # Controlled refuses
27 1 our $DAS_LOOKUP_ERR_QUOTA_EXCEEDED = 101;
28 1 our $DAS_LOOKUP_ERR_NO_ACCESS = 102;
29 1 our $DAS_LOOKUP_ERR_REFERRAL_DENIED = 103;
30  
31 # DB and other problems, all the 'ERROR - xxxx'
32 # See raw_text for details on the problem.
33 1 our $DAS_LOOKUP_ERR_OTHER = 104;
34  
35 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 $Data::Dumper::Indent=1;
37  
38 1 ▶use vars qw/$AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 26 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.4
41 26 my $self=shift;
42 26 $AUTOLOAD =~ s/.*:://;
43
44 26 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 26 26
45 # set operation
46 0 return $self->{$AUTOLOAD} = shift;
47 } else {
48 # get operation
49 26 return $self->{$AUTOLOAD};
50 }
51 }
52  
53 4 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod documented
54 4 my ($proto, $query, $das_server, $das_port, $client_ip)=@_;
55 4 ▶ my $class=ref $proto||$proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
56 4 my $self=bless {},$class;
57  
58 # $query is required for something to happen
59 4 ▶ return $self unless $query;
Branch: 1 branch
BranchTrueFalseTotal
unless $query 0 4 4
60  
61 # defaults
62 4 ▶ $das_server = 'finger.norid.no' unless ($das_server);
Branch: 1 branch
BranchTrueFalseTotal
unless $das_server 0 4 4
63 4 ▶ $das_port = 79 unless ($das_port);
Branch: 1 branch
BranchTrueFalseTotal
unless $das_port 4 0 4
64  
65 4 return $self->lookup($query, $das_server, $das_port, $client_ip);
66 }
67  
68 4 ▶sub lookup {
Subroutine lookup: called CC=12 SCAR=46.2
Pod documented
69 4 my ($self, $query, $das_server, $das_port, $client_ip) = @_;
70  
71 4 my ($line, $text);
72  
73 4 my $sock = IO::Socket::INET6->new (
74 PeerAddr => $das_server,
75 PeerPort => $das_port,
76 Proto => 'tcp',
77 Timeout => 10,
78 );
79  
80 4 ▶ unless($sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 4 0 4
81 4 $self->{errno} = $DAS_LOOKUP_ERR_NO_CONN;
82 #print STDERR "SOCK ERR: $!\n";
83 4 return $self;
84 }
85  
86 0 $query = Encode::encode('UTF-8', $query);
87  
88 # Always code query as utf-8
89 0 ▶ if ($client_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_ip) { } 0 0 0
90 # Use the special -V option to identify the client IP
91 # for proper rate limiting purposes.
92 # Note that the ip address must be registered by Norid
93 # to work properly, if not, a referral error is returned.
94 0 print $sock "-V v0,$client_ip -c utf-8 $query\n";
95 } else {
96 0 print $sock "-c utf-8 $query\n";
97 }
98
99 # Read all answer lines into one long LF separated $text
100 0 ▶ while ($line = <$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $sock)) 0 0 0
101 0 $text .= $line;
102 }
103 0 close $sock;
104 0 chomp $text;
105
106 0 $text = Encode::decode('UTF-8', $text);
107  
108 # Parse DAS response and map values into object methods.
109 0 $self->{raw_text} = $text;
110  
111 # Detect any of the error situations
112 0 ▶ if ($text =~ m/Quota exceeded/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($text =~ /Quota exceeded/) { } 0 0 0
elsif ($text =~ /Access denied/) { } 0 0 0
elsif ($text =~ /Referral denied/) { } 0 0 0
elsif ($text =~ /ERROR - /) { } 0 0 0
elsif ($text =~ / is available /) { } 0 0 0
elsif ($text =~ / is delegated /) { } 0 0 0
elsif ($text =~ /This domain can currently not be registered/) { } 0 0 0
elsif ($text =~ /Domain is not valid /) { } 0 0 0
113 0 $self->{errno} = $DAS_LOOKUP_ERR_QUOTA_EXCEEDED;
114  
115 } elsif ($text =~ m/Access denied/) {
116 0 $self->{errno} = $DAS_LOOKUP_ERR_NO_ACCESS;
117
118 } elsif ($text =~ m/Referral denied/) {
119 0 $self->{errno} = $DAS_LOOKUP_ERR_REFERRAL_DENIED;
120  
121 } elsif ($text =~ m/ERROR - /) {
122 0 $self->{errno} = $DAS_LOOKUP_ERR_OTHER;
123  
124 } elsif ($text =~ m/ is available /) {
125 0 $self->{available} = 1;
126  
127 } elsif ($text =~ m/ is delegated /) {
128 0 $self->{delegated} = 1;
129  
130 } elsif ($text =~ m/This domain can currently not be registered/) {
131 0 $self->{prohibited} = 1;
132  
133 } elsif ($text =~ m/Domain is not valid /) {
134 0 $self->{invalid} = 1;
135 }
136  
137 #print STDERR "\n\n====\nDAS self after $query: ", Dumper $self;
138 0 return $self;
139 }
140  
141 =pod
142  
143 =encoding ISO-8859-1
144  
145 =head1 NAME
146  
147 NOLookup::DAS::DASLookup - Lookup DAS data from Norid.
148  
149 =head1 SYNOPSIS
150  
151 my $das = NOLookup::DAS::DASLookup->new('norid.no');
152  
153 One of the below accessor methods will return something:
154
155 $das->errno() Set to an error code if some error has occured.
156 $das->available() True if domain is available, thus registration can be attempted.
157 $das->delegated() True if domain is delegated, and thus not available.
158 $das->prohibited() True if domain is prohibited by policy, and thus not allowed.
159 $das->invalid() True if domain or zone is invalid, like a .com domain,
160 or if the zone is not administered by Norid, like
161 some 'test.mil.no' domain.
162 $das->raw_text() Contains the raw DAS response.
163  
164 =head1 DESCRIPTION
165  
166 This module provides an object oriented API for use with the
167 Norid DAS service. It uses the command line based DAS interface
168 internally to fetch information from Norid.
169  
170 =head2 METHODS
171  
172 =over 5
173  
174 =item new
175  
176 The constructor. Takes a lookup argument. Returns a new object.
177  
178 =item lookup
179  
180 Do a DAS lookup in the Norid database and populate the object
181 from the result.
182  
183  
184 =item AUTOLOAD
185  
186 This module uses the autoload mechanism to provide accessors for any
187 available data through the get mechanism above.
188  
189 =item errno, available, delegated, prohibited, invalid
190  
191 See SYNOPSIS.
192  
193 =back
194  
195 =head1 SEE ALSO
196  
197 L<https://www.norid.no/en>
198 L<https://teknisk.norid.no/en/integrere-mot-norid/whois>
199  
200 =head1 AUTHOR
201  
202 Trond Haugen, E<lt>(nospam)info(at)norid.noE<gt>
203  
204 =head1 COPYRIGHT
205  
206 Copyright (c) 2017 Trond Haugen <(nospam)info(at)norid.no>.
207 All rights reserved.
208  
209 This program is free software; you can redistribute it and/or modify
210 it under the terms of the GNU General Public License as published by
211 the Free Software Foundation; either version 2 of the License, or
212 (at your option) any later version.
213  
214 =head1 LICENSE
215  
216 This library is free software. You can redistribute it and/or modify
217 it under the same terms as Perl itself.
218  
219 =cut
220  
221 1 1;
« lib/NOLookup/Patent/DataLookup.pm ↑ Summary lib/NOLookup/Brreg/DataLookup.pm »