BIO_s_connect.pod 8.6 KB

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