xref: /freebsd/crypto/openssl/doc/man3/BIO_s_accept.pod (revision 0d0c8621fd181e507f0fb50ffcca606faf66a8c2)
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
76*b077aed3SPierre Proncheryport of BIO I<b>.  "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.
80*b077aed3SPierre ProncheryIf the given port is C<0> then a random available port is chosen.
81*b077aed3SPierre ProncheryIt may be queried using BIO_sock_info() and L<BIO_ADDR_service_string(3)>.
82e71b7053SJung-uk Kim
83e71b7053SJung-uk KimBIO_new_accept() combines BIO_new() and BIO_set_accept_name() into
84e71b7053SJung-uk Kima single call: that is it creates a new accept BIO with port
85e71b7053SJung-uk KimB<host_port>.
86e71b7053SJung-uk Kim
87e71b7053SJung-uk KimBIO_set_nbio_accept() sets the accept socket to blocking mode
88e71b7053SJung-uk Kim(the default) if B<n> is 0 or non blocking mode if B<n> is 1.
89e71b7053SJung-uk Kim
90e71b7053SJung-uk KimBIO_set_accept_bios() can be used to set a chain of BIOs which
91e71b7053SJung-uk Kimwill be duplicated and prepended to the chain when an incoming
92e71b7053SJung-uk Kimconnection is received. This is useful if, for example, a
93e71b7053SJung-uk Kimbuffering or SSL BIO is required for each connection. The
94e71b7053SJung-uk Kimchain of BIOs must not be freed after this call, they will
95e71b7053SJung-uk Kimbe automatically freed when the accept BIO is freed.
96e71b7053SJung-uk Kim
97*b077aed3SPierre ProncheryBIO_get_accept_ip_family() returns the IP family accepted by the BIO I<b>,
98*b077aed3SPierre Proncherywhich may be B<BIO_FAMILY_IPV4>, B<BIO_FAMILY_IPV6>, or B<BIO_FAMILY_IPANY>.
99*b077aed3SPierre Pronchery
100*b077aed3SPierre ProncheryBIO_set_accept_ip_family() sets the IP family I<family> accepted by BIO I<b>.
101*b077aed3SPierre ProncheryThe default is B<BIO_FAMILY_IPANY>.
102*b077aed3SPierre Pronchery
103e71b7053SJung-uk KimBIO_set_bind_mode() and BIO_get_bind_mode() set and retrieve
104e71b7053SJung-uk Kimthe current bind mode. If B<BIO_BIND_NORMAL> (the default) is set
105e71b7053SJung-uk Kimthen another socket cannot be bound to the same port. If
106e71b7053SJung-uk KimB<BIO_BIND_REUSEADDR> is set then other sockets can bind to the
107e71b7053SJung-uk Kimsame port. If B<BIO_BIND_REUSEADDR_IF_UNUSED> is set then and
108e71b7053SJung-uk Kimattempt is first made to use BIO_BIN_NORMAL, if this fails
109e71b7053SJung-uk Kimand the port is not in use then a second attempt is made
110e71b7053SJung-uk Kimusing B<BIO_BIND_REUSEADDR>.
111e71b7053SJung-uk Kim
112e71b7053SJung-uk KimBIO_do_accept() serves two functions. When it is first
113e71b7053SJung-uk Kimcalled, after the accept BIO has been setup, it will attempt
114e71b7053SJung-uk Kimto create the accept socket and bind an address to it. Second
115e71b7053SJung-uk Kimand subsequent calls to BIO_do_accept() will await an incoming
116e71b7053SJung-uk Kimconnection, or request a retry in non blocking mode.
117e71b7053SJung-uk Kim
118e71b7053SJung-uk Kim=head1 NOTES
119e71b7053SJung-uk Kim
120e71b7053SJung-uk KimWhen an accept BIO is at the end of a chain it will await an
121e71b7053SJung-uk Kimincoming connection before processing I/O calls. When an accept
122e71b7053SJung-uk KimBIO is not at then end of a chain it passes I/O calls to the next
123e71b7053SJung-uk KimBIO in the chain.
124e71b7053SJung-uk Kim
125e71b7053SJung-uk KimWhen a connection is established a new socket BIO is created for
126e71b7053SJung-uk Kimthe connection and appended to the chain. That is the chain is now
127e71b7053SJung-uk Kimaccept->socket. This effectively means that attempting I/O on
128e71b7053SJung-uk Kiman initial accept socket will await an incoming connection then
129e71b7053SJung-uk Kimperform I/O on it.
130e71b7053SJung-uk Kim
131e71b7053SJung-uk KimIf any additional BIOs have been set using BIO_set_accept_bios()
132e71b7053SJung-uk Kimthen they are placed between the socket and the accept BIO,
133e71b7053SJung-uk Kimthat is the chain will be accept->otherbios->socket.
134e71b7053SJung-uk Kim
135e71b7053SJung-uk KimIf a server wishes to process multiple connections (as is normally
136e71b7053SJung-uk Kimthe case) then the accept BIO must be made available for further
137e71b7053SJung-uk Kimincoming connections. This can be done by waiting for a connection and
138e71b7053SJung-uk Kimthen calling:
139e71b7053SJung-uk Kim
140e71b7053SJung-uk Kim connection = BIO_pop(accept);
141e71b7053SJung-uk Kim
142e71b7053SJung-uk KimAfter this call B<connection> will contain a BIO for the recently
143e71b7053SJung-uk Kimestablished connection and B<accept> will now be a single BIO
144e71b7053SJung-uk Kimagain which can be used to await further incoming connections.
145e71b7053SJung-uk KimIf no further connections will be accepted the B<accept> can
146e71b7053SJung-uk Kimbe freed using BIO_free().
147e71b7053SJung-uk Kim
148e71b7053SJung-uk KimIf only a single connection will be processed it is possible to
149e71b7053SJung-uk Kimperform I/O using the accept BIO itself. This is often undesirable
150e71b7053SJung-uk Kimhowever because the accept BIO will still accept additional incoming
151e71b7053SJung-uk Kimconnections. This can be resolved by using BIO_pop() (see above)
152e71b7053SJung-uk Kimand freeing up the accept BIO after the initial connection.
153e71b7053SJung-uk Kim
15458f35182SJung-uk KimIf the underlying accept socket is nonblocking and BIO_do_accept() is
155e71b7053SJung-uk Kimcalled to await an incoming connection it is possible for
156e71b7053SJung-uk KimBIO_should_io_special() with the reason BIO_RR_ACCEPT. If this happens
157e71b7053SJung-uk Kimthen it is an indication that an accept attempt would block: the application
158e71b7053SJung-uk Kimshould take appropriate action to wait until the underlying socket has
159e71b7053SJung-uk Kimaccepted a connection and retry the call.
160e71b7053SJung-uk Kim
161e71b7053SJung-uk KimBIO_set_accept_name(), BIO_get_accept_name(), BIO_set_accept_port(),
162e71b7053SJung-uk KimBIO_get_accept_port(), BIO_set_nbio_accept(), BIO_set_accept_bios(),
163e71b7053SJung-uk KimBIO_get_peer_name(), BIO_get_peer_port(),
164e71b7053SJung-uk KimBIO_get_accept_ip_family(), BIO_set_accept_ip_family(),
165e71b7053SJung-uk KimBIO_set_bind_mode(), BIO_get_bind_mode() and BIO_do_accept() are macros.
166e71b7053SJung-uk Kim
167e71b7053SJung-uk Kim=head1 RETURN VALUES
168e71b7053SJung-uk Kim
169e71b7053SJung-uk KimBIO_do_accept(),
170e71b7053SJung-uk KimBIO_set_accept_name(), BIO_set_accept_port(), BIO_set_nbio_accept(),
171e71b7053SJung-uk KimBIO_set_accept_bios(), BIO_set_accept_ip_family(), and BIO_set_bind_mode()
172*b077aed3SPierre Proncheryreturn 1 for success and <= 0 for failure.
173e71b7053SJung-uk Kim
174e71b7053SJung-uk KimBIO_get_accept_name() returns the accept name or NULL on error.
175e71b7053SJung-uk KimBIO_get_peer_name() returns the peer name or NULL on error.
176e71b7053SJung-uk Kim
177e71b7053SJung-uk KimBIO_get_accept_port() returns the accept port as a string or NULL on error.
178e71b7053SJung-uk KimBIO_get_peer_port() returns the peer port as a string or NULL on error.
179*b077aed3SPierre ProncheryBIO_get_accept_ip_family() returns the IP family or <= 0 on error.
180e71b7053SJung-uk Kim
181*b077aed3SPierre ProncheryBIO_get_bind_mode() returns the set of B<BIO_BIND> flags, or <= 0 on failure.
182e71b7053SJung-uk Kim
183e71b7053SJung-uk KimBIO_new_accept() returns a BIO or NULL on error.
184e71b7053SJung-uk Kim
185da327cd2SJung-uk Kim=head1 EXAMPLES
186e71b7053SJung-uk Kim
187e71b7053SJung-uk KimThis example accepts two connections on port 4444, sends messages
188e71b7053SJung-uk Kimdown each and finally closes both down.
189e71b7053SJung-uk Kim
190e71b7053SJung-uk Kim BIO *abio, *cbio, *cbio2;
191e71b7053SJung-uk Kim
192*b077aed3SPierre Pronchery /* First call to BIO_do_accept() sets up accept BIO */
193e71b7053SJung-uk Kim abio = BIO_new_accept("4444");
194e71b7053SJung-uk Kim if (BIO_do_accept(abio) <= 0) {
195e71b7053SJung-uk Kim     fprintf(stderr, "Error setting up accept\n");
196e71b7053SJung-uk Kim     ERR_print_errors_fp(stderr);
197e71b7053SJung-uk Kim     exit(1);
198e71b7053SJung-uk Kim }
199e71b7053SJung-uk Kim
200e71b7053SJung-uk Kim /* Wait for incoming connection */
201e71b7053SJung-uk Kim if (BIO_do_accept(abio) <= 0) {
202e71b7053SJung-uk Kim     fprintf(stderr, "Error accepting connection\n");
203e71b7053SJung-uk Kim     ERR_print_errors_fp(stderr);
204e71b7053SJung-uk Kim     exit(1);
205e71b7053SJung-uk Kim }
206e71b7053SJung-uk Kim fprintf(stderr, "Connection 1 established\n");
207e71b7053SJung-uk Kim
208e71b7053SJung-uk Kim /* Retrieve BIO for connection */
209e71b7053SJung-uk Kim cbio = BIO_pop(abio);
210e71b7053SJung-uk Kim BIO_puts(cbio, "Connection 1: Sending out Data on initial connection\n");
211e71b7053SJung-uk Kim fprintf(stderr, "Sent out data on connection 1\n");
212e71b7053SJung-uk Kim
213e71b7053SJung-uk Kim /* Wait for another connection */
214e71b7053SJung-uk Kim if (BIO_do_accept(abio) <= 0) {
215e71b7053SJung-uk Kim     fprintf(stderr, "Error accepting connection\n");
216e71b7053SJung-uk Kim     ERR_print_errors_fp(stderr);
217e71b7053SJung-uk Kim     exit(1);
218e71b7053SJung-uk Kim }
219e71b7053SJung-uk Kim fprintf(stderr, "Connection 2 established\n");
220e71b7053SJung-uk Kim
221e71b7053SJung-uk Kim /* Close accept BIO to refuse further connections */
222e71b7053SJung-uk Kim cbio2 = BIO_pop(abio);
223e71b7053SJung-uk Kim BIO_free(abio);
224e71b7053SJung-uk Kim BIO_puts(cbio2, "Connection 2: Sending out Data on second\n");
225e71b7053SJung-uk Kim fprintf(stderr, "Sent out data on connection 2\n");
226e71b7053SJung-uk Kim
227e71b7053SJung-uk Kim BIO_puts(cbio, "Connection 1: Second connection established\n");
228e71b7053SJung-uk Kim
229e71b7053SJung-uk Kim /* Close the two established connections */
230e71b7053SJung-uk Kim BIO_free(cbio);
231e71b7053SJung-uk Kim BIO_free(cbio2);
232e71b7053SJung-uk Kim
233e71b7053SJung-uk Kim=head1 COPYRIGHT
234e71b7053SJung-uk Kim
235*b077aed3SPierre ProncheryCopyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
236e71b7053SJung-uk Kim
237*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
238e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
239e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
240e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
241e71b7053SJung-uk Kim
242e71b7053SJung-uk Kim=cut
243