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


In reply to Re: TCP - SSL proxy problems by ferrency
in thread TCP - SSL proxy problems by ferrency

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.