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