-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip2dnsbl.pl
executable file
·77 lines (70 loc) · 1.93 KB
/
ip2dnsbl.pl
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
#!/usr/bin/perl -w
#
# Script for sending ip(s) to the sa2dnsbl server
#TODO: manpage
use strict;
use IO::Socket;
my (%addresses, %config);
my ($ip) = @ARGV;
my $retries = 3;
my $timeout = 2;
my $configfile = '/etc/sa2dnsbld.cf';
# read config file and check input from config file
if ( -r $configfile ) {
%config = do $configfile;
} else {
die("can not read $configfile");
}
if ( $config{LOCALPORT} !~ /^\d+$/) { die("$configfile: LOCALPORT has wrong format"); }
if ( $config{LOCALIP} !~ /^[.:a-f\d]+$/i) { die("$configfile: LOCALIP is not an IP address"); }
# check IP addresses
if(not $ip) {
while (<STDIN>) {
chomp;
if($_ =~ m/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/ or
$_ =~ m/^[a-f:0-9]{2,41}/i ) {
$addresses{$_}++;
}
}
} else {
if($ip =~ m/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/ or
$ip =~ m/^[a-f:0-9]{2,41}/i ) {
$addresses{$ip}++;
}
}
# open socket
my $sock = IO::Socket::INET->new(Proto => 'udp', PeerPort => $config{LOCALPORT}, PeerAddr => $config{LOCALIP});
if(!defined($sock)) {
# Error if socket connection failed!
die("failed to create socket");
}
foreach my $address (keys %addresses) {
my $output = "$address:'spam'#''#''#'ip2dnsbl client'";
$sock->send($output);
my $input = udp_recieve($sock, $timeout);
my $retry = 1;
while(!$input && $retry <= $retries) {
print "retry: $retry\n";
$sock->send($output);
$input = udp_recieve($sock, $timeout);
$retry++;
}
if($retry > $retries) {
print "error: failed to send data\n";
}
if($input > 0) {
printf("Send %s to sa2dnsbld on %s:%d (Result: %d)\n", $address, $config{LOCALIP}, $config{LOCALPORT}, $input);
}
}
$sock->close();
sub udp_recieve {
my ($sock, $timeout) = @_;
my $input;
eval {
local $SIG{ALRM} = sub { return 0 };
alarm $timeout;
$sock->recv($input, 1024);
alarm 0;
return $input;
} or return 0;
}