summaryrefslogtreecommitdiff
path: root/src/socket.c
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2016-08-22 13:21:07 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2016-08-22 17:08:43 +0200
commitfa2be566a18ad644e7832d64c8e06a7c17587c0b (patch)
treeed863ecd5eb9e79c0cf5d1e5f3ccc437393c94be /src/socket.c
parent19d163a3903c91b85db8540875c5c637bdf046c5 (diff)
downloadgnutls-fa2be566a18ad644e7832d64c8e06a7c17587c0b.tar.gz
gnutls-cli: added bufferring in starttls read of packets
Diffstat (limited to 'src/socket.c')
-rw-r--r--src/socket.c41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/socket.c b/src/socket.c
index a7b43ab764..77d7e8855c 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -161,14 +161,22 @@ static
ssize_t wait_for_text(socket_st * socket, const char *txt, unsigned txt_size)
{
char buf[1024];
- char *p;
+ char *pbuf, *p;
int ret;
fd_set read_fds;
struct timeval tv;
+ size_t left, got;
+
+ if (txt_size > sizeof(buf))
+ abort();
if (socket->verbose && txt != NULL)
fprintf(stderr, "starttls: waiting for: \"%.*s\"\n", txt_size, txt);
+ pbuf = buf;
+ left = sizeof(buf)-1;
+ got = 0;
+
do {
FD_ZERO(&read_fds);
FD_SET(socket->fd, &read_fds);
@@ -178,28 +186,37 @@ ssize_t wait_for_text(socket_st * socket, const char *txt, unsigned txt_size)
if (ret <= 0)
ret = -1;
else
- ret = recv(socket->fd, buf, sizeof(buf)-1, 0);
- if (ret == -1) {
- fprintf(stderr, "error receiving %s\n", txt);
+ ret = recv(socket->fd, pbuf, left, 0);
+ if (ret == -1 || ret == 0) {
+ int e = errno;
+ fprintf(stderr, "error receiving %s: %s\n", txt, strerror(e));
exit(2);
}
- buf[ret] = 0;
+ pbuf[ret] = 0;
if (txt == NULL)
break;
if (socket->verbose)
- fprintf(stderr, "starttls: received: %s\n", buf);
+ fprintf(stderr, "starttls: received: %s\n", pbuf);
+
+ pbuf += ret;
+ left -= ret;
+ got += ret;
- p = memmem(buf, ret, txt, txt_size);
- if (p != NULL && p != buf) {
- p--;
- if (*p == '\n')
+
+ /* check for text after a newline in buffer */
+ if (got > txt_size) {
+ p = memmem(buf, got, txt, txt_size);
+ if (p != NULL && p != buf) {
+ p--;
+ if (*p == '\n' || *p == '\r')
break;
+ }
}
- } while(ret < (int)txt_size || strncmp(buf, txt, txt_size) != 0);
+ } while(got < txt_size || strncmp(buf, txt, txt_size) != 0);
- return ret;
+ return got;
}
static void