Hi All,
I am trying to write a non-blocking client. Based on previous threads
that I have read on this forum, I have adopted the following strategy.
1) Get a new BIO object.
2) I set the BIO to be non-blocking. As I understood from the manual,
I set it to non-blocking just before calling BIO_do_connect
3) I try to connect using BIO_do_connect.
4) If the function returns < 0, I check if connection setup is
ongoing. If so, I will wait on that socket for a write condition to be
true. I get the socket descriptor by calling BIO_get_fd.
5) When select returns, I want to call getsockopt to make sure that
the connection actually was completed.
I am stuck at this point.
getsockopt fails with the message: "Invalid argument" which is EINVAL.
When I checked the man pages on my CentOS machine, EINVAL is specified
for setsockopt, but not for getsockopt. I just have no idea what to do
next. What argument is invalid? The only thing that I am suspicious
about is the socket descriptor. Is it ok? optval and optlen are both
int.
Please help!
Also, is this the correct way to write a non-blocking client?
Here's the code:
SSL_library_init();
ERR_load_BIO_strings();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
ctx = SSL_CTX_new(SSLv23_client_method());
SSL_CTX_load_verify_locations(ctx, "mycert.pem", NULL);
bio = BIO_new_ssl_connect(ctx);
BIO_get_ssl(bio, & ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(bio, "www.dcu.org:443");
BIO_set_nbio(bio, 1);
if(BIO_do_connect(bio) <= 0) {
if (errno == EINPROGRESS) {
sock = BIO_get_fd(bio, &sock);
FD_ZERO(&write_fd);
FD_SET(sock, &write_fd);
num_fd = sock;
select(num_fd+1, NULL, &write_fd, NULL, NULL);
for (i=0; i<=num_fd; i++) {
if (FD_ISSET(i, &write_fd)) {
/* Make sure that the
connection was indded made */
res = getsockopt(sock,
SOL_SOCKET, SO_ERROR, &optval, &optlen);
if (res < 0) {
printf("getsockopt failed\n");
perror(NULL);
return 0;
}
if (SO_ERROR == 0) {
/* Success */
printf("Succeeded\n");
break;
}
else {
printf ("Connection
not established.\n");
perror(NULL);
return 0;
}
}
}
}
else {
printf("Different error\n");
}
}
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [email protected]
Automated List Manager [email protected]