2000-04-13 09:19:23 +00:00
|
|
|
/*
|
2000-07-23 13:02:27 +00:00
|
|
|
* Read a line from a descriptor with fgets
|
2000-06-25 18:55:56 +00:00
|
|
|
*
|
2000-06-29 09:21:31 +00:00
|
|
|
* $Id$
|
2000-06-29 11:48:49 +00:00
|
|
|
*
|
|
|
|
*/
|
2000-04-13 09:19:23 +00:00
|
|
|
|
|
|
|
#include "echoping.h"
|
|
|
|
|
|
|
|
int
|
2000-07-23 13:02:27 +00:00
|
|
|
readline (fs, ptr, maxlen, ln)
|
|
|
|
FILE *fs;
|
2000-04-13 09:19:23 +00:00
|
|
|
char *ptr;
|
|
|
|
int maxlen;
|
|
|
|
unsigned short ln;
|
|
|
|
{
|
2000-07-23 13:02:27 +00:00
|
|
|
int n = 1;
|
|
|
|
char *rc;
|
2001-01-21 21:04:59 +00:00
|
|
|
int r;
|
2000-09-27 05:38:29 +00:00
|
|
|
|
2001-01-23 20:03:35 +00:00
|
|
|
/* Reading with fgets or fread instead of read
|
|
|
|
one-character-at-a-time is more than ten times faster, on a local
|
|
|
|
server. */
|
2000-09-27 05:38:29 +00:00
|
|
|
if (ln)
|
|
|
|
{
|
|
|
|
rc = fgets (ptr, maxlen + 1, fs);
|
|
|
|
if (rc == NULL)
|
|
|
|
{
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
n = strlen (rc);
|
|
|
|
return n;
|
2000-04-13 09:19:23 +00:00
|
|
|
}
|
2000-09-27 05:38:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while (n < maxlen)
|
|
|
|
{
|
2001-01-22 15:38:44 +00:00
|
|
|
r = fread (ptr, 1, maxlen, fs);
|
2001-01-21 21:04:59 +00:00
|
|
|
if (r == 0)
|
2000-09-27 05:38:29 +00:00
|
|
|
{
|
|
|
|
if (timeout_flag)
|
|
|
|
return n;
|
|
|
|
if (n == 1)
|
|
|
|
return (0); /* EOF, no data read */
|
|
|
|
else
|
|
|
|
break; /* EOF, some data was read */
|
|
|
|
}
|
2001-01-21 21:04:59 +00:00
|
|
|
n = n + r;
|
2000-09-27 05:38:29 +00:00
|
|
|
}
|
2000-07-23 13:02:27 +00:00
|
|
|
}
|
2000-04-13 09:19:23 +00:00
|
|
|
return (n);
|
|
|
|
}
|
2000-11-04 12:36:37 +00:00
|
|
|
|
|
|
|
#ifdef OPENSSL
|
|
|
|
|
|
|
|
char SSL_buffer[MAXTOREAD];
|
|
|
|
int buf_ptr;
|
|
|
|
int buf_end;
|
|
|
|
|
|
|
|
int
|
|
|
|
SSL_readline (sslh, ptr, maxlen, ln)
|
|
|
|
SSL *sslh;
|
|
|
|
char *ptr;
|
|
|
|
int maxlen;
|
|
|
|
unsigned short ln;
|
|
|
|
{
|
|
|
|
int rc = 0;
|
2001-01-26 10:01:49 +00:00
|
|
|
int n = 0;
|
2000-11-04 12:36:37 +00:00
|
|
|
int i, oi;
|
|
|
|
if (ln)
|
|
|
|
{
|
|
|
|
/* Empty buffer */
|
2001-01-25 15:25:13 +00:00
|
|
|
if (buf_end == 0)
|
|
|
|
{
|
|
|
|
rc = SSL_read (sslh, SSL_buffer, maxlen);
|
2003-11-05 12:14:01 +00:00
|
|
|
if (rc == -1)
|
2001-03-11 13:38:45 +00:00
|
|
|
return rc;
|
2001-01-25 15:25:13 +00:00
|
|
|
buf_end = rc;
|
2001-01-29 09:26:11 +00:00
|
|
|
buf_ptr = 0;
|
2001-01-25 15:25:13 +00:00
|
|
|
}
|
2000-11-04 12:36:37 +00:00
|
|
|
/* No more data in the buffer */
|
2001-01-25 15:25:13 +00:00
|
|
|
else if (buf_ptr == buf_end)
|
|
|
|
{
|
|
|
|
buf_ptr = 0;
|
|
|
|
rc = SSL_read (sslh, SSL_buffer, maxlen);
|
2003-11-05 12:14:01 +00:00
|
|
|
if (rc == -1)
|
2001-03-11 13:38:45 +00:00
|
|
|
return rc;
|
2001-01-25 15:25:13 +00:00
|
|
|
buf_end = rc;
|
|
|
|
}
|
2003-11-05 12:14:01 +00:00
|
|
|
else if (SSL_buffer[buf_end] != '\n')
|
|
|
|
{
|
|
|
|
/* We have a probleme here is the first SSL_read sent back
|
|
|
|
a text not finished by a \n. See www.SSL.de for an
|
|
|
|
example. We get more data. See bug #230384 */
|
|
|
|
rc = SSL_read (sslh, SSL_buffer + buf_end, maxlen);
|
|
|
|
if (rc == -1)
|
2001-03-11 13:38:45 +00:00
|
|
|
return rc;
|
|
|
|
buf_end = buf_end + rc;
|
2003-11-05 12:14:01 +00:00
|
|
|
}
|
|
|
|
for (oi = buf_ptr, i = buf_ptr;
|
|
|
|
i <= buf_end && SSL_buffer[i] != '\n'; i++)
|
2001-01-25 15:25:13 +00:00
|
|
|
{
|
|
|
|
*ptr++ = SSL_buffer[i];
|
|
|
|
buf_ptr++;
|
|
|
|
}
|
|
|
|
if (SSL_buffer[i] == '\n')
|
2000-11-04 12:36:37 +00:00
|
|
|
buf_ptr++;
|
|
|
|
*ptr = '\0';
|
2001-03-11 13:38:45 +00:00
|
|
|
/* if (ln)
|
2003-11-05 12:14:01 +00:00
|
|
|
printf ("SSL_readline returns %d (%s)\n", i - oi, SSL_buffer); */
|
2001-01-25 15:25:13 +00:00
|
|
|
return (i - oi);
|
2000-11-04 12:36:37 +00:00
|
|
|
}
|
2001-01-25 15:25:13 +00:00
|
|
|
else
|
|
|
|
{
|
2001-01-26 10:01:49 +00:00
|
|
|
/* OpenSSL reads at most 4096 characters */
|
2003-11-05 12:14:01 +00:00
|
|
|
rc = 1; /* Just to avoid exiting too soon */
|
|
|
|
while (n < maxlen && rc != 0)
|
|
|
|
{
|
|
|
|
if ((buf_end == 0) || (buf_ptr > buf_end))
|
|
|
|
{
|
|
|
|
rc = SSL_read (sslh, ptr, maxlen);
|
|
|
|
buf_end = 0;
|
|
|
|
buf_ptr = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = buf_ptr; i < maxlen && i <= buf_end; i++)
|
|
|
|
{
|
|
|
|
*ptr++ = SSL_buffer[i];
|
|
|
|
rc++;
|
|
|
|
}
|
|
|
|
buf_ptr = i;
|
|
|
|
}
|
|
|
|
n = n + rc;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef GNUTLS
|
|
|
|
|
|
|
|
char TLS_buffer[MAXTOREAD];
|
|
|
|
int buf_ptr;
|
|
|
|
int buf_end;
|
|
|
|
|
|
|
|
int
|
|
|
|
TLS_readline (session, ptr, maxlen, ln)
|
|
|
|
gnutls_session session;
|
|
|
|
char *ptr;
|
|
|
|
int maxlen;
|
|
|
|
unsigned short ln;
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int n = 0;
|
|
|
|
int i, oi;
|
|
|
|
if (ln)
|
|
|
|
{
|
|
|
|
/* Empty buffer */
|
|
|
|
if (buf_end == 0)
|
|
|
|
{
|
|
|
|
rc = gnutls_record_recv (session, TLS_buffer, maxlen);
|
|
|
|
if (rc == -1)
|
|
|
|
return rc;
|
|
|
|
buf_end = rc;
|
|
|
|
buf_ptr = 0;
|
|
|
|
}
|
|
|
|
/* No more data in the buffer */
|
|
|
|
else if (buf_ptr == buf_end)
|
|
|
|
{
|
|
|
|
buf_ptr = 0;
|
|
|
|
rc = gnutls_record_recv (session, TLS_buffer, maxlen);
|
|
|
|
if (rc == -1)
|
|
|
|
return rc;
|
|
|
|
buf_end = rc;
|
|
|
|
}
|
|
|
|
else if (TLS_buffer[buf_end] != '\n')
|
|
|
|
{
|
|
|
|
rc = gnutls_record_recv (session, TLS_buffer + buf_end, maxlen);
|
|
|
|
if (rc == -1)
|
|
|
|
return rc;
|
|
|
|
buf_end = buf_end + rc;
|
|
|
|
}
|
|
|
|
for (oi = buf_ptr, i = buf_ptr;
|
|
|
|
i <= buf_end && TLS_buffer[i] != '\n'; i++)
|
|
|
|
{
|
|
|
|
*ptr++ = TLS_buffer[i];
|
|
|
|
buf_ptr++;
|
|
|
|
}
|
|
|
|
if (TLS_buffer[i] == '\n')
|
|
|
|
buf_ptr++;
|
|
|
|
*ptr = '\0';
|
|
|
|
/* printf ("DEBUG: TLS_readline returns %d (%s)\n", i - oi, TLS_buffer); */
|
|
|
|
return (i - oi);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = 1; /* Just to avoid exiting too soon */
|
|
|
|
while (n < maxlen && rc > 0)
|
|
|
|
{
|
|
|
|
if ((buf_end == 0) || (buf_ptr > buf_end))
|
|
|
|
{
|
|
|
|
rc = gnutls_record_recv (session, ptr, maxlen);
|
|
|
|
buf_end = 0;
|
|
|
|
buf_ptr = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = buf_ptr; i < maxlen && i <= buf_end; i++)
|
|
|
|
{
|
|
|
|
*ptr++ = TLS_buffer[i];
|
|
|
|
rc++;
|
|
|
|
/* printf ("DEBUG: Now %d chars read\n", rc); */
|
|
|
|
}
|
|
|
|
buf_ptr = i;
|
|
|
|
}
|
|
|
|
if (rc > 0)
|
|
|
|
n = n + rc;
|
|
|
|
/* printf ("DEBUG: Now %d chars to send\n", n); */
|
|
|
|
}
|
2001-01-26 10:01:49 +00:00
|
|
|
return n;
|
2000-11-04 12:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|