1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5*b077aed3SPierre ProncheryBIO_read_ex, BIO_write_ex, BIO_read, BIO_write, 6*b077aed3SPierre ProncheryBIO_gets, BIO_get_line, BIO_puts 7e71b7053SJung-uk Kim- BIO I/O functions 8e71b7053SJung-uk Kim 9e71b7053SJung-uk Kim=head1 SYNOPSIS 10e71b7053SJung-uk Kim 11e71b7053SJung-uk Kim #include <openssl/bio.h> 12e71b7053SJung-uk Kim 13e71b7053SJung-uk Kim int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes); 14e71b7053SJung-uk Kim int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written); 15e71b7053SJung-uk Kim 16e71b7053SJung-uk Kim int BIO_read(BIO *b, void *data, int dlen); 17e71b7053SJung-uk Kim int BIO_gets(BIO *b, char *buf, int size); 18*b077aed3SPierre Pronchery int BIO_get_line(BIO *b, char *buf, int size); 19e71b7053SJung-uk Kim int BIO_write(BIO *b, const void *data, int dlen); 20e71b7053SJung-uk Kim int BIO_puts(BIO *b, const char *buf); 21e71b7053SJung-uk Kim 22e71b7053SJung-uk Kim=head1 DESCRIPTION 23e71b7053SJung-uk Kim 24*b077aed3SPierre ProncheryBIO_read_ex() attempts to read I<dlen> bytes from BIO I<b> and places the data 25*b077aed3SPierre Proncheryin I<data>. If any bytes were successfully read then the number of bytes read is 26*b077aed3SPierre Proncherystored in I<*readbytes>. 27e71b7053SJung-uk Kim 28*b077aed3SPierre ProncheryBIO_write_ex() attempts to write I<dlen> bytes from I<data> to BIO I<b>. 29*b077aed3SPierre ProncheryIf successful then the number of bytes written is stored in I<*written> 30*b077aed3SPierre Proncheryunless I<written> is NULL. 31e71b7053SJung-uk Kim 32*b077aed3SPierre ProncheryBIO_read() attempts to read I<len> bytes from BIO I<b> and places 33*b077aed3SPierre Proncherythe data in I<buf>. 34e71b7053SJung-uk Kim 35e71b7053SJung-uk KimBIO_gets() performs the BIOs "gets" operation and places the data 36*b077aed3SPierre Proncheryin I<buf>. Usually this operation will attempt to read a line of data 37*b077aed3SPierre Proncheryfrom the BIO of maximum length I<size-1>. There are exceptions to this, 38e71b7053SJung-uk Kimhowever; for example, BIO_gets() on a digest BIO will calculate and 39e71b7053SJung-uk Kimreturn the digest and other BIOs may not support BIO_gets() at all. 40e71b7053SJung-uk KimThe returned string is always NUL-terminated and the '\n' is preserved 41e71b7053SJung-uk Kimif present in the input data. 42*b077aed3SPierre ProncheryOn binary input there may be NUL characters within the string; 43*b077aed3SPierre Proncheryin this case the return value (if nonnegative) may give an incorrect length. 44e71b7053SJung-uk Kim 45*b077aed3SPierre ProncheryBIO_get_line() attempts to read from BIO I<b> a line of data up to the next '\n' 46*b077aed3SPierre Proncheryor the maximum length I<size-1> is reached and places the data in I<buf>. 47*b077aed3SPierre ProncheryThe returned string is always NUL-terminated and the '\n' is preserved 48*b077aed3SPierre Proncheryif present in the input data. 49*b077aed3SPierre ProncheryOn binary input there may be NUL characters within the string; 50*b077aed3SPierre Proncheryin this case the return value (if nonnegative) gives the actual length read. 51*b077aed3SPierre ProncheryFor implementing this, unfortunately the data needs to be read byte-by-byte. 52e71b7053SJung-uk Kim 53*b077aed3SPierre ProncheryBIO_write() attempts to write I<len> bytes from I<buf> to BIO I<b>. 54*b077aed3SPierre Pronchery 55*b077aed3SPierre ProncheryBIO_puts() attempts to write a NUL-terminated string I<buf> to BIO I<b>. 56e71b7053SJung-uk Kim 57e71b7053SJung-uk Kim=head1 RETURN VALUES 58e71b7053SJung-uk Kim 59*b077aed3SPierre ProncheryBIO_read_ex() returns 1 if data was successfully read, and 0 otherwise. 60*b077aed3SPierre Pronchery 61*b077aed3SPierre ProncheryBIO_write_ex() returns 1 if no error was encountered writing data, 0 otherwise. 62*b077aed3SPierre ProncheryRequesting to write 0 bytes is not considered an error. 63*b077aed3SPierre Pronchery 64*b077aed3SPierre ProncheryBIO_write() returns -2 if the "write" operation is not implemented by the BIO 65*b077aed3SPierre Proncheryor -1 on other errors. 66*b077aed3SPierre ProncheryOtherwise it returns the number of bytes written. 67*b077aed3SPierre ProncheryThis may be 0 if the BIO I<b> is NULL or I<dlen <= 0>. 68*b077aed3SPierre Pronchery 69*b077aed3SPierre ProncheryBIO_gets() returns -2 if the "gets" operation is not implemented by the BIO 70*b077aed3SPierre Proncheryor -1 on other errors. 71*b077aed3SPierre ProncheryOtherwise it typically returns the amount of data read, 72*b077aed3SPierre Proncherybut depending on the implementation it may return only the length up to 73*b077aed3SPierre Proncherythe first NUL character contained in the data read. 74*b077aed3SPierre ProncheryIn any case the trailing NUL that is added after the data read 75*b077aed3SPierre Proncheryis not included in the length returned. 76e71b7053SJung-uk Kim 77e71b7053SJung-uk KimAll other functions return either the amount of data successfully read or 78e71b7053SJung-uk Kimwritten (if the return value is positive) or that no data was successfully 79e71b7053SJung-uk Kimread or written if the result is 0 or -1. If the return value is -2 then 80*b077aed3SPierre Proncherythe operation is not implemented in the specific BIO type. 81e71b7053SJung-uk Kim 82e71b7053SJung-uk Kim=head1 NOTES 83e71b7053SJung-uk Kim 84e71b7053SJung-uk KimA 0 or -1 return is not necessarily an indication of an error. In 8558f35182SJung-uk Kimparticular when the source/sink is nonblocking or of a certain type 86e71b7053SJung-uk Kimit may merely be an indication that no data is currently available and that 87e71b7053SJung-uk Kimthe application should retry the operation later. 88e71b7053SJung-uk Kim 89e71b7053SJung-uk KimOne technique sometimes used with blocking sockets is to use a system call 90e71b7053SJung-uk Kim(such as select(), poll() or equivalent) to determine when data is available 91e71b7053SJung-uk Kimand then call read() to read the data. The equivalent with BIOs (that is call 92e71b7053SJung-uk Kimselect() on the underlying I/O structure and then call BIO_read() to 93e71b7053SJung-uk Kimread the data) should B<not> be used because a single call to BIO_read() 94e71b7053SJung-uk Kimcan cause several reads (and writes in the case of SSL BIOs) on the underlying 95e71b7053SJung-uk KimI/O structure and may block as a result. Instead select() (or equivalent) 96e71b7053SJung-uk Kimshould be combined with non blocking I/O so successive reads will request 97e71b7053SJung-uk Kima retry instead of blocking. 98e71b7053SJung-uk Kim 99e71b7053SJung-uk KimSee L<BIO_should_retry(3)> for details of how to 100e71b7053SJung-uk Kimdetermine the cause of a retry and other I/O issues. 101e71b7053SJung-uk Kim 102*b077aed3SPierre ProncheryIf the "gets" method is not supported by a BIO then BIO_get_line() can be used. 103*b077aed3SPierre ProncheryIt is also possible to make BIO_gets() usable even if the "gets" method is not 104*b077aed3SPierre Proncherysupported by adding a buffering BIO L<BIO_f_buffer(3)> to the chain. 105e71b7053SJung-uk Kim 106e71b7053SJung-uk Kim=head1 SEE ALSO 107e71b7053SJung-uk Kim 108e71b7053SJung-uk KimL<BIO_should_retry(3)> 109e71b7053SJung-uk Kim 110e71b7053SJung-uk Kim=head1 HISTORY 111e71b7053SJung-uk Kim 112*b077aed3SPierre ProncheryBIO_gets() on 1.1.0 and older when called on BIO_fd() based BIO did not 113e71b7053SJung-uk Kimkeep the '\n' at the end of the line in the buffer. 114e71b7053SJung-uk Kim 115*b077aed3SPierre ProncheryBIO_get_line() was added in OpenSSL 3.0. 116*b077aed3SPierre Pronchery 117*b077aed3SPierre ProncheryBIO_write_ex() returns 1 if the size of the data to write is 0 and the 118*b077aed3SPierre ProncheryI<written> parameter of the function can be NULL since OpenSSL 3.0. 119*b077aed3SPierre Pronchery 120e71b7053SJung-uk Kim=head1 COPYRIGHT 121e71b7053SJung-uk Kim 122*b077aed3SPierre ProncheryCopyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved. 123e71b7053SJung-uk Kim 124*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 125e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 126e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 127e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 128e71b7053SJung-uk Kim 129e71b7053SJung-uk Kim=cut 130