---
name: "libressl-BIO_s_connect"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-BIO_s_connect"
---

# libressl-BIO\_s\_connect(3)

connect BIO

## NAME

**BIO\_s\_connect**, **BIO\_new\_connect**, **BIO\_set\_conn\_hostname**, **BIO\_set\_conn\_port**, **BIO\_set\_conn\_ip**, **BIO\_set\_conn\_int\_port**, **BIO\_get\_conn\_hostname**, **BIO\_get\_conn\_port**, **BIO\_get\_conn\_ip**, **BIO\_get\_conn\_int\_port**, **BIO\_set\_nbio**, **BIO\_do\_connect** – connect BIO

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/bio.h\>**

*const* *BIO\_METHOD* *\**\

**BIO\_s\_connect**(*void*);

*BIO* *\**\

**BIO\_new\_connect**(*const char \*name*);

*long*\

**BIO\_set\_conn\_hostname**(*BIO \*b*, *char \*name*);

*long*\

**BIO\_set\_conn\_port**(*BIO \*b*, *char \*port*);

*long*\

**BIO\_set\_conn\_ip**(*BIO \*b*, *char \*ip*);

*long*\

**BIO\_set\_conn\_int\_port**(*BIO \*b*, *char \*port*);

*char* *\**\

**BIO\_get\_conn\_hostname**(*BIO \*b*);

*char* *\**\

**BIO\_get\_conn\_port**(*BIO \*b*);

*char* *\**\

**BIO\_get\_conn\_ip**(*BIO \*b*);

*long*\

**BIO\_get\_conn\_int\_port**(*BIO \*b*);

*long*\

**BIO\_set\_nbio**(*BIO \*b*, *long n*);

*long*\

**BIO\_do\_connect**(*BIO \*b*);

## DESCRIPTION

**BIO\_s\_connect**() returns the connect BIO method. This is a wrapper around the platform's TCP/IP socket connection routines.

Using connect BIOs, TCP/IP connections can be made and data transferred using only BIO routines. In this way any platform specific operations are hidden by the BIO abstraction.

Read and write operations on a connect BIO will perform I/O on the underlying connection. If no connection is established and the port and hostname (see below) is set up properly, then a connection is established first.

Connect BIOs support BIO\_puts(3) but not BIO\_gets(3).

If the close flag is set on a connect BIO, then any active connection is shutdown and the socket closed when the BIO is freed.

Calling BIO\_reset(3) on a connect BIO will close any active connection and reset the BIO into a state where it can connect to the same host again.

BIO\_get\_fd(3) places the underlying socket in *c* if it is not NULL and also returns the socket. If *c* is not NULL, it should be of type *int \**.

BIO\_set\_info\_callback(3) and BIO\_callback\_ctrl(3) with a *cmd* of BIO\_CTRL\_SET\_CALLBACK save the pointer to the *cb* function internally in *b* and BIO\_get\_info\_callback(3) retrieves this function pointer. If such an info callback is installed, it is invoked whenever a state change or error occurs in the connect BIO state machine. The arguments of the callback include the new *state* in case of a state change or the old *state* in case of an error and the value *res* that the state machine would return to whatever operation invoked it if no info callback had been installed. If an info callback is installed, the state machine returns the return value of the info callback instead. Consequently, the info callback is supposed to usually return *res*. The precise effect of the return value depends on which operation the state machine was invoked from. Usually, -1 is used to indicate failure and return values less than or equal to zero abort the operation in question, whereas positive values indicate success and allow the operation to proceed.

The *state* constants passed to the callback are named according to which operation needs to be performed next. They are listed here in the order the states are passed through:

- BIO\_CONN\_S\_BEFORE

  The BIO is idle and no connection has been initiated yet.

- BIO\_CONN\_S\_GET\_IP

  The hostname to connect to needs to be converted to an IP address.

- BIO\_CONN\_S\_GET\_PORT

  The service name to connect to needs to be converted to a TCP port number.

- BIO\_CONN\_S\_CREATE\_SOCKET

  The TCP socket needs to be created with the [socket(2)](/socket/2.md) system call.

- BIO\_CONN\_S\_NBIO

  Socket options may need to be set using [fcntl(2)](/fcntl/2.md) and [setsockopt(2)](/setsockopt/2.md).

- BIO\_CONN\_S\_CONNECT

  The connection needs to be initiated with the [connect(2)](/connect/2.md) system call.

- BIO\_CONN\_S\_BLOCKED\_CONNECT

  The [connect(2)](/connect/2.md) system call would have blocked and needs to be tried again.

- BIO\_CONN\_S\_OK

  The connection has been established and can now be used to transfer data.

**BIO\_set\_conn\_hostname**() uses the string *name* to set the hostname. The hostname can be an IP address. The hostname can also include the port in the form *hostname*:*port*. It is also acceptable to use the forms *hostname*/*any/other/path* or *hostname*:*port*/*any/other/path*.

**BIO\_set\_conn\_port**() sets the port to *port*. *port* is looked up as a service using [getaddrinfo(3)](/getaddrinfo/3.md).

**BIO\_set\_conn\_ip**() sets the IP address to *ip* using binary form i.e. four bytes specifying the IP address in big-endian form.

**BIO\_set\_conn\_int\_port**() sets the port using *port*. *port* should be of type *int \**.

**BIO\_get\_conn\_hostname**() returns the hostname of the connect BIO or NULL if the BIO is initialized but no hostname is set. This return value is an internal pointer which should not be modified.

**BIO\_get\_conn\_port**() returns the port as a string. This return value is an internal pointer which should not be modified.

**BIO\_get\_conn\_ip**() returns the IP address in binary form.

**BIO\_get\_conn\_int\_port**() returns the port as an *int*.

**BIO\_set\_nbio**() sets the non-blocking I/O flag to *n*. If *n* is zero then blocking I/O is set. If *n* is 1 then non-blocking I/O is set. Blocking I/O is the default. The call to **BIO\_set\_nbio**() should be made before the connection is established because non-blocking I/O is set during the connect process.

**BIO\_new\_connect**() combines BIO\_new(3) and **BIO\_set\_conn\_hostname**() into a single call. It creates a new connect BIO with *name*.

**BIO\_do\_connect**() attempts to connect the supplied BIO. It returns 1 if the connection was established successfully. A zero or negative value is returned if the connection could not be established. The call BIO\_should\_retry(3) should be used for non-blocking connect BIOs to determine if the call should be retried.

If blocking I/O is set then a non-positive return value from any I/O call is caused by an error condition, although a zero return will normally mean that the connection was closed.

If the port name is supplied as part of the host name then this will override any value set with **BIO\_set\_conn\_port**(). This may be undesirable if the application does not wish to allow connection to arbitrary ports. This can be avoided by checking for the presence of the ‘:’ character in the passed hostname and either indicating an error or truncating the string at that point.

The values returned by **BIO\_get\_conn\_hostname**(), **BIO\_get\_conn\_port**(), **BIO\_get\_conn\_ip**(), and **BIO\_get\_conn\_int\_port**() are updated when a connection attempt is made. Before any connection attempt the values returned are those set by the application itself.

Applications do not have to call **BIO\_do\_connect**() but may wish to do so to separate the connection process from other I/O processing.

If non-blocking I/O is set, then retries will be requested as appropriate.

In addition to BIO\_should\_read(3) and BIO\_should\_write(3) it is also possible for BIO\_should\_io\_special(3) to be true during the initial connection process with the reason BIO\_RR\_CONNECT. If this is returned, it is an indication that a connection attempt would block. The application should then take appropriate action to wait until the underlying socket has connected and retry the call.

When a chain containing a connect BIO is copied with BIO\_dup\_chain(3), **BIO\_set\_conn\_hostname**(), **BIO\_set\_conn\_port**(), **BIO\_set\_nbio**(), and BIO\_set\_info\_callback(3) are called internally to automatically copy the hostname, port, non-blocking I/O flag, and info callback from the original BIO object to the new one.

BIO\_ctrl(3) *cmd* and *larg* arguments correspond to macros as follows:

| | | |
| --- | --- | --- |
| *cmd* constant | *larg* | corresponding macro |
| BIO\_C\_DO\_STATE\_MACHINE | 0 | **BIO\_do\_connect**() |
| BIO\_C\_GET\_CONNECT | 0 | **BIO\_get\_conn\_hostname**() |
|  | 1 | **BIO\_get\_conn\_port**() |
|  | 2 | **BIO\_get\_conn\_ip**() |
|  | 3 | **BIO\_get\_conn\_int\_port**() |
| BIO\_C\_GET\_FD | 0 | BIO\_get\_fd(3) |
| BIO\_C\_SET\_CONNECT | 0 | **BIO\_set\_conn\_hostname**() |
|  | 1 | **BIO\_set\_conn\_port**() |
|  | 2 | **BIO\_set\_conn\_ip**() |
|  | 3 | **BIO\_set\_conn\_int\_port**() |
| BIO\_C\_SET\_NBIO | *n* | **BIO\_set\_nbio**() |
| BIO\_CTRL\_GET\_CLOSE | 0 | BIO\_get\_close(3) |
| BIO\_CTRL\_RESET | 0 | BIO\_reset(3) |
| BIO\_CTRL\_SET\_CLOSE | *flag* | BIO\_set\_close(3) |

## RETURN VALUES

**BIO\_s\_connect**() returns the connect BIO method.

When called on a connect BIO object, BIO\_method\_type(3) returns the constant BIO\_TYPE\_CONNECT and BIO\_method\_name(3) returns a pointer to the static string "socket connect".

BIO\_get\_fd(3) returns the socket or -1 if the BIO has not been initialized.

**BIO\_set\_conn\_hostname**(), **BIO\_set\_conn\_port**(), **BIO\_set\_conn\_ip**(), and **BIO\_set\_conn\_int\_port**() always return 1.

**BIO\_get\_conn\_hostname**() returns the connected hostname or NULL if none is set.

**BIO\_get\_conn\_port**() returns a string representing the connected port or NULL if not set.

**BIO\_get\_conn\_ip**() returns a pointer to the connected IP address in binary form or all zeros if not set.

**BIO\_get\_conn\_int\_port**() returns the connected port or 0 if none was set.

**BIO\_set\_nbio**() always returns 1.

**BIO\_do\_connect**() returns 1 if the connection was successfully established and 0 or -1 if the connection failed.

## EXAMPLES

This example connects to a webserver on the local host and attempts to retrieve a page and copy the result to standard output.

```
BIO *cbio, *out;
int len;
char tmpbuf[1024];
ERR_load_crypto_strings();
cbio = BIO_new_connect("localhost:http");
out = BIO_new_fp(stdout, BIO_NOCLOSE);
if (BIO_do_connect(cbio) <= 0) {
	fprintf(stderr, "Error connecting to server\n");
	ERR_print_errors_fp(stderr);
	/* whatever ... */
}
BIO_puts(cbio, "GET / HTTP/1.0\n\n");
for(;;) {
	len = BIO_read(cbio, tmpbuf, 1024);
	if (len <= 0)
		break;
	BIO_write(out, tmpbuf, len);
}
BIO_free(cbio);
BIO_free(out);
```

## SEE ALSO

BIO\_new(3)

## HISTORY

**BIO\_s\_connect**(), **BIO\_new\_connect**(), **BIO\_set\_nbio**(), and **BIO\_do\_connect**() first appeared in SSLeay 0.8.0. **BIO\_set\_conn\_hostname**(), **BIO\_set\_conn\_port**(), **BIO\_set\_conn\_ip**(), **BIO\_set\_conn\_int\_port**(), **BIO\_get\_conn\_hostname**(), **BIO\_get\_conn\_port**(), **BIO\_get\_conn\_ip**(), and **BIO\_get\_conn\_int\_port**() first appeared in SSLeay 0.9.0. All these functions have been available since OpenBSD 2.4.
