-#if ENABLE_SSL
-/*
- * 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 (%p)\n", sp->altlen,
- 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)(*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;
-#if HAVE_GNUTLS_H
- res = gnutls_record_recv(sp->session, *buf + hasread,
- CS_TCPIP_BUFCHUNK);
- if (res == 0)
- {
- TRC(fprintf(stderr, "gnutls_record_recv returned 0\n"));
- return 0;
- }
- else if (res < 0)
- {
- if (ssl_check_error(h, sp, res))
- break;
- return -1;
- }
-#else
- 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)
- {
- if (ssl_check_error(h, sp, res))
- break;
- return -1;
- }
-#endif
- hasread += res;
- }
- 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(%p)\n", tomove,
- sp->altbuf));
- memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
- }
- if (berlen < CS_TCPIP_BUFCHUNK - 1)
- *(*buf + berlen) = '\0';
- return berlen ? berlen : 1;
-}
-#endif
-