xref: /openbsd/lib/libcrypto/man/BIO_s_connect.3 (revision d415bd75)
1.\" $OpenBSD: BIO_s_connect.3,v 1.19 2023/04/30 13:53:54 schwarze Exp $
2.\" full merge up to: OpenSSL 0e474b8b Nov 1 15:45:49 2015 +0100
3.\"
4.\" This file is a derived work.
5.\" The changes are covered by the following Copyright and license:
6.\"
7.\" Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org>
8.\"
9.\" Permission to use, copy, modify, and distribute this software for any
10.\" purpose with or without fee is hereby granted, provided that the above
11.\" copyright notice and this permission notice appear in all copies.
12.\"
13.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20.\"
21.\" The original file was written by Dr. Stephen Henson <steve@openssl.org>.
22.\" Copyright (c) 2000, 2015 The OpenSSL Project.  All rights reserved.
23.\"
24.\" Redistribution and use in source and binary forms, with or without
25.\" modification, are permitted provided that the following conditions
26.\" are met:
27.\"
28.\" 1. Redistributions of source code must retain the above copyright
29.\"    notice, this list of conditions and the following disclaimer.
30.\"
31.\" 2. Redistributions in binary form must reproduce the above copyright
32.\"    notice, this list of conditions and the following disclaimer in
33.\"    the documentation and/or other materials provided with the
34.\"    distribution.
35.\"
36.\" 3. All advertising materials mentioning features or use of this
37.\"    software must display the following acknowledgment:
38.\"    "This product includes software developed by the OpenSSL Project
39.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
40.\"
41.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
42.\"    endorse or promote products derived from this software without
43.\"    prior written permission. For written permission, please contact
44.\"    openssl-core@openssl.org.
45.\"
46.\" 5. Products derived from this software may not be called "OpenSSL"
47.\"    nor may "OpenSSL" appear in their names without prior written
48.\"    permission of the OpenSSL Project.
49.\"
50.\" 6. Redistributions of any form whatsoever must retain the following
51.\"    acknowledgment:
52.\"    "This product includes software developed by the OpenSSL Project
53.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
54.\"
55.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
56.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
59.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
60.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
62.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
64.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
66.\" OF THE POSSIBILITY OF SUCH DAMAGE.
67.\"
68.Dd $Mdocdate: April 30 2023 $
69.Dt BIO_S_CONNECT 3
70.Os
71.Sh NAME
72.Nm BIO_s_connect ,
73.Nm BIO_new_connect ,
74.Nm BIO_set_conn_hostname ,
75.Nm BIO_set_conn_port ,
76.Nm BIO_set_conn_ip ,
77.Nm BIO_set_conn_int_port ,
78.Nm BIO_get_conn_hostname ,
79.Nm BIO_get_conn_port ,
80.Nm BIO_get_conn_ip ,
81.Nm BIO_get_conn_int_port ,
82.Nm BIO_set_nbio ,
83.Nm BIO_do_connect
84.Nd connect BIO
85.Sh SYNOPSIS
86.In openssl/bio.h
87.Ft const BIO_METHOD *
88.Fo BIO_s_connect
89.Fa void
90.Fc
91.Ft BIO *
92.Fo BIO_new_connect
93.Fa "const char *name"
94.Fc
95.Ft long
96.Fo BIO_set_conn_hostname
97.Fa "BIO *b"
98.Fa "char *name"
99.Fc
100.Ft long
101.Fo BIO_set_conn_port
102.Fa "BIO *b"
103.Fa "char *port"
104.Fc
105.Ft long
106.Fo BIO_set_conn_ip
107.Fa "BIO *b"
108.Fa "char *ip"
109.Fc
110.Ft long
111.Fo BIO_set_conn_int_port
112.Fa "BIO *b"
113.Fa "char *port"
114.Fc
115.Ft char *
116.Fo BIO_get_conn_hostname
117.Fa "BIO *b"
118.Fc
119.Ft char *
120.Fo BIO_get_conn_port
121.Fa "BIO *b"
122.Fc
123.Ft char *
124.Fo BIO_get_conn_ip
125.Fa "BIO *b"
126.Fc
127.Ft long
128.Fo BIO_get_conn_int_port
129.Fa "BIO *b"
130.Fc
131.Ft long
132.Fo BIO_set_nbio
133.Fa "BIO *b"
134.Fa "long n"
135.Fc
136.Ft long
137.Fo BIO_do_connect
138.Fa "BIO *b"
139.Fc
140.Sh DESCRIPTION
141.Fn BIO_s_connect
142returns the connect BIO method.
143This is a wrapper around the platform's TCP/IP socket connection routines.
144.Pp
145Using connect BIOs, TCP/IP connections can be made and data
146transferred using only BIO routines.
147In this way any platform specific operations
148are hidden by the BIO abstraction.
149.Pp
150Read and write operations on a connect BIO will perform I/O
151on the underlying connection.
152If no connection is established and the port and hostname (see below)
153is set up properly, then a connection is established first.
154.Pp
155Connect BIOs support
156.Xr BIO_puts 3
157but not
158.Xr BIO_gets 3 .
159.Pp
160If the close flag is set on a connect BIO, then any active connection
161is shutdown and the socket closed when the BIO is freed.
162.Pp
163Calling
164.Xr BIO_reset 3
165on a connect BIO will close any active connection and reset the BIO
166into a state where it can connect to the same host again.
167.Pp
168.Xr BIO_get_fd 3
169places the underlying socket in
170.Fa c
171if it is not
172.Dv NULL
173and also returns the socket.
174If
175.Fa c
176is not
177.Dv NULL ,
178it should be of type
179.Vt "int *" .
180.Pp
181.Xr BIO_set_info_callback 3
182and
183.Xr BIO_callback_ctrl 3
184with a
185.Fa cmd
186of
187.Dv BIO_CTRL_SET_CALLBACK
188save the pointer to the
189.Fa cb
190function internally in
191.Fa b
192and
193.Xr BIO_get_info_callback 3
194retrieves this function pointer.
195If such an info callback is installed, it is invoked whenever
196a state change or error occurs in the connect BIO state machine.
197The arguments of the callback include the new
198.Fa state
199in case of a state change or the old
200.Fa state
201in case of an error and the value
202.Fa res
203that the state machine would return to whatever operation invoked it
204if no info callback had been installed.
205If an info callback is installed, the state machine
206returns the return value of the info callback instead.
207Consequently, the info callback is supposed to usually return
208.Fa res .
209The precise effect of the return value depends on which operation
210the state machine was invoked from.
211Usually, \-1 is used to indicate failure and return values less than
212or equal to zero abort the operation in question, whereas positive
213values indicate success and allow the operation to proceed.
214.Pp
215The
216.Fa state
217constants passed to the callback are named according to
218which operation needs to be performed next.
219They are listed here in the order the states are passed through:
220.Pp
221.Bl -tag -width BIO_CONN_S_BLOCKED_CONNECT -offset 3n -compact
222.It Dv BIO_CONN_S_BEFORE
223The BIO is idle and no connection has been initiated yet.
224.It Dv BIO_CONN_S_GET_IP
225The hostname to connect to needs to be converted to an IP address.
226.It Dv BIO_CONN_S_GET_PORT
227The service name to connect to needs to be converted to a TCP port number.
228.It Dv BIO_CONN_S_CREATE_SOCKET
229The TCP socket needs to be created with the
230.Xr socket 2
231system call.
232.It Dv BIO_CONN_S_NBIO
233Socket options may need to be set using
234.Xr fcntl 2
235and
236.Xr setsockopt 2 .
237.It Dv BIO_CONN_S_CONNECT
238The connection needs to be initiated with the
239.Xr connect 2
240system call.
241.It Dv BIO_CONN_S_BLOCKED_CONNECT
242The
243.Xr connect 2
244system call would have blocked and needs to be tried again.
245.It Dv BIO_CONN_S_OK
246The connection has been established and can now be used to transfer data.
247.El
248.Pp
249.Fn BIO_set_conn_hostname
250uses the string
251.Fa name
252to set the hostname.
253The hostname can be an IP address.
254The hostname can also include the port in the form
255.Ar hostname : Ns Ar port .
256It is also acceptable to use the forms
257.Ar hostname Ns / Ns Pa any/other/path
258or
259.Ar hostname : Ns Ar port Ns / Ns Pa any/other/path .
260.Pp
261.Fn BIO_set_conn_port
262sets the port to
263.Fa port .
264.Fa port
265is looked up as a service using
266.Xr getaddrinfo 3 .
267.Pp
268.Fn BIO_set_conn_ip
269sets the IP address to
270.Fa ip
271using binary form i.e. four bytes specifying the IP address
272in big-endian form.
273.Pp
274.Fn BIO_set_conn_int_port
275sets the port using
276.Fa port .
277.Fa port
278should
279be of type
280.Vt "int *" .
281.Pp
282.Fn BIO_get_conn_hostname
283returns the hostname of the connect BIO or
284.Dv NULL
285if the BIO is initialized but no hostname is set.
286This return value is an internal pointer which should not be modified.
287.Pp
288.Fn BIO_get_conn_port
289returns the port as a string.
290This return value is an internal pointer which should not be modified.
291.Pp
292.Fn BIO_get_conn_ip
293returns the IP address in binary form.
294.Pp
295.Fn BIO_get_conn_int_port
296returns the port as an
297.Vt int .
298.Pp
299.Fn BIO_set_nbio
300sets the non-blocking I/O flag to
301.Fa n .
302If
303.Fa n
304is zero then blocking I/O is set.
305If
306.Fa n
307is 1 then non-blocking I/O is set.
308Blocking I/O is the default.
309The call to
310.Fn BIO_set_nbio
311should be made before the connection is established
312because non-blocking I/O is set during the connect process.
313.Pp
314.Fn BIO_new_connect
315combines
316.Xr BIO_new 3
317and
318.Fn BIO_set_conn_hostname
319into a single call.
320It creates a new connect BIO with
321.Fa name .
322.Pp
323.Fn BIO_do_connect
324attempts to connect the supplied BIO.
325It returns 1 if the connection was established successfully.
326A zero or negative value is returned if the connection
327could not be established.
328The call
329.Xr BIO_should_retry 3
330should be used for non-blocking connect BIOs
331to determine if the call should be retried.
332.Pp
333If blocking I/O is set then a non-positive return value from any
334I/O call is caused by an error condition, although a zero return
335will normally mean that the connection was closed.
336.Pp
337If the port name is supplied as part of the host name then this will
338override any value set with
339.Fn BIO_set_conn_port .
340This may be undesirable if the application does not wish to allow
341connection to arbitrary ports.
342This can be avoided by checking for the presence of the
343.Sq \&:
344character in the passed hostname and either indicating an error
345or truncating the string at that point.
346.Pp
347The values returned by
348.Fn BIO_get_conn_hostname ,
349.Fn BIO_get_conn_port ,
350.Fn BIO_get_conn_ip ,
351and
352.Fn BIO_get_conn_int_port
353are updated when a connection attempt is made.
354Before any connection attempt the values returned
355are those set by the application itself.
356.Pp
357Applications do not have to call
358.Fn BIO_do_connect
359but may wish to do so to separate the connection process
360from other I/O processing.
361.Pp
362If non-blocking I/O is set,
363then retries will be requested as appropriate.
364.Pp
365In addition to
366.Xr BIO_should_read 3
367and
368.Xr BIO_should_write 3
369it is also possible for
370.Xr BIO_should_io_special 3
371to be true during the initial connection process with the reason
372.Dv BIO_RR_CONNECT .
373If this is returned, it is an indication
374that a connection attempt would block.
375The application should then take appropriate action to wait
376until the underlying socket has connected and retry the call.
377.Pp
378When a chain containing a connect BIO is copied with
379.Xr BIO_dup_chain 3 ,
380.Fn BIO_set_conn_hostname ,
381.Fn BIO_set_conn_port ,
382.Fn BIO_set_nbio ,
383and
384.Xr BIO_set_info_callback 3
385are called internally to automatically copy the hostname, port,
386non-blocking I/O flag, and info callback from the original BIO object
387to the new one.
388.Pp
389.Xr BIO_ctrl 3
390.Fa cmd
391and
392.Fa larg
393arguments correspond to macros as follows:
394.Bl -column BIO_C_DO_STATE_MACHINE larg BIO_get_conn_hostname(3) -offset 3n
395.It Fa cmd No constant        Ta Fa larg Ta corresponding macro
396.It Dv BIO_C_DO_STATE_MACHINE Ta 0       Ta Fn BIO_do_connect
397.It Dv BIO_C_GET_CONNECT      Ta 0       Ta Fn BIO_get_conn_hostname
398.It                           Ta 1       Ta Fn BIO_get_conn_port
399.It                           Ta 2       Ta Fn BIO_get_conn_ip
400.It                           Ta 3       Ta Fn BIO_get_conn_int_port
401.It Dv BIO_C_GET_FD           Ta 0       Ta Xr BIO_get_fd 3
402.It Dv BIO_C_SET_CONNECT      Ta 0       Ta Fn BIO_set_conn_hostname
403.It                           Ta 1       Ta Fn BIO_set_conn_port
404.It                           Ta 2       Ta Fn BIO_set_conn_ip
405.It                           Ta 3       Ta Fn BIO_set_conn_int_port
406.It Dv BIO_C_SET_NBIO         Ta Fa n    Ta Fn BIO_set_nbio
407.It Dv BIO_CTRL_GET_CLOSE     Ta 0       Ta Xr BIO_get_close 3
408.It Dv BIO_CTRL_RESET         Ta 0       Ta Xr BIO_reset 3
409.It Dv BIO_CTRL_SET_CLOSE     Ta Fa flag Ta Xr BIO_set_close 3
410.El
411.Sh RETURN VALUES
412.Fn BIO_s_connect
413returns the connect BIO method.
414.Pp
415When called on a connect BIO object,
416.Xr BIO_method_type 3
417returns the constant
418.Dv BIO_TYPE_CONNECT
419and
420.Xr BIO_method_name 3
421returns a pointer to the static string
422.Qq socket connect .
423.Pp
424.Xr BIO_get_fd 3
425returns the socket or -1 if the BIO has not been initialized.
426.Pp
427.Fn BIO_set_conn_hostname ,
428.Fn BIO_set_conn_port ,
429.Fn BIO_set_conn_ip ,
430and
431.Fn BIO_set_conn_int_port
432always return 1.
433.Pp
434.Fn BIO_get_conn_hostname
435returns the connected hostname or
436.Dv NULL
437if none is set.
438.Pp
439.Fn BIO_get_conn_port
440returns a string representing the connected port or
441.Dv NULL
442if not set.
443.Pp
444.Fn BIO_get_conn_ip
445returns a pointer to the connected IP address in binary form
446or all zeros if not set.
447.Pp
448.Fn BIO_get_conn_int_port
449returns the connected port or 0 if none was set.
450.Pp
451.Fn BIO_set_nbio
452always returns 1.
453.Pp
454.Fn BIO_do_connect
455returns 1 if the connection was successfully
456established and 0 or -1 if the connection failed.
457.Sh EXAMPLES
458This example connects to a webserver on the local host and attempts
459to retrieve a page and copy the result to standard output.
460.Bd -literal -offset 2n
461BIO *cbio, *out;
462int len;
463char tmpbuf[1024];
464
465ERR_load_crypto_strings();
466cbio = BIO_new_connect("localhost:http");
467out = BIO_new_fp(stdout, BIO_NOCLOSE);
468if (BIO_do_connect(cbio) <= 0) {
469	fprintf(stderr, "Error connecting to server\en");
470	ERR_print_errors_fp(stderr);
471	/* whatever ... */
472}
473BIO_puts(cbio, "GET / HTTP/1.0\en\en");
474for(;;) {
475	len = BIO_read(cbio, tmpbuf, 1024);
476	if (len <= 0)
477		break;
478	BIO_write(out, tmpbuf, len);
479}
480BIO_free(cbio);
481BIO_free(out);
482.Ed
483.Sh SEE ALSO
484.Xr BIO_new 3
485.Sh HISTORY
486.Fn BIO_s_connect ,
487.Fn BIO_new_connect ,
488.Fn BIO_set_nbio ,
489and
490.Fn BIO_do_connect
491first appeared in SSLeay 0.8.0.
492.Fn BIO_set_conn_hostname ,
493.Fn BIO_set_conn_port ,
494.Fn BIO_set_conn_ip ,
495.Fn BIO_set_conn_int_port ,
496.Fn BIO_get_conn_hostname ,
497.Fn BIO_get_conn_port ,
498.Fn BIO_get_conn_ip ,
499and
500.Fn BIO_get_conn_int_port
501first appeared in SSLeay 0.9.0.
502All these functions have been available since
503.Ox 2.4 .
504