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