
#use wml::openssl-macros area=docs page=BIO_s_accept

<title>Documents, BIO_s_accept(3)</title>

<h1>BIO_s_accept(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#EXAMPLE">EXAMPLE</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
BIO_s_accept, BIO_set_accept_port, BIO_get_accept_port,
BIO_set_nbio_accept, BIO_set_accept_bios, BIO_set_bind_mode,
BIO_get_bind_mode, BIO_do_accept - accept BIO

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bio.h&gt;
</PRE>
<PRE> BIO_METHOD *BIO_s_accept(void);
</PRE>
<PRE> long BIO_set_accept_port(BIO *b, char *name);
 char *BIO_get_accept_port(BIO *b);
</PRE>
<PRE> BIO *BIO_new_accept(char *host_port);
</PRE>
<PRE> long BIO_set_nbio_accept(BIO *b, int n);
 long BIO_set_accept_bios(BIO *b, char *bio);
</PRE>
<PRE> long BIO_set_bind_mode(BIO *b, long mode);
 long BIO_get_bind_mode(BIO *b, long dummy);
</PRE>
<PRE> #define BIO_BIND_NORMAL                0
 \#define BIO_BIND_REUSEADDR_IF_UNUSED   1
 \#define BIO_BIND_REUSEADDR             2
</PRE>
<PRE> int BIO_do_accept(BIO *b);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>BIO_s_accept()</CODE> returns the accept BIO method. This is a
wrapper round the platform's TCP/IP socket accept routines.

</P>
<P>
Using accept BIOs, TCP/IP connections can be accepted and data transferred
using only BIO routines. In this way any platform specific operations are
hidden by the BIO abstraction.

</P>
<P>
Read and write operations on an accept BIO will perform I/O on the
underlying connection. If no connection is established and the port (see
below) is set up properly then the BIO waits for an incoming connection.

</P>
<P>
Accept BIOs support <CODE>BIO_puts()</CODE> but not
<CODE>BIO_gets().</CODE>

</P>
<P>
If the close flag is set on an accept BIO then any active connection on
that chain is shutdown and the socket closed when the BIO is freed.

</P>
<P>
Calling <CODE>BIO_reset()</CODE> on a accept BIO will close any active
connection and reset the BIO into a state where it awaits another incoming
connection.

</P>
<P>
<CODE>BIO_get_fd()</CODE> and <CODE>BIO_set_fd()</CODE> can be called to
retrieve or set the accept socket. See <A HREF="../crypto/BIO_s_fd.html#">BIO_s_fd(3)</A>



</P>
<P>
<CODE>BIO_set_accept_port()</CODE> uses the string <STRONG>name</STRONG> to set the accept port. The port is represented as a string of the form
``host:port'', where ``host'' is the interface to use and ``port'' is the
port. Either or both values can be ``*'' which is interpreted as meaning
any interface or port respectively. ``port'' has the same syntax as the
port specified in <CODE>BIO_set_conn_port()</CODE> for connect BIOs, that
is it can be a numerical port string or a string to lookup using
<CODE>getservbyname()</CODE> and a string table.

</P>
<P>
<CODE>BIO_new_accept()</CODE> combines <CODE>BIO_new()</CODE> and
<CODE>BIO_set_accept_port()</CODE> into a single call: that is it creates a
new accept BIO with port
<STRONG>host_port</STRONG>.

</P>
<P>
<CODE>BIO_set_nbio_accept()</CODE> sets the accept socket to blocking mode
(the default) if <STRONG>n</STRONG> is 0 or non blocking mode if <STRONG>n</STRONG> is 1.

</P>
<P>
<CODE>BIO_set_accept_bios()</CODE> can be used to set a chain of BIOs which
will be duplicated and prepended to the chain when an incoming connection
is received. This is useful if, for example, a buffering or SSL BIO is
required for each connection. The chain of BIOs must not be freed after
this call, they will be automatically freed when the accept BIO is freed.

</P>
<P>
<CODE>BIO_set_bind_mode()</CODE> and <CODE>BIO_get_bind_mode()</CODE> set
and retrieve the current bind mode. If BIO_BIND_NORMAL (the default) is set
then another socket cannot be bound to the same port. If BIO_BIND_REUSEADDR
is set then other sockets can bind to the same port. If
BIO_BIND_REUSEADDR_IF_UNUSED is set then and attempt is first made to use
BIO_BIN_NORMAL, if this fails and the port is not in use then a second
attempt is made using BIO_BIND_REUSEADDR.

</P>
<P>
<CODE>BIO_do_accept()</CODE> serves two functions. When it is first called,
after the accept BIO has been setup, it will attempt to create the accept
socket and bind an address to it. Second and subsequent calls to
<CODE>BIO_do_accept()</CODE> will await an incoming connection, or request
a retry in non blocking mode.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
When an accept BIO is at the end of a chain it will await an incoming
connection before processing I/O calls. When an accept BIO is not at then
end of a chain it passes I/O calls to the next BIO in the chain.

</P>
<P>
When a connection is established a new socket BIO is created for the
connection and appended to the chain. That is the chain is now
accept-&gt;socket. This effectively means that attempting I/O on an initial
accept socket will await an incoming connection then perform I/O on it.

</P>
<P>
If any additional BIOs have been set using
<CODE>BIO_set_accept_bios()</CODE> then they are placed between the socket
and the accept BIO, that is the chain will be
accept-&gt;otherbios-&gt;socket.

</P>
<P>
If a server wishes to process multiple connections (as is normally the
case) then the accept BIO must be made available for further incoming
connections. This can be done by waiting for a connection and then calling:

</P>
<PRE> connection = BIO_pop(accept);
</PRE>
<P>
After this call <STRONG>connection</STRONG> will contain a BIO for the recently established connection and <STRONG>accept</STRONG> will now be a single BIO again which can be used to await further incoming
connections. If no further connections will be accepted the <STRONG>accept</STRONG> can be freed using <CODE>BIO_free().</CODE>

</P>
<P>
If only a single connection will be processed it is possible to perform I/O
using the accept BIO itself. This is often undesirable however because the
accept BIO will still accept additional incoming connections. This can be
resolved by using <CODE>BIO_pop()</CODE> (see above) and freeing up the
accept BIO after the initial connection.

</P>
<P>
If the underlying accept socket is non-blocking and
<CODE>BIO_do_accept()</CODE> is called to await an incoming connection it
is possible for <CODE>BIO_should_io_special()</CODE> with the reason
BIO_RR_ACCEPT. If this happens then it is an indication that an accept
attempt would block: the application should take appropriate action to wait
until the underlying socket has accepted a connection and retry the call.

</P>
<P>
<CODE>BIO_set_accept_port(),</CODE> <CODE>BIO_get_accept_port(),</CODE>
<CODE>BIO_set_nbio_accept(),</CODE> <CODE>BIO_set_accept_bios(),</CODE>
<CODE>BIO_set_bind_mode(),</CODE> <CODE>BIO_get_bind_mode()</CODE> and
<CODE>BIO_do_accept()</CODE> are macros.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
TBA

</P>
<P>
<HR>
<H1><A NAME="EXAMPLE">EXAMPLE</A></H1>
<P>
This example accepts two connections on port 4444, sends messages down each
and finally closes both down.

</P>
<PRE> BIO *abio, *cbio, *cbio2;
 ERR_load_crypto_strings();
 abio = BIO_new_accept(&quot;4444&quot;);
</PRE>
<PRE> /* First call to BIO_accept() sets up accept BIO */
 if(BIO_do_accept(abio) &lt;= 0) {
        fprintf(stderr, &quot;Error setting up accept\n&quot;);
        ERR_print_errors_fp(stderr);
        exit(0);                
 }
</PRE>
<PRE> /* Wait for incoming connection */
 if(BIO_do_accept(abio) &lt;= 0) {
        fprintf(stderr, &quot;Error accepting connection\n&quot;);
        ERR_print_errors_fp(stderr);
        exit(0);                
 }
 fprintf(stderr, &quot;Connection 1 established\n&quot;);
 /* Retrieve BIO for connection */
 cbio = BIO_pop(abio);
 BIO_puts(cbio, &quot;Connection 1: Sending out Data on initial connection\n&quot;);
 fprintf(stderr, &quot;Sent out data on connection 1\n&quot;);
 /* Wait for another connection */
 if(BIO_do_accept(abio) &lt;= 0) {
        fprintf(stderr, &quot;Error accepting connection\n&quot;);
        ERR_print_errors_fp(stderr);
        exit(0);                
 }
 fprintf(stderr, &quot;Connection 2 established\n&quot;);
 /* Close accept BIO to refuse further connections */
 cbio2 = BIO_pop(abio);
 BIO_free(abio);
 BIO_puts(cbio2, &quot;Connection 2: Sending out Data on second\n&quot;);
 fprintf(stderr, &quot;Sent out data on connection 2\n&quot;);
</PRE>
<PRE> BIO_puts(cbio, &quot;Connection 1: Second connection established\n&quot;);
 /* Close the two established connections */
 BIO_free(cbio);
 BIO_free(cbio2);
</PRE>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
TBA
</P>
:}


