New macro BIO_set_shutdown_wr().
[openssl.git] / doc / crypto / BIO_s_accept.pod
1 =pod
2
3 =head1 NAME
4
5 BIO_s_accept, BIO_set_nbio, BIO_set_accept_port, BIO_get_accept_port,
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  #define BIO_set_accept_port(b,name) BIO_ctrl(b,BIO_C_SET_ACCEPT,0,(char *)name)
16  #define BIO_get_accept_port(b) BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,0)
17
18  BIO *BIO_new_accept(char *host_port);
19
20  #define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?"a":NULL)
21  #define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio)
22
23  #define BIO_set_bind_mode(b,mode) BIO_ctrl(b,BIO_C_SET_BIND_MODE,mode,NULL)
24  #define BIO_get_bind_mode(b,mode) BIO_ctrl(b,BIO_C_GET_BIND_MODE,0,NULL)
25
26  #define BIO_BIND_NORMAL                0
27  #define BIO_BIND_REUSEADDR_IF_UNUSED   1
28  #define BIO_BIND_REUSEADDR             2
29
30  #define BIO_do_accept(b)       BIO_do_handshake(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)|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 Either or both values can be "*" which is interpreted as meaning
63 any interface or port respectively. "port" has the same syntax
64 as the port specified in BIO_set_conn_port() for connect BIOs,
65 that is it can be a numerical port string or a string to lookup
66 using getservbyname() and a string table.
67
68 BIO_new_accept() combines BIO_new() and BIO_set_accept_port() into
69 a single call: that is it creates a new accept BIO with port
70 B<host_port>.
71
72 BIO_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
75 BIO_set_accept_bios() can be used to set a chain of BIOs which
76 will be duplicated and prepended to the chain when an incoming
77 connection is received. This is useful if, for example, a 
78 buffering or SSL BIO is required for each connection. The
79 chain of BIOs must not be freed after this call, they will
80 be automatically freed when the accept BIO is freed.
81
82 BIO_set_bind_mode() and BIO_get_bind_mode() set and retrieve
83 the current bind mode. If BIO_BIND_NORMAL (the default) is set
84 then another socket cannot be bound to the same port. If
85 BIO_BIND_REUSEADDR is set then other sockets can bind to the
86 same port. If BIO_BIND_REUSEADDR_IF_UNUSED is set then and
87 attempt is first made to use BIO_BIN_NORMAL, if this fails
88 and the port is not in use then a second attempt is made
89 using BIO_BIND_REUSEADDR.
90
91 BIO_do_accept() serves two functions. When it is first
92 called, after the accept BIO has been setup, it will attempt
93 to create the accept socket and bind an address to it. Second
94 and subsequent calls to BIO_do_accept() will await an incoming
95 connection.
96
97 =head1 NOTES
98
99 When an accept BIO is at the end of a chain it will await an
100 incoming connection before processing I/O calls. When an accept
101 BIO is not at then end of a chain it passes I/O calls to the next
102 BIO in the chain.
103
104 When a connection is established a new socket BIO is created for
105 the connection and appended to the chain. That is the chain is now
106 accept->socket. This effectively means that attempting I/O on
107 an initial accept socket will await an incoming connection then
108 perform I/O on it.
109
110 If any additional BIOs have been set using BIO_set_accept_bios()
111 then they are placed between the socket and the accept BIO,
112 that is the chain will be accept->otherbios->socket.
113
114 If a server wishes to process multiple connections (as is normally
115 the case) then the accept BIO must be made available for further
116 incoming connections. This can be done by waiting for a connection and
117 then calling:
118
119  connection = BIO_pop(accept);
120
121 After this call B<connection> will contain a BIO for the recently
122 established connection and B<accept> will now be a single BIO
123 again which can be used to await further incoming connections.
124 If no further connections will be accepted the B<accept> can
125 be freed using BIO_free().
126
127 If only a single connection will be processed it is possible to
128 perform I/O using the accept BIO itself. This is often undesirable
129 however because the accept BIO will still accept additional incoming
130 connections. This can be resolved by using BIO_pop() (see above)
131 and freeing up the accept BIO after the initial connection.
132
133 =head1 RETURN VALUES
134
135 TBA
136
137 =head1 EXAMPLE
138
139 This example accepts two connections on port 4444, sends messages
140 down each and finally closes both down.
141
142  BIO *abio, *cbio, *cbio2;
143  ERR_load_crypto_strings();
144  abio = BIO_new_accept("4444");
145
146  /* First call to BIO_accept() sets up accept BIO */
147  if(BIO_do_accept(abio) <= 0) {
148         fprintf(stderr, "Error setting up accept\n");
149         ERR_print_errors_fp(stderr);
150         exit(0);                
151  }
152
153  /* Wait for incoming connection */
154  if(BIO_do_accept(abio) <= 0) {
155         fprintf(stderr, "Error accepting connection\n");
156         ERR_print_errors_fp(stderr);
157         exit(0);                
158  }
159  fprintf(stderr, "Connection 1 established\n");
160  /* Retrieve BIO for connection */
161  cbio = BIO_pop(abio);
162  BIO_puts(cbio, "Connection 1: Sending out Data on initial connection\n");
163  fprintf(stderr, "Sent out data on connection 1\n");
164  /* Wait for another 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 2 established\n");
171  /* Close accept BIO to refuse further connections */
172  cbio2 = BIO_pop(abio);
173  BIO_free(abio);
174  BIO_puts(cbio2, "Connection 2: Sending out Data on second\n");
175  fprintf(stderr, "Sent out data on connection 2\n");
176
177  BIO_puts(cbio, "Connection 1: Second connection established\n");
178  /* Close the two established connections */
179  BIO_free(cbio);
180  BIO_free(cbio2);
181
182 =head1 SEE ALSO
183
184 TBA