BIO_sendmmsg.pod 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. =pod
  2. =head1 NAME
  3. BIO_sendmmsg, BIO_recvmmsg, BIO_dgram_set_local_addr_enable,
  4. BIO_dgram_get_local_addr_enable, BIO_dgram_get_local_addr_cap,
  5. BIO_err_is_non_fatal - send and receive multiple datagrams in a single call
  6. =head1 SYNOPSIS
  7. #include <openssl/bio.h>
  8. typedef struct bio_msg_st {
  9. void *data;
  10. size_t data_len;
  11. BIO_ADDR *peer, *local;
  12. uint64_t flags;
  13. } BIO_MSG;
  14. int BIO_sendmmsg(BIO *b, BIO_MSG *msg,
  15. size_t stride, size_t num_msg, uint64_t flags,
  16. size_t *msgs_processed);
  17. int BIO_recvmmsg(BIO *b, BIO_MSG *msg,
  18. size_t stride, size_t num_msg, uint64_t flags,
  19. size_t *msgs_processed);
  20. int BIO_dgram_set_local_addr_enable(BIO *b, int enable);
  21. int BIO_dgram_get_local_addr_enable(BIO *b, int *enable);
  22. int BIO_dgram_get_local_addr_cap(BIO *b);
  23. int BIO_err_is_non_fatal(unsigned int errcode);
  24. =head1 DESCRIPTION
  25. BIO_sendmmsg() and BIO_recvmmsg() functions can be used to send and receive
  26. multiple messages in a single call to a BIO. They are analagous to sendmmsg(2)
  27. and recvmmsg(2) on operating systems which provide those functions.
  28. The B<BIO_MSG> structure provides a subset of the functionality of the B<struct
  29. msghdr> structure defined by POSIX. These functions accept an array of
  30. B<BIO_MSG> structures. On any particular invocation, these functions may process
  31. all of the passed structures, some of them, or none of them. This is indicated
  32. by the value stored in I<*msgs_processed>, which expresses the number of
  33. messages processed.
  34. The caller should set the I<data> member of a B<BIO_MSG> to a buffer containing
  35. the data to send, or to be filled with a received message. I<data_len> should be
  36. set to the size of the buffer in bytes. If the given B<BIO_MSG> is processed (in
  37. other words, if the integer returned by the function is greater than or equal to
  38. that B<BIO_MSG>'s array index), I<data_len> will be modified to specify the
  39. actual amount of data sent or received.
  40. The I<flags> field of a B<BIO_MSG> provides input per-message flags to the
  41. invocation. If the invocation processes that B<BIO_MSG>, the I<flags> field is
  42. written with output per-message flags, or zero if no such flags are applicable.
  43. Currently, no input or output per-message flags are defined and this field
  44. should be set to zero before calling BIO_sendmmsg() or BIO_recvmmsg().
  45. The I<flags> argument to BIO_sendmmsg() and BIO_recvmmsg() provides global
  46. flags which affect the entire invocation. No global flags are currently
  47. defined and this argument should be set to zero.
  48. When these functions are used to send and receive datagrams, the I<peer> field
  49. of a B<BIO_MSG> allows the destination address of sent datagrams to be specified
  50. on a per-datagram basis, and the source address of received datagrams to be
  51. determined. The I<peer> field should be set to point to a B<BIO_ADDR>, which
  52. will be read by BIO_sendmmsg() and used as the destination address for sent
  53. datagrams, and written by BIO_recvmmsg() with the source address of received
  54. datagrams.
  55. Similarly, the I<local> field of a B<BIO_MSG> allows the source address of sent
  56. datagrams to be specified on a per-datagram basis, and the destination address
  57. of received datagrams to be determined. Unlike I<peer>, support for I<local>
  58. must be explicitly enabled on a B<BIO> before it can be used; see
  59. BIO_dgram_set_local_addr_enable(). If I<local> is non-NULL in a B<BIO_MSG> and
  60. support for I<local> has not been enabled, processing of that B<BIO_MSG> fails.
  61. I<peer> and I<local> should be set to NULL if they are not required. Support for
  62. I<local> may not be available on all platforms; on these platforms, these
  63. functions always fail if I<local> is non-NULL.
  64. If I<local> is specified and local address support is enabled, but the operating
  65. system does not report a local address for a specific received message, the
  66. B<BIO_ADDR> it points to will be cleared (address family set to C<AF_UNSPEC>).
  67. This is known to happen on Windows when a packet is received which was sent by
  68. the local system, regardless of whether the packet's destination address was the
  69. loopback address or the IP address of a local non-loopback interface. This is
  70. also known to happen on macOS in some circumstances, such as for packets sent
  71. before local address support was enabled for a receiving socket. These are
  72. OS-specific limitations. As such, users of this API using local address support
  73. should expect to sometimes receive a cleared local B<BIO_ADDR> instead of the
  74. correct value.
  75. The I<stride> argument must be set to C<sizeof(BIO_MSG)>. This argument
  76. facilitates backwards compatibility if fields are added to B<BIO_MSG>. Callers
  77. must zero-initialize B<BIO_MSG>.
  78. I<num_msg> should be sent to the maximum number of messages to send or receive,
  79. which is also the length of the array pointed to by I<msg>.
  80. I<msgs_processed> must be non-NULL and points to an integer written with the
  81. number of messages successfully processed; see the RETURN VALUES section for
  82. further discussion.
  83. Unlike most BIO functions, these functions explicitly support multi-threaded
  84. use. Multiple concurrent writers and multiple concurrent readers of the same BIO
  85. are permitted in any combination. As such, these functions do not clear, set, or
  86. otherwise modify BIO retry flags. The return value must be used to determine
  87. whether an operation should be retried; see below.
  88. The support for concurrent use extends to BIO_sendmmsg() and BIO_recvmmsg()
  89. only, and no other function may be called on a given BIO while any call to
  90. BIO_sendmmsg() or BIO_recvmmsg() is in progress, or vice versa.
  91. BIO_dgram_set_local_addr_enable() and BIO_dgram_get_local_addr_enable() control
  92. whether local address support is enabled. To enable local address support, call
  93. BIO_dgram_set_local_addr_enable() with an argument of 1. The call will fail if
  94. local address support is not available for the platform.
  95. BIO_dgram_get_local_addr_enable() retrieves the value set by
  96. BIO_dgram_set_local_addr_enable().
  97. BIO_dgram_get_local_addr_cap() determines if the B<BIO> is capable of supporting
  98. local addresses.
  99. BIO_err_is_non_fatal() determines if a packed error code represents an error
  100. which is transient in nature.
  101. =head1 NOTES
  102. Some implementations of the BIO_sendmmsg() and BIO_recvmmsg() BIO methods might
  103. always process at most one message at a time, for example when OS-level
  104. functionality to transmit or receive multiple messages at a time is not
  105. available.
  106. =head1 RETURN VALUES
  107. On success, the functions BIO_sendmmsg() and BIO_recvmmsg() return 1 and write
  108. the number of messages successfully processed (which need not be nonzero) to
  109. I<msgs_processed>. Where a positive value n is written to I<msgs_processed>, all
  110. entries in the B<BIO_MSG> array from 0 through n-1 inclusive have their
  111. I<data_len> and I<flags> fields updated with the results of the operation on
  112. that message. If the call was to BIO_recvmmsg() and the I<peer> or I<local>
  113. fields of that message are non-NULL, the B<BIO_ADDR> structures they point to
  114. are written with the relevant address.
  115. On failure, the functions BIO_sendmmsg() and BIO_recvmmsg() return 0 and write
  116. zero to I<msgs_processed>. Thus I<msgs_processed> is always written regardless
  117. of the outcome of the function call.
  118. If BIO_sendmmsg() and BIO_recvmmsg() fail, they always raise an B<ERR_LIB_BIO>
  119. error using L<ERR_raise(3)>. Any error may be raised, but the following in
  120. particular may be noted:
  121. =over 2
  122. =item B<BIO_R_LOCAL_ADDR_NOT_AVAILABLE>
  123. The I<local> field was set to a non-NULL value, but local address support is not
  124. available or not enabled on the BIO.
  125. =item B<BIO_R_PEER_ADDR_NOT_AVAILABLE>
  126. The I<peer> field was set to a non-NULL value, but peer address support is not
  127. available on the BIO.
  128. =item B<BIO_R_UNSUPPORTED_METHOD>
  129. The BIO_sendmmsg() or BIO_recvmmsg() method is not supported on the BIO.
  130. =item B<BIO_R_NON_FATAL>
  131. The call failed due to a transient, non-fatal error (for example, because the
  132. BIO is in nonblocking mode and the call would otherwise have blocked).
  133. Implementations of this interface which do not make system calls and thereby
  134. pass through system error codes using B<ERR_LIB_SYS> (for example, memory-based
  135. implementations) should issue this reason code to indicate a transient failure.
  136. However, users of this interface should not test for this reason code directly,
  137. as there are multiple possible packed error codes representing a transient
  138. failure; use BIO_err_is_non_fatal() instead (discussed below).
  139. =item Socket errors
  140. OS-level socket errors are reported using an error with library code
  141. B<ERR_LIB_SYS>; for a packed error code B<errcode> where
  142. C<ERR_SYSTEM_ERROR(errcode) == 1>, the OS-level socket error code can be
  143. retrieved using C<ERR_GET_REASON(errcode)>. The packed error code can be
  144. retrieved by calling L<ERR_peek_last_error(3)> after the call to BIO_sendmmsg()
  145. or BIO_recvmmsg() returns 0.
  146. =item Non-fatal errors
  147. Whether an error is transient can be determined by passing the packed error code
  148. to BIO_err_is_non_fatal(). Callers should do this instead of testing the reason
  149. code directly, as there are many possible error codes which can indicate a
  150. transient error, many of which are system specific.
  151. =back
  152. BIO_dgram_set_local_addr_enable() returns 1 if local address support was
  153. successfully enabled or disabled and 0 otherwise.
  154. BIO_dgram_get_local_addr_enable() returns 1 if the local address support enable
  155. flag was successfully retrieved.
  156. BIO_dgram_get_local_addr_cap() returns 1 if the B<BIO> can support local
  157. addresses.
  158. BIO_err_is_non_fatal() returns 1 if the passed packed error code represents an
  159. error which is transient in nature.
  160. =head1 HISTORY
  161. These functions were added in OpenSSL 3.2.
  162. =head1 COPYRIGHT
  163. Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
  164. Licensed under the Apache License 2.0 (the "License"). You may not use
  165. this file except in compliance with the License. You can obtain a copy
  166. in the file LICENSE in the source distribution or at
  167. L<https://www.openssl.org/source/license.html>.
  168. =cut