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