#include #include #include #include void set_nonblocking(SSL * ssl) { int fd, flags; /* SSL_get_rfd returns -1 on error */ if( (fd = SSL_get_rfd(ssl)) ) { flags = fcntl(fd, F_GETFL); flags |= O_NONBLOCK; fcntl(fd, F_SETFL, flags); } /* SSL_get_wfd returns -1 on error */ if( (fd = SSL_get_wfd(ssl)) ) { flags = fcntl(fd, F_GETFL); flags |= O_NONBLOCK; fcntl(fd, F_SETFL, flags); } } void set_blocking(SSL * ssl) { int fd, flags; /* SSL_get_rfd returns -1 on error */ if( (fd = SSL_get_rfd(ssl)) ) { flags = fcntl(fd, F_GETFL); flags &= ~O_NONBLOCK; fcntl(fd, F_SETFL, flags); } /* SSL_get_wfd returns -1 on error */ if( (fd = SSL_get_wfd(ssl)) ) { flags = fcntl(fd, F_GETFL); flags &= ~O_NONBLOCK; fcntl(fd, F_SETFL, flags); } } void check_availability(SSL *a, unsigned int *read_a, unsigned int *write_a, SSL *b, unsigned int *read_b, unsigned int *write_b) { int a_rfd, a_wfd, b_rfd, b_wfd; fd_set read_fds, write_fds; FD_ZERO(&read_fds); FD_ZERO(&write_fds); if( (a_rfd = SSL_get_rfd(a)) ) FD_SET(a_rfd, &read_fds); if( (a_wfd = SSL_get_wfd(a)) ) FD_SET(a_wfd, &write_fds); if( (b_rfd = SSL_get_rfd(b)) ) FD_SET(b_rfd, &read_fds); if( (b_wfd = SSL_get_wfd(b)) ) FD_SET(b_wfd, &write_fds); select(2, &read_fds, &write_fds, 0, 0); if(a_rfd && FD_ISSET(a_rfd, &read_fds)) *read_a = 1; else *read_a = 0; if(a_wfd && FD_ISSET(a_wfd, &write_fds)) *write_a = 1; else *write_a = 0; if(b_rfd && FD_ISSET(b_rfd, &read_fds)) *read_b = 1; else *read_b = 0; if(b_wfd && FD_ISSET(b_wfd, &write_fds)) *write_b = 1; else *write_b = 0; }