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