BIO_s_bio.pod 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. BIO_METHOD *BIO_s_bio(void);
  10. #define BIO_make_bio_pair(b1,b2) (int)BIO_ctrl(b1,BIO_C_MAKE_BIO_PAIR,0,b2)
  11. #define BIO_destroy_bio_pair(b) (int)BIO_ctrl(b,BIO_C_DESTROY_BIO_PAIR,0,NULL)
  12. #define BIO_shutdown_wr(b) (int)BIO_ctrl(b, BIO_C_SHUTDOWN_WR, 0, NULL)
  13. #define BIO_set_write_buf_size(b,size) (int)BIO_ctrl(b,BIO_C_SET_WRITE_BUF_SIZE,size,NULL)
  14. #define BIO_get_write_buf_size(b,size) (size_t)BIO_ctrl(b,BIO_C_GET_WRITE_BUF_SIZE,size,NULL)
  15. int BIO_new_bio_pair(BIO **bio1, size_t writebuf1, BIO **bio2, size_t writebuf2);
  16. #define BIO_get_write_guarantee(b) (int)BIO_ctrl(b,BIO_C_GET_WRITE_GUARANTEE,0,NULL)
  17. size_t BIO_ctrl_get_write_guarantee(BIO *b);
  18. #define BIO_get_read_request(b) (int)BIO_ctrl(b,BIO_C_GET_READ_REQUEST,0,NULL)
  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() will read data from the buffer or request a retry if no
  33. data is available.
  34. Calls to BIO_write() 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() less than the amount requested or if the
  59. buffer is full request a retry. BIO_ctrl_get_write_guarantee() is a function
  60. 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() and a response read with BIO_read(), this can occur during an
  84. TLS/SSL handshake for example. BIO_write() will succeed and place data in the write
  85. buffer. BIO_read() will initially fail and BIO_should_read() will be true. If
  86. the application then waits for data to be available on the underlying transport
  87. before flushing the write buffer it will never succeed because the request was
  88. never sent!
  89. =head1 RETURN VALUES
  90. BIO_new_bio_pair() returns 1 on success, with the new BIOs available in
  91. B<bio1> and B<bio2>, or 0 on failure, with NULL pointers stored into the
  92. locations for B<bio1> and B<bio2>. Check the error stack for more information.
  93. [XXXXX: More return values need to be added here]
  94. =head1 EXAMPLE
  95. The BIO pair can be used to have full control over the network access of an
  96. application. The application can call select() on the socket as required
  97. without having to go through the SSL-interface.
  98. BIO *internal_bio, *network_bio;
  99. ...
  100. BIO_new_bio_pair(internal_bio, 0, network_bio, 0);
  101. SSL_set_bio(ssl, internal_bio, internal_bio);
  102. SSL_operations();
  103. ...
  104. application | TLS-engine
  105. | |
  106. +----------> SSL_operations()
  107. | /\ ||
  108. | || \/
  109. | BIO-pair (internal_bio)
  110. +----------< BIO-pair (network_bio)
  111. | |
  112. socket |
  113. ...
  114. SSL_free(ssl); /* implicitly frees internal_bio */
  115. BIO_free(network_bio);
  116. ...
  117. As the BIO pair will only buffer the data and never directly access the
  118. connection, it behaves non-blocking and will return as soon as the write
  119. buffer is full or the read buffer is drained. Then the application has to
  120. flush the write buffer and/or fill the read buffer.
  121. Use the BIO_ctrl_pending(), to find out whether data is buffered in the BIO
  122. and must be transfered to the network. Use BIO_ctrl_get_read_request() to
  123. find out, how many bytes must be written into the buffer before the
  124. SSL_operation() can successfully be continued.
  125. =head1 WARNING
  126. As the data is buffered, SSL_operation() may return with a ERROR_SSL_WANT_READ
  127. condition, but there is still data in the write buffer. An application must
  128. not rely on the error value of SSL_operation() but must assure that the
  129. write buffer is always flushed first. Otherwise a deadlock may occur as
  130. the peer might be waiting for the data before being able to continue.
  131. =head1 SEE ALSO
  132. L<SSL_set_bio(3)|SSL_set_bio(3)>, L<ssl(3)|ssl(3)>, L<bio(3)|bio(3)>,
  133. L<BIO_should_retry(3)|BIO_should_retry(3)>, L<BIO_read(3)|BIO_read(3)>
  134. =cut