]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/BIO_s_accept.pod
Add d2i_KeyParams/i2d_KeyParams API's.
[thirdparty/openssl.git] / doc / man3 / BIO_s_accept.pod
CommitLineData
68a1c6b0
DSH
1=pod
2
3=head1 NAME
4
9d8c2dfe 5BIO_s_accept, BIO_set_accept_name, BIO_set_accept_port, BIO_get_accept_name,
b4c1d72e 6BIO_get_accept_port, BIO_new_accept, BIO_set_nbio_accept, BIO_set_accept_bios,
88b8d83e
VC
7BIO_get_peer_name, BIO_get_peer_port,
8BIO_get_accept_ip_family, BIO_set_accept_ip_family,
b4c1d72e 9BIO_set_bind_mode, BIO_get_bind_mode, BIO_do_accept - accept BIO
68a1c6b0
DSH
10
11=head1 SYNOPSIS
12
13 #include <openssl/bio.h>
14
04f6b0fd 15 const BIO_METHOD *BIO_s_accept(void);
68a1c6b0 16
b4c1d72e 17 long BIO_set_accept_name(BIO *b, char *name);
18 char *BIO_get_accept_name(BIO *b);
19
3b7a5758 20 long BIO_set_accept_port(BIO *b, char *port);
bbdc9c98 21 char *BIO_get_accept_port(BIO *b);
68a1c6b0
DSH
22
23 BIO *BIO_new_accept(char *host_port);
24
bbdc9c98
UM
25 long BIO_set_nbio_accept(BIO *b, int n);
26 long BIO_set_accept_bios(BIO *b, char *bio);
68a1c6b0 27
88b8d83e
VC
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
bbdc9c98 33 long BIO_set_bind_mode(BIO *b, long mode);
8976eb24 34 long BIO_get_bind_mode(BIO *b);
68a1c6b0 35
bbdc9c98 36 int BIO_do_accept(BIO *b);
68a1c6b0
DSH
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
bbdc9c98 43Using accept BIOs, TCP/IP connections can be accepted and data
68a1c6b0
DSH
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
b9b6a7e5 58Calling BIO_reset() on an accept BIO will close any active
68a1c6b0
DSH
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
9b86974e 63the accept socket. See L<BIO_s_fd(3)>
68a1c6b0 64
b4c1d72e 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",
68a1c6b0 67where "host" is the interface to use and "port" is the port.
417be660 68The host can be "*" or empty which is interpreted as meaning
b9b6a7e5 69any interface. If the host is an IPv6 address, it has to be
417be660 70enclosed in brackets, for example "[::1]:https". "port" has the
3b7a5758 71same syntax as the port specified in BIO_set_conn_port() for
417be660
RL
72connect BIOs, that is it can be a numerical port string or a
73string to lookup using getservbyname() and a string table.
68a1c6b0 74
3b7a5758
J
75BIO_set_accept_port() uses the string B<port> to set the accept
76port. "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.
80
b4c1d72e 81BIO_new_accept() combines BIO_new() and BIO_set_accept_name() into
68a1c6b0
DSH
82a single call: that is it creates a new accept BIO with port
83B<host_port>.
84
85BIO_set_nbio_accept() sets the accept socket to blocking mode
86(the default) if B<n> is 0 or non blocking mode if B<n> is 1.
87
88BIO_set_accept_bios() can be used to set a chain of BIOs which
89will be duplicated and prepended to the chain when an incoming
1bc74519 90connection is received. This is useful if, for example, a
2c281ebb
DSH
91buffering or SSL BIO is required for each connection. The
92chain of BIOs must not be freed after this call, they will
93be automatically freed when the accept BIO is freed.
68a1c6b0
DSH
94
95BIO_set_bind_mode() and BIO_get_bind_mode() set and retrieve
91da5e77 96the current bind mode. If B<BIO_BIND_NORMAL> (the default) is set
68a1c6b0 97then another socket cannot be bound to the same port. If
91da5e77
RS
98B<BIO_BIND_REUSEADDR> is set then other sockets can bind to the
99same port. If B<BIO_BIND_REUSEADDR_IF_UNUSED> is set then and
68a1c6b0
DSH
100attempt is first made to use BIO_BIN_NORMAL, if this fails
101and the port is not in use then a second attempt is made
91da5e77 102using B<BIO_BIND_REUSEADDR>.
68a1c6b0
DSH
103
104BIO_do_accept() serves two functions. When it is first
105called, after the accept BIO has been setup, it will attempt
106to create the accept socket and bind an address to it. Second
107and subsequent calls to BIO_do_accept() will await an incoming
924046ce 108connection, or request a retry in non blocking mode.
68a1c6b0
DSH
109
110=head1 NOTES
111
112When an accept BIO is at the end of a chain it will await an
113incoming connection before processing I/O calls. When an accept
114BIO is not at then end of a chain it passes I/O calls to the next
115BIO in the chain.
116
117When a connection is established a new socket BIO is created for
1e4e5492 118the connection and appended to the chain. That is the chain is now
68a1c6b0
DSH
119accept->socket. This effectively means that attempting I/O on
120an initial accept socket will await an incoming connection then
121perform I/O on it.
122
123If any additional BIOs have been set using BIO_set_accept_bios()
124then they are placed between the socket and the accept BIO,
125that is the chain will be accept->otherbios->socket.
126
127If a server wishes to process multiple connections (as is normally
128the case) then the accept BIO must be made available for further
129incoming connections. This can be done by waiting for a connection and
130then calling:
131
132 connection = BIO_pop(accept);
133
134After this call B<connection> will contain a BIO for the recently
135established connection and B<accept> will now be a single BIO
136again which can be used to await further incoming connections.
137If no further connections will be accepted the B<accept> can
138be freed using BIO_free().
139
140If only a single connection will be processed it is possible to
141perform I/O using the accept BIO itself. This is often undesirable
142however because the accept BIO will still accept additional incoming
143connections. This can be resolved by using BIO_pop() (see above)
144and freeing up the accept BIO after the initial connection.
145
bbdc9c98 146If the underlying accept socket is non-blocking and BIO_do_accept() is
924046ce
DSH
147called to await an incoming connection it is possible for
148BIO_should_io_special() with the reason BIO_RR_ACCEPT. If this happens
149then it is an indication that an accept attempt would block: the application
150should take appropriate action to wait until the underlying socket has
151accepted a connection and retry the call.
152
b4c1d72e 153BIO_set_accept_name(), BIO_get_accept_name(), BIO_set_accept_port(),
154BIO_get_accept_port(), BIO_set_nbio_accept(), BIO_set_accept_bios(),
88b8d83e
VC
155BIO_get_peer_name(), BIO_get_peer_port(),
156BIO_get_accept_ip_family(), BIO_set_accept_ip_family(),
b4c1d72e 157BIO_set_bind_mode(), BIO_get_bind_mode() and BIO_do_accept() are macros.
bbdc9c98 158
68a1c6b0
DSH
159=head1 RETURN VALUES
160
c03726ca 161BIO_do_accept(),
b4c1d72e 162BIO_set_accept_name(), BIO_set_accept_port(), BIO_set_nbio_accept(),
88b8d83e
VC
163BIO_set_accept_bios(), BIO_set_accept_ip_family(), and BIO_set_bind_mode()
164return 1 for success and 0 or -1 for failure.
c03726ca 165
e86e76a6 166BIO_get_accept_name() returns the accept name or NULL on error.
88b8d83e 167BIO_get_peer_name() returns the peer name or NULL on error.
3b7a5758 168
88b8d83e
VC
169BIO_get_accept_port() returns the accept port as a string or NULL on error.
170BIO_get_peer_port() returns the peer port as a string or NULL on error.
171BIO_get_accept_ip_family() returns the IP family or -1 on error.
c03726ca
RS
172
173BIO_get_bind_mode() returns the set of B<BIO_BIND> flags, or -1 on failure.
174
175BIO_new_accept() returns a BIO or NULL on error.
68a1c6b0
DSH
176
177=head1 EXAMPLE
178
179This example accepts two connections on port 4444, sends messages
180down each and finally closes both down.
181
182 BIO *abio, *cbio, *cbio2;
f672aee4 183
68a1c6b0 184 /* First call to BIO_accept() sets up accept BIO */
05ea606a
RS
185 abio = BIO_new_accept("4444");
186 if (BIO_do_accept(abio) <= 0) {
187 fprintf(stderr, "Error setting up accept\n");
188 ERR_print_errors_fp(stderr);
189 exit(1);
68a1c6b0
DSH
190 }
191
192 /* Wait for incoming connection */
05ea606a
RS
193 if (BIO_do_accept(abio) <= 0) {
194 fprintf(stderr, "Error accepting connection\n");
195 ERR_print_errors_fp(stderr);
196 exit(1);
68a1c6b0
DSH
197 }
198 fprintf(stderr, "Connection 1 established\n");
05ea606a 199
68a1c6b0
DSH
200 /* Retrieve BIO for connection */
201 cbio = BIO_pop(abio);
202 BIO_puts(cbio, "Connection 1: Sending out Data on initial connection\n");
203 fprintf(stderr, "Sent out data on connection 1\n");
05ea606a 204
68a1c6b0 205 /* Wait for another connection */
05ea606a
RS
206 if (BIO_do_accept(abio) <= 0) {
207 fprintf(stderr, "Error accepting connection\n");
208 ERR_print_errors_fp(stderr);
209 exit(1);
68a1c6b0
DSH
210 }
211 fprintf(stderr, "Connection 2 established\n");
05ea606a 212
68a1c6b0
DSH
213 /* Close accept BIO to refuse further connections */
214 cbio2 = BIO_pop(abio);
215 BIO_free(abio);
216 BIO_puts(cbio2, "Connection 2: Sending out Data on second\n");
217 fprintf(stderr, "Sent out data on connection 2\n");
218
219 BIO_puts(cbio, "Connection 1: Second connection established\n");
05ea606a 220
68a1c6b0
DSH
221 /* Close the two established connections */
222 BIO_free(cbio);
223 BIO_free(cbio2);
224
e2f92610
RS
225=head1 COPYRIGHT
226
c4d3c19b 227Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
e2f92610 228
4746f25a 229Licensed under the Apache License 2.0 (the "License"). You may not use
e2f92610
RS
230this file except in compliance with the License. You can obtain a copy
231in the file LICENSE in the source distribution or at
232L<https://www.openssl.org/source/license.html>.
233
234=cut