BIO_s_accept.pod 6.9 KB

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