1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5e71b7053SJung-uk KimBIO_s_accept, BIO_set_accept_name, BIO_set_accept_port, BIO_get_accept_name, 6e71b7053SJung-uk KimBIO_get_accept_port, BIO_new_accept, BIO_set_nbio_accept, BIO_set_accept_bios, 7e71b7053SJung-uk KimBIO_get_peer_name, BIO_get_peer_port, 8e71b7053SJung-uk KimBIO_get_accept_ip_family, BIO_set_accept_ip_family, 9e71b7053SJung-uk KimBIO_set_bind_mode, BIO_get_bind_mode, BIO_do_accept - accept BIO 10e71b7053SJung-uk Kim 11e71b7053SJung-uk Kim=head1 SYNOPSIS 12e71b7053SJung-uk Kim 13e71b7053SJung-uk Kim #include <openssl/bio.h> 14e71b7053SJung-uk Kim 15e71b7053SJung-uk Kim const BIO_METHOD *BIO_s_accept(void); 16e71b7053SJung-uk Kim 17e71b7053SJung-uk Kim long BIO_set_accept_name(BIO *b, char *name); 18e71b7053SJung-uk Kim char *BIO_get_accept_name(BIO *b); 19e71b7053SJung-uk Kim 20e71b7053SJung-uk Kim long BIO_set_accept_port(BIO *b, char *port); 21e71b7053SJung-uk Kim char *BIO_get_accept_port(BIO *b); 22e71b7053SJung-uk Kim 23e71b7053SJung-uk Kim BIO *BIO_new_accept(char *host_port); 24e71b7053SJung-uk Kim 25e71b7053SJung-uk Kim long BIO_set_nbio_accept(BIO *b, int n); 26e71b7053SJung-uk Kim long BIO_set_accept_bios(BIO *b, char *bio); 27e71b7053SJung-uk Kim 28e71b7053SJung-uk Kim char *BIO_get_peer_name(BIO *b); 29e71b7053SJung-uk Kim char *BIO_get_peer_port(BIO *b); 30e71b7053SJung-uk Kim long BIO_get_accept_ip_family(BIO *b); 31e71b7053SJung-uk Kim long BIO_set_accept_ip_family(BIO *b, long family); 32e71b7053SJung-uk Kim 33e71b7053SJung-uk Kim long BIO_set_bind_mode(BIO *b, long mode); 34e71b7053SJung-uk Kim long BIO_get_bind_mode(BIO *b); 35e71b7053SJung-uk Kim 36e71b7053SJung-uk Kim int BIO_do_accept(BIO *b); 37e71b7053SJung-uk Kim 38e71b7053SJung-uk Kim=head1 DESCRIPTION 39e71b7053SJung-uk Kim 40e71b7053SJung-uk KimBIO_s_accept() returns the accept BIO method. This is a wrapper 41e71b7053SJung-uk Kimround the platform's TCP/IP socket accept routines. 42e71b7053SJung-uk Kim 43e71b7053SJung-uk KimUsing accept BIOs, TCP/IP connections can be accepted and data 44e71b7053SJung-uk Kimtransferred using only BIO routines. In this way any platform 45e71b7053SJung-uk Kimspecific operations are hidden by the BIO abstraction. 46e71b7053SJung-uk Kim 47e71b7053SJung-uk KimRead and write operations on an accept BIO will perform I/O 48e71b7053SJung-uk Kimon the underlying connection. If no connection is established 49e71b7053SJung-uk Kimand the port (see below) is set up properly then the BIO 50e71b7053SJung-uk Kimwaits for an incoming connection. 51e71b7053SJung-uk Kim 52e71b7053SJung-uk KimAccept BIOs support BIO_puts() but not BIO_gets(). 53e71b7053SJung-uk Kim 54e71b7053SJung-uk KimIf the close flag is set on an accept BIO then any active 55e71b7053SJung-uk Kimconnection on that chain is shutdown and the socket closed when 56e71b7053SJung-uk Kimthe BIO is freed. 57e71b7053SJung-uk Kim 58e71b7053SJung-uk KimCalling BIO_reset() on an accept BIO will close any active 59e71b7053SJung-uk Kimconnection and reset the BIO into a state where it awaits another 60e71b7053SJung-uk Kimincoming connection. 61e71b7053SJung-uk Kim 62e71b7053SJung-uk KimBIO_get_fd() and BIO_set_fd() can be called to retrieve or set 63e71b7053SJung-uk Kimthe accept socket. See L<BIO_s_fd(3)> 64e71b7053SJung-uk Kim 65e71b7053SJung-uk KimBIO_set_accept_name() uses the string B<name> to set the accept 66e71b7053SJung-uk Kimname. The name is represented as a string of the form "host:port", 67e71b7053SJung-uk Kimwhere "host" is the interface to use and "port" is the port. 68e71b7053SJung-uk KimThe host can be "*" or empty which is interpreted as meaning 69e71b7053SJung-uk Kimany interface. If the host is an IPv6 address, it has to be 70e71b7053SJung-uk Kimenclosed in brackets, for example "[::1]:https". "port" has the 71e71b7053SJung-uk Kimsame syntax as the port specified in BIO_set_conn_port() for 72e71b7053SJung-uk Kimconnect BIOs, that is it can be a numerical port string or a 73e71b7053SJung-uk Kimstring to lookup using getservbyname() and a string table. 74e71b7053SJung-uk Kim 75e71b7053SJung-uk KimBIO_set_accept_port() uses the string B<port> to set the accept 76e71b7053SJung-uk Kimport. "port" has the same syntax as the port specified in 77e71b7053SJung-uk KimBIO_set_conn_port() for connect BIOs, that is it can be a numerical 78e71b7053SJung-uk Kimport string or a string to lookup using getservbyname() and a string 79e71b7053SJung-uk Kimtable. 80e71b7053SJung-uk Kim 81e71b7053SJung-uk KimBIO_new_accept() combines BIO_new() and BIO_set_accept_name() into 82e71b7053SJung-uk Kima single call: that is it creates a new accept BIO with port 83e71b7053SJung-uk KimB<host_port>. 84e71b7053SJung-uk Kim 85e71b7053SJung-uk KimBIO_set_nbio_accept() sets the accept socket to blocking mode 86e71b7053SJung-uk Kim(the default) if B<n> is 0 or non blocking mode if B<n> is 1. 87e71b7053SJung-uk Kim 88e71b7053SJung-uk KimBIO_set_accept_bios() can be used to set a chain of BIOs which 89e71b7053SJung-uk Kimwill be duplicated and prepended to the chain when an incoming 90e71b7053SJung-uk Kimconnection is received. This is useful if, for example, a 91e71b7053SJung-uk Kimbuffering or SSL BIO is required for each connection. The 92e71b7053SJung-uk Kimchain of BIOs must not be freed after this call, they will 93e71b7053SJung-uk Kimbe automatically freed when the accept BIO is freed. 94e71b7053SJung-uk Kim 95e71b7053SJung-uk KimBIO_set_bind_mode() and BIO_get_bind_mode() set and retrieve 96e71b7053SJung-uk Kimthe current bind mode. If B<BIO_BIND_NORMAL> (the default) is set 97e71b7053SJung-uk Kimthen another socket cannot be bound to the same port. If 98e71b7053SJung-uk KimB<BIO_BIND_REUSEADDR> is set then other sockets can bind to the 99e71b7053SJung-uk Kimsame port. If B<BIO_BIND_REUSEADDR_IF_UNUSED> is set then and 100e71b7053SJung-uk Kimattempt is first made to use BIO_BIN_NORMAL, if this fails 101e71b7053SJung-uk Kimand the port is not in use then a second attempt is made 102e71b7053SJung-uk Kimusing B<BIO_BIND_REUSEADDR>. 103e71b7053SJung-uk Kim 104e71b7053SJung-uk KimBIO_do_accept() serves two functions. When it is first 105e71b7053SJung-uk Kimcalled, after the accept BIO has been setup, it will attempt 106e71b7053SJung-uk Kimto create the accept socket and bind an address to it. Second 107e71b7053SJung-uk Kimand subsequent calls to BIO_do_accept() will await an incoming 108e71b7053SJung-uk Kimconnection, or request a retry in non blocking mode. 109e71b7053SJung-uk Kim 110e71b7053SJung-uk Kim=head1 NOTES 111e71b7053SJung-uk Kim 112e71b7053SJung-uk KimWhen an accept BIO is at the end of a chain it will await an 113e71b7053SJung-uk Kimincoming connection before processing I/O calls. When an accept 114e71b7053SJung-uk KimBIO is not at then end of a chain it passes I/O calls to the next 115e71b7053SJung-uk KimBIO in the chain. 116e71b7053SJung-uk Kim 117e71b7053SJung-uk KimWhen a connection is established a new socket BIO is created for 118e71b7053SJung-uk Kimthe connection and appended to the chain. That is the chain is now 119e71b7053SJung-uk Kimaccept->socket. This effectively means that attempting I/O on 120e71b7053SJung-uk Kiman initial accept socket will await an incoming connection then 121e71b7053SJung-uk Kimperform I/O on it. 122e71b7053SJung-uk Kim 123e71b7053SJung-uk KimIf any additional BIOs have been set using BIO_set_accept_bios() 124e71b7053SJung-uk Kimthen they are placed between the socket and the accept BIO, 125e71b7053SJung-uk Kimthat is the chain will be accept->otherbios->socket. 126e71b7053SJung-uk Kim 127e71b7053SJung-uk KimIf a server wishes to process multiple connections (as is normally 128e71b7053SJung-uk Kimthe case) then the accept BIO must be made available for further 129e71b7053SJung-uk Kimincoming connections. This can be done by waiting for a connection and 130e71b7053SJung-uk Kimthen calling: 131e71b7053SJung-uk Kim 132e71b7053SJung-uk Kim connection = BIO_pop(accept); 133e71b7053SJung-uk Kim 134e71b7053SJung-uk KimAfter this call B<connection> will contain a BIO for the recently 135e71b7053SJung-uk Kimestablished connection and B<accept> will now be a single BIO 136e71b7053SJung-uk Kimagain which can be used to await further incoming connections. 137e71b7053SJung-uk KimIf no further connections will be accepted the B<accept> can 138e71b7053SJung-uk Kimbe freed using BIO_free(). 139e71b7053SJung-uk Kim 140e71b7053SJung-uk KimIf only a single connection will be processed it is possible to 141e71b7053SJung-uk Kimperform I/O using the accept BIO itself. This is often undesirable 142e71b7053SJung-uk Kimhowever because the accept BIO will still accept additional incoming 143e71b7053SJung-uk Kimconnections. This can be resolved by using BIO_pop() (see above) 144e71b7053SJung-uk Kimand freeing up the accept BIO after the initial connection. 145e71b7053SJung-uk Kim 146e71b7053SJung-uk KimIf the underlying accept socket is non-blocking and BIO_do_accept() is 147e71b7053SJung-uk Kimcalled to await an incoming connection it is possible for 148e71b7053SJung-uk KimBIO_should_io_special() with the reason BIO_RR_ACCEPT. If this happens 149e71b7053SJung-uk Kimthen it is an indication that an accept attempt would block: the application 150e71b7053SJung-uk Kimshould take appropriate action to wait until the underlying socket has 151e71b7053SJung-uk Kimaccepted a connection and retry the call. 152e71b7053SJung-uk Kim 153e71b7053SJung-uk KimBIO_set_accept_name(), BIO_get_accept_name(), BIO_set_accept_port(), 154e71b7053SJung-uk KimBIO_get_accept_port(), BIO_set_nbio_accept(), BIO_set_accept_bios(), 155e71b7053SJung-uk KimBIO_get_peer_name(), BIO_get_peer_port(), 156e71b7053SJung-uk KimBIO_get_accept_ip_family(), BIO_set_accept_ip_family(), 157e71b7053SJung-uk KimBIO_set_bind_mode(), BIO_get_bind_mode() and BIO_do_accept() are macros. 158e71b7053SJung-uk Kim 159e71b7053SJung-uk Kim=head1 RETURN VALUES 160e71b7053SJung-uk Kim 161e71b7053SJung-uk KimBIO_do_accept(), 162e71b7053SJung-uk KimBIO_set_accept_name(), BIO_set_accept_port(), BIO_set_nbio_accept(), 163e71b7053SJung-uk KimBIO_set_accept_bios(), BIO_set_accept_ip_family(), and BIO_set_bind_mode() 164e71b7053SJung-uk Kimreturn 1 for success and 0 or -1 for failure. 165e71b7053SJung-uk Kim 166e71b7053SJung-uk KimBIO_get_accept_name() returns the accept name or NULL on error. 167e71b7053SJung-uk KimBIO_get_peer_name() returns the peer name or NULL on error. 168e71b7053SJung-uk Kim 169e71b7053SJung-uk KimBIO_get_accept_port() returns the accept port as a string or NULL on error. 170e71b7053SJung-uk KimBIO_get_peer_port() returns the peer port as a string or NULL on error. 171e71b7053SJung-uk KimBIO_get_accept_ip_family() returns the IP family or -1 on error. 172e71b7053SJung-uk Kim 173e71b7053SJung-uk KimBIO_get_bind_mode() returns the set of B<BIO_BIND> flags, or -1 on failure. 174e71b7053SJung-uk Kim 175e71b7053SJung-uk KimBIO_new_accept() returns a BIO or NULL on error. 176e71b7053SJung-uk Kim 177*da327cd2SJung-uk Kim=head1 EXAMPLES 178e71b7053SJung-uk Kim 179e71b7053SJung-uk KimThis example accepts two connections on port 4444, sends messages 180e71b7053SJung-uk Kimdown each and finally closes both down. 181e71b7053SJung-uk Kim 182e71b7053SJung-uk Kim BIO *abio, *cbio, *cbio2; 183e71b7053SJung-uk Kim 184e71b7053SJung-uk Kim /* First call to BIO_accept() sets up accept BIO */ 185e71b7053SJung-uk Kim abio = BIO_new_accept("4444"); 186e71b7053SJung-uk Kim if (BIO_do_accept(abio) <= 0) { 187e71b7053SJung-uk Kim fprintf(stderr, "Error setting up accept\n"); 188e71b7053SJung-uk Kim ERR_print_errors_fp(stderr); 189e71b7053SJung-uk Kim exit(1); 190e71b7053SJung-uk Kim } 191e71b7053SJung-uk Kim 192e71b7053SJung-uk Kim /* Wait for incoming connection */ 193e71b7053SJung-uk Kim if (BIO_do_accept(abio) <= 0) { 194e71b7053SJung-uk Kim fprintf(stderr, "Error accepting connection\n"); 195e71b7053SJung-uk Kim ERR_print_errors_fp(stderr); 196e71b7053SJung-uk Kim exit(1); 197e71b7053SJung-uk Kim } 198e71b7053SJung-uk Kim fprintf(stderr, "Connection 1 established\n"); 199e71b7053SJung-uk Kim 200e71b7053SJung-uk Kim /* Retrieve BIO for connection */ 201e71b7053SJung-uk Kim cbio = BIO_pop(abio); 202e71b7053SJung-uk Kim BIO_puts(cbio, "Connection 1: Sending out Data on initial connection\n"); 203e71b7053SJung-uk Kim fprintf(stderr, "Sent out data on connection 1\n"); 204e71b7053SJung-uk Kim 205e71b7053SJung-uk Kim /* Wait for another connection */ 206e71b7053SJung-uk Kim if (BIO_do_accept(abio) <= 0) { 207e71b7053SJung-uk Kim fprintf(stderr, "Error accepting connection\n"); 208e71b7053SJung-uk Kim ERR_print_errors_fp(stderr); 209e71b7053SJung-uk Kim exit(1); 210e71b7053SJung-uk Kim } 211e71b7053SJung-uk Kim fprintf(stderr, "Connection 2 established\n"); 212e71b7053SJung-uk Kim 213e71b7053SJung-uk Kim /* Close accept BIO to refuse further connections */ 214e71b7053SJung-uk Kim cbio2 = BIO_pop(abio); 215e71b7053SJung-uk Kim BIO_free(abio); 216e71b7053SJung-uk Kim BIO_puts(cbio2, "Connection 2: Sending out Data on second\n"); 217e71b7053SJung-uk Kim fprintf(stderr, "Sent out data on connection 2\n"); 218e71b7053SJung-uk Kim 219e71b7053SJung-uk Kim BIO_puts(cbio, "Connection 1: Second connection established\n"); 220e71b7053SJung-uk Kim 221e71b7053SJung-uk Kim /* Close the two established connections */ 222e71b7053SJung-uk Kim BIO_free(cbio); 223e71b7053SJung-uk Kim BIO_free(cbio2); 224e71b7053SJung-uk Kim 225e71b7053SJung-uk Kim=head1 COPYRIGHT 226e71b7053SJung-uk Kim 227*da327cd2SJung-uk KimCopyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved. 228e71b7053SJung-uk Kim 229e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License"). You may not use 230e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 231e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 232e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 233e71b7053SJung-uk Kim 234e71b7053SJung-uk Kim=cut 235