4efd5676b859139573da80ae07b677c5c5d9aa2c
[openssl.git] / doc / crypto / BIO_s_connect.pod
1 =pod
2
3 =head1 NAME
4
5 BIO_s_connect, BIO_new_connect, BIO_set_conn_hostname, BIO_set_conn_port,
6 BIO_set_conn_ip, BIO_set_conn_int_port, BIO_get_conn_hostname,
7 BIO_get_conn_port, BIO_get_conn_ip, BIO_get_conn_int_port,
8 BIO_set_nbio, BIO_do_connect - connect BIO
9
10 =head1 SYNOPSIS
11
12  #include <openssl/bio.h>
13
14  BIO_METHOD * BIO_s_connect(void);
15
16  BIO *BIO_new_connect(char *name);
17
18  long BIO_set_conn_hostname(BIO *b, char *name);
19  long BIO_set_conn_port(BIO *b, char *port);
20  long BIO_set_conn_ip(BIO *b, char *ip);
21  long BIO_set_conn_int_port(BIO *b, char *port);
22  char *BIO_get_conn_hostname(BIO *b);
23  char *BIO_get_conn_port(BIO *b);
24  char *BIO_get_conn_ip(BIO *b, dummy);
25  long BIO_get_conn_int_port(BIO *b, int port);
26
27  long BIO_set_nbio(BIO *b, long n);
28
29  int BIO_do_connect(BIO *b);
30
31 =head1 DESCRIPTION
32
33 BIO_s_connect() returns the connect BIO method. This is a wrapper
34 round the platform's TCP/IP socket connection routines.
35
36 Using connect BIOs, TCP/IP connections can be made and data
37 transferred using only BIO routines. In this way any platform
38 specific operations are hidden by the BIO abstraction.
39
40 Read and write operations on a connect BIO will perform I/O
41 on the underlying connection. If no connection is established
42 and the port and hostname (see below) is set up properly then
43 a connection is established first.
44
45 Connect BIOs support BIO_puts() but not BIO_gets().
46
47 If the close flag is set on a connect BIO then any active
48 connection is shutdown and the socket closed when the BIO
49 is freed.
50
51 Calling BIO_reset() on a connect BIO will close any active
52 connection and reset the BIO into a state where it can connect
53 to the same host again.
54
55 BIO_get_fd() places the underlying socket in B<c> if it is not NULL,
56 it also returns the socket . If B<c> is not NULL it should be of
57 type (int *).
58
59 BIO_set_conn_hostname() uses the string B<name> to set the hostname.
60 The hostname can be an IP address. The hostname can also include the
61 port in the form hostname:port . It is also acceptable to use the
62 form "hostname/any/other/path" or "hostname:port/any/other/path".
63
64 BIO_set_conn_port() sets the port to B<port>. B<port> can be the
65 numerical form or a string such as "http". A string will be looked
66 up first using getservbyname() on the host platform but if that
67 fails a standard table of port names will be used. This internal
68 list is http, telnet, socks, https, ssl, ftp, and gopher.
69
70 BIO_set_conn_ip() sets the IP address to B<ip> using binary form,
71 that is four bytes specifying the IP address in big-endian form.
72
73 BIO_set_conn_int_port() sets the port using B<port>. B<port> should
74 be of type (int *).
75
76 BIO_get_conn_hostname() returns the hostname of the connect BIO or
77 NULL if the BIO is initialized but no hostname is set.
78 This return value is an internal pointer which should not be modified.
79
80 BIO_get_conn_port() returns the port as a string.
81
82 BIO_get_conn_ip() returns the IP address in binary form.
83
84 BIO_get_conn_int_port() returns the port as an int.
85
86 BIO_set_nbio() sets the non blocking I/O flag to B<n>. If B<n> is
87 zero then blocking I/O is set. If B<n> is 1 then non blocking I/O
88 is set. Blocking I/O is the default. The call to BIO_set_nbio()
89 should be made before the connection is established because 
90 non blocking I/O is set during the connect process.
91
92 BIO_new_connect() combines BIO_new() and BIO_set_conn_hostname() into
93 a single call: that is it creates a new connect BIO with B<name>.
94
95 BIO_do_connect() attempts to connect the supplied BIO. It returns 1
96 if the connection was established successfully. A zero or negative
97 value is returned if the connection could not be established, the
98 call BIO_should_retry() should be used for non blocking connect BIOs
99 to determine if the call should be retried.
100
101 =head1 NOTES
102
103 If blocking I/O is set then a non positive return value from any
104 I/O call is caused by an error condition, although a zero return
105 will normally mean that the connection was closed.
106
107 If the port name is supplied as part of the host name then this will
108 override any value set with BIO_set_conn_port(). This may be undesirable
109 if the application does not wish to allow connection to arbitrary
110 ports. This can be avoided by checking for the presence of the ':'
111 character in the passed hostname and either indicating an error or
112 truncating the string at that point.
113
114 The values returned by BIO_get_conn_hostname(), BIO_get_conn_port(),
115 BIO_get_conn_ip() and BIO_get_conn_int_port() are updated when a
116 connection attempt is made. Before any connection attempt the values
117 returned are those set by the application itself.
118
119 Applications do not have to call BIO_do_connect() but may wish to do
120 so to separate the connection process from other I/O processing.
121
122 If non blocking I/O is set then retries will be requested as appropriate.
123
124 It addition to BIO_should_read() and BIO_should_write() it is also
125 possible for BIO_should_io_special() to be true during the initial
126 connection process with the reason BIO_RR_CONNECT. If this is returned
127 then this is an indication that a connection attempt would block,
128 the application should then take appropriate action to wait until
129 the underlying socket has connected and retry the call.
130
131 BIO_set_conn_hostname(), BIO_set_conn_port(), BIO_set_conn_ip(),
132 BIO_set_conn_int_port(), BIO_get_conn_hostname(), BIO_get_conn_port(),
133 BIO_get_conn_ip(), BIO_get_conn_int_port(), BIO_set_nbio() and
134 BIO_do_connect() are macros.
135
136 =head1 RETURN VALUES
137
138 BIO_s_connect() returns the connect BIO method.
139
140 BIO_get_fd() returns the socket or -1 if the BIO has not
141 been initialized.
142
143 BIO_set_conn_hostname(), BIO_set_conn_port(), BIO_set_conn_ip() and
144 BIO_set_conn_int_port() always return 1.
145
146 BIO_get_conn_hostname() returns the connected hostname or NULL is
147 none was set.
148
149 BIO_get_conn_port() returns a string representing the connected
150 port or NULL if not set.
151
152 BIO_get_conn_ip() returns a pointer to the connected IP address in
153 binary form or all zeros if not set.
154
155 BIO_get_conn_int_port() returns the connected port or 0 if none was
156 set.
157
158 BIO_set_nbio() always returns 1.
159
160 BIO_do_connect() returns 1 if the connection was successfully
161 established and 0 or -1 if the connection failed.
162
163 =head1 EXAMPLE
164
165 This is example connects to a webserver on the local host and attempts
166 to retrieve a page and copy the result to standard output.
167
168
169  BIO *cbio, *out;
170  int len;
171  char tmpbuf[1024];
172  ERR_load_crypto_strings();
173  cbio = BIO_new_connect("localhost:http");
174  out = BIO_new_fp(stdout, BIO_NOCLOSE);
175  if(BIO_do_connect(cbio) <= 0) {
176         fprintf(stderr, "Error connecting to server\n");
177         ERR_print_errors_fp(stderr);
178         /* whatever ... */
179         }
180  BIO_puts(cbio, "GET / HTTP/1.0\n\n");
181  for(;;) {      
182         len = BIO_read(cbio, tmpbuf, 1024);
183         if(len <= 0) break;
184         BIO_write(out, tmpbuf, len);
185  }
186  BIO_free(cbio);
187  BIO_free(out);
188
189
190 =head1 SEE ALSO
191
192 TBA