File Coverage

blib/lib/Net/DNSBLLookup.pm
Criterion Covered Total %
statement 57 92 61.9
branch 0 8 0.0
condition 0 2 0.0
subroutine 19 21 90.4
pod 2 2 100.0
total 78 125 62.4


line stmt bran cond sub pod time code
1             package Net::DNSBLLookup;
2              
3 1     1   7131 use 5.005;
  1         6  
  1         62  
4 1     1   5 use strict;
  1         2  
  1         67  
5              
6             require Exporter;
7 1     1   7 use vars qw($VERSION @EXPORT @ISA);
  1         8  
  1         85  
8 1     1   1236 use Net::DNS;
  1         125828  
  1         102  
9 1     1   10 use IO::Select;
  1         2  
  1         73  
10             $VERSION = '0.05';
11             @ISA = qw(Exporter);
12              
13             @EXPORT = qw(DNSBLLOOKUP_OPEN_RELAY DNSBLLOOKUP_DYNAMIC_IP
14             DNSBLLOOKUP_CONFIRMED_SPAM DNSBLLOOKUP_SMARTHOST DNSBLLOOKUP_SPAMHOUSE DNSBLLOOKUP_LISTSERVER
15             DNSBLLOOKUP_FORMMAIL DNSBLLOOKUP_OPEN_PROXY DNSBLLOOKUP_OPEN_PROXY_HTTP DNSBLLOOKUP_OPEN_PROXY_SOCKS
16             DNSBLLOOKUP_OPEN_PROXY_MISC DNSBLLOOKUP_HIJACKED DNSBLLOOKUP_MULTI_OPEN_RELAY DNSBLLOOKUP_UNKNOWN);
17              
18 1     1   5 use constant DNSBLLOOKUP_OPEN_RELAY => 1;
  1         2  
  1         55  
19 1     1   4 use constant DNSBLLOOKUP_DYNAMIC_IP => 2;
  1         2  
  1         41  
20 1     1   6 use constant DNSBLLOOKUP_CONFIRMED_SPAM => 3;
  1         1  
  1         33  
21 1     1   4 use constant DNSBLLOOKUP_SMARTHOST => 4;
  1         1  
  1         34  
22 1     1   4 use constant DNSBLLOOKUP_SPAMHOUSE => 5;
  1         1  
  1         49  
23 1     1   5 use constant DNSBLLOOKUP_LISTSERVER => 6;
  1         1  
  1         50  
24 1     1   5 use constant DNSBLLOOKUP_FORMMAIL => 7;
  1         1  
  1         93  
25 1     1   4 use constant DNSBLLOOKUP_OPEN_PROXY => 8;
  1         3  
  1         37  
26 1     1   17 use constant DNSBLLOOKUP_OPEN_PROXY_HTTP => 9;
  1         1  
  1         32  
27 1     1   4 use constant DNSBLLOOKUP_OPEN_PROXY_SOCKS => 10;
  1         1  
  1         34  
28 1     1   4 use constant DNSBLLOOKUP_OPEN_PROXY_MISC => 11;
  1         1  
  1         33  
29 1     1   4 use constant DNSBLLOOKUP_HIJACKED => 12;
  1         1  
  1         32  
30 1     1   4 use constant DNSBLLOOKUP_MULTI_OPEN_RELAY => 13;
  1         1  
  1         33  
31 1     1   4 use constant DNSBLLOOKUP_UNKNOWN => 14;
  1         2  
  1         662  
