1.\" $OpenBSD: BIO_should_retry.3,v 1.9 2018/12/19 21:12:58 schwarze Exp $
2.\" full merge up to: OpenSSL 60e24554 Apr 6 14:45:18 2010 +0000
3.\" selective merge up to: OpenSSL 57fd5170 May 13 11:24:11 2018 +0200
4.\"
5.\" This file was written by Dr. Stephen Henson <steve@openssl.org>.
6.\" Copyright (c) 2000, 2010, 2016 The OpenSSL Project.  All rights reserved.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\"
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\"
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in
17.\"    the documentation and/or other materials provided with the
18.\"    distribution.
19.\"
20.\" 3. All advertising materials mentioning features or use of this
21.\"    software must display the following acknowledgment:
22.\"    "This product includes software developed by the OpenSSL Project
23.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24.\"
25.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26.\"    endorse or promote products derived from this software without
27.\"    prior written permission. For written permission, please contact
28.\"    openssl-core@openssl.org.
29.\"
30.\" 5. Products derived from this software may not be called "OpenSSL"
31.\"    nor may "OpenSSL" appear in their names without prior written
32.\"    permission of the OpenSSL Project.
33.\"
34.\" 6. Redistributions of any form whatsoever must retain the following
35.\"    acknowledgment:
36.\"    "This product includes software developed by the OpenSSL Project
37.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38.\"
39.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50.\" OF THE POSSIBILITY OF SUCH DAMAGE.
51.\"
52.Dd $Mdocdate: December 19 2018 $
53.Dt BIO_SHOULD_RETRY 3
54.Os
55.Sh NAME
56.Nm BIO_should_read ,
57.Nm BIO_should_write ,
58.Nm BIO_should_io_special ,
59.Nm BIO_retry_type ,
60.Nm BIO_should_retry ,
61.Nm BIO_get_retry_BIO ,
62.Nm BIO_get_retry_reason
63.Nd BIO retry functions
64.Sh SYNOPSIS
65.In openssl/bio.h
66.Ft int
67.Fo BIO_should_read
68.Fa "BIO *b"
69.Fc
70.Ft int
71.Fo BIO_should_write
72.Fa "BIO *b"
73.Fc
74.Ft int
75.Fo BIO_should_io_special
76.Fa "BIO *b"
77.Fc
78.Ft int
79.Fo BIO_retry_type
80.Fa "BIO *b"
81.Fc
82.Ft int
83.Fo BIO_should_retry
84.Fa "BIO *b"
85.Fc
86.Fd #define BIO_FLAGS_READ			0x01
87.Fd #define BIO_FLAGS_WRITE			0x02
88.Fd #define BIO_FLAGS_IO_SPECIAL		0x04
89.Fd #define BIO_FLAGS_RWS \e
90.Fd \&	(BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL)
91.Fd #define BIO_FLAGS_SHOULD_RETRY	0x08
92.Ft BIO *
93.Fo BIO_get_retry_BIO
94.Fa "BIO *bio"
95.Fa "int *reason"
96.Fc
97.Ft int
98.Fo BIO_get_retry_reason
99.Fa "BIO *bio"
100.Fc
101.Sh DESCRIPTION
102These functions determine why a BIO is not able to read or write data.
103They will typically be called after a failed
104.Xr BIO_read 3
105or
106.Xr BIO_write 3
107call.
108.Pp
109.Fn BIO_should_retry
110returns 1 if the call that produced this condition should be retried
111at a later time, or 0 if an error occurred.
112.Pp
113.Fn BIO_should_read
114returns 1 if the cause of the retry condition is that a BIO needs
115to read data, or 0 otherwise.
116.Pp
117.Fn BIO_should_write
118returns 1 if the cause of the retry condition is that a BIO needs
119to write data, or 0 otherwise.
120.Pp
121.Fn BIO_should_io_special
122returns 1 if some special condition (i.e. a reason other than reading
123or writing) is the cause of the retry condition, or 0 otherwise.
124.Pp
125.Fn BIO_retry_type
126returns the bitwise OR of one or more of the flags
127.Dv BIO_FLAGS_READ ,
128.Dv BIO_FLAGS_WRITE ,
129and
130.Dv BIO_FLAGS_IO_SPECIAL
131representing the cause of the current retry condition,
132or 0 if there is no retry condition.
133Current BIO types only set one of the flags at a time.
134.Pp
135.Fn BIO_get_retry_BIO
136determines the precise reason for the special condition.
137It returns the BIO that caused this condition and if
138.Fa reason
139is not
140.Dv NULL
141it contains the reason code.
142The meaning of the reason code and the action that should be taken
143depends on the type of BIO that resulted in this condition.
144.Pp
145.Fn BIO_get_retry_reason
146returns the reason for a special condition
147if passed the relevant BIO, for example as returned by
148.Fn BIO_get_retry_BIO .
149.Pp
150.Fn BIO_should_retry ,
151.Fn BIO_should_read ,
152.Fn BIO_should_write ,
153.Fn BIO_should_io_special ,
154and
155.Fn BIO_retry_type
156are implemented as macros.
157.Pp
158If
159.Fn BIO_should_retry
160returns false, then the precise "error condition" depends on
161the BIO type that caused it and the return code of the BIO operation.
162For example if a call to
163.Xr BIO_read 3
164on a socket BIO returns 0 and
165.Fn BIO_should_retry
166is false, then the cause will be that the connection closed.
167A similar condition on a file BIO will mean that it has reached EOF.
168Some BIO types may place additional information on the error queue.
169For more details see the individual BIO type manual pages.
170.Pp
171If the underlying I/O structure is in a blocking mode,
172almost all current BIO types will not request a retry,
173because the underlying I/O calls will not.
174If the application knows that the BIO type will never
175signal a retry then it need not call
176.Fn BIO_should_retry
177after a failed BIO I/O call.
178This is typically done with file BIOs.
179.Pp
180SSL BIOs are the only current exception to this rule:
181they can request a retry even if the underlying I/O structure
182is blocking, if a handshake occurs during a call to
183.Xr BIO_read 3 .
184An application can retry the failed call immediately
185or avoid this situation by setting
186.Dv SSL_MODE_AUTO_RETRY
187on the underlying SSL structure.
188.Pp
189While an application may retry a failed non-blocking call immediately,
190this is likely to be very inefficient because the call will fail
191repeatedly until data can be processed or is available.
192An application will normally wait until the necessary condition
193is satisfied.
194How this is done depends on the underlying I/O structure.
195.Pp
196For example if the cause is ultimately a socket and
197.Fn BIO_should_read
198is true then a call to
199.Xr select 2
200may be made to wait until data is available
201and then retry the BIO operation.
202By combining the retry conditions of several non-blocking BIOs in a single
203.Xr select 2
204call it is possible to service several BIOs in a single thread,
205though the performance may be poor if SSL BIOs are present because
206long delays can occur during the initial handshake process.
207.Pp
208It is possible for a BIO to block indefinitely if the underlying I/O
209structure cannot process or return any data.
210This depends on the behaviour of the platforms I/O functions.
211This is often not desirable: one solution is to use non-blocking I/O
212and use a timeout on the
213.Xr select 2
214(or equivalent) call.
215.Sh SEE ALSO
216.Xr BIO_new 3 ,
217.Xr BIO_read 3
218.Sh HISTORY
219.Fn BIO_should_read ,
220.Fn BIO_should_write ,
221.Fn BIO_retry_type ,
222and
223.Fn BIO_should_retry
224first appeared in SSLeay 0.6.0.
225.Fn BIO_should_io_special ,
226.Fn BIO_get_retry_BIO ,
227and
228.Fn BIO_get_retry_reason
229first appeared in SSLeay 0.8.0.
230All these functions have been available since
231.Ox 2.4 .
232.Sh BUGS
233The OpenSSL ASN.1 functions cannot gracefully deal with non-blocking I/O:
234they cannot retry after a partial read or write.
235This is usually worked around by only passing the relevant data to ASN.1
236functions when the entire structure can be read or written.
237