vici: Use non-blocking first read when receiving message during client on_read()
authorMartin Willi <martin@revosec.ch>
Wed, 9 Apr 2014 12:01:45 +0000 (14:01 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 7 May 2014 12:13:37 +0000 (14:13 +0200)
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

index 9b7ccb1..becd886 100644 (file)
@@ -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))
        {