xref: /onnv-gate/usr/src/common/openssl/doc/crypto/BIO_s_accept.pod (revision 2175:b0b2f052a486)
1*2175Sjp161948=pod
2*2175Sjp161948
3*2175Sjp161948=head1 NAME
4*2175Sjp161948
5*2175Sjp161948BIO_s_accept, BIO_set_accept_port, BIO_get_accept_port,
6*2175Sjp161948BIO_set_nbio_accept, BIO_set_accept_bios, BIO_set_bind_mode,
7*2175Sjp161948BIO_get_bind_mode, BIO_do_accept - accept BIO
8*2175Sjp161948
9*2175Sjp161948=head1 SYNOPSIS
10*2175Sjp161948
11*2175Sjp161948 #include <openssl/bio.h>
12*2175Sjp161948
13*2175Sjp161948 BIO_METHOD *BIO_s_accept(void);
14*2175Sjp161948
15*2175Sjp161948 long BIO_set_accept_port(BIO *b, char *name);
16*2175Sjp161948 char *BIO_get_accept_port(BIO *b);
17*2175Sjp161948
18*2175Sjp161948 BIO *BIO_new_accept(char *host_port);
19*2175Sjp161948
20*2175Sjp161948 long BIO_set_nbio_accept(BIO *b, int n);
21*2175Sjp161948 long BIO_set_accept_bios(BIO *b, char *bio);
22*2175Sjp161948
23*2175Sjp161948 long BIO_set_bind_mode(BIO *b, long mode);
24*2175Sjp161948 long BIO_get_bind_mode(BIO *b, long dummy);
25*2175Sjp161948
26*2175Sjp161948 #define BIO_BIND_NORMAL		0
27*2175Sjp161948 #define BIO_BIND_REUSEADDR_IF_UNUSED	1
28*2175Sjp161948 #define BIO_BIND_REUSEADDR		2
29*2175Sjp161948
30*2175Sjp161948 int BIO_do_accept(BIO *b);
31*2175Sjp161948
32*2175Sjp161948=head1 DESCRIPTION
33*2175Sjp161948
34*2175Sjp161948BIO_s_accept() returns the accept BIO method. This is a wrapper
35*2175Sjp161948round the platform's TCP/IP socket accept routines.
36*2175Sjp161948
37*2175Sjp161948Using accept BIOs, TCP/IP connections can be accepted and data
38*2175Sjp161948transferred using only BIO routines. In this way any platform
39*2175Sjp161948specific operations are hidden by the BIO abstraction.
40*2175Sjp161948
41*2175Sjp161948Read and write operations on an accept BIO will perform I/O
42*2175Sjp161948on the underlying connection. If no connection is established
43*2175Sjp161948and the port (see below) is set up properly then the BIO
44*2175Sjp161948waits for an incoming connection.
45*2175Sjp161948
46*2175Sjp161948Accept BIOs support BIO_puts() but not BIO_gets().
47*2175Sjp161948
48*2175Sjp161948If the close flag is set on an accept BIO then any active
49*2175Sjp161948connection on that chain is shutdown and the socket closed when
50*2175Sjp161948the BIO is freed.
51*2175Sjp161948
52*2175Sjp161948Calling BIO_reset() on a accept BIO will close any active
53*2175Sjp161948connection and reset the BIO into a state where it awaits another
54*2175Sjp161948incoming connection.
55*2175Sjp161948
56*2175Sjp161948BIO_get_fd() and BIO_set_fd() can be called to retrieve or set
57*2175Sjp161948the accept socket. See L<BIO_s_fd(3)|BIO_s_fd(3)>
58*2175Sjp161948
59*2175Sjp161948BIO_set_accept_port() uses the string B<name> to set the accept
60*2175Sjp161948port. The port is represented as a string of the form "host:port",
61*2175Sjp161948where "host" is the interface to use and "port" is the port.
62*2175Sjp161948Either or both values can be "*" which is interpreted as meaning
63*2175Sjp161948any interface or port respectively. "port" has the same syntax
64*2175Sjp161948as the port specified in BIO_set_conn_port() for connect BIOs,
65*2175Sjp161948that is it can be a numerical port string or a string to lookup
66*2175Sjp161948using getservbyname() and a string table.
67*2175Sjp161948
68*2175Sjp161948BIO_new_accept() combines BIO_new() and BIO_set_accept_port() into
69*2175Sjp161948a single call: that is it creates a new accept BIO with port
70*2175Sjp161948B<host_port>.
71*2175Sjp161948
72*2175Sjp161948BIO_set_nbio_accept() sets the accept socket to blocking mode
73*2175Sjp161948(the default) if B<n> is 0 or non blocking mode if B<n> is 1.
74*2175Sjp161948
75*2175Sjp161948BIO_set_accept_bios() can be used to set a chain of BIOs which
76*2175Sjp161948will be duplicated and prepended to the chain when an incoming
77*2175Sjp161948connection is received. This is useful if, for example, a
78*2175Sjp161948buffering or SSL BIO is required for each connection. The
79*2175Sjp161948chain of BIOs must not be freed after this call, they will
80*2175Sjp161948be automatically freed when the accept BIO is freed.
81*2175Sjp161948
82*2175Sjp161948BIO_set_bind_mode() and BIO_get_bind_mode() set and retrieve
83*2175Sjp161948the current bind mode. If BIO_BIND_NORMAL (the default) is set
84*2175Sjp161948then another socket cannot be bound to the same port. If
85*2175Sjp161948BIO_BIND_REUSEADDR is set then other sockets can bind to the
86*2175Sjp161948same port. If BIO_BIND_REUSEADDR_IF_UNUSED is set then and
87*2175Sjp161948attempt is first made to use BIO_BIN_NORMAL, if this fails
88*2175Sjp161948and the port is not in use then a second attempt is made
89*2175Sjp161948using BIO_BIND_REUSEADDR.
90*2175Sjp161948
91*2175Sjp161948BIO_do_accept() serves two functions. When it is first
92*2175Sjp161948called, after the accept BIO has been setup, it will attempt
93*2175Sjp161948to create the accept socket and bind an address to it. Second
94*2175Sjp161948and subsequent calls to BIO_do_accept() will await an incoming
95*2175Sjp161948connection, or request a retry in non blocking mode.
96*2175Sjp161948
97*2175Sjp161948=head1 NOTES
98*2175Sjp161948
99*2175Sjp161948When an accept BIO is at the end of a chain it will await an
100*2175Sjp161948incoming connection before processing I/O calls. When an accept
101*2175Sjp161948BIO is not at then end of a chain it passes I/O calls to the next
102*2175Sjp161948BIO in the chain.
103*2175Sjp161948
104*2175Sjp161948When a connection is established a new socket BIO is created for
105*2175Sjp161948the connection and appended to the chain. That is the chain is now
106*2175Sjp161948accept->socket. This effectively means that attempting I/O on
107*2175Sjp161948an initial accept socket will await an incoming connection then
108*2175Sjp161948perform I/O on it.
109*2175Sjp161948
110*2175Sjp161948If any additional BIOs have been set using BIO_set_accept_bios()
111*2175Sjp161948then they are placed between the socket and the accept BIO,
112*2175Sjp161948that is the chain will be accept->otherbios->socket.
113*2175Sjp161948
114*2175Sjp161948If a server wishes to process multiple connections (as is normally
115*2175Sjp161948the case) then the accept BIO must be made available for further
116*2175Sjp161948incoming connections. This can be done by waiting for a connection and
117*2175Sjp161948then calling:
118*2175Sjp161948
119*2175Sjp161948 connection = BIO_pop(accept);
120*2175Sjp161948
121*2175Sjp161948After this call B<connection> will contain a BIO for the recently
122*2175Sjp161948established connection and B<accept> will now be a single BIO
123*2175Sjp161948again which can be used to await further incoming connections.
124*2175Sjp161948If no further connections will be accepted the B<accept> can
125*2175Sjp161948be freed using BIO_free().
126*2175Sjp161948
127*2175Sjp161948If only a single connection will be processed it is possible to
128*2175Sjp161948perform I/O using the accept BIO itself. This is often undesirable
129*2175Sjp161948however because the accept BIO will still accept additional incoming
130*2175Sjp161948connections. This can be resolved by using BIO_pop() (see above)
131*2175Sjp161948and freeing up the accept BIO after the initial connection.
132*2175Sjp161948
133*2175Sjp161948If the underlying accept socket is non-blocking and BIO_do_accept() is
134*2175Sjp161948called to await an incoming connection it is possible for
135*2175Sjp161948BIO_should_io_special() with the reason BIO_RR_ACCEPT. If this happens
136*2175Sjp161948then it is an indication that an accept attempt would block: the application
137*2175Sjp161948should take appropriate action to wait until the underlying socket has
138*2175Sjp161948accepted a connection and retry the call.
139*2175Sjp161948
140*2175Sjp161948BIO_set_accept_port(), BIO_get_accept_port(), BIO_set_nbio_accept(),
141*2175Sjp161948BIO_set_accept_bios(), BIO_set_bind_mode(), BIO_get_bind_mode() and
142*2175Sjp161948BIO_do_accept() are macros.
143*2175Sjp161948
144*2175Sjp161948=head1 RETURN VALUES
145*2175Sjp161948
146*2175Sjp161948TBA
147*2175Sjp161948
148*2175Sjp161948=head1 EXAMPLE
149*2175Sjp161948
150*2175Sjp161948This example accepts two connections on port 4444, sends messages
151*2175Sjp161948down each and finally closes both down.
152*2175Sjp161948
153*2175Sjp161948 BIO *abio, *cbio, *cbio2;
154*2175Sjp161948 ERR_load_crypto_strings();
155*2175Sjp161948 abio = BIO_new_accept("4444");
156*2175Sjp161948
157*2175Sjp161948 /* First call to BIO_accept() sets up accept BIO */
158*2175Sjp161948 if(BIO_do_accept(abio) <= 0) {
159*2175Sjp161948	fprintf(stderr, "Error setting up accept\n");
160*2175Sjp161948	ERR_print_errors_fp(stderr);
161*2175Sjp161948	exit(0);
162*2175Sjp161948 }
163*2175Sjp161948
164*2175Sjp161948 /* Wait for incoming connection */
165*2175Sjp161948 if(BIO_do_accept(abio) <= 0) {
166*2175Sjp161948	fprintf(stderr, "Error accepting connection\n");
167*2175Sjp161948	ERR_print_errors_fp(stderr);
168*2175Sjp161948	exit(0);
169*2175Sjp161948 }
170*2175Sjp161948 fprintf(stderr, "Connection 1 established\n");
171*2175Sjp161948 /* Retrieve BIO for connection */
172*2175Sjp161948 cbio = BIO_pop(abio);
173*2175Sjp161948 BIO_puts(cbio, "Connection 1: Sending out Data on initial connection\n");
174*2175Sjp161948 fprintf(stderr, "Sent out data on connection 1\n");
175*2175Sjp161948 /* Wait for another connection */
176*2175Sjp161948 if(BIO_do_accept(abio) <= 0) {
177*2175Sjp161948	fprintf(stderr, "Error accepting connection\n");
178*2175Sjp161948	ERR_print_errors_fp(stderr);
179*2175Sjp161948	exit(0);
180*2175Sjp161948 }
181*2175Sjp161948 fprintf(stderr, "Connection 2 established\n");
182*2175Sjp161948 /* Close accept BIO to refuse further connections */
183*2175Sjp161948 cbio2 = BIO_pop(abio);
184*2175Sjp161948 BIO_free(abio);
185*2175Sjp161948 BIO_puts(cbio2, "Connection 2: Sending out Data on second\n");
186*2175Sjp161948 fprintf(stderr, "Sent out data on connection 2\n");
187*2175Sjp161948
188*2175Sjp161948 BIO_puts(cbio, "Connection 1: Second connection established\n");
189*2175Sjp161948 /* Close the two established connections */
190*2175Sjp161948 BIO_free(cbio);
191*2175Sjp161948 BIO_free(cbio2);
192*2175Sjp161948
193*2175Sjp161948=head1 SEE ALSO
194*2175Sjp161948
195*2175Sjp161948TBA
196