in reply to TCP - SSL proxy problems

Hello,

While waiting for answers to my earlier question, I implemented what I think is a working version of the "remarkably more difficult" solution, which is shown below. I'd like more eyes on this code, however, becuase "it has worked all afternoon" isn't enough to convince me there aren't potential race conditions or deadlocks lurking in there somewhere. Hopefully if it's not too broken, someone else can derive some value from it.

The basic technique is as follows:

And now for the code:
my $asock = new IO::Socket::INET (..., Listen => 5); # accepting socket my $sslsock = new IO::Socket::SSL(..., PeerAddr => ...,); # SSL socket $sslsock->blocking(0); while (1) { my $csock = $asock->accept(); # Accept $csock, connecting socket $csock->blocking(0); my $readsel = new IO::Select($csock, $sslsock); my $writesel = new IO::Select(); # For convenience: translation table for socket to proxy from/to my %proxy = ( $csock => $sslsock, $sslsock => $csock, ); my %buffer; OUTER: while (1) { my ($read, $write, $err) = IO::Select::select($readsel, $writesel, $readsel); foreach (@$read) { my $b; while (defined(my $line = <$_>)) { $b .= $line; } if (!defined($b) && ($_ == $csock or !$_->connected())) { # SSL socket seems to return undef sometimes even # while staying connected... INET socket returns undef + if # read and disconnected print "read undef: lost socket connection\n"; last OUTER; } $buffer{$proxy{$_}} .= $b; $writesel->add($proxy{$_}); } foreach (@$write) { if (exists($buffer{$_})) { # Do we need to handle partial writes here? $_->print($buffer{$_}); delete $buffer{$_}; } $writesel->remove($_); } if (@$err) { print "Error conditions: ", join (', ', @$err), "\n"; last OUTER; } } }
Does anyone see anything wrong with this? If not, I'm good to go, thanks :) But I still think there should be a more simple perlish solution to attaching the two sockets together.

Thanks,

Alan