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