xref: /openbsd/lib/libcrypto/man/BIO_s_connect.3 (revision 264ca280)
1.Dd $Mdocdate: February 16 2015 $
2.Dt BIO_S_CONNECT 3
3.Os
4.Sh NAME
5.Nm BIO_s_connect ,
6.Nm BIO_new_connect ,
7.Nm BIO_set_conn_hostname ,
8.Nm BIO_set_conn_port ,
9.Nm BIO_set_conn_ip ,
10.Nm BIO_set_conn_int_port ,
11.Nm BIO_get_conn_hostname ,
12.Nm BIO_get_conn_port ,
13.Nm BIO_get_conn_ip ,
14.Nm BIO_get_conn_int_port ,
15.Nm BIO_set_nbio ,
16.Nm BIO_do_connect
17.Nd connect BIO
18.Sh SYNOPSIS
19.In openssl/bio.h
20.Ft BIO_METHOD *
21.Fo BIO_s_connect
22.Fa void
23.Fc
24.Ft BIO *
25.Fo BIO_new_connect
26.Fa "char *name"
27.Fc
28.Ft long
29.Fo BIO_set_conn_hostname
30.Fa "BIO *b"
31.Fa "char *name"
32.Fc
33.Ft long
34.Fo BIO_set_conn_port
35.Fa "BIO *b"
36.Fa "char *port"
37.Fc
38.Ft long
39.Fo BIO_set_conn_ip
40.Fa "BIO *b"
41.Fa "char *ip"
42.Fc
43.Ft long
44.Fo BIO_set_conn_int_port
45.Fa "BIO *b"
46.Fa "char *port"
47.Fc
48.Ft char *
49.Fo BIO_get_conn_hostname
50.Fa "BIO *b"
51.Fc
52.Ft char *
53.Fo BIO_get_conn_port
54.Fa "BIO *b"
55.Fc
56.Ft char *
57.Fo BIO_get_conn_ip
58.Fa "BIO *b"
59.Fa "dummy"
60.Fc
61.Ft long
62.Fo BIO_get_conn_int_port
63.Fa "BIO *b"
64.Fa "int port"
65.Fc
66.Ft long
67.Fo BIO_set_nbio
68.Fa "BIO *b"
69.Fa "long n"
70.Fc
71.Ft int
72.Fo BIO_do_connect
73.Fa "BIO *b"
74.Fc
75.Sh DESCRIPTION
76.Fn BIO_s_connect
77returns the connect BIO method.
78This is a wrapper around the platform's TCP/IP socket connection routines.
79.Pp
80Using connect BIOs, TCP/IP connections can be made and data
81transferred using only BIO routines.
82In this way any platform specific operations
83are hidden by the BIO abstraction.
84.Pp
85Read and write operations on a connect BIO will perform I/O
86on the underlying connection.
87If no connection is established and the port and hostname (see below)
88is set up properly, then a connection is established first.
89.Pp
90Connect BIOs support
91.Xr BIO_puts 3
92but not
93.Xr BIO_gets 3 .
94.Pp
95If the close flag is set on a connect BIO, then any active connection
96is shutdown and the socket closed when the BIO is freed.
97.Pp
98Calling
99.Xr BIO_reset 3
100on a connect BIO will close any active connection and reset the BIO
101into a state where it can connect to the same host again.
102.Pp
103.Xr BIO_get_fd 3
104places the underlying socket in
105.Fa c
106if it is not
107.Dv NULL ,
108it also returns the socket.
109If
110.Fa c
111is not
112.Dv NULL
113it should be of type
114.Vt "int *" .
115.Pp
116.Fn BIO_set_conn_hostname
117uses the string
118.Fa name
119to set the hostname.
120The hostname can be an IP address.
121The hostname can also include the port in the form
122.Ar hostname : Ns Ar port .
123It is also acceptable to use the forms
124.Ar hostname Ns / Ns Pa any/other/path
125or
126.Ar hostname : Ns Ar port Ns / Ns Pa any/other/path .
127.Pp
128.Fn BIO_set_conn_port
129sets the port to
130.Fa port .
131.Fa port
132can be the numerical form or a string such as
133.Cm http .
134A string will be looked up first using
135.Xr getservbyname 3
136on the host platform, but if that fails
137a built-in table of port names will be used.
138Currently the list is
139.Cm http ,
140.Cm telnet ,
141.Cm socks ,
142.Cm https ,
143.Cm ssl ,
144.Cm ftp ,
145.Cm gopher ,
146and
147.Cm wais .
148.Pp
149.Fn BIO_set_conn_ip
150sets the IP address to
151.Fa ip
152using binary form, that is four bytes specifying the IP address
153in big-endian form.
154.Pp
155.Fn BIO_set_conn_int_port
156sets the port using
157.Fa port .
158.Fa port
159should
160be of type
161.Vt "int *" .
162.Pp
163.Fn BIO_get_conn_hostname
164returns the hostname of the connect BIO or
165.Dv NULL
166if the BIO is initialized but no hostname is set.
167This return value is an internal pointer which should not be modified.
168.Pp
169.Fn BIO_get_conn_port
170returns the port as a string.
171.Pp
172.Fn BIO_get_conn_ip
173returns the IP address in binary form.
174.Pp
175.Fn BIO_get_conn_int_port
176returns the port as an
177.Vt int .
178.Pp
179.Fn BIO_set_nbio
180sets the non blocking I/O flag to
181.Fa n .
182If
183.Fa n
184is zero then blocking I/O is set.
185If
186.Fa n
187is 1 then non blocking I/O is set.
188Blocking I/O is the default.
189The call to
190.Fn BIO_set_nbio
191should be made before the connection is established
192because non blocking I/O is set during the connect process.
193.Pp
194.Fn BIO_new_connect
195combines
196.Xr BIO_new 3
197and
198.Fn BIO_set_conn_hostname
199into a single call.
200It creates a new connect BIO with
201.Fa name .
202.Pp
203.Fn BIO_do_connect
204attempts to connect the supplied BIO.
205It returns 1 if the connection was established successfully.
206A zero or negative value is returned if the connection
207could not be established.
208The call
209.Xr BIO_should_retry 3
210should be used for non blocking connect BIOs
211to determine if the call should be retried.
212.Sh NOTES
213If blocking I/O is set then a non positive return value from any
214I/O call is caused by an error condition, although a zero return
215will normally mean that the connection was closed.
216.Pp
217If the port name is supplied as part of the host name then this will
218override any value set with
219.Fn BIO_set_conn_port .
220This may be undesirable if the application does not wish to allow
221connection to arbitrary ports.
222This can be avoided by checking for the presence of the
223.Sq \&:
224character in the passed hostname and either indicating an error
225or truncating the string at that point.
226.Pp
227The values returned by
228.Fn BIO_get_conn_hostname ,
229.Fn BIO_get_conn_port ,
230.Fn BIO_get_conn_ip ,
231and
232.Fn BIO_get_conn_int_port
233are updated when a connection attempt is made.
234Before any connection attempt the values returned
235are those set by the application itself.
236.Pp
237Applications do not have to call
238.Fn BIO_do_connect
239but may wish to do so to separate the connection process
240from other I/O processing.
241.Pp
242If non-blocking I/O is set,
243then retries will be requested as appropriate.
244.Pp
245It addition to
246.Xr BIO_should_read 3
247and
248.Xr BIO_should_write 3
249it is also possible for
250.Xr BIO_should_io_special 3
251to be true during the initial connection process with the reason
252.Dv BIO_RR_CONNECT .
253If this is returned, it is an indication
254that a connection attempt would block.
255The application should then take appropriate action to wait
256until the underlying socket has connected and retry the call.
257.Pp
258.Fn BIO_set_conn_hostname ,
259.Fn BIO_set_conn_port ,
260.Fn BIO_set_conn_ip ,
261.Fn BIO_set_conn_int_port ,
262.Fn BIO_get_conn_hostname ,
263.Fn BIO_get_conn_port ,
264.Fn BIO_get_conn_ip ,
265.Fn BIO_get_conn_int_port ,
266.Fn BIO_set_nbio ,
267and
268.Fn BIO_do_connect
269are macros.
270.Sh RETURN VALUES
271.Fn BIO_s_connect
272returns the connect BIO method.
273.Pp
274.Xr BIO_get_fd 3
275returns the socket or -1 if the BIO has not been initialized.
276.Pp
277.Fn BIO_set_conn_hostname ,
278.Fn BIO_set_conn_port ,
279.Fn BIO_set_conn_ip ,
280and
281.Fn BIO_set_conn_int_port
282always return 1.
283.Pp
284.Fn BIO_get_conn_hostname
285returns the connected hostname or
286.Dv NULL
287if none is set.
288.Pp
289.Fn BIO_get_conn_port
290returns a string representing the connected port or
291.Dv NULL
292if not set.
293.Pp
294.Fn BIO_get_conn_ip
295returns a pointer to the connected IP address in binary form
296or all zeros if not set.
297.Pp
298.Fn BIO_get_conn_int_port
299returns the connected port or 0 if none was set.
300.Pp
301.Fn BIO_set_nbio
302always returns 1.
303.Pp
304.Fn BIO_do_connect
305returns 1 if the connection was successfully
306established and 0 or -1 if the connection failed.
307.Sh EXAMPLES
308This example connects to a webserver on the local host and attempts
309to retrieve a page and copy the result to standard output.
310.Bd -literal -offset 2n
311BIO *cbio, *out;
312int len;
313char tmpbuf[1024];
314
315ERR_load_crypto_strings();
316cbio = BIO_new_connect("localhost:http");
317out = BIO_new_fp(stdout, BIO_NOCLOSE);
318if (BIO_do_connect(cbio) <= 0) {
319	fprintf(stderr, "Error connecting to server\en");
320	ERR_print_errors_fp(stderr);
321	/* whatever ... */
322}
323BIO_puts(cbio, "GET / HTTP/1.0\en\en");
324for(;;) {
325	len = BIO_read(cbio, tmpbuf, 1024);
326	if (len <= 0)
327		break;
328	BIO_write(out, tmpbuf, len);
329}
330BIO_free(cbio);
331BIO_free(out);
332.Ed
333