… | |
… | |
83 | waiting for data. |
83 | waiting for data. |
84 | |
84 | |
85 | =item on_error => $cb->($self) |
85 | =item on_error => $cb->($self) |
86 | |
86 | |
87 | This is the fatal error callback, that is called when, well, a fatal error |
87 | This is the fatal error callback, that is called when, well, a fatal error |
88 | ocurs, such as not being able to resolve the hostname, failure to connect |
88 | occurs, such as not being able to resolve the hostname, failure to connect |
89 | or a read error. |
89 | or a read error. |
90 | |
90 | |
91 | The object will not be in a usable state when this callback has been |
91 | The object will not be in a usable state when this callback has been |
92 | called. |
92 | called. |
93 | |
93 | |
… | |
… | |
102 | |
102 | |
103 | This sets the default read callback, which is called when data arrives |
103 | This sets the default read callback, which is called when data arrives |
104 | and no read request is in the queue. |
104 | and no read request is in the queue. |
105 | |
105 | |
106 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
106 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
107 | method or acces sthe C<$self->{rbuf}> member directly. |
107 | method or access the C<$self->{rbuf}> member directly. |
108 | |
108 | |
109 | When an EOF condition is detected then AnyEvent::Handle will first try to |
109 | When an EOF condition is detected then AnyEvent::Handle will first try to |
110 | feed all the remaining data to the queued callbacks and C<on_read> before |
110 | feed all the remaining data to the queued callbacks and C<on_read> before |
111 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
111 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
112 | error will be raised (with C<$!> set to C<EPIPE>). |
112 | error will be raised (with C<$!> set to C<EPIPE>). |
… | |
… | |
139 | |
139 | |
140 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
140 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
141 | buffer: If the write reaches this size or gets even samller it is |
141 | buffer: If the write reaches this size or gets even samller it is |
142 | considered empty. |
142 | considered empty. |
143 | |
143 | |
|
|
144 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
|
|
145 | |
|
|
146 | When this parameter is given, it enables TLS (SSL) mode, that means it |
|
|
147 | will start making tls handshake and will transparently encrypt/decrypt |
|
|
148 | data. |
|
|
149 | |
|
|
150 | For the TLS server side, use C<accept>, and for the TLS client side of a |
|
|
151 | connection, use C<connect> mode. |
|
|
152 | |
|
|
153 | You can also provide your own TLS connection object, but you have |
|
|
154 | to make sure that you call either C<Net::SSLeay::set_connect_state> |
|
|
155 | or C<Net::SSLeay::set_accept_state> on it before you pass it to |
|
|
156 | AnyEvent::Handle. |
|
|
157 | |
|
|
158 | =item tls_ctx => $ssl_ctx |
|
|
159 | |
|
|
160 | Use the given Net::SSLeay::CTX object to create the new TLS connection |
|
|
161 | (unless a connection object was specified directly). If this parameter is |
|
|
162 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
|
|
163 | |
144 | =back |
164 | =back |
145 | |
165 | |
146 | =cut |
166 | =cut |
147 | |
167 | |
148 | sub new { |
168 | sub new { |
… | |
… | |
151 | my $self = bless { @_ }, $class; |
171 | my $self = bless { @_ }, $class; |
152 | |
172 | |
153 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
173 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
154 | |
174 | |
155 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
175 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
|
|
176 | |
|
|
177 | if ($self->{tls}) { |
|
|
178 | require Net::SSLeay; |
|
|
179 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
|
|
180 | } |
156 | |
181 | |
157 | $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; |
182 | $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; |
158 | $self->on_error (delete $self->{on_error}) if $self->{on_error}; |
183 | $self->on_error (delete $self->{on_error}) if $self->{on_error}; |
159 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
184 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
160 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
185 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
… | |
… | |
225 | for reading. |
250 | for reading. |
226 | |
251 | |
227 | The write queue is very simple: you can add data to its end, and |
252 | The write queue is very simple: you can add data to its end, and |
228 | AnyEvent::Handle will automatically try to get rid of it for you. |
253 | AnyEvent::Handle will automatically try to get rid of it for you. |
229 | |
254 | |
230 | When data could be writtena nd the write buffer is shorter then the low |
255 | When data could be written and the write buffer is shorter then the low |
231 | water mark, the C<on_drain> callback will be invoked. |
256 | water mark, the C<on_drain> callback will be invoked. |
232 | |
257 | |
233 | =over 4 |
258 | =over 4 |
234 | |
259 | |
235 | =item $handle->on_drain ($cb) |
260 | =item $handle->on_drain ($cb) |
… | |
… | |
463 | Append the given callback to the end of the queue (C<push_read>) or |
488 | Append the given callback to the end of the queue (C<push_read>) or |
464 | prepend it (C<unshift_read>). |
489 | prepend it (C<unshift_read>). |
465 | |
490 | |
466 | The callback is called each time some additional read data arrives. |
491 | The callback is called each time some additional read data arrives. |
467 | |
492 | |
468 | It must check wether enough data is in the read buffer already. |
493 | It must check whether enough data is in the read buffer already. |
469 | |
494 | |
470 | If not enough data is available, it must return the empty list or a false |
495 | If not enough data is available, it must return the empty list or a false |
471 | value, in which case it will be called repeatedly until enough data is |
496 | value, in which case it will be called repeatedly until enough data is |
472 | available (or an error condition is detected). |
497 | available (or an error condition is detected). |
473 | |
498 | |
… | |
… | |
615 | } |
640 | } |
616 | }); |
641 | }); |
617 | } |
642 | } |
618 | } |
643 | } |
619 | |
644 | |
|
|
645 | sub _dotls { |
|
|
646 | my ($self) = @_; |
|
|
647 | |
|
|
648 | if (length $self->{tls_wbuf}) { |
|
|
649 | my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf}); |
|
|
650 | substr $self->{tls_wbuf}, 0, $len, "" if $len > 0; |
|
|
651 | } |
|
|
652 | |
|
|
653 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { |
|
|
654 | $self->{wbuf} .= $buf; |
|
|
655 | $self->_drain_wbuf; |
|
|
656 | } |
|
|
657 | |
|
|
658 | if (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
|
|
659 | $self->{rbuf} .= $buf; |
|
|
660 | $self->_drain_rbuf; |
|
|
661 | } elsif ( |
|
|
662 | (my $err = Net::SSLeay::get_error ($self->{tls}, -1)) |
|
|
663 | != Net::SSLeay::ERROR_WANT_READ () |
|
|
664 | ) { |
|
|
665 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
|
|
666 | $self->error; |
|
|
667 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
|
|
668 | $! = &Errno::EIO; |
|
|
669 | $self->error; |
|
|
670 | } |
|
|
671 | |
|
|
672 | # all others are fine for our purposes |
|
|
673 | } |
|
|
674 | } |
|
|
675 | |
|
|
676 | # TODO: maybe document... |
|
|
677 | sub starttls { |
|
|
678 | my ($self, $ssl, $ctx) = @_; |
|
|
679 | |
|
|
680 | if ($ssl eq "accept") { |
|
|
681 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
682 | Net::SSLeay::set_accept_state ($ssl); |
|
|
683 | } elsif ($ssl eq "connect") { |
|
|
684 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
685 | Net::SSLeay::set_connect_state ($ssl); |
|
|
686 | } |
|
|
687 | |
|
|
688 | $self->{tls} = $ssl; |
|
|
689 | |
|
|
690 | # basically, this is deep magic (because SSL_read should have the same issues) |
|
|
691 | # but the openssl maintainers basically said: "trust us, it just works". |
|
|
692 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
|
|
693 | # and mismaintained ssleay-module doesn't even offer them). |
|
|
694 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
|
|
695 | (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
|
|
696 | | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
|
|
697 | |
|
|
698 | $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
699 | $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
700 | |
|
|
701 | Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); |
|
|
702 | |
|
|
703 | $self->{filter_w} = sub { |
|
|
704 | $_[0]{tls_wbuf} .= ${$_[1]}; |
|
|
705 | &_dotls; |
|
|
706 | }; |
|
|
707 | $self->{filter_r} = sub { |
|
|
708 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
|
|
709 | &_dotls; |
|
|
710 | }; |
|
|
711 | } |
|
|
712 | |
|
|
713 | sub DESTROY { |
|
|
714 | my $self = shift; |
|
|
715 | |
|
|
716 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
|
|
717 | } |
|
|
718 | |
|
|
719 | =item AnyEvent::Handle::TLS_CTX |
|
|
720 | |
|
|
721 | This function creates and returns the Net::SSLeay::CTX object used by |
|
|
722 | default for TLS mode. |
|
|
723 | |
|
|
724 | The context is created like this: |
|
|
725 | |
|
|
726 | Net::SSLeay::load_error_strings; |
|
|
727 | Net::SSLeay::SSLeay_add_ssl_algorithms; |
|
|
728 | Net::SSLeay::randomize; |
|
|
729 | |
|
|
730 | my $CTX = Net::SSLeay::CTX_new; |
|
|
731 | |
|
|
732 | Net::SSLeay::CTX_set_options $CTX, Net::SSLeay::OP_ALL |
|
|
733 | |
|
|
734 | =cut |
|
|
735 | |
|
|
736 | our $TLS_CTX; |
|
|
737 | |
|
|
738 | sub TLS_CTX() { |
|
|
739 | $TLS_CTX || do { |
|
|
740 | require Net::SSLeay; |
|
|
741 | |
|
|
742 | Net::SSLeay::load_error_strings (); |
|
|
743 | Net::SSLeay::SSLeay_add_ssl_algorithms (); |
|
|
744 | Net::SSLeay::randomize (); |
|
|
745 | |
|
|
746 | $TLS_CTX = Net::SSLeay::CTX_new (); |
|
|
747 | |
|
|
748 | Net::SSLeay::CTX_set_options ($TLS_CTX, Net::SSLeay::OP_ALL ()); |
|
|
749 | |
|
|
750 | $TLS_CTX |
|
|
751 | } |
|
|
752 | } |
|
|
753 | |
620 | =back |
754 | =back |
621 | |
755 | |
622 | =head1 AUTHOR |
756 | =head1 AUTHOR |
623 | |
757 | |
624 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
758 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |