xref: /freebsd/crypto/openssl/doc/man3/BIO_s_connect.pod (revision da327cd22e88f26f6cab9d4c45805b512139aa11)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimBIO_set_conn_address, BIO_get_conn_address,
6e71b7053SJung-uk KimBIO_s_connect, BIO_new_connect, BIO_set_conn_hostname, BIO_set_conn_port,
7e71b7053SJung-uk KimBIO_set_conn_ip_family, BIO_get_conn_ip_family,
8e71b7053SJung-uk KimBIO_get_conn_hostname, BIO_get_conn_port,
9e71b7053SJung-uk KimBIO_set_nbio, BIO_do_connect - connect BIO
10e71b7053SJung-uk Kim
11e71b7053SJung-uk Kim=head1 SYNOPSIS
12e71b7053SJung-uk Kim
13e71b7053SJung-uk Kim #include <openssl/bio.h>
14e71b7053SJung-uk Kim
15e71b7053SJung-uk Kim const BIO_METHOD * BIO_s_connect(void);
16e71b7053SJung-uk Kim
17e71b7053SJung-uk Kim BIO *BIO_new_connect(char *name);
18e71b7053SJung-uk Kim
19e71b7053SJung-uk Kim long BIO_set_conn_hostname(BIO *b, char *name);
20e71b7053SJung-uk Kim long BIO_set_conn_port(BIO *b, char *port);
21e71b7053SJung-uk Kim long BIO_set_conn_address(BIO *b, BIO_ADDR *addr);
22e71b7053SJung-uk Kim long BIO_set_conn_ip_family(BIO *b, long family);
23e71b7053SJung-uk Kim const char *BIO_get_conn_hostname(BIO *b);
24e71b7053SJung-uk Kim const char *BIO_get_conn_port(BIO *b);
25e71b7053SJung-uk Kim const BIO_ADDR *BIO_get_conn_address(BIO *b);
26e71b7053SJung-uk Kim const long BIO_get_conn_ip_family(BIO *b);
27e71b7053SJung-uk Kim
28e71b7053SJung-uk Kim long BIO_set_nbio(BIO *b, long n);
29e71b7053SJung-uk Kim
30e71b7053SJung-uk Kim int BIO_do_connect(BIO *b);
31e71b7053SJung-uk Kim
32e71b7053SJung-uk Kim=head1 DESCRIPTION
33e71b7053SJung-uk Kim
34e71b7053SJung-uk KimBIO_s_connect() returns the connect BIO method. This is a wrapper
35e71b7053SJung-uk Kimround the platform's TCP/IP socket connection routines.
36e71b7053SJung-uk Kim
37e71b7053SJung-uk KimUsing connect BIOs, TCP/IP connections can be made and data
38e71b7053SJung-uk Kimtransferred using only BIO routines. In this way any platform
39e71b7053SJung-uk Kimspecific operations are hidden by the BIO abstraction.
40e71b7053SJung-uk Kim
41e71b7053SJung-uk KimRead and write operations on a connect BIO will perform I/O
42e71b7053SJung-uk Kimon the underlying connection. If no connection is established
43e71b7053SJung-uk Kimand the port and hostname (see below) is set up properly then
44e71b7053SJung-uk Kima connection is established first.
45e71b7053SJung-uk Kim
46e71b7053SJung-uk KimConnect BIOs support BIO_puts() but not BIO_gets().
47e71b7053SJung-uk Kim
48e71b7053SJung-uk KimIf the close flag is set on a connect BIO then any active
49e71b7053SJung-uk Kimconnection is shutdown and the socket closed when the BIO
50e71b7053SJung-uk Kimis freed.
51e71b7053SJung-uk Kim
52e71b7053SJung-uk KimCalling BIO_reset() on a connect BIO will close any active
53e71b7053SJung-uk Kimconnection and reset the BIO into a state where it can connect
54e71b7053SJung-uk Kimto the same host again.
55e71b7053SJung-uk Kim
56e71b7053SJung-uk KimBIO_get_fd() places the underlying socket in B<c> if it is not NULL,
57e71b7053SJung-uk Kimit also returns the socket . If B<c> is not NULL it should be of
58e71b7053SJung-uk Kimtype (int *).
59e71b7053SJung-uk Kim
60e71b7053SJung-uk KimBIO_set_conn_hostname() uses the string B<name> to set the hostname.
61e71b7053SJung-uk KimThe hostname can be an IP address; if the address is an IPv6 one, it
62e71b7053SJung-uk Kimmust be enclosed with brackets. The hostname can also include the
63e71b7053SJung-uk Kimport in the form hostname:port.
64e71b7053SJung-uk Kim
65e71b7053SJung-uk KimBIO_set_conn_port() sets the port to B<port>. B<port> can be the
66e71b7053SJung-uk Kimnumerical form or a string such as "http". A string will be looked
67e71b7053SJung-uk Kimup first using getservbyname() on the host platform but if that
68e71b7053SJung-uk Kimfails a standard table of port names will be used. This internal
69e71b7053SJung-uk Kimlist is http, telnet, socks, https, ssl, ftp, and gopher.
70e71b7053SJung-uk Kim
71e71b7053SJung-uk KimBIO_set_conn_address() sets the address and port information using
72e71b7053SJung-uk Kima BIO_ADDR(3ssl).
73e71b7053SJung-uk Kim
74e71b7053SJung-uk KimBIO_set_conn_ip_family() sets the IP family.
75e71b7053SJung-uk Kim
76e71b7053SJung-uk KimBIO_get_conn_hostname() returns the hostname of the connect BIO or
77e71b7053SJung-uk KimNULL if the BIO is initialized but no hostname is set.
78e71b7053SJung-uk KimThis return value is an internal pointer which should not be modified.
79e71b7053SJung-uk Kim
80e71b7053SJung-uk KimBIO_get_conn_port() returns the port as a string.
81e71b7053SJung-uk KimThis return value is an internal pointer which should not be modified.
82e71b7053SJung-uk Kim
83e71b7053SJung-uk KimBIO_get_conn_address() returns the address information as a BIO_ADDR.
84e71b7053SJung-uk KimThis return value is an internal pointer which should not be modified.
85e71b7053SJung-uk Kim
86e71b7053SJung-uk KimBIO_get_conn_ip_family() returns the IP family of the connect BIO.
87e71b7053SJung-uk Kim
88e71b7053SJung-uk KimBIO_set_nbio() sets the non blocking I/O flag to B<n>. If B<n> is
89e71b7053SJung-uk Kimzero then blocking I/O is set. If B<n> is 1 then non blocking I/O
90e71b7053SJung-uk Kimis set. Blocking I/O is the default. The call to BIO_set_nbio()
91e71b7053SJung-uk Kimshould be made before the connection is established because
92e71b7053SJung-uk Kimnon blocking I/O is set during the connect process.
93e71b7053SJung-uk Kim
94e71b7053SJung-uk KimBIO_new_connect() combines BIO_new() and BIO_set_conn_hostname() into
95e71b7053SJung-uk Kima single call: that is it creates a new connect BIO with B<name>.
96e71b7053SJung-uk Kim
97e71b7053SJung-uk KimBIO_do_connect() attempts to connect the supplied BIO. It returns 1
98e71b7053SJung-uk Kimif the connection was established successfully. A zero or negative
99e71b7053SJung-uk Kimvalue is returned if the connection could not be established, the
100e71b7053SJung-uk Kimcall BIO_should_retry() should be used for non blocking connect BIOs
101e71b7053SJung-uk Kimto determine if the call should be retried.
102e71b7053SJung-uk Kim
103e71b7053SJung-uk Kim=head1 NOTES
104e71b7053SJung-uk Kim
105e71b7053SJung-uk KimIf blocking I/O is set then a non positive return value from any
106e71b7053SJung-uk KimI/O call is caused by an error condition, although a zero return
107e71b7053SJung-uk Kimwill normally mean that the connection was closed.
108e71b7053SJung-uk Kim
109e71b7053SJung-uk KimIf the port name is supplied as part of the host name then this will
110e71b7053SJung-uk Kimoverride any value set with BIO_set_conn_port(). This may be undesirable
111e71b7053SJung-uk Kimif the application does not wish to allow connection to arbitrary
112e71b7053SJung-uk Kimports. This can be avoided by checking for the presence of the ':'
113e71b7053SJung-uk Kimcharacter in the passed hostname and either indicating an error or
114e71b7053SJung-uk Kimtruncating the string at that point.
115e71b7053SJung-uk Kim
116e71b7053SJung-uk KimThe values returned by BIO_get_conn_hostname(), BIO_get_conn_address(),
117e71b7053SJung-uk Kimand BIO_get_conn_port() are updated when a connection attempt is made.
118e71b7053SJung-uk KimBefore any connection attempt the values returned are those set by the
119e71b7053SJung-uk Kimapplication itself.
120e71b7053SJung-uk Kim
121e71b7053SJung-uk KimApplications do not have to call BIO_do_connect() but may wish to do
122e71b7053SJung-uk Kimso to separate the connection process from other I/O processing.
123e71b7053SJung-uk Kim
124e71b7053SJung-uk KimIf non blocking I/O is set then retries will be requested as appropriate.
125e71b7053SJung-uk Kim
126e71b7053SJung-uk KimIt addition to BIO_should_read() and BIO_should_write() it is also
127e71b7053SJung-uk Kimpossible for BIO_should_io_special() to be true during the initial
128e71b7053SJung-uk Kimconnection process with the reason BIO_RR_CONNECT. If this is returned
129e71b7053SJung-uk Kimthen this is an indication that a connection attempt would block,
130e71b7053SJung-uk Kimthe application should then take appropriate action to wait until
131e71b7053SJung-uk Kimthe underlying socket has connected and retry the call.
132e71b7053SJung-uk Kim
133e71b7053SJung-uk KimBIO_set_conn_hostname(), BIO_set_conn_port(), BIO_get_conn_hostname(),
134e71b7053SJung-uk KimBIO_set_conn_address(), BIO_get_conn_port(), BIO_get_conn_address(),
135e71b7053SJung-uk KimBIO_set_conn_ip_family(), BIO_get_conn_ip_family(),
136e71b7053SJung-uk KimBIO_set_nbio(), and BIO_do_connect() are macros.
137e71b7053SJung-uk Kim
138e71b7053SJung-uk Kim=head1 RETURN VALUES
139e71b7053SJung-uk Kim
140e71b7053SJung-uk KimBIO_s_connect() returns the connect BIO method.
141e71b7053SJung-uk Kim
142e71b7053SJung-uk KimBIO_get_fd() returns the socket or -1 if the BIO has not
143e71b7053SJung-uk Kimbeen initialized.
144e71b7053SJung-uk Kim
145e71b7053SJung-uk KimBIO_set_conn_address(), BIO_set_conn_port(), and BIO_set_conn_ip_family()
146e71b7053SJung-uk Kimalways return 1.
147e71b7053SJung-uk Kim
148e71b7053SJung-uk KimBIO_set_conn_hostname() returns 1 on success and 0 on failure.
149e71b7053SJung-uk Kim
150e71b7053SJung-uk KimBIO_get_conn_address() returns the address information or NULL if none
151e71b7053SJung-uk Kimwas set.
152e71b7053SJung-uk Kim
153e71b7053SJung-uk KimBIO_get_conn_hostname() returns the connected hostname or NULL if
154e71b7053SJung-uk Kimnone was set.
155e71b7053SJung-uk Kim
156e71b7053SJung-uk KimBIO_get_conn_ip_family() returns the address family or -1 if none was set.
157e71b7053SJung-uk Kim
158e71b7053SJung-uk KimBIO_get_conn_port() returns a string representing the connected
159e71b7053SJung-uk Kimport or NULL if not set.
160e71b7053SJung-uk Kim
161e71b7053SJung-uk KimBIO_set_nbio() always returns 1.
162e71b7053SJung-uk Kim
163e71b7053SJung-uk KimBIO_do_connect() returns 1 if the connection was successfully
164e71b7053SJung-uk Kimestablished and 0 or -1 if the connection failed.
165e71b7053SJung-uk Kim
166*da327cd2SJung-uk Kim=head1 EXAMPLES
167e71b7053SJung-uk Kim
168e71b7053SJung-uk KimThis is example connects to a webserver on the local host and attempts
169e71b7053SJung-uk Kimto retrieve a page and copy the result to standard output.
170e71b7053SJung-uk Kim
171e71b7053SJung-uk Kim
172e71b7053SJung-uk Kim BIO *cbio, *out;
173e71b7053SJung-uk Kim int len;
174e71b7053SJung-uk Kim char tmpbuf[1024];
175e71b7053SJung-uk Kim
176e71b7053SJung-uk Kim cbio = BIO_new_connect("localhost:http");
177e71b7053SJung-uk Kim out = BIO_new_fp(stdout, BIO_NOCLOSE);
178e71b7053SJung-uk Kim if (BIO_do_connect(cbio) <= 0) {
179e71b7053SJung-uk Kim     fprintf(stderr, "Error connecting to server\n");
180e71b7053SJung-uk Kim     ERR_print_errors_fp(stderr);
181e71b7053SJung-uk Kim     exit(1);
182e71b7053SJung-uk Kim }
183e71b7053SJung-uk Kim BIO_puts(cbio, "GET / HTTP/1.0\n\n");
184e71b7053SJung-uk Kim for (;;) {
185e71b7053SJung-uk Kim     len = BIO_read(cbio, tmpbuf, 1024);
186e71b7053SJung-uk Kim     if (len <= 0)
187e71b7053SJung-uk Kim         break;
188e71b7053SJung-uk Kim     BIO_write(out, tmpbuf, len);
189e71b7053SJung-uk Kim }
190e71b7053SJung-uk Kim BIO_free(cbio);
191e71b7053SJung-uk Kim BIO_free(out);
192e71b7053SJung-uk Kim
193e71b7053SJung-uk Kim
194e71b7053SJung-uk Kim=head1 SEE ALSO
195e71b7053SJung-uk Kim
196e71b7053SJung-uk KimL<BIO_ADDR(3)>
197e71b7053SJung-uk Kim
198e71b7053SJung-uk Kim=head1 HISTORY
199e71b7053SJung-uk Kim
200e71b7053SJung-uk KimBIO_set_conn_int_port(), BIO_get_conn_int_port(), BIO_set_conn_ip(), and BIO_get_conn_ip()
201e71b7053SJung-uk Kimwere removed in OpenSSL 1.1.0.
202e71b7053SJung-uk KimUse BIO_set_conn_address() and BIO_get_conn_address() instead.
203e71b7053SJung-uk Kim
204e71b7053SJung-uk Kim=head1 COPYRIGHT
205e71b7053SJung-uk Kim
206*da327cd2SJung-uk KimCopyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
207e71b7053SJung-uk Kim
208e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License").  You may not use
209e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
210e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
211e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
212e71b7053SJung-uk Kim
213e71b7053SJung-uk Kim=cut
214