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