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