From 59b826d4bbdb1bc7e1d895dfefdceb612425531c Mon Sep 17 00:00:00 2001 From: Matt Sergeant Date: Fri, 26 Sep 2008 17:40:04 +0000 Subject: [PATCH] Fix to check client is writable before writing to it. git-svn-id: https://svn.perl.org/qpsmtpd/trunk@946 958fd67b-6ff1-0310-b445-bb7760255be9 --- lib/Qpsmtpd/TcpServer.pm | 23 +++++++++++++++++++---- lib/Qpsmtpd/TcpServer/Prefork.pm | 6 ++++++ qpsmtpd-forkserver | 2 +- qpsmtpd-prefork | 2 +- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/Qpsmtpd/TcpServer.pm b/lib/Qpsmtpd/TcpServer.pm index abf29d2..2a67902 100644 --- a/lib/Qpsmtpd/TcpServer.pm +++ b/lib/Qpsmtpd/TcpServer.pm @@ -67,16 +67,17 @@ sub start_connection { } sub run { - my $self = shift; + my ($self, $client) = @_; - # should be somewhere in Qpsmtpd.pm and not here... - $self->load_plugins unless $self->{hooks}; + # Set local client_socket to passed client object for testing socket state on writes + $self->{__client_socket} = $client; + + $self->load_plugins; my $rc = $self->start_conversation; return if $rc != DONE; # this should really be the loop and read_input should just get one line; I think - $self->read_input; } @@ -104,6 +105,12 @@ sub read_input { sub respond { my ($self, $code, @messages) = @_; my $buf = ''; + + if ( !$self->check_socket() ) { + $self->log(LOGERROR, "Lost connection to client, cannot send response."); + return(0); + } + while (my $msg = shift @messages) { my $line = $code . (@messages?"-":" ").$msg; $self->log(LOGINFO, $line); @@ -161,4 +168,12 @@ sub tcpenv { return ($TCPLOCALIP, $TCPREMOTEIP, $TCPREMOTEHOST || "Unknown"); } +sub check_socket() { + my $self = shift; + + return 1 if ( $self->{__client_socket}->connected ); + + return 0; +} + 1; diff --git a/lib/Qpsmtpd/TcpServer/Prefork.pm b/lib/Qpsmtpd/TcpServer/Prefork.pm index 96d1753..28f60dc 100644 --- a/lib/Qpsmtpd/TcpServer/Prefork.pm +++ b/lib/Qpsmtpd/TcpServer/Prefork.pm @@ -48,6 +48,12 @@ sub read_input { sub respond { my ($self, $code, @messages) = @_; + + if ( !$self->check_socket() ) { + $self->log(LOGERROR, "Lost connection to client, cannot send response."); + return(0); + } + while (my $msg = shift @messages) { my $line = $code . (@messages?"-":" ").$msg; $self->log(LOGINFO, $line); diff --git a/qpsmtpd-forkserver b/qpsmtpd-forkserver index 40de277..c22ff5c 100755 --- a/qpsmtpd-forkserver +++ b/qpsmtpd-forkserver @@ -316,7 +316,7 @@ while (1) { remote_ip => $ENV{TCPREMOTEIP}, remote_port => $port, ); - $qpsmtpd->run(); + $qpsmtpd->run($client); $qpsmtpd->run_hooks("post-connection"); $qpsmtpd->connection->reset; diff --git a/qpsmtpd-prefork b/qpsmtpd-prefork index 360b414..600133c 100755 --- a/qpsmtpd-prefork +++ b/qpsmtpd-prefork @@ -691,7 +691,7 @@ sub qpsmtpd_session { remote_ip => $ENV{TCPREMOTEIP}, remote_port => $client->peerport, ); - $qpsmtpd->run(); + $qpsmtpd->run($client); $qpsmtpd->run_hooks("post-connection"); $qpsmtpd->connection->reset; };