BIO_s_bio.pod 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. =pod
  2. =head1 NAME
  3. BIO_s_bio, BIO_make_bio_pair, BIO_destroy_bio_pair, BIO_shutdown_wr,
  4. BIO_set_write_buf_size, BIO_get_write_buf_size, BIO_new_bio_pair,
  5. BIO_get_write_guarantee, BIO_ctrl_get_write_guarantee, BIO_get_read_request,
  6. BIO_ctrl_get_read_request, BIO_ctrl_reset_read_request - BIO pair BIO
  7. =head1 SYNOPSIS
  8. #include <openssl/bio.h>
  9. const BIO_METHOD *BIO_s_bio(void);
  10. int BIO_make_bio_pair(BIO *b1, BIO *b2);
  11. int BIO_destroy_bio_pair(BIO *b);
  12. int BIO_shutdown_wr(BIO *b);
  13. int BIO_set_write_buf_size(BIO *b, long size);
  14. size_t BIO_get_write_buf_size(BIO *b, long size);
  15. int BIO_new_bio_pair(BIO **bio1, size_t writebuf1, BIO **bio2, size_t writebuf2);
  16. int BIO_get_write_guarantee(BIO *b);
  17. size_t BIO_ctrl_get_write_guarantee(BIO *b);
  18. int BIO_get_read_request(BIO *b);
  19. size_t BIO_ctrl_get_read_request(BIO *b);
  20. int BIO_ctrl_reset_read_request(BIO *b);
  21. =head1 DESCRIPTION
  22. BIO_s_bio() returns the method for a BIO pair. A BIO pair is a pair of source/sink
  23. BIOs where data written to either half of the pair is buffered and can be read from
  24. the other half. Both halves must usually by handled by the same application thread
  25. since no locking is done on the internal data structures.
  26. Since BIO chains typically end in a source/sink BIO it is possible to make this
  27. one half of a BIO pair and have all the data processed by the chain under application
  28. control.
  29. One typical use of BIO pairs is to place TLS/SSL I/O under application control, this
  30. can be used when the application wishes to use a non standard transport for
  31. TLS/SSL or the normal socket routines are inappropriate.
  32. Calls to BIO_read_ex() will read data from the buffer or request a retry if no
  33. data is available.
  34. Calls to BIO_write_ex() will place data in the buffer or request a retry if the
  35. buffer is full.
  36. The standard calls BIO_ctrl_pending() and BIO_ctrl_wpending() can be used to
  37. determine the amount of pending data in the read or write buffer.
  38. BIO_reset() clears any data in the write buffer.
  39. BIO_make_bio_pair() joins two separate BIOs into a connected pair.
  40. BIO_destroy_pair() destroys the association between two connected BIOs. Freeing
  41. up any half of the pair will automatically destroy the association.
  42. BIO_shutdown_wr() is used to close down a BIO B<b>. After this call no further
  43. writes on BIO B<b> are allowed (they will return an error). Reads on the other
  44. half of the pair will return any pending data or EOF when all pending data has
  45. been read.
  46. BIO_set_write_buf_size() sets the write buffer size of BIO B<b> to B<size>.
  47. If the size is not initialized a default value is used. This is currently
  48. 17K, sufficient for a maximum size TLS record.
  49. BIO_get_write_buf_size() returns the size of the write buffer.
  50. BIO_new_bio_pair() combines the calls to BIO_new(), BIO_make_bio_pair() and
  51. BIO_set_write_buf_size() to create a connected pair of BIOs B<bio1>, B<bio2>
  52. with write buffer sizes B<writebuf1> and B<writebuf2>. If either size is
  53. zero then the default size is used. BIO_new_bio_pair() does not check whether
  54. B<bio1> or B<bio2> do point to some other BIO, the values are overwritten,
  55. BIO_free() is not called.
  56. BIO_get_write_guarantee() and BIO_ctrl_get_write_guarantee() return the maximum
  57. length of data that can be currently written to the BIO. Writes larger than this
  58. value will return a value from BIO_write_ex() less than the amount requested or
  59. if the buffer is full request a retry. BIO_ctrl_get_write_guarantee() is a
  60. function whereas BIO_get_write_guarantee() is a macro.
  61. BIO_get_read_request() and BIO_ctrl_get_read_request() return the
  62. amount of data requested, or the buffer size if it is less, if the
  63. last read attempt at the other half of the BIO pair failed due to an
  64. empty buffer. This can be used to determine how much data should be
  65. written to the BIO so the next read will succeed: this is most useful
  66. in TLS/SSL applications where the amount of data read is usually
  67. meaningful rather than just a buffer size. After a successful read
  68. this call will return zero. It also will return zero once new data
  69. has been written satisfying the read request or part of it.
  70. Note that BIO_get_read_request() never returns an amount larger
  71. than that returned by BIO_get_write_guarantee().
  72. BIO_ctrl_reset_read_request() can also be used to reset the value returned by
  73. BIO_get_read_request() to zero.
  74. =head1 NOTES
  75. Both halves of a BIO pair should be freed. That is even if one half is implicit
  76. freed due to a BIO_free_all() or SSL_free() call the other half needs to be freed.
  77. When used in bidirectional applications (such as TLS/SSL) care should be taken to
  78. flush any data in the write buffer. This can be done by calling BIO_pending()
  79. on the other half of the pair and, if any data is pending, reading it and sending
  80. it to the underlying transport. This must be done before any normal processing
  81. (such as calling select() ) due to a request and BIO_should_read() being true.
  82. To see why this is important consider a case where a request is sent using
  83. BIO_write_ex() and a response read with BIO_read_ex(), this can occur during an
  84. TLS/SSL handshake for example. BIO_write_ex() will succeed and place data in the
  85. write buffer. BIO_read_ex() will initially fail and BIO_should_read() will be
  86. true. If the application then waits for data to be available on the underlying
  87. transport before flushing the write buffer it will never succeed because the
  88. request was never sent!
  89. BIO_eof() is true if no data is in the peer BIO and the peer BIO has been
  90. shutdown.
  91. BIO_make_bio_pair(), BIO_destroy_bio_pair(), BIO_shutdown_wr(),
  92. BIO_set_write_buf_size(), BIO_get_write_buf_size(),
  93. BIO_get_write_guarantee(), and BIO_get_read_request() are implemented
  94. as macros.
  95. =head1 RETURN VALUES
  96. BIO_new_bio_pair() returns 1 on success, with the new BIOs available in
  97. B<bio1> and B<bio2>, or 0 on failure, with NULL pointers stored into the
  98. locations for B<bio1> and B<bio2>. Check the error stack for more information.
  99. [XXXXX: More return values need to be added here]
  100. =head1 EXAMPLES
  101. The BIO pair can be used to have full control over the network access of an
  102. application. The application can call select() on the socket as required
  103. without having to go through the SSL-interface.
  104. BIO *internal_bio, *network_bio;
  105. ...
  106. BIO_new_bio_pair(&internal_bio, 0, &network_bio, 0);
  107. SSL_set_bio(ssl, internal_bio, internal_bio);
  108. SSL_operations(); /* e.g. SSL_read and SSL_write */
  109. ...
  110. application | TLS-engine
  111. | |
  112. +----------> SSL_operations()
  113. | /\ ||
  114. | || \/
  115. | BIO-pair (internal_bio)
  116. | BIO-pair (network_bio)
  117. | || /\
  118. | \/ ||
  119. +-----------< BIO_operations()
  120. | |
  121. | |
  122. socket
  123. ...
  124. SSL_free(ssl); /* implicitly frees internal_bio */
  125. BIO_free(network_bio);
  126. ...
  127. As the BIO pair will only buffer the data and never directly access the
  128. connection, it behaves non-blocking and will return as soon as the write
  129. buffer is full or the read buffer is drained. Then the application has to
  130. flush the write buffer and/or fill the read buffer.
  131. Use the BIO_ctrl_pending(), to find out whether data is buffered in the BIO
  132. and must be transferred to the network. Use BIO_ctrl_get_read_request() to
  133. find out, how many bytes must be written into the buffer before the
  134. SSL_operation() can successfully be continued.
  135. =head1 WARNINGS
  136. As the data is buffered, SSL_operation() may return with an ERROR_SSL_WANT_READ
  137. condition, but there is still data in the write buffer. An application must
  138. not rely on the error value of SSL_operation() but must assure that the
  139. write buffer is always flushed first. Otherwise a deadlock may occur as
  140. the peer might be waiting for the data before being able to continue.
  141. =head1 SEE ALSO
  142. L<SSL_set_bio(3)>, L<ssl(7)>, L<bio(7)>,
  143. L<BIO_should_retry(3)>, L<BIO_read_ex(3)>
  144. =head1 COPYRIGHT
  145. Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
  146. Licensed under the Apache License 2.0 (the "License"). You may not use
  147. this file except in compliance with the License. You can obtain a copy
  148. in the file LICENSE in the source distribution or at
  149. L<https://www.openssl.org/source/license.html>.
  150. =cut