2a6485af9a7eae800671f10cfe3dc67d449a4817
[openssl.git] / doc / crypto / BIO_should_retry.pod
1 =pod
2
3 =head1 NAME
4
5 BIO_FLAGS_READ, BIO_FLAGS_WRITE, BIO_FLAGS_IO_SPECIAL, BIO_FLAGS_RWS,
6 BIO_FLAGS_SHOULD_RETRY,
7 BIO_should_read, BIO_should_write,
8 BIO_should_io_special, BIO_retry_type, BIO_should_retry,
9 BIO_get_retry_BIO, BIO_get_retry_reason, BIO_set_retry_reason - BIO retry
10 functions
11
12 =head1 SYNOPSIS
13
14  #include <openssl/bio.h>
15
16  #define BIO_should_read(a)             ((a)->flags & BIO_FLAGS_READ)
17  #define BIO_should_write(a)            ((a)->flags & BIO_FLAGS_WRITE)
18  #define BIO_should_io_special(a)       ((a)->flags & BIO_FLAGS_IO_SPECIAL)
19  #define BIO_retry_type(a)              ((a)->flags & BIO_FLAGS_RWS)
20  #define BIO_should_retry(a)            ((a)->flags & BIO_FLAGS_SHOULD_RETRY)
21
22  #define BIO_FLAGS_READ         0x01
23  #define BIO_FLAGS_WRITE        0x02
24  #define BIO_FLAGS_IO_SPECIAL   0x04
25  #define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL)
26  #define BIO_FLAGS_SHOULD_RETRY 0x08
27
28  BIO *BIO_get_retry_BIO(BIO *bio, int *reason);
29  int BIO_get_retry_reason(BIO *bio);
30  void BIO_set_retry_reason(BIO *bio, int reason);
31
32 =head1 DESCRIPTION
33
34 These functions determine why a BIO is not able to read or write data.
35 They will typically be called after a failed BIO_read() or BIO_write()
36 call.
37
38 BIO_should_retry() is true if the call that produced this condition
39 should then be retried at a later time.
40
41 If BIO_should_retry() is false then the cause is an error condition.
42
43 BIO_should_read() is true if the cause of the condition is that a BIO
44 needs to read data.
45
46 BIO_should_write() is true if the cause of the condition is that a BIO
47 needs to read data.
48
49 BIO_should_io_special() is true if some "special" condition, that is a
50 reason other than reading or writing is the cause of the condition.
51
52 BIO_retry_type() returns a mask of the cause of a retry condition
53 consisting of the values B<BIO_FLAGS_READ>, B<BIO_FLAGS_WRITE>,
54 B<BIO_FLAGS_IO_SPECIAL> though current BIO types will only set one of
55 these.
56
57 BIO_get_retry_BIO() determines the precise reason for the special
58 condition, it returns the BIO that caused this condition and if
59 B<reason> is not NULL it contains the reason code. The meaning of
60 the reason code and the action that should be taken depends on
61 the type of BIO that resulted in this condition.
62
63 BIO_get_retry_reason() returns the reason for a special condition if
64 passed the relevant BIO, for example as returned by BIO_get_retry_BIO().
65
66 BIO_set_retry_reason() sets the retry reason for a special condition for a given
67 BIO. This would usually only be called by BIO implementations.
68
69 =head1 NOTES
70
71 If BIO_should_retry() returns false then the precise "error condition"
72 depends on the BIO type that caused it and the return code of the BIO
73 operation. For example if a call to BIO_read() on a socket BIO returns
74 0 and BIO_should_retry() is false then the cause will be that the
75 connection closed. A similar condition on a file BIO will mean that it
76 has reached EOF. Some BIO types may place additional information on
77 the error queue. For more details see the individual BIO type manual
78 pages.
79
80 If the underlying I/O structure is in a blocking mode almost all current
81 BIO types will not request a retry, because the underlying I/O
82 calls will not. If the application knows that the BIO type will never
83 signal a retry then it need not call BIO_should_retry() after a failed
84 BIO I/O call. This is typically done with file BIOs.
85
86 SSL BIOs are the only current exception to this rule: they can request a
87 retry even if the underlying I/O structure is blocking, if a handshake
88 occurs during a call to BIO_read(). An application can retry the failed
89 call immediately or avoid this situation by setting SSL_MODE_AUTO_RETRY
90 on the underlying SSL structure.
91
92 While an application may retry a failed non blocking call immediately
93 this is likely to be very inefficient because the call will fail
94 repeatedly until data can be processed or is available. An application
95 will normally wait until the necessary condition is satisfied. How
96 this is done depends on the underlying I/O structure.
97
98 For example if the cause is ultimately a socket and BIO_should_read()
99 is true then a call to select() may be made to wait until data is
100 available and then retry the BIO operation. By combining the retry
101 conditions of several non blocking BIOs in a single select() call
102 it is possible to service several BIOs in a single thread, though
103 the performance may be poor if SSL BIOs are present because long delays
104 can occur during the initial handshake process.
105
106 It is possible for a BIO to block indefinitely if the underlying I/O
107 structure cannot process or return any data. This depends on the behaviour of
108 the platforms I/O functions. This is often not desirable: one solution
109 is to use non blocking I/O and use a timeout on the select() (or
110 equivalent) call.
111
112 =head1 BUGS
113
114 The OpenSSL ASN1 functions cannot gracefully deal with non blocking I/O:
115 that is they cannot retry after a partial read or write. This is usually
116 worked around by only passing the relevant data to ASN1 functions when
117 the entire structure can be read or written.
118
119 =head1 SEE ALSO
120
121 L<bio>
122
123 =head1 HISTORY
124
125 The BIO_get_retry_reason() and BIO_set_retry_reason() functions were added in
126 OpenSSL version 1.1.0.
127
128 =head1 COPYRIGHT
129
130 Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
131
132 Licensed under the OpenSSL license (the "License").  You may not use
133 this file except in compliance with the License.  You can obtain a copy
134 in the file LICENSE in the source distribution or at
135 L<https://www.openssl.org/source/license.html>.
136
137 =cut