BIO_s_connect.pod 7.4 KB

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