1.\" $OpenBSD: BIO_should_retry.3,v 1.10 2022/11/27 19:11:11 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 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: November 27 2022 $ 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.Nm BIO_set_retry_reason 64.Nd BIO retry functions 65.Sh SYNOPSIS 66.In openssl/bio.h 67.Ft int 68.Fo BIO_should_read 69.Fa "BIO *b" 70.Fc 71.Ft int 72.Fo BIO_should_write 73.Fa "BIO *b" 74.Fc 75.Ft int 76.Fo BIO_should_io_special 77.Fa "BIO *b" 78.Fc 79.Ft int 80.Fo BIO_retry_type 81.Fa "BIO *b" 82.Fc 83.Ft int 84.Fo BIO_should_retry 85.Fa "BIO *b" 86.Fc 87.Fd #define BIO_FLAGS_READ 0x01 88.Fd #define BIO_FLAGS_WRITE 0x02 89.Fd #define BIO_FLAGS_IO_SPECIAL 0x04 90.Fd #define BIO_FLAGS_RWS \e 91.Fd \& (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL) 92.Fd #define BIO_FLAGS_SHOULD_RETRY 0x08 93.Ft BIO * 94.Fo BIO_get_retry_BIO 95.Fa "BIO *bio" 96.Fa "int *reason" 97.Fc 98.Ft int 99.Fo BIO_get_retry_reason 100.Fa "BIO *bio" 101.Fc 102.Ft void 103.Fo BIO_set_retry_reason 104.Fa "BIO *bio" 105.Fa "int reason" 106.Fc 107.Sh DESCRIPTION 108These functions determine why a BIO is not able to read or write data. 109They will typically be called after a failed 110.Xr BIO_read 3 111or 112.Xr BIO_write 3 113call. 114.Pp 115.Fn BIO_should_retry 116returns 1 if the call that produced this condition should be retried 117at a later time, or 0 if an error occurred. 118.Pp 119.Fn BIO_should_read 120returns 1 if the cause of the retry condition is that a BIO needs 121to read data, or 0 otherwise. 122.Pp 123.Fn BIO_should_write 124returns 1 if the cause of the retry condition is that a BIO needs 125to write data, or 0 otherwise. 126.Pp 127.Fn BIO_should_io_special 128returns 1 if some special condition (i.e. a reason other than reading 129or writing) is the cause of the retry condition, or 0 otherwise. 130.Pp 131.Fn BIO_retry_type 132returns the bitwise OR of one or more of the flags 133.Dv BIO_FLAGS_READ , 134.Dv BIO_FLAGS_WRITE , 135and 136.Dv BIO_FLAGS_IO_SPECIAL 137representing the cause of the current retry condition, 138or 0 if there is no retry condition. 139Current BIO types only set one of the flags at a time. 140.Pp 141.Fn BIO_get_retry_BIO 142determines the precise reason for the special condition. 143It returns the BIO that caused this condition and if 144.Fa reason 145is not 146.Dv NULL 147it contains the reason code. 148The meaning of the reason code and the action that should be taken 149depends on the type of BIO that resulted in this condition. 150.Pp 151.Fn BIO_get_retry_reason 152returns the reason for a special condition 153if passed the relevant BIO, for example as returned by 154.Fn BIO_get_retry_BIO . 155.Pp 156.Fn BIO_set_retry_reason 157sets the retry reason for a special condition for the given 158.Fa bio . 159It is intended to be called by functions implementing a BIO type 160rather than by functions merely using BIOs. 161.Pp 162.Fn BIO_should_retry , 163.Fn BIO_should_read , 164.Fn BIO_should_write , 165.Fn BIO_should_io_special , 166and 167.Fn BIO_retry_type 168are implemented as macros. 169.Pp 170If 171.Fn BIO_should_retry 172returns false, then the precise "error condition" depends on 173the BIO type that caused it and the return code of the BIO operation. 174For example if a call to 175.Xr BIO_read 3 176on a socket BIO returns 0 and 177.Fn BIO_should_retry 178is false, then the cause will be that the connection closed. 179A similar condition on a file BIO will mean that it has reached EOF. 180Some BIO types may place additional information on the error queue. 181For more details see the individual BIO type manual pages. 182.Pp 183If the underlying I/O structure is in a blocking mode, 184almost all current BIO types will not request a retry, 185because the underlying I/O calls will not. 186If the application knows that the BIO type will never 187signal a retry then it need not call 188.Fn BIO_should_retry 189after a failed BIO I/O call. 190This is typically done with file BIOs. 191.Pp 192SSL BIOs are the only current exception to this rule: 193they can request a retry even if the underlying I/O structure 194is blocking, if a handshake occurs during a call to 195.Xr BIO_read 3 . 196An application can retry the failed call immediately 197or avoid this situation by setting 198.Dv SSL_MODE_AUTO_RETRY 199on the underlying SSL structure. 200.Pp 201While an application may retry a failed non-blocking call immediately, 202this is likely to be very inefficient because the call will fail 203repeatedly until data can be processed or is available. 204An application will normally wait until the necessary condition 205is satisfied. 206How this is done depends on the underlying I/O structure. 207.Pp 208For example if the cause is ultimately a socket and 209.Fn BIO_should_read 210is true then a call to 211.Xr select 2 212may be made to wait until data is available 213and then retry the BIO operation. 214By combining the retry conditions of several non-blocking BIOs in a single 215.Xr select 2 216call it is possible to service several BIOs in a single thread, 217though the performance may be poor if SSL BIOs are present because 218long delays can occur during the initial handshake process. 219.Pp 220It is possible for a BIO to block indefinitely if the underlying I/O 221structure cannot process or return any data. 222This depends on the behaviour of the platforms I/O functions. 223This is often not desirable: one solution is to use non-blocking I/O 224and use a timeout on the 225.Xr select 2 226(or equivalent) call. 227.Sh SEE ALSO 228.Xr BIO_new 3 , 229.Xr BIO_read 3 230.Sh HISTORY 231.Fn BIO_should_read , 232.Fn BIO_should_write , 233.Fn BIO_retry_type , 234and 235.Fn BIO_should_retry 236first appeared in SSLeay 0.6.0. 237.Fn BIO_should_io_special , 238.Fn BIO_get_retry_BIO , 239and 240.Fn BIO_get_retry_reason 241first appeared in SSLeay 0.8.0. 242All these functions have been available since 243.Ox 2.4 . 244.Pp 245.Fn BIO_set_retry_reason 246first appeared in OpenSSL 1.1.0 and has been available since 247.Ox 7.1 . 248.Sh BUGS 249The OpenSSL ASN.1 functions cannot gracefully deal with non-blocking I/O: 250they cannot retry after a partial read or write. 251This is usually worked around by only passing the relevant data to ASN.1 252functions when the entire structure can be read or written. 253