aboutgitcodebugslistschat
path: root/doc
diff options
context:
space:
mode:
Diffstat (limited to 'doc')
-rw-r--r--doc/migration/.gitignore2
-rw-r--r--doc/migration/Makefile20
-rw-r--r--doc/migration/README51
-rw-r--r--doc/migration/source.c92
-rw-r--r--doc/migration/target.c102
-rw-r--r--doc/platform-requirements/.gitignore2
-rw-r--r--doc/platform-requirements/Makefile8
-rw-r--r--doc/platform-requirements/common.h1
-rw-r--r--doc/platform-requirements/listen-vs-repair.c128
-rw-r--r--doc/platform-requirements/reuseaddr-priority.c6
-rw-r--r--doc/platform-requirements/tcp-close-rst.c204
11 files changed, 611 insertions, 5 deletions
diff --git a/doc/migration/.gitignore b/doc/migration/.gitignore
new file mode 100644
index 0000000..59cb765
--- /dev/null
+++ b/doc/migration/.gitignore
@@ -0,0 +1,2 @@
+/source
+/target
diff --git a/doc/migration/Makefile b/doc/migration/Makefile
new file mode 100644
index 0000000..04f6891
--- /dev/null
+++ b/doc/migration/Makefile
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+# PASST - Plug A Simple Socket Transport
+# for qemu/UNIX domain socket mode
+#
+# PASTA - Pack A Subtle Tap Abstraction
+# for network namespace/tap device mode
+#
+# Copyright (c) 2025 Red Hat GmbH
+# Author: Stefano Brivio <sbrivio@redhat.com>
+
+TARGETS = source target
+CFLAGS = -Wall -Wextra -pedantic
+
+all: $(TARGETS)
+
+$(TARGETS): %: %.c
+
+clean:
+ rm -f $(TARGETS)
diff --git a/doc/migration/README b/doc/migration/README
new file mode 100644
index 0000000..375603b
--- /dev/null
+++ b/doc/migration/README
@@ -0,0 +1,51 @@
+<!---
+SPDX-License-Identifier: GPL-2.0-or-later
+Copyright (c) 2025 Red Hat GmbH
+Author: Stefano Brivio <sbrivio@redhat.com>
+-->
+
+Migration
+=========
+
+These test programs show a migration of a TCP connection from one process to
+another using the TCP_REPAIR socket option.
+
+The two processes are a mock of the matching implementation in passt(1), and run
+unprivileged, so they rely on the passt-repair helper to connect to them and set
+or clear TCP_REPAIR on the connection socket, transferred to the helper using
+SCM_RIGHTS.
+
+The passt-repair helper needs to have the CAP_NET_ADMIN capability, or run as
+root.
+
+Example of usage
+----------------
+
+* Start the test server
+
+ $ nc -l 9999
+
+* Start the source side of the TCP client (mock of the source instance of passt)
+
+ $ ./source 127.0.0.1 9999 9998 /tmp/repair.sock
+
+* The client sends a test string, and waits for a connection from passt-repair
+
+ # passt-repair /tmp/repair.sock
+
+* The socket is now in repair mode, and `source` dumps sequences, then exits
+
+ sending sequence: 3244673313
+ receiving sequence: 2250449386
+
+* Continue the connection on the target side, restarting from those sequences
+
+ $ ./target 127.0.0.1 9999 9998 /tmp/repair.sock 3244673313 2250449386
+
+* The target side now waits for a connection from passt-repair
+
+ # passt-repair /tmp/repair.sock
+
+* The target side asks passt-repair to switch the socket to repair mode, sets up
+ the TCP sequences, then asks passt-repair to clear repair mode, and sends a
+ test string to the server
diff --git a/doc/migration/source.c b/doc/migration/source.c
new file mode 100644
index 0000000..d44ebf1
--- /dev/null
+++ b/doc/migration/source.c
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/* PASST - Plug A Simple Socket Transport
+ * for qemu/UNIX domain socket mode
+ *
+ * PASTA - Pack A Subtle Tap Abstraction
+ * for network namespace/tap device mode
+ *
+ * doc/migration/source.c - Mock of TCP migration source, use with passt-repair
+ *
+ * Copyright (c) 2025 Red Hat GmbH
+ * Author: Stefano Brivio <sbrivio@redhat.com>
+ */
+
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <netinet/tcp.h>
+
+int main(int argc, char **argv)
+{
+ struct sockaddr_in a = { AF_INET, htons(atoi(argv[3])), { 0 }, { 0 } };
+ struct addrinfo hints = { 0, AF_UNSPEC, SOCK_STREAM, 0, 0,
+ NULL, NULL, NULL };
+ struct sockaddr_un a_helper = { AF_UNIX, { 0 } };
+ int seq, s, s_helper;
+ int8_t cmd;
+ struct iovec iov = { &cmd, sizeof(cmd) };
+ char buf[CMSG_SPACE(sizeof(int))];
+ struct msghdr msg = { NULL, 0, &iov, 1, buf, sizeof(buf), 0 };
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ socklen_t seqlen = sizeof(int);
+ struct addrinfo *r;
+
+ (void)argc;
+
+ if (argc != 5) {
+ fprintf(stderr, "%s DST_ADDR DST_PORT SRC_PORT HELPER_PATH\n",
+ argv[0]);
+ return -1;
+ }
+
+ strcpy(a_helper.sun_path, argv[4]);
+ getaddrinfo(argv[1], argv[2], &hints, &r);
+
+ /* Connect socket to server and send some data */
+ s = socket(r->ai_family, SOCK_STREAM, IPPROTO_TCP);
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &((int){ 1 }), sizeof(int));
+ bind(s, (struct sockaddr *)&a, sizeof(a));
+ connect(s, r->ai_addr, r->ai_addrlen);
+ send(s, "before migration\n", sizeof("before migration\n"), 0);
+
+ /* Wait for helper */
+ s_helper = socket(AF_UNIX, SOCK_STREAM, 0);
+ unlink(a_helper.sun_path);
+ bind(s_helper, (struct sockaddr *)&a_helper, sizeof(a_helper));
+ listen(s_helper, 1);
+ s_helper = accept(s_helper, NULL, NULL);
+
+ /* Set up message for helper, with socket */
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ memcpy(CMSG_DATA(cmsg), &s, sizeof(s));
+
+ /* Send command to helper: turn repair mode on, wait for reply */
+ cmd = TCP_REPAIR_ON;
+ sendmsg(s_helper, &msg, 0);
+ recv(s_helper, &((int8_t){ 0 }), 1, 0);
+
+ /* Terminate helper */
+ close(s_helper);
+
+ /* Get sending sequence */
+ seq = TCP_SEND_QUEUE;
+ setsockopt(s, SOL_TCP, TCP_REPAIR_QUEUE, &seq, sizeof(seq));
+ getsockopt(s, SOL_TCP, TCP_QUEUE_SEQ, &seq, &seqlen);
+ fprintf(stdout, "%u ", seq);
+
+ /* Get receiving sequence */
+ seq = TCP_RECV_QUEUE;
+ setsockopt(s, SOL_TCP, TCP_REPAIR_QUEUE, &seq, sizeof(seq));
+ getsockopt(s, SOL_TCP, TCP_QUEUE_SEQ, &seq, &seqlen);
+ fprintf(stdout, "%u\n", seq);
+}
diff --git a/doc/migration/target.c b/doc/migration/target.c
new file mode 100644
index 0000000..f7d3108
--- /dev/null
+++ b/doc/migration/target.c
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/* PASST - Plug A Simple Socket Transport
+ * for qemu/UNIX domain socket mode
+ *
+ * PASTA - Pack A Subtle Tap Abstraction
+ * for network namespace/tap device mode
+ *
+ * doc/migration/target.c - Mock of TCP migration target, use with passt-repair
+ *
+ * Copyright (c) 2025 Red Hat GmbH
+ * Author: Stefano Brivio <sbrivio@redhat.com>
+ */
+
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <netinet/tcp.h>
+
+int main(int argc, char **argv)
+{
+ struct sockaddr_in a = { AF_INET, htons(atoi(argv[3])), { 0 }, { 0 } };
+ struct addrinfo hints = { 0, AF_UNSPEC, SOCK_STREAM, 0, 0,
+ NULL, NULL, NULL };
+ struct sockaddr_un a_helper = { AF_UNIX, { 0 } };
+ int s, s_helper, seq;
+ int8_t cmd;
+ struct iovec iov = { &cmd, sizeof(cmd) };
+ char buf[CMSG_SPACE(sizeof(int))];
+ struct msghdr msg = { NULL, 0, &iov, 1, buf, sizeof(buf), 0 };
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ struct addrinfo *r;
+
+ (void)argc;
+
+ strcpy(a_helper.sun_path, argv[4]);
+ getaddrinfo(argv[1], argv[2], &hints, &r);
+
+ if (argc != 7) {
+ fprintf(stderr,
+ "%s DST_ADDR DST_PORT SRC_PORT HELPER_PATH SSEQ RSEQ\n",
+ argv[0]);
+ return -1;
+ }
+
+ /* Prepare socket, bind to source port */
+ s = socket(r->ai_family, SOCK_STREAM, IPPROTO_TCP);
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &((int){ 1 }), sizeof(int));
+ bind(s, (struct sockaddr *)&a, sizeof(a));
+
+ /* Wait for helper */
+ s_helper = socket(AF_UNIX, SOCK_STREAM, 0);
+ unlink(a_helper.sun_path);
+ bind(s_helper, (struct sockaddr *)&a_helper, sizeof(a_helper));
+ listen(s_helper, 1);
+ s_helper = accept(s_helper, NULL, NULL);
+
+ /* Set up message for helper, with socket */
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+ memcpy(CMSG_DATA(cmsg), &s, sizeof(s));
+
+ /* Send command to helper: turn repair mode on, wait for reply */
+ cmd = TCP_REPAIR_ON;
+ sendmsg(s_helper, &msg, 0);
+ recv(s_helper, &((int){ 0 }), 1, 0);
+
+ /* Set sending sequence */
+ seq = TCP_SEND_QUEUE;
+ setsockopt(s, SOL_TCP, TCP_REPAIR_QUEUE, &seq, sizeof(seq));
+ seq = atoi(argv[5]);
+ setsockopt(s, SOL_TCP, TCP_QUEUE_SEQ, &seq, sizeof(seq));
+
+ /* Set receiving sequence */
+ seq = TCP_RECV_QUEUE;
+ setsockopt(s, SOL_TCP, TCP_REPAIR_QUEUE, &seq, sizeof(seq));
+ seq = atoi(argv[6]);
+ setsockopt(s, SOL_TCP, TCP_QUEUE_SEQ, &seq, sizeof(seq));
+
+ /* Connect setting kernel state only, without actual SYN / handshake */
+ connect(s, r->ai_addr, r->ai_addrlen);
+
+ /* Send command to helper: turn repair mode off, wait for reply */
+ cmd = TCP_REPAIR_OFF;
+ sendmsg(s_helper, &msg, 0);
+
+ recv(s_helper, &((int8_t){ 0 }), 1, 0);
+
+ /* Terminate helper */
+ close(s_helper);
+
+ /* Send some more data */
+ send(s, "after migration\n", sizeof("after migration\n"), 0);
+}
diff --git a/doc/platform-requirements/.gitignore b/doc/platform-requirements/.gitignore
index 3b5a10a..b2a0069 100644
--- a/doc/platform-requirements/.gitignore
+++ b/doc/platform-requirements/.gitignore
@@ -1,3 +1,5 @@
+/listen-vs-repair
/reuseaddr-priority
/recv-zero
+/tcp-close-rst
/udp-close-dup
diff --git a/doc/platform-requirements/Makefile b/doc/platform-requirements/Makefile
index 6a7d374..204341b 100644
--- a/doc/platform-requirements/Makefile
+++ b/doc/platform-requirements/Makefile
@@ -3,8 +3,10 @@
# Copyright Red Hat
# Author: David Gibson <david@gibson.dropbear.id.au>
-TARGETS = reuseaddr-priority recv-zero udp-close-dup
-SRCS = reuseaddr-priority.c recv-zero.c udp-close-dup.c
+TARGETS = reuseaddr-priority recv-zero udp-close-dup listen-vs-repair \
+ tcp-close-rst
+SRCS = reuseaddr-priority.c recv-zero.c udp-close-dup.c listen-vs-repair.c \
+ tcp-close-rst.c
CFLAGS = -Wall
all: cppcheck clang-tidy $(TARGETS:%=check-%)
@@ -25,6 +27,7 @@ clang-tidy:
clang-tidy --checks=*,\
-altera-id-dependent-backward-branch,\
-altera-unroll-loops,\
+ -android-cloexec-accept,\
-bugprone-easily-swappable-parameters,\
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,\
-concurrency-mt-unsafe,\
@@ -37,6 +40,7 @@ clang-tidy:
-misc-include-cleaner,\
-modernize-macro-to-enum,\
-readability-braces-around-statements,\
+ -readability-function-cognitive-complexity,\
-readability-identifier-length,\
-readability-isolate-declaration \
$(SRCS)
diff --git a/doc/platform-requirements/common.h b/doc/platform-requirements/common.h
index 8844b1e..e85fc2b 100644
--- a/doc/platform-requirements/common.h
+++ b/doc/platform-requirements/common.h
@@ -15,6 +15,7 @@
#include <stdio.h>
#include <stdlib.h>
+__attribute__((format(printf, 1, 2), noreturn))
static inline void die(const char *fmt, ...)
{
va_list ap;
diff --git a/doc/platform-requirements/listen-vs-repair.c b/doc/platform-requirements/listen-vs-repair.c
new file mode 100644
index 0000000..e21d168
--- /dev/null
+++ b/doc/platform-requirements/listen-vs-repair.c
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/* liste-vs-repair.c
+ *
+ * Do listening sockets have address conflicts with sockets under repair
+ * ====================================================================
+ *
+ * When we accept() an incoming connection the accept()ed socket will have the
+ * same local address as the listening socket. This can be a complication on
+ * migration. On the migration target we've already set up listening sockets
+ * according to the command line. However to restore connections that we're
+ * migrating in we need to bind the new sockets to the same address, which would
+ * be an address conflict on the face of it. This test program verifies that
+ * enabling repair mode before bind() correctly suppresses that conflict.
+ *
+ * Copyright Red Hat
+ * Author: David Gibson <david@gibson.dropbear.id.au>
+ */
+
+/* NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
+#define _GNU_SOURCE
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <sched.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "common.h"
+
+#define PORT 13256U
+#define CPORT 13257U
+
+/* 127.0.0.1:PORT */
+static const struct sockaddr_in addr = SOCKADDR_INIT(INADDR_LOOPBACK, PORT);
+
+/* 127.0.0.1:CPORT */
+static const struct sockaddr_in caddr = SOCKADDR_INIT(INADDR_LOOPBACK, CPORT);
+
+/* Put ourselves into a network sandbox */
+static void net_sandbox(void)
+{
+ /* NOLINTNEXTLINE(altera-struct-pack-align) */
+ const struct req_t {
+ struct nlmsghdr nlh;
+ struct ifinfomsg ifm;
+ } __attribute__((packed)) req = {
+ .nlh.nlmsg_type = RTM_NEWLINK,
+ .nlh.nlmsg_flags = NLM_F_REQUEST,
+ .nlh.nlmsg_len = sizeof(req),
+ .nlh.nlmsg_seq = 1,
+ .ifm.ifi_family = AF_UNSPEC,
+ .ifm.ifi_index = 1,
+ .ifm.ifi_flags = IFF_UP,
+ .ifm.ifi_change = IFF_UP,
+ };
+ int nl;
+
+ if (unshare(CLONE_NEWUSER | CLONE_NEWNET))
+ die("unshare(): %s\n", strerror(errno));
+
+ /* Bring up lo in the new netns */
+ nl = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
+ if (nl < 0)
+ die("Can't create netlink socket: %s\n", strerror(errno));
+
+ if (send(nl, &req, sizeof(req), 0) < 0)
+ die("Netlink send(): %s\n", strerror(errno));
+ close(nl);
+}
+
+static void check(void)
+{
+ int s1, s2, op;
+
+ s1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (s1 < 0)
+ die("socket() 1: %s\n", strerror(errno));
+
+ if (bind(s1, (struct sockaddr *)&addr, sizeof(addr)))
+ die("bind() 1: %s\n", strerror(errno));
+
+ if (listen(s1, 0))
+ die("listen(): %s\n", strerror(errno));
+
+ s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (s2 < 0)
+ die("socket() 2: %s\n", strerror(errno));
+
+ op = TCP_REPAIR_ON;
+ if (setsockopt(s2, SOL_TCP, TCP_REPAIR, &op, sizeof(op)))
+ die("TCP_REPAIR: %s\n", strerror(errno));
+
+ if (bind(s2, (struct sockaddr *)&addr, sizeof(addr)))
+ die("bind() 2: %s\n", strerror(errno));
+
+ if (connect(s2, (struct sockaddr *)&caddr, sizeof(caddr)))
+ die("connect(): %s\n", strerror(errno));
+
+ op = TCP_REPAIR_OFF_NO_WP;
+ if (setsockopt(s2, SOL_TCP, TCP_REPAIR, &op, sizeof(op)))
+ die("TCP_REPAIR: %s\n", strerror(errno));
+
+ close(s1);
+ close(s2);
+}
+
+int main(int argc, char *argv[])
+{
+ (void)argc;
+ (void)argv;
+
+ net_sandbox();
+
+ check();
+
+ printf("Repair mode appears to properly suppress conflicts with listening sockets\n");
+
+ exit(0);
+}
diff --git a/doc/platform-requirements/reuseaddr-priority.c b/doc/platform-requirements/reuseaddr-priority.c
index 701b6ff..af39a39 100644
--- a/doc/platform-requirements/reuseaddr-priority.c
+++ b/doc/platform-requirements/reuseaddr-priority.c
@@ -46,13 +46,13 @@
/* Different cases for receiving socket configuration */
enum sock_type {
/* Socket is bound to 0.0.0.0:DSTPORT and not connected */
- SOCK_BOUND_ANY = 0,
+ SOCK_BOUND_ANY,
/* Socket is bound to 127.0.0.1:DSTPORT and not connected */
- SOCK_BOUND_LO = 1,
+ SOCK_BOUND_LO,
/* Socket is bound to 0.0.0.0:DSTPORT and connected to 127.0.0.1:SRCPORT */
- SOCK_CONNECTED = 2,
+ SOCK_CONNECTED,
NUM_SOCK_TYPES,
};
diff --git a/doc/platform-requirements/tcp-close-rst.c b/doc/platform-requirements/tcp-close-rst.c
new file mode 100644
index 0000000..0e508f6
--- /dev/null
+++ b/doc/platform-requirements/tcp-close-rst.c
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/* tcp-close-rst.c
+ *
+ * Check what operations on a TCP socket will trigger an RST.
+ *
+ * Copyright Red Hat
+ * Author: David Gibson <david@gibson.dropbear.id.au>
+ */
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "common.h"
+
+#define DSTPORT 13258U
+
+#define SRCADDR(n) \
+ (0x7f000000U | (n) << 16U | (n) << 8U | 0x1U)
+
+#define BASENUM 100
+
+/* 127.0.0.1:DSTPORT */
+static const struct sockaddr_in lo_dst = SOCKADDR_INIT(INADDR_LOOPBACK, DSTPORT);
+
+#define LINGER 0x01U
+#define SHUT_CLIENT 0x02U
+#define SHUT_SERVER 0x04U
+
+#define NUM_OPTIONS (SHUT_SERVER << 1U)
+
+static void client_close(int sl, unsigned flags)
+{
+ struct sockaddr_in src = SOCKADDR_INIT(SRCADDR(flags), 0);
+ struct linger linger0 = {
+ .l_onoff = 1,
+ .l_linger = 0,
+ };
+ int sockerr, sc, sa;
+ socklen_t errlen = sizeof(sockerr);
+
+ printf("Client close %u:%s%s%s\n", flags,
+ flags & LINGER ? " LINGER" : "",
+ flags & SHUT_CLIENT ? " SHUT_CLIENT" : "",
+ flags & SHUT_SERVER ? " SHUT_SERVER" : "");
+
+ sc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (sc < 0)
+ die("socket() for connect(): %s\n", strerror(errno));
+
+ if (bind(sc, (struct sockaddr *)&src, sizeof(src)) < 0)
+ die("bind() for connect: %s\n", strerror(errno));
+
+ if (connect(sc, (struct sockaddr *)&lo_dst, sizeof(lo_dst)) < 0)
+ die("connect(): %s\n", strerror(errno));
+
+ /* cppcheck-suppress [android-cloexec-accept,unmatchedSuppression] */
+ sa = accept(sl, NULL, NULL);
+ if (sa < 0)
+ die("accept(): %s\n", strerror(errno));
+
+ if (flags & SHUT_SERVER)
+ if (shutdown(sa, SHUT_WR) < 0)
+ die("shutdown() server: %s\n", strerror(errno));
+
+ if (flags & SHUT_CLIENT)
+ if (shutdown(sc, SHUT_WR) < 0)
+ die("shutdown() client: %s\n", strerror(errno));
+
+ if (flags & LINGER)
+ if (setsockopt(sc, SOL_SOCKET, SO_LINGER,
+ &linger0, sizeof(linger0)) < 0)
+ die("SO_LINGER: %s\n", strerror(errno));
+
+ close(sc);
+
+ if (getsockopt(sa, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) < 0)
+ die("SO_ERROR: %s\n", strerror(errno));
+
+ if (errlen != sizeof(sockerr))
+ die("SO_ERROR: bad option length\n");
+
+ printf("Server error: %s\n", strerror(sockerr));
+
+ if (flags & LINGER) {
+ if (!(flags & SHUT_SERVER) || !(flags & SHUT_CLIENT)) {
+ if (sockerr == 0)
+ die("No error after abrupt close(), no RST?\n");
+ } else {
+ if (sockerr != 0)
+ die("Error after full shutdown, bogus RST?\n");
+ }
+ }
+
+ close(sa);
+}
+
+static void server_close(int sl, unsigned flags)
+{
+ struct sockaddr_in src = SOCKADDR_INIT(SRCADDR(flags), 0);
+ struct linger linger0 = {
+ .l_onoff = 1,
+ .l_linger = 0,
+ };
+ int sockerr, sc, sa;
+ socklen_t errlen = sizeof(sockerr);
+
+ printf("Server close %u:%s%s%s\n", flags,
+ flags & LINGER ? " LINGER" : "",
+ flags & SHUT_CLIENT ? " SHUT_CLIENT" : "",
+ flags & SHUT_SERVER ? " SHUT_SERVER" : "");
+
+ sc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (sc < 0)
+ die("socket() for connect(): %s\n", strerror(errno));
+
+ if (bind(sc, (struct sockaddr *)&src, sizeof(src)) < 0)
+ die("bind() for connect: %s\n", strerror(errno));
+
+ if (connect(sc, (struct sockaddr *)&lo_dst, sizeof(lo_dst)) < 0)
+ die("connect(): %s\n", strerror(errno));
+
+ /* cppcheck-suppress [android-cloexec-accept,unmatchedSuppression] */
+ sa = accept(sl, NULL, NULL);
+ if (sa < 0)
+ die("accept(): %s\n", strerror(errno));
+
+ if (flags & SHUT_SERVER)
+ if (shutdown(sa, SHUT_WR) < 0)
+ die("shutdown() server: %s\n", strerror(errno));
+
+ if (flags & SHUT_CLIENT)
+ if (shutdown(sc, SHUT_WR) < 0)
+ die("shutdown() client: %s\n", strerror(errno));
+
+ if (flags & LINGER)
+ if (setsockopt(sa, SOL_SOCKET, SO_LINGER,
+ &linger0, sizeof(linger0)) < 0)
+ die("SO_LINGER: %s\n", strerror(errno));
+
+ close(sa);
+
+ if (getsockopt(sc, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) < 0)
+ die("SO_ERROR: %s\n", strerror(errno));
+
+ if (errlen != sizeof(sockerr))
+ die("SO_ERROR: bad option length\n");
+
+ printf("Client error: %s\n", strerror(sockerr));
+
+ if (flags & LINGER) {
+ if (!(flags & SHUT_SERVER) || !(flags & SHUT_CLIENT)) {
+ if (sockerr == 0)
+ die("No error after abrupt close(), no RST?\n");
+ } else {
+ if (sockerr != 0)
+ die("Error after full shutdown, bogus RST?\n");
+ }
+ }
+
+ close(sc);
+}
+
+int main(int argc, char *argv[])
+{
+ unsigned flags;
+ int y = 1;
+ int sl;
+
+ (void)argc;
+ (void)argv;
+
+ sl = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (sl < 0)
+ die("socket() for listen: %s\n", strerror(errno));
+
+ if (setsockopt(sl, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y)) < 0)
+ die("SO_REUSEADDR for listen: %s\n", strerror(errno));
+
+ if (bind(sl, (struct sockaddr *)&lo_dst, sizeof(lo_dst)) < 0)
+ die("bind() for listen: %s\n", strerror(errno));
+
+ if (listen(sl, 1) < 0)
+ die("listen(): %s\n", strerror(errno));
+
+ printf("Listening on port %u\n", DSTPORT);
+
+ for (flags = 0; flags < NUM_OPTIONS; flags++) {
+ client_close(sl, flags);
+ server_close(sl, flags);
+ }
+
+ close(sl);
+ exit(0);
+}