| 1 | package SpamMonkey::Utils; | ||
| 2 | use strict; | ||
| 3 | my %cache; | ||
| 4 | use POSIX qw(SIGALRM); | ||
| 5 | |||
| 6 | sub host_to_ip { # Basic method | ||
| 7 | my ($self, $host) = @_; | ||
| 8 | my $addr; | ||
| 9 | return $cache{$host} if exists $cache{$host}; | ||
| 10 | $addr = eval { | ||
| 11 | POSIX::sigaction(SIGALRM, POSIX::SigAction->new(sub { die "alarm" })); | ||
| 12 | alarm 5; | ||
| 13 | (gethostbyname $host)[4]; | ||
| 14 | }; | ||
| 15 | alarm 0; | ||
| 16 | $cache{$host} = $addr; # Yes, this caches failures | ||
| 17 | return unless $addr; | ||
| 18 | my @bits = unpack("C4",$addr); | ||
| 19 | return wantarray ? @bits : join ".", @bits; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub rbl_check { # Complex method | ||
| 23 | my ($self, $host, $type, $timeout) = @_; | ||
| 24 | my $resolver = Net::DNS::Resolver->new(); | ||
| 25 | $resolver->tcp_timeout($timeout) if $timeout; | ||
| 26 | $resolver->udp_timeout($timeout) if $timeout; | ||
| 27 | return ! ! $resolver->query($host, $type); | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | 1; |