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