Author: msergeant
Date: Fri Sep 26 10:40:04 2008
New Revision: 946

Modified:
   trunk/lib/Qpsmtpd/TcpServer.pm
   trunk/lib/Qpsmtpd/TcpServer/Prefork.pm
   trunk/qpsmtpd-forkserver
   trunk/qpsmtpd-prefork

Log:
Fix to check client is writable before writing to it.


Modified: trunk/lib/Qpsmtpd/TcpServer.pm
==============================================================================
--- trunk/lib/Qpsmtpd/TcpServer.pm      (original)
+++ trunk/lib/Qpsmtpd/TcpServer.pm      Fri Sep 26 10:40:04 2008
@@ -67,16 +67,17 @@
 }
 
 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 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 @@
   return ($TCPLOCALIP, $TCPREMOTEIP, $TCPREMOTEHOST || "Unknown");
 }
 
+sub check_socket() {
+  my $self = shift;
+
+  return 1 if ( $self->{__client_socket}->connected );
+ 
+  return 0;
+}
+
 1;

Modified: trunk/lib/Qpsmtpd/TcpServer/Prefork.pm
==============================================================================
--- trunk/lib/Qpsmtpd/TcpServer/Prefork.pm      (original)
+++ trunk/lib/Qpsmtpd/TcpServer/Prefork.pm      Fri Sep 26 10:40:04 2008
@@ -48,6 +48,12 @@
 
 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);

Modified: trunk/qpsmtpd-forkserver
==============================================================================
--- trunk/qpsmtpd-forkserver    (original)
+++ trunk/qpsmtpd-forkserver    Fri Sep 26 10:40:04 2008
@@ -316,7 +316,7 @@
        remote_ip   => $ENV{TCPREMOTEIP},
        remote_port => $port,
       );
-    $qpsmtpd->run();
+    $qpsmtpd->run($client);
     
     $qpsmtpd->run_hooks("post-connection");
     $qpsmtpd->connection->reset;

Modified: trunk/qpsmtpd-prefork
==============================================================================
--- trunk/qpsmtpd-prefork       (original)
+++ trunk/qpsmtpd-prefork       Fri Sep 26 10:40:04 2008
@@ -691,7 +691,7 @@
                                    remote_ip   => $ENV{TCPREMOTEIP},
                                    remote_port => $client->peerport,
                                   );
-        $qpsmtpd->run();
+        $qpsmtpd->run($client);
         $qpsmtpd->run_hooks("post-connection");
         $qpsmtpd->connection->reset;
     };

Reply via email to