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>
9048d19d4SFlorian Westphal #include <stdbool.h>
10048d19d4SFlorian Westphal #include <stdint.h>
11048d19d4SFlorian Westphal #include <stdio.h>
12048d19d4SFlorian Westphal #include <stdlib.h>
13048d19d4SFlorian Westphal #include <strings.h>
14048d19d4SFlorian Westphal #include <unistd.h>
15048d19d4SFlorian Westphal 
16048d19d4SFlorian Westphal #include <sys/poll.h>
17048d19d4SFlorian Westphal #include <sys/sendfile.h>
18048d19d4SFlorian Westphal #include <sys/stat.h>
19048d19d4SFlorian Westphal #include <sys/socket.h>
20048d19d4SFlorian Westphal #include <sys/types.h>
21048d19d4SFlorian Westphal #include <sys/mman.h>
22048d19d4SFlorian Westphal 
23048d19d4SFlorian Westphal #include <netdb.h>
24048d19d4SFlorian Westphal #include <netinet/in.h>
25048d19d4SFlorian Westphal 
26048d19d4SFlorian Westphal #include <linux/tcp.h>
27048d19d4SFlorian Westphal 
28048d19d4SFlorian Westphal extern int optind;
29048d19d4SFlorian Westphal 
30048d19d4SFlorian Westphal #ifndef IPPROTO_MPTCP
31048d19d4SFlorian Westphal #define IPPROTO_MPTCP 262
32048d19d4SFlorian Westphal #endif
33048d19d4SFlorian Westphal #ifndef TCP_ULP
34048d19d4SFlorian Westphal #define TCP_ULP 31
35048d19d4SFlorian Westphal #endif
36048d19d4SFlorian Westphal 
37*8a4b910dSFlorian Westphal static int  poll_timeout = 10 * 1000;
38048d19d4SFlorian Westphal static bool listen_mode;
39048d19d4SFlorian Westphal 
40048d19d4SFlorian Westphal enum cfg_mode {
41048d19d4SFlorian Westphal 	CFG_MODE_POLL,
42048d19d4SFlorian Westphal 	CFG_MODE_MMAP,
43048d19d4SFlorian Westphal 	CFG_MODE_SENDFILE,
44048d19d4SFlorian Westphal };
45048d19d4SFlorian Westphal 
46048d19d4SFlorian Westphal static enum cfg_mode cfg_mode = CFG_MODE_POLL;
47048d19d4SFlorian Westphal static const char *cfg_host;
48048d19d4SFlorian Westphal static const char *cfg_port	= "12000";
49048d19d4SFlorian Westphal static int cfg_sock_proto	= IPPROTO_MPTCP;
50048d19d4SFlorian Westphal static bool tcpulp_audit;
51048d19d4SFlorian Westphal static int pf = AF_INET;
52048d19d4SFlorian Westphal static int cfg_sndbuf;
53*8a4b910dSFlorian Westphal static int cfg_rcvbuf;
54048d19d4SFlorian Westphal 
55048d19d4SFlorian Westphal static void die_usage(void)
56048d19d4SFlorian Westphal {
57*8a4b910dSFlorian Westphal 	fprintf(stderr, "Usage: mptcp_connect [-6] [-u] [-s MPTCP|TCP] [-p port] [-m mode]"
58*8a4b910dSFlorian Westphal 		"[-l] connect_address\n");
59*8a4b910dSFlorian Westphal 	fprintf(stderr, "\t-6 use ipv6\n");
60*8a4b910dSFlorian Westphal 	fprintf(stderr, "\t-t num -- set poll timeout to num\n");
61*8a4b910dSFlorian Westphal 	fprintf(stderr, "\t-S num -- set SO_SNDBUF to num\n");
62*8a4b910dSFlorian Westphal 	fprintf(stderr, "\t-R num -- set SO_RCVBUF to num\n");
63*8a4b910dSFlorian Westphal 	fprintf(stderr, "\t-p num -- use port num\n");
64*8a4b910dSFlorian Westphal 	fprintf(stderr, "\t-m [MPTCP|TCP] -- use tcp or mptcp sockets\n");
65*8a4b910dSFlorian Westphal 	fprintf(stderr, "\t-s [mmap|poll] -- use poll (default) or mmap\n");
66*8a4b910dSFlorian Westphal 	fprintf(stderr, "\t-u -- check mptcp ulp\n");
67048d19d4SFlorian Westphal 	exit(1);
68048d19d4SFlorian Westphal }
69048d19d4SFlorian Westphal 
70048d19d4SFlorian Westphal static const char *getxinfo_strerr(int err)
71048d19d4SFlorian Westphal {
72048d19d4SFlorian Westphal 	if (err == EAI_SYSTEM)
73048d19d4SFlorian Westphal 		return strerror(errno);
74048d19d4SFlorian Westphal 
75048d19d4SFlorian Westphal 	return gai_strerror(err);
76048d19d4SFlorian Westphal }
77048d19d4SFlorian Westphal 
78048d19d4SFlorian Westphal static void xgetnameinfo(const struct sockaddr *addr, socklen_t addrlen,
79048d19d4SFlorian Westphal 			 char *host, socklen_t hostlen,
80048d19d4SFlorian Westphal 			 char *serv, socklen_t servlen)
81048d19d4SFlorian Westphal {
82048d19d4SFlorian Westphal 	int flags = NI_NUMERICHOST | NI_NUMERICSERV;
83048d19d4SFlorian Westphal 	int err = getnameinfo(addr, addrlen, host, hostlen, serv, servlen,
84048d19d4SFlorian Westphal 			      flags);
85048d19d4SFlorian Westphal 
86048d19d4SFlorian Westphal 	if (err) {
87048d19d4SFlorian Westphal 		const char *errstr = getxinfo_strerr(err);
88048d19d4SFlorian Westphal 
89048d19d4SFlorian Westphal 		fprintf(stderr, "Fatal: getnameinfo: %s\n", errstr);
90048d19d4SFlorian Westphal 		exit(1);
91048d19d4SFlorian Westphal 	}
92048d19d4SFlorian Westphal }
93048d19d4SFlorian Westphal 
94048d19d4SFlorian Westphal static void xgetaddrinfo(const char *node, const char *service,
95048d19d4SFlorian Westphal 			 const struct addrinfo *hints,
96048d19d4SFlorian Westphal 			 struct addrinfo **res)
97048d19d4SFlorian Westphal {
98048d19d4SFlorian Westphal 	int err = getaddrinfo(node, service, hints, res);
99048d19d4SFlorian Westphal 
100048d19d4SFlorian Westphal 	if (err) {
101048d19d4SFlorian Westphal 		const char *errstr = getxinfo_strerr(err);
102048d19d4SFlorian Westphal 
103048d19d4SFlorian Westphal 		fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n",
104048d19d4SFlorian Westphal 			node ? node : "", service ? service : "", errstr);
105048d19d4SFlorian Westphal 		exit(1);
106048d19d4SFlorian Westphal 	}
107048d19d4SFlorian Westphal }
108048d19d4SFlorian Westphal 
109*8a4b910dSFlorian Westphal static void set_rcvbuf(int fd, unsigned int size)
110*8a4b910dSFlorian Westphal {
111*8a4b910dSFlorian Westphal 	int err;
112*8a4b910dSFlorian Westphal 
113*8a4b910dSFlorian Westphal 	err = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
114*8a4b910dSFlorian Westphal 	if (err) {
115*8a4b910dSFlorian Westphal 		perror("set SO_RCVBUF");
116*8a4b910dSFlorian Westphal 		exit(1);
117*8a4b910dSFlorian Westphal 	}
118*8a4b910dSFlorian Westphal }
119*8a4b910dSFlorian Westphal 
120048d19d4SFlorian Westphal static void set_sndbuf(int fd, unsigned int size)
121048d19d4SFlorian Westphal {
122048d19d4SFlorian Westphal 	int err;
123048d19d4SFlorian Westphal 
124048d19d4SFlorian Westphal 	err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
125048d19d4SFlorian Westphal 	if (err) {
126048d19d4SFlorian Westphal 		perror("set SO_SNDBUF");
127048d19d4SFlorian Westphal 		exit(1);
128048d19d4SFlorian Westphal 	}
129048d19d4SFlorian Westphal }
130048d19d4SFlorian Westphal 
131048d19d4SFlorian Westphal static int sock_listen_mptcp(const char * const listenaddr,
132048d19d4SFlorian Westphal 			     const char * const port)
133048d19d4SFlorian Westphal {
134048d19d4SFlorian Westphal 	int sock;
135048d19d4SFlorian Westphal 	struct addrinfo hints = {
136048d19d4SFlorian Westphal 		.ai_protocol = IPPROTO_TCP,
137048d19d4SFlorian Westphal 		.ai_socktype = SOCK_STREAM,
138048d19d4SFlorian Westphal 		.ai_flags = AI_PASSIVE | AI_NUMERICHOST
139048d19d4SFlorian Westphal 	};
140048d19d4SFlorian Westphal 
141048d19d4SFlorian Westphal 	hints.ai_family = pf;
142048d19d4SFlorian Westphal 
143048d19d4SFlorian Westphal 	struct addrinfo *a, *addr;
144048d19d4SFlorian Westphal 	int one = 1;
145048d19d4SFlorian Westphal 
146048d19d4SFlorian Westphal 	xgetaddrinfo(listenaddr, port, &hints, &addr);
147048d19d4SFlorian Westphal 	hints.ai_family = pf;
148048d19d4SFlorian Westphal 
149048d19d4SFlorian Westphal 	for (a = addr; a; a = a->ai_next) {
150048d19d4SFlorian Westphal 		sock = socket(a->ai_family, a->ai_socktype, cfg_sock_proto);
151048d19d4SFlorian Westphal 		if (sock < 0)
152048d19d4SFlorian Westphal 			continue;
153048d19d4SFlorian Westphal 
154048d19d4SFlorian Westphal 		if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one,
155048d19d4SFlorian Westphal 				     sizeof(one)))
156048d19d4SFlorian Westphal 			perror("setsockopt");
157048d19d4SFlorian Westphal 
158048d19d4SFlorian Westphal 		if (bind(sock, a->ai_addr, a->ai_addrlen) == 0)
159048d19d4SFlorian Westphal 			break; /* success */
160048d19d4SFlorian Westphal 
161048d19d4SFlorian Westphal 		perror("bind");
162048d19d4SFlorian Westphal 		close(sock);
163048d19d4SFlorian Westphal 		sock = -1;
164048d19d4SFlorian Westphal 	}
165048d19d4SFlorian Westphal 
166048d19d4SFlorian Westphal 	freeaddrinfo(addr);
167048d19d4SFlorian Westphal 
168048d19d4SFlorian Westphal 	if (sock < 0) {
169048d19d4SFlorian Westphal 		fprintf(stderr, "Could not create listen socket\n");
170048d19d4SFlorian Westphal 		return sock;
171048d19d4SFlorian Westphal 	}
172048d19d4SFlorian Westphal 
173048d19d4SFlorian Westphal 	if (listen(sock, 20)) {
174048d19d4SFlorian Westphal 		perror("listen");
175048d19d4SFlorian Westphal 		close(sock);
176048d19d4SFlorian Westphal 		return -1;
177048d19d4SFlorian Westphal 	}
178048d19d4SFlorian Westphal 
179048d19d4SFlorian Westphal 	return sock;
180048d19d4SFlorian Westphal }
181048d19d4SFlorian Westphal 
182048d19d4SFlorian Westphal static bool sock_test_tcpulp(const char * const remoteaddr,
183048d19d4SFlorian Westphal 			     const char * const port)
184048d19d4SFlorian Westphal {
185048d19d4SFlorian Westphal 	struct addrinfo hints = {
186048d19d4SFlorian Westphal 		.ai_protocol = IPPROTO_TCP,
187048d19d4SFlorian Westphal 		.ai_socktype = SOCK_STREAM,
188048d19d4SFlorian Westphal 	};
189048d19d4SFlorian Westphal 	struct addrinfo *a, *addr;
190048d19d4SFlorian Westphal 	int sock = -1, ret = 0;
191048d19d4SFlorian Westphal 	bool test_pass = false;
192048d19d4SFlorian Westphal 
193048d19d4SFlorian Westphal 	hints.ai_family = AF_INET;
194048d19d4SFlorian Westphal 
195048d19d4SFlorian Westphal 	xgetaddrinfo(remoteaddr, port, &hints, &addr);
196048d19d4SFlorian Westphal 	for (a = addr; a; a = a->ai_next) {
197048d19d4SFlorian Westphal 		sock = socket(a->ai_family, a->ai_socktype, IPPROTO_TCP);
198048d19d4SFlorian Westphal 		if (sock < 0) {
199048d19d4SFlorian Westphal 			perror("socket");
200048d19d4SFlorian Westphal 			continue;
201048d19d4SFlorian Westphal 		}
202048d19d4SFlorian Westphal 		ret = setsockopt(sock, IPPROTO_TCP, TCP_ULP, "mptcp",
203048d19d4SFlorian Westphal 				 sizeof("mptcp"));
204048d19d4SFlorian Westphal 		if (ret == -1 && errno == EOPNOTSUPP)
205048d19d4SFlorian Westphal 			test_pass = true;
206048d19d4SFlorian Westphal 		close(sock);
207048d19d4SFlorian Westphal 
208048d19d4SFlorian Westphal 		if (test_pass)
209048d19d4SFlorian Westphal 			break;
210048d19d4SFlorian Westphal 		if (!ret)
211048d19d4SFlorian Westphal 			fprintf(stderr,
212048d19d4SFlorian Westphal 				"setsockopt(TCP_ULP) returned 0\n");
213048d19d4SFlorian Westphal 		else
214048d19d4SFlorian Westphal 			perror("setsockopt(TCP_ULP)");
215048d19d4SFlorian Westphal 	}
216048d19d4SFlorian Westphal 	return test_pass;
217048d19d4SFlorian Westphal }
218048d19d4SFlorian Westphal 
219048d19d4SFlorian Westphal static int sock_connect_mptcp(const char * const remoteaddr,
220048d19d4SFlorian Westphal 			      const char * const port, int proto)
221048d19d4SFlorian Westphal {
222048d19d4SFlorian Westphal 	struct addrinfo hints = {
223048d19d4SFlorian Westphal 		.ai_protocol = IPPROTO_TCP,
224048d19d4SFlorian Westphal 		.ai_socktype = SOCK_STREAM,
225048d19d4SFlorian Westphal 	};
226048d19d4SFlorian Westphal 	struct addrinfo *a, *addr;
227048d19d4SFlorian Westphal 	int sock = -1;
228048d19d4SFlorian Westphal 
229048d19d4SFlorian Westphal 	hints.ai_family = pf;
230048d19d4SFlorian Westphal 
231048d19d4SFlorian Westphal 	xgetaddrinfo(remoteaddr, port, &hints, &addr);
232048d19d4SFlorian Westphal 	for (a = addr; a; a = a->ai_next) {
233048d19d4SFlorian Westphal 		sock = socket(a->ai_family, a->ai_socktype, proto);
234048d19d4SFlorian Westphal 		if (sock < 0) {
235048d19d4SFlorian Westphal 			perror("socket");
236048d19d4SFlorian Westphal 			continue;
237048d19d4SFlorian Westphal 		}
238048d19d4SFlorian Westphal 
239048d19d4SFlorian Westphal 		if (connect(sock, a->ai_addr, a->ai_addrlen) == 0)
240048d19d4SFlorian Westphal 			break; /* success */
241048d19d4SFlorian Westphal 
242048d19d4SFlorian Westphal 		perror("connect()");
243048d19d4SFlorian Westphal 		close(sock);
244048d19d4SFlorian Westphal 		sock = -1;
245048d19d4SFlorian Westphal 	}
246048d19d4SFlorian Westphal 
247048d19d4SFlorian Westphal 	freeaddrinfo(addr);
248048d19d4SFlorian Westphal 	return sock;
249048d19d4SFlorian Westphal }
250048d19d4SFlorian Westphal 
251048d19d4SFlorian Westphal static size_t do_rnd_write(const int fd, char *buf, const size_t len)
252048d19d4SFlorian Westphal {
253048d19d4SFlorian Westphal 	unsigned int do_w;
254048d19d4SFlorian Westphal 	ssize_t bw;
255048d19d4SFlorian Westphal 
256048d19d4SFlorian Westphal 	do_w = rand() & 0xffff;
257048d19d4SFlorian Westphal 	if (do_w == 0 || do_w > len)
258048d19d4SFlorian Westphal 		do_w = len;
259048d19d4SFlorian Westphal 
260048d19d4SFlorian Westphal 	bw = write(fd, buf, do_w);
261048d19d4SFlorian Westphal 	if (bw < 0)
262048d19d4SFlorian Westphal 		perror("write");
263048d19d4SFlorian Westphal 
264048d19d4SFlorian Westphal 	return bw;
265048d19d4SFlorian Westphal }
266048d19d4SFlorian Westphal 
267048d19d4SFlorian Westphal static size_t do_write(const int fd, char *buf, const size_t len)
268048d19d4SFlorian Westphal {
269048d19d4SFlorian Westphal 	size_t offset = 0;
270048d19d4SFlorian Westphal 
271048d19d4SFlorian Westphal 	while (offset < len) {
272048d19d4SFlorian Westphal 		size_t written;
273048d19d4SFlorian Westphal 		ssize_t bw;
274048d19d4SFlorian Westphal 
275048d19d4SFlorian Westphal 		bw = write(fd, buf + offset, len - offset);
276048d19d4SFlorian Westphal 		if (bw < 0) {
277048d19d4SFlorian Westphal 			perror("write");
278048d19d4SFlorian Westphal 			return 0;
279048d19d4SFlorian Westphal 		}
280048d19d4SFlorian Westphal 
281048d19d4SFlorian Westphal 		written = (size_t)bw;
282048d19d4SFlorian Westphal 		offset += written;
283048d19d4SFlorian Westphal 	}
284048d19d4SFlorian Westphal 
285048d19d4SFlorian Westphal 	return offset;
286048d19d4SFlorian Westphal }
287048d19d4SFlorian Westphal 
288048d19d4SFlorian Westphal static ssize_t do_rnd_read(const int fd, char *buf, const size_t len)
289048d19d4SFlorian Westphal {
290048d19d4SFlorian Westphal 	size_t cap = rand();
291048d19d4SFlorian Westphal 
292048d19d4SFlorian Westphal 	cap &= 0xffff;
293048d19d4SFlorian Westphal 
294048d19d4SFlorian Westphal 	if (cap == 0)
295048d19d4SFlorian Westphal 		cap = 1;
296048d19d4SFlorian Westphal 	else if (cap > len)
297048d19d4SFlorian Westphal 		cap = len;
298048d19d4SFlorian Westphal 
299048d19d4SFlorian Westphal 	return read(fd, buf, cap);
300048d19d4SFlorian Westphal }
301048d19d4SFlorian Westphal 
302048d19d4SFlorian Westphal static void set_nonblock(int fd)
303048d19d4SFlorian Westphal {
304048d19d4SFlorian Westphal 	int flags = fcntl(fd, F_GETFL);
305048d19d4SFlorian Westphal 
306048d19d4SFlorian Westphal 	if (flags == -1)
307048d19d4SFlorian Westphal 		return;
308048d19d4SFlorian Westphal 
309048d19d4SFlorian Westphal 	fcntl(fd, F_SETFL, flags | O_NONBLOCK);
310048d19d4SFlorian Westphal }
311048d19d4SFlorian Westphal 
312048d19d4SFlorian Westphal static int copyfd_io_poll(int infd, int peerfd, int outfd)
313048d19d4SFlorian Westphal {
314048d19d4SFlorian Westphal 	struct pollfd fds = {
315048d19d4SFlorian Westphal 		.fd = peerfd,
316048d19d4SFlorian Westphal 		.events = POLLIN | POLLOUT,
317048d19d4SFlorian Westphal 	};
318048d19d4SFlorian Westphal 	unsigned int woff = 0, wlen = 0;
319048d19d4SFlorian Westphal 	char wbuf[8192];
320048d19d4SFlorian Westphal 
321048d19d4SFlorian Westphal 	set_nonblock(peerfd);
322048d19d4SFlorian Westphal 
323048d19d4SFlorian Westphal 	for (;;) {
324048d19d4SFlorian Westphal 		char rbuf[8192];
325048d19d4SFlorian Westphal 		ssize_t len;
326048d19d4SFlorian Westphal 
327048d19d4SFlorian Westphal 		if (fds.events == 0)
328048d19d4SFlorian Westphal 			break;
329048d19d4SFlorian Westphal 
330048d19d4SFlorian Westphal 		switch (poll(&fds, 1, poll_timeout)) {
331048d19d4SFlorian Westphal 		case -1:
332048d19d4SFlorian Westphal 			if (errno == EINTR)
333048d19d4SFlorian Westphal 				continue;
334048d19d4SFlorian Westphal 			perror("poll");
335048d19d4SFlorian Westphal 			return 1;
336048d19d4SFlorian Westphal 		case 0:
337048d19d4SFlorian Westphal 			fprintf(stderr, "%s: poll timed out (events: "
338048d19d4SFlorian Westphal 				"POLLIN %u, POLLOUT %u)\n", __func__,
339048d19d4SFlorian Westphal 				fds.events & POLLIN, fds.events & POLLOUT);
340048d19d4SFlorian Westphal 			return 2;
341048d19d4SFlorian Westphal 		}
342048d19d4SFlorian Westphal 
343048d19d4SFlorian Westphal 		if (fds.revents & POLLIN) {
344048d19d4SFlorian Westphal 			len = do_rnd_read(peerfd, rbuf, sizeof(rbuf));
345048d19d4SFlorian Westphal 			if (len == 0) {
346048d19d4SFlorian Westphal 				/* no more data to receive:
347048d19d4SFlorian Westphal 				 * peer has closed its write side
348048d19d4SFlorian Westphal 				 */
349048d19d4SFlorian Westphal 				fds.events &= ~POLLIN;
350048d19d4SFlorian Westphal 
351048d19d4SFlorian Westphal 				if ((fds.events & POLLOUT) == 0)
352048d19d4SFlorian Westphal 					/* and nothing more to send */
353048d19d4SFlorian Westphal 					break;
354048d19d4SFlorian Westphal 
355048d19d4SFlorian Westphal 			/* Else, still have data to transmit */
356048d19d4SFlorian Westphal 			} else if (len < 0) {
357048d19d4SFlorian Westphal 				perror("read");
358048d19d4SFlorian Westphal 				return 3;
359048d19d4SFlorian Westphal 			}
360048d19d4SFlorian Westphal 
361048d19d4SFlorian Westphal 			do_write(outfd, rbuf, len);
362048d19d4SFlorian Westphal 		}
363048d19d4SFlorian Westphal 
364048d19d4SFlorian Westphal 		if (fds.revents & POLLOUT) {
365048d19d4SFlorian Westphal 			if (wlen == 0) {
366048d19d4SFlorian Westphal 				woff = 0;
367048d19d4SFlorian Westphal 				wlen = read(infd, wbuf, sizeof(wbuf));
368048d19d4SFlorian Westphal 			}
369048d19d4SFlorian Westphal 
370048d19d4SFlorian Westphal 			if (wlen > 0) {
371048d19d4SFlorian Westphal 				ssize_t bw;
372048d19d4SFlorian Westphal 
373048d19d4SFlorian Westphal 				bw = do_rnd_write(peerfd, wbuf + woff, wlen);
374048d19d4SFlorian Westphal 				if (bw < 0)
375048d19d4SFlorian Westphal 					return 111;
376048d19d4SFlorian Westphal 
377048d19d4SFlorian Westphal 				woff += bw;
378048d19d4SFlorian Westphal 				wlen -= bw;
379048d19d4SFlorian Westphal 			} else if (wlen == 0) {
380048d19d4SFlorian Westphal 				/* We have no more data to send. */
381048d19d4SFlorian Westphal 				fds.events &= ~POLLOUT;
382048d19d4SFlorian Westphal 
383048d19d4SFlorian Westphal 				if ((fds.events & POLLIN) == 0)
384048d19d4SFlorian Westphal 					/* ... and peer also closed already */
385048d19d4SFlorian Westphal 					break;
386048d19d4SFlorian Westphal 
387048d19d4SFlorian Westphal 				/* ... but we still receive.
388048d19d4SFlorian Westphal 				 * Close our write side.
389048d19d4SFlorian Westphal 				 */
390048d19d4SFlorian Westphal 				shutdown(peerfd, SHUT_WR);
391048d19d4SFlorian Westphal 			} else {
392048d19d4SFlorian Westphal 				if (errno == EINTR)
393048d19d4SFlorian Westphal 					continue;
394048d19d4SFlorian Westphal 				perror("read");
395048d19d4SFlorian Westphal 				return 4;
396048d19d4SFlorian Westphal 			}
397048d19d4SFlorian Westphal 		}
398048d19d4SFlorian Westphal 
399048d19d4SFlorian Westphal 		if (fds.revents & (POLLERR | POLLNVAL)) {
400048d19d4SFlorian Westphal 			fprintf(stderr, "Unexpected revents: "
401048d19d4SFlorian Westphal 				"POLLERR/POLLNVAL(%x)\n", fds.revents);
402048d19d4SFlorian Westphal 			return 5;
403048d19d4SFlorian Westphal 		}
404048d19d4SFlorian Westphal 	}
405048d19d4SFlorian Westphal 
406048d19d4SFlorian Westphal 	close(peerfd);
407048d19d4SFlorian Westphal 	return 0;
408048d19d4SFlorian Westphal }
409048d19d4SFlorian Westphal 
410048d19d4SFlorian Westphal static int do_recvfile(int infd, int outfd)
411048d19d4SFlorian Westphal {
412048d19d4SFlorian Westphal 	ssize_t r;
413048d19d4SFlorian Westphal 
414048d19d4SFlorian Westphal 	do {
415048d19d4SFlorian Westphal 		char buf[16384];
416048d19d4SFlorian Westphal 
417048d19d4SFlorian Westphal 		r = do_rnd_read(infd, buf, sizeof(buf));
418048d19d4SFlorian Westphal 		if (r > 0) {
419048d19d4SFlorian Westphal 			if (write(outfd, buf, r) != r)
420048d19d4SFlorian Westphal 				break;
421048d19d4SFlorian Westphal 		} else if (r < 0) {
422048d19d4SFlorian Westphal 			perror("read");
423048d19d4SFlorian Westphal 		}
424048d19d4SFlorian Westphal 	} while (r > 0);
425048d19d4SFlorian Westphal 
426048d19d4SFlorian Westphal 	return (int)r;
427048d19d4SFlorian Westphal }
428048d19d4SFlorian Westphal 
429048d19d4SFlorian Westphal static int do_mmap(int infd, int outfd, unsigned int size)
430048d19d4SFlorian Westphal {
431048d19d4SFlorian Westphal 	char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0);
432048d19d4SFlorian Westphal 	ssize_t ret = 0, off = 0;
433048d19d4SFlorian Westphal 	size_t rem;
434048d19d4SFlorian Westphal 
435048d19d4SFlorian Westphal 	if (inbuf == MAP_FAILED) {
436048d19d4SFlorian Westphal 		perror("mmap");
437048d19d4SFlorian Westphal 		return 1;
438048d19d4SFlorian Westphal 	}
439048d19d4SFlorian Westphal 
440048d19d4SFlorian Westphal 	rem = size;
441048d19d4SFlorian Westphal 
442048d19d4SFlorian Westphal 	while (rem > 0) {
443048d19d4SFlorian Westphal 		ret = write(outfd, inbuf + off, rem);
444048d19d4SFlorian Westphal 
445048d19d4SFlorian Westphal 		if (ret < 0) {
446048d19d4SFlorian Westphal 			perror("write");
447048d19d4SFlorian Westphal 			break;
448048d19d4SFlorian Westphal 		}
449048d19d4SFlorian Westphal 
450048d19d4SFlorian Westphal 		off += ret;
451048d19d4SFlorian Westphal 		rem -= ret;
452048d19d4SFlorian Westphal 	}
453048d19d4SFlorian Westphal 
454048d19d4SFlorian Westphal 	munmap(inbuf, size);
455048d19d4SFlorian Westphal 	return rem;
456048d19d4SFlorian Westphal }
457048d19d4SFlorian Westphal 
458048d19d4SFlorian Westphal static int get_infd_size(int fd)
459048d19d4SFlorian Westphal {
460048d19d4SFlorian Westphal 	struct stat sb;
461048d19d4SFlorian Westphal 	ssize_t count;
462048d19d4SFlorian Westphal 	int err;
463048d19d4SFlorian Westphal 
464048d19d4SFlorian Westphal 	err = fstat(fd, &sb);
465048d19d4SFlorian Westphal 	if (err < 0) {
466048d19d4SFlorian Westphal 		perror("fstat");
467048d19d4SFlorian Westphal 		return -1;
468048d19d4SFlorian Westphal 	}
469048d19d4SFlorian Westphal 
470048d19d4SFlorian Westphal 	if ((sb.st_mode & S_IFMT) != S_IFREG) {
471048d19d4SFlorian Westphal 		fprintf(stderr, "%s: stdin is not a regular file\n", __func__);
472048d19d4SFlorian Westphal 		return -2;
473048d19d4SFlorian Westphal 	}
474048d19d4SFlorian Westphal 
475048d19d4SFlorian Westphal 	count = sb.st_size;
476048d19d4SFlorian Westphal 	if (count > INT_MAX) {
477048d19d4SFlorian Westphal 		fprintf(stderr, "File too large: %zu\n", count);
478048d19d4SFlorian Westphal 		return -3;
479048d19d4SFlorian Westphal 	}
480048d19d4SFlorian Westphal 
481048d19d4SFlorian Westphal 	return (int)count;
482048d19d4SFlorian Westphal }
483048d19d4SFlorian Westphal 
484048d19d4SFlorian Westphal static int do_sendfile(int infd, int outfd, unsigned int count)
485048d19d4SFlorian Westphal {
486048d19d4SFlorian Westphal 	while (count > 0) {
487048d19d4SFlorian Westphal 		ssize_t r;
488048d19d4SFlorian Westphal 
489048d19d4SFlorian Westphal 		r = sendfile(outfd, infd, NULL, count);
490048d19d4SFlorian Westphal 		if (r < 0) {
491048d19d4SFlorian Westphal 			perror("sendfile");
492048d19d4SFlorian Westphal 			return 3;
493048d19d4SFlorian Westphal 		}
494048d19d4SFlorian Westphal 
495048d19d4SFlorian Westphal 		count -= r;
496048d19d4SFlorian Westphal 	}
497048d19d4SFlorian Westphal 
498048d19d4SFlorian Westphal 	return 0;
499048d19d4SFlorian Westphal }
500048d19d4SFlorian Westphal 
501048d19d4SFlorian Westphal static int copyfd_io_mmap(int infd, int peerfd, int outfd,
502048d19d4SFlorian Westphal 			  unsigned int size)
503048d19d4SFlorian Westphal {
504048d19d4SFlorian Westphal 	int err;
505048d19d4SFlorian Westphal 
506048d19d4SFlorian Westphal 	if (listen_mode) {
507048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
508048d19d4SFlorian Westphal 		if (err)
509048d19d4SFlorian Westphal 			return err;
510048d19d4SFlorian Westphal 
511048d19d4SFlorian Westphal 		err = do_mmap(infd, peerfd, size);
512048d19d4SFlorian Westphal 	} else {
513048d19d4SFlorian Westphal 		err = do_mmap(infd, peerfd, size);
514048d19d4SFlorian Westphal 		if (err)
515048d19d4SFlorian Westphal 			return err;
516048d19d4SFlorian Westphal 
517048d19d4SFlorian Westphal 		shutdown(peerfd, SHUT_WR);
518048d19d4SFlorian Westphal 
519048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
520048d19d4SFlorian Westphal 	}
521048d19d4SFlorian Westphal 
522048d19d4SFlorian Westphal 	return err;
523048d19d4SFlorian Westphal }
524048d19d4SFlorian Westphal 
525048d19d4SFlorian Westphal static int copyfd_io_sendfile(int infd, int peerfd, int outfd,
526048d19d4SFlorian Westphal 			      unsigned int size)
527048d19d4SFlorian Westphal {
528048d19d4SFlorian Westphal 	int err;
529048d19d4SFlorian Westphal 
530048d19d4SFlorian Westphal 	if (listen_mode) {
531048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
532048d19d4SFlorian Westphal 		if (err)
533048d19d4SFlorian Westphal 			return err;
534048d19d4SFlorian Westphal 
535048d19d4SFlorian Westphal 		err = do_sendfile(infd, peerfd, size);
536048d19d4SFlorian Westphal 	} else {
537048d19d4SFlorian Westphal 		err = do_sendfile(infd, peerfd, size);
538048d19d4SFlorian Westphal 		if (err)
539048d19d4SFlorian Westphal 			return err;
540048d19d4SFlorian Westphal 		err = do_recvfile(peerfd, outfd);
541048d19d4SFlorian Westphal 	}
542048d19d4SFlorian Westphal 
543048d19d4SFlorian Westphal 	return err;
544048d19d4SFlorian Westphal }
545048d19d4SFlorian Westphal 
546048d19d4SFlorian Westphal static int copyfd_io(int infd, int peerfd, int outfd)
547048d19d4SFlorian Westphal {
548048d19d4SFlorian Westphal 	int file_size;
549048d19d4SFlorian Westphal 
550048d19d4SFlorian Westphal 	switch (cfg_mode) {
551048d19d4SFlorian Westphal 	case CFG_MODE_POLL:
552048d19d4SFlorian Westphal 		return copyfd_io_poll(infd, peerfd, outfd);
553048d19d4SFlorian Westphal 	case CFG_MODE_MMAP:
554048d19d4SFlorian Westphal 		file_size = get_infd_size(infd);
555048d19d4SFlorian Westphal 		if (file_size < 0)
556048d19d4SFlorian Westphal 			return file_size;
557048d19d4SFlorian Westphal 		return copyfd_io_mmap(infd, peerfd, outfd, file_size);
558048d19d4SFlorian Westphal 	case CFG_MODE_SENDFILE:
559048d19d4SFlorian Westphal 		file_size = get_infd_size(infd);
560048d19d4SFlorian Westphal 		if (file_size < 0)
561048d19d4SFlorian Westphal 			return file_size;
562048d19d4SFlorian Westphal 		return copyfd_io_sendfile(infd, peerfd, outfd, file_size);
563048d19d4SFlorian Westphal 	}
564048d19d4SFlorian Westphal 
565048d19d4SFlorian Westphal 	fprintf(stderr, "Invalid mode %d\n", cfg_mode);
566048d19d4SFlorian Westphal 
567048d19d4SFlorian Westphal 	die_usage();
568048d19d4SFlorian Westphal 	return 1;
569048d19d4SFlorian Westphal }
570048d19d4SFlorian Westphal 
571048d19d4SFlorian Westphal static void check_sockaddr(int pf, struct sockaddr_storage *ss,
572048d19d4SFlorian Westphal 			   socklen_t salen)
573048d19d4SFlorian Westphal {
574048d19d4SFlorian Westphal 	struct sockaddr_in6 *sin6;
575048d19d4SFlorian Westphal 	struct sockaddr_in *sin;
576048d19d4SFlorian Westphal 	socklen_t wanted_size = 0;
577048d19d4SFlorian Westphal 
578048d19d4SFlorian Westphal 	switch (pf) {
579048d19d4SFlorian Westphal 	case AF_INET:
580048d19d4SFlorian Westphal 		wanted_size = sizeof(*sin);
581048d19d4SFlorian Westphal 		sin = (void *)ss;
582048d19d4SFlorian Westphal 		if (!sin->sin_port)
583048d19d4SFlorian Westphal 			fprintf(stderr, "accept: something wrong: ip connection from port 0");
584048d19d4SFlorian Westphal 		break;
585048d19d4SFlorian Westphal 	case AF_INET6:
586048d19d4SFlorian Westphal 		wanted_size = sizeof(*sin6);
587048d19d4SFlorian Westphal 		sin6 = (void *)ss;
588048d19d4SFlorian Westphal 		if (!sin6->sin6_port)
589048d19d4SFlorian Westphal 			fprintf(stderr, "accept: something wrong: ipv6 connection from port 0");
590048d19d4SFlorian Westphal 		break;
591048d19d4SFlorian Westphal 	default:
592048d19d4SFlorian Westphal 		fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen);
593048d19d4SFlorian Westphal 		return;
594048d19d4SFlorian Westphal 	}
595048d19d4SFlorian Westphal 
596048d19d4SFlorian Westphal 	if (salen != wanted_size)
597048d19d4SFlorian Westphal 		fprintf(stderr, "accept: size mismatch, got %d expected %d\n",
598048d19d4SFlorian Westphal 			(int)salen, wanted_size);
599048d19d4SFlorian Westphal 
600048d19d4SFlorian Westphal 	if (ss->ss_family != pf)
601048d19d4SFlorian Westphal 		fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n",
602048d19d4SFlorian Westphal 			(int)ss->ss_family, pf);
603048d19d4SFlorian Westphal }
604048d19d4SFlorian Westphal 
605048d19d4SFlorian Westphal static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen)
606048d19d4SFlorian Westphal {
607048d19d4SFlorian Westphal 	struct sockaddr_storage peerss;
608048d19d4SFlorian Westphal 	socklen_t peersalen = sizeof(peerss);
609048d19d4SFlorian Westphal 
610048d19d4SFlorian Westphal 	if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) {
611048d19d4SFlorian Westphal 		perror("getpeername");
612048d19d4SFlorian Westphal 		return;
613048d19d4SFlorian Westphal 	}
614048d19d4SFlorian Westphal 
615048d19d4SFlorian Westphal 	if (peersalen != salen) {
616048d19d4SFlorian Westphal 		fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen);
617048d19d4SFlorian Westphal 		return;
618048d19d4SFlorian Westphal 	}
619048d19d4SFlorian Westphal 
620048d19d4SFlorian Westphal 	if (memcmp(ss, &peerss, peersalen)) {
621048d19d4SFlorian Westphal 		char a[INET6_ADDRSTRLEN];
622048d19d4SFlorian Westphal 		char b[INET6_ADDRSTRLEN];
623048d19d4SFlorian Westphal 		char c[INET6_ADDRSTRLEN];
624048d19d4SFlorian Westphal 		char d[INET6_ADDRSTRLEN];
625048d19d4SFlorian Westphal 
626048d19d4SFlorian Westphal 		xgetnameinfo((struct sockaddr *)ss, salen,
627048d19d4SFlorian Westphal 			     a, sizeof(a), b, sizeof(b));
628048d19d4SFlorian Westphal 
629048d19d4SFlorian Westphal 		xgetnameinfo((struct sockaddr *)&peerss, peersalen,
630048d19d4SFlorian Westphal 			     c, sizeof(c), d, sizeof(d));
631048d19d4SFlorian Westphal 
632048d19d4SFlorian Westphal 		fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n",
633048d19d4SFlorian Westphal 			__func__, a, c, b, d, peersalen, salen);
634048d19d4SFlorian Westphal 	}
635048d19d4SFlorian Westphal }
636048d19d4SFlorian Westphal 
637048d19d4SFlorian Westphal static void check_getpeername_connect(int fd)
638048d19d4SFlorian Westphal {
639048d19d4SFlorian Westphal 	struct sockaddr_storage ss;
640048d19d4SFlorian Westphal 	socklen_t salen = sizeof(ss);
641048d19d4SFlorian Westphal 	char a[INET6_ADDRSTRLEN];
642048d19d4SFlorian Westphal 	char b[INET6_ADDRSTRLEN];
643048d19d4SFlorian Westphal 
644048d19d4SFlorian Westphal 	if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) {
645048d19d4SFlorian Westphal 		perror("getpeername");
646048d19d4SFlorian Westphal 		return;
647048d19d4SFlorian Westphal 	}
648048d19d4SFlorian Westphal 
649048d19d4SFlorian Westphal 	xgetnameinfo((struct sockaddr *)&ss, salen,
650048d19d4SFlorian Westphal 		     a, sizeof(a), b, sizeof(b));
651048d19d4SFlorian Westphal 
652048d19d4SFlorian Westphal 	if (strcmp(cfg_host, a) || strcmp(cfg_port, b))
653048d19d4SFlorian Westphal 		fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__,
654048d19d4SFlorian Westphal 			cfg_host, a, cfg_port, b);
655048d19d4SFlorian Westphal }
656048d19d4SFlorian Westphal 
657b0519de8SFlorian Westphal static void maybe_close(int fd)
658b0519de8SFlorian Westphal {
659b0519de8SFlorian Westphal 	unsigned int r = rand();
660b0519de8SFlorian Westphal 
661b0519de8SFlorian Westphal 	if (r & 1)
662b0519de8SFlorian Westphal 		close(fd);
663b0519de8SFlorian Westphal }
664b0519de8SFlorian Westphal 
665048d19d4SFlorian Westphal int main_loop_s(int listensock)
666048d19d4SFlorian Westphal {
667048d19d4SFlorian Westphal 	struct sockaddr_storage ss;
668048d19d4SFlorian Westphal 	struct pollfd polls;
669048d19d4SFlorian Westphal 	socklen_t salen;
670048d19d4SFlorian Westphal 	int remotesock;
671048d19d4SFlorian Westphal 
672048d19d4SFlorian Westphal 	polls.fd = listensock;
673048d19d4SFlorian Westphal 	polls.events = POLLIN;
674048d19d4SFlorian Westphal 
675048d19d4SFlorian Westphal 	switch (poll(&polls, 1, poll_timeout)) {
676048d19d4SFlorian Westphal 	case -1:
677048d19d4SFlorian Westphal 		perror("poll");
678048d19d4SFlorian Westphal 		return 1;
679048d19d4SFlorian Westphal 	case 0:
680048d19d4SFlorian Westphal 		fprintf(stderr, "%s: timed out\n", __func__);
681048d19d4SFlorian Westphal 		close(listensock);
682048d19d4SFlorian Westphal 		return 2;
683048d19d4SFlorian Westphal 	}
684048d19d4SFlorian Westphal 
685048d19d4SFlorian Westphal 	salen = sizeof(ss);
686048d19d4SFlorian Westphal 	remotesock = accept(listensock, (struct sockaddr *)&ss, &salen);
687048d19d4SFlorian Westphal 	if (remotesock >= 0) {
688b0519de8SFlorian Westphal 		maybe_close(listensock);
689048d19d4SFlorian Westphal 		check_sockaddr(pf, &ss, salen);
690048d19d4SFlorian Westphal 		check_getpeername(remotesock, &ss, salen);
691048d19d4SFlorian Westphal 
692048d19d4SFlorian Westphal 		return copyfd_io(0, remotesock, 1);
693048d19d4SFlorian Westphal 	}
694048d19d4SFlorian Westphal 
695048d19d4SFlorian Westphal 	perror("accept");
696048d19d4SFlorian Westphal 
697048d19d4SFlorian Westphal 	return 1;
698048d19d4SFlorian Westphal }
699048d19d4SFlorian Westphal 
700048d19d4SFlorian Westphal static void init_rng(void)
701048d19d4SFlorian Westphal {
702048d19d4SFlorian Westphal 	int fd = open("/dev/urandom", O_RDONLY);
703048d19d4SFlorian Westphal 	unsigned int foo;
704048d19d4SFlorian Westphal 
705048d19d4SFlorian Westphal 	if (fd > 0) {
706048d19d4SFlorian Westphal 		int ret = read(fd, &foo, sizeof(foo));
707048d19d4SFlorian Westphal 
708048d19d4SFlorian Westphal 		if (ret < 0)
709048d19d4SFlorian Westphal 			srand(fd + foo);
710048d19d4SFlorian Westphal 		close(fd);
711048d19d4SFlorian Westphal 	}
712048d19d4SFlorian Westphal 
713048d19d4SFlorian Westphal 	srand(foo);
714048d19d4SFlorian Westphal }
715048d19d4SFlorian Westphal 
716048d19d4SFlorian Westphal int main_loop(void)
717048d19d4SFlorian Westphal {
718048d19d4SFlorian Westphal 	int fd;
719048d19d4SFlorian Westphal 
720048d19d4SFlorian Westphal 	/* listener is ready. */
721048d19d4SFlorian Westphal 	fd = sock_connect_mptcp(cfg_host, cfg_port, cfg_sock_proto);
722048d19d4SFlorian Westphal 	if (fd < 0)
723048d19d4SFlorian Westphal 		return 2;
724048d19d4SFlorian Westphal 
725048d19d4SFlorian Westphal 	check_getpeername_connect(fd);
726048d19d4SFlorian Westphal 
727*8a4b910dSFlorian Westphal 	if (cfg_rcvbuf)
728*8a4b910dSFlorian Westphal 		set_rcvbuf(fd, cfg_rcvbuf);
729048d19d4SFlorian Westphal 	if (cfg_sndbuf)
730048d19d4SFlorian Westphal 		set_sndbuf(fd, cfg_sndbuf);
731048d19d4SFlorian Westphal 
732048d19d4SFlorian Westphal 	return copyfd_io(0, fd, 1);
733048d19d4SFlorian Westphal }
734048d19d4SFlorian Westphal 
735048d19d4SFlorian Westphal int parse_proto(const char *proto)
736048d19d4SFlorian Westphal {
737048d19d4SFlorian Westphal 	if (!strcasecmp(proto, "MPTCP"))
738048d19d4SFlorian Westphal 		return IPPROTO_MPTCP;
739048d19d4SFlorian Westphal 	if (!strcasecmp(proto, "TCP"))
740048d19d4SFlorian Westphal 		return IPPROTO_TCP;
741048d19d4SFlorian Westphal 
742048d19d4SFlorian Westphal 	fprintf(stderr, "Unknown protocol: %s\n.", proto);
743048d19d4SFlorian Westphal 	die_usage();
744048d19d4SFlorian Westphal 
745048d19d4SFlorian Westphal 	/* silence compiler warning */
746048d19d4SFlorian Westphal 	return 0;
747048d19d4SFlorian Westphal }
748048d19d4SFlorian Westphal 
749048d19d4SFlorian Westphal int parse_mode(const char *mode)
750048d19d4SFlorian Westphal {
751048d19d4SFlorian Westphal 	if (!strcasecmp(mode, "poll"))
752048d19d4SFlorian Westphal 		return CFG_MODE_POLL;
753048d19d4SFlorian Westphal 	if (!strcasecmp(mode, "mmap"))
754048d19d4SFlorian Westphal 		return CFG_MODE_MMAP;
755048d19d4SFlorian Westphal 	if (!strcasecmp(mode, "sendfile"))
756048d19d4SFlorian Westphal 		return CFG_MODE_SENDFILE;
757048d19d4SFlorian Westphal 
758048d19d4SFlorian Westphal 	fprintf(stderr, "Unknown test mode: %s\n", mode);
759048d19d4SFlorian Westphal 	fprintf(stderr, "Supported modes are:\n");
760048d19d4SFlorian Westphal 	fprintf(stderr, "\t\t\"poll\" - interleaved read/write using poll()\n");
761048d19d4SFlorian Westphal 	fprintf(stderr, "\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n");
762048d19d4SFlorian Westphal 	fprintf(stderr, "\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n");
763048d19d4SFlorian Westphal 
764048d19d4SFlorian Westphal 	die_usage();
765048d19d4SFlorian Westphal 
766048d19d4SFlorian Westphal 	/* silence compiler warning */
767048d19d4SFlorian Westphal 	return 0;
768048d19d4SFlorian Westphal }
769048d19d4SFlorian Westphal 
770*8a4b910dSFlorian Westphal static int parse_int(const char *size)
771048d19d4SFlorian Westphal {
772048d19d4SFlorian Westphal 	unsigned long s;
773048d19d4SFlorian Westphal 
774048d19d4SFlorian Westphal 	errno = 0;
775048d19d4SFlorian Westphal 
776048d19d4SFlorian Westphal 	s = strtoul(size, NULL, 0);
777048d19d4SFlorian Westphal 
778048d19d4SFlorian Westphal 	if (errno) {
779048d19d4SFlorian Westphal 		fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
780048d19d4SFlorian Westphal 			size, strerror(errno));
781048d19d4SFlorian Westphal 		die_usage();
782048d19d4SFlorian Westphal 	}
783048d19d4SFlorian Westphal 
784048d19d4SFlorian Westphal 	if (s > INT_MAX) {
785048d19d4SFlorian Westphal 		fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
786048d19d4SFlorian Westphal 			size, strerror(ERANGE));
787048d19d4SFlorian Westphal 		die_usage();
788048d19d4SFlorian Westphal 	}
789048d19d4SFlorian Westphal 
790*8a4b910dSFlorian Westphal 	return (int)s;
791048d19d4SFlorian Westphal }
792048d19d4SFlorian Westphal 
793048d19d4SFlorian Westphal static void parse_opts(int argc, char **argv)
794048d19d4SFlorian Westphal {
795048d19d4SFlorian Westphal 	int c;
796048d19d4SFlorian Westphal 
797*8a4b910dSFlorian Westphal 	while ((c = getopt(argc, argv, "6lp:s:hut:m:S:R:")) != -1) {
798048d19d4SFlorian Westphal 		switch (c) {
799048d19d4SFlorian Westphal 		case 'l':
800048d19d4SFlorian Westphal 			listen_mode = true;
801048d19d4SFlorian Westphal 			break;
802048d19d4SFlorian Westphal 		case 'p':
803048d19d4SFlorian Westphal 			cfg_port = optarg;
804048d19d4SFlorian Westphal 			break;
805048d19d4SFlorian Westphal 		case 's':
806048d19d4SFlorian Westphal 			cfg_sock_proto = parse_proto(optarg);
807048d19d4SFlorian Westphal 			break;
808048d19d4SFlorian Westphal 		case 'h':
809048d19d4SFlorian Westphal 			die_usage();
810048d19d4SFlorian Westphal 			break;
811048d19d4SFlorian Westphal 		case 'u':
812048d19d4SFlorian Westphal 			tcpulp_audit = true;
813048d19d4SFlorian Westphal 			break;
814048d19d4SFlorian Westphal 		case '6':
815048d19d4SFlorian Westphal 			pf = AF_INET6;
816048d19d4SFlorian Westphal 			break;
817048d19d4SFlorian Westphal 		case 't':
818048d19d4SFlorian Westphal 			poll_timeout = atoi(optarg) * 1000;
819048d19d4SFlorian Westphal 			if (poll_timeout <= 0)
820048d19d4SFlorian Westphal 				poll_timeout = -1;
821048d19d4SFlorian Westphal 			break;
822048d19d4SFlorian Westphal 		case 'm':
823048d19d4SFlorian Westphal 			cfg_mode = parse_mode(optarg);
824048d19d4SFlorian Westphal 			break;
825*8a4b910dSFlorian Westphal 		case 'S':
826*8a4b910dSFlorian Westphal 			cfg_sndbuf = parse_int(optarg);
827*8a4b910dSFlorian Westphal 			break;
828*8a4b910dSFlorian Westphal 		case 'R':
829*8a4b910dSFlorian Westphal 			cfg_rcvbuf = parse_int(optarg);
830048d19d4SFlorian Westphal 			break;
831048d19d4SFlorian Westphal 		}
832048d19d4SFlorian Westphal 	}
833048d19d4SFlorian Westphal 
834048d19d4SFlorian Westphal 	if (optind + 1 != argc)
835048d19d4SFlorian Westphal 		die_usage();
836048d19d4SFlorian Westphal 	cfg_host = argv[optind];
837048d19d4SFlorian Westphal 
838048d19d4SFlorian Westphal 	if (strchr(cfg_host, ':'))
839048d19d4SFlorian Westphal 		pf = AF_INET6;
840048d19d4SFlorian Westphal }
841048d19d4SFlorian Westphal 
842048d19d4SFlorian Westphal int main(int argc, char *argv[])
843048d19d4SFlorian Westphal {
844048d19d4SFlorian Westphal 	init_rng();
845048d19d4SFlorian Westphal 
846048d19d4SFlorian Westphal 	parse_opts(argc, argv);
847048d19d4SFlorian Westphal 
848048d19d4SFlorian Westphal 	if (tcpulp_audit)
849048d19d4SFlorian Westphal 		return sock_test_tcpulp(cfg_host, cfg_port) ? 0 : 1;
850048d19d4SFlorian Westphal 
851048d19d4SFlorian Westphal 	if (listen_mode) {
852048d19d4SFlorian Westphal 		int fd = sock_listen_mptcp(cfg_host, cfg_port);
853048d19d4SFlorian Westphal 
854048d19d4SFlorian Westphal 		if (fd < 0)
855048d19d4SFlorian Westphal 			return 1;
856048d19d4SFlorian Westphal 
857*8a4b910dSFlorian Westphal 		if (cfg_rcvbuf)
858*8a4b910dSFlorian Westphal 			set_rcvbuf(fd, cfg_rcvbuf);
859048d19d4SFlorian Westphal 		if (cfg_sndbuf)
860048d19d4SFlorian Westphal 			set_sndbuf(fd, cfg_sndbuf);
861048d19d4SFlorian Westphal 
862048d19d4SFlorian Westphal 		return main_loop_s(fd);
863048d19d4SFlorian Westphal 	}
864048d19d4SFlorian Westphal 
865048d19d4SFlorian Westphal 	return main_loop();
866048d19d4SFlorian Westphal }
867