a4c464ffa14a3ca112dc28eaa32fcd807a3e64c2
[openssl.git] / doc / crypto / BIO_should_retry.pod
1 =pod
2
3 =head1 NAME
4
5         BIO_should_retry, BIO_should_read, BIO_should_write - BIO retry functions
6
7 =head1 SYNOPSIS
8
9  #include <openssl/bio.h>
10
11  #define BIO_should_read(a)             ((a)->flags & BIO_FLAGS_READ)
12  #define BIO_should_write(a)            ((a)->flags & BIO_FLAGS_WRITE)
13  #define BIO_should_io_special(a)       ((a)->flags & BIO_FLAGS_IO_SPECIAL)
14  #define BIO_retry_type(a)              ((a)->flags & BIO_FLAGS_RWS)
15  #define BIO_should_retry(a)            ((a)->flags & BIO_FLAGS_SHOULD_RETRY)
16
17  #define BIO_FLAGS_READ         0x01
18  #define BIO_FLAGS_WRITE        0x02
19  #define BIO_FLAGS_IO_SPECIAL   0x04
20  #define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL)
21  #define BIO_FLAGS_SHOULD_RETRY 0x08
22
23  BIO *  BIO_get_retry_BIO(BIO *bio, int *reason);
24  int    BIO_get_retry_reason(BIO *bio);
25
26 =head1 DESCRIPTION
27
28 These functions determine why a BIO is not able to read or write data.
29 They will typically be called after a failed BIO_read() or BIO_write()
30 call.
31
32 BIO_should_retry() is true if the call that produced this condition
33 should then be retried at a later time.
34
35 If BIO_should_retry() is false then the cause is an error condition.
36
37 BIO_should_read() is true if the cause of the condition is that a BIO
38 needs to read data.
39
40 BIO_should_write() is true if the cause of the condition is that a BIO
41 needs to read data.
42
43 BIO_should_io_special() is true if some "special" condition, that is a
44 reason other than reading or writing is the cause of the condition.
45
46 BIO_get_retry_reason() returns a mask of the cause of a retry condition
47 consisting of the values B<BIO_FLAGS_READ>, B<BIO_FLAGS_WRITE>,
48 B<BIO_FLAGS_IO_SPECIAL> though current BIO types will only set one of
49 these (Q: is this correct?).
50
51 BIO_get_retry_BIO() determines the precise reason for the special
52 condition, it returns the BIO that caused this condition and if 
53 B<reason> is not NULL it contains the reason code. The meaning of
54 the reason code and the action that should be taken depends on
55 the type of BIO that resulted in this condition.
56
57 BIO_get_retry_reason() returns the reason for a special condition if
58 pass the relevant BIO, for example as returned by BIO_get_retry_BIO().
59
60 =head1 NOTES
61
62 If BIO_should_retry() returns false then the precise "error condition"
63 depends on the BIO type that caused it and the return code of the BIO
64 operation. For example if a call to BIO_read() on a socket BIO returns
65 0 and BIO_should_retry() is false then the cause will be that the
66 connection closed. A similar condition on a file BIO will mean that it
67 has reached EOF. Some BIO types may place additional information on
68 the error queue. For more details see the individual BIO type manual
69 pages.
70
71 If the underlying I/O structure is in a blocking mode then most BIO
72 types will not signal a retry condition, because the underlying I/O
73 calls will not. If the application knows that the BIO type will never
74 signal a retry then it need not call BIO_should_retry() after a failed
75 BIO I/O call. This is typically done with file BIOs.
76
77 The presence of an SSL BIO is an exception to this rule: it can
78 request a retry because the handshake process is underway (either
79 initially or due to a session renegotiation) even if the underlying
80 I/O structure (for example a socket) is in a blocking mode.
81
82 The action an application should take after a BIO has signalled that a
83 retry is required depends on the BIO that caused the retry.
84
85 If the underlying I/O structure is in a blocking mode then the BIO
86 call can be retried immediately. That is something like this can be
87 done:
88
89  do {
90     len = BIO_read(bio, buf, len);
91  } while((len <= 0) && BIO_should_retry(bio));
92
93 While an application may retry a failed non blocking call immediately
94 this is likely to be very inefficient because the call is likely to
95 fail repeatedly until data can be processed. An application will normally
96 wait until the necessary condition is satisfied. How this is done depends
97 on the underlying I/O structure.
98
99 For example if the cause is ultimately a socket and BIO_should_read()
100 is true then a call to select() may be made to wait until data is
101 available and then retry the BIO operation. By combining the retry
102 conditions of several non blocking BIOs in a single select() call
103 it is possible to service several BIOs in a single thread. 
104
105 The cause of the retry condition may not be the same as the call that
106 made it: for example if BIO_write() fails BIO_should_read() can be
107 true. One possible reason for this is that an SSL handshake is taking
108 place.
109
110 Even if data is read from the underlying I/O structure this does not
111 imply that the next BIO I/O call will succeed. For example if an
112 encryption BIO reads only a fraction of a block it will not be
113 able to pass any data to the application until a complete block has
114 been read.
115
116 It is possible for a BIO to block indefinitely if the underlying I/O
117 structure cannot process the data. This depends on the behaviour of
118 the platforms I/O functions. This is often not desirable: one solution
119 is to use non blocking I/O and use a timeout on the select() (or
120 equivalent) call.
121
122 =head1 BUGS
123
124 The OpenSSL ASN1 functions cannot gracefully deal with non blocking I/O:
125 that is they cannot retry after a partial read or write. This is usually
126 worked around by only passing the relevant data to ASN1 functions when
127 the entire structure can be read or written.
128
129 =head1 SEE ALSO
130
131 TBA