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