aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-07-05 20:44:09 +1000
committerStefano Brivio <sbrivio@redhat.com>2024-07-05 15:26:48 +0200
commitec2691a12ed4c25b6fe3f542f45d13a5ef52db67 (patch)
tree87f1a858927fcd69788276e9be2355f5b67f0ba4
parent299c407501378134f31b6931645531ad0f700066 (diff)
downloadpasst-ec2691a12ed4c25b6fe3f542f45d13a5ef52db67.tar
passt-ec2691a12ed4c25b6fe3f542f45d13a5ef52db67.tar.gz
passt-ec2691a12ed4c25b6fe3f542f45d13a5ef52db67.tar.bz2
passt-ec2691a12ed4c25b6fe3f542f45d13a5ef52db67.tar.lz
passt-ec2691a12ed4c25b6fe3f542f45d13a5ef52db67.tar.xz
passt-ec2691a12ed4c25b6fe3f542f45d13a5ef52db67.tar.zst
passt-ec2691a12ed4c25b6fe3f542f45d13a5ef52db67.zip
doc: Test behaviour of zero length datagram recv()s
Add a test program verifying that we're able to discard datagrams from a socket without needing a big discard buffer, by using a zero length recv(). Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--doc/platform-requirements/.gitignore1
-rw-r--r--doc/platform-requirements/Makefile6
-rw-r--r--doc/platform-requirements/recv-zero.c74
3 files changed, 78 insertions, 3 deletions
diff --git a/doc/platform-requirements/.gitignore b/doc/platform-requirements/.gitignore
index c1baa98..555031d 100644
--- a/doc/platform-requirements/.gitignore
+++ b/doc/platform-requirements/.gitignore
@@ -1 +1,2 @@
/reuseaddr-priority
+/recv-zero
diff --git a/doc/platform-requirements/Makefile b/doc/platform-requirements/Makefile
index 6e1d966..82aaac2 100644
--- a/doc/platform-requirements/Makefile
+++ b/doc/platform-requirements/Makefile
@@ -3,8 +3,8 @@
# Copyright Red Hat
# Author: David Gibson <david@gibson.dropbear.id.au>
-TARGETS = reuseaddr-priority
-SRCS = reuseaddr-priority.c
+TARGETS = reuseaddr-priority recv-zero
+SRCS = reuseaddr-priority.c recv-zero.c
CFLAGS = -Wall
all: cppcheck clang-tidy $(TARGETS:%=check-%)
@@ -16,7 +16,7 @@ check-%: %
cppcheck:
cppcheck --std=c11 --error-exitcode=1 --enable=all --force \
- --check-level=exhaustive \
+ --check-level=exhaustive --inline-suppr \
--inconclusive --library=posix --quiet \
--suppress=missingIncludeSystem \
$(SRCS)
diff --git a/doc/platform-requirements/recv-zero.c b/doc/platform-requirements/recv-zero.c
new file mode 100644
index 0000000..f161e5c
--- /dev/null
+++ b/doc/platform-requirements/recv-zero.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/* recv-zero.c
+ *
+ * Verify that we're able to discard datagrams by recv()ing into a zero-length
+ * buffer.
+ *
+ * Copyright Red Hat
+ * Author: David Gibson <david@gibson.dropbear.id.au>
+ */
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "common.h"
+
+#define DSTPORT 13257U
+
+/* 127.0.0.1:DSTPORT */
+static const struct sockaddr_in lo_dst = SOCKADDR_INIT(INADDR_LOOPBACK, DSTPORT);
+
+static void test_discard(void)
+{
+ long token1, token2;
+ int recv_s, send_s;
+ ssize_t rc;
+
+ token1 = random();
+ token2 = random();
+
+ recv_s = sock_reuseaddr();
+ if (bind(recv_s, (struct sockaddr *)&lo_dst, sizeof(lo_dst)) < 0)
+ die("bind(): %s\n", strerror(errno));
+
+ send_s = sock_reuseaddr();
+ if (connect(send_s, (struct sockaddr *)&lo_dst, sizeof(lo_dst)) < 0)
+ die("connect(): %s\n", strerror(errno));
+
+ send_token(send_s, token1);
+ send_token(send_s, token2);
+
+ /* cppcheck-suppress nullPointer */
+ rc = recv(recv_s, NULL, 0, MSG_DONTWAIT);
+ if (rc < 0)
+ die("discarding recv(): %s\n", strerror(errno));
+
+ recv_token(recv_s, token2);
+
+ /* cppcheck-suppress nullPointer */
+ rc = recv(recv_s, NULL, 0, MSG_DONTWAIT);
+ if (rc < 0 && errno != EAGAIN)
+ die("redundant discarding recv(): %s\n", strerror(errno));
+ if (rc >= 0)
+ die("Unexpected receive: rc=%zd\n", rc);
+}
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+
+ test_discard();
+
+ printf("Discarding datagrams with a 0-length recv() seems to work\n");
+
+ exit(0);
+}