ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
Revision: 1.26
Committed: Sat May 24 15:20:46 2008 UTC (16 years ago) by root
Branch: MAIN
Changes since 1.25: +5 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 package AnyEvent::Handle;
2    
3 elmex 1.6 no warnings;
4 elmex 1.1 use strict;
5    
6 root 1.8 use AnyEvent ();
7     use AnyEvent::Util ();
8     use Scalar::Util ();
9     use Carp ();
10     use Fcntl ();
11 elmex 1.1 use Errno qw/EAGAIN EINTR/;
12    
13     =head1 NAME
14    
15 root 1.22 AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent
16 elmex 1.1
17 root 1.15 This module is experimental.
18    
19 elmex 1.1 =cut
20    
21 root 1.15 our $VERSION = '0.04';
22 elmex 1.1
23     =head1 SYNOPSIS
24    
25     use AnyEvent;
26     use AnyEvent::Handle;
27    
28     my $cv = AnyEvent->condvar;
29    
30     my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN);
31    
32 root 1.8 #TODO
33 elmex 1.1
34 elmex 1.2 # or use the constructor to pass the callback:
35    
36     my $ae_fh2 =
37     AnyEvent::Handle->new (
38     fh => \*STDIN,
39     on_eof => sub {
40     $cv->broadcast;
41     },
42 root 1.8 #TODO
43 elmex 1.2 );
44    
45 elmex 1.1 $cv->wait;
46    
47     =head1 DESCRIPTION
48    
49 root 1.8 This module is a helper module to make it easier to do event-based I/O on
50 elmex 1.13 filehandles. For utility functions for doing non-blocking connects and accepts
51     on sockets see L<AnyEvent::Util>.
52 root 1.8
53     In the following, when the documentation refers to of "bytes" then this
54     means characters. As sysread and syswrite are used for all I/O, their
55     treatment of characters applies to this module as well.
56 elmex 1.1
57 root 1.8 All callbacks will be invoked with the handle object as their first
58     argument.
59 elmex 1.1
60     =head1 METHODS
61    
62     =over 4
63    
64     =item B<new (%args)>
65    
66 root 1.8 The constructor supports these arguments (all as key => value pairs).
67 elmex 1.1
68     =over 4
69    
70 root 1.8 =item fh => $filehandle [MANDATORY]
71 elmex 1.1
72     The filehandle this L<AnyEvent::Handle> object will operate on.
73    
74 root 1.8 NOTE: The filehandle will be set to non-blocking (using
75     AnyEvent::Util::fh_nonblocking).
76    
77 root 1.16 =item on_eof => $cb->($self)
78 root 1.10
79     Set the callback to be called on EOF.
80 root 1.8
81 root 1.16 While not mandatory, it is highly recommended to set an eof callback,
82     otherwise you might end up with a closed socket while you are still
83     waiting for data.
84    
85 root 1.10 =item on_error => $cb->($self)
86    
87     This is the fatal error callback, that is called when, well, a fatal error
88 elmex 1.20 occurs, such as not being able to resolve the hostname, failure to connect
89 root 1.10 or a read error.
90 root 1.8
91     The object will not be in a usable state when this callback has been
92     called.
93    
94 root 1.10 On callback entrance, the value of C<$!> contains the operating system
95 root 1.8 error (or C<ENOSPC> or C<EPIPE>).
96    
97 root 1.10 While not mandatory, it is I<highly> recommended to set this callback, as
98     you will not be notified of errors otherwise. The default simply calls
99     die.
100 root 1.8
101     =item on_read => $cb->($self)
102    
103     This sets the default read callback, which is called when data arrives
104 root 1.10 and no read request is in the queue.
105 root 1.8
106     To access (and remove data from) the read buffer, use the C<< ->rbuf >>
107 elmex 1.20 method or access the C<$self->{rbuf}> member directly.
108 root 1.8
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
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>).
113 elmex 1.1
114 root 1.8 =item on_drain => $cb->()
115 elmex 1.1
116 root 1.8 This sets the callback that is called when the write buffer becomes empty
117     (or when the callback is set and the buffer is empty already).
118 elmex 1.1
119 root 1.8 To append to the write buffer, use the C<< ->push_write >> method.
120 elmex 1.2
121 root 1.8 =item rbuf_max => <bytes>
122 elmex 1.2
123 root 1.8 If defined, then a fatal error will be raised (with C<$!> set to C<ENOSPC>)
124     when the read buffer ever (strictly) exceeds this size. This is useful to
125     avoid denial-of-service attacks.
126 elmex 1.2
127 root 1.8 For example, a server accepting connections from untrusted sources should
128     be configured to accept only so-and-so much data that it cannot act on
129     (for example, when expecting a line, an attacker could send an unlimited
130     amount of data without a callback ever being called as long as the line
131     isn't finished).
132 elmex 1.2
133 root 1.8 =item read_size => <bytes>
134 elmex 1.2
135 root 1.8 The default read block size (the amount of bytes this module will try to read
136     on each [loop iteration). Default: C<4096>.
137    
138     =item low_water_mark => <bytes>
139    
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
142     considered empty.
143 elmex 1.2
144 root 1.19 =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 root 1.26 TLS mode requires Net::SSLeay to be installed (it will be loaded
151     automatically when you try to create a TLS handle).
152    
153 root 1.19 For the TLS server side, use C<accept>, and for the TLS client side of a
154     connection, use C<connect> mode.
155    
156     You can also provide your own TLS connection object, but you have
157     to make sure that you call either C<Net::SSLeay::set_connect_state>
158     or C<Net::SSLeay::set_accept_state> on it before you pass it to
159     AnyEvent::Handle.
160    
161 root 1.26 See the C<starttls> method if you need to start TLs negotiation later.
162    
163 root 1.19 =item tls_ctx => $ssl_ctx
164    
165     Use the given Net::SSLeay::CTX object to create the new TLS connection
166     (unless a connection object was specified directly). If this parameter is
167     missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>.
168    
169 elmex 1.1 =back
170    
171     =cut
172    
173     sub new {
174 root 1.8 my $class = shift;
175    
176     my $self = bless { @_ }, $class;
177    
178     $self->{fh} or Carp::croak "mandatory argument fh is missing";
179    
180     AnyEvent::Util::fh_nonblocking $self->{fh}, 1;
181 elmex 1.1
182 root 1.19 if ($self->{tls}) {
183     require Net::SSLeay;
184     $self->starttls (delete $self->{tls}, delete $self->{tls_ctx});
185     }
186    
187 root 1.16 $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof};
188 root 1.10 $self->on_error (delete $self->{on_error}) if $self->{on_error};
189 root 1.8 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain};
190     $self->on_read (delete $self->{on_read} ) if $self->{on_read};
191 elmex 1.1
192 root 1.10 $self->start_read;
193    
194 root 1.8 $self
195     }
196 elmex 1.2
197 root 1.8 sub _shutdown {
198     my ($self) = @_;
199 elmex 1.2
200 root 1.8 delete $self->{rw};
201     delete $self->{ww};
202     delete $self->{fh};
203     }
204    
205     sub error {
206     my ($self) = @_;
207    
208     {
209     local $!;
210     $self->_shutdown;
211 elmex 1.1 }
212    
213 root 1.10 if ($self->{on_error}) {
214     $self->{on_error}($self);
215     } else {
216     die "AnyEvent::Handle uncaught fatal error: $!";
217     }
218 elmex 1.1 }
219    
220 root 1.8 =item $fh = $handle->fh
221 elmex 1.1
222 root 1.22 This method returns the file handle of the L<AnyEvent::Handle> object.
223 elmex 1.1
224     =cut
225    
226     sub fh { $_[0]->{fh} }
227    
228 root 1.8 =item $handle->on_error ($cb)
229 elmex 1.1
230 root 1.8 Replace the current C<on_error> callback (see the C<on_error> constructor argument).
231 elmex 1.1
232 root 1.8 =cut
233    
234     sub on_error {
235     $_[0]{on_error} = $_[1];
236     }
237    
238     =item $handle->on_eof ($cb)
239    
240     Replace the current C<on_eof> callback (see the C<on_eof> constructor argument).
241 elmex 1.1
242     =cut
243    
244 root 1.8 sub on_eof {
245     $_[0]{on_eof} = $_[1];
246     }
247    
248 root 1.9 #############################################################################
249    
250     =back
251    
252     =head2 WRITE QUEUE
253    
254     AnyEvent::Handle manages two queues per handle, one for writing and one
255     for reading.
256    
257     The write queue is very simple: you can add data to its end, and
258     AnyEvent::Handle will automatically try to get rid of it for you.
259    
260 elmex 1.20 When data could be written and the write buffer is shorter then the low
261 root 1.9 water mark, the C<on_drain> callback will be invoked.
262    
263     =over 4
264    
265 root 1.8 =item $handle->on_drain ($cb)
266    
267     Sets the C<on_drain> callback or clears it (see the description of
268     C<on_drain> in the constructor).
269    
270     =cut
271    
272     sub on_drain {
273 elmex 1.1 my ($self, $cb) = @_;
274    
275 root 1.8 $self->{on_drain} = $cb;
276    
277     $cb->($self)
278     if $cb && $self->{low_water_mark} >= length $self->{wbuf};
279     }
280    
281     =item $handle->push_write ($data)
282    
283     Queues the given scalar to be written. You can push as much data as you
284     want (only limited by the available memory), as C<AnyEvent::Handle>
285     buffers it independently of the kernel.
286    
287     =cut
288    
289 root 1.17 sub _drain_wbuf {
290     my ($self) = @_;
291 root 1.8
292     unless ($self->{ww}) {
293     Scalar::Util::weaken $self;
294     my $cb = sub {
295     my $len = syswrite $self->{fh}, $self->{wbuf};
296    
297     if ($len > 0) {
298     substr $self->{wbuf}, 0, $len, "";
299    
300     $self->{on_drain}($self)
301     if $self->{low_water_mark} >= length $self->{wbuf}
302     && $self->{on_drain};
303    
304     delete $self->{ww} unless length $self->{wbuf};
305     } elsif ($! != EAGAIN && $! != EINTR) {
306     $self->error;
307 elmex 1.1 }
308 root 1.8 };
309    
310     $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb);
311    
312     $cb->($self);
313     };
314     }
315    
316 root 1.17 sub push_write {
317     my $self = shift;
318    
319     if ($self->{filter_w}) {
320 root 1.18 $self->{filter_w}->($self, \$_[0]);
321 root 1.17 } else {
322     $self->{wbuf} .= $_[0];
323     $self->_drain_wbuf;
324     }
325     }
326    
327 root 1.8 #############################################################################
328    
329 root 1.9 =back
330    
331     =head2 READ QUEUE
332    
333     AnyEvent::Handle manages two queues per handle, one for writing and one
334     for reading.
335    
336     The read queue is more complex than the write queue. It can be used in two
337     ways, the "simple" way, using only C<on_read> and the "complex" way, using
338     a queue.
339    
340     In the simple case, you just install an C<on_read> callback and whenever
341     new data arrives, it will be called. You can then remove some data (if
342     enough is there) from the read buffer (C<< $handle->rbuf >>) if you want
343     or not.
344    
345     In the more complex case, you want to queue multiple callbacks. In this
346     case, AnyEvent::Handle will call the first queued callback each time new
347     data arrives and removes it when it has done its job (see C<push_read>,
348     below).
349    
350     This way you can, for example, push three line-reads, followed by reading
351     a chunk of data, and AnyEvent::Handle will execute them in order.
352    
353     Example 1: EPP protocol parser. EPP sends 4 byte length info, followed by
354     the specified number of bytes which give an XML datagram.
355    
356     # in the default state, expect some header bytes
357     $handle->on_read (sub {
358     # some data is here, now queue the length-header-read (4 octets)
359     shift->unshift_read_chunk (4, sub {
360     # header arrived, decode
361     my $len = unpack "N", $_[1];
362    
363     # now read the payload
364     shift->unshift_read_chunk ($len, sub {
365     my $xml = $_[1];
366     # handle xml
367     });
368     });
369     });
370    
371     Example 2: Implement a client for a protocol that replies either with
372     "OK" and another line or "ERROR" for one request, and 64 bytes for the
373     second request. Due tot he availability of a full queue, we can just
374     pipeline sending both requests and manipulate the queue as necessary in
375     the callbacks:
376    
377     # request one
378     $handle->push_write ("request 1\015\012");
379    
380     # we expect "ERROR" or "OK" as response, so push a line read
381     $handle->push_read_line (sub {
382     # if we got an "OK", we have to _prepend_ another line,
383     # so it will be read before the second request reads its 64 bytes
384     # which are already in the queue when this callback is called
385     # we don't do this in case we got an error
386     if ($_[1] eq "OK") {
387     $_[0]->unshift_read_line (sub {
388     my $response = $_[1];
389     ...
390     });
391     }
392     });
393    
394     # request two
395     $handle->push_write ("request 2\015\012");
396    
397     # simply read 64 bytes, always
398     $handle->push_read_chunk (64, sub {
399     my $response = $_[1];
400     ...
401     });
402    
403     =over 4
404    
405 root 1.10 =cut
406    
407 root 1.8 sub _drain_rbuf {
408     my ($self) = @_;
409 elmex 1.1
410 root 1.17 if (
411     defined $self->{rbuf_max}
412     && $self->{rbuf_max} < length $self->{rbuf}
413     ) {
414     $! = &Errno::ENOSPC; return $self->error;
415     }
416    
417 root 1.11 return if $self->{in_drain};
418 root 1.8 local $self->{in_drain} = 1;
419 elmex 1.1
420 root 1.8 while (my $len = length $self->{rbuf}) {
421     no strict 'refs';
422 root 1.10 if (my $cb = shift @{ $self->{queue} }) {
423     if (!$cb->($self)) {
424     if ($self->{eof}) {
425     # no progress can be made (not enough data and no data forthcoming)
426     $! = &Errno::EPIPE; return $self->error;
427     }
428    
429     unshift @{ $self->{queue} }, $cb;
430 root 1.8 return;
431     }
432     } elsif ($self->{on_read}) {
433     $self->{on_read}($self);
434    
435     if (
436     $self->{eof} # if no further data will arrive
437     && $len == length $self->{rbuf} # and no data has been consumed
438     && !@{ $self->{queue} } # and the queue is still empty
439     && $self->{on_read} # and we still want to read data
440     ) {
441     # then no progress can be made
442     $! = &Errno::EPIPE; return $self->error;
443 elmex 1.1 }
444 root 1.8 } else {
445     # read side becomes idle
446     delete $self->{rw};
447     return;
448     }
449     }
450    
451     if ($self->{eof}) {
452     $self->_shutdown;
453 root 1.16 $self->{on_eof}($self)
454     if $self->{on_eof};
455 root 1.8 }
456 elmex 1.1 }
457    
458 root 1.8 =item $handle->on_read ($cb)
459 elmex 1.1
460 root 1.8 This replaces the currently set C<on_read> callback, or clears it (when
461     the new callback is C<undef>). See the description of C<on_read> in the
462     constructor.
463 elmex 1.1
464 root 1.8 =cut
465    
466     sub on_read {
467     my ($self, $cb) = @_;
468 elmex 1.1
469 root 1.8 $self->{on_read} = $cb;
470 elmex 1.1 }
471    
472 root 1.8 =item $handle->rbuf
473    
474     Returns the read buffer (as a modifiable lvalue).
475 elmex 1.1
476 root 1.8 You can access the read buffer directly as the C<< ->{rbuf} >> member, if
477     you want.
478 elmex 1.1
479 root 1.8 NOTE: The read buffer should only be used or modified if the C<on_read>,
480     C<push_read> or C<unshift_read> methods are used. The other read methods
481     automatically manage the read buffer.
482 elmex 1.1
483     =cut
484    
485 elmex 1.2 sub rbuf : lvalue {
486 root 1.8 $_[0]{rbuf}
487 elmex 1.2 }
488 elmex 1.1
489 root 1.8 =item $handle->push_read ($cb)
490    
491     =item $handle->unshift_read ($cb)
492    
493     Append the given callback to the end of the queue (C<push_read>) or
494     prepend it (C<unshift_read>).
495    
496     The callback is called each time some additional read data arrives.
497 elmex 1.1
498 elmex 1.20 It must check whether enough data is in the read buffer already.
499 elmex 1.1
500 root 1.8 If not enough data is available, it must return the empty list or a false
501     value, in which case it will be called repeatedly until enough data is
502     available (or an error condition is detected).
503    
504     If enough data was available, then the callback must remove all data it is
505     interested in (which can be none at all) and return a true value. After returning
506     true, it will be removed from the queue.
507 elmex 1.1
508     =cut
509    
510 root 1.8 sub push_read {
511     my ($self, $cb) = @_;
512 elmex 1.1
513 root 1.8 push @{ $self->{queue} }, $cb;
514     $self->_drain_rbuf;
515 elmex 1.1 }
516    
517 root 1.8 sub unshift_read {
518     my ($self, $cb) = @_;
519    
520     push @{ $self->{queue} }, $cb;
521     $self->_drain_rbuf;
522     }
523 elmex 1.1
524 root 1.8 =item $handle->push_read_chunk ($len, $cb->($self, $data))
525 elmex 1.1
526 root 1.8 =item $handle->unshift_read_chunk ($len, $cb->($self, $data))
527 elmex 1.1
528 root 1.8 Append the given callback to the end of the queue (C<push_read_chunk>) or
529     prepend it (C<unshift_read_chunk>).
530 elmex 1.1
531 root 1.8 The callback will be called only once C<$len> bytes have been read, and
532     these C<$len> bytes will be passed to the callback.
533 elmex 1.1
534     =cut
535    
536 root 1.8 sub _read_chunk($$) {
537 root 1.10 my ($self, $len, $cb) = @_;
538 elmex 1.1
539 root 1.8 sub {
540     $len <= length $_[0]{rbuf} or return;
541 elmex 1.12 $cb->($_[0], substr $_[0]{rbuf}, 0, $len, "");
542 root 1.8 1
543     }
544     }
545    
546     sub push_read_chunk {
547 root 1.10 $_[0]->push_read (&_read_chunk);
548 root 1.8 }
549 elmex 1.1
550 elmex 1.5
551 root 1.8 sub unshift_read_chunk {
552 root 1.10 $_[0]->unshift_read (&_read_chunk);
553 elmex 1.1 }
554    
555 root 1.8 =item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol))
556 elmex 1.1
557 root 1.8 =item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol))
558 elmex 1.1
559 root 1.8 Append the given callback to the end of the queue (C<push_read_line>) or
560     prepend it (C<unshift_read_line>).
561 elmex 1.1
562 root 1.8 The callback will be called only once a full line (including the end of
563     line marker, C<$eol>) has been read. This line (excluding the end of line
564     marker) will be passed to the callback as second argument (C<$line>), and
565     the end of line marker as the third argument (C<$eol>).
566 elmex 1.1
567 root 1.8 The end of line marker, C<$eol>, can be either a string, in which case it
568     will be interpreted as a fixed record end marker, or it can be a regex
569     object (e.g. created by C<qr>), in which case it is interpreted as a
570     regular expression.
571 elmex 1.1
572 root 1.8 The end of line marker argument C<$eol> is optional, if it is missing (NOT
573     undef), then C<qr|\015?\012|> is used (which is good for most internet
574     protocols).
575 elmex 1.1
576 root 1.8 Partial lines at the end of the stream will never be returned, as they are
577     not marked by the end of line marker.
578 elmex 1.1
579 root 1.8 =cut
580 elmex 1.1
581 root 1.8 sub _read_line($$) {
582 root 1.10 my $self = shift;
583 root 1.8 my $cb = pop;
584     my $eol = @_ ? shift : qr|(\015?\012)|;
585     my $pos;
586 elmex 1.1
587 root 1.14 $eol = quotemeta $eol unless ref $eol;
588     $eol = qr|^(.*?)($eol)|s;
589 elmex 1.1
590 root 1.8 sub {
591     $_[0]{rbuf} =~ s/$eol// or return;
592 elmex 1.1
593 elmex 1.12 $cb->($_[0], $1, $2);
594 root 1.8 1
595     }
596     }
597 elmex 1.1
598 root 1.8 sub push_read_line {
599 root 1.10 $_[0]->push_read (&_read_line);
600     }
601    
602     sub unshift_read_line {
603     $_[0]->unshift_read (&_read_line);
604     }
605    
606     =item $handle->stop_read
607    
608     =item $handle->start_read
609    
610 root 1.18 In rare cases you actually do not want to read anything from the
611 root 1.10 socket. In this case you can call C<stop_read>. Neither C<on_read> no
612 root 1.22 any queued callbacks will be executed then. To start reading again, call
613 root 1.10 C<start_read>.
614    
615     =cut
616    
617     sub stop_read {
618     my ($self) = @_;
619 elmex 1.1
620 root 1.10 delete $self->{rw};
621 root 1.8 }
622 elmex 1.1
623 root 1.10 sub start_read {
624     my ($self) = @_;
625    
626     unless ($self->{rw} || $self->{eof}) {
627     Scalar::Util::weaken $self;
628    
629     $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub {
630 root 1.17 my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf};
631     my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf;
632 root 1.10
633     if ($len > 0) {
634 root 1.17 $self->{filter_r}
635 root 1.18 ? $self->{filter_r}->($self, $rbuf)
636 root 1.17 : $self->_drain_rbuf;
637 root 1.10
638     } elsif (defined $len) {
639 root 1.17 delete $self->{rw};
640 root 1.10 $self->{eof} = 1;
641 root 1.17 $self->_drain_rbuf;
642 root 1.10
643     } elsif ($! != EAGAIN && $! != EINTR) {
644     return $self->error;
645     }
646     });
647     }
648 elmex 1.1 }
649    
650 root 1.19 sub _dotls {
651     my ($self) = @_;
652    
653     if (length $self->{tls_wbuf}) {
654 root 1.22 while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf})) > 0) {
655     substr $self->{tls_wbuf}, 0, $len, "";
656     }
657 root 1.19 }
658    
659     if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) {
660     $self->{wbuf} .= $buf;
661     $self->_drain_wbuf;
662     }
663    
664 root 1.23 while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) {
665     $self->{rbuf} .= $buf;
666     $self->_drain_rbuf;
667     }
668    
669 root 1.24 my $err = Net::SSLeay::get_error ($self->{tls}, -1);
670    
671     if ($err!= Net::SSLeay::ERROR_WANT_READ ()) {
672 root 1.23 if ($err == Net::SSLeay::ERROR_SYSCALL ()) {
673     $self->error;
674     } elsif ($err == Net::SSLeay::ERROR_SSL ()) {
675     $! = &Errno::EIO;
676     $self->error;
677 root 1.19 }
678 root 1.23
679     # all others are fine for our purposes
680 root 1.19 }
681     }
682    
683 root 1.25 =item $handle->starttls ($tls[, $tls_ctx])
684    
685     Instead of starting TLS negotiation immediately when the AnyEvent::Handle
686     object is created, you can also do that at a later time by calling
687     C<starttls>.
688    
689     The first argument is the same as the C<tls> constructor argument (either
690     C<"connect">, C<"accept"> or an existing Net::SSLeay object).
691    
692     The second argument is the optional C<Net::SSLeay::CTX> object that is
693     used when AnyEvent::Handle has to create its own TLS connection object.
694    
695     =cut
696    
697 root 1.19 # TODO: maybe document...
698     sub starttls {
699     my ($self, $ssl, $ctx) = @_;
700    
701 root 1.25 $self->stoptls;
702    
703 root 1.19 if ($ssl eq "accept") {
704     $ssl = Net::SSLeay::new ($ctx || TLS_CTX ());
705     Net::SSLeay::set_accept_state ($ssl);
706     } elsif ($ssl eq "connect") {
707     $ssl = Net::SSLeay::new ($ctx || TLS_CTX ());
708     Net::SSLeay::set_connect_state ($ssl);
709     }
710    
711     $self->{tls} = $ssl;
712    
713 root 1.21 # basically, this is deep magic (because SSL_read should have the same issues)
714     # but the openssl maintainers basically said: "trust us, it just works".
715     # (unfortunately, we have to hardcode constants because the abysmally misdesigned
716     # and mismaintained ssleay-module doesn't even offer them).
717     Net::SSLeay::CTX_set_mode ($self->{tls},
718     (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1)
719     | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2));
720    
721 root 1.19 $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
722     $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
723    
724     Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio});
725    
726     $self->{filter_w} = sub {
727     $_[0]{tls_wbuf} .= ${$_[1]};
728     &_dotls;
729     };
730     $self->{filter_r} = sub {
731     Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]});
732     &_dotls;
733     };
734     }
735    
736 root 1.25 =item $handle->stoptls
737    
738     Destroys the SSL connection, if any. Partial read or write data will be
739     lost.
740    
741     =cut
742    
743     sub stoptls {
744     my ($self) = @_;
745    
746     Net::SSLeay::free (delete $self->{tls}) if $self->{tls};
747     delete $self->{tls_rbio};
748     delete $self->{tls_wbio};
749     delete $self->{tls_wbuf};
750     delete $self->{filter_r};
751     delete $self->{filter_w};
752     }
753    
754 root 1.19 sub DESTROY {
755     my $self = shift;
756    
757 root 1.25 $self->stoptls;
758 root 1.19 }
759    
760     =item AnyEvent::Handle::TLS_CTX
761    
762     This function creates and returns the Net::SSLeay::CTX object used by
763     default for TLS mode.
764    
765     The context is created like this:
766    
767     Net::SSLeay::load_error_strings;
768     Net::SSLeay::SSLeay_add_ssl_algorithms;
769     Net::SSLeay::randomize;
770    
771     my $CTX = Net::SSLeay::CTX_new;
772    
773     Net::SSLeay::CTX_set_options $CTX, Net::SSLeay::OP_ALL
774    
775     =cut
776    
777     our $TLS_CTX;
778    
779     sub TLS_CTX() {
780     $TLS_CTX || do {
781     require Net::SSLeay;
782    
783     Net::SSLeay::load_error_strings ();
784     Net::SSLeay::SSLeay_add_ssl_algorithms ();
785     Net::SSLeay::randomize ();
786    
787     $TLS_CTX = Net::SSLeay::CTX_new ();
788    
789     Net::SSLeay::CTX_set_options ($TLS_CTX, Net::SSLeay::OP_ALL ());
790    
791     $TLS_CTX
792     }
793     }
794    
795 elmex 1.1 =back
796    
797     =head1 AUTHOR
798    
799 root 1.8 Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>.
800 elmex 1.1
801     =cut
802    
803     1; # End of AnyEvent::Handle