+ TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
+ hasread, berlen));
+ /* move surplus buffer (or everything if we didn't get a BER rec.) */
+ if (hasread > berlen)
+ {
+ tomove = req = hasread - berlen;
+ rest = tomove % CS_TCPIP_BUFCHUNK;
+ if (rest)
+ req += CS_TCPIP_BUFCHUNK - rest;
+ if (!sp->altbuf)
+ {
+ if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
+ return -1;
+ } else if (sp->altsize < req)
+ if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
+ return -1;
+ TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
+ (unsigned) sp->altbuf));
+ memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
+ }
+ if (berlen < CS_TCPIP_BUFCHUNK - 1)
+ *(*buf + berlen) = '\0';
+ return berlen ? berlen : 1;
+}
+
+
+#if HAVE_OPENSSL_SSL_H
+/*
+ * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
+ * 0=connection closed.
+ */
+int ssl_get(COMSTACK h, char **buf, int *bufsize)
+{
+ tcpip_state *sp = (tcpip_state *)h->cprivate;
+ char *tmpc;
+ int tmpi, berlen, rest, req, tomove;
+ int hasread = 0, res;
+
+ TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
+ if (sp->altlen) /* switch buffers */
+ {
+ TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
+ (unsigned) sp->altbuf));
+ tmpc = *buf;
+ tmpi = *bufsize;
+ *buf = sp->altbuf;
+ *bufsize = sp->altsize;
+ hasread = sp->altlen;
+ sp->altlen = 0;
+ sp->altbuf = tmpc;
+ sp->altsize = tmpi;
+ }
+ h->io_pending = 0;
+ while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
+ {
+ if (!*bufsize)
+ {
+ if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK)))
+ return -1;
+ }
+ else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
+ if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
+ return -1;
+ res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
+ TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
+ if (res <= 0)
+ {
+ int ssl_err = SSL_get_error(sp->ssl, res);
+ if (ssl_err == SSL_ERROR_WANT_READ)
+ {
+ h->io_pending = CS_WANT_READ;
+ yaz_log (LOG_LOG, "SSL_read. want_read");
+ break;
+ }
+ if (ssl_err == SSL_ERROR_WANT_WRITE)
+ {
+ h->io_pending = CS_WANT_WRITE;
+ yaz_log (LOG_LOG, "SSL_read. want_write");
+ break;
+ }
+ if (res == 0)
+ return 0;
+ h->cerrno = CSERRORSSL;
+ return -1;
+ }
+ hasread += res;
+ }
+ TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",