… | |
… | |
11 | |
11 | |
12 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
12 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
13 | |
13 | |
14 | =cut |
14 | =cut |
15 | |
15 | |
16 | our $VERSION = 4.86; |
16 | our $VERSION = 4.87; |
17 | |
17 | |
18 | =head1 SYNOPSIS |
18 | =head1 SYNOPSIS |
19 | |
19 | |
20 | use AnyEvent; |
20 | use AnyEvent; |
21 | use AnyEvent::Handle; |
21 | use AnyEvent::Handle; |
… | |
… | |
84 | C<AnyEvent::Socket::tcp_connect>. The C<$host> additionally becomes the |
84 | C<AnyEvent::Socket::tcp_connect>. The C<$host> additionally becomes the |
85 | default C<peername>. |
85 | default C<peername>. |
86 | |
86 | |
87 | You have to specify either this parameter, or C<fh>, above. |
87 | You have to specify either this parameter, or C<fh>, above. |
88 | |
88 | |
|
|
89 | It is possible to push requests on the read and write queues, and modify |
|
|
90 | properties of the stream, even while AnyEvent::Handle is connecting. |
|
|
91 | |
89 | When this parameter is specified, then the C<on_prepare>, |
92 | When this parameter is specified, then the C<on_prepare>, |
90 | C<on_connect_error> and C<on_connect> callbacks will be called under the |
93 | C<on_connect_error> and C<on_connect> callbacks will be called under the |
91 | appropriate circumstances: |
94 | appropriate circumstances: |
92 | |
95 | |
93 | =over 4 |
96 | =over 4 |
… | |
… | |
97 | This (rarely used) callback is called before a new connection is |
100 | This (rarely used) callback is called before a new connection is |
98 | attempted, but after the file handle has been created. It could be used to |
101 | attempted, but after the file handle has been created. It could be used to |
99 | prepare the file handle with parameters required for the actual connect |
102 | prepare the file handle with parameters required for the actual connect |
100 | (as opposed to settings that can be changed when the connection is already |
103 | (as opposed to settings that can be changed when the connection is already |
101 | established). |
104 | established). |
|
|
105 | |
|
|
106 | The return value of this callback should be the connect timeout value in |
|
|
107 | seconds (or C<0>, or C<undef>, or the empty list, to indicate the default |
|
|
108 | timeout is to be used). |
102 | |
109 | |
103 | =item on_connect => $cb->($handle, $host, $port, $retry->()) |
110 | =item on_connect => $cb->($handle, $host, $port, $retry->()) |
104 | |
111 | |
105 | This callback is called when a connection has been successfully established. |
112 | This callback is called when a connection has been successfully established. |
106 | |
113 | |
… | |
… | |
441 | } else { |
448 | } else { |
442 | if ($self->{on_connect_error}) { |
449 | if ($self->{on_connect_error}) { |
443 | $self->{on_connect_error}($self, "$!"); |
450 | $self->{on_connect_error}($self, "$!"); |
444 | $self->destroy; |
451 | $self->destroy; |
445 | } else { |
452 | } else { |
446 | $self->fatal ($!, 1); |
453 | $self->_error ($!, 1); |
447 | } |
454 | } |
448 | } |
455 | } |
449 | }, |
456 | }, |
450 | sub { |
457 | sub { |
451 | local $self->{fh} = $_[0]; |
458 | local $self->{fh} = $_[0]; |
452 | |
459 | |
|
|
460 | $self->{on_prepare} |
453 | $self->{on_prepare}->($self) |
461 | ? $self->{on_prepare}->($self) |
454 | if $self->{on_prepare}; |
462 | : () |
455 | } |
463 | } |
456 | ); |
464 | ); |
457 | } |
465 | } |
458 | |
466 | |
459 | } else { |
467 | } else { |
… | |
… | |
478 | |
486 | |
479 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
487 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
480 | |
488 | |
481 | $self->start_read |
489 | $self->start_read |
482 | if $self->{on_read} || @{ $self->{_queue} }; |
490 | if $self->{on_read} || @{ $self->{_queue} }; |
|
|
491 | |
|
|
492 | $self->_drain_wbuf; |
483 | } |
493 | } |
484 | |
494 | |
485 | #sub _shutdown { |
495 | #sub _shutdown { |
486 | # my ($self) = @_; |
496 | # my ($self) = @_; |
487 | # |
497 | # |
… | |
… | |
740 | ->($self, @_); |
750 | ->($self, @_); |
741 | } |
751 | } |
742 | |
752 | |
743 | if ($self->{tls}) { |
753 | if ($self->{tls}) { |
744 | $self->{_tls_wbuf} .= $_[0]; |
754 | $self->{_tls_wbuf} .= $_[0]; |
745 | |
755 | &_dotls ($self) if $self->{fh}; |
746 | &_dotls ($self); |
|
|
747 | } else { |
756 | } else { |
748 | $self->{wbuf} .= $_[0]; |
757 | $self->{wbuf} .= $_[0]; |
749 | $self->_drain_wbuf if $self->{fh}; |
758 | $self->_drain_wbuf if $self->{fh}; |
750 | } |
759 | } |
751 | } |
760 | } |
752 | |
761 | |
753 | =item $handle->push_write (type => @args) |
762 | =item $handle->push_write (type => @args) |
… | |
… | |
985 | } |
994 | } |
986 | |
995 | |
987 | while () { |
996 | while () { |
988 | # we need to use a separate tls read buffer, as we must not receive data while |
997 | # we need to use a separate tls read buffer, as we must not receive data while |
989 | # we are draining the buffer, and this can only happen with TLS. |
998 | # we are draining the buffer, and this can only happen with TLS. |
990 | $self->{rbuf} .= delete $self->{_tls_rbuf} if exists $self->{_tls_rbuf}; |
999 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
|
|
1000 | if exists $self->{_tls_rbuf}; |
991 | |
1001 | |
992 | my $len = length $self->{rbuf}; |
1002 | my $len = length $self->{rbuf}; |
993 | |
1003 | |
994 | if (my $cb = shift @{ $self->{_queue} }) { |
1004 | if (my $cb = shift @{ $self->{_queue} }) { |
995 | unless ($cb->($self)) { |
1005 | unless ($cb->($self)) { |
996 | if ($self->{_eof}) { |
1006 | # no progress can be made |
997 | # no progress can be made (not enough data and no data forthcoming) |
1007 | # (not enough data and no data forthcoming) |
998 | $self->_error (Errno::EPIPE, 1), return; |
1008 | $self->_error (Errno::EPIPE, 1), return |
999 | } |
1009 | if $self->{_eof}; |
1000 | |
1010 | |
1001 | unshift @{ $self->{_queue} }, $cb; |
1011 | unshift @{ $self->{_queue} }, $cb; |
1002 | last; |
1012 | last; |
1003 | } |
1013 | } |
1004 | } elsif ($self->{on_read}) { |
1014 | } elsif ($self->{on_read}) { |
… | |
… | |
1024 | last; |
1034 | last; |
1025 | } |
1035 | } |
1026 | } |
1036 | } |
1027 | |
1037 | |
1028 | if ($self->{_eof}) { |
1038 | if ($self->{_eof}) { |
1029 | if ($self->{on_eof}) { |
1039 | $self->{on_eof} |
1030 | $self->{on_eof}($self) |
1040 | ? $self->{on_eof}($self) |
1031 | } else { |
|
|
1032 | $self->_error (0, 1, "Unexpected end-of-file"); |
1041 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1033 | } |
1042 | |
|
|
1043 | return; |
1034 | } |
1044 | } |
1035 | |
1045 | |
1036 | # may need to restart read watcher |
1046 | # may need to restart read watcher |
1037 | unless ($self->{_rw}) { |
1047 | unless ($self->{_rw}) { |
1038 | $self->start_read |
1048 | $self->start_read |
… | |
… | |
1649 | The TLS connection object will end up in C<< $handle->{tls} >>, the TLS |
1659 | The TLS connection object will end up in C<< $handle->{tls} >>, the TLS |
1650 | context in C<< $handle->{tls_ctx} >> after this call and can be used or |
1660 | context in C<< $handle->{tls_ctx} >> after this call and can be used or |
1651 | changed to your liking. Note that the handshake might have already started |
1661 | changed to your liking. Note that the handshake might have already started |
1652 | when this function returns. |
1662 | when this function returns. |
1653 | |
1663 | |
1654 | If it an error to start a TLS handshake more than once per |
1664 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1655 | AnyEvent::Handle object (this is due to bugs in OpenSSL). |
1665 | handshakes on the same stream. Best do not attempt to use the stream after |
|
|
1666 | stopping TLS. |
1656 | |
1667 | |
1657 | =cut |
1668 | =cut |
1658 | |
1669 | |
1659 | our %TLS_CACHE; #TODO not yet documented, should we? |
1670 | our %TLS_CACHE; #TODO not yet documented, should we? |
1660 | |
1671 | |
1661 | sub starttls { |
1672 | sub starttls { |
1662 | my ($self, $ssl, $ctx) = @_; |
1673 | my ($self, $tls, $ctx) = @_; |
|
|
1674 | |
|
|
1675 | Carp::croak "It is an error to call starttls on an AnyEvent::Handle object while TLS is already active, caught" |
|
|
1676 | if $self->{tls}; |
|
|
1677 | |
|
|
1678 | $self->{tls} = $tls; |
|
|
1679 | $self->{tls_ctx} = $ctx if @_ > 2; |
|
|
1680 | |
|
|
1681 | return unless $self->{fh}; |
1663 | |
1682 | |
1664 | require Net::SSLeay; |
1683 | require Net::SSLeay; |
1665 | |
|
|
1666 | Carp::croak "it is an error to call starttls more than once on an AnyEvent::Handle object" |
|
|
1667 | if $self->{tls}; |
|
|
1668 | |
1684 | |
1669 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1685 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1670 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1686 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1671 | |
1687 | |
|
|
1688 | $tls = $self->{tls}; |
1672 | $ctx ||= $self->{tls_ctx}; |
1689 | $ctx = $self->{tls_ctx}; |
1673 | |
1690 | |
1674 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1691 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1675 | |
1692 | |
1676 | if ("HASH" eq ref $ctx) { |
1693 | if ("HASH" eq ref $ctx) { |
1677 | require AnyEvent::TLS; |
1694 | require AnyEvent::TLS; |
… | |
… | |
1683 | $ctx = new AnyEvent::TLS %$ctx; |
1700 | $ctx = new AnyEvent::TLS %$ctx; |
1684 | } |
1701 | } |
1685 | } |
1702 | } |
1686 | |
1703 | |
1687 | $self->{tls_ctx} = $ctx || TLS_CTX (); |
1704 | $self->{tls_ctx} = $ctx || TLS_CTX (); |
1688 | $self->{tls} = $ssl = $self->{tls_ctx}->_get_session ($ssl, $self, $self->{peername}); |
1705 | $self->{tls} = $tls = $self->{tls_ctx}->_get_session ($tls, $self, $self->{peername}); |
1689 | |
1706 | |
1690 | # basically, this is deep magic (because SSL_read should have the same issues) |
1707 | # basically, this is deep magic (because SSL_read should have the same issues) |
1691 | # but the openssl maintainers basically said: "trust us, it just works". |
1708 | # but the openssl maintainers basically said: "trust us, it just works". |
1692 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
1709 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
1693 | # and mismaintained ssleay-module doesn't even offer them). |
1710 | # and mismaintained ssleay-module doesn't even offer them). |
… | |
… | |
1700 | # and we drive openssl fully in blocking mode here. Or maybe we don't - openssl seems to |
1717 | # and we drive openssl fully in blocking mode here. Or maybe we don't - openssl seems to |
1701 | # have identity issues in that area. |
1718 | # have identity issues in that area. |
1702 | # Net::SSLeay::CTX_set_mode ($ssl, |
1719 | # Net::SSLeay::CTX_set_mode ($ssl, |
1703 | # (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
1720 | # (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
1704 | # | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
1721 | # | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
1705 | Net::SSLeay::CTX_set_mode ($ssl, 1|2); |
1722 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1706 | |
1723 | |
1707 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1724 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1708 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1725 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1709 | |
1726 | |
1710 | Net::SSLeay::set_bio ($ssl, $self->{_rbio}, $self->{_wbio}); |
1727 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1711 | |
1728 | |
1712 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1729 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1713 | if $self->{on_starttls}; |
1730 | if $self->{on_starttls}; |
1714 | |
1731 | |
1715 | &_dotls; # need to trigger the initial handshake |
1732 | &_dotls; # need to trigger the initial handshake |
… | |
… | |
1718 | |
1735 | |
1719 | =item $handle->stoptls |
1736 | =item $handle->stoptls |
1720 | |
1737 | |
1721 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1738 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1722 | sending a close notify to the other side, but since OpenSSL doesn't |
1739 | sending a close notify to the other side, but since OpenSSL doesn't |
1723 | support non-blocking shut downs, it is not possible to re-use the stream |
1740 | support non-blocking shut downs, it is not guarenteed that you can re-use |
1724 | afterwards. |
1741 | the stream afterwards. |
1725 | |
1742 | |
1726 | =cut |
1743 | =cut |
1727 | |
1744 | |
1728 | sub stoptls { |
1745 | sub stoptls { |
1729 | my ($self) = @_; |
1746 | my ($self) = @_; |
… | |
… | |
1742 | sub _freetls { |
1759 | sub _freetls { |
1743 | my ($self) = @_; |
1760 | my ($self) = @_; |
1744 | |
1761 | |
1745 | return unless $self->{tls}; |
1762 | return unless $self->{tls}; |
1746 | |
1763 | |
1747 | $self->{tls_ctx}->_put_session (delete $self->{tls}); |
1764 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
|
|
1765 | if ref $self->{tls}; |
1748 | |
1766 | |
1749 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1767 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1750 | } |
1768 | } |
1751 | |
1769 | |
1752 | sub DESTROY { |
1770 | sub DESTROY { |
… | |
… | |
1803 | sub destroy { |
1821 | sub destroy { |
1804 | my ($self) = @_; |
1822 | my ($self) = @_; |
1805 | |
1823 | |
1806 | $self->DESTROY; |
1824 | $self->DESTROY; |
1807 | %$self = (); |
1825 | %$self = (); |
|
|
1826 | bless $self, "AnyEvent::Handle::destroyed"; |
|
|
1827 | } |
|
|
1828 | |
|
|
1829 | { |
|
|
1830 | package AnyEvent::Handle::destroyed; |
|
|
1831 | |
|
|
1832 | sub AUTOLOAD { |
|
|
1833 | #nop |
|
|
1834 | } |
1808 | } |
1835 | } |
1809 | |
1836 | |
1810 | =item AnyEvent::Handle::TLS_CTX |
1837 | =item AnyEvent::Handle::TLS_CTX |
1811 | |
1838 | |
1812 | This function creates and returns the AnyEvent::TLS object used by default |
1839 | This function creates and returns the AnyEvent::TLS object used by default |