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