diff --git a/lib/POE/Component/SSLify/ClientHandle.pm b/lib/POE/Component/SSLify/ClientHandle.pm index f583cd3..6ee1ffa 100644 --- a/lib/POE/Component/SSLify/ClientHandle.pm +++ b/lib/POE/Component/SSLify/ClientHandle.pm @@ -28,14 +28,15 @@ # die_if_ssl_error won't die on non-blocking errors. We don't need to call connect() # again, because OpenSSL I/O functions (read, write, ...) can handle that entirely # by self (it's needed to connect() once to determine connection type). - my $resp = Net::SSLeay::connect( $ssl ) or die_if_ssl_error( 'ssl connect' ); - + my $res = Net::SSLeay::connect( $ssl ) or die_if_ssl_error( 'ssl connect' ); +warn "Net::SSLeay::connect(TIEHANDLE) -> $res"; my $self = bless { 'ssl' => $ssl, 'ctx' => $ctx, 'socket' => $socket, 'fileno' => $fileno, 'client' => 1, + 'status' => $res, }, $class; return $self; diff --git a/lib/POE/Component/SSLify/ServerHandle.pm b/lib/POE/Component/SSLify/ServerHandle.pm index f6bdbcd..b038c23 100644 --- a/lib/POE/Component/SSLify/ServerHandle.pm +++ b/lib/POE/Component/SSLify/ServerHandle.pm @@ -19,16 +19,45 @@ # die_if_ssl_error won't die on non-blocking errors. We don't need to call accept() # again, because OpenSSL I/O functions (read, write, ...) can handle that entirely # by self (it's needed to accept() once to determine connection type). - my $err = Net::SSLeay::accept( $ssl ) and die_if_ssl_error( 'ssl accept' ); - + my $res = Net::SSLeay::accept( $ssl ) and die_if_ssl_error( 'ssl accept' ); +warn "Net::SSLeay::accept(TIEHANDLE) -> $res"; my $self = bless { 'ssl' => $ssl, 'ctx' => $ctx, 'socket' => $socket, 'fileno' => $fileno, + 'status' => $res, }, $class; return $self; +} + +sub _check_status { + my $self = shift; + my $method = shift; + + # Okay, is negotiation done? + # http://www.openssl.org/docs/ssl/SSL_connect.html#RETURN_VALUES + if ( $self->{'status'} == -1 ) { + # client or server? + my $res; + if ( exists $self->{'client'} ) { + $res = Net::SSLeay::connect( $self->{'ssl'} ); + warn "Net::SSLeay::connect($method) -> $res"; + } else { + $res = Net::SSLeay::accept( $self->{'ssl'} ); + warn "Net::SSLeay::accept($method) -> $res"; + } + + if ( $res == 0 ) { + # TODO error? + } elsif ( $res == 1 ) { + $self->{'status'} = 1; + + # TODO call the hook function for successful connect + warn "CALLING HOOK FUNCTION for " . ( exists $self->{'client'} ? 'CLIENT' : 'SERVER' ); + } + } } # Read something from the socket @@ -38,6 +67,9 @@ # Get the pointers to buffer, length, and the offset my( $buf, $len, $offset ) = \( @_ ); + + # Check connection status + $self->_check_status( 'READ' ); # If we have no offset, replace the buffer with some input if ( ! defined $$offset ) { @@ -75,6 +107,9 @@ sub WRITE { # Get ourself + buffer + length + offset to write my( $self, $buf, $len, $offset ) = @_; + + # Check connection status + $self->_check_status( 'WRITE' ); # If we have nothing to offset, then start from the beginning if ( ! defined $offset ) {