
| Current Path : /var/www/web-klick.de/dsh/10_customer2017/1183__ud/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/web-klick.de/dsh/10_customer2017/1183__ud/connection.pm |
package MRI::connection;
use strict;
use Socket;
use Symbol;
use Switch;
use MRI::precache;
use Fcntl;
use Log::Log4perl qw( :easy);
use Errno qw( EAGAIN EINTR );
my $host = "mx1.nd.denic.de";
my $port = 25;
my $proto = getprotobyname('tcp');
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
my $log = get_logger();
my $con = gensym;
my $lastline;
my $buffer = "";
use constant TIMEOUT => 25;
use constant BUFFSIZE => 65534;
sub getConnection {
if (eof($con)) {
$log->debug("Reseting Cache State.");
MRI::precache::resetStatus();
$log->debug("Trying to open connection.");
socket($con, PF_INET, SOCK_STREAM, $proto) or return 0;
$log->debug("Socket created.");
connect($con, $paddr) or return 0;
$log->debug("Connection established.");
fcntl ($con, F_SETFL,
( fcntl($con, F_GETFL, 0) )
| O_NONBLOCK);
$log->debug("Activate NON_BLOCK.");
# reset buffer on reconnect.
$buffer = "";
select((select($con), $| = 1)[0]);
}
}
sub closeConnection {
if (defined($con)) {
$log->debug("Closing connection.");
close $con;
}
}
sub getByLine {
my $line;
return $line if defined($line = splitByLine());
my $iBuff = "";
my $len = 0;
my $done = 0;
my $rbits = '';
vec($rbits, fileno $con, 1) = 1;
while(!$done) {
unless (
my $nfound = select $rbits, undef, undef, TIMEOUT
) {
$log->error("Timeout reached. Closing connection.");
closeConnection();
return;
} elsif ( $nfound < 0 && !$!{EINTR} ) {
$log->error("select(): $!");
closeConnection();
return;
}
unless( defined(
my $read = sysread $con, $iBuff, BUFFSIZE
) ) {
$log->error("Error reading data: $!");
return;
} elsif( not $read ) {
$log->warn("No data to read.");
return;
}
$buffer .= $iBuff;
if (index($buffer, "\n") != -1) {
$done = 1;
}
$iBuff = "";
}
return splitByLine();
}
sub splitByLine {
my $x = index $buffer, "\n";
my $line;
if ($x != -1) {
$line = substr $buffer, 0, $x;
$buffer = substr $buffer, $x+1;
}
return $line;
}
sub proceedByLine {
my $useLastLine = shift;
my $line = "";
my $skip = 0;
$log->info("Domain: " . MRI::precache::getDomain());
while($skip || defined($line = getByLine())) {
if ($skip) {
$skip = 0;
$line = $lastline;
}
$lastline = $line;
$log->debug("<" . $line);
my $response = MRI::precache::doSMTPLine($line);
switch($response) {
case -1 {
$log->error("Invalid remote message.");
}
case -2 {
$log->warn("Skipping Domain.");
$skip = 1;
}
# damn, error occoured
case 0 {
$log->error("Don't know what to do.. waiting.");
}
case 1 {
$log->info("done.");
return 0;
}
case 2 {
$log->info("List empty. Can't registry anything.");
return 1;
}
case 3 {
# we're too fast error.
return 0;
}
case 1000 {
# everything went fine.
}
else {
$log->error("Unknown result. " . $response);
}
}
}
$log->error("Connection error occoured. Stop.");
return -1;
}
sub sendOut {
my $output = shift;
$log->debug(">" . $output);
print $con $output . "\n";
return 1;
}
1;