1048d19d4SFlorian Westphal // SPDX-License-Identifier: GPL-2.0
2048d19d4SFlorian Westphal 
3048d19d4SFlorian Westphal #define _GNU_SOURCE
4048d19d4SFlorian Westphal 
5048d19d4SFlorian Westphal #include <errno.h>
6048d19d4SFlorian Westphal #include <limits.h>
7048d19d4SFlorian Westphal #include <fcntl.h>
8048d19d4SFlorian Westphal #include <string.h>
95e6af0a7SFlorian Westphal #include <stdarg.h>
10048d19d4SFlorian Westphal #include <stdbool.h>
11048d19d4SFlorian Westphal #include <stdint.h>
12048d19d4SFlorian Westphal #include <stdio.h>
13048d19d4SFlorian Westphal #include <stdlib.h>
14048d19d4SFlorian Westphal #include <strings.h>
15df62f2ecSPaolo Abeni #include <signal.h>
16048d19d4SFlorian Westphal #include <unistd.h>
17b6ab64b0SPaolo Abeni #include <time.h>
18048d19d4SFlorian Westphal 
19*05be5e27SPaolo Abeni #include <sys/ioctl.h>
20048d19d4SFlorian Westphal #include <sys/poll.h>
21048d19d4SFlorian Westphal #include <sys/sendfile.h>
22048d19d4SFlorian Westphal #include <sys/stat.h>
23048d19d4SFlorian Westphal #include <sys/socket.h>
24048d19d4SFlorian Westphal #include <sys/types.h>
25048d19d4SFlorian Westphal #include <sys/mman.h>
26048d19d4SFlorian Westphal 
27048d19d4SFlorian Westphal #include <netdb.h>
28048d19d4SFlorian Westphal #include <netinet/in.h>
29048d19d4SFlorian Westphal 
30048d19d4SFlorian Westphal #include <linux/tcp.h>
315e6af0a7SFlorian Westphal #include <linux/time_types.h>
32*05be5e27SPaolo Abeni #include <linux/sockios.h>
33048d19d4SFlorian Westphal 
34048d19d4SFlorian Westphal extern int optind;
35048d19d4SFlorian Westphal 
36048d19d4SFlorian Westphal #ifndef IPPROTO_MPTCP
37048d19d4SFlorian Westphal #define IPPROTO_MPTCP 262
38048d19d4SFlorian Westphal #endif
39048d19d4SFlorian Westphal #ifndef TCP_ULP
40048d19d4SFlorian Westphal #define TCP_ULP 31
41048d19d4SFlorian Westphal #endif
42048d19d4SFlorian Westphal 
438a4b910dSFlorian Westphal static int  poll_timeout = 10 * 1000;
44048d19d4SFlorian Westphal static bool listen_mode;
45df62f2ecSPaolo Abeni static bool quit;
46048d19d4SFlorian Westphal 
47048d19d4SFlorian Westphal enum cfg_mode {
48048d19d4SFlorian Westphal 	CFG_MODE_POLL,
49048d19d4SFlorian Westphal 	CFG_MODE_MMAP,
50048d19d4SFlorian Westphal 	CFG_MODE_SENDFILE,
51048d19d4SFlorian Westphal };
52048d19d4SFlorian Westphal 
53df8aee6dSYonglong Li enum cfg_peek {
54df8aee6dSYonglong Li 	CFG_NONE_PEEK,
55df8aee6dSYonglong Li 	CFG_WITH_PEEK,
56df8aee6dSYonglong Li 	CFG_AFTER_PEEK,
57df8aee6dSYonglong Li };
58df8aee6dSYonglong Li 
59048d19d4SFlorian Westphal static enum cfg_mode cfg_mode = CFG_MODE_POLL;
60df8aee6dSYonglong Li static enum cfg_peek cfg_peek = CFG_NONE_PEEK;
61048d19d4SFlorian Westphal static const char *cfg_host;
62048d19d4SFlorian Westphal static const char *cfg_port	= "12000";
63048d19d4SFlorian Westphal static int cfg_sock_proto	= IPPROTO_MPTCP;
64048d19d4SFlorian Westphal static int pf = AF_INET;
65048d19d4SFlorian Westphal static int cfg_sndbuf;
668a4b910dSFlorian Westphal static int cfg_rcvbuf;
67b08fbf24SPaolo Abeni static bool cfg_join;
6813153324SGeliang Tang static bool cfg_remove;
69b6ab64b0SPaolo Abeni static unsigned int cfg_time;
702e580a63SGeliang Tang static unsigned int cfg_do_w;
71df62f2ecSPaolo Abeni static int cfg_wait;
72dc65fe82SFlorian Westphal static uint32_t cfg_mark;
73*05be5e27SPaolo Abeni static char *cfg_input;
74*05be5e27SPaolo Abeni static int cfg_repeat = 1;
75048d19d4SFlorian Westphal 
765e6af0a7SFlorian Westphal struct cfg_cmsg_types {
775e6af0a7SFlorian Westphal 	unsigned int cmsg_enabled:1;
785e6af0a7SFlorian Westphal 	unsigned int timestampns:1;
795cbd886cSFlorian Westphal 	unsigned int tcp_inq:1;
805e6af0a7SFlorian Westphal };
815e6af0a7SFlorian Westphal 
825fb62e9cSFlorian Westphal struct cfg_sockopt_types {
835fb62e9cSFlorian Westphal 	unsigned int transparent:1;
845fb62e9cSFlorian Westphal };
855fb62e9cSFlorian Westphal 
865cbd886cSFlorian Westphal struct tcp_inq_state {
875cbd886cSFlorian Westphal 	unsigned int last;
885cbd886cSFlorian Westphal 	bool expect_eof;
895cbd886cSFlorian Westphal };
905cbd886cSFlorian Westphal 
915cbd886cSFlorian Westphal static struct tcp_inq_state tcp_inq;
925cbd886cSFlorian Westphal 
935e6af0a7SFlorian Westphal static struct cfg_cmsg_types cfg_cmsg_types;
945fb62e9cSFlorian Westphal static struct cfg_sockopt_types cfg_sockopt_types;
955e6af0a7SFlorian Westphal 
96048d19d4SFlorian Westphal static void die_usage(void)
97048d19d4SFlorian Westphal {
98*05be5e27SPaolo Abeni 	fprintf(stderr, "Usage: mptcp_connect [-6] [-c cmsg] [-i file] [-I num] [-j] [-l] "
99*05be5e27SPaolo Abeni 		"[-m mode] [-M mark] [-o option] [-p port] [-P mode] [-j] [-l] [-r num] "
100*05be5e27SPaolo Abeni 		"[-s MPTCP|TCP] [-S num] [-r num] [-t num] [-T num] [-u] [-w sec] connect_address\n");
1018a4b910dSFlorian Westphal 	fprintf(stderr, "\t-6 use ipv6\n");
102*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-c cmsg -- test cmsg type <cmsg>\n");
103*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-i file -- read the data to send from the given file instead of stdin");
104*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-I num -- repeat the transfer 'num' times. In listen mode accepts num "
105*05be5e27SPaolo Abeni 		"incoming connections, in client mode, disconnect and reconnect to the server\n");
106*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-j     -- add additional sleep at connection start and tear down "
107*05be5e27SPaolo Abeni 		"-- for MPJ tests\n");
108*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-l     -- listens mode, accepts incoming connection\n");
109c6f4c2b0SDavide Caratti 	fprintf(stderr, "\t-m [poll|mmap|sendfile] -- use poll(default)/mmap+write/sendfile\n");
110dc65fe82SFlorian Westphal 	fprintf(stderr, "\t-M mark -- set socket packet mark\n");
1115fb62e9cSFlorian Westphal 	fprintf(stderr, "\t-o option -- test sockopt <option>\n");
112*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-p num -- use port num\n");
113df8aee6dSYonglong Li 	fprintf(stderr,
114df8aee6dSYonglong Li 		"\t-P [saveWithPeek|saveAfterPeek] -- save data with/after MSG_PEEK form tcp socket\n");
115*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-t num -- set poll timeout to num\n");
116*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-T num -- set expected runtime to num ms\n");
117*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-r num -- enable slow mode, limiting each write to num bytes "
118*05be5e27SPaolo Abeni 		"-- for remove addr tests\n");
119*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-R num -- set SO_RCVBUF to num\n");
120*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-s [MPTCP|TCP] -- use mptcp(default) or tcp sockets\n");
121*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-S num -- set SO_SNDBUF to num\n");
122*05be5e27SPaolo Abeni 	fprintf(stderr, "\t-w num -- wait num sec before closing the socket\n");
123048d19d4SFlorian Westphal 	exit(1);
124048d19d4SFlorian Westphal }
125048d19d4SFlorian Westphal 
1265e6af0a7SFlorian Westphal static void xerror(const char *fmt, ...)
1275e6af0a7SFlorian Westphal {
1285e6af0a7SFlorian Westphal 	va_list ap;
1295e6af0a7SFlorian Westphal 
1305e6af0a7SFlorian Westphal 	va_start(ap, fmt);
1315e6af0a7SFlorian Westphal 	vfprintf(stderr, fmt, ap);
1325e6af0a7SFlorian Westphal 	va_end(ap);
1335e6af0a7SFlorian Westphal 	exit(1);
1345e6af0a7SFlorian Westphal }
1355e6af0a7SFlorian Westphal 
136df62f2ecSPaolo Abeni static void handle_signal(int nr)
137df62f2ecSPaolo Abeni {
138df62f2ecSPaolo Abeni 	quit = true;
139df62f2ecSPaolo Abeni }
140df62f2ecSPaolo Abeni 
141048d19d4SFlorian Westphal static const char *getxinfo_strerr(int err)
142048d19d4SFlorian Westphal {
143048d19d4SFlorian Westphal 	if (err == EAI_SYSTEM)
144048d19d4SFlorian Westphal 		return strerror(errno);
145048d19d4SFlorian Westphal 
146048d19d4SFlorian Westphal 	return gai_strerror(err);
147048d19d4SFlorian Westphal }
148048d19d4SFlorian Westphal 
149048d19d4SFlorian Westphal static void xgetnameinfo(const struct sockaddr *addr, socklen_t addrlen,
150048d19d4SFlorian Westphal 			 char *host, socklen_t hostlen,
151048d19d4SFlorian Westphal 			 char *serv, socklen_t servlen)
152048d19d4SFlorian Westphal {
153048d19d4SFlorian Westphal 	int flags = NI_NUMERICHOST | NI_NUMERICSERV;
154048d19d4SFlorian Westphal 	int err = getnameinfo(addr, addrlen, host, hostlen, serv, servlen,
155048d19d4SFlorian Westphal 			      flags);
156048d19d4SFlorian Westphal 
157048d19d4SFlorian Westphal 	if (err) {
158048d19d4SFlorian Westphal 		const char *errstr = getxinfo_strerr(err);
159048d19d4SFlorian Westphal 
160048d19d4SFlorian Westphal 		fprintf(stderr, "Fatal: getnameinfo: %s\n", errstr);
161048d19d4SFlorian Westphal 		exit(1);
162048d19d4SFlorian Westphal 	}
163048d19d4SFlorian Westphal }
164048d19d4SFlorian Westphal 
165048d19d4SFlorian Westphal static void xgetaddrinfo(const char *node, const char *service,
166048d19d4SFlorian Westphal 			 const struct addrinfo *hints,
167048d19d4SFlorian Westphal 			 struct addrinfo **res)
168048d19d4SFlorian Westphal {
169048d19d4SFlorian Westphal 	int err = getaddrinfo(node, service, hints, res);
170048d19d4SFlorian Westphal 
171048d19d4SFlorian Westphal 	if (err) {
172048d19d4SFlorian Westphal 		const char *errstr = getxinfo_strerr(err);
173048d19d4SFlorian Westphal 
174048d19d4SFlorian Westphal 		fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n",
175048d19d4SFlorian Westphal 			node ? node : "", service ? service : "", errstr);
176048d19d4SFlorian Westphal 		exit(1);
177048d19d4SFlorian Westphal 	}
178048d19d4SFlorian Westphal }
179048d19d4SFlorian Westphal 
1808a4b910dSFlorian Westphal static void set_rcvbuf(int fd, unsigned int size)
1818a4b910dSFlorian Westphal {
1828a4b910dSFlorian Westphal 	int err;
1838a4b910dSFlorian Westphal 
1848a4b910dSFlorian Westphal 	err = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
1858a4b910dSFlorian Westphal 	if (err) {
1868a4b910dSFlorian Westphal 		perror("set SO_RCVBUF");
1878a4b910dSFlorian Westphal 		exit(1);
1888a4b910dSFlorian Westphal 	}
1898a4b910dSFlorian Westphal }
1908a4b910dSFlorian Westphal 
191048d19d4SFlorian Westphal static void set_sndbuf(int fd, unsigned int size)
192048d19d4SFlorian Westphal {
193048d19d4SFlorian Westphal 	int err;
194048d19d4SFlorian Westphal 
195048d19d4SFlorian Westphal 	err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
196048d19d4SFlorian Westphal 	if (err) {
197048d19d4SFlorian Westphal 		perror("set SO_SNDBUF");
198048d19d4SFlorian Westphal 		exit(1);
199048d19d4SFlorian Westphal 	}
200048d19d4SFlorian Westphal }
201048d19d4SFlorian Westphal 
202dc65fe82SFlorian Westphal static void set_mark(int fd, uint32_t mark)
203dc65fe82SFlorian Westphal {
204dc65fe82SFlorian Westphal 	int err;
205dc65fe82SFlorian Westphal 
206dc65fe82SFlorian Westphal 	err = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
207dc65fe82SFlorian Westphal 	if (err) {
208dc65fe82SFlorian Westphal 		perror("set SO_MARK");
209dc65fe82SFlorian Westphal 		exit(1);
210dc65fe82SFlorian Westphal 	}
211dc65fe82SFlorian Westphal }
212dc65fe82SFlorian Westphal 
2135fb62e9cSFlorian Westphal static void set_transparent(int fd, int pf)
2145fb62e9cSFlorian Westphal {
2155fb62e9cSFlorian Westphal 	int one = 1;
2165fb62e9cSFlorian Westphal 
2175fb62e9cSFlorian Westphal 	switch (pf) {
2185fb62e9cSFlorian Westphal 	case AF_INET:
2195fb62e9cSFlorian Westphal 		if (-1 == setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)))
2205fb62e9cSFlorian Westphal 			perror("IP_TRANSPARENT");
2215fb62e9cSFlorian Westphal 		break;
2225fb62e9cSFlorian Westphal 	case AF_INET6:
2235fb62e9cSFlorian Westphal 		if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)))
2245fb62e9cSFlorian Westphal 			perror("IPV6_TRANSPARENT");
2255fb62e9cSFlorian Westphal 		break;
2265fb62e9cSFlorian Westphal 	}
2275fb62e9cSFlorian Westphal }
2285fb62e9cSFlorian Westphal 
229f730b65cSFlorian Westphal static int do_ulp_so(int sock, const char *name)
230f730b65cSFlorian Westphal {
231f730b65cSFlorian Westphal 	return setsockopt(sock, IPPROTO_TCP, TCP_ULP, name, strlen(name));
232f730b65cSFlorian Westphal }
233f730b65cSFlorian Westphal 
234f730b65cSFlorian Westphal #define X(m)	xerror("%s:%u: %s: failed for proto %d at line %u", __FILE__, __LINE__, (m), proto, line)
235f730b65cSFlorian Westphal static void sock_test_tcpulp(int sock, int proto, unsigned int line)
236f730b65cSFlorian Westphal {
237f730b65cSFlorian Westphal 	socklen_t buflen = 8;
238f730b65cSFlorian Westphal 	char buf[8] = "";
239f730b65cSFlorian Westphal 	int ret = getsockopt(sock, IPPROTO_TCP, TCP_ULP, buf, &buflen);
240f730b65cSFlorian Westphal 
241f730b65cSFlorian Westphal 	if (ret != 0)
242f730b65cSFlorian Westphal 		X("getsockopt");
243f730b65cSFlorian Westphal 
244f730b65cSFlorian Westphal 	if (buflen > 0) {
245f730b65cSFlorian Westphal 		if (strcmp(buf, "mptcp") != 0)
246f730b65cSFlorian Westphal 			xerror("unexpected ULP '%s' for proto %d at line %u", buf, proto, line);
247f730b65cSFlorian Westphal 		ret = do_ulp_so(sock, "tls");
248f730b65cSFlorian Westphal 		if (ret == 0)
249f730b65cSFlorian Westphal 			X("setsockopt");
250f730b65cSFlorian Westphal 	} else if (proto == IPPROTO_MPTCP) {
251f730b65cSFlorian Westphal 		ret = do_ulp_so(sock, "tls");
252f730b65cSFlorian Westphal 		if (ret != -1)
253f730b65cSFlorian Westphal 			X("setsockopt");
254f730b65cSFlorian Westphal 	}
255f730b65cSFlorian Westphal 
256f730b65cSFlorian Westphal 	ret = do_ulp_so(sock, "mptcp");
257f730b65cSFlorian Westphal 	if (ret != -1)
258f730b65cSFlorian Westphal 		X("setsockopt");
259f730b65cSFlorian Westphal 
260f730b65cSFlorian Westphal #undef X
261f730b65cSFlorian Westphal }
262f730b65cSFlorian Westphal 
263f730b65cSFlorian Westphal #define SOCK_TEST_TCPULP(s, p) sock_test_tcpulp((s), (p), __LINE__)
264f730b65cSFlorian Westphal 
265048d19d4SFlorian Westphal static int sock_listen_mptcp(const char * const listenaddr,
266048d19d4SFlorian Westphal 			     const char * const port)
267048d19d4SFlorian Westphal {
268048d19d4SFlorian Westphal 	int sock;
269048d19d4SFlorian Westphal 	struct addrinfo hints = {
270048d19d4SFlorian Westphal 		.ai_protocol = IPPROTO_TCP,
271048d19d4SFlorian Westphal 		.ai_socktype = SOCK_STREAM,
272048d19d4SFlorian Westphal 		.ai_flags = AI_PASSIVE | AI_NUMERICHOST
273048d19d4SFlorian Westphal 	};
274048d19d4SFlorian Westphal 
275048d19d4SFlorian Westphal 	hints.ai_family = pf;
276048d19d4SFlorian Westphal 
277048d19d4SFlorian Westphal 	struct addrinfo *a, *addr;
278048d19d4SFlorian Westphal 	int one = 1;
279048d19d4SFlorian Westphal 
280048d19d4SFlorian Westphal 	xgetaddrinfo(listenaddr, port, &hints, &addr);
281048d19d4SFlorian Westphal 	hints.ai_family = pf;
282048d19d4SFlorian Westphal 
283048d19d4SFlorian Westphal 	for (a = addr; a; a = a->ai_next) {
284048d19d4SFlorian Westphal 		sock = socket(a->ai_family, a->ai_socktype, cfg_sock_proto);
285048d19d4SFlorian Westphal 		if (sock < 0)
286048d19d4SFlorian Westphal 			continue;
287048d19d4SFlorian Westphal 
288f730b65cSFlorian Westphal 		SOCK_TEST_TCPULP(sock, cfg_sock_proto);
289f730b65cSFlorian Westphal 
290048d19d4SFlorian Westphal 		if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one,
291048d19d4SFlorian Westphal 				     sizeof(one)))
292048d19d4SFlorian Westphal 			perror("setsockopt");
293048d19d4SFlorian Westphal 
2945fb62e9cSFlorian Westphal 		if (cfg_sockopt_types.transparent)
2955fb62e9cSFlorian Westphal 			set_transparent(sock, pf);
2965fb62e9cSFlorian Westphal 
297048d19d4SFlorian Westphal 		if (bind(sock, a->ai_addr, a->ai_addrlen) == 0)
298048d19d4SFlorian Westphal 			break; /* success */
299048d19d4SFlorian Westphal 
300048d19d4SFlorian Westphal 		perror("bind");
301048d19d4SFlorian Westphal 		close(sock);
302048d19d4SFlorian Westphal 		sock = -1;
303048d19d4SFlorian Westphal 	}
304048d19d4SFlorian Westphal 
305048d19d4SFlorian Westphal 	freeaddrinfo(addr);
306048d19d4SFlorian Westphal 
307048d19d4SFlorian Westphal 	if (sock < 0) {
308048d19d4SFlorian Westphal 		fprintf(stderr, "Could not create listen socket\n");
309048d19d4SFlorian Westphal 		return sock;
310048d19d4SFlorian Westphal 	}
311048d19d4SFlorian Westphal 
312f730b65cSFlorian Westphal 	SOCK_TEST_TCPULP(sock, cfg_sock_proto);
313f730b65cSFlorian Westphal 
314048d19d4SFlorian Westphal 	if (listen(sock, 20)) {
315048d19d4SFlorian Westphal 		perror("listen");
316048d19d4SFlorian Westphal 		close(sock);
317048d19d4SFlorian Westphal 		return -1;
318048d19d4SFlorian Westphal 	}
319048d19d4SFlorian Westphal 
320f730b65cSFlorian Westphal 	SOCK_TEST_TCPULP(sock, cfg_sock_proto);
321f730b65cSFlorian Westphal 
322048d19d4SFlorian Westphal 	return sock;
323048d19d4SFlorian Westphal }
324048d19d4SFlorian Westphal 
325048d19d4SFlorian Westphal static int sock_connect_mptcp(const char * const remoteaddr,
326*05be5e27SPaolo Abeni 			      const char * const port, int proto,
327*05be5e27SPaolo Abeni 			      struct addrinfo **peer)
328048d19d4SFlorian Westphal {
329048d19d4SFlorian Westphal 	struct addrinfo hints = {
330048d19d4SFlorian Westphal 		.ai_protocol = IPPROTO_TCP,
331048d19d4SFlorian Westphal 		.ai_socktype = SOCK_STREAM,
332048d19d4SFlorian Westphal 	};
333048d19d4SFlorian Westphal 	struct addrinfo *a, *addr;
334048d19d4SFlorian Westphal 	int sock = -1;
335048d19d4SFlorian Westphal 
336048d19d4SFlorian Westphal 	hints.ai_family = pf;
337048d19d4SFlorian Westphal 
338048d19d4SFlorian Westphal 	xgetaddrinfo(remoteaddr, port, &hints, &addr);
339048d19d4SFlorian Westphal 	for (a = addr; a; a = a->ai_next) {
340048d19d4SFlorian Westphal 		sock = socket(a->ai_family, a->ai_socktype, proto);
341048d19d4SFlorian Westphal 		if (sock < 0) {
342048d19d4SFlorian Westphal 			perror("socket");
343048d19d4SFlorian Westphal 			continue;
344048d19d4SFlorian Westphal 		}
345048d19d4SFlorian Westphal 
346f730b65cSFlorian Westphal 		SOCK_TEST_TCPULP(sock, proto);
347f730b65cSFlorian Westphal 
348dc65fe82SFlorian Westphal 		if (cfg_mark)
349dc65fe82SFlorian Westphal 			set_mark(sock, cfg_mark);
350dc65fe82SFlorian Westphal 
351*05be5e27SPaolo Abeni 		if (connect(sock, a->ai_addr, a->ai_addrlen) == 0) {
352*05be5e27SPaolo Abeni 			*peer = a;
353048d19d4SFlorian Westphal 			break; /* success */
354*05be5e27SPaolo Abeni 		}
355048d19d4SFlorian Westphal 
356048d19d4SFlorian Westphal 		perror("connect()");
357048d19d4SFlorian Westphal 		close(sock);
358048d19d4SFlorian Westphal 		sock = -1;
359048d19d4SFlorian Westphal 	}
360048d19d4SFlorian Westphal 
361048d19d4SFlorian Westphal 	freeaddrinfo(addr);
362f730b65cSFlorian Westphal 	if (sock != -1)
363f730b65cSFlorian Westphal 		SOCK_TEST_TCPULP(sock, proto);
364048d19d4SFlorian Westphal 	return sock;
365048d19d4SFlorian Westphal }
366048d19d4SFlorian Westphal 
367048d19d4SFlorian Westphal static size_t do_rnd_write(const int fd, char *buf, const size_t len)
368048d19d4SFlorian Westphal {
369b08fbf24SPaolo Abeni 	static bool first = true;
370048d19d4SFlorian Westphal 	unsigned int do_w;
371048d19d4SFlorian Westphal 	ssize_t bw;
372048d19d4SFlorian Westphal 
373048d19d4SFlorian Westphal 	do_w = rand() & 0xffff;
374048d19d4SFlorian Westphal 	if (do_w == 0 || do_w > len)
375048d19d4SFlorian Westphal 		do_w = len;
376048d19d4SFlorian Westphal 
377b08fbf24SPaolo Abeni 	if (cfg_join && first && do_w > 100)
378b08fbf24SPaolo Abeni 		do_w = 100;
379b08fbf24SPaolo Abeni 
3802e580a63SGeliang Tang 	if (cfg_remove && do_w > cfg_do_w)
3812e580a63SGeliang Tang 		do_w = cfg_do_w;
38213153324SGeliang Tang 
383048d19d4SFlorian Westphal 	bw = write(fd, buf, do_w);
384048d19d4SFlorian Westphal 	if (bw < 0)
385048d19d4SFlorian Westphal 		perror("write");
386048d19d4SFlorian Westphal 
387b08fbf24SPaolo Abeni 	/* let the join handshake complete, before going on */
388b08fbf24SPaolo Abeni 	if (cfg_join && first) {
389b08fbf24SPaolo Abeni 		usleep(200000);
390b08fbf24SPaolo Abeni 		first = false;
391b08fbf24SPaolo Abeni 	}
392b08fbf24SPaolo Abeni 
39313153324SGeliang Tang 	if (cfg_remove)
39413153324SGeliang Tang 		usleep(200000);
39513153324SGeliang Tang 
396048d19d4SFlorian Westphal 	return bw;
397048d19d4SFlorian Westphal }
398048d19d4SFlorian Westphal 
399048d19d4SFlorian Westphal static size_t do_write(const int fd, char *buf, const size_t len)
400048d19d4SFlorian Westphal {
401048d19d4SFlorian Westphal 	size_t offset = 0;
402048d19d4SFlorian Westphal 
403048d19d4SFlorian Westphal 	while (offset < len) {
404048d19d4SFlorian Westphal 		size_t written;
405048d19d4SFlorian Westphal 		ssize_t bw;
406048d19d4SFlorian Westphal 
407048d19d4SFlorian Westphal 		bw = write(fd, buf + offset, len - offset);
408048d19d4SFlorian Westphal 		if (bw < 0) {
409048d19d4SFlorian Westphal 			perror("write");
410048d19d4SFlorian Westphal 			return 0;
411048d19d4SFlorian Westphal 		}
412048d19d4SFlorian Westphal 
413048d19d4SFlorian Westphal 		written = (size_t)bw;
414048d19d4SFlorian Westphal 		offset += written;
415048d19d4SFlorian Westphal 	}
416048d19d4SFlorian Westphal 
417048d19d4SFlorian Westphal 	return offset;
418048d19d4SFlorian Westphal }
419048d19d4SFlorian Westphal 
4205e6af0a7SFlorian Westphal static void process_cmsg(struct msghdr *msgh)
4215e6af0a7SFlorian Westphal {
4225e6af0a7SFlorian Westphal 	struct __kernel_timespec ts;
4235cbd886cSFlorian Westphal 	bool inq_found = false;
4245e6af0a7SFlorian Westphal 	bool ts_found = false;
4255cbd886cSFlorian Westphal 	unsigned int inq = 0;
4265e6af0a7SFlorian Westphal 	struct cmsghdr *cmsg;
4275e6af0a7SFlorian Westphal 
4285e6af0a7SFlorian Westphal 	for (cmsg = CMSG_FIRSTHDR(msgh); cmsg ; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
4295e6af0a7SFlorian Westphal 		if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMPNS_NEW) {
4305e6af0a7SFlorian Westphal 			memcpy(&ts, CMSG_DATA(cmsg), sizeof(ts));
4315e6af0a7SFlorian Westphal 			ts_found = true;
4325e6af0a7SFlorian Westphal 			continue;
4335e6af0a7SFlorian Westphal 		}
4345cbd886cSFlorian Westphal 		if (cmsg->cmsg_level == IPPROTO_TCP && cmsg->cmsg_type == TCP_CM_INQ) {
4355cbd886cSFlorian Westphal 			memcpy(&inq, CMSG_DATA(cmsg), sizeof(inq));
4365cbd886cSFlorian Westphal 			inq_found = true;
4375cbd886cSFlorian Westphal 			continue;
4385cbd886cSFlorian Westphal 		}
4395cbd886cSFlorian Westphal 
4405e6af0a7SFlorian Westphal 	}
4415e6af0a7SFlorian Westphal 
4425e6af0a7SFlorian Westphal 	if (cfg_cmsg_types.timestampns) {
4435e6af0a7SFlorian Westphal 		if (!ts_found)
4445e6af0a7SFlorian Westphal 			xerror("TIMESTAMPNS not present\n");
4455e6af0a7SFlorian Westphal 	}
4465cbd886cSFlorian Westphal 
4475cbd886cSFlorian Westphal 	if (cfg_cmsg_types.tcp_inq) {
4485cbd886cSFlorian Westphal 		if (!inq_found)
4495cbd886cSFlorian Westphal 			xerror("TCP_INQ not present\n");
4505cbd886cSFlorian Westphal 
4515cbd886cSFlorian Westphal 		if (inq > 1024)
4525cbd886cSFlorian Westphal 			xerror("tcp_inq %u is larger than one kbyte\n", inq);
4535cbd886cSFlorian Westphal 		tcp_inq.last = inq;
4545cbd886cSFlorian Westphal 	}
4555e6af0a7SFlorian Westphal }
4565e6af0a7SFlorian Westphal 
4575e6af0a7SFlorian Westphal static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len)
4585e6af0a7SFlorian Westphal {
4595e6af0a7SFlorian Westphal 	char msg_buf[8192];
4605e6af0a7SFlorian Westphal 	struct iovec iov = {
4615e6af0a7SFlorian Westphal 		.iov_base = buf,
4625e6af0a7SFlorian Westphal 		.iov_len = len,
4635e6af0a7SFlorian Westphal 	};
4645e6af0a7SFlorian Westphal 	struct msghdr msg = {
4655e6af0a7SFlorian Westphal 		.msg_iov = &iov,
4665e6af0a7SFlorian Westphal 		.msg_iovlen = 1,
4675e6af0a7SFlorian Westphal 		.msg_control = msg_buf,
4685e6af0a7SFlorian Westphal 		.msg_controllen = sizeof(msg_buf),
4695e6af0a7SFlorian Westphal 	};
4705e6af0a7SFlorian Westphal 	int flags = 0;
4715cbd886cSFlorian Westphal 	unsigned int last_hint = tcp_inq.last;
4725e6af0a7SFlorian Westphal 	int ret = recvmsg(fd, &msg, flags);
4735e6af0a7SFlorian Westphal 
4745cbd886cSFlorian Westphal 	if (ret <= 0) {
4755cbd886cSFlorian Westphal 		if (ret == 0 && tcp_inq.expect_eof)
4765e6af0a7SFlorian Westphal 			return ret;
4775e6af0a7SFlorian Westphal 
4785cbd886cSFlorian Westphal 		if (ret == 0 && cfg_cmsg_types.tcp_inq)
4795cbd886cSFlorian Westphal 			if (last_hint != 1 && last_hint != 0)
4805cbd886cSFlorian Westphal 				xerror("EOF but last tcp_inq hint was %u\n", last_hint);
4815cbd886cSFlorian Westphal 
4825cbd886cSFlorian Westphal 		return ret;
4835cbd886cSFlorian Westphal 	}
4845cbd886cSFlorian Westphal 
4855cbd886cSFlorian Westphal 	if (tcp_inq.expect_eof)
4865cbd886cSFlorian Westphal 		xerror("expected EOF, last_hint %u, now %u\n",
4875cbd886cSFlorian Westphal 		       last_hint, tcp_inq.last);
4885cbd886cSFlorian Westphal 
4895e6af0a7SFlorian Westphal 	if (msg.msg_controllen && !cfg_cmsg_types.cmsg_enabled)
4905e6af0a7SFlorian Westphal 		xerror("got %lu bytes of cmsg data, expected 0\n",
4915e6af0a7SFlorian Westphal 		       (unsigned long)msg.msg_controllen);
4925e6af0a7SFlorian Westphal 
4935e6af0a7SFlorian Westphal 	if (msg.msg_controllen == 0 && cfg_cmsg_types.cmsg_enabled)
4945e6af0a7SFlorian Westphal 		xerror("%s\n", "got no cmsg data");
4955e6af0a7SFlorian Westphal 
4965e6af0a7SFlorian Westphal 	if (msg.msg_controllen)
4975e6af0a7SFlorian Westphal 		process_cmsg(&msg);
4985e6af0a7SFlorian Westphal 
4995cbd886cSFlorian Westphal 	if (cfg_cmsg_types.tcp_inq) {
5005cbd886cSFlorian Westphal 		if ((size_t)ret < len && last_hint > (unsigned int)ret) {
5015cbd886cSFlorian Westphal 			if (ret + 1 != (int)last_hint) {
5025cbd886cSFlorian Westphal 				int next = read(fd, msg_buf, sizeof(msg_buf));
5035cbd886cSFlorian Westphal 
5045cbd886cSFlorian Westphal 				xerror("read %u of %u, last_hint was %u tcp_inq hint now %u next_read returned %d/%m\n",
5055cbd886cSFlorian Westphal 				       ret, (unsigned int)len, last_hint, tcp_inq.last, next);
5065cbd886cSFlorian Westphal 			} else {
5075cbd886cSFlorian Westphal 				tcp_inq.expect_eof = true;
5085cbd886cSFlorian Westphal 			}
5095cbd886cSFlorian Westphal 		}
5105cbd886cSFlorian Westphal 	}
5115cbd886cSFlorian Westphal 
5125e6af0a7SFlorian Westphal 	return ret;
5135e6af0a7SFlorian Westphal }
5145e6af0a7SFlorian Westphal 
515048d19d4SFlorian Westphal static ssize_t do_rnd_read(const int fd, char *buf, const size_t len)
516048d19d4SFlorian Westphal {
517df8aee6dSYonglong Li 	int ret = 0;
518df8aee6dSYonglong Li 	char tmp[16384];
519048d19d4SFlorian Westphal 	size_t cap = rand();
520048d19d4SFlorian Westphal 
521048d19d4SFlorian Westphal 	cap &= 0xffff;
522048d19d4SFlorian Westphal 
523048d19d4SFlorian Westphal 	if (cap == 0)
524048d19d4SFlorian Westphal 		cap = 1;
525048d19d4SFlorian Westphal 	else if (cap > len)
526048d19d4SFlorian Westphal 		cap = len;
527048d19d4SFlorian Westphal 
528df8aee6dSYonglong Li 	if (cfg_peek == CFG_WITH_PEEK) {
529df8aee6dSYonglong Li 		ret = recv(fd, buf, cap, MSG_PEEK);
530df8aee6dSYonglong Li 		ret = (ret < 0) ? ret : read(fd, tmp, ret);
531df8aee6dSYonglong Li 	} else if (cfg_peek == CFG_AFTER_PEEK) {
532df8aee6dSYonglong Li 		ret = recv(fd, buf, cap, MSG_PEEK);
533df8aee6dSYonglong Li 		ret = (ret < 0) ? ret : read(fd, buf, cap);
5345e6af0a7SFlorian Westphal 	} else if (cfg_cmsg_types.cmsg_enabled) {
5355e6af0a7SFlorian Westphal 		ret = do_recvmsg_cmsg(fd, buf, cap);
536df8aee6dSYonglong Li 	} else {
537df8aee6dSYonglong Li 		ret = read(fd, buf, cap);
538df8aee6dSYonglong Li 	}
539df8aee6dSYonglong Li 
540df8aee6dSYonglong Li 	return ret;
541048d19d4SFlorian Westphal }
542048d19d4SFlorian Westphal 
543*05be5e27SPaolo Abeni static void set_nonblock(int fd, bool nonblock)
544048d19d4SFlorian Westphal {
545048d19d4SFlorian Westphal 	int flags = fcntl(fd, F_GETFL);
546048d19d4SFlorian Westphal 
547048d19d4SFlorian Westphal 	if (flags == -1)
548048d19d4SFlorian Westphal 		return;
549048d19d4SFlorian Westphal 
550*05be5e27SPaolo Abeni 	if (nonblock)
551048d19d4SFlorian Westphal 		fcntl(fd, F_SETFL, flags | O_NONBLOCK);
552*05be5e27SPaolo Abeni 	else
553*05be5e27SPaolo Abeni 		fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
554048d19d4SFlorian Westphal }
555048d19d4SFlorian Westphal 
556b6ab64b0SPaolo Abeni static int copyfd_io_poll(int infd, int peerfd, int outfd, bool *in_closed_after_out)
557048d19d4SFlorian Westphal {
558048d19d4SFlorian Westphal 	struct pollfd fds = {
559048d19d4SFlorian Westphal 		.fd = peerfd,
560048d19d4SFlorian Westphal 		.events = POLLIN | POLLOUT,
561048d19d4SFlorian Westphal 	};
562048d19d4SFlorian Westphal 	unsigned int woff = 0, wlen = 0;
563048d19d4SFlorian Westphal 	char wbuf[8192];
564048d19d4SFlorian Westphal 
565*05be5e27SPaolo Abeni 	set_nonblock(peerfd, true);
566048d19d4SFlorian Westphal 
567048d19d4SFlorian Westphal 	for (;;) {
568048d19d4SFlorian Westphal 		char rbuf[8192];
569048d19d4SFlorian Westphal 		ssize_t len;
570048d19d4SFlorian Westphal 
571048d19d4SFlorian Westphal 		if (fds.events == 0)
572048d19d4SFlorian Westphal 			break;
573048d19d4SFlorian Westphal 
574048d19d4SFlorian Westphal 		switch (poll(&fds, 1, poll_timeout)) {
575048d19d4SFlorian Westphal 		case -1:
576048d19d4SFlorian Westphal 			if (errno == EINTR)
577048d19d4SFlorian Westphal 				continue;
578048d19d4SFlorian Westphal 			perror("poll");
579048d19d4SFlorian Westphal 			return 1;
580048d19d4SFlorian Westphal 		case 0:
581048d19d4SFlorian Westphal 			fprintf(stderr, "%s: poll timed out (events: "
582048d19d4SFlorian Westphal 				"POLLIN %u, POLLOUT %u)\n", __func__,
583048d19d4SFlorian Westphal 				fds.events & POLLIN, fds.events & POLLOUT);
584048d19d4SFlorian Westphal 			return 2;
585048d19d4SFlorian Westphal 		}
586048d19d4SFlorian Westphal 
587048d19d4SFlorian Westphal 		if (fds.revents & POLLIN) {
588048d19d4SFlorian Westphal 			len = do_rnd_read(peerfd, rbuf, sizeof(rbuf));
589048d19d4SFlorian Westphal 			if (len == 0) {
590048d19d4SFlorian Westphal 				/* no more data to receive:
591048d19d4SFlorian Westphal 				 * peer has closed its write side
592048d19d4SFlorian Westphal 				 */
593048d19d4SFlorian Westphal 				fds.events &= ~POLLIN;
594048d19d4SFlorian Westphal 
595b6ab64b0SPaolo Abeni 				if ((fds.events & POLLOUT) == 0) {
596b6ab64b0SPaolo Abeni 					*in_closed_after_out = true;
597048d19d4SFlorian Westphal 					/* and nothing more to send */
598048d19d4SFlorian Westphal 					break;
599b6ab64b0SPaolo Abeni 				}
600048d19d4SFlorian Westphal 
601048d19d4SFlorian Westphal 			/* Else, still have data to transmit */
602048d19d4SFlorian Westphal 			} else if (len < 0) {
603048d19d4SFlorian Westphal 				perror("read");
604048d19d4SFlorian Westphal 				return 3;
605048d19d4SFlorian Westphal 			}
606048d19d4SFlorian Westphal 
607048d19d4SFlorian Westphal 			do_write(outfd, rbuf, len);
608048d19d4SFlorian Westphal 		}
609048d19d4SFlorian Westphal 
610048d19d4SFlorian Westphal 		if (fds.revents & POLLOUT) {
611048d19d4SFlorian Westphal 			if (wlen == 0) {
612048d19d4SFlorian Westphal 				woff = 0;
613048d19d4SFlorian Westphal 				wlen = read(infd, wbuf, sizeof(wbuf));
614048d19d4SFlorian Westphal 			}
615048d19d4SFlorian Westphal 
616048d19d4SFlorian Westphal 			if (wlen > 0) {
617048d19d4SFlorian Westphal 				ssize_t bw;
618048d19d4SFlorian Westphal 
619048d19d4SFlorian Westphal 				bw = do_rnd_write(peerfd, wbuf + woff, wlen);
620048d19d4SFlorian Westphal 				if (bw < 0)
621048d19d4SFlorian Westphal 					return 111;
622048d19d4SFlorian Westphal 
623048d19d4SFlorian Westphal 				woff += bw;
624048d19d4SFlorian Westphal 				wlen -= bw;
625048d19d4SFlorian Westphal 			} else if (wlen == 0) {
626048d19d4SFlorian Westphal 				/* We have no more data to send. */
627048d19d4SFlorian Westphal 				fds.events &= ~POLLOUT;
628048d19d4SFlorian Westphal 
629048d19d4SFlorian Westphal 				if ((fds.events & POLLIN) == 0)
630048d19d4SFlorian Westphal 					/* ... and peer also closed already */
631048d19d4SFlorian Westphal 					break;
632048d19d4SFlorian Westphal 
633048d19d4SFlorian Westphal 				/* ... but we still receive.
634b08fbf24SPaolo Abeni 				 * Close our write side, ev. give some time
6356bdb6211SPaolo Abeni 				 * for address notification and/or checking
6366bdb6211SPaolo Abeni 				 * the current status
637048d19d4SFlorian Westphal 				 */
6386bdb6211SPaolo Abeni 				if (cfg_wait)
6396bdb6211SPaolo Abeni 					usleep(cfg_wait);
640048d19d4SFlorian Westphal 				shutdown(peerfd, SHUT_WR);
641048d19d4SFlorian Westphal 			} else {
642048d19d4SFlorian Westphal 				if (errno == EINTR)
643048d19d4SFlorian Westphal 					continue;
644048d19d4SFlorian Westphal 				perror("read");
645048d19d4SFlorian Westphal 				return 4;
646048d19d4SFlorian Westphal 			}
647048d19d4SFlorian Westphal 		}
648048d19d4SFlorian Westphal 
649048d19d4SFlorian Westphal 		if (fds.revents & (POLLERR | POLLNVAL)) {
650048d19d4SFlorian Westphal 			fprintf(stderr, "Unexpected revents: "
651048d19d4SFlorian Westphal 				"POLLERR/POLLNVAL(%x)\n", fds.revents);
652048d19d4SFlorian Westphal 			return 5;
653048d19d4SFlorian Westphal 		}
654048d19d4SFlorian Westphal 	}
655048d19d4SFlorian Westphal 
656b08fbf24SPaolo Abeni 	/* leave some time for late join/announce */
657b6ab64b0SPaolo Abeni 	if (cfg_remove)
658df62f2ecSPaolo Abeni 		usleep(cfg_wait);
659b08fbf24SPaolo Abeni 
660048d19d4SFlorian Westphal 	return 0;
661048d19d4SFlorian Westphal }
662048d19d4SFlorian Westphal 
663048d19d4SFlorian Westphal static int do_recvfile(int infd, int outfd)
664048d19d4SFlorian Westphal {
665048d19d4SFlorian Westphal 	ssize_t r;
666048d19d4SFlorian Westphal 
667048d19d4SFlorian Westphal 	do {
668048d19d4SFlorian Westphal 		char buf[16384];
669048d19d4SFlorian Westphal 
670048d19d4SFlorian Westphal 		r = do_rnd_read(infd, buf, sizeof(buf));
671048d19d4SFlorian Westphal 		if (r > 0) {
672048d19d4SFlorian Westphal 			if (write(outfd, buf, r) != r)
673048d19d4SFlorian Westphal 				break;
674048d19d4SFlorian Westphal 		} else if (r < 0) {
675048d19d4SFlorian Westphal 			perror("read");
676048d19d4SFlorian Westphal 		}
677048d19d4SFlorian Westphal 	} while (r > 0);
678048d19d4SFlorian Westphal 
679048d19d4SFlorian Westphal 	return (int)r;
680048d19d4SFlorian Westphal }
681048d19d4SFlorian Westphal 
682048d19d4SFlorian Westphal static int do_mmap(int infd, int outfd, unsigned int size)
683048d19d4SFlorian Westphal {
684048d19d4SFlorian Westphal 	char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0);
685048d19d4SFlorian Westphal 	ssize_t ret = 0, off = 0;
686048d19d4SFlorian Westphal 	size_t rem;
687048d19d4SFlorian Westphal 
688048d19d4SFlorian Westphal 	if (inbuf == MAP_FAILED) {
689048d19d4SFlorian Westphal 		perror("mmap");
690048d19d4SFlorian Westphal 		return 1;
691048d19d4SFlorian Westphal 	}
692048d19d4SFlorian Westphal 
693048d19d4SFlorian Westphal 	rem = size;
694048d19d4SFlorian Westphal 
695048d19d4SFlorian Westphal 	while (rem > 0) {
696048d19d4SFlorian Westphal 		ret = write(outfd, inbuf + off, rem);
697048d19d4SFlorian Westphal 
698048d19d4SFlorian Westphal 		if (ret < 0) {
699048d19d4SFlorian Westphal 			perror("write");
700048d19d4SFlorian Westphal 			break;
701048d19d4SFlorian Westphal 		}
702048d19d4SFlorian Westphal 
703048d19d4SFlorian Westphal 		off += ret;
704048d19d4SFlorian Westphal 		rem -= ret;
705048d19d4SFlorian Westphal 	}
706048d19d4SFlorian Westphal 
707048d19d4SFlorian Westphal 	munmap(inbuf, size);
708048d19d4SFlorian Westphal 	return rem;
709048d19d4SFlorian Westphal }
710048d19d4SFlorian Westphal 
711048d19d4SFlorian Westphal static int get_infd_size(int fd)
712048d19d4SFlorian Westphal {
713048d19d4SFlorian Westphal 	struct stat sb;
714048d19d4SFlorian Westphal 	ssize_t count;
715048d19d4SFlorian Westphal 	int err;
716048d19d4SFlorian Westphal 
717048d19d4SFlorian Westphal 	err = fstat(fd, &sb);
718048d19d4SFlorian Westphal 	if (err < 0) {
719048d19d4SFlorian Westphal 		perror("fstat");
720048d19d4SFlorian Westphal 		return -1;
721048d19d4SFlorian Westphal 	}
722048d19d4SFlorian Westphal 
723048d19d4SFlorian Westphal 	if ((sb.st_mode & S_IFMT) != S_IFREG) {
724048d19d4SFlorian Westphal 		fprintf(stderr, "%s: stdin is not a regular file\n", __func__);
725048d19d4SFlorian Westphal 		return -2;
726048d19d4SFlorian Westphal 	}
727048d19d4SFlorian Westphal 
728048d19d4SFlorian Westphal 	count = sb.st_size;
729048d19d4SFlorian Westphal 	if (count > INT_MAX) {
730048d19d4SFlorian Westphal 		fprintf(stderr, "File too large: %zu\n", count);
731048d19d4SFlorian Westphal 		return -3;
732048d19d4SFlorian Westphal 	}
733048d19d4SFlorian Westphal 
734048d19d4SFlorian Westphal 	return (int)count;
735048d19d4SFlorian Westphal }
736048d19d4SFlorian Westphal 
737048d19d4SFlorian Westphal static int do_sendfile(int infd, int outfd, unsigned int count)
738048d19d4SFlorian Westphal {
739048d19d4SFlorian Westphal 	while (count > 0) {
740048d19d4SFlorian Westphal 		ssize_t r;
741048d19d4SFlorian Westphal 
742048d19d4SFlorian Westphal 		r = sendfile(outfd, infd, NULL, count);
743048d19d4SFlorian Westphal 		if (r < 0) {
744048d19d4SFlorian Westphal 			perror("sendfile");
745048d19d4SFlorian Westphal 			return 3;
746048d19d4SFlorian Westphal 		}
747048d19d4SFlorian Westphal 
748048d19d4SFlorian Westphal 		count -= r;
749048d19d4SFlorian Westphal 	}
750048d19d4SFlorian Westphal 
751048d19d4SFlorian Westphal 	return 0;
752048d19d4SFlorian Westphal }
753048d19d4SFlorian Westphal 
754048d19d4SFlorian Westphal static int copyfd_io_mmap(int infd, int peerfd, int outfd,
755b6ab64b0SPaolo Abeni 			  unsigned int size, bool *in_closed_after_out)
756048d19d4SFlorian Westphal {
757048d19d4SFlorian Westphal 	int err;
758048d19d4SFlorian Westphal 
759048d19d4SFlorian Westphal 	if (listen_mode) {
760048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
761048d19d4SFlorian Westphal 		if (err)
762048d19d4SFlorian Westphal 			return err;
763048d19d4SFlorian Westphal 
764048d19d4SFlorian Westphal 		err = do_mmap(infd, peerfd, size);
765048d19d4SFlorian Westphal 	} else {
766048d19d4SFlorian Westphal 		err = do_mmap(infd, peerfd, size);
767048d19d4SFlorian Westphal 		if (err)
768048d19d4SFlorian Westphal 			return err;
769048d19d4SFlorian Westphal 
770048d19d4SFlorian Westphal 		shutdown(peerfd, SHUT_WR);
771048d19d4SFlorian Westphal 
772048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
773b6ab64b0SPaolo Abeni 		*in_closed_after_out = true;
774048d19d4SFlorian Westphal 	}
775048d19d4SFlorian Westphal 
776048d19d4SFlorian Westphal 	return err;
777048d19d4SFlorian Westphal }
778048d19d4SFlorian Westphal 
779048d19d4SFlorian Westphal static int copyfd_io_sendfile(int infd, int peerfd, int outfd,
780b6ab64b0SPaolo Abeni 			      unsigned int size, bool *in_closed_after_out)
781048d19d4SFlorian Westphal {
782048d19d4SFlorian Westphal 	int err;
783048d19d4SFlorian Westphal 
784048d19d4SFlorian Westphal 	if (listen_mode) {
785048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
786048d19d4SFlorian Westphal 		if (err)
787048d19d4SFlorian Westphal 			return err;
788048d19d4SFlorian Westphal 
789048d19d4SFlorian Westphal 		err = do_sendfile(infd, peerfd, size);
790048d19d4SFlorian Westphal 	} else {
791048d19d4SFlorian Westphal 		err = do_sendfile(infd, peerfd, size);
792048d19d4SFlorian Westphal 		if (err)
793048d19d4SFlorian Westphal 			return err;
794048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
795b6ab64b0SPaolo Abeni 		*in_closed_after_out = true;
796048d19d4SFlorian Westphal 	}
797048d19d4SFlorian Westphal 
798048d19d4SFlorian Westphal 	return err;
799048d19d4SFlorian Westphal }
800048d19d4SFlorian Westphal 
801*05be5e27SPaolo Abeni static int copyfd_io(int infd, int peerfd, int outfd, bool close_peerfd)
802048d19d4SFlorian Westphal {
803b6ab64b0SPaolo Abeni 	bool in_closed_after_out = false;
804b6ab64b0SPaolo Abeni 	struct timespec start, end;
805048d19d4SFlorian Westphal 	int file_size;
806b6ab64b0SPaolo Abeni 	int ret;
807b6ab64b0SPaolo Abeni 
808b6ab64b0SPaolo Abeni 	if (cfg_time && (clock_gettime(CLOCK_MONOTONIC, &start) < 0))
809b6ab64b0SPaolo Abeni 		xerror("can not fetch start time %d", errno);
810048d19d4SFlorian Westphal 
811048d19d4SFlorian Westphal 	switch (cfg_mode) {
812048d19d4SFlorian Westphal 	case CFG_MODE_POLL:
813b6ab64b0SPaolo Abeni 		ret = copyfd_io_poll(infd, peerfd, outfd, &in_closed_after_out);
814b6ab64b0SPaolo Abeni 		break;
815b6ab64b0SPaolo Abeni 
816048d19d4SFlorian Westphal 	case CFG_MODE_MMAP:
817048d19d4SFlorian Westphal 		file_size = get_infd_size(infd);
818048d19d4SFlorian Westphal 		if (file_size < 0)
819048d19d4SFlorian Westphal 			return file_size;
820b6ab64b0SPaolo Abeni 		ret = copyfd_io_mmap(infd, peerfd, outfd, file_size, &in_closed_after_out);
821b6ab64b0SPaolo Abeni 		break;
822b6ab64b0SPaolo Abeni 
823048d19d4SFlorian Westphal 	case CFG_MODE_SENDFILE:
824048d19d4SFlorian Westphal 		file_size = get_infd_size(infd);
825048d19d4SFlorian Westphal 		if (file_size < 0)
826048d19d4SFlorian Westphal 			return file_size;
827b6ab64b0SPaolo Abeni 		ret = copyfd_io_sendfile(infd, peerfd, outfd, file_size, &in_closed_after_out);
828b6ab64b0SPaolo Abeni 		break;
829048d19d4SFlorian Westphal 
830b6ab64b0SPaolo Abeni 	default:
831048d19d4SFlorian Westphal 		fprintf(stderr, "Invalid mode %d\n", cfg_mode);
832048d19d4SFlorian Westphal 
833048d19d4SFlorian Westphal 		die_usage();
834048d19d4SFlorian Westphal 		return 1;
835048d19d4SFlorian Westphal 	}
836048d19d4SFlorian Westphal 
837b6ab64b0SPaolo Abeni 	if (ret)
838b6ab64b0SPaolo Abeni 		return ret;
839b6ab64b0SPaolo Abeni 
840*05be5e27SPaolo Abeni 	if (close_peerfd)
841*05be5e27SPaolo Abeni 		close(peerfd);
842*05be5e27SPaolo Abeni 
843b6ab64b0SPaolo Abeni 	if (cfg_time) {
844b6ab64b0SPaolo Abeni 		unsigned int delta_ms;
845b6ab64b0SPaolo Abeni 
846b6ab64b0SPaolo Abeni 		if (clock_gettime(CLOCK_MONOTONIC, &end) < 0)
847b6ab64b0SPaolo Abeni 			xerror("can not fetch end time %d", errno);
848b6ab64b0SPaolo Abeni 		delta_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000;
849b6ab64b0SPaolo Abeni 		if (delta_ms > cfg_time) {
850b6ab64b0SPaolo Abeni 			xerror("transfer slower than expected! runtime %d ms, expected %d ms",
851b6ab64b0SPaolo Abeni 			       delta_ms, cfg_time);
852b6ab64b0SPaolo Abeni 		}
853b6ab64b0SPaolo Abeni 
854b6ab64b0SPaolo Abeni 		/* show the runtime only if this end shutdown(wr) before receiving the EOF,
855b6ab64b0SPaolo Abeni 		 * (that is, if this end got the longer runtime)
856b6ab64b0SPaolo Abeni 		 */
857b6ab64b0SPaolo Abeni 		if (in_closed_after_out)
858b6ab64b0SPaolo Abeni 			fprintf(stderr, "%d", delta_ms);
859b6ab64b0SPaolo Abeni 	}
860b6ab64b0SPaolo Abeni 
861b6ab64b0SPaolo Abeni 	return 0;
862b6ab64b0SPaolo Abeni }
863b6ab64b0SPaolo Abeni 
864048d19d4SFlorian Westphal static void check_sockaddr(int pf, struct sockaddr_storage *ss,
865048d19d4SFlorian Westphal 			   socklen_t salen)
866048d19d4SFlorian Westphal {
867048d19d4SFlorian Westphal 	struct sockaddr_in6 *sin6;
868048d19d4SFlorian Westphal 	struct sockaddr_in *sin;
869048d19d4SFlorian Westphal 	socklen_t wanted_size = 0;
870048d19d4SFlorian Westphal 
871048d19d4SFlorian Westphal 	switch (pf) {
872048d19d4SFlorian Westphal 	case AF_INET:
873048d19d4SFlorian Westphal 		wanted_size = sizeof(*sin);
874048d19d4SFlorian Westphal 		sin = (void *)ss;
875048d19d4SFlorian Westphal 		if (!sin->sin_port)
876048d19d4SFlorian Westphal 			fprintf(stderr, "accept: something wrong: ip connection from port 0");
877048d19d4SFlorian Westphal 		break;
878048d19d4SFlorian Westphal 	case AF_INET6:
879048d19d4SFlorian Westphal 		wanted_size = sizeof(*sin6);
880048d19d4SFlorian Westphal 		sin6 = (void *)ss;
881048d19d4SFlorian Westphal 		if (!sin6->sin6_port)
882048d19d4SFlorian Westphal 			fprintf(stderr, "accept: something wrong: ipv6 connection from port 0");
883048d19d4SFlorian Westphal 		break;
884048d19d4SFlorian Westphal 	default:
885048d19d4SFlorian Westphal 		fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen);
886048d19d4SFlorian Westphal 		return;
887048d19d4SFlorian Westphal 	}
888048d19d4SFlorian Westphal 
889048d19d4SFlorian Westphal 	if (salen != wanted_size)
890048d19d4SFlorian Westphal 		fprintf(stderr, "accept: size mismatch, got %d expected %d\n",
891048d19d4SFlorian Westphal 			(int)salen, wanted_size);
892048d19d4SFlorian Westphal 
893048d19d4SFlorian Westphal 	if (ss->ss_family != pf)
894048d19d4SFlorian Westphal 		fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n",
895048d19d4SFlorian Westphal 			(int)ss->ss_family, pf);
896048d19d4SFlorian Westphal }
897048d19d4SFlorian Westphal 
898048d19d4SFlorian Westphal static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen)
899048d19d4SFlorian Westphal {
900048d19d4SFlorian Westphal 	struct sockaddr_storage peerss;
901048d19d4SFlorian Westphal 	socklen_t peersalen = sizeof(peerss);
902048d19d4SFlorian Westphal 
903048d19d4SFlorian Westphal 	if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) {
904048d19d4SFlorian Westphal 		perror("getpeername");
905048d19d4SFlorian Westphal 		return;
906048d19d4SFlorian Westphal 	}
907048d19d4SFlorian Westphal 
908048d19d4SFlorian Westphal 	if (peersalen != salen) {
909048d19d4SFlorian Westphal 		fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen);
910048d19d4SFlorian Westphal 		return;
911048d19d4SFlorian Westphal 	}
912048d19d4SFlorian Westphal 
913048d19d4SFlorian Westphal 	if (memcmp(ss, &peerss, peersalen)) {
914048d19d4SFlorian Westphal 		char a[INET6_ADDRSTRLEN];
915048d19d4SFlorian Westphal 		char b[INET6_ADDRSTRLEN];
916048d19d4SFlorian Westphal 		char c[INET6_ADDRSTRLEN];
917048d19d4SFlorian Westphal 		char d[INET6_ADDRSTRLEN];
918048d19d4SFlorian Westphal 
919048d19d4SFlorian Westphal 		xgetnameinfo((struct sockaddr *)ss, salen,
920048d19d4SFlorian Westphal 			     a, sizeof(a), b, sizeof(b));
921048d19d4SFlorian Westphal 
922048d19d4SFlorian Westphal 		xgetnameinfo((struct sockaddr *)&peerss, peersalen,
923048d19d4SFlorian Westphal 			     c, sizeof(c), d, sizeof(d));
924048d19d4SFlorian Westphal 
925048d19d4SFlorian Westphal 		fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n",
926048d19d4SFlorian Westphal 			__func__, a, c, b, d, peersalen, salen);
927048d19d4SFlorian Westphal 	}
928048d19d4SFlorian Westphal }
929048d19d4SFlorian Westphal 
930048d19d4SFlorian Westphal static void check_getpeername_connect(int fd)
931048d19d4SFlorian Westphal {
932048d19d4SFlorian Westphal 	struct sockaddr_storage ss;
933048d19d4SFlorian Westphal 	socklen_t salen = sizeof(ss);
934048d19d4SFlorian Westphal 	char a[INET6_ADDRSTRLEN];
935048d19d4SFlorian Westphal 	char b[INET6_ADDRSTRLEN];
936048d19d4SFlorian Westphal 
937048d19d4SFlorian Westphal 	if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) {
938048d19d4SFlorian Westphal 		perror("getpeername");
939048d19d4SFlorian Westphal 		return;
940048d19d4SFlorian Westphal 	}
941048d19d4SFlorian Westphal 
942048d19d4SFlorian Westphal 	xgetnameinfo((struct sockaddr *)&ss, salen,
943048d19d4SFlorian Westphal 		     a, sizeof(a), b, sizeof(b));
944048d19d4SFlorian Westphal 
945048d19d4SFlorian Westphal 	if (strcmp(cfg_host, a) || strcmp(cfg_port, b))
946048d19d4SFlorian Westphal 		fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__,
947048d19d4SFlorian Westphal 			cfg_host, a, cfg_port, b);
948048d19d4SFlorian Westphal }
949048d19d4SFlorian Westphal 
950b0519de8SFlorian Westphal static void maybe_close(int fd)
951b0519de8SFlorian Westphal {
952b0519de8SFlorian Westphal 	unsigned int r = rand();
953b0519de8SFlorian Westphal 
954*05be5e27SPaolo Abeni 	if (!(cfg_join || cfg_remove || cfg_repeat > 1) && (r & 1))
955b0519de8SFlorian Westphal 		close(fd);
956b0519de8SFlorian Westphal }
957b0519de8SFlorian Westphal 
958048d19d4SFlorian Westphal int main_loop_s(int listensock)
959048d19d4SFlorian Westphal {
960048d19d4SFlorian Westphal 	struct sockaddr_storage ss;
961048d19d4SFlorian Westphal 	struct pollfd polls;
962048d19d4SFlorian Westphal 	socklen_t salen;
963048d19d4SFlorian Westphal 	int remotesock;
964*05be5e27SPaolo Abeni 	int fd = 0;
965048d19d4SFlorian Westphal 
966*05be5e27SPaolo Abeni again:
967048d19d4SFlorian Westphal 	polls.fd = listensock;
968048d19d4SFlorian Westphal 	polls.events = POLLIN;
969048d19d4SFlorian Westphal 
970048d19d4SFlorian Westphal 	switch (poll(&polls, 1, poll_timeout)) {
971048d19d4SFlorian Westphal 	case -1:
972048d19d4SFlorian Westphal 		perror("poll");
973048d19d4SFlorian Westphal 		return 1;
974048d19d4SFlorian Westphal 	case 0:
975048d19d4SFlorian Westphal 		fprintf(stderr, "%s: timed out\n", __func__);
976048d19d4SFlorian Westphal 		close(listensock);
977048d19d4SFlorian Westphal 		return 2;
978048d19d4SFlorian Westphal 	}
979048d19d4SFlorian Westphal 
980048d19d4SFlorian Westphal 	salen = sizeof(ss);
981048d19d4SFlorian Westphal 	remotesock = accept(listensock, (struct sockaddr *)&ss, &salen);
982048d19d4SFlorian Westphal 	if (remotesock >= 0) {
983b0519de8SFlorian Westphal 		maybe_close(listensock);
984048d19d4SFlorian Westphal 		check_sockaddr(pf, &ss, salen);
985048d19d4SFlorian Westphal 		check_getpeername(remotesock, &ss, salen);
986048d19d4SFlorian Westphal 
987*05be5e27SPaolo Abeni 		if (cfg_input) {
988*05be5e27SPaolo Abeni 			fd = open(cfg_input, O_RDONLY);
989*05be5e27SPaolo Abeni 			if (fd < 0)
990*05be5e27SPaolo Abeni 				xerror("can't open %s: %d", cfg_input, errno);
991048d19d4SFlorian Westphal 		}
992048d19d4SFlorian Westphal 
993*05be5e27SPaolo Abeni 		SOCK_TEST_TCPULP(remotesock, 0);
994048d19d4SFlorian Westphal 
995*05be5e27SPaolo Abeni 		copyfd_io(fd, remotesock, 1, true);
996*05be5e27SPaolo Abeni 	} else {
997*05be5e27SPaolo Abeni 		perror("accept");
998048d19d4SFlorian Westphal 		return 1;
999048d19d4SFlorian Westphal 	}
1000048d19d4SFlorian Westphal 
1001*05be5e27SPaolo Abeni 	if (--cfg_repeat > 0) {
1002*05be5e27SPaolo Abeni 		if (cfg_input)
1003*05be5e27SPaolo Abeni 			close(fd);
1004*05be5e27SPaolo Abeni 		goto again;
1005*05be5e27SPaolo Abeni 	}
1006*05be5e27SPaolo Abeni 
1007*05be5e27SPaolo Abeni 	return 0;
1008*05be5e27SPaolo Abeni }
1009*05be5e27SPaolo Abeni 
1010048d19d4SFlorian Westphal static void init_rng(void)
1011048d19d4SFlorian Westphal {
1012048d19d4SFlorian Westphal 	int fd = open("/dev/urandom", O_RDONLY);
1013048d19d4SFlorian Westphal 	unsigned int foo;
1014048d19d4SFlorian Westphal 
1015048d19d4SFlorian Westphal 	if (fd > 0) {
1016048d19d4SFlorian Westphal 		int ret = read(fd, &foo, sizeof(foo));
1017048d19d4SFlorian Westphal 
1018048d19d4SFlorian Westphal 		if (ret < 0)
1019048d19d4SFlorian Westphal 			srand(fd + foo);
1020048d19d4SFlorian Westphal 		close(fd);
1021048d19d4SFlorian Westphal 	}
1022048d19d4SFlorian Westphal 
1023048d19d4SFlorian Westphal 	srand(foo);
1024048d19d4SFlorian Westphal }
1025048d19d4SFlorian Westphal 
10265e6af0a7SFlorian Westphal static void xsetsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
10275e6af0a7SFlorian Westphal {
10285e6af0a7SFlorian Westphal 	int err;
10295e6af0a7SFlorian Westphal 
10305e6af0a7SFlorian Westphal 	err = setsockopt(fd, level, optname, optval, optlen);
10315e6af0a7SFlorian Westphal 	if (err) {
10325e6af0a7SFlorian Westphal 		perror("setsockopt");
10335e6af0a7SFlorian Westphal 		exit(1);
10345e6af0a7SFlorian Westphal 	}
10355e6af0a7SFlorian Westphal }
10365e6af0a7SFlorian Westphal 
10375e6af0a7SFlorian Westphal static void apply_cmsg_types(int fd, const struct cfg_cmsg_types *cmsg)
10385e6af0a7SFlorian Westphal {
10395e6af0a7SFlorian Westphal 	static const unsigned int on = 1;
10405e6af0a7SFlorian Westphal 
10415e6af0a7SFlorian Westphal 	if (cmsg->timestampns)
10425e6af0a7SFlorian Westphal 		xsetsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS_NEW, &on, sizeof(on));
10435cbd886cSFlorian Westphal 	if (cmsg->tcp_inq)
10445cbd886cSFlorian Westphal 		xsetsockopt(fd, IPPROTO_TCP, TCP_INQ, &on, sizeof(on));
10455e6af0a7SFlorian Westphal }
10465e6af0a7SFlorian Westphal 
10475e6af0a7SFlorian Westphal static void parse_cmsg_types(const char *type)
10485e6af0a7SFlorian Westphal {
10495e6af0a7SFlorian Westphal 	char *next = strchr(type, ',');
10505e6af0a7SFlorian Westphal 	unsigned int len = 0;
10515e6af0a7SFlorian Westphal 
10525e6af0a7SFlorian Westphal 	cfg_cmsg_types.cmsg_enabled = 1;
10535e6af0a7SFlorian Westphal 
10545e6af0a7SFlorian Westphal 	if (next) {
10555e6af0a7SFlorian Westphal 		parse_cmsg_types(next + 1);
10565e6af0a7SFlorian Westphal 		len = next - type;
10575e6af0a7SFlorian Westphal 	} else {
10585e6af0a7SFlorian Westphal 		len = strlen(type);
10595e6af0a7SFlorian Westphal 	}
10605e6af0a7SFlorian Westphal 
10615e6af0a7SFlorian Westphal 	if (strncmp(type, "TIMESTAMPNS", len) == 0) {
10625e6af0a7SFlorian Westphal 		cfg_cmsg_types.timestampns = 1;
10635e6af0a7SFlorian Westphal 		return;
10645e6af0a7SFlorian Westphal 	}
10655e6af0a7SFlorian Westphal 
10665cbd886cSFlorian Westphal 	if (strncmp(type, "TCPINQ", len) == 0) {
10675cbd886cSFlorian Westphal 		cfg_cmsg_types.tcp_inq = 1;
10685cbd886cSFlorian Westphal 		return;
10695cbd886cSFlorian Westphal 	}
10705cbd886cSFlorian Westphal 
10715e6af0a7SFlorian Westphal 	fprintf(stderr, "Unrecognized cmsg option %s\n", type);
10725e6af0a7SFlorian Westphal 	exit(1);
10735e6af0a7SFlorian Westphal }
10745e6af0a7SFlorian Westphal 
10755fb62e9cSFlorian Westphal static void parse_setsock_options(const char *name)
10765fb62e9cSFlorian Westphal {
10775fb62e9cSFlorian Westphal 	char *next = strchr(name, ',');
10785fb62e9cSFlorian Westphal 	unsigned int len = 0;
10795fb62e9cSFlorian Westphal 
10805fb62e9cSFlorian Westphal 	if (next) {
10815fb62e9cSFlorian Westphal 		parse_setsock_options(next + 1);
10825fb62e9cSFlorian Westphal 		len = next - name;
10835fb62e9cSFlorian Westphal 	} else {
10845fb62e9cSFlorian Westphal 		len = strlen(name);
10855fb62e9cSFlorian Westphal 	}
10865fb62e9cSFlorian Westphal 
10875fb62e9cSFlorian Westphal 	if (strncmp(name, "TRANSPARENT", len) == 0) {
10885fb62e9cSFlorian Westphal 		cfg_sockopt_types.transparent = 1;
10895fb62e9cSFlorian Westphal 		return;
10905fb62e9cSFlorian Westphal 	}
10915fb62e9cSFlorian Westphal 
10925fb62e9cSFlorian Westphal 	fprintf(stderr, "Unrecognized setsockopt option %s\n", name);
10935fb62e9cSFlorian Westphal 	exit(1);
10945fb62e9cSFlorian Westphal }
10955fb62e9cSFlorian Westphal 
1096*05be5e27SPaolo Abeni void xdisconnect(int fd, int addrlen)
1097*05be5e27SPaolo Abeni {
1098*05be5e27SPaolo Abeni 	struct sockaddr_storage empty;
1099*05be5e27SPaolo Abeni 	int msec_sleep = 10;
1100*05be5e27SPaolo Abeni 	int queued = 1;
1101*05be5e27SPaolo Abeni 	int i;
1102*05be5e27SPaolo Abeni 
1103*05be5e27SPaolo Abeni 	shutdown(fd, SHUT_WR);
1104*05be5e27SPaolo Abeni 
1105*05be5e27SPaolo Abeni 	/* while until the pending data is completely flushed, the later
1106*05be5e27SPaolo Abeni 	 * disconnect will bypass/ignore/drop any pending data.
1107*05be5e27SPaolo Abeni 	 */
1108*05be5e27SPaolo Abeni 	for (i = 0; ; i += msec_sleep) {
1109*05be5e27SPaolo Abeni 		if (ioctl(fd, SIOCOUTQ, &queued) < 0)
1110*05be5e27SPaolo Abeni 			xerror("can't query out socket queue: %d", errno);
1111*05be5e27SPaolo Abeni 
1112*05be5e27SPaolo Abeni 		if (!queued)
1113*05be5e27SPaolo Abeni 			break;
1114*05be5e27SPaolo Abeni 
1115*05be5e27SPaolo Abeni 		if (i > poll_timeout)
1116*05be5e27SPaolo Abeni 			xerror("timeout while waiting for spool to complete");
1117*05be5e27SPaolo Abeni 		usleep(msec_sleep * 1000);
1118*05be5e27SPaolo Abeni 	}
1119*05be5e27SPaolo Abeni 
1120*05be5e27SPaolo Abeni 	memset(&empty, 0, sizeof(empty));
1121*05be5e27SPaolo Abeni 	empty.ss_family = AF_UNSPEC;
1122*05be5e27SPaolo Abeni 	if (connect(fd, (struct sockaddr *)&empty, addrlen) < 0)
1123*05be5e27SPaolo Abeni 		xerror("can't disconnect: %d", errno);
1124*05be5e27SPaolo Abeni }
1125*05be5e27SPaolo Abeni 
1126048d19d4SFlorian Westphal int main_loop(void)
1127048d19d4SFlorian Westphal {
1128*05be5e27SPaolo Abeni 	int fd, ret, fd_in = 0;
1129*05be5e27SPaolo Abeni 	struct addrinfo *peer;
1130048d19d4SFlorian Westphal 
1131048d19d4SFlorian Westphal 	/* listener is ready. */
1132*05be5e27SPaolo Abeni 	fd = sock_connect_mptcp(cfg_host, cfg_port, cfg_sock_proto, &peer);
1133048d19d4SFlorian Westphal 	if (fd < 0)
1134048d19d4SFlorian Westphal 		return 2;
1135048d19d4SFlorian Westphal 
1136*05be5e27SPaolo Abeni again:
1137048d19d4SFlorian Westphal 	check_getpeername_connect(fd);
1138048d19d4SFlorian Westphal 
1139f730b65cSFlorian Westphal 	SOCK_TEST_TCPULP(fd, cfg_sock_proto);
1140f730b65cSFlorian Westphal 
11418a4b910dSFlorian Westphal 	if (cfg_rcvbuf)
11428a4b910dSFlorian Westphal 		set_rcvbuf(fd, cfg_rcvbuf);
1143048d19d4SFlorian Westphal 	if (cfg_sndbuf)
1144048d19d4SFlorian Westphal 		set_sndbuf(fd, cfg_sndbuf);
11455e6af0a7SFlorian Westphal 	if (cfg_cmsg_types.cmsg_enabled)
11465e6af0a7SFlorian Westphal 		apply_cmsg_types(fd, &cfg_cmsg_types);
1147048d19d4SFlorian Westphal 
1148*05be5e27SPaolo Abeni 	if (cfg_input) {
1149*05be5e27SPaolo Abeni 		fd_in = open(cfg_input, O_RDONLY);
1150*05be5e27SPaolo Abeni 		if (fd < 0)
1151*05be5e27SPaolo Abeni 			xerror("can't open %s:%d", cfg_input, errno);
1152*05be5e27SPaolo Abeni 	}
1153*05be5e27SPaolo Abeni 
1154*05be5e27SPaolo Abeni 	/* close the client socket open only if we are not going to reconnect */
1155*05be5e27SPaolo Abeni 	ret = copyfd_io(fd_in, fd, 1, cfg_repeat == 1);
1156*05be5e27SPaolo Abeni 	if (ret)
1157*05be5e27SPaolo Abeni 		return ret;
1158*05be5e27SPaolo Abeni 
1159*05be5e27SPaolo Abeni 	if (--cfg_repeat > 0) {
1160*05be5e27SPaolo Abeni 		xdisconnect(fd, peer->ai_addrlen);
1161*05be5e27SPaolo Abeni 
1162*05be5e27SPaolo Abeni 		/* the socket could be unblocking at this point, we need the
1163*05be5e27SPaolo Abeni 		 * connect to be blocking
1164*05be5e27SPaolo Abeni 		 */
1165*05be5e27SPaolo Abeni 		set_nonblock(fd, false);
1166*05be5e27SPaolo Abeni 		if (connect(fd, peer->ai_addr, peer->ai_addrlen))
1167*05be5e27SPaolo Abeni 			xerror("can't reconnect: %d", errno);
1168*05be5e27SPaolo Abeni 		if (cfg_input)
1169*05be5e27SPaolo Abeni 			close(fd_in);
1170*05be5e27SPaolo Abeni 		goto again;
1171*05be5e27SPaolo Abeni 	}
1172*05be5e27SPaolo Abeni 	return 0;
1173048d19d4SFlorian Westphal }
1174048d19d4SFlorian Westphal 
1175048d19d4SFlorian Westphal int parse_proto(const char *proto)
1176048d19d4SFlorian Westphal {
1177048d19d4SFlorian Westphal 	if (!strcasecmp(proto, "MPTCP"))
1178048d19d4SFlorian Westphal 		return IPPROTO_MPTCP;
1179048d19d4SFlorian Westphal 	if (!strcasecmp(proto, "TCP"))
1180048d19d4SFlorian Westphal 		return IPPROTO_TCP;
1181048d19d4SFlorian Westphal 
1182048d19d4SFlorian Westphal 	fprintf(stderr, "Unknown protocol: %s\n.", proto);
1183048d19d4SFlorian Westphal 	die_usage();
1184048d19d4SFlorian Westphal 
1185048d19d4SFlorian Westphal 	/* silence compiler warning */
1186048d19d4SFlorian Westphal 	return 0;
1187048d19d4SFlorian Westphal }
1188048d19d4SFlorian Westphal 
1189048d19d4SFlorian Westphal int parse_mode(const char *mode)
1190048d19d4SFlorian Westphal {
1191048d19d4SFlorian Westphal 	if (!strcasecmp(mode, "poll"))
1192048d19d4SFlorian Westphal 		return CFG_MODE_POLL;
1193048d19d4SFlorian Westphal 	if (!strcasecmp(mode, "mmap"))
1194048d19d4SFlorian Westphal 		return CFG_MODE_MMAP;
1195048d19d4SFlorian Westphal 	if (!strcasecmp(mode, "sendfile"))
1196048d19d4SFlorian Westphal 		return CFG_MODE_SENDFILE;
1197048d19d4SFlorian Westphal 
1198048d19d4SFlorian Westphal 	fprintf(stderr, "Unknown test mode: %s\n", mode);
1199048d19d4SFlorian Westphal 	fprintf(stderr, "Supported modes are:\n");
1200048d19d4SFlorian Westphal 	fprintf(stderr, "\t\t\"poll\" - interleaved read/write using poll()\n");
1201048d19d4SFlorian Westphal 	fprintf(stderr, "\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n");
1202048d19d4SFlorian Westphal 	fprintf(stderr, "\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n");
1203048d19d4SFlorian Westphal 
1204048d19d4SFlorian Westphal 	die_usage();
1205048d19d4SFlorian Westphal 
1206048d19d4SFlorian Westphal 	/* silence compiler warning */
1207048d19d4SFlorian Westphal 	return 0;
1208048d19d4SFlorian Westphal }
1209048d19d4SFlorian Westphal 
1210df8aee6dSYonglong Li int parse_peek(const char *mode)
1211df8aee6dSYonglong Li {
1212df8aee6dSYonglong Li 	if (!strcasecmp(mode, "saveWithPeek"))
1213df8aee6dSYonglong Li 		return CFG_WITH_PEEK;
1214df8aee6dSYonglong Li 	if (!strcasecmp(mode, "saveAfterPeek"))
1215df8aee6dSYonglong Li 		return CFG_AFTER_PEEK;
1216df8aee6dSYonglong Li 
1217df8aee6dSYonglong Li 	fprintf(stderr, "Unknown: %s\n", mode);
1218df8aee6dSYonglong Li 	fprintf(stderr, "Supported MSG_PEEK mode are:\n");
1219df8aee6dSYonglong Li 	fprintf(stderr,
1220df8aee6dSYonglong Li 		"\t\t\"saveWithPeek\" - recv data with flags 'MSG_PEEK' and save the peek data into file\n");
1221df8aee6dSYonglong Li 	fprintf(stderr,
1222df8aee6dSYonglong Li 		"\t\t\"saveAfterPeek\" - read and save data into file after recv with flags 'MSG_PEEK'\n");
1223df8aee6dSYonglong Li 
1224df8aee6dSYonglong Li 	die_usage();
1225df8aee6dSYonglong Li 
1226df8aee6dSYonglong Li 	/* silence compiler warning */
1227df8aee6dSYonglong Li 	return 0;
1228df8aee6dSYonglong Li }
1229df8aee6dSYonglong Li 
12308a4b910dSFlorian Westphal static int parse_int(const char *size)
1231048d19d4SFlorian Westphal {
1232048d19d4SFlorian Westphal 	unsigned long s;
1233048d19d4SFlorian Westphal 
1234048d19d4SFlorian Westphal 	errno = 0;
1235048d19d4SFlorian Westphal 
1236048d19d4SFlorian Westphal 	s = strtoul(size, NULL, 0);
1237048d19d4SFlorian Westphal 
1238048d19d4SFlorian Westphal 	if (errno) {
1239048d19d4SFlorian Westphal 		fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1240048d19d4SFlorian Westphal 			size, strerror(errno));
1241048d19d4SFlorian Westphal 		die_usage();
1242048d19d4SFlorian Westphal 	}
1243048d19d4SFlorian Westphal 
1244048d19d4SFlorian Westphal 	if (s > INT_MAX) {
1245048d19d4SFlorian Westphal 		fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1246048d19d4SFlorian Westphal 			size, strerror(ERANGE));
1247048d19d4SFlorian Westphal 		die_usage();
1248048d19d4SFlorian Westphal 	}
1249048d19d4SFlorian Westphal 
12508a4b910dSFlorian Westphal 	return (int)s;
1251048d19d4SFlorian Westphal }
1252048d19d4SFlorian Westphal 
1253048d19d4SFlorian Westphal static void parse_opts(int argc, char **argv)
1254048d19d4SFlorian Westphal {
1255048d19d4SFlorian Westphal 	int c;
1256048d19d4SFlorian Westphal 
1257*05be5e27SPaolo Abeni 	while ((c = getopt(argc, argv, "6c:hi:I:jlm:M:o:p:P:r:R:s:S:t:T:w:")) != -1) {
1258048d19d4SFlorian Westphal 		switch (c) {
1259b08fbf24SPaolo Abeni 		case 'j':
1260b08fbf24SPaolo Abeni 			cfg_join = true;
1261b08fbf24SPaolo Abeni 			cfg_mode = CFG_MODE_POLL;
1262b08fbf24SPaolo Abeni 			break;
126313153324SGeliang Tang 		case 'r':
126413153324SGeliang Tang 			cfg_remove = true;
126513153324SGeliang Tang 			cfg_mode = CFG_MODE_POLL;
126613153324SGeliang Tang 			cfg_wait = 400000;
12672e580a63SGeliang Tang 			cfg_do_w = atoi(optarg);
12682e580a63SGeliang Tang 			if (cfg_do_w <= 0)
12692e580a63SGeliang Tang 				cfg_do_w = 50;
127013153324SGeliang Tang 			break;
1271*05be5e27SPaolo Abeni 		case 'i':
1272*05be5e27SPaolo Abeni 			cfg_input = optarg;
1273*05be5e27SPaolo Abeni 			break;
1274*05be5e27SPaolo Abeni 		case 'I':
1275*05be5e27SPaolo Abeni 			cfg_repeat = atoi(optarg);
1276*05be5e27SPaolo Abeni 			break;
1277048d19d4SFlorian Westphal 		case 'l':
1278048d19d4SFlorian Westphal 			listen_mode = true;
1279048d19d4SFlorian Westphal 			break;
1280048d19d4SFlorian Westphal 		case 'p':
1281048d19d4SFlorian Westphal 			cfg_port = optarg;
1282048d19d4SFlorian Westphal 			break;
1283048d19d4SFlorian Westphal 		case 's':
1284048d19d4SFlorian Westphal 			cfg_sock_proto = parse_proto(optarg);
1285048d19d4SFlorian Westphal 			break;
1286048d19d4SFlorian Westphal 		case 'h':
1287048d19d4SFlorian Westphal 			die_usage();
1288048d19d4SFlorian Westphal 			break;
1289048d19d4SFlorian Westphal 		case '6':
1290048d19d4SFlorian Westphal 			pf = AF_INET6;
1291048d19d4SFlorian Westphal 			break;
1292048d19d4SFlorian Westphal 		case 't':
1293048d19d4SFlorian Westphal 			poll_timeout = atoi(optarg) * 1000;
1294048d19d4SFlorian Westphal 			if (poll_timeout <= 0)
1295048d19d4SFlorian Westphal 				poll_timeout = -1;
1296048d19d4SFlorian Westphal 			break;
1297b6ab64b0SPaolo Abeni 		case 'T':
1298b6ab64b0SPaolo Abeni 			cfg_time = atoi(optarg);
1299b6ab64b0SPaolo Abeni 			break;
1300048d19d4SFlorian Westphal 		case 'm':
1301048d19d4SFlorian Westphal 			cfg_mode = parse_mode(optarg);
1302048d19d4SFlorian Westphal 			break;
13038a4b910dSFlorian Westphal 		case 'S':
13048a4b910dSFlorian Westphal 			cfg_sndbuf = parse_int(optarg);
13058a4b910dSFlorian Westphal 			break;
13068a4b910dSFlorian Westphal 		case 'R':
13078a4b910dSFlorian Westphal 			cfg_rcvbuf = parse_int(optarg);
1308048d19d4SFlorian Westphal 			break;
1309df62f2ecSPaolo Abeni 		case 'w':
1310df62f2ecSPaolo Abeni 			cfg_wait = atoi(optarg)*1000000;
1311df62f2ecSPaolo Abeni 			break;
1312dc65fe82SFlorian Westphal 		case 'M':
1313dc65fe82SFlorian Westphal 			cfg_mark = strtol(optarg, NULL, 0);
1314dc65fe82SFlorian Westphal 			break;
1315df8aee6dSYonglong Li 		case 'P':
1316df8aee6dSYonglong Li 			cfg_peek = parse_peek(optarg);
1317df8aee6dSYonglong Li 			break;
13185e6af0a7SFlorian Westphal 		case 'c':
13195e6af0a7SFlorian Westphal 			parse_cmsg_types(optarg);
13205e6af0a7SFlorian Westphal 			break;
13215fb62e9cSFlorian Westphal 		case 'o':
13225fb62e9cSFlorian Westphal 			parse_setsock_options(optarg);
13235fb62e9cSFlorian Westphal 			break;
1324048d19d4SFlorian Westphal 		}
1325048d19d4SFlorian Westphal 	}
1326048d19d4SFlorian Westphal 
1327048d19d4SFlorian Westphal 	if (optind + 1 != argc)
1328048d19d4SFlorian Westphal 		die_usage();
1329048d19d4SFlorian Westphal 	cfg_host = argv[optind];
1330048d19d4SFlorian Westphal 
1331048d19d4SFlorian Westphal 	if (strchr(cfg_host, ':'))
1332048d19d4SFlorian Westphal 		pf = AF_INET6;
1333048d19d4SFlorian Westphal }
1334048d19d4SFlorian Westphal 
1335048d19d4SFlorian Westphal int main(int argc, char *argv[])
1336048d19d4SFlorian Westphal {
1337048d19d4SFlorian Westphal 	init_rng();
1338048d19d4SFlorian Westphal 
1339df62f2ecSPaolo Abeni 	signal(SIGUSR1, handle_signal);
1340048d19d4SFlorian Westphal 	parse_opts(argc, argv);
1341048d19d4SFlorian Westphal 
1342048d19d4SFlorian Westphal 	if (listen_mode) {
1343048d19d4SFlorian Westphal 		int fd = sock_listen_mptcp(cfg_host, cfg_port);
1344048d19d4SFlorian Westphal 
1345048d19d4SFlorian Westphal 		if (fd < 0)
1346048d19d4SFlorian Westphal 			return 1;
1347048d19d4SFlorian Westphal 
13488a4b910dSFlorian Westphal 		if (cfg_rcvbuf)
13498a4b910dSFlorian Westphal 			set_rcvbuf(fd, cfg_rcvbuf);
1350048d19d4SFlorian Westphal 		if (cfg_sndbuf)
1351048d19d4SFlorian Westphal 			set_sndbuf(fd, cfg_sndbuf);
1352dc65fe82SFlorian Westphal 		if (cfg_mark)
1353dc65fe82SFlorian Westphal 			set_mark(fd, cfg_mark);
13545e6af0a7SFlorian Westphal 		if (cfg_cmsg_types.cmsg_enabled)
13555e6af0a7SFlorian Westphal 			apply_cmsg_types(fd, &cfg_cmsg_types);
1356048d19d4SFlorian Westphal 
1357048d19d4SFlorian Westphal 		return main_loop_s(fd);
1358048d19d4SFlorian Westphal 	}
1359048d19d4SFlorian Westphal 
1360048d19d4SFlorian Westphal 	return main_loop();
1361048d19d4SFlorian Westphal }
1362