xref: /openbmc/linux/tools/testing/selftests/net/mptcp/mptcp_connect.c (revision 360823a09426347ea8f232b0b0b5156d0aed0302)
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 
1905be5e27SPaolo Abeni #include <sys/ioctl.h>
20048d19d4SFlorian Westphal #include <sys/poll.h>
2120bb3a96SWillem de Bruijn #include <sys/random.h>
22048d19d4SFlorian Westphal #include <sys/sendfile.h>
23048d19d4SFlorian Westphal #include <sys/stat.h>
24048d19d4SFlorian Westphal #include <sys/socket.h>
25048d19d4SFlorian Westphal #include <sys/types.h>
26048d19d4SFlorian Westphal #include <sys/mman.h>
27048d19d4SFlorian Westphal 
2875deec40SPaolo Abeni #include <arpa/inet.h>
2975deec40SPaolo Abeni 
30048d19d4SFlorian Westphal #include <netdb.h>
31048d19d4SFlorian Westphal #include <netinet/in.h>
32048d19d4SFlorian Westphal 
33048d19d4SFlorian Westphal #include <linux/tcp.h>
345e6af0a7SFlorian Westphal #include <linux/time_types.h>
3505be5e27SPaolo Abeni #include <linux/sockios.h>
36048d19d4SFlorian Westphal 
37048d19d4SFlorian Westphal extern int optind;
38048d19d4SFlorian Westphal 
39048d19d4SFlorian Westphal #ifndef IPPROTO_MPTCP
40048d19d4SFlorian Westphal #define IPPROTO_MPTCP 262
41048d19d4SFlorian Westphal #endif
42048d19d4SFlorian Westphal #ifndef TCP_ULP
43048d19d4SFlorian Westphal #define TCP_ULP 31
44048d19d4SFlorian Westphal #endif
45048d19d4SFlorian Westphal 
468a4b910dSFlorian Westphal static int  poll_timeout = 10 * 1000;
47048d19d4SFlorian Westphal static bool listen_mode;
48df62f2ecSPaolo Abeni static bool quit;
49048d19d4SFlorian Westphal 
50048d19d4SFlorian Westphal enum cfg_mode {
51048d19d4SFlorian Westphal 	CFG_MODE_POLL,
52048d19d4SFlorian Westphal 	CFG_MODE_MMAP,
53048d19d4SFlorian Westphal 	CFG_MODE_SENDFILE,
54048d19d4SFlorian Westphal };
55048d19d4SFlorian Westphal 
56df8aee6dSYonglong Li enum cfg_peek {
57df8aee6dSYonglong Li 	CFG_NONE_PEEK,
58df8aee6dSYonglong Li 	CFG_WITH_PEEK,
59df8aee6dSYonglong Li 	CFG_AFTER_PEEK,
60df8aee6dSYonglong Li };
61df8aee6dSYonglong Li 
62048d19d4SFlorian Westphal static enum cfg_mode cfg_mode = CFG_MODE_POLL;
63df8aee6dSYonglong Li static enum cfg_peek cfg_peek = CFG_NONE_PEEK;
64048d19d4SFlorian Westphal static const char *cfg_host;
65048d19d4SFlorian Westphal static const char *cfg_port	= "12000";
66048d19d4SFlorian Westphal static int cfg_sock_proto	= IPPROTO_MPTCP;
67048d19d4SFlorian Westphal static int pf = AF_INET;
68048d19d4SFlorian Westphal static int cfg_sndbuf;
698a4b910dSFlorian Westphal static int cfg_rcvbuf;
70b08fbf24SPaolo Abeni static bool cfg_join;
7113153324SGeliang Tang static bool cfg_remove;
72b6ab64b0SPaolo Abeni static unsigned int cfg_time;
732e580a63SGeliang Tang static unsigned int cfg_do_w;
74df62f2ecSPaolo Abeni static int cfg_wait;
75dc65fe82SFlorian Westphal static uint32_t cfg_mark;
7605be5e27SPaolo Abeni static char *cfg_input;
7705be5e27SPaolo Abeni static int cfg_repeat = 1;
786bf41020SPaolo Abeni static int cfg_truncate;
796bf41020SPaolo Abeni static int cfg_rcv_trunc;
80048d19d4SFlorian Westphal 
815e6af0a7SFlorian Westphal struct cfg_cmsg_types {
825e6af0a7SFlorian Westphal 	unsigned int cmsg_enabled:1;
835e6af0a7SFlorian Westphal 	unsigned int timestampns:1;
845cbd886cSFlorian Westphal 	unsigned int tcp_inq:1;
855e6af0a7SFlorian Westphal };
865e6af0a7SFlorian Westphal 
875fb62e9cSFlorian Westphal struct cfg_sockopt_types {
885fb62e9cSFlorian Westphal 	unsigned int transparent:1;
89ca7ae891SDmytro Shytyi 	unsigned int mptfo:1;
905fb62e9cSFlorian Westphal };
915fb62e9cSFlorian Westphal 
925cbd886cSFlorian Westphal struct tcp_inq_state {
935cbd886cSFlorian Westphal 	unsigned int last;
945cbd886cSFlorian Westphal 	bool expect_eof;
955cbd886cSFlorian Westphal };
965cbd886cSFlorian Westphal 
97ca7ae891SDmytro Shytyi struct wstate {
98ca7ae891SDmytro Shytyi 	char buf[8192];
99ca7ae891SDmytro Shytyi 	unsigned int len;
100ca7ae891SDmytro Shytyi 	unsigned int off;
101ca7ae891SDmytro Shytyi 	unsigned int total_len;
102ca7ae891SDmytro Shytyi };
103ca7ae891SDmytro Shytyi 
1045cbd886cSFlorian Westphal static struct tcp_inq_state tcp_inq;
1055cbd886cSFlorian Westphal 
1065e6af0a7SFlorian Westphal static struct cfg_cmsg_types cfg_cmsg_types;
1075fb62e9cSFlorian Westphal static struct cfg_sockopt_types cfg_sockopt_types;
1085e6af0a7SFlorian Westphal 
die_usage(void)109048d19d4SFlorian Westphal static void die_usage(void)
110048d19d4SFlorian Westphal {
1116bf41020SPaolo Abeni 	fprintf(stderr, "Usage: mptcp_connect [-6] [-c cmsg] [-f offset] [-i file] [-I num] [-j] [-l] "
1120a85264eSMatthieu Baerts 		"[-m mode] [-M mark] [-o option] [-p port] [-P mode] [-r num] [-R num] "
1130a85264eSMatthieu Baerts 		"[-s MPTCP|TCP] [-S num] [-t num] [-T num] [-w sec] connect_address\n");
1148a4b910dSFlorian Westphal 	fprintf(stderr, "\t-6 use ipv6\n");
11505be5e27SPaolo Abeni 	fprintf(stderr, "\t-c cmsg -- test cmsg type <cmsg>\n");
1166bf41020SPaolo Abeni 	fprintf(stderr, "\t-f offset -- stop the I/O after receiving and sending the specified amount "
1176bf41020SPaolo Abeni 		"of bytes. If there are unread bytes in the receive queue, that will cause a MPTCP "
1186bf41020SPaolo Abeni 		"fastclose at close/shutdown. If offset is negative, expect the peer to close before "
1196bf41020SPaolo Abeni 		"all the local data as been sent, thus toleration errors on write and EPIPE signals\n");
12005be5e27SPaolo Abeni 	fprintf(stderr, "\t-i file -- read the data to send from the given file instead of stdin");
12105be5e27SPaolo Abeni 	fprintf(stderr, "\t-I num -- repeat the transfer 'num' times. In listen mode accepts num "
12205be5e27SPaolo Abeni 		"incoming connections, in client mode, disconnect and reconnect to the server\n");
12305be5e27SPaolo Abeni 	fprintf(stderr, "\t-j     -- add additional sleep at connection start and tear down "
12405be5e27SPaolo Abeni 		"-- for MPJ tests\n");
12505be5e27SPaolo Abeni 	fprintf(stderr, "\t-l     -- listens mode, accepts incoming connection\n");
126c6f4c2b0SDavide Caratti 	fprintf(stderr, "\t-m [poll|mmap|sendfile] -- use poll(default)/mmap+write/sendfile\n");
127dc65fe82SFlorian Westphal 	fprintf(stderr, "\t-M mark -- set socket packet mark\n");
1285fb62e9cSFlorian Westphal 	fprintf(stderr, "\t-o option -- test sockopt <option>\n");
12905be5e27SPaolo Abeni 	fprintf(stderr, "\t-p num -- use port num\n");
130df8aee6dSYonglong Li 	fprintf(stderr,
131df8aee6dSYonglong Li 		"\t-P [saveWithPeek|saveAfterPeek] -- save data with/after MSG_PEEK form tcp socket\n");
13205be5e27SPaolo Abeni 	fprintf(stderr, "\t-r num -- enable slow mode, limiting each write to num bytes "
13305be5e27SPaolo Abeni 		"-- for remove addr tests\n");
13405be5e27SPaolo Abeni 	fprintf(stderr, "\t-R num -- set SO_RCVBUF to num\n");
13505be5e27SPaolo Abeni 	fprintf(stderr, "\t-s [MPTCP|TCP] -- use mptcp(default) or tcp sockets\n");
13605be5e27SPaolo Abeni 	fprintf(stderr, "\t-S num -- set SO_SNDBUF to num\n");
1370a85264eSMatthieu Baerts 	fprintf(stderr, "\t-t num -- set poll timeout to num\n");
1380a85264eSMatthieu Baerts 	fprintf(stderr, "\t-T num -- set expected runtime to num ms\n");
13905be5e27SPaolo Abeni 	fprintf(stderr, "\t-w num -- wait num sec before closing the socket\n");
140048d19d4SFlorian Westphal 	exit(1);
141048d19d4SFlorian Westphal }
142048d19d4SFlorian Westphal 
xerror(const char * fmt,...)1435e6af0a7SFlorian Westphal static void xerror(const char *fmt, ...)
1445e6af0a7SFlorian Westphal {
1455e6af0a7SFlorian Westphal 	va_list ap;
1465e6af0a7SFlorian Westphal 
1475e6af0a7SFlorian Westphal 	va_start(ap, fmt);
1485e6af0a7SFlorian Westphal 	vfprintf(stderr, fmt, ap);
1495e6af0a7SFlorian Westphal 	va_end(ap);
1505e6af0a7SFlorian Westphal 	exit(1);
1515e6af0a7SFlorian Westphal }
1525e6af0a7SFlorian Westphal 
handle_signal(int nr)153df62f2ecSPaolo Abeni static void handle_signal(int nr)
154df62f2ecSPaolo Abeni {
155df62f2ecSPaolo Abeni 	quit = true;
156df62f2ecSPaolo Abeni }
157df62f2ecSPaolo Abeni 
getxinfo_strerr(int err)158048d19d4SFlorian Westphal static const char *getxinfo_strerr(int err)
159048d19d4SFlorian Westphal {
160048d19d4SFlorian Westphal 	if (err == EAI_SYSTEM)
161048d19d4SFlorian Westphal 		return strerror(errno);
162048d19d4SFlorian Westphal 
163048d19d4SFlorian Westphal 	return gai_strerror(err);
164048d19d4SFlorian Westphal }
165048d19d4SFlorian Westphal 
xgetnameinfo(const struct sockaddr * addr,socklen_t addrlen,char * host,socklen_t hostlen,char * serv,socklen_t servlen)166048d19d4SFlorian Westphal static void xgetnameinfo(const struct sockaddr *addr, socklen_t addrlen,
167048d19d4SFlorian Westphal 			 char *host, socklen_t hostlen,
168048d19d4SFlorian Westphal 			 char *serv, socklen_t servlen)
169048d19d4SFlorian Westphal {
170048d19d4SFlorian Westphal 	int flags = NI_NUMERICHOST | NI_NUMERICSERV;
171048d19d4SFlorian Westphal 	int err = getnameinfo(addr, addrlen, host, hostlen, serv, servlen,
172048d19d4SFlorian Westphal 			      flags);
173048d19d4SFlorian Westphal 
174048d19d4SFlorian Westphal 	if (err) {
175048d19d4SFlorian Westphal 		const char *errstr = getxinfo_strerr(err);
176048d19d4SFlorian Westphal 
177048d19d4SFlorian Westphal 		fprintf(stderr, "Fatal: getnameinfo: %s\n", errstr);
178048d19d4SFlorian Westphal 		exit(1);
179048d19d4SFlorian Westphal 	}
180048d19d4SFlorian Westphal }
181048d19d4SFlorian Westphal 
xgetaddrinfo(const char * node,const char * service,const struct addrinfo * hints,struct addrinfo ** res)182048d19d4SFlorian Westphal static void xgetaddrinfo(const char *node, const char *service,
183048d19d4SFlorian Westphal 			 const struct addrinfo *hints,
184048d19d4SFlorian Westphal 			 struct addrinfo **res)
185048d19d4SFlorian Westphal {
186048d19d4SFlorian Westphal 	int err = getaddrinfo(node, service, hints, res);
187048d19d4SFlorian Westphal 
188048d19d4SFlorian Westphal 	if (err) {
189048d19d4SFlorian Westphal 		const char *errstr = getxinfo_strerr(err);
190048d19d4SFlorian Westphal 
191048d19d4SFlorian Westphal 		fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n",
192048d19d4SFlorian Westphal 			node ? node : "", service ? service : "", errstr);
193048d19d4SFlorian Westphal 		exit(1);
194048d19d4SFlorian Westphal 	}
195048d19d4SFlorian Westphal }
196048d19d4SFlorian Westphal 
set_rcvbuf(int fd,unsigned int size)1978a4b910dSFlorian Westphal static void set_rcvbuf(int fd, unsigned int size)
1988a4b910dSFlorian Westphal {
1998a4b910dSFlorian Westphal 	int err;
2008a4b910dSFlorian Westphal 
2018a4b910dSFlorian Westphal 	err = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
2028a4b910dSFlorian Westphal 	if (err) {
2038a4b910dSFlorian Westphal 		perror("set SO_RCVBUF");
2048a4b910dSFlorian Westphal 		exit(1);
2058a4b910dSFlorian Westphal 	}
2068a4b910dSFlorian Westphal }
2078a4b910dSFlorian Westphal 
set_sndbuf(int fd,unsigned int size)208048d19d4SFlorian Westphal static void set_sndbuf(int fd, unsigned int size)
209048d19d4SFlorian Westphal {
210048d19d4SFlorian Westphal 	int err;
211048d19d4SFlorian Westphal 
212048d19d4SFlorian Westphal 	err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
213048d19d4SFlorian Westphal 	if (err) {
214048d19d4SFlorian Westphal 		perror("set SO_SNDBUF");
215048d19d4SFlorian Westphal 		exit(1);
216048d19d4SFlorian Westphal 	}
217048d19d4SFlorian Westphal }
218048d19d4SFlorian Westphal 
set_mark(int fd,uint32_t mark)219dc65fe82SFlorian Westphal static void set_mark(int fd, uint32_t mark)
220dc65fe82SFlorian Westphal {
221dc65fe82SFlorian Westphal 	int err;
222dc65fe82SFlorian Westphal 
223dc65fe82SFlorian Westphal 	err = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
224dc65fe82SFlorian Westphal 	if (err) {
225dc65fe82SFlorian Westphal 		perror("set SO_MARK");
226dc65fe82SFlorian Westphal 		exit(1);
227dc65fe82SFlorian Westphal 	}
228dc65fe82SFlorian Westphal }
229dc65fe82SFlorian Westphal 
set_transparent(int fd,int pf)2305fb62e9cSFlorian Westphal static void set_transparent(int fd, int pf)
2315fb62e9cSFlorian Westphal {
2325fb62e9cSFlorian Westphal 	int one = 1;
2335fb62e9cSFlorian Westphal 
2345fb62e9cSFlorian Westphal 	switch (pf) {
2355fb62e9cSFlorian Westphal 	case AF_INET:
2365fb62e9cSFlorian Westphal 		if (-1 == setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)))
2375fb62e9cSFlorian Westphal 			perror("IP_TRANSPARENT");
2385fb62e9cSFlorian Westphal 		break;
2395fb62e9cSFlorian Westphal 	case AF_INET6:
2405fb62e9cSFlorian Westphal 		if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)))
2415fb62e9cSFlorian Westphal 			perror("IPV6_TRANSPARENT");
2425fb62e9cSFlorian Westphal 		break;
2435fb62e9cSFlorian Westphal 	}
2445fb62e9cSFlorian Westphal }
2455fb62e9cSFlorian Westphal 
set_mptfo(int fd,int pf)246ca7ae891SDmytro Shytyi static void set_mptfo(int fd, int pf)
247ca7ae891SDmytro Shytyi {
248ca7ae891SDmytro Shytyi 	int qlen = 25;
249ca7ae891SDmytro Shytyi 
250ca7ae891SDmytro Shytyi 	if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1)
251ca7ae891SDmytro Shytyi 		perror("TCP_FASTOPEN");
252ca7ae891SDmytro Shytyi }
253ca7ae891SDmytro Shytyi 
do_ulp_so(int sock,const char * name)254f730b65cSFlorian Westphal static int do_ulp_so(int sock, const char *name)
255f730b65cSFlorian Westphal {
256f730b65cSFlorian Westphal 	return setsockopt(sock, IPPROTO_TCP, TCP_ULP, name, strlen(name));
257f730b65cSFlorian Westphal }
258f730b65cSFlorian Westphal 
259f730b65cSFlorian Westphal #define X(m)	xerror("%s:%u: %s: failed for proto %d at line %u", __FILE__, __LINE__, (m), proto, line)
sock_test_tcpulp(int sock,int proto,unsigned int line)260f730b65cSFlorian Westphal static void sock_test_tcpulp(int sock, int proto, unsigned int line)
261f730b65cSFlorian Westphal {
262f730b65cSFlorian Westphal 	socklen_t buflen = 8;
263f730b65cSFlorian Westphal 	char buf[8] = "";
264f730b65cSFlorian Westphal 	int ret = getsockopt(sock, IPPROTO_TCP, TCP_ULP, buf, &buflen);
265f730b65cSFlorian Westphal 
266f730b65cSFlorian Westphal 	if (ret != 0)
267f730b65cSFlorian Westphal 		X("getsockopt");
268f730b65cSFlorian Westphal 
269f730b65cSFlorian Westphal 	if (buflen > 0) {
270f730b65cSFlorian Westphal 		if (strcmp(buf, "mptcp") != 0)
271f730b65cSFlorian Westphal 			xerror("unexpected ULP '%s' for proto %d at line %u", buf, proto, line);
272f730b65cSFlorian Westphal 		ret = do_ulp_so(sock, "tls");
273f730b65cSFlorian Westphal 		if (ret == 0)
274f730b65cSFlorian Westphal 			X("setsockopt");
275f730b65cSFlorian Westphal 	} else if (proto == IPPROTO_MPTCP) {
276f730b65cSFlorian Westphal 		ret = do_ulp_so(sock, "tls");
277f730b65cSFlorian Westphal 		if (ret != -1)
278f730b65cSFlorian Westphal 			X("setsockopt");
279f730b65cSFlorian Westphal 	}
280f730b65cSFlorian Westphal 
281f730b65cSFlorian Westphal 	ret = do_ulp_so(sock, "mptcp");
282f730b65cSFlorian Westphal 	if (ret != -1)
283f730b65cSFlorian Westphal 		X("setsockopt");
284f730b65cSFlorian Westphal 
285f730b65cSFlorian Westphal #undef X
286f730b65cSFlorian Westphal }
287f730b65cSFlorian Westphal 
288f730b65cSFlorian Westphal #define SOCK_TEST_TCPULP(s, p) sock_test_tcpulp((s), (p), __LINE__)
289f730b65cSFlorian Westphal 
sock_listen_mptcp(const char * const listenaddr,const char * const port)290048d19d4SFlorian Westphal static int sock_listen_mptcp(const char * const listenaddr,
291048d19d4SFlorian Westphal 			     const char * const port)
292048d19d4SFlorian Westphal {
293fd37c2ecSMat Martineau 	int sock = -1;
294048d19d4SFlorian Westphal 	struct addrinfo hints = {
295048d19d4SFlorian Westphal 		.ai_protocol = IPPROTO_TCP,
296048d19d4SFlorian Westphal 		.ai_socktype = SOCK_STREAM,
297048d19d4SFlorian Westphal 		.ai_flags = AI_PASSIVE | AI_NUMERICHOST
298048d19d4SFlorian Westphal 	};
299048d19d4SFlorian Westphal 
300048d19d4SFlorian Westphal 	hints.ai_family = pf;
301048d19d4SFlorian Westphal 
302048d19d4SFlorian Westphal 	struct addrinfo *a, *addr;
303048d19d4SFlorian Westphal 	int one = 1;
304048d19d4SFlorian Westphal 
305048d19d4SFlorian Westphal 	xgetaddrinfo(listenaddr, port, &hints, &addr);
306048d19d4SFlorian Westphal 	hints.ai_family = pf;
307048d19d4SFlorian Westphal 
308048d19d4SFlorian Westphal 	for (a = addr; a; a = a->ai_next) {
309048d19d4SFlorian Westphal 		sock = socket(a->ai_family, a->ai_socktype, cfg_sock_proto);
310048d19d4SFlorian Westphal 		if (sock < 0)
311048d19d4SFlorian Westphal 			continue;
312048d19d4SFlorian Westphal 
313f730b65cSFlorian Westphal 		SOCK_TEST_TCPULP(sock, cfg_sock_proto);
314f730b65cSFlorian Westphal 
315048d19d4SFlorian Westphal 		if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one,
316048d19d4SFlorian Westphal 				     sizeof(one)))
317048d19d4SFlorian Westphal 			perror("setsockopt");
318048d19d4SFlorian Westphal 
3195fb62e9cSFlorian Westphal 		if (cfg_sockopt_types.transparent)
3205fb62e9cSFlorian Westphal 			set_transparent(sock, pf);
3215fb62e9cSFlorian Westphal 
322ca7ae891SDmytro Shytyi 		if (cfg_sockopt_types.mptfo)
323ca7ae891SDmytro Shytyi 			set_mptfo(sock, pf);
324ca7ae891SDmytro Shytyi 
325048d19d4SFlorian Westphal 		if (bind(sock, a->ai_addr, a->ai_addrlen) == 0)
326048d19d4SFlorian Westphal 			break; /* success */
327048d19d4SFlorian Westphal 
328048d19d4SFlorian Westphal 		perror("bind");
329048d19d4SFlorian Westphal 		close(sock);
330048d19d4SFlorian Westphal 		sock = -1;
331048d19d4SFlorian Westphal 	}
332048d19d4SFlorian Westphal 
333048d19d4SFlorian Westphal 	freeaddrinfo(addr);
334048d19d4SFlorian Westphal 
335048d19d4SFlorian Westphal 	if (sock < 0) {
336048d19d4SFlorian Westphal 		fprintf(stderr, "Could not create listen socket\n");
337048d19d4SFlorian Westphal 		return sock;
338048d19d4SFlorian Westphal 	}
339048d19d4SFlorian Westphal 
340f730b65cSFlorian Westphal 	SOCK_TEST_TCPULP(sock, cfg_sock_proto);
341f730b65cSFlorian Westphal 
342048d19d4SFlorian Westphal 	if (listen(sock, 20)) {
343048d19d4SFlorian Westphal 		perror("listen");
344048d19d4SFlorian Westphal 		close(sock);
345048d19d4SFlorian Westphal 		return -1;
346048d19d4SFlorian Westphal 	}
347048d19d4SFlorian Westphal 
348f730b65cSFlorian Westphal 	SOCK_TEST_TCPULP(sock, cfg_sock_proto);
349f730b65cSFlorian Westphal 
350048d19d4SFlorian Westphal 	return sock;
351048d19d4SFlorian Westphal }
352048d19d4SFlorian Westphal 
sock_connect_mptcp(const char * const remoteaddr,const char * const port,int proto,struct addrinfo ** peer,int infd,struct wstate * winfo)353048d19d4SFlorian Westphal static int sock_connect_mptcp(const char * const remoteaddr,
35405be5e27SPaolo Abeni 			      const char * const port, int proto,
355ca7ae891SDmytro Shytyi 			      struct addrinfo **peer,
356ca7ae891SDmytro Shytyi 			      int infd, struct wstate *winfo)
357048d19d4SFlorian Westphal {
358048d19d4SFlorian Westphal 	struct addrinfo hints = {
359048d19d4SFlorian Westphal 		.ai_protocol = IPPROTO_TCP,
360048d19d4SFlorian Westphal 		.ai_socktype = SOCK_STREAM,
361048d19d4SFlorian Westphal 	};
362048d19d4SFlorian Westphal 	struct addrinfo *a, *addr;
363ca7ae891SDmytro Shytyi 	int syn_copied = 0;
364048d19d4SFlorian Westphal 	int sock = -1;
365048d19d4SFlorian Westphal 
366048d19d4SFlorian Westphal 	hints.ai_family = pf;
367048d19d4SFlorian Westphal 
368048d19d4SFlorian Westphal 	xgetaddrinfo(remoteaddr, port, &hints, &addr);
369048d19d4SFlorian Westphal 	for (a = addr; a; a = a->ai_next) {
370048d19d4SFlorian Westphal 		sock = socket(a->ai_family, a->ai_socktype, proto);
371048d19d4SFlorian Westphal 		if (sock < 0) {
372048d19d4SFlorian Westphal 			perror("socket");
373048d19d4SFlorian Westphal 			continue;
374048d19d4SFlorian Westphal 		}
375048d19d4SFlorian Westphal 
376f730b65cSFlorian Westphal 		SOCK_TEST_TCPULP(sock, proto);
377f730b65cSFlorian Westphal 
378dc65fe82SFlorian Westphal 		if (cfg_mark)
379dc65fe82SFlorian Westphal 			set_mark(sock, cfg_mark);
380dc65fe82SFlorian Westphal 
381ca7ae891SDmytro Shytyi 		if (cfg_sockopt_types.mptfo) {
382ca7ae891SDmytro Shytyi 			if (!winfo->total_len)
383ca7ae891SDmytro Shytyi 				winfo->total_len = winfo->len = read(infd, winfo->buf,
384ca7ae891SDmytro Shytyi 								     sizeof(winfo->buf));
385ca7ae891SDmytro Shytyi 
386ca7ae891SDmytro Shytyi 			syn_copied = sendto(sock, winfo->buf, winfo->len, MSG_FASTOPEN,
387ca7ae891SDmytro Shytyi 					    a->ai_addr, a->ai_addrlen);
388ca7ae891SDmytro Shytyi 			if (syn_copied >= 0) {
389ca7ae891SDmytro Shytyi 				winfo->off = syn_copied;
390ca7ae891SDmytro Shytyi 				winfo->len -= syn_copied;
391ca7ae891SDmytro Shytyi 				*peer = a;
392ca7ae891SDmytro Shytyi 				break; /* success */
393ca7ae891SDmytro Shytyi 			}
394ca7ae891SDmytro Shytyi 		} else {
39505be5e27SPaolo Abeni 			if (connect(sock, a->ai_addr, a->ai_addrlen) == 0) {
39605be5e27SPaolo Abeni 				*peer = a;
397048d19d4SFlorian Westphal 				break; /* success */
39805be5e27SPaolo Abeni 			}
399ca7ae891SDmytro Shytyi 		}
400ca7ae891SDmytro Shytyi 		if (cfg_sockopt_types.mptfo) {
401ca7ae891SDmytro Shytyi 			perror("sendto()");
402ca7ae891SDmytro Shytyi 			close(sock);
403ca7ae891SDmytro Shytyi 			sock = -1;
404ca7ae891SDmytro Shytyi 		} else {
405048d19d4SFlorian Westphal 			perror("connect()");
406048d19d4SFlorian Westphal 			close(sock);
407048d19d4SFlorian Westphal 			sock = -1;
408048d19d4SFlorian Westphal 		}
409ca7ae891SDmytro Shytyi 	}
410048d19d4SFlorian Westphal 
411048d19d4SFlorian Westphal 	freeaddrinfo(addr);
412f730b65cSFlorian Westphal 	if (sock != -1)
413f730b65cSFlorian Westphal 		SOCK_TEST_TCPULP(sock, proto);
414048d19d4SFlorian Westphal 	return sock;
415048d19d4SFlorian Westphal }
416048d19d4SFlorian Westphal 
do_rnd_write(const int fd,char * buf,const size_t len)417048d19d4SFlorian Westphal static size_t do_rnd_write(const int fd, char *buf, const size_t len)
418048d19d4SFlorian Westphal {
419b08fbf24SPaolo Abeni 	static bool first = true;
420048d19d4SFlorian Westphal 	unsigned int do_w;
421048d19d4SFlorian Westphal 	ssize_t bw;
422048d19d4SFlorian Westphal 
423048d19d4SFlorian Westphal 	do_w = rand() & 0xffff;
424048d19d4SFlorian Westphal 	if (do_w == 0 || do_w > len)
425048d19d4SFlorian Westphal 		do_w = len;
426048d19d4SFlorian Westphal 
427b08fbf24SPaolo Abeni 	if (cfg_join && first && do_w > 100)
428b08fbf24SPaolo Abeni 		do_w = 100;
429b08fbf24SPaolo Abeni 
4302e580a63SGeliang Tang 	if (cfg_remove && do_w > cfg_do_w)
4312e580a63SGeliang Tang 		do_w = cfg_do_w;
43213153324SGeliang Tang 
433048d19d4SFlorian Westphal 	bw = write(fd, buf, do_w);
434048d19d4SFlorian Westphal 	if (bw < 0)
4356bf41020SPaolo Abeni 		return bw;
436048d19d4SFlorian Westphal 
437b08fbf24SPaolo Abeni 	/* let the join handshake complete, before going on */
438b08fbf24SPaolo Abeni 	if (cfg_join && first) {
439b08fbf24SPaolo Abeni 		usleep(200000);
440b08fbf24SPaolo Abeni 		first = false;
441b08fbf24SPaolo Abeni 	}
442b08fbf24SPaolo Abeni 
44313153324SGeliang Tang 	if (cfg_remove)
44413153324SGeliang Tang 		usleep(200000);
44513153324SGeliang Tang 
446048d19d4SFlorian Westphal 	return bw;
447048d19d4SFlorian Westphal }
448048d19d4SFlorian Westphal 
do_write(const int fd,char * buf,const size_t len)449048d19d4SFlorian Westphal static size_t do_write(const int fd, char *buf, const size_t len)
450048d19d4SFlorian Westphal {
451048d19d4SFlorian Westphal 	size_t offset = 0;
452048d19d4SFlorian Westphal 
453048d19d4SFlorian Westphal 	while (offset < len) {
454048d19d4SFlorian Westphal 		size_t written;
455048d19d4SFlorian Westphal 		ssize_t bw;
456048d19d4SFlorian Westphal 
457048d19d4SFlorian Westphal 		bw = write(fd, buf + offset, len - offset);
458048d19d4SFlorian Westphal 		if (bw < 0) {
459048d19d4SFlorian Westphal 			perror("write");
460048d19d4SFlorian Westphal 			return 0;
461048d19d4SFlorian Westphal 		}
462048d19d4SFlorian Westphal 
463048d19d4SFlorian Westphal 		written = (size_t)bw;
464048d19d4SFlorian Westphal 		offset += written;
465048d19d4SFlorian Westphal 	}
466048d19d4SFlorian Westphal 
467048d19d4SFlorian Westphal 	return offset;
468048d19d4SFlorian Westphal }
469048d19d4SFlorian Westphal 
process_cmsg(struct msghdr * msgh)4705e6af0a7SFlorian Westphal static void process_cmsg(struct msghdr *msgh)
4715e6af0a7SFlorian Westphal {
4725e6af0a7SFlorian Westphal 	struct __kernel_timespec ts;
4735cbd886cSFlorian Westphal 	bool inq_found = false;
4745e6af0a7SFlorian Westphal 	bool ts_found = false;
4755cbd886cSFlorian Westphal 	unsigned int inq = 0;
4765e6af0a7SFlorian Westphal 	struct cmsghdr *cmsg;
4775e6af0a7SFlorian Westphal 
4785e6af0a7SFlorian Westphal 	for (cmsg = CMSG_FIRSTHDR(msgh); cmsg ; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
4795e6af0a7SFlorian Westphal 		if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMPNS_NEW) {
4805e6af0a7SFlorian Westphal 			memcpy(&ts, CMSG_DATA(cmsg), sizeof(ts));
4815e6af0a7SFlorian Westphal 			ts_found = true;
4825e6af0a7SFlorian Westphal 			continue;
4835e6af0a7SFlorian Westphal 		}
4845cbd886cSFlorian Westphal 		if (cmsg->cmsg_level == IPPROTO_TCP && cmsg->cmsg_type == TCP_CM_INQ) {
4855cbd886cSFlorian Westphal 			memcpy(&inq, CMSG_DATA(cmsg), sizeof(inq));
4865cbd886cSFlorian Westphal 			inq_found = true;
4875cbd886cSFlorian Westphal 			continue;
4885cbd886cSFlorian Westphal 		}
4895cbd886cSFlorian Westphal 
4905e6af0a7SFlorian Westphal 	}
4915e6af0a7SFlorian Westphal 
4925e6af0a7SFlorian Westphal 	if (cfg_cmsg_types.timestampns) {
4935e6af0a7SFlorian Westphal 		if (!ts_found)
4945e6af0a7SFlorian Westphal 			xerror("TIMESTAMPNS not present\n");
4955e6af0a7SFlorian Westphal 	}
4965cbd886cSFlorian Westphal 
4975cbd886cSFlorian Westphal 	if (cfg_cmsg_types.tcp_inq) {
4985cbd886cSFlorian Westphal 		if (!inq_found)
4995cbd886cSFlorian Westphal 			xerror("TCP_INQ not present\n");
5005cbd886cSFlorian Westphal 
5015cbd886cSFlorian Westphal 		if (inq > 1024)
5025cbd886cSFlorian Westphal 			xerror("tcp_inq %u is larger than one kbyte\n", inq);
5035cbd886cSFlorian Westphal 		tcp_inq.last = inq;
5045cbd886cSFlorian Westphal 	}
5055e6af0a7SFlorian Westphal }
5065e6af0a7SFlorian Westphal 
do_recvmsg_cmsg(const int fd,char * buf,const size_t len)5075e6af0a7SFlorian Westphal static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len)
5085e6af0a7SFlorian Westphal {
5095e6af0a7SFlorian Westphal 	char msg_buf[8192];
5105e6af0a7SFlorian Westphal 	struct iovec iov = {
5115e6af0a7SFlorian Westphal 		.iov_base = buf,
5125e6af0a7SFlorian Westphal 		.iov_len = len,
5135e6af0a7SFlorian Westphal 	};
5145e6af0a7SFlorian Westphal 	struct msghdr msg = {
5155e6af0a7SFlorian Westphal 		.msg_iov = &iov,
5165e6af0a7SFlorian Westphal 		.msg_iovlen = 1,
5175e6af0a7SFlorian Westphal 		.msg_control = msg_buf,
5185e6af0a7SFlorian Westphal 		.msg_controllen = sizeof(msg_buf),
5195e6af0a7SFlorian Westphal 	};
5205e6af0a7SFlorian Westphal 	int flags = 0;
5215cbd886cSFlorian Westphal 	unsigned int last_hint = tcp_inq.last;
5225e6af0a7SFlorian Westphal 	int ret = recvmsg(fd, &msg, flags);
5235e6af0a7SFlorian Westphal 
5245cbd886cSFlorian Westphal 	if (ret <= 0) {
5255cbd886cSFlorian Westphal 		if (ret == 0 && tcp_inq.expect_eof)
5265e6af0a7SFlorian Westphal 			return ret;
5275e6af0a7SFlorian Westphal 
5285cbd886cSFlorian Westphal 		if (ret == 0 && cfg_cmsg_types.tcp_inq)
5295cbd886cSFlorian Westphal 			if (last_hint != 1 && last_hint != 0)
5305cbd886cSFlorian Westphal 				xerror("EOF but last tcp_inq hint was %u\n", last_hint);
5315cbd886cSFlorian Westphal 
5325cbd886cSFlorian Westphal 		return ret;
5335cbd886cSFlorian Westphal 	}
5345cbd886cSFlorian Westphal 
5355cbd886cSFlorian Westphal 	if (tcp_inq.expect_eof)
5365cbd886cSFlorian Westphal 		xerror("expected EOF, last_hint %u, now %u\n",
5375cbd886cSFlorian Westphal 		       last_hint, tcp_inq.last);
5385cbd886cSFlorian Westphal 
5395e6af0a7SFlorian Westphal 	if (msg.msg_controllen && !cfg_cmsg_types.cmsg_enabled)
5405e6af0a7SFlorian Westphal 		xerror("got %lu bytes of cmsg data, expected 0\n",
5415e6af0a7SFlorian Westphal 		       (unsigned long)msg.msg_controllen);
5425e6af0a7SFlorian Westphal 
5435e6af0a7SFlorian Westphal 	if (msg.msg_controllen == 0 && cfg_cmsg_types.cmsg_enabled)
5445e6af0a7SFlorian Westphal 		xerror("%s\n", "got no cmsg data");
5455e6af0a7SFlorian Westphal 
5465e6af0a7SFlorian Westphal 	if (msg.msg_controllen)
5475e6af0a7SFlorian Westphal 		process_cmsg(&msg);
5485e6af0a7SFlorian Westphal 
5495cbd886cSFlorian Westphal 	if (cfg_cmsg_types.tcp_inq) {
5505cbd886cSFlorian Westphal 		if ((size_t)ret < len && last_hint > (unsigned int)ret) {
5515cbd886cSFlorian Westphal 			if (ret + 1 != (int)last_hint) {
5525cbd886cSFlorian Westphal 				int next = read(fd, msg_buf, sizeof(msg_buf));
5535cbd886cSFlorian Westphal 
5545cbd886cSFlorian Westphal 				xerror("read %u of %u, last_hint was %u tcp_inq hint now %u next_read returned %d/%m\n",
5555cbd886cSFlorian Westphal 				       ret, (unsigned int)len, last_hint, tcp_inq.last, next);
5565cbd886cSFlorian Westphal 			} else {
5575cbd886cSFlorian Westphal 				tcp_inq.expect_eof = true;
5585cbd886cSFlorian Westphal 			}
5595cbd886cSFlorian Westphal 		}
5605cbd886cSFlorian Westphal 	}
5615cbd886cSFlorian Westphal 
5625e6af0a7SFlorian Westphal 	return ret;
5635e6af0a7SFlorian Westphal }
5645e6af0a7SFlorian Westphal 
do_rnd_read(const int fd,char * buf,const size_t len)565048d19d4SFlorian Westphal static ssize_t do_rnd_read(const int fd, char *buf, const size_t len)
566048d19d4SFlorian Westphal {
567df8aee6dSYonglong Li 	int ret = 0;
568df8aee6dSYonglong Li 	char tmp[16384];
569048d19d4SFlorian Westphal 	size_t cap = rand();
570048d19d4SFlorian Westphal 
571048d19d4SFlorian Westphal 	cap &= 0xffff;
572048d19d4SFlorian Westphal 
573048d19d4SFlorian Westphal 	if (cap == 0)
574048d19d4SFlorian Westphal 		cap = 1;
575048d19d4SFlorian Westphal 	else if (cap > len)
576048d19d4SFlorian Westphal 		cap = len;
577048d19d4SFlorian Westphal 
578df8aee6dSYonglong Li 	if (cfg_peek == CFG_WITH_PEEK) {
579df8aee6dSYonglong Li 		ret = recv(fd, buf, cap, MSG_PEEK);
580df8aee6dSYonglong Li 		ret = (ret < 0) ? ret : read(fd, tmp, ret);
581df8aee6dSYonglong Li 	} else if (cfg_peek == CFG_AFTER_PEEK) {
582df8aee6dSYonglong Li 		ret = recv(fd, buf, cap, MSG_PEEK);
583df8aee6dSYonglong Li 		ret = (ret < 0) ? ret : read(fd, buf, cap);
5845e6af0a7SFlorian Westphal 	} else if (cfg_cmsg_types.cmsg_enabled) {
5855e6af0a7SFlorian Westphal 		ret = do_recvmsg_cmsg(fd, buf, cap);
586df8aee6dSYonglong Li 	} else {
587df8aee6dSYonglong Li 		ret = read(fd, buf, cap);
588df8aee6dSYonglong Li 	}
589df8aee6dSYonglong Li 
590df8aee6dSYonglong Li 	return ret;
591048d19d4SFlorian Westphal }
592048d19d4SFlorian Westphal 
set_nonblock(int fd,bool nonblock)59305be5e27SPaolo Abeni static void set_nonblock(int fd, bool nonblock)
594048d19d4SFlorian Westphal {
595048d19d4SFlorian Westphal 	int flags = fcntl(fd, F_GETFL);
596048d19d4SFlorian Westphal 
597048d19d4SFlorian Westphal 	if (flags == -1)
598048d19d4SFlorian Westphal 		return;
599048d19d4SFlorian Westphal 
60005be5e27SPaolo Abeni 	if (nonblock)
601048d19d4SFlorian Westphal 		fcntl(fd, F_SETFL, flags | O_NONBLOCK);
60205be5e27SPaolo Abeni 	else
60305be5e27SPaolo Abeni 		fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
604048d19d4SFlorian Westphal }
605048d19d4SFlorian Westphal 
shut_wr(int fd)606df9e03aeSFlorian Westphal static void shut_wr(int fd)
607df9e03aeSFlorian Westphal {
608df9e03aeSFlorian Westphal 	/* Close our write side, ev. give some time
609df9e03aeSFlorian Westphal 	 * for address notification and/or checking
610df9e03aeSFlorian Westphal 	 * the current status
611df9e03aeSFlorian Westphal 	 */
612df9e03aeSFlorian Westphal 	if (cfg_wait)
613df9e03aeSFlorian Westphal 		usleep(cfg_wait);
614df9e03aeSFlorian Westphal 
615df9e03aeSFlorian Westphal 	shutdown(fd, SHUT_WR);
616df9e03aeSFlorian Westphal }
617df9e03aeSFlorian Westphal 
copyfd_io_poll(int infd,int peerfd,int outfd,bool * in_closed_after_out,struct wstate * winfo)618ca7ae891SDmytro Shytyi static int copyfd_io_poll(int infd, int peerfd, int outfd,
619ca7ae891SDmytro Shytyi 			  bool *in_closed_after_out, struct wstate *winfo)
620048d19d4SFlorian Westphal {
621048d19d4SFlorian Westphal 	struct pollfd fds = {
622048d19d4SFlorian Westphal 		.fd = peerfd,
623048d19d4SFlorian Westphal 		.events = POLLIN | POLLOUT,
624048d19d4SFlorian Westphal 	};
625ca7ae891SDmytro Shytyi 	unsigned int total_wlen = 0, total_rlen = 0;
626048d19d4SFlorian Westphal 
62705be5e27SPaolo Abeni 	set_nonblock(peerfd, true);
628048d19d4SFlorian Westphal 
629048d19d4SFlorian Westphal 	for (;;) {
630048d19d4SFlorian Westphal 		char rbuf[8192];
631048d19d4SFlorian Westphal 		ssize_t len;
632048d19d4SFlorian Westphal 
6334a753ca5SMenglong Dong 		if (fds.events == 0 || quit)
634048d19d4SFlorian Westphal 			break;
635048d19d4SFlorian Westphal 
636048d19d4SFlorian Westphal 		switch (poll(&fds, 1, poll_timeout)) {
637048d19d4SFlorian Westphal 		case -1:
638048d19d4SFlorian Westphal 			if (errno == EINTR)
639048d19d4SFlorian Westphal 				continue;
640048d19d4SFlorian Westphal 			perror("poll");
641048d19d4SFlorian Westphal 			return 1;
642048d19d4SFlorian Westphal 		case 0:
643048d19d4SFlorian Westphal 			fprintf(stderr, "%s: poll timed out (events: "
644048d19d4SFlorian Westphal 				"POLLIN %u, POLLOUT %u)\n", __func__,
645048d19d4SFlorian Westphal 				fds.events & POLLIN, fds.events & POLLOUT);
646048d19d4SFlorian Westphal 			return 2;
647048d19d4SFlorian Westphal 		}
648048d19d4SFlorian Westphal 
649048d19d4SFlorian Westphal 		if (fds.revents & POLLIN) {
6506bf41020SPaolo Abeni 			ssize_t rb = sizeof(rbuf);
6516bf41020SPaolo Abeni 
6526bf41020SPaolo Abeni 			/* limit the total amount of read data to the trunc value*/
6536bf41020SPaolo Abeni 			if (cfg_truncate > 0) {
6546bf41020SPaolo Abeni 				if (rb + total_rlen > cfg_truncate)
6556bf41020SPaolo Abeni 					rb = cfg_truncate - total_rlen;
6566bf41020SPaolo Abeni 				len = read(peerfd, rbuf, rb);
6576bf41020SPaolo Abeni 			} else {
658048d19d4SFlorian Westphal 				len = do_rnd_read(peerfd, rbuf, sizeof(rbuf));
6596bf41020SPaolo Abeni 			}
660048d19d4SFlorian Westphal 			if (len == 0) {
661048d19d4SFlorian Westphal 				/* no more data to receive:
662048d19d4SFlorian Westphal 				 * peer has closed its write side
663048d19d4SFlorian Westphal 				 */
664048d19d4SFlorian Westphal 				fds.events &= ~POLLIN;
665048d19d4SFlorian Westphal 
666b6ab64b0SPaolo Abeni 				if ((fds.events & POLLOUT) == 0) {
667b6ab64b0SPaolo Abeni 					*in_closed_after_out = true;
668048d19d4SFlorian Westphal 					/* and nothing more to send */
669048d19d4SFlorian Westphal 					break;
670b6ab64b0SPaolo Abeni 				}
671048d19d4SFlorian Westphal 
672048d19d4SFlorian Westphal 			/* Else, still have data to transmit */
673048d19d4SFlorian Westphal 			} else if (len < 0) {
6746bf41020SPaolo Abeni 				if (cfg_rcv_trunc)
6756bf41020SPaolo Abeni 					return 0;
676048d19d4SFlorian Westphal 				perror("read");
677048d19d4SFlorian Westphal 				return 3;
678048d19d4SFlorian Westphal 			}
679048d19d4SFlorian Westphal 
6806bf41020SPaolo Abeni 			total_rlen += len;
681048d19d4SFlorian Westphal 			do_write(outfd, rbuf, len);
682048d19d4SFlorian Westphal 		}
683048d19d4SFlorian Westphal 
684048d19d4SFlorian Westphal 		if (fds.revents & POLLOUT) {
685ca7ae891SDmytro Shytyi 			if (winfo->len == 0) {
686ca7ae891SDmytro Shytyi 				winfo->off = 0;
687ca7ae891SDmytro Shytyi 				winfo->len = read(infd, winfo->buf, sizeof(winfo->buf));
688048d19d4SFlorian Westphal 			}
689048d19d4SFlorian Westphal 
690ca7ae891SDmytro Shytyi 			if (winfo->len > 0) {
691048d19d4SFlorian Westphal 				ssize_t bw;
692048d19d4SFlorian Westphal 
6936bf41020SPaolo Abeni 				/* limit the total amount of written data to the trunc value */
694ca7ae891SDmytro Shytyi 				if (cfg_truncate > 0 && winfo->len + total_wlen > cfg_truncate)
695ca7ae891SDmytro Shytyi 					winfo->len = cfg_truncate - total_wlen;
6966bf41020SPaolo Abeni 
697ca7ae891SDmytro Shytyi 				bw = do_rnd_write(peerfd, winfo->buf + winfo->off, winfo->len);
6986bf41020SPaolo Abeni 				if (bw < 0) {
6996bf41020SPaolo Abeni 					if (cfg_rcv_trunc)
7006bf41020SPaolo Abeni 						return 0;
7016bf41020SPaolo Abeni 					perror("write");
702048d19d4SFlorian Westphal 					return 111;
7036bf41020SPaolo Abeni 				}
704048d19d4SFlorian Westphal 
705ca7ae891SDmytro Shytyi 				winfo->off += bw;
706ca7ae891SDmytro Shytyi 				winfo->len -= bw;
7076bf41020SPaolo Abeni 				total_wlen += bw;
708ca7ae891SDmytro Shytyi 			} else if (winfo->len == 0) {
709048d19d4SFlorian Westphal 				/* We have no more data to send. */
710048d19d4SFlorian Westphal 				fds.events &= ~POLLOUT;
711048d19d4SFlorian Westphal 
712048d19d4SFlorian Westphal 				if ((fds.events & POLLIN) == 0)
713048d19d4SFlorian Westphal 					/* ... and peer also closed already */
714048d19d4SFlorian Westphal 					break;
715048d19d4SFlorian Westphal 
716df9e03aeSFlorian Westphal 				shut_wr(peerfd);
717048d19d4SFlorian Westphal 			} else {
718048d19d4SFlorian Westphal 				if (errno == EINTR)
719048d19d4SFlorian Westphal 					continue;
720048d19d4SFlorian Westphal 				perror("read");
721048d19d4SFlorian Westphal 				return 4;
722048d19d4SFlorian Westphal 			}
723048d19d4SFlorian Westphal 		}
724048d19d4SFlorian Westphal 
725048d19d4SFlorian Westphal 		if (fds.revents & (POLLERR | POLLNVAL)) {
7266bf41020SPaolo Abeni 			if (cfg_rcv_trunc)
7276bf41020SPaolo Abeni 				return 0;
728048d19d4SFlorian Westphal 			fprintf(stderr, "Unexpected revents: "
729048d19d4SFlorian Westphal 				"POLLERR/POLLNVAL(%x)\n", fds.revents);
730048d19d4SFlorian Westphal 			return 5;
731048d19d4SFlorian Westphal 		}
7326bf41020SPaolo Abeni 
7336bf41020SPaolo Abeni 		if (cfg_truncate > 0 && total_wlen >= cfg_truncate &&
7346bf41020SPaolo Abeni 		    total_rlen >= cfg_truncate)
7356bf41020SPaolo Abeni 			break;
736048d19d4SFlorian Westphal 	}
737048d19d4SFlorian Westphal 
738b08fbf24SPaolo Abeni 	/* leave some time for late join/announce */
7394a753ca5SMenglong Dong 	if (cfg_remove && !quit)
740df62f2ecSPaolo Abeni 		usleep(cfg_wait);
741b08fbf24SPaolo Abeni 
742048d19d4SFlorian Westphal 	return 0;
743048d19d4SFlorian Westphal }
744048d19d4SFlorian Westphal 
do_recvfile(int infd,int outfd)745048d19d4SFlorian Westphal static int do_recvfile(int infd, int outfd)
746048d19d4SFlorian Westphal {
747048d19d4SFlorian Westphal 	ssize_t r;
748048d19d4SFlorian Westphal 
749048d19d4SFlorian Westphal 	do {
750048d19d4SFlorian Westphal 		char buf[16384];
751048d19d4SFlorian Westphal 
752048d19d4SFlorian Westphal 		r = do_rnd_read(infd, buf, sizeof(buf));
753048d19d4SFlorian Westphal 		if (r > 0) {
754048d19d4SFlorian Westphal 			if (write(outfd, buf, r) != r)
755048d19d4SFlorian Westphal 				break;
756048d19d4SFlorian Westphal 		} else if (r < 0) {
757048d19d4SFlorian Westphal 			perror("read");
758048d19d4SFlorian Westphal 		}
759048d19d4SFlorian Westphal 	} while (r > 0);
760048d19d4SFlorian Westphal 
761048d19d4SFlorian Westphal 	return (int)r;
762048d19d4SFlorian Westphal }
763048d19d4SFlorian Westphal 
spool_buf(int fd,struct wstate * winfo)764ca7ae891SDmytro Shytyi static int spool_buf(int fd, struct wstate *winfo)
765ca7ae891SDmytro Shytyi {
766ca7ae891SDmytro Shytyi 	while (winfo->len) {
767ca7ae891SDmytro Shytyi 		int ret = write(fd, winfo->buf + winfo->off, winfo->len);
768ca7ae891SDmytro Shytyi 
769ca7ae891SDmytro Shytyi 		if (ret < 0) {
770ca7ae891SDmytro Shytyi 			perror("write");
771ca7ae891SDmytro Shytyi 			return 4;
772ca7ae891SDmytro Shytyi 		}
773ca7ae891SDmytro Shytyi 		winfo->off += ret;
774ca7ae891SDmytro Shytyi 		winfo->len -= ret;
775ca7ae891SDmytro Shytyi 	}
776ca7ae891SDmytro Shytyi 	return 0;
777ca7ae891SDmytro Shytyi }
778ca7ae891SDmytro Shytyi 
do_mmap(int infd,int outfd,unsigned int size,struct wstate * winfo)779ca7ae891SDmytro Shytyi static int do_mmap(int infd, int outfd, unsigned int size,
780ca7ae891SDmytro Shytyi 		   struct wstate *winfo)
781048d19d4SFlorian Westphal {
782048d19d4SFlorian Westphal 	char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0);
783ca7ae891SDmytro Shytyi 	ssize_t ret = 0, off = winfo->total_len;
784048d19d4SFlorian Westphal 	size_t rem;
785048d19d4SFlorian Westphal 
786048d19d4SFlorian Westphal 	if (inbuf == MAP_FAILED) {
787048d19d4SFlorian Westphal 		perror("mmap");
788048d19d4SFlorian Westphal 		return 1;
789048d19d4SFlorian Westphal 	}
790048d19d4SFlorian Westphal 
791ca7ae891SDmytro Shytyi 	ret = spool_buf(outfd, winfo);
792ca7ae891SDmytro Shytyi 	if (ret < 0)
793ca7ae891SDmytro Shytyi 		return ret;
794ca7ae891SDmytro Shytyi 
795ca7ae891SDmytro Shytyi 	rem = size - winfo->total_len;
796048d19d4SFlorian Westphal 
797048d19d4SFlorian Westphal 	while (rem > 0) {
798048d19d4SFlorian Westphal 		ret = write(outfd, inbuf + off, rem);
799048d19d4SFlorian Westphal 
800048d19d4SFlorian Westphal 		if (ret < 0) {
801048d19d4SFlorian Westphal 			perror("write");
802048d19d4SFlorian Westphal 			break;
803048d19d4SFlorian Westphal 		}
804048d19d4SFlorian Westphal 
805048d19d4SFlorian Westphal 		off += ret;
806048d19d4SFlorian Westphal 		rem -= ret;
807048d19d4SFlorian Westphal 	}
808048d19d4SFlorian Westphal 
809048d19d4SFlorian Westphal 	munmap(inbuf, size);
810048d19d4SFlorian Westphal 	return rem;
811048d19d4SFlorian Westphal }
812048d19d4SFlorian Westphal 
get_infd_size(int fd)813048d19d4SFlorian Westphal static int get_infd_size(int fd)
814048d19d4SFlorian Westphal {
815048d19d4SFlorian Westphal 	struct stat sb;
816048d19d4SFlorian Westphal 	ssize_t count;
817048d19d4SFlorian Westphal 	int err;
818048d19d4SFlorian Westphal 
819048d19d4SFlorian Westphal 	err = fstat(fd, &sb);
820048d19d4SFlorian Westphal 	if (err < 0) {
821048d19d4SFlorian Westphal 		perror("fstat");
822048d19d4SFlorian Westphal 		return -1;
823048d19d4SFlorian Westphal 	}
824048d19d4SFlorian Westphal 
825048d19d4SFlorian Westphal 	if ((sb.st_mode & S_IFMT) != S_IFREG) {
826048d19d4SFlorian Westphal 		fprintf(stderr, "%s: stdin is not a regular file\n", __func__);
827048d19d4SFlorian Westphal 		return -2;
828048d19d4SFlorian Westphal 	}
829048d19d4SFlorian Westphal 
830048d19d4SFlorian Westphal 	count = sb.st_size;
831048d19d4SFlorian Westphal 	if (count > INT_MAX) {
832048d19d4SFlorian Westphal 		fprintf(stderr, "File too large: %zu\n", count);
833048d19d4SFlorian Westphal 		return -3;
834048d19d4SFlorian Westphal 	}
835048d19d4SFlorian Westphal 
836048d19d4SFlorian Westphal 	return (int)count;
837048d19d4SFlorian Westphal }
838048d19d4SFlorian Westphal 
do_sendfile(int infd,int outfd,unsigned int count,struct wstate * winfo)839ca7ae891SDmytro Shytyi static int do_sendfile(int infd, int outfd, unsigned int count,
840ca7ae891SDmytro Shytyi 		       struct wstate *winfo)
841048d19d4SFlorian Westphal {
842ca7ae891SDmytro Shytyi 	int ret = spool_buf(outfd, winfo);
843ca7ae891SDmytro Shytyi 
844ca7ae891SDmytro Shytyi 	if (ret < 0)
845ca7ae891SDmytro Shytyi 		return ret;
846ca7ae891SDmytro Shytyi 
847ca7ae891SDmytro Shytyi 	count -= winfo->total_len;
848ca7ae891SDmytro Shytyi 
849048d19d4SFlorian Westphal 	while (count > 0) {
850048d19d4SFlorian Westphal 		ssize_t r;
851048d19d4SFlorian Westphal 
852048d19d4SFlorian Westphal 		r = sendfile(outfd, infd, NULL, count);
853048d19d4SFlorian Westphal 		if (r < 0) {
854048d19d4SFlorian Westphal 			perror("sendfile");
855048d19d4SFlorian Westphal 			return 3;
856048d19d4SFlorian Westphal 		}
857048d19d4SFlorian Westphal 
858048d19d4SFlorian Westphal 		count -= r;
859048d19d4SFlorian Westphal 	}
860048d19d4SFlorian Westphal 
861048d19d4SFlorian Westphal 	return 0;
862048d19d4SFlorian Westphal }
863048d19d4SFlorian Westphal 
copyfd_io_mmap(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)864048d19d4SFlorian Westphal static int copyfd_io_mmap(int infd, int peerfd, int outfd,
865ca7ae891SDmytro Shytyi 			  unsigned int size, bool *in_closed_after_out,
866ca7ae891SDmytro Shytyi 			  struct wstate *winfo)
867048d19d4SFlorian Westphal {
868048d19d4SFlorian Westphal 	int err;
869048d19d4SFlorian Westphal 
870048d19d4SFlorian Westphal 	if (listen_mode) {
871048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
872048d19d4SFlorian Westphal 		if (err)
873048d19d4SFlorian Westphal 			return err;
874048d19d4SFlorian Westphal 
875ca7ae891SDmytro Shytyi 		err = do_mmap(infd, peerfd, size, winfo);
876048d19d4SFlorian Westphal 	} else {
877ca7ae891SDmytro Shytyi 		err = do_mmap(infd, peerfd, size, winfo);
878048d19d4SFlorian Westphal 		if (err)
879048d19d4SFlorian Westphal 			return err;
880048d19d4SFlorian Westphal 
881df9e03aeSFlorian Westphal 		shut_wr(peerfd);
882048d19d4SFlorian Westphal 
883048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
884b6ab64b0SPaolo Abeni 		*in_closed_after_out = true;
885048d19d4SFlorian Westphal 	}
886048d19d4SFlorian Westphal 
887048d19d4SFlorian Westphal 	return err;
888048d19d4SFlorian Westphal }
889048d19d4SFlorian Westphal 
copyfd_io_sendfile(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)890048d19d4SFlorian Westphal static int copyfd_io_sendfile(int infd, int peerfd, int outfd,
891ca7ae891SDmytro Shytyi 			      unsigned int size, bool *in_closed_after_out, struct wstate *winfo)
892048d19d4SFlorian Westphal {
893048d19d4SFlorian Westphal 	int err;
894048d19d4SFlorian Westphal 
895048d19d4SFlorian Westphal 	if (listen_mode) {
896048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
897048d19d4SFlorian Westphal 		if (err)
898048d19d4SFlorian Westphal 			return err;
899048d19d4SFlorian Westphal 
900ca7ae891SDmytro Shytyi 		err = do_sendfile(infd, peerfd, size, winfo);
901048d19d4SFlorian Westphal 	} else {
902ca7ae891SDmytro Shytyi 		err = do_sendfile(infd, peerfd, size, winfo);
903048d19d4SFlorian Westphal 		if (err)
904048d19d4SFlorian Westphal 			return err;
905df9e03aeSFlorian Westphal 
906df9e03aeSFlorian Westphal 		shut_wr(peerfd);
907df9e03aeSFlorian Westphal 
908048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
909b6ab64b0SPaolo Abeni 		*in_closed_after_out = true;
910048d19d4SFlorian Westphal 	}
911048d19d4SFlorian Westphal 
912048d19d4SFlorian Westphal 	return err;
913048d19d4SFlorian Westphal }
914048d19d4SFlorian Westphal 
copyfd_io(int infd,int peerfd,int outfd,bool close_peerfd,struct wstate * winfo)915ca7ae891SDmytro Shytyi static int copyfd_io(int infd, int peerfd, int outfd, bool close_peerfd, struct wstate *winfo)
916048d19d4SFlorian Westphal {
917b6ab64b0SPaolo Abeni 	bool in_closed_after_out = false;
918b6ab64b0SPaolo Abeni 	struct timespec start, end;
919048d19d4SFlorian Westphal 	int file_size;
920b6ab64b0SPaolo Abeni 	int ret;
921b6ab64b0SPaolo Abeni 
922b6ab64b0SPaolo Abeni 	if (cfg_time && (clock_gettime(CLOCK_MONOTONIC, &start) < 0))
923b6ab64b0SPaolo Abeni 		xerror("can not fetch start time %d", errno);
924048d19d4SFlorian Westphal 
925048d19d4SFlorian Westphal 	switch (cfg_mode) {
926048d19d4SFlorian Westphal 	case CFG_MODE_POLL:
927ca7ae891SDmytro Shytyi 		ret = copyfd_io_poll(infd, peerfd, outfd, &in_closed_after_out,
928ca7ae891SDmytro Shytyi 				     winfo);
929b6ab64b0SPaolo Abeni 		break;
930b6ab64b0SPaolo Abeni 
931048d19d4SFlorian Westphal 	case CFG_MODE_MMAP:
932048d19d4SFlorian Westphal 		file_size = get_infd_size(infd);
933048d19d4SFlorian Westphal 		if (file_size < 0)
934048d19d4SFlorian Westphal 			return file_size;
935ca7ae891SDmytro Shytyi 		ret = copyfd_io_mmap(infd, peerfd, outfd, file_size,
936ca7ae891SDmytro Shytyi 				     &in_closed_after_out, winfo);
937b6ab64b0SPaolo Abeni 		break;
938b6ab64b0SPaolo Abeni 
939048d19d4SFlorian Westphal 	case CFG_MODE_SENDFILE:
940048d19d4SFlorian Westphal 		file_size = get_infd_size(infd);
941048d19d4SFlorian Westphal 		if (file_size < 0)
942048d19d4SFlorian Westphal 			return file_size;
943ca7ae891SDmytro Shytyi 		ret = copyfd_io_sendfile(infd, peerfd, outfd, file_size,
944ca7ae891SDmytro Shytyi 					 &in_closed_after_out, winfo);
945b6ab64b0SPaolo Abeni 		break;
946048d19d4SFlorian Westphal 
947b6ab64b0SPaolo Abeni 	default:
948048d19d4SFlorian Westphal 		fprintf(stderr, "Invalid mode %d\n", cfg_mode);
949048d19d4SFlorian Westphal 
950048d19d4SFlorian Westphal 		die_usage();
951048d19d4SFlorian Westphal 		return 1;
952048d19d4SFlorian Westphal 	}
953048d19d4SFlorian Westphal 
954b6ab64b0SPaolo Abeni 	if (ret)
955b6ab64b0SPaolo Abeni 		return ret;
956b6ab64b0SPaolo Abeni 
95705be5e27SPaolo Abeni 	if (close_peerfd)
95805be5e27SPaolo Abeni 		close(peerfd);
95905be5e27SPaolo Abeni 
960b6ab64b0SPaolo Abeni 	if (cfg_time) {
961b6ab64b0SPaolo Abeni 		unsigned int delta_ms;
962b6ab64b0SPaolo Abeni 
963b6ab64b0SPaolo Abeni 		if (clock_gettime(CLOCK_MONOTONIC, &end) < 0)
964b6ab64b0SPaolo Abeni 			xerror("can not fetch end time %d", errno);
965b6ab64b0SPaolo Abeni 		delta_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000;
966b6ab64b0SPaolo Abeni 		if (delta_ms > cfg_time) {
967b6ab64b0SPaolo Abeni 			xerror("transfer slower than expected! runtime %d ms, expected %d ms",
968b6ab64b0SPaolo Abeni 			       delta_ms, cfg_time);
969b6ab64b0SPaolo Abeni 		}
970b6ab64b0SPaolo Abeni 
971b6ab64b0SPaolo Abeni 		/* show the runtime only if this end shutdown(wr) before receiving the EOF,
972b6ab64b0SPaolo Abeni 		 * (that is, if this end got the longer runtime)
973b6ab64b0SPaolo Abeni 		 */
974b6ab64b0SPaolo Abeni 		if (in_closed_after_out)
975b6ab64b0SPaolo Abeni 			fprintf(stderr, "%d", delta_ms);
976b6ab64b0SPaolo Abeni 	}
977b6ab64b0SPaolo Abeni 
978b6ab64b0SPaolo Abeni 	return 0;
979b6ab64b0SPaolo Abeni }
980b6ab64b0SPaolo Abeni 
check_sockaddr(int pf,struct sockaddr_storage * ss,socklen_t salen)981048d19d4SFlorian Westphal static void check_sockaddr(int pf, struct sockaddr_storage *ss,
982048d19d4SFlorian Westphal 			   socklen_t salen)
983048d19d4SFlorian Westphal {
984048d19d4SFlorian Westphal 	struct sockaddr_in6 *sin6;
985048d19d4SFlorian Westphal 	struct sockaddr_in *sin;
986048d19d4SFlorian Westphal 	socklen_t wanted_size = 0;
987048d19d4SFlorian Westphal 
988048d19d4SFlorian Westphal 	switch (pf) {
989048d19d4SFlorian Westphal 	case AF_INET:
990048d19d4SFlorian Westphal 		wanted_size = sizeof(*sin);
991048d19d4SFlorian Westphal 		sin = (void *)ss;
992048d19d4SFlorian Westphal 		if (!sin->sin_port)
993048d19d4SFlorian Westphal 			fprintf(stderr, "accept: something wrong: ip connection from port 0");
994048d19d4SFlorian Westphal 		break;
995048d19d4SFlorian Westphal 	case AF_INET6:
996048d19d4SFlorian Westphal 		wanted_size = sizeof(*sin6);
997048d19d4SFlorian Westphal 		sin6 = (void *)ss;
998048d19d4SFlorian Westphal 		if (!sin6->sin6_port)
999048d19d4SFlorian Westphal 			fprintf(stderr, "accept: something wrong: ipv6 connection from port 0");
1000048d19d4SFlorian Westphal 		break;
1001048d19d4SFlorian Westphal 	default:
1002048d19d4SFlorian Westphal 		fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen);
1003048d19d4SFlorian Westphal 		return;
1004048d19d4SFlorian Westphal 	}
1005048d19d4SFlorian Westphal 
1006048d19d4SFlorian Westphal 	if (salen != wanted_size)
1007048d19d4SFlorian Westphal 		fprintf(stderr, "accept: size mismatch, got %d expected %d\n",
1008048d19d4SFlorian Westphal 			(int)salen, wanted_size);
1009048d19d4SFlorian Westphal 
1010048d19d4SFlorian Westphal 	if (ss->ss_family != pf)
1011048d19d4SFlorian Westphal 		fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n",
1012048d19d4SFlorian Westphal 			(int)ss->ss_family, pf);
1013048d19d4SFlorian Westphal }
1014048d19d4SFlorian Westphal 
check_getpeername(int fd,struct sockaddr_storage * ss,socklen_t salen)1015048d19d4SFlorian Westphal static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen)
1016048d19d4SFlorian Westphal {
1017048d19d4SFlorian Westphal 	struct sockaddr_storage peerss;
1018048d19d4SFlorian Westphal 	socklen_t peersalen = sizeof(peerss);
1019048d19d4SFlorian Westphal 
1020048d19d4SFlorian Westphal 	if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) {
1021048d19d4SFlorian Westphal 		perror("getpeername");
1022048d19d4SFlorian Westphal 		return;
1023048d19d4SFlorian Westphal 	}
1024048d19d4SFlorian Westphal 
1025048d19d4SFlorian Westphal 	if (peersalen != salen) {
1026048d19d4SFlorian Westphal 		fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen);
1027048d19d4SFlorian Westphal 		return;
1028048d19d4SFlorian Westphal 	}
1029048d19d4SFlorian Westphal 
1030048d19d4SFlorian Westphal 	if (memcmp(ss, &peerss, peersalen)) {
1031048d19d4SFlorian Westphal 		char a[INET6_ADDRSTRLEN];
1032048d19d4SFlorian Westphal 		char b[INET6_ADDRSTRLEN];
1033048d19d4SFlorian Westphal 		char c[INET6_ADDRSTRLEN];
1034048d19d4SFlorian Westphal 		char d[INET6_ADDRSTRLEN];
1035048d19d4SFlorian Westphal 
1036048d19d4SFlorian Westphal 		xgetnameinfo((struct sockaddr *)ss, salen,
1037048d19d4SFlorian Westphal 			     a, sizeof(a), b, sizeof(b));
1038048d19d4SFlorian Westphal 
1039048d19d4SFlorian Westphal 		xgetnameinfo((struct sockaddr *)&peerss, peersalen,
1040048d19d4SFlorian Westphal 			     c, sizeof(c), d, sizeof(d));
1041048d19d4SFlorian Westphal 
1042048d19d4SFlorian Westphal 		fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n",
1043048d19d4SFlorian Westphal 			__func__, a, c, b, d, peersalen, salen);
1044048d19d4SFlorian Westphal 	}
1045048d19d4SFlorian Westphal }
1046048d19d4SFlorian Westphal 
check_getpeername_connect(int fd)1047048d19d4SFlorian Westphal static void check_getpeername_connect(int fd)
1048048d19d4SFlorian Westphal {
1049048d19d4SFlorian Westphal 	struct sockaddr_storage ss;
1050048d19d4SFlorian Westphal 	socklen_t salen = sizeof(ss);
1051048d19d4SFlorian Westphal 	char a[INET6_ADDRSTRLEN];
1052048d19d4SFlorian Westphal 	char b[INET6_ADDRSTRLEN];
1053048d19d4SFlorian Westphal 
1054048d19d4SFlorian Westphal 	if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) {
1055048d19d4SFlorian Westphal 		perror("getpeername");
1056048d19d4SFlorian Westphal 		return;
1057048d19d4SFlorian Westphal 	}
1058048d19d4SFlorian Westphal 
1059048d19d4SFlorian Westphal 	xgetnameinfo((struct sockaddr *)&ss, salen,
1060048d19d4SFlorian Westphal 		     a, sizeof(a), b, sizeof(b));
1061048d19d4SFlorian Westphal 
1062048d19d4SFlorian Westphal 	if (strcmp(cfg_host, a) || strcmp(cfg_port, b))
1063048d19d4SFlorian Westphal 		fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__,
1064048d19d4SFlorian Westphal 			cfg_host, a, cfg_port, b);
1065048d19d4SFlorian Westphal }
1066048d19d4SFlorian Westphal 
maybe_close(int fd)1067b0519de8SFlorian Westphal static void maybe_close(int fd)
1068b0519de8SFlorian Westphal {
1069b0519de8SFlorian Westphal 	unsigned int r = rand();
1070b0519de8SFlorian Westphal 
107105be5e27SPaolo Abeni 	if (!(cfg_join || cfg_remove || cfg_repeat > 1) && (r & 1))
1072b0519de8SFlorian Westphal 		close(fd);
1073b0519de8SFlorian Westphal }
1074b0519de8SFlorian Westphal 
main_loop_s(int listensock)1075048d19d4SFlorian Westphal int main_loop_s(int listensock)
1076048d19d4SFlorian Westphal {
1077048d19d4SFlorian Westphal 	struct sockaddr_storage ss;
1078ca7ae891SDmytro Shytyi 	struct wstate winfo;
1079048d19d4SFlorian Westphal 	struct pollfd polls;
1080048d19d4SFlorian Westphal 	socklen_t salen;
1081048d19d4SFlorian Westphal 	int remotesock;
108205be5e27SPaolo Abeni 	int fd = 0;
1083048d19d4SFlorian Westphal 
108405be5e27SPaolo Abeni again:
1085048d19d4SFlorian Westphal 	polls.fd = listensock;
1086048d19d4SFlorian Westphal 	polls.events = POLLIN;
1087048d19d4SFlorian Westphal 
1088048d19d4SFlorian Westphal 	switch (poll(&polls, 1, poll_timeout)) {
1089048d19d4SFlorian Westphal 	case -1:
1090048d19d4SFlorian Westphal 		perror("poll");
1091048d19d4SFlorian Westphal 		return 1;
1092048d19d4SFlorian Westphal 	case 0:
1093048d19d4SFlorian Westphal 		fprintf(stderr, "%s: timed out\n", __func__);
1094048d19d4SFlorian Westphal 		close(listensock);
1095048d19d4SFlorian Westphal 		return 2;
1096048d19d4SFlorian Westphal 	}
1097048d19d4SFlorian Westphal 
1098048d19d4SFlorian Westphal 	salen = sizeof(ss);
1099048d19d4SFlorian Westphal 	remotesock = accept(listensock, (struct sockaddr *)&ss, &salen);
1100048d19d4SFlorian Westphal 	if (remotesock >= 0) {
1101b0519de8SFlorian Westphal 		maybe_close(listensock);
1102048d19d4SFlorian Westphal 		check_sockaddr(pf, &ss, salen);
1103048d19d4SFlorian Westphal 		check_getpeername(remotesock, &ss, salen);
1104048d19d4SFlorian Westphal 
110505be5e27SPaolo Abeni 		if (cfg_input) {
110605be5e27SPaolo Abeni 			fd = open(cfg_input, O_RDONLY);
110705be5e27SPaolo Abeni 			if (fd < 0)
110805be5e27SPaolo Abeni 				xerror("can't open %s: %d", cfg_input, errno);
1109048d19d4SFlorian Westphal 		}
1110048d19d4SFlorian Westphal 
111105be5e27SPaolo Abeni 		SOCK_TEST_TCPULP(remotesock, 0);
1112048d19d4SFlorian Westphal 
1113ca7ae891SDmytro Shytyi 		memset(&winfo, 0, sizeof(winfo));
1114ca7ae891SDmytro Shytyi 		copyfd_io(fd, remotesock, 1, true, &winfo);
111505be5e27SPaolo Abeni 	} else {
111605be5e27SPaolo Abeni 		perror("accept");
1117048d19d4SFlorian Westphal 		return 1;
1118048d19d4SFlorian Westphal 	}
1119048d19d4SFlorian Westphal 
112005be5e27SPaolo Abeni 	if (cfg_input)
112105be5e27SPaolo Abeni 		close(fd);
1122ffe8c864SLiu Jing 
1123ffe8c864SLiu Jing 	if (--cfg_repeat > 0)
112405be5e27SPaolo Abeni 		goto again;
112505be5e27SPaolo Abeni 
112605be5e27SPaolo Abeni 	return 0;
112705be5e27SPaolo Abeni }
112805be5e27SPaolo Abeni 
init_rng(void)1129048d19d4SFlorian Westphal static void init_rng(void)
1130048d19d4SFlorian Westphal {
1131048d19d4SFlorian Westphal 	unsigned int foo;
1132048d19d4SFlorian Westphal 
113320bb3a96SWillem de Bruijn 	if (getrandom(&foo, sizeof(foo), 0) == -1) {
113420bb3a96SWillem de Bruijn 		perror("getrandom");
113520bb3a96SWillem de Bruijn 		exit(1);
1136048d19d4SFlorian Westphal 	}
1137048d19d4SFlorian Westphal 
1138048d19d4SFlorian Westphal 	srand(foo);
1139048d19d4SFlorian Westphal }
1140048d19d4SFlorian Westphal 
xsetsockopt(int fd,int level,int optname,const void * optval,socklen_t optlen)11415e6af0a7SFlorian Westphal static void xsetsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
11425e6af0a7SFlorian Westphal {
11435e6af0a7SFlorian Westphal 	int err;
11445e6af0a7SFlorian Westphal 
11455e6af0a7SFlorian Westphal 	err = setsockopt(fd, level, optname, optval, optlen);
11465e6af0a7SFlorian Westphal 	if (err) {
11475e6af0a7SFlorian Westphal 		perror("setsockopt");
11485e6af0a7SFlorian Westphal 		exit(1);
11495e6af0a7SFlorian Westphal 	}
11505e6af0a7SFlorian Westphal }
11515e6af0a7SFlorian Westphal 
apply_cmsg_types(int fd,const struct cfg_cmsg_types * cmsg)11525e6af0a7SFlorian Westphal static void apply_cmsg_types(int fd, const struct cfg_cmsg_types *cmsg)
11535e6af0a7SFlorian Westphal {
11545e6af0a7SFlorian Westphal 	static const unsigned int on = 1;
11555e6af0a7SFlorian Westphal 
11565e6af0a7SFlorian Westphal 	if (cmsg->timestampns)
11575e6af0a7SFlorian Westphal 		xsetsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS_NEW, &on, sizeof(on));
11585cbd886cSFlorian Westphal 	if (cmsg->tcp_inq)
11595cbd886cSFlorian Westphal 		xsetsockopt(fd, IPPROTO_TCP, TCP_INQ, &on, sizeof(on));
11605e6af0a7SFlorian Westphal }
11615e6af0a7SFlorian Westphal 
parse_cmsg_types(const char * type)11625e6af0a7SFlorian Westphal static void parse_cmsg_types(const char *type)
11635e6af0a7SFlorian Westphal {
11645e6af0a7SFlorian Westphal 	char *next = strchr(type, ',');
11655e6af0a7SFlorian Westphal 	unsigned int len = 0;
11665e6af0a7SFlorian Westphal 
11675e6af0a7SFlorian Westphal 	cfg_cmsg_types.cmsg_enabled = 1;
11685e6af0a7SFlorian Westphal 
11695e6af0a7SFlorian Westphal 	if (next) {
11705e6af0a7SFlorian Westphal 		parse_cmsg_types(next + 1);
11715e6af0a7SFlorian Westphal 		len = next - type;
11725e6af0a7SFlorian Westphal 	} else {
11735e6af0a7SFlorian Westphal 		len = strlen(type);
11745e6af0a7SFlorian Westphal 	}
11755e6af0a7SFlorian Westphal 
11765e6af0a7SFlorian Westphal 	if (strncmp(type, "TIMESTAMPNS", len) == 0) {
11775e6af0a7SFlorian Westphal 		cfg_cmsg_types.timestampns = 1;
11785e6af0a7SFlorian Westphal 		return;
11795e6af0a7SFlorian Westphal 	}
11805e6af0a7SFlorian Westphal 
11815cbd886cSFlorian Westphal 	if (strncmp(type, "TCPINQ", len) == 0) {
11825cbd886cSFlorian Westphal 		cfg_cmsg_types.tcp_inq = 1;
11835cbd886cSFlorian Westphal 		return;
11845cbd886cSFlorian Westphal 	}
11855cbd886cSFlorian Westphal 
11865e6af0a7SFlorian Westphal 	fprintf(stderr, "Unrecognized cmsg option %s\n", type);
11875e6af0a7SFlorian Westphal 	exit(1);
11885e6af0a7SFlorian Westphal }
11895e6af0a7SFlorian Westphal 
parse_setsock_options(const char * name)11905fb62e9cSFlorian Westphal static void parse_setsock_options(const char *name)
11915fb62e9cSFlorian Westphal {
11925fb62e9cSFlorian Westphal 	char *next = strchr(name, ',');
11935fb62e9cSFlorian Westphal 	unsigned int len = 0;
11945fb62e9cSFlorian Westphal 
11955fb62e9cSFlorian Westphal 	if (next) {
11965fb62e9cSFlorian Westphal 		parse_setsock_options(next + 1);
11975fb62e9cSFlorian Westphal 		len = next - name;
11985fb62e9cSFlorian Westphal 	} else {
11995fb62e9cSFlorian Westphal 		len = strlen(name);
12005fb62e9cSFlorian Westphal 	}
12015fb62e9cSFlorian Westphal 
12025fb62e9cSFlorian Westphal 	if (strncmp(name, "TRANSPARENT", len) == 0) {
12035fb62e9cSFlorian Westphal 		cfg_sockopt_types.transparent = 1;
12045fb62e9cSFlorian Westphal 		return;
12055fb62e9cSFlorian Westphal 	}
12065fb62e9cSFlorian Westphal 
1207ca7ae891SDmytro Shytyi 	if (strncmp(name, "MPTFO", len) == 0) {
1208ca7ae891SDmytro Shytyi 		cfg_sockopt_types.mptfo = 1;
1209ca7ae891SDmytro Shytyi 		return;
1210ca7ae891SDmytro Shytyi 	}
1211ca7ae891SDmytro Shytyi 
12125fb62e9cSFlorian Westphal 	fprintf(stderr, "Unrecognized setsockopt option %s\n", name);
12135fb62e9cSFlorian Westphal 	exit(1);
12145fb62e9cSFlorian Westphal }
12155fb62e9cSFlorian Westphal 
xdisconnect(int fd)121675deec40SPaolo Abeni void xdisconnect(int fd)
121705be5e27SPaolo Abeni {
121875deec40SPaolo Abeni 	socklen_t addrlen = sizeof(struct sockaddr_storage);
121975deec40SPaolo Abeni 	struct sockaddr_storage addr, empty;
122005be5e27SPaolo Abeni 	int msec_sleep = 10;
122175deec40SPaolo Abeni 	void *raw_addr;
122275deec40SPaolo Abeni 	int i, cmdlen;
122375deec40SPaolo Abeni 	char cmd[128];
122475deec40SPaolo Abeni 
122575deec40SPaolo Abeni 	/* get the local address and convert it to string */
122675deec40SPaolo Abeni 	if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) < 0)
122775deec40SPaolo Abeni 		xerror("getsockname");
122875deec40SPaolo Abeni 
122975deec40SPaolo Abeni 	if (addr.ss_family == AF_INET)
123075deec40SPaolo Abeni 		raw_addr = &(((struct sockaddr_in *)&addr)->sin_addr);
123175deec40SPaolo Abeni 	else if (addr.ss_family == AF_INET6)
123275deec40SPaolo Abeni 		raw_addr = &(((struct sockaddr_in6 *)&addr)->sin6_addr);
123375deec40SPaolo Abeni 	else
123475deec40SPaolo Abeni 		xerror("bad family");
123575deec40SPaolo Abeni 
123675deec40SPaolo Abeni 	strcpy(cmd, "ss -M | grep -q ");
123775deec40SPaolo Abeni 	cmdlen = strlen(cmd);
123875deec40SPaolo Abeni 	if (!inet_ntop(addr.ss_family, raw_addr, &cmd[cmdlen],
123975deec40SPaolo Abeni 		       sizeof(cmd) - cmdlen))
124075deec40SPaolo Abeni 		xerror("inet_ntop");
124105be5e27SPaolo Abeni 
124205be5e27SPaolo Abeni 	shutdown(fd, SHUT_WR);
124305be5e27SPaolo Abeni 
124475deec40SPaolo Abeni 	/*
124575deec40SPaolo Abeni 	 * wait until the pending data is completely flushed and all
124675deec40SPaolo Abeni 	 * the MPTCP sockets reached the closed status.
124705be5e27SPaolo Abeni 	 * disconnect will bypass/ignore/drop any pending data.
124805be5e27SPaolo Abeni 	 */
124905be5e27SPaolo Abeni 	for (i = 0; ; i += msec_sleep) {
125075deec40SPaolo Abeni 		/* closed socket are not listed by 'ss' */
125175deec40SPaolo Abeni 		if (system(cmd) != 0)
125205be5e27SPaolo Abeni 			break;
125305be5e27SPaolo Abeni 
125405be5e27SPaolo Abeni 		if (i > poll_timeout)
125505be5e27SPaolo Abeni 			xerror("timeout while waiting for spool to complete");
125605be5e27SPaolo Abeni 		usleep(msec_sleep * 1000);
125705be5e27SPaolo Abeni 	}
125805be5e27SPaolo Abeni 
125905be5e27SPaolo Abeni 	memset(&empty, 0, sizeof(empty));
126005be5e27SPaolo Abeni 	empty.ss_family = AF_UNSPEC;
126105be5e27SPaolo Abeni 	if (connect(fd, (struct sockaddr *)&empty, addrlen) < 0)
126205be5e27SPaolo Abeni 		xerror("can't disconnect: %d", errno);
126305be5e27SPaolo Abeni }
126405be5e27SPaolo Abeni 
main_loop(void)1265048d19d4SFlorian Westphal int main_loop(void)
1266048d19d4SFlorian Westphal {
1267ca7ae891SDmytro Shytyi 	int fd = 0, ret, fd_in = 0;
126805be5e27SPaolo Abeni 	struct addrinfo *peer;
1269ca7ae891SDmytro Shytyi 	struct wstate winfo;
1270048d19d4SFlorian Westphal 
1271ca7ae891SDmytro Shytyi 	if (cfg_input && cfg_sockopt_types.mptfo) {
1272ca7ae891SDmytro Shytyi 		fd_in = open(cfg_input, O_RDONLY);
1273ca7ae891SDmytro Shytyi 		if (fd < 0)
1274ca7ae891SDmytro Shytyi 			xerror("can't open %s:%d", cfg_input, errno);
1275ca7ae891SDmytro Shytyi 	}
1276ca7ae891SDmytro Shytyi 
1277ca7ae891SDmytro Shytyi 	memset(&winfo, 0, sizeof(winfo));
1278ca7ae891SDmytro Shytyi 	fd = sock_connect_mptcp(cfg_host, cfg_port, cfg_sock_proto, &peer, fd_in, &winfo);
1279048d19d4SFlorian Westphal 	if (fd < 0)
1280048d19d4SFlorian Westphal 		return 2;
1281048d19d4SFlorian Westphal 
128205be5e27SPaolo Abeni again:
1283048d19d4SFlorian Westphal 	check_getpeername_connect(fd);
1284048d19d4SFlorian Westphal 
1285f730b65cSFlorian Westphal 	SOCK_TEST_TCPULP(fd, cfg_sock_proto);
1286f730b65cSFlorian Westphal 
12878a4b910dSFlorian Westphal 	if (cfg_rcvbuf)
12888a4b910dSFlorian Westphal 		set_rcvbuf(fd, cfg_rcvbuf);
1289048d19d4SFlorian Westphal 	if (cfg_sndbuf)
1290048d19d4SFlorian Westphal 		set_sndbuf(fd, cfg_sndbuf);
12915e6af0a7SFlorian Westphal 	if (cfg_cmsg_types.cmsg_enabled)
12925e6af0a7SFlorian Westphal 		apply_cmsg_types(fd, &cfg_cmsg_types);
1293048d19d4SFlorian Westphal 
1294ca7ae891SDmytro Shytyi 	if (cfg_input && !cfg_sockopt_types.mptfo) {
129505be5e27SPaolo Abeni 		fd_in = open(cfg_input, O_RDONLY);
129605be5e27SPaolo Abeni 		if (fd < 0)
129705be5e27SPaolo Abeni 			xerror("can't open %s:%d", cfg_input, errno);
129805be5e27SPaolo Abeni 	}
129905be5e27SPaolo Abeni 
1300ca7ae891SDmytro Shytyi 	ret = copyfd_io(fd_in, fd, 1, 0, &winfo);
130105be5e27SPaolo Abeni 	if (ret)
130205be5e27SPaolo Abeni 		return ret;
130305be5e27SPaolo Abeni 
13046bf41020SPaolo Abeni 	if (cfg_truncate > 0) {
1305*7a9b86bdSMatthieu Baerts (NGI0) 		shutdown(fd, SHUT_WR);
13066bf41020SPaolo Abeni 	} else if (--cfg_repeat > 0) {
130775deec40SPaolo Abeni 		xdisconnect(fd);
130805be5e27SPaolo Abeni 
130905be5e27SPaolo Abeni 		/* the socket could be unblocking at this point, we need the
131005be5e27SPaolo Abeni 		 * connect to be blocking
131105be5e27SPaolo Abeni 		 */
131205be5e27SPaolo Abeni 		set_nonblock(fd, false);
131305be5e27SPaolo Abeni 		if (connect(fd, peer->ai_addr, peer->ai_addrlen))
131405be5e27SPaolo Abeni 			xerror("can't reconnect: %d", errno);
131505be5e27SPaolo Abeni 		if (cfg_input)
131605be5e27SPaolo Abeni 			close(fd_in);
1317ca7ae891SDmytro Shytyi 		memset(&winfo, 0, sizeof(winfo));
131805be5e27SPaolo Abeni 		goto again;
13196bf41020SPaolo Abeni 	} else {
13206bf41020SPaolo Abeni 		close(fd);
132105be5e27SPaolo Abeni 	}
13226bf41020SPaolo Abeni 
132305be5e27SPaolo Abeni 	return 0;
1324048d19d4SFlorian Westphal }
1325048d19d4SFlorian Westphal 
parse_proto(const char * proto)1326048d19d4SFlorian Westphal int parse_proto(const char *proto)
1327048d19d4SFlorian Westphal {
1328048d19d4SFlorian Westphal 	if (!strcasecmp(proto, "MPTCP"))
1329048d19d4SFlorian Westphal 		return IPPROTO_MPTCP;
1330048d19d4SFlorian Westphal 	if (!strcasecmp(proto, "TCP"))
1331048d19d4SFlorian Westphal 		return IPPROTO_TCP;
1332048d19d4SFlorian Westphal 
1333048d19d4SFlorian Westphal 	fprintf(stderr, "Unknown protocol: %s\n.", proto);
1334048d19d4SFlorian Westphal 	die_usage();
1335048d19d4SFlorian Westphal 
1336048d19d4SFlorian Westphal 	/* silence compiler warning */
1337048d19d4SFlorian Westphal 	return 0;
1338048d19d4SFlorian Westphal }
1339048d19d4SFlorian Westphal 
parse_mode(const char * mode)1340048d19d4SFlorian Westphal int parse_mode(const char *mode)
1341048d19d4SFlorian Westphal {
1342048d19d4SFlorian Westphal 	if (!strcasecmp(mode, "poll"))
1343048d19d4SFlorian Westphal 		return CFG_MODE_POLL;
1344048d19d4SFlorian Westphal 	if (!strcasecmp(mode, "mmap"))
1345048d19d4SFlorian Westphal 		return CFG_MODE_MMAP;
1346048d19d4SFlorian Westphal 	if (!strcasecmp(mode, "sendfile"))
1347048d19d4SFlorian Westphal 		return CFG_MODE_SENDFILE;
1348048d19d4SFlorian Westphal 
1349048d19d4SFlorian Westphal 	fprintf(stderr, "Unknown test mode: %s\n", mode);
1350048d19d4SFlorian Westphal 	fprintf(stderr, "Supported modes are:\n");
1351048d19d4SFlorian Westphal 	fprintf(stderr, "\t\t\"poll\" - interleaved read/write using poll()\n");
1352048d19d4SFlorian Westphal 	fprintf(stderr, "\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n");
1353048d19d4SFlorian Westphal 	fprintf(stderr, "\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n");
1354048d19d4SFlorian Westphal 
1355048d19d4SFlorian Westphal 	die_usage();
1356048d19d4SFlorian Westphal 
1357048d19d4SFlorian Westphal 	/* silence compiler warning */
1358048d19d4SFlorian Westphal 	return 0;
1359048d19d4SFlorian Westphal }
1360048d19d4SFlorian Westphal 
parse_peek(const char * mode)1361df8aee6dSYonglong Li int parse_peek(const char *mode)
1362df8aee6dSYonglong Li {
1363df8aee6dSYonglong Li 	if (!strcasecmp(mode, "saveWithPeek"))
1364df8aee6dSYonglong Li 		return CFG_WITH_PEEK;
1365df8aee6dSYonglong Li 	if (!strcasecmp(mode, "saveAfterPeek"))
1366df8aee6dSYonglong Li 		return CFG_AFTER_PEEK;
1367df8aee6dSYonglong Li 
1368df8aee6dSYonglong Li 	fprintf(stderr, "Unknown: %s\n", mode);
1369df8aee6dSYonglong Li 	fprintf(stderr, "Supported MSG_PEEK mode are:\n");
1370df8aee6dSYonglong Li 	fprintf(stderr,
1371df8aee6dSYonglong Li 		"\t\t\"saveWithPeek\" - recv data with flags 'MSG_PEEK' and save the peek data into file\n");
1372df8aee6dSYonglong Li 	fprintf(stderr,
1373df8aee6dSYonglong Li 		"\t\t\"saveAfterPeek\" - read and save data into file after recv with flags 'MSG_PEEK'\n");
1374df8aee6dSYonglong Li 
1375df8aee6dSYonglong Li 	die_usage();
1376df8aee6dSYonglong Li 
1377df8aee6dSYonglong Li 	/* silence compiler warning */
1378df8aee6dSYonglong Li 	return 0;
1379df8aee6dSYonglong Li }
1380df8aee6dSYonglong Li 
parse_int(const char * size)13818a4b910dSFlorian Westphal static int parse_int(const char *size)
1382048d19d4SFlorian Westphal {
1383048d19d4SFlorian Westphal 	unsigned long s;
1384048d19d4SFlorian Westphal 
1385048d19d4SFlorian Westphal 	errno = 0;
1386048d19d4SFlorian Westphal 
1387048d19d4SFlorian Westphal 	s = strtoul(size, NULL, 0);
1388048d19d4SFlorian Westphal 
1389048d19d4SFlorian Westphal 	if (errno) {
1390048d19d4SFlorian Westphal 		fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1391048d19d4SFlorian Westphal 			size, strerror(errno));
1392048d19d4SFlorian Westphal 		die_usage();
1393048d19d4SFlorian Westphal 	}
1394048d19d4SFlorian Westphal 
1395048d19d4SFlorian Westphal 	if (s > INT_MAX) {
1396048d19d4SFlorian Westphal 		fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1397048d19d4SFlorian Westphal 			size, strerror(ERANGE));
1398048d19d4SFlorian Westphal 		die_usage();
1399048d19d4SFlorian Westphal 	}
1400048d19d4SFlorian Westphal 
14018a4b910dSFlorian Westphal 	return (int)s;
1402048d19d4SFlorian Westphal }
1403048d19d4SFlorian Westphal 
parse_opts(int argc,char ** argv)1404048d19d4SFlorian Westphal static void parse_opts(int argc, char **argv)
1405048d19d4SFlorian Westphal {
1406048d19d4SFlorian Westphal 	int c;
1407048d19d4SFlorian Westphal 
14086bf41020SPaolo Abeni 	while ((c = getopt(argc, argv, "6c:f:hi:I:jlm:M:o:p:P:r:R:s:S:t:T:w:")) != -1) {
1409048d19d4SFlorian Westphal 		switch (c) {
14106bf41020SPaolo Abeni 		case 'f':
14116bf41020SPaolo Abeni 			cfg_truncate = atoi(optarg);
14126bf41020SPaolo Abeni 
14136bf41020SPaolo Abeni 			/* when receiving a fastclose, ignore PIPE signals and
14146bf41020SPaolo Abeni 			 * all the I/O errors later in the code
14156bf41020SPaolo Abeni 			 */
14166bf41020SPaolo Abeni 			if (cfg_truncate < 0) {
14176bf41020SPaolo Abeni 				cfg_rcv_trunc = true;
14186bf41020SPaolo Abeni 				signal(SIGPIPE, handle_signal);
14196bf41020SPaolo Abeni 			}
14206bf41020SPaolo Abeni 			break;
1421b08fbf24SPaolo Abeni 		case 'j':
1422b08fbf24SPaolo Abeni 			cfg_join = true;
1423b08fbf24SPaolo Abeni 			cfg_mode = CFG_MODE_POLL;
1424b08fbf24SPaolo Abeni 			break;
142513153324SGeliang Tang 		case 'r':
142613153324SGeliang Tang 			cfg_remove = true;
142713153324SGeliang Tang 			cfg_mode = CFG_MODE_POLL;
142813153324SGeliang Tang 			cfg_wait = 400000;
14292e580a63SGeliang Tang 			cfg_do_w = atoi(optarg);
14302e580a63SGeliang Tang 			if (cfg_do_w <= 0)
14312e580a63SGeliang Tang 				cfg_do_w = 50;
143213153324SGeliang Tang 			break;
143305be5e27SPaolo Abeni 		case 'i':
143405be5e27SPaolo Abeni 			cfg_input = optarg;
143505be5e27SPaolo Abeni 			break;
143605be5e27SPaolo Abeni 		case 'I':
143705be5e27SPaolo Abeni 			cfg_repeat = atoi(optarg);
143805be5e27SPaolo Abeni 			break;
1439048d19d4SFlorian Westphal 		case 'l':
1440048d19d4SFlorian Westphal 			listen_mode = true;
1441048d19d4SFlorian Westphal 			break;
1442048d19d4SFlorian Westphal 		case 'p':
1443048d19d4SFlorian Westphal 			cfg_port = optarg;
1444048d19d4SFlorian Westphal 			break;
1445048d19d4SFlorian Westphal 		case 's':
1446048d19d4SFlorian Westphal 			cfg_sock_proto = parse_proto(optarg);
1447048d19d4SFlorian Westphal 			break;
1448048d19d4SFlorian Westphal 		case 'h':
1449048d19d4SFlorian Westphal 			die_usage();
1450048d19d4SFlorian Westphal 			break;
1451048d19d4SFlorian Westphal 		case '6':
1452048d19d4SFlorian Westphal 			pf = AF_INET6;
1453048d19d4SFlorian Westphal 			break;
1454048d19d4SFlorian Westphal 		case 't':
1455048d19d4SFlorian Westphal 			poll_timeout = atoi(optarg) * 1000;
1456048d19d4SFlorian Westphal 			if (poll_timeout <= 0)
1457048d19d4SFlorian Westphal 				poll_timeout = -1;
1458048d19d4SFlorian Westphal 			break;
1459b6ab64b0SPaolo Abeni 		case 'T':
1460b6ab64b0SPaolo Abeni 			cfg_time = atoi(optarg);
1461b6ab64b0SPaolo Abeni 			break;
1462048d19d4SFlorian Westphal 		case 'm':
1463048d19d4SFlorian Westphal 			cfg_mode = parse_mode(optarg);
1464048d19d4SFlorian Westphal 			break;
14658a4b910dSFlorian Westphal 		case 'S':
14668a4b910dSFlorian Westphal 			cfg_sndbuf = parse_int(optarg);
14678a4b910dSFlorian Westphal 			break;
14688a4b910dSFlorian Westphal 		case 'R':
14698a4b910dSFlorian Westphal 			cfg_rcvbuf = parse_int(optarg);
1470048d19d4SFlorian Westphal 			break;
1471df62f2ecSPaolo Abeni 		case 'w':
1472df62f2ecSPaolo Abeni 			cfg_wait = atoi(optarg)*1000000;
1473df62f2ecSPaolo Abeni 			break;
1474dc65fe82SFlorian Westphal 		case 'M':
1475dc65fe82SFlorian Westphal 			cfg_mark = strtol(optarg, NULL, 0);
1476dc65fe82SFlorian Westphal 			break;
1477df8aee6dSYonglong Li 		case 'P':
1478df8aee6dSYonglong Li 			cfg_peek = parse_peek(optarg);
1479df8aee6dSYonglong Li 			break;
14805e6af0a7SFlorian Westphal 		case 'c':
14815e6af0a7SFlorian Westphal 			parse_cmsg_types(optarg);
14825e6af0a7SFlorian Westphal 			break;
14835fb62e9cSFlorian Westphal 		case 'o':
14845fb62e9cSFlorian Westphal 			parse_setsock_options(optarg);
14855fb62e9cSFlorian Westphal 			break;
1486048d19d4SFlorian Westphal 		}
1487048d19d4SFlorian Westphal 	}
1488048d19d4SFlorian Westphal 
1489048d19d4SFlorian Westphal 	if (optind + 1 != argc)
1490048d19d4SFlorian Westphal 		die_usage();
1491048d19d4SFlorian Westphal 	cfg_host = argv[optind];
1492048d19d4SFlorian Westphal 
1493048d19d4SFlorian Westphal 	if (strchr(cfg_host, ':'))
1494048d19d4SFlorian Westphal 		pf = AF_INET6;
1495048d19d4SFlorian Westphal }
1496048d19d4SFlorian Westphal 
main(int argc,char * argv[])1497048d19d4SFlorian Westphal int main(int argc, char *argv[])
1498048d19d4SFlorian Westphal {
1499048d19d4SFlorian Westphal 	init_rng();
1500048d19d4SFlorian Westphal 
1501df62f2ecSPaolo Abeni 	signal(SIGUSR1, handle_signal);
1502048d19d4SFlorian Westphal 	parse_opts(argc, argv);
1503048d19d4SFlorian Westphal 
1504048d19d4SFlorian Westphal 	if (listen_mode) {
1505048d19d4SFlorian Westphal 		int fd = sock_listen_mptcp(cfg_host, cfg_port);
1506048d19d4SFlorian Westphal 
1507048d19d4SFlorian Westphal 		if (fd < 0)
1508048d19d4SFlorian Westphal 			return 1;
1509048d19d4SFlorian Westphal 
15108a4b910dSFlorian Westphal 		if (cfg_rcvbuf)
15118a4b910dSFlorian Westphal 			set_rcvbuf(fd, cfg_rcvbuf);
1512048d19d4SFlorian Westphal 		if (cfg_sndbuf)
1513048d19d4SFlorian Westphal 			set_sndbuf(fd, cfg_sndbuf);
1514dc65fe82SFlorian Westphal 		if (cfg_mark)
1515dc65fe82SFlorian Westphal 			set_mark(fd, cfg_mark);
15165e6af0a7SFlorian Westphal 		if (cfg_cmsg_types.cmsg_enabled)
15175e6af0a7SFlorian Westphal 			apply_cmsg_types(fd, &cfg_cmsg_types);
1518048d19d4SFlorian Westphal 
1519048d19d4SFlorian Westphal 		return main_loop_s(fd);
1520048d19d4SFlorian Westphal 	}
1521048d19d4SFlorian Westphal 
1522048d19d4SFlorian Westphal 	return main_loop();
1523048d19d4SFlorian Westphal }
1524