xref: /openssl/doc/man3/BIO_s_connect.pod (revision 7a9b09fe)
1=pod
2
3=head1 NAME
4
5BIO_s_connect, BIO_new_connect,
6BIO_set_conn_hostname, BIO_set_conn_port,
7BIO_set_conn_address, BIO_set_conn_ip_family,
8BIO_get_conn_hostname, BIO_get_conn_port,
9BIO_get_conn_address, BIO_get_conn_ip_family,
10BIO_set_nbio, BIO_do_connect - connect BIO
11
12=head1 SYNOPSIS
13
14 #include <openssl/bio.h>
15
16 const BIO_METHOD *BIO_s_connect(void);
17
18 BIO *BIO_new_connect(const char *name);
19
20 long BIO_set_conn_hostname(BIO *b, char *name);
21 long BIO_set_conn_port(BIO *b, char *port);
22 long BIO_set_conn_address(BIO *b, BIO_ADDR *addr);
23 long BIO_set_conn_ip_family(BIO *b, long family);
24 const char *BIO_get_conn_hostname(BIO *b);
25 const char *BIO_get_conn_port(BIO *b);
26 const BIO_ADDR *BIO_get_conn_address(BIO *b);
27 const long BIO_get_conn_ip_family(BIO *b);
28
29 long BIO_set_nbio(BIO *b, long n);
30
31 int BIO_do_connect(BIO *b);
32
33=head1 DESCRIPTION
34
35BIO_s_connect() returns the connect BIO method. This is a wrapper
36round the platform's TCP/IP socket connection routines.
37
38Using connect BIOs, TCP/IP connections can be made and data
39transferred using only BIO routines. In this way any platform
40specific operations are hidden by the BIO abstraction.
41
42Read and write operations on a connect BIO will perform I/O
43on the underlying connection. If no connection is established
44and the port and hostname (see below) is set up properly then
45a connection is established first.
46
47Connect BIOs support BIO_puts() and BIO_gets().
48
49If the close flag is set on a connect BIO then any active
50connection is shutdown and the socket closed when the BIO
51is freed.
52
53Calling BIO_reset() on a connect BIO will close any active
54connection and reset the BIO into a state where it can connect
55to the same host again.
56
57BIO_new_connect() combines BIO_new() and BIO_set_conn_hostname() into
58a single call: that is it creates a new connect BIO with hostname B<name>.
59
60BIO_set_conn_hostname() uses the string B<name> to set the hostname.
61The hostname can be an IP address; if the address is an IPv6 one, it
62must be enclosed with brackets C<[> and C<]>.
63The hostname can also include the port in the form hostname:port;
64see L<BIO_parse_hostserv(3)> and BIO_set_conn_port() for details.
65
66BIO_set_conn_port() sets the port to B<port>. B<port> can be the
67numerical form or a service string such as "http", which
68will be mapped to a port number using the system function getservbyname().
69
70BIO_set_conn_address() sets the address and port information using
71a BIO_ADDR(3ssl).
72
73BIO_set_conn_ip_family() sets the IP family.
74
75BIO_get_conn_hostname() returns the hostname of the connect BIO or
76NULL if the BIO is initialized but no hostname is set.
77This return value is an internal pointer which should not be modified.
78
79BIO_get_conn_port() returns the port as a string.
80This return value is an internal pointer which should not be modified.
81
82BIO_get_conn_address() returns the address information as a BIO_ADDR.
83This return value is an internal pointer which should not be modified.
84
85BIO_get_conn_ip_family() returns the IP family of the connect BIO.
86
87BIO_set_nbio() sets the non blocking I/O flag to B<n>. If B<n> is
88zero then blocking I/O is set. If B<n> is 1 then non blocking I/O
89is set. Blocking I/O is the default. The call to BIO_set_nbio()
90should be made before the connection is established because
91non blocking I/O is set during the connect process.
92
93BIO_do_connect() attempts to connect the supplied BIO.
94This performs an SSL/TLS handshake as far as supported by the BIO.
95For non-SSL BIOs the connection is done typically at TCP level.
96If domain name resolution yields multiple IP addresses all of them are tried
97after connect() failures.
98The function returns 1 if the connection was established successfully.
99A zero or negative value is returned if the connection could not be established.
100The call BIO_should_retry() should be used for non blocking connect BIOs
101to determine if the call should be retried.
102If a connection has already been established this call has no effect.
103
104=head1 NOTES
105
106If blocking I/O is set then a non positive return value from any
107I/O call is caused by an error condition, although a zero return
108will normally mean that the connection was closed.
109
110If the port name is supplied as part of the hostname then this will
111override any value set with BIO_set_conn_port(). This may be undesirable
112if the application does not wish to allow connection to arbitrary
113ports. This can be avoided by checking for the presence of the ':'
114character in the passed hostname and either indicating an error or
115truncating the string at that point.
116
117The values returned by BIO_get_conn_hostname(), BIO_get_conn_address(),
118and BIO_get_conn_port() are updated when a connection attempt is made.
119Before any connection attempt the values returned are those set by the
120application itself.
121
122Applications do not have to call BIO_do_connect() but may wish to do
123so to separate the connection process from other I/O processing.
124
125If non blocking I/O is set then retries will be requested as appropriate.
126
127It addition to BIO_should_read() and BIO_should_write() it is also
128possible for BIO_should_io_special() to be true during the initial
129connection process with the reason BIO_RR_CONNECT. If this is returned
130then this is an indication that a connection attempt would block,
131the application should then take appropriate action to wait until
132the underlying socket has connected and retry the call.
133
134BIO_set_conn_hostname(), BIO_set_conn_port(), BIO_get_conn_hostname(),
135BIO_set_conn_address(), BIO_get_conn_port(), BIO_get_conn_address(),
136BIO_set_conn_ip_family(), BIO_get_conn_ip_family(),
137BIO_set_nbio(), and BIO_do_connect() are macros.
138
139=head1 RETURN VALUES
140
141BIO_s_connect() returns the connect BIO method.
142
143BIO_set_conn_address(), BIO_set_conn_port(), and BIO_set_conn_ip_family()
144return 1 or <=0 if an error occurs.
145
146BIO_set_conn_hostname() returns 1 on success and <=0 on failure.
147
148BIO_get_conn_address() returns the address information or NULL if none
149was set.
150
151BIO_get_conn_hostname() returns the connected hostname or NULL if
152none was set.
153
154BIO_get_conn_ip_family() returns the address family or -1 if none was set.
155
156BIO_get_conn_port() returns a string representing the connected
157port or NULL if not set.
158
159BIO_set_nbio() returns 1 or <=0 if an error occurs.
160
161BIO_do_connect() returns 1 if the connection was successfully
162established and <=0 if the connection failed.
163
164=head1 EXAMPLES
165
166This is example connects to a webserver on the local host and attempts
167to retrieve a page and copy the result to standard output.
168
169
170 BIO *cbio, *out;
171 int len;
172 char tmpbuf[1024];
173
174 cbio = BIO_new_connect("localhost:http");
175 out = BIO_new_fp(stdout, BIO_NOCLOSE);
176 if (BIO_do_connect(cbio) <= 0) {
177     fprintf(stderr, "Error connecting to server\n");
178     ERR_print_errors_fp(stderr);
179     exit(1);
180 }
181 BIO_puts(cbio, "GET / HTTP/1.0\n\n");
182 for (;;) {
183     len = BIO_read(cbio, tmpbuf, 1024);
184     if (len <= 0)
185         break;
186     BIO_write(out, tmpbuf, len);
187 }
188 BIO_free(cbio);
189 BIO_free(out);
190
191
192=head1 SEE ALSO
193
194L<BIO_ADDR(3)>, L<BIO_parse_hostserv(3)>
195
196=head1 HISTORY
197
198BIO_set_conn_int_port(), BIO_get_conn_int_port(), BIO_set_conn_ip(), and BIO_get_conn_ip()
199were removed in OpenSSL 1.1.0.
200Use BIO_set_conn_address() and BIO_get_conn_address() instead.
201
202Connect BIOs support BIO_gets() since OpenSSL 3.1.
203
204=head1 COPYRIGHT
205
206Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
207
208Licensed under the Apache License 2.0 (the "License").  You may not use
209this file except in compliance with the License.  You can obtain a copy
210in the file LICENSE in the source distribution or at
211L<https://www.openssl.org/source/license.html>.
212
213=cut
214