public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
blob ab27704dc2dd5037f00d628359c5d385f349b07f 2634 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 
// 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

enum discard_method {
	DISCARD_NULL_BUF,
	DISCARD_ZERO_IOV,
	DISCARD_NULL_IOV,
	NUM_METHODS,
};

/* 127.0.0.1:DSTPORT */
static const struct sockaddr_in lo_dst = SOCKADDR_INIT(INADDR_LOOPBACK, DSTPORT);

static void test_discard(enum discard_method method)
{
	struct iovec zero_iov = { .iov_base = NULL, .iov_len = 0, };
	struct msghdr mh_zero = {
		.msg_iov = &zero_iov,
		.msg_iovlen = 1,
	};
	struct msghdr mh_null = {
		.msg_iov = NULL,
		.msg_iovlen = 0,
	};
	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);

	switch (method) {
	case DISCARD_NULL_BUF:
		/* cppcheck-suppress nullPointer */
		rc = recv(recv_s, NULL, 0, MSG_DONTWAIT);
		if (rc < 0)
			die("discarding recv(): %s\n", strerror(errno));
		break;

	case DISCARD_ZERO_IOV:
		rc = recvmsg(recv_s, &mh_zero, MSG_DONTWAIT);
		if (rc < 0)
			die("recvmsg() with zero-length buffer: %s\n",
			    strerror(errno));
		if (!((unsigned)mh_zero.msg_flags & MSG_TRUNC))
			die("Missing MSG_TRUNC flag\n");
		break;

	case DISCARD_NULL_IOV:
		rc = recvmsg(recv_s, &mh_null, MSG_DONTWAIT);
		if (rc < 0)
			die("recvmsg() with zero-length iov: %s\n",
			    strerror(errno));
		if (!((unsigned)mh_null.msg_flags & MSG_TRUNC))
			die("Missing MSG_TRUNC flag\n");
		break;

	default:
		die("Bad method\n");
	}

	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[])
{
	enum discard_method method;

	(void)argc;
	(void)argv;

	for (method = 0; method < NUM_METHODS; method++)
		test_discard(method);

	printf("Discarding datagrams with a 0-length receives seems to work\n");

	exit(0);
}

debug log:

solving ab27704d ...
found ab27704d in https://archives.passt.top/passt-dev/20240717045223.2309975-7-david@gibson.dropbear.id.au/
found f161e5c2 in https://passt.top/passt
preparing index
index prepared:
100644 f161e5c23fe4436fc51d3bf399616459624e59d2	doc/platform-requirements/recv-zero.c

applying [1/1] https://archives.passt.top/passt-dev/20240717045223.2309975-7-david@gibson.dropbear.id.au/
diff --git a/doc/platform-requirements/recv-zero.c b/doc/platform-requirements/recv-zero.c
index f161e5c2..ab27704d 100644

Checking patch doc/platform-requirements/recv-zero.c...
Applied patch doc/platform-requirements/recv-zero.c cleanly.

index at:
100644 ab27704dc2dd5037f00d628359c5d385f349b07f	doc/platform-requirements/recv-zero.c

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).