简体   繁体   中英

Set connection timeout for SSL_read in OpenSSL in C++

I am developing a linux application which communicates with OpenSSL. I am currently running some robustness tests and one of the is giving me a hard time.

I plug out the Ethernet cable when my program is downloading a big file and I wish that it stops after 30seconds for example. But it never stop.

I use SSL_read and this is where it blocks :

count = SSL_read(ssl, buffer, BUFSIZE);

Is it possible to set a timeout to SSL_read ?

I have tried SSL_CTX_set_timeout() but it is not working. I have also seen that it was maybe possible to use select() but I don't understand how to use it with SSL_read()

You can do that in the same way as you do it with normal sockets. Basically you set the timeval on a socket and in ssl code the SSL_read will return -1 when the time set in timeval passes.

int fd, new_fd;
struct timeval tv;
char buffer[1024];

// ...

tv.tv_sec = 5; // 5 seconds
tv.tv_usec = 0;
setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);

// ...

// this will return -1 if nothing is received after 5 seconds
SSL_read(ssl, buffer, sizeof buffer); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM