diff options
author | Lennart Poettering <lennart@poettering.net> | 2016-02-15 22:50:01 +0100 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2016-05-25 11:56:39 -0400 |
commit | 2eb25bc571d6b5babe50ab116c2619e17de39ec9 (patch) | |
tree | 3df98494a97998a379936551a08456ea86241e2e /src/basic/socket-util.c | |
parent | 95adafc428b5b4be0ddd4d43a7b96658390388bc (diff) |
networkd: FIONREAD is not reliable on some sockets
Fixes: #2457
Diffstat (limited to 'src/basic/socket-util.c')
-rw-r--r-- | src/basic/socket-util.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 49e5f5b125..58512686e3 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -936,3 +936,37 @@ int receive_one_fd(int transport_fd, int flags) { return *(int*) CMSG_DATA(found); } + +ssize_t next_datagram_size_fd(int fd) { + ssize_t l; + int k; + + /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will + * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD dosn't + * do. This difference is actually of major importance as we need to be sure that the size returned here + * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of + * the wrong size. */ + + l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC); + if (l < 0) { + if (errno == EOPNOTSUPP) + goto fallback; + + return -errno; + } + if (l == 0) + goto fallback; + + return l; + +fallback: + k = 0; + + /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD + * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */ + + if (ioctl(fd, FIONREAD, &k) < 0) + return -errno; + + return (ssize_t) k; +} |