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

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

<h1>BIO_s_connect(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_connect, BIO_set_conn_hostname, BIO_set_conn_port, BIO_set_conn_ip,
BIO_set_conn_int_port, BIO_get_conn_hostname, BIO_get_conn_port,
BIO_get_conn_ip, BIO_get_conn_int_port, BIO_set_nbio, BIO_do_connect -
connect BIO

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bio.h&gt;
</PRE>
<PRE> BIO_METHOD * BIO_s_connect(void);
</PRE>
<PRE> BIO *BIO_new_connect(char *name);
</PRE>
<PRE> long BIO_set_conn_hostname(BIO *b, char *name);
 long BIO_set_conn_port(BIO *b, char *port);
 long BIO_set_conn_ip(BIO *b, char *ip);
 long BIO_set_conn_int_port(BIO *b, char *port);
 char *BIO_get_conn_hostname(BIO *b);
 char *BIO_get_conn_port(BIO *b);
 char *BIO_get_conn_ip(BIO *b, dummy);
 long BIO_get_conn_int_port(BIO *b, int port);
</PRE>
<PRE> long BIO_set_nbio(BIO *b, long n);
</PRE>
<PRE> int BIO_do_connect(BIO *b);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>BIO_s_connect()</CODE> returns the connect BIO method. This is a
wrapper round the platform's TCP/IP socket connection routines.

</P>
<P>
Using connect BIOs, TCP/IP connections can be made 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 a connect BIO will perform I/O on the
underlying connection. If no connection is established and the port and
hostname (see below) is set up properly then a connection is established
first.

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

</P>
<P>
If the close flag is set on a connect BIO then any active connection is
shutdown and the socket closed when the BIO is freed.

</P>
<P>
Calling <CODE>BIO_reset()</CODE> on a connect BIO will close any active
connection and reset the BIO into a state where it can connect to the same
host again.

</P>
<P>
<CODE>BIO_get_fd()</CODE> places the underlying socket in <STRONG>c</STRONG> if it is not NULL, it also returns the socket . If <STRONG>c</STRONG> is not NULL it should be of type (int *).

</P>
<P>
<CODE>BIO_set_conn_hostname()</CODE> uses the string <STRONG>name</STRONG> to set the hostname. The hostname can be an IP address. The hostname can
also include the port in the form hostname:port . It is also acceptable to
use the form ``hostname/any/other/path'' or
``hostname:port/any/other/path''.

</P>
<P>
<CODE>BIO_set_conn_port()</CODE> sets the port to <STRONG>port</STRONG>. <STRONG>port</STRONG> can be the numerical form or a string such as ``http''. A string will be
looked up first using <CODE>getservbyname()</CODE> on the host platform but
if that fails a standard table of port names will be used. Currently the
list is http, telnet, socks, https, ssl, ftp, gopher and wais.

</P>
<P>
<CODE>BIO_set_conn_ip()</CODE> sets the IP address to <STRONG>ip</STRONG> using binary form, that is four bytes specifying the IP address in
big-endian form.

</P>
<P>
<CODE>BIO_set_conn_int_port()</CODE> sets the port using <STRONG>port</STRONG>. <STRONG>port</STRONG> should be of type (int *).

</P>
<P>
<CODE>BIO_get_conn_hostname()</CODE> returns the hostname of the connect
BIO or NULL if the BIO is initialized but no hostname is set. This return
value is an internal pointer which should not be modified.

</P>
<P>
<CODE>BIO_get_conn_port()</CODE> returns the port as a string.

</P>
<P>
<CODE>BIO_get_conn_ip()</CODE> returns the IP address in binary form.

</P>
<P>
<CODE>BIO_get_conn_int_port()</CODE> returns the port as an int.

</P>
<P>
<CODE>BIO_set_nbio()</CODE> sets the non blocking I/O flag to <STRONG>n</STRONG>. If <STRONG>n</STRONG> is zero then blocking I/O is set. If <STRONG>n</STRONG> is 1 then non blocking I/O is set. Blocking I/O is the default. The call to
<CODE>BIO_set_nbio()</CODE> should be made before the connection is
established because non blocking I/O is set during the connect process.

</P>
<P>
<CODE>BIO_new_connect()</CODE> combines <CODE>BIO_new()</CODE> and
<CODE>BIO_set_conn_hostname()</CODE> into a single call: that is it creates
a new connect BIO with <STRONG>name</STRONG>.

</P>
<P>
<CODE>BIO_do_connect()</CODE> attempts to connect the supplied BIO. It
returns 1 if the connection was established successfully. A zero or
negative value is returned if the connection could not be established, the
call <CODE>BIO_should_retry()</CODE> should be used for non blocking
connect BIOs to determine if the call should be retried.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
If blocking I/O is set then a non positive return value from any I/O call
is caused by an error condition, although a zero return will normally mean
that the connection was closed.

</P>
<P>
If the port name is supplied as part of the host name then this will
override any value set with <CODE>BIO_set_conn_port().</CODE> This may be
undesirable if the application does not wish to allow connection to
arbitrary ports. This can be avoided by checking for the presence of the
':' character in the passed hostname and either indicating an error or
truncating the string at that point.

</P>
<P>
The values returned by <CODE>BIO_get_conn_hostname(),</CODE>
<CODE>BIO_get_conn_port(),</CODE> <CODE>BIO_get_conn_ip()</CODE> and
<CODE>BIO_get_conn_int_port()</CODE> are updated when a connection attempt
is made. Before any connection attempt the values returned are those set by
the application itself.

</P>
<P>
Applications do not have to call <CODE>BIO_do_connect()</CODE> but may wish
to do so to separate the connection process from other I/O processing.

</P>
<P>
If non blocking I/O is set then retries will be requested as appropriate.

</P>
<P>
It addition to <CODE>BIO_should_read()</CODE> and
<CODE>BIO_should_write()</CODE> it is also possible for
<CODE>BIO_should_io_special()</CODE> to be true during the initial
connection process with the reason BIO_RR_CONNECT. If this is returned then
this is an indication that a connection attempt would block, the
application should then take appropriate action to wait until the
underlying socket has connected and retry the call.

</P>
<P>
<CODE>BIO_set_conn_hostname(),</CODE> <CODE>BIO_set_conn_port(),</CODE>
<CODE>BIO_set_conn_ip(),</CODE> <CODE>BIO_set_conn_int_port(),</CODE>
<CODE>BIO_get_conn_hostname(),</CODE> <CODE>BIO_get_conn_port(),</CODE>
<CODE>BIO_get_conn_ip(),</CODE> <CODE>BIO_get_conn_int_port(),</CODE>
<CODE>BIO_set_nbio()</CODE> and <CODE>BIO_do_connect()</CODE> are macros.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>BIO_s_connect()</CODE> returns the connect BIO method.

</P>
<P>
<CODE>BIO_get_fd()</CODE> returns the socket or -1 if the BIO has not been
initialized.

</P>
<P>
<CODE>BIO_set_conn_hostname(),</CODE> <CODE>BIO_set_conn_port(),</CODE>
<CODE>BIO_set_conn_ip()</CODE> and <CODE>BIO_set_conn_int_port()</CODE>
always return 1.

</P>
<P>
<CODE>BIO_get_conn_hostname()</CODE> returns the connected hostname or NULL
is none was set.

</P>
<P>
<CODE>BIO_get_conn_port()</CODE> returns a string representing the
connected port or NULL if not set.

</P>
<P>
<CODE>BIO_get_conn_ip()</CODE> returns a pointer to the connected IP
address in binary form or all zeros if not set.

</P>
<P>
<CODE>BIO_get_conn_int_port()</CODE> returns the connected port or 0 if
none was set.

</P>
<P>
<CODE>BIO_set_nbio()</CODE> always returns 1.

</P>
<P>
<CODE>BIO_do_connect()</CODE> returns 1 if the connection was successfully
established and 0 or -1 if the connection failed.

</P>
<P>
<HR>
<H1><A NAME="EXAMPLE">EXAMPLE</A></H1>
<P>
This is example connects to a webserver on the local host and attempts to
retrieve a page and copy the result to standard output.

</P>
<PRE> BIO *cbio, *out;
 int len;
 char tmpbuf[1024];
 ERR_load_crypto_strings();
 cbio = BIO_new_connect(&quot;localhost:http&quot;);
 out = BIO_new_fp(stdout, BIO_NOCLOSE);
 if(BIO_do_connect(cbio) &lt;= 0) {
        fprintf(stderr, &quot;Error connecting to server\n&quot;);
        ERR_print_errors_fp(stderr);
        /* whatever ... */
        }
 BIO_puts(cbio, &quot;GET / HTTP/1.0\n\n&quot;);
 for(;;) {      
        len = BIO_read(cbio, tmpbuf, 1024);
        if(len &lt;= 0) break;
        BIO_write(out, tmpbuf, len);
 }
 BIO_free(cbio);
 BIO_free(out);
</PRE>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
TBA
</P>
:}