32              
33             require Net::DNSBLLookup::Result;
34              
35             %Net::DNSBLLookup::dns_servers = (
36              
37             # no longer implemented, since osirusoft.com was taken offline due
38             # to DDos attacks from spammers
39              
40             # 'relays.osirusoft.com' => {
41             # '127.0.0.2' => DNSBLLOOKUP_OPEN_RELAY,
42             # '127.0.0.3' => DNSBLLOOKUP_DYNAMIC_IP, # dialup
43             # '127.0.0.4' => DNSBLLOOKUP_CONFIRMED_SPAM,
44             # '127.0.0.5' => DNSBLLOOKUP_SMARTHOST,
45             # '127.0.0.6' => DNSBLLOOKUP_SPAMHOUSE,
46             # '127.0.0.7' => DNSBLLOOKUP_LISTSERVER,
47             # '127.0.0.8' => DNSBLLOOKUP_FORMMAIL,
48             # '127.0.0.9' => DNSBLLOOKUP_OPEN_PROXY,
49             # },
50             'dnsbl.sorbs.net' => {
51             '127.0.0.2' => DNSBLLOOKUP_OPEN_PROXY_HTTP,
52             '127.0.0.3' => DNSBLLOOKUP_OPEN_PROXY_SOCKS,
53             '127.0.0.4' => DNSBLLOOKUP_OPEN_PROXY_MISC,
54             '127.0.0.5' => DNSBLLOOKUP_OPEN_RELAY,
55             '127.0.0.6' => DNSBLLOOKUP_SPAMHOUSE,
56             '127.0.0.7' => DNSBLLOOKUP_FORMMAIL,
57             '127.0.0.8' => DNSBLLOOKUP_CONFIRMED_SPAM,
58             '127.0.0.9' => DNSBLLOOKUP_HIJACKED,
59             '127.0.0.10' => DNSBLLOOKUP_DYNAMIC_IP, # not same as dialup
60             },
61             'proxies.blackholes.easynet.net' => {
62             '127.0.0.2' => DNSBLLOOKUP_OPEN_PROXY,
63             },
64             'dnsbl.njabl.org' => {
65             '127.0.0.2' => DNSBLLOOKUP_OPEN_RELAY,
66             '127.0.0.3' => DNSBLLOOKUP_DYNAMIC_IP,
67             '127.0.0.4' => DNSBLLOOKUP_SPAMHOUSE,
68             '127.0.0.5' => DNSBLLOOKUP_MULTI_OPEN_RELAY,
69             '127.0.0.8' => DNSBLLOOKUP_FORMMAIL,
70             '127.0.0.9' => DNSBLLOOKUP_OPEN_PROXY,
71             },
72             # 'list.dsbl.org' => {
73             # '127.0.0.2' => DNSBLLOOKUP_UNKNOWN,
74             # },
75             # 'opm.blitzed.org' => sub {
76             # my ($ip) = @_;
77             # # todo deal with bitmasks properly
78             # # see http://opm.blitzed.org/info
79             # return DNSBLLOOKUP_OPEN_PROXY;
80             # },
81             'cbl.abuseat.org' => {
82             '127.0.0.2' => DNSBLLOOKUP_OPEN_PROXY,
83             },
84             'psbl.surriel.com' => {
85             '127.0.0.2' => DNSBLLOOKUP_OPEN_PROXY,
86             },
87             );
88              
89             sub new {
90 0     0 1   my ($class) = shift;
91 0           my $self = { @_ };
92 0           bless $self, $class;
93 0 0         unless (exists $self->{zones}) {
94 0           @{$self->{zones}} = grep !/^relays\.osirusoft\.com$/, keys %Net::DNSBLLookup::dns_servers;
  0            
95             }
96 0   0       $self->{timeout} ||= 5;
97 0           return $self;
98             }
99              
100             sub lookup {
101 0     0 1   my ($self, $ip) = @_;
102              
103 0           my $res = Net::DNS::Resolver->new;
104 0           my $sel = IO::Select->new;
105 0           my @sockets;
106              
107 0           my $result = Net::DNSBLLookup::Result->new();
108              
109 0           my $reverse_ip = join('.',reverse split('\.',$ip));
110              
111 0           for my $zone (@{$self->{zones}}) {
  0            
112 0           my $host = join('.',$reverse_ip,$zone);
113 0           my $socket = $res->bgsend($host);
114 0           $sel->add($socket);
115 0           undef $socket;
116             }
117              
118 0           while ($sel->count > 0) {
119 0           my @ready = $sel->can_read($self->{timeout});
120 0 0         last unless @ready;
121 0           foreach my $sock (@ready) {
122 0           my $packet = $res->bgread($sock);
123 0           my ($question) = $packet->question;
124 0 0         next unless $question;
125 0           my $qname = $question->qname;
126 0           (my $dnsbl = $qname) =~ s!^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\.!!;
127 0           $result->add_dnsbl($dnsbl);
128 0           foreach my $rr ($packet->answer) {
129 0 0         next unless $rr->type eq "A";
130 0           $result->add($dnsbl, $rr->address);
131             }
132 0           $sel->remove($sock);
133             }
134             }
135 0           return $result;
136             }
137              
138             1;
139             __END__