+/*
+ * client_write_ssl() Send binary data to the client encrypted.
+ */
+void client_write_ssl(char *buf, int nbytes)
+{
+ int retval;
+ int nremain;
+ char junk[1];
+
+ nremain = nbytes;
+
+ while (nremain > 0) {
+ if (SSL_want_write(THREADSSL)) {
+ if ((SSL_read(THREADSSL, junk, 0)) < 1) {
+ lprintf(9, "SSL_read in client_write: %s\n", ERR_reason_error_string(ERR_get_error()));
+ }
+ }
+ retval =
+ SSL_write(THREADSSL, &buf[nbytes - nremain], nremain);
+ if (retval < 1) {
+ long errval;
+
+ errval = SSL_get_error(THREADSSL, retval);
+ if (errval == SSL_ERROR_WANT_READ ||
+ errval == SSL_ERROR_WANT_WRITE) {
+ sleep(1);
+ continue;
+ }
+ lprintf(9, "SSL_write got error %ld, ret %d\n", errval, retval);
+ if (retval == -1)
+ lprintf(9, "errno is %d\n", errno);
+ endtls();
+ client_write(&buf[nbytes - nremain], nremain);
+ return;
+ }
+ nremain -= retval;
+ }
+}
+
+
+/*
+ * client_read_ssl() - read data from the encrypted layer.
+ */
+int client_read_ssl(char *buf, int bytes, int timeout)
+{
+#if 0
+ fd_set rfds;
+ struct timeval tv;
+ int retval;
+ int s;
+#endif
+ int len, rlen;
+ char junk[1];
+
+ len = 0;
+ while (len < bytes) {
+#if 0
+ /*
+ * This code is disabled because we don't need it when
+ * using blocking reads (which we are). -IO
+ */
+ FD_ZERO(&rfds);
+ s = BIO_get_fd(THREADSSL->rbio, NULL);
+ FD_SET(s, &rfds);
+ tv.tv_sec = timeout;
+ tv.tv_usec = 0;
+
+ retval = select(s + 1, &rfds, NULL, NULL, &tv);
+
+ if (FD_ISSET(s, &rfds) == 0) {
+ return (0);
+ }
+
+#endif
+ if (SSL_want_read(THREADSSL)) {
+ if ((SSL_write(THREADSSL, junk, 0)) < 1) {
+ lprintf(9, "SSL_write in client_read: %s\n", ERR_reason_error_string(ERR_get_error()));
+ }
+ }
+ rlen = SSL_read(THREADSSL, &buf[len], bytes - len);
+ if (rlen < 1) {
+ long errval;
+
+ errval = SSL_get_error(THREADSSL, rlen);
+ if (errval == SSL_ERROR_WANT_READ ||
+ errval == SSL_ERROR_WANT_WRITE) {
+ sleep(1);
+ continue;
+ }
+ lprintf(9, "SSL_read got error %ld\n", errval);
+ endtls();
+ return (client_read_to
+ (&buf[len], bytes - len, timeout));
+ }
+ len += rlen;
+ }
+ return (1);
+}
+
+