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