BIO_s_accept.pod 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. =pod
  2. =head1 NAME
  3. BIO_s_accept, BIO_set_accept_name, BIO_set_accept_port, BIO_get_accept_name,
  4. BIO_get_accept_port, BIO_new_accept, BIO_set_nbio_accept, BIO_set_accept_bios,
  5. BIO_get_peer_name, BIO_get_peer_port,
  6. BIO_get_accept_ip_family, BIO_set_accept_ip_family,
  7. BIO_set_bind_mode, BIO_get_bind_mode, BIO_do_accept - accept BIO
  8. =head1 SYNOPSIS
  9. #include <openssl/bio.h>
  10. const BIO_METHOD *BIO_s_accept(void);
  11. long BIO_set_accept_name(BIO *b, char *name);
  12. char *BIO_get_accept_name(BIO *b);
  13. long BIO_set_accept_port(BIO *b, char *port);
  14. char *BIO_get_accept_port(BIO *b);
  15. BIO *BIO_new_accept(char *host_port);
  16. long BIO_set_nbio_accept(BIO *b, int n);
  17. long BIO_set_accept_bios(BIO *b, char *bio);
  18. char *BIO_get_peer_name(BIO *b);
  19. char *BIO_get_peer_port(BIO *b);
  20. long BIO_get_accept_ip_family(BIO *b);
  21. long BIO_set_accept_ip_family(BIO *b, long family);
  22. long BIO_set_bind_mode(BIO *b, long mode);
  23. long BIO_get_bind_mode(BIO *b);
  24. int BIO_do_accept(BIO *b);
  25. =head1 DESCRIPTION
  26. BIO_s_accept() returns the accept BIO method. This is a wrapper
  27. round the platform's TCP/IP socket accept routines.
  28. Using accept BIOs, TCP/IP connections can be accepted and data
  29. transferred using only BIO routines. In this way any platform
  30. specific operations are hidden by the BIO abstraction.
  31. Read and write operations on an accept BIO will perform I/O
  32. on the underlying connection. If no connection is established
  33. and the port (see below) is set up properly then the BIO
  34. waits for an incoming connection.
  35. Accept BIOs support BIO_puts() but not BIO_gets().
  36. If the close flag is set on an accept BIO then any active
  37. connection on that chain is shutdown and the socket closed when
  38. the BIO is freed.
  39. Calling BIO_reset() on an accept BIO will close any active
  40. connection and reset the BIO into a state where it awaits another
  41. incoming connection.
  42. BIO_get_fd() and BIO_set_fd() can be called to retrieve or set
  43. the accept socket. See L<BIO_s_fd(3)>
  44. BIO_set_accept_name() uses the string B<name> to set the accept
  45. name. The name is represented as a string of the form "host:port",
  46. where "host" is the interface to use and "port" is the port.
  47. The host can be "*" or empty which is interpreted as meaning
  48. any interface. If the host is an IPv6 address, it has to be
  49. enclosed in brackets, for example "[::1]:https". "port" has the
  50. same syntax as the port specified in BIO_set_conn_port() for
  51. connect BIOs, that is it can be a numerical port string or a
  52. string to lookup using getservbyname() and a string table.
  53. BIO_set_accept_port() uses the string B<port> to set the accept
  54. port. "port" has the same syntax as the port specified in
  55. BIO_set_conn_port() for connect BIOs, that is it can be a numerical
  56. port string or a string to lookup using getservbyname() and a string
  57. table.
  58. BIO_new_accept() combines BIO_new() and BIO_set_accept_name() into
  59. a single call: that is it creates a new accept BIO with port
  60. B<host_port>.
  61. BIO_set_nbio_accept() sets the accept socket to blocking mode
  62. (the default) if B<n> is 0 or non blocking mode if B<n> is 1.
  63. BIO_set_accept_bios() can be used to set a chain of BIOs which
  64. will be duplicated and prepended to the chain when an incoming
  65. connection is received. This is useful if, for example, a
  66. buffering or SSL BIO is required for each connection. The
  67. chain of BIOs must not be freed after this call, they will
  68. be automatically freed when the accept BIO is freed.
  69. BIO_set_bind_mode() and BIO_get_bind_mode() set and retrieve
  70. the current bind mode. If B<BIO_BIND_NORMAL> (the default) is set
  71. then another socket cannot be bound to the same port. If
  72. B<BIO_BIND_REUSEADDR> is set then other sockets can bind to the
  73. same port. If B<BIO_BIND_REUSEADDR_IF_UNUSED> is set then and
  74. attempt is first made to use BIO_BIN_NORMAL, if this fails
  75. and the port is not in use then a second attempt is made
  76. using B<BIO_BIND_REUSEADDR>.
  77. BIO_do_accept() serves two functions. When it is first
  78. called, after the accept BIO has been setup, it will attempt
  79. to create the accept socket and bind an address to it. Second
  80. and subsequent calls to BIO_do_accept() will await an incoming
  81. connection, or request a retry in non blocking mode.
  82. =head1 NOTES
  83. When an accept BIO is at the end of a chain it will await an
  84. incoming connection before processing I/O calls. When an accept
  85. BIO is not at then end of a chain it passes I/O calls to the next
  86. BIO in the chain.
  87. When a connection is established a new socket BIO is created for
  88. the connection and appended to the chain. That is the chain is now
  89. accept->socket. This effectively means that attempting I/O on
  90. an initial accept socket will await an incoming connection then
  91. perform I/O on it.
  92. If any additional BIOs have been set using BIO_set_accept_bios()
  93. then they are placed between the socket and the accept BIO,
  94. that is the chain will be accept->otherbios->socket.
  95. If a server wishes to process multiple connections (as is normally
  96. the case) then the accept BIO must be made available for further
  97. incoming connections. This can be done by waiting for a connection and
  98. then calling:
  99. connection = BIO_pop(accept);
  100. After this call B<connection> will contain a BIO for the recently
  101. established connection and B<accept> will now be a single BIO
  102. again which can be used to await further incoming connections.
  103. If no further connections will be accepted the B<accept> can
  104. be freed using BIO_free().
  105. If only a single connection will be processed it is possible to
  106. perform I/O using the accept BIO itself. This is often undesirable
  107. however because the accept BIO will still accept additional incoming
  108. connections. This can be resolved by using BIO_pop() (see above)
  109. and freeing up the accept BIO after the initial connection.
  110. If the underlying accept socket is non-blocking and BIO_do_accept() is
  111. called to await an incoming connection it is possible for
  112. BIO_should_io_special() with the reason BIO_RR_ACCEPT. If this happens
  113. then it is an indication that an accept attempt would block: the application
  114. should take appropriate action to wait until the underlying socket has
  115. accepted a connection and retry the call.
  116. BIO_set_accept_name(), BIO_get_accept_name(), BIO_set_accept_port(),
  117. BIO_get_accept_port(), BIO_set_nbio_accept(), BIO_set_accept_bios(),
  118. BIO_get_peer_name(), BIO_get_peer_port(),
  119. BIO_get_accept_ip_family(), BIO_set_accept_ip_family(),
  120. BIO_set_bind_mode(), BIO_get_bind_mode() and BIO_do_accept() are macros.
  121. =head1 RETURN VALUES
  122. BIO_do_accept(),
  123. BIO_set_accept_name(), BIO_set_accept_port(), BIO_set_nbio_accept(),
  124. BIO_set_accept_bios(), BIO_set_accept_ip_family(), and BIO_set_bind_mode()
  125. return 1 for success and 0 or -1 for failure.
  126. BIO_get_accept_name() returns the accept name or NULL on error.
  127. BIO_get_peer_name() returns the peer name or NULL on error.
  128. BIO_get_accept_port() returns the accept port as a string or NULL on error.
  129. BIO_get_peer_port() returns the peer port as a string or NULL on error.
  130. BIO_get_accept_ip_family() returns the IP family or -1 on error.
  131. BIO_get_bind_mode() returns the set of B<BIO_BIND> flags, or -1 on failure.
  132. BIO_new_accept() returns a BIO or NULL on error.
  133. =head1 EXAMPLE
  134. This example accepts two connections on port 4444, sends messages
  135. down each and finally closes both down.
  136. BIO *abio, *cbio, *cbio2;
  137. /* First call to BIO_accept() sets up accept BIO */
  138. abio = BIO_new_accept("4444");
  139. if (BIO_do_accept(abio) <= 0) {
  140. fprintf(stderr, "Error setting up accept\n");
  141. ERR_print_errors_fp(stderr);
  142. exit(1);
  143. }
  144. /* Wait for incoming connection */
  145. if (BIO_do_accept(abio) <= 0) {
  146. fprintf(stderr, "Error accepting connection\n");
  147. ERR_print_errors_fp(stderr);
  148. exit(1);
  149. }
  150. fprintf(stderr, "Connection 1 established\n");
  151. /* Retrieve BIO for connection */
  152. cbio = BIO_pop(abio);
  153. BIO_puts(cbio, "Connection 1: Sending out Data on initial connection\n");
  154. fprintf(stderr, "Sent out data on connection 1\n");
  155. /* Wait for another connection */
  156. if (BIO_do_accept(abio) <= 0) {
  157. fprintf(stderr, "Error accepting connection\n");
  158. ERR_print_errors_fp(stderr);
  159. exit(1);
  160. }
  161. fprintf(stderr, "Connection 2 established\n");
  162. /* Close accept BIO to refuse further connections */
  163. cbio2 = BIO_pop(abio);
  164. BIO_free(abio);
  165. BIO_puts(cbio2, "Connection 2: Sending out Data on second\n");
  166. fprintf(stderr, "Sent out data on connection 2\n");
  167. BIO_puts(cbio, "Connection 1: Second connection established\n");
  168. /* Close the two established connections */
  169. BIO_free(cbio);
  170. BIO_free(cbio2);
  171. =head1 COPYRIGHT
  172. Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
  173. Licensed under the OpenSSL license (the "License"). You may not use
  174. this file except in compliance with the License. You can obtain a copy
  175. in the file LICENSE in the source distribution or at
  176. L<https://www.openssl.org/source/license.html>.
  177. =cut