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