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