I am using the Openssl Bio API to start an SSL connection:

SSL* ssl{nullptr};
SSL_CTX* ctx = SSL_CTX_new(SSLv23_client_method());
BIO* bio = BIO_new_ssl_connect(ctx);

BIO_get_ssl(bio, &ssl);
SSL_set_tlsext_host_name(ssl, host);

BIO_set_conn_hostname(bio, host_and_port);
int res = BIO_do_connect(bio);

This has worked fine.

Now I want to set the socket to use a specific Linux interface using setsockopt() with SO_BINDTODEVICE.

To get the raw socket file desciptor I use BIO_get_fd():

int sock = -1;
BIO_get_fd(bio, &sock);

setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, interface_name, strlen(interface_name));

// Here sock = -1
// Here sock > 0

The problem is the socket file descriptor is invalid if called before BIO_do_connect(). The file descriptor is valid after BIO_do_connect()
and presumably I need to set the interface before openssl calls ::connect()?

Is it still possible to set the socket to use the interface?