From e00ce378fa6083a162bad505a35719fc25494573 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Wed, 9 Apr 2014 14:01:45 +0200 Subject: [PATCH] vici: Use non-blocking first read when receiving message during client on_read() As select() and finally the watcher may signal an FD even if it does not actually have data, we must make a non-block read to avoid hanging in the read callback. --- src/libcharon/plugins/vici/libvici.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libcharon/plugins/vici/libvici.c b/src/libcharon/plugins/vici/libvici.c index 9b7ccb13e..becd88671 100644 --- a/src/libcharon/plugins/vici/libvici.c +++ b/src/libcharon/plugins/vici/libvici.c @@ -199,11 +199,25 @@ CALLBACK(on_read, bool, { u_int16_t len; u_int8_t op; + ssize_t hlen; - if (!stream->read_all(stream, &len, sizeof(len))) + hlen = stream->read(stream, &len, sizeof(len), FALSE); + if (hlen <= 0) { + if (errno == EWOULDBLOCK) + { + return TRUE; + } return read_error(conn, errno); } + if (hlen < sizeof(len)) + { + if (!stream->read_all(stream, ((void*)&len) + hlen, sizeof(len) - hlen)) + { + return read_error(conn, errno); + } + } + len = ntohs(len); if (len-- < sizeof(op)) {