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