xref: /linux/tools/testing/selftests/net/mptcp/mptcp_connect.c (revision f2a3b12b305c7bb72467b2a56d19a4587b6007f9)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define _GNU_SOURCE
4 
5 #include <errno.h>
6 #include <limits.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <strings.h>
15 #include <signal.h>
16 #include <unistd.h>
17 #include <time.h>
18 
19 #include <sys/ioctl.h>
20 #include <sys/poll.h>
21 #include <sys/random.h>
22 #include <sys/sendfile.h>
23 #include <sys/stat.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/mman.h>
27 
28 #include <arpa/inet.h>
29 
30 #include <netdb.h>
31 #include <netinet/in.h>
32 
33 #include <linux/tcp.h>
34 #include <linux/time_types.h>
35 #include <linux/sockios.h>
36 #include <linux/compiler.h>
37 
38 extern int optind;
39 
40 #ifndef IPPROTO_MPTCP
41 #define IPPROTO_MPTCP 262
42 #endif
43 #ifndef TCP_ULP
44 #define TCP_ULP 31
45 #endif
46 
47 static int  poll_timeout = 10 * 1000;
48 static bool listen_mode;
49 static bool quit;
50 
51 enum cfg_mode {
52 	CFG_MODE_POLL,
53 	CFG_MODE_MMAP,
54 	CFG_MODE_SENDFILE,
55 };
56 
57 enum cfg_peek {
58 	CFG_NONE_PEEK,
59 	CFG_WITH_PEEK,
60 	CFG_AFTER_PEEK,
61 };
62 
63 static enum cfg_mode cfg_mode = CFG_MODE_POLL;
64 static enum cfg_peek cfg_peek = CFG_NONE_PEEK;
65 static const char *cfg_host;
66 static const char *cfg_port	= "12000";
67 static int cfg_sock_proto	= IPPROTO_MPTCP;
68 static int pf = AF_INET;
69 static int cfg_sndbuf;
70 static int cfg_rcvbuf;
71 static bool cfg_join;
72 static bool cfg_remove;
73 static unsigned int cfg_time;
74 static unsigned int cfg_do_w;
75 static int cfg_wait;
76 static uint32_t cfg_mark;
77 static char *cfg_input;
78 static int cfg_repeat = 1;
79 static int cfg_truncate;
80 static int cfg_rcv_trunc;
81 
82 struct cfg_cmsg_types {
83 	unsigned int cmsg_enabled:1;
84 	unsigned int timestampns:1;
85 	unsigned int tcp_inq:1;
86 };
87 
88 struct cfg_sockopt_types {
89 	unsigned int transparent:1;
90 	unsigned int mptfo:1;
91 };
92 
93 struct tcp_inq_state {
94 	unsigned int last;
95 	bool expect_eof;
96 };
97 
98 struct wstate {
99 	char buf[8192];
100 	unsigned int len;
101 	unsigned int off;
102 	unsigned int total_len;
103 };
104 
105 static struct tcp_inq_state tcp_inq;
106 
107 static struct cfg_cmsg_types cfg_cmsg_types;
108 static struct cfg_sockopt_types cfg_sockopt_types;
109 
die_usage(void)110 static void die_usage(void)
111 {
112 	fprintf(stderr, "Usage: mptcp_connect [-6] [-c cmsg] [-f offset] [-i file] [-I num] [-j] [-l] "
113 		"[-m mode] [-M mark] [-o option] [-p port] [-P mode] [-r num] [-R num] "
114 		"[-s MPTCP|TCP] [-S num] [-t num] [-T num] [-w sec] connect_address\n");
115 	fprintf(stderr, "\t-6 use ipv6\n");
116 	fprintf(stderr, "\t-c cmsg -- test cmsg type <cmsg>\n");
117 	fprintf(stderr, "\t-f offset -- stop the I/O after receiving and sending the specified amount "
118 		"of bytes. If there are unread bytes in the receive queue, that will cause a MPTCP "
119 		"fastclose at close/shutdown. If offset is negative, expect the peer to close before "
120 		"all the local data as been sent, thus toleration errors on write and EPIPE signals\n");
121 	fprintf(stderr, "\t-i file -- read the data to send from the given file instead of stdin");
122 	fprintf(stderr, "\t-I num -- repeat the transfer 'num' times. In listen mode accepts num "
123 		"incoming connections, in client mode, disconnect and reconnect to the server\n");
124 	fprintf(stderr, "\t-j     -- add additional sleep at connection start and tear down "
125 		"-- for MPJ tests\n");
126 	fprintf(stderr, "\t-l     -- listens mode, accepts incoming connection\n");
127 	fprintf(stderr, "\t-m [poll|mmap|sendfile] -- use poll(default)/mmap+write/sendfile\n");
128 	fprintf(stderr, "\t-M mark -- set socket packet mark\n");
129 	fprintf(stderr, "\t-o option -- test sockopt <option>\n");
130 	fprintf(stderr, "\t-p num -- use port num\n");
131 	fprintf(stderr,
132 		"\t-P [saveWithPeek|saveAfterPeek] -- save data with/after MSG_PEEK form tcp socket\n");
133 	fprintf(stderr, "\t-r num -- enable slow mode, limiting each write to num bytes "
134 		"-- for remove addr tests\n");
135 	fprintf(stderr, "\t-R num -- set SO_RCVBUF to num\n");
136 	fprintf(stderr, "\t-s [MPTCP|TCP] -- use mptcp(default) or tcp sockets\n");
137 	fprintf(stderr, "\t-S num -- set SO_SNDBUF to num\n");
138 	fprintf(stderr, "\t-t num -- set poll timeout to num\n");
139 	fprintf(stderr, "\t-T num -- set expected runtime to num ms\n");
140 	fprintf(stderr, "\t-w num -- wait num sec before closing the socket\n");
141 	exit(1);
142 }
143 
xerror(const char * fmt,...)144 static void __noreturn xerror(const char *fmt, ...)
145 {
146 	va_list ap;
147 
148 	va_start(ap, fmt);
149 	vfprintf(stderr, fmt, ap);
150 	va_end(ap);
151 	exit(1);
152 }
153 
handle_signal(int nr)154 static void handle_signal(int nr)
155 {
156 	quit = true;
157 }
158 
getxinfo_strerr(int err)159 static const char *getxinfo_strerr(int err)
160 {
161 	if (err == EAI_SYSTEM)
162 		return strerror(errno);
163 
164 	return gai_strerror(err);
165 }
166 
xgetnameinfo(const struct sockaddr * addr,socklen_t addrlen,char * host,socklen_t hostlen,char * serv,socklen_t servlen)167 static void xgetnameinfo(const struct sockaddr *addr, socklen_t addrlen,
168 			 char *host, socklen_t hostlen,
169 			 char *serv, socklen_t servlen)
170 {
171 	int flags = NI_NUMERICHOST | NI_NUMERICSERV;
172 	int err = getnameinfo(addr, addrlen, host, hostlen, serv, servlen,
173 			      flags);
174 
175 	if (err) {
176 		const char *errstr = getxinfo_strerr(err);
177 
178 		fprintf(stderr, "Fatal: getnameinfo: %s\n", errstr);
179 		exit(1);
180 	}
181 }
182 
xgetaddrinfo(const char * node,const char * service,struct addrinfo * hints,struct addrinfo ** res)183 static void xgetaddrinfo(const char *node, const char *service,
184 			 struct addrinfo *hints,
185 			 struct addrinfo **res)
186 {
187 	int err;
188 
189 again:
190 	err = getaddrinfo(node, service, hints, res);
191 	if (err) {
192 		const char *errstr;
193 
194 		/* glibc starts to support MPTCP since v2.42.
195 		 * For older versions, use IPPROTO_TCP to resolve,
196 		 * and use TCP/MPTCP to create socket.
197 		 * Link: https://sourceware.org/git/?p=glibc.git;a=commit;h=a8e9022e0f82
198 		 */
199 		if (err == EAI_SOCKTYPE) {
200 			hints->ai_protocol = IPPROTO_TCP;
201 			goto again;
202 		}
203 
204 		errstr = getxinfo_strerr(err);
205 
206 		fprintf(stderr, "Fatal: getaddrinfo(%s:%s): %s\n",
207 			node ? node : "", service ? service : "", errstr);
208 		exit(1);
209 	}
210 }
211 
set_rcvbuf(int fd,unsigned int size)212 static void set_rcvbuf(int fd, unsigned int size)
213 {
214 	int err;
215 
216 	err = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
217 	if (err) {
218 		perror("set SO_RCVBUF");
219 		exit(1);
220 	}
221 }
222 
set_sndbuf(int fd,unsigned int size)223 static void set_sndbuf(int fd, unsigned int size)
224 {
225 	int err;
226 
227 	err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
228 	if (err) {
229 		perror("set SO_SNDBUF");
230 		exit(1);
231 	}
232 }
233 
set_mark(int fd,uint32_t mark)234 static void set_mark(int fd, uint32_t mark)
235 {
236 	int err;
237 
238 	err = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
239 	if (err) {
240 		perror("set SO_MARK");
241 		exit(1);
242 	}
243 }
244 
set_transparent(int fd,int pf)245 static void set_transparent(int fd, int pf)
246 {
247 	int one = 1;
248 
249 	switch (pf) {
250 	case AF_INET:
251 		if (-1 == setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)))
252 			perror("IP_TRANSPARENT");
253 		break;
254 	case AF_INET6:
255 		if (-1 == setsockopt(fd, IPPROTO_IPV6, IPV6_TRANSPARENT, &one, sizeof(one)))
256 			perror("IPV6_TRANSPARENT");
257 		break;
258 	}
259 }
260 
set_mptfo(int fd,int pf)261 static void set_mptfo(int fd, int pf)
262 {
263 	int qlen = 25;
264 
265 	if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)) == -1)
266 		perror("TCP_FASTOPEN");
267 }
268 
do_ulp_so(int sock,const char * name)269 static int do_ulp_so(int sock, const char *name)
270 {
271 	return setsockopt(sock, IPPROTO_TCP, TCP_ULP, name, strlen(name));
272 }
273 
274 #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)275 static void sock_test_tcpulp(int sock, int proto, unsigned int line)
276 {
277 	socklen_t buflen = 8;
278 	char buf[8] = "";
279 	int ret = getsockopt(sock, IPPROTO_TCP, TCP_ULP, buf, &buflen);
280 
281 	if (ret != 0)
282 		X("getsockopt");
283 
284 	if (buflen > 0) {
285 		if (strcmp(buf, "mptcp") != 0)
286 			xerror("unexpected ULP '%s' for proto %d at line %u", buf, proto, line);
287 		ret = do_ulp_so(sock, "tls");
288 		if (ret == 0)
289 			X("setsockopt");
290 	} else if (proto == IPPROTO_MPTCP) {
291 		ret = do_ulp_so(sock, "tls");
292 		if (ret != -1)
293 			X("setsockopt");
294 	}
295 
296 	ret = do_ulp_so(sock, "mptcp");
297 	if (ret != -1)
298 		X("setsockopt");
299 
300 #undef X
301 }
302 
303 #define SOCK_TEST_TCPULP(s, p) sock_test_tcpulp((s), (p), __LINE__)
304 
sock_listen_mptcp(const char * const listenaddr,const char * const port)305 static int sock_listen_mptcp(const char * const listenaddr,
306 			     const char * const port)
307 {
308 	int sock = -1;
309 	struct addrinfo hints = {
310 		.ai_protocol = IPPROTO_MPTCP,
311 		.ai_socktype = SOCK_STREAM,
312 		.ai_flags = AI_PASSIVE | AI_NUMERICHOST
313 	};
314 
315 	hints.ai_family = pf;
316 
317 	struct addrinfo *a, *addr;
318 	int one = 1;
319 
320 	xgetaddrinfo(listenaddr, port, &hints, &addr);
321 	hints.ai_family = pf;
322 
323 	for (a = addr; a; a = a->ai_next) {
324 		sock = socket(a->ai_family, a->ai_socktype, cfg_sock_proto);
325 		if (sock < 0)
326 			continue;
327 
328 		SOCK_TEST_TCPULP(sock, cfg_sock_proto);
329 
330 		if (-1 == setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one,
331 				     sizeof(one)))
332 			perror("setsockopt");
333 
334 		if (cfg_sockopt_types.transparent)
335 			set_transparent(sock, pf);
336 
337 		if (cfg_sockopt_types.mptfo)
338 			set_mptfo(sock, pf);
339 
340 		if (bind(sock, a->ai_addr, a->ai_addrlen) == 0)
341 			break; /* success */
342 
343 		perror("bind");
344 		close(sock);
345 		sock = -1;
346 	}
347 
348 	freeaddrinfo(addr);
349 
350 	if (sock < 0) {
351 		fprintf(stderr, "Could not create listen socket\n");
352 		return sock;
353 	}
354 
355 	SOCK_TEST_TCPULP(sock, cfg_sock_proto);
356 
357 	if (listen(sock, 20)) {
358 		perror("listen");
359 		close(sock);
360 		return -1;
361 	}
362 
363 	SOCK_TEST_TCPULP(sock, cfg_sock_proto);
364 
365 	return sock;
366 }
367 
sock_connect_mptcp(const char * const remoteaddr,const char * const port,int proto,struct addrinfo ** peer,int infd,struct wstate * winfo)368 static int sock_connect_mptcp(const char * const remoteaddr,
369 			      const char * const port, int proto,
370 			      struct addrinfo **peer,
371 			      int infd, struct wstate *winfo)
372 {
373 	struct addrinfo hints = {
374 		.ai_protocol = IPPROTO_MPTCP,
375 		.ai_socktype = SOCK_STREAM,
376 	};
377 	struct addrinfo *a, *addr;
378 	int syn_copied = 0;
379 	int sock = -1;
380 
381 	hints.ai_family = pf;
382 
383 	xgetaddrinfo(remoteaddr, port, &hints, &addr);
384 	for (a = addr; a; a = a->ai_next) {
385 		sock = socket(a->ai_family, a->ai_socktype, proto);
386 		if (sock < 0) {
387 			perror("socket");
388 			continue;
389 		}
390 
391 		SOCK_TEST_TCPULP(sock, proto);
392 
393 		if (cfg_mark)
394 			set_mark(sock, cfg_mark);
395 
396 		if (cfg_sockopt_types.mptfo) {
397 			if (!winfo->total_len)
398 				winfo->total_len = winfo->len = read(infd, winfo->buf,
399 								     sizeof(winfo->buf));
400 
401 			syn_copied = sendto(sock, winfo->buf, winfo->len, MSG_FASTOPEN,
402 					    a->ai_addr, a->ai_addrlen);
403 			if (syn_copied >= 0) {
404 				winfo->off = syn_copied;
405 				winfo->len -= syn_copied;
406 				*peer = a;
407 				break; /* success */
408 			}
409 		} else {
410 			if (connect(sock, a->ai_addr, a->ai_addrlen) == 0) {
411 				*peer = a;
412 				break; /* success */
413 			}
414 		}
415 		if (cfg_sockopt_types.mptfo) {
416 			perror("sendto()");
417 			close(sock);
418 			sock = -1;
419 		} else {
420 			perror("connect()");
421 			close(sock);
422 			sock = -1;
423 		}
424 	}
425 
426 	freeaddrinfo(addr);
427 	if (sock != -1)
428 		SOCK_TEST_TCPULP(sock, proto);
429 	return sock;
430 }
431 
do_rnd_write(const int fd,char * buf,const size_t len)432 static size_t do_rnd_write(const int fd, char *buf, const size_t len)
433 {
434 	static bool first = true;
435 	unsigned int do_w;
436 	ssize_t bw;
437 
438 	do_w = rand() & 0xffff;
439 	if (do_w == 0 || do_w > len)
440 		do_w = len;
441 
442 	if (cfg_join && first && do_w > 100)
443 		do_w = 100;
444 
445 	if (cfg_remove && do_w > cfg_do_w)
446 		do_w = cfg_do_w;
447 
448 	bw = write(fd, buf, do_w);
449 	if (bw < 0)
450 		return bw;
451 
452 	/* let the join handshake complete, before going on */
453 	if (cfg_join && first) {
454 		usleep(200000);
455 		first = false;
456 	}
457 
458 	if (cfg_remove)
459 		usleep(200000);
460 
461 	return bw;
462 }
463 
do_write(const int fd,char * buf,const size_t len)464 static size_t do_write(const int fd, char *buf, const size_t len)
465 {
466 	size_t offset = 0;
467 
468 	while (offset < len) {
469 		size_t written;
470 		ssize_t bw;
471 
472 		bw = write(fd, buf + offset, len - offset);
473 		if (bw < 0) {
474 			perror("write");
475 			return 0;
476 		}
477 
478 		written = (size_t)bw;
479 		offset += written;
480 	}
481 
482 	return offset;
483 }
484 
process_cmsg(struct msghdr * msgh)485 static void process_cmsg(struct msghdr *msgh)
486 {
487 	struct __kernel_timespec ts;
488 	bool inq_found = false;
489 	bool ts_found = false;
490 	unsigned int inq = 0;
491 	struct cmsghdr *cmsg;
492 
493 	for (cmsg = CMSG_FIRSTHDR(msgh); cmsg ; cmsg = CMSG_NXTHDR(msgh, cmsg)) {
494 		if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMPNS_NEW) {
495 			memcpy(&ts, CMSG_DATA(cmsg), sizeof(ts));
496 			ts_found = true;
497 			continue;
498 		}
499 		if (cmsg->cmsg_level == IPPROTO_TCP && cmsg->cmsg_type == TCP_CM_INQ) {
500 			memcpy(&inq, CMSG_DATA(cmsg), sizeof(inq));
501 			inq_found = true;
502 			continue;
503 		}
504 
505 	}
506 
507 	if (cfg_cmsg_types.timestampns) {
508 		if (!ts_found)
509 			xerror("TIMESTAMPNS not present\n");
510 	}
511 
512 	if (cfg_cmsg_types.tcp_inq) {
513 		if (!inq_found)
514 			xerror("TCP_INQ not present\n");
515 
516 		if (inq > 1024)
517 			xerror("tcp_inq %u is larger than one kbyte\n", inq);
518 		tcp_inq.last = inq;
519 	}
520 }
521 
do_recvmsg_cmsg(const int fd,char * buf,const size_t len)522 static ssize_t do_recvmsg_cmsg(const int fd, char *buf, const size_t len)
523 {
524 	char msg_buf[8192];
525 	struct iovec iov = {
526 		.iov_base = buf,
527 		.iov_len = len,
528 	};
529 	struct msghdr msg = {
530 		.msg_iov = &iov,
531 		.msg_iovlen = 1,
532 		.msg_control = msg_buf,
533 		.msg_controllen = sizeof(msg_buf),
534 	};
535 	int flags = 0;
536 	unsigned int last_hint = tcp_inq.last;
537 	int ret = recvmsg(fd, &msg, flags);
538 
539 	if (ret <= 0) {
540 		if (ret == 0 && tcp_inq.expect_eof)
541 			return ret;
542 
543 		if (ret == 0 && cfg_cmsg_types.tcp_inq)
544 			if (last_hint != 1 && last_hint != 0)
545 				xerror("EOF but last tcp_inq hint was %u\n", last_hint);
546 
547 		return ret;
548 	}
549 
550 	if (tcp_inq.expect_eof)
551 		xerror("expected EOF, last_hint %u, now %u\n",
552 		       last_hint, tcp_inq.last);
553 
554 	if (msg.msg_controllen && !cfg_cmsg_types.cmsg_enabled)
555 		xerror("got %lu bytes of cmsg data, expected 0\n",
556 		       (unsigned long)msg.msg_controllen);
557 
558 	if (msg.msg_controllen == 0 && cfg_cmsg_types.cmsg_enabled)
559 		xerror("%s\n", "got no cmsg data");
560 
561 	if (msg.msg_controllen)
562 		process_cmsg(&msg);
563 
564 	if (cfg_cmsg_types.tcp_inq) {
565 		if ((size_t)ret < len && last_hint > (unsigned int)ret) {
566 			if (ret + 1 != (int)last_hint) {
567 				int next = read(fd, msg_buf, sizeof(msg_buf));
568 
569 				xerror("read %u of %u, last_hint was %u tcp_inq hint now %u next_read returned %d/%m\n",
570 				       ret, (unsigned int)len, last_hint, tcp_inq.last, next);
571 			} else {
572 				tcp_inq.expect_eof = true;
573 			}
574 		}
575 	}
576 
577 	return ret;
578 }
579 
do_rnd_read(const int fd,char * buf,const size_t len)580 static ssize_t do_rnd_read(const int fd, char *buf, const size_t len)
581 {
582 	int ret = 0;
583 	char tmp[16384];
584 	size_t cap = rand();
585 
586 	cap &= 0xffff;
587 
588 	if (cap == 0)
589 		cap = 1;
590 	else if (cap > len)
591 		cap = len;
592 
593 	if (cfg_peek == CFG_WITH_PEEK) {
594 		ret = recv(fd, buf, cap, MSG_PEEK);
595 		ret = (ret < 0) ? ret : read(fd, tmp, ret);
596 	} else if (cfg_peek == CFG_AFTER_PEEK) {
597 		ret = recv(fd, buf, cap, MSG_PEEK);
598 		ret = (ret < 0) ? ret : read(fd, buf, cap);
599 	} else if (cfg_cmsg_types.cmsg_enabled) {
600 		ret = do_recvmsg_cmsg(fd, buf, cap);
601 	} else {
602 		ret = read(fd, buf, cap);
603 	}
604 
605 	return ret;
606 }
607 
set_nonblock(int fd,bool nonblock)608 static void set_nonblock(int fd, bool nonblock)
609 {
610 	int flags = fcntl(fd, F_GETFL);
611 
612 	if (flags == -1)
613 		return;
614 
615 	if (nonblock)
616 		fcntl(fd, F_SETFL, flags | O_NONBLOCK);
617 	else
618 		fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
619 }
620 
shut_wr(int fd)621 static void shut_wr(int fd)
622 {
623 	/* Close our write side, ev. give some time
624 	 * for address notification and/or checking
625 	 * the current status
626 	 */
627 	if (cfg_wait)
628 		usleep(cfg_wait);
629 
630 	shutdown(fd, SHUT_WR);
631 }
632 
copyfd_io_poll(int infd,int peerfd,int outfd,bool * in_closed_after_out,struct wstate * winfo)633 static int copyfd_io_poll(int infd, int peerfd, int outfd,
634 			  bool *in_closed_after_out, struct wstate *winfo)
635 {
636 	struct pollfd fds = {
637 		.fd = peerfd,
638 		.events = POLLIN | POLLOUT,
639 	};
640 	unsigned int total_wlen = 0, total_rlen = 0;
641 
642 	set_nonblock(peerfd, true);
643 
644 	for (;;) {
645 		char rbuf[8192];
646 		ssize_t len;
647 
648 		if (fds.events == 0 || quit)
649 			break;
650 
651 		switch (poll(&fds, 1, poll_timeout)) {
652 		case -1:
653 			if (errno == EINTR)
654 				continue;
655 			perror("poll");
656 			return 1;
657 		case 0:
658 			fprintf(stderr, "%s: poll timed out (events: "
659 				"POLLIN %u, POLLOUT %u)\n", __func__,
660 				fds.events & POLLIN, fds.events & POLLOUT);
661 			return 2;
662 		}
663 
664 		if (fds.revents & POLLIN) {
665 			ssize_t rb = sizeof(rbuf);
666 
667 			/* limit the total amount of read data to the trunc value*/
668 			if (cfg_truncate > 0) {
669 				if (rb + total_rlen > cfg_truncate)
670 					rb = cfg_truncate - total_rlen;
671 				len = read(peerfd, rbuf, rb);
672 			} else {
673 				len = do_rnd_read(peerfd, rbuf, sizeof(rbuf));
674 			}
675 			if (len == 0) {
676 				/* no more data to receive:
677 				 * peer has closed its write side
678 				 */
679 				fds.events &= ~POLLIN;
680 
681 				if ((fds.events & POLLOUT) == 0) {
682 					*in_closed_after_out = true;
683 					/* and nothing more to send */
684 					break;
685 				}
686 
687 			/* Else, still have data to transmit */
688 			} else if (len < 0) {
689 				if (cfg_rcv_trunc)
690 					return 0;
691 				perror("read");
692 				return 3;
693 			}
694 
695 			total_rlen += len;
696 			do_write(outfd, rbuf, len);
697 		}
698 
699 		if (fds.revents & POLLOUT) {
700 			if (winfo->len == 0) {
701 				winfo->off = 0;
702 				winfo->len = read(infd, winfo->buf, sizeof(winfo->buf));
703 			}
704 
705 			if (winfo->len > 0) {
706 				ssize_t bw;
707 
708 				/* limit the total amount of written data to the trunc value */
709 				if (cfg_truncate > 0 && winfo->len + total_wlen > cfg_truncate)
710 					winfo->len = cfg_truncate - total_wlen;
711 
712 				bw = do_rnd_write(peerfd, winfo->buf + winfo->off, winfo->len);
713 				if (bw < 0) {
714 					/* expected reset, continue to read */
715 					if (cfg_rcv_trunc &&
716 					    (errno == ECONNRESET ||
717 					     errno == EPIPE)) {
718 						fds.events &= ~POLLOUT;
719 						continue;
720 					}
721 
722 					perror("write");
723 					return 111;
724 				}
725 
726 				winfo->off += bw;
727 				winfo->len -= bw;
728 				total_wlen += bw;
729 			} else if (winfo->len == 0) {
730 				/* We have no more data to send. */
731 				fds.events &= ~POLLOUT;
732 
733 				if ((fds.events & POLLIN) == 0)
734 					/* ... and peer also closed already */
735 					break;
736 
737 				shut_wr(peerfd);
738 			} else {
739 				if (errno == EINTR)
740 					continue;
741 				perror("read");
742 				return 4;
743 			}
744 		}
745 
746 		if (fds.revents & (POLLERR | POLLNVAL)) {
747 			if (cfg_rcv_trunc) {
748 				fds.events &= ~(POLLERR | POLLNVAL);
749 				continue;
750 			}
751 			fprintf(stderr, "Unexpected revents: "
752 				"POLLERR/POLLNVAL(%x)\n", fds.revents);
753 			return 5;
754 		}
755 
756 		if (cfg_truncate > 0 && total_wlen >= cfg_truncate &&
757 		    total_rlen >= cfg_truncate)
758 			break;
759 	}
760 
761 	/* leave some time for late join/announce */
762 	if (cfg_remove && !quit)
763 		usleep(cfg_wait);
764 
765 	return 0;
766 }
767 
do_recvfile(int infd,int outfd)768 static int do_recvfile(int infd, int outfd)
769 {
770 	ssize_t r;
771 
772 	do {
773 		char buf[16384];
774 
775 		r = do_rnd_read(infd, buf, sizeof(buf));
776 		if (r > 0) {
777 			if (write(outfd, buf, r) != r)
778 				break;
779 		} else if (r < 0) {
780 			perror("read");
781 		}
782 	} while (r > 0);
783 
784 	return (int)r;
785 }
786 
spool_buf(int fd,struct wstate * winfo)787 static int spool_buf(int fd, struct wstate *winfo)
788 {
789 	while (winfo->len) {
790 		int ret = write(fd, winfo->buf + winfo->off, winfo->len);
791 
792 		if (ret < 0) {
793 			perror("write");
794 			return 4;
795 		}
796 		winfo->off += ret;
797 		winfo->len -= ret;
798 	}
799 	return 0;
800 }
801 
do_mmap(int infd,int outfd,unsigned int size,struct wstate * winfo)802 static int do_mmap(int infd, int outfd, unsigned int size,
803 		   struct wstate *winfo)
804 {
805 	char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0);
806 	ssize_t ret = 0, off = winfo->total_len;
807 	size_t rem;
808 
809 	if (inbuf == MAP_FAILED) {
810 		perror("mmap");
811 		return 1;
812 	}
813 
814 	ret = spool_buf(outfd, winfo);
815 	if (ret < 0)
816 		return ret;
817 
818 	rem = size - winfo->total_len;
819 
820 	while (rem > 0) {
821 		ret = write(outfd, inbuf + off, rem);
822 
823 		if (ret < 0) {
824 			perror("write");
825 			break;
826 		}
827 
828 		off += ret;
829 		rem -= ret;
830 	}
831 
832 	munmap(inbuf, size);
833 	return rem;
834 }
835 
get_infd_size(int fd)836 static int get_infd_size(int fd)
837 {
838 	struct stat sb;
839 	ssize_t count;
840 	int err;
841 
842 	err = fstat(fd, &sb);
843 	if (err < 0) {
844 		perror("fstat");
845 		return -1;
846 	}
847 
848 	if ((sb.st_mode & S_IFMT) != S_IFREG) {
849 		fprintf(stderr, "%s: stdin is not a regular file\n", __func__);
850 		return -2;
851 	}
852 
853 	count = sb.st_size;
854 	if (count > INT_MAX) {
855 		fprintf(stderr, "File too large: %zu\n", count);
856 		return -3;
857 	}
858 
859 	return (int)count;
860 }
861 
do_sendfile(int infd,int outfd,unsigned int count,struct wstate * winfo)862 static int do_sendfile(int infd, int outfd, unsigned int count,
863 		       struct wstate *winfo)
864 {
865 	int ret = spool_buf(outfd, winfo);
866 
867 	if (ret < 0)
868 		return ret;
869 
870 	count -= winfo->total_len;
871 
872 	while (count > 0) {
873 		ssize_t r;
874 
875 		r = sendfile(outfd, infd, NULL, count);
876 		if (r < 0) {
877 			perror("sendfile");
878 			return 3;
879 		}
880 
881 		count -= r;
882 	}
883 
884 	return 0;
885 }
886 
copyfd_io_mmap(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)887 static int copyfd_io_mmap(int infd, int peerfd, int outfd,
888 			  unsigned int size, bool *in_closed_after_out,
889 			  struct wstate *winfo)
890 {
891 	int err;
892 
893 	if (listen_mode) {
894 		err = do_recvfile(peerfd, outfd);
895 		if (err)
896 			return err;
897 
898 		err = do_mmap(infd, peerfd, size, winfo);
899 	} else {
900 		err = do_mmap(infd, peerfd, size, winfo);
901 		if (err)
902 			return err;
903 
904 		shut_wr(peerfd);
905 
906 		err = do_recvfile(peerfd, outfd);
907 		*in_closed_after_out = true;
908 	}
909 
910 	return err;
911 }
912 
copyfd_io_sendfile(int infd,int peerfd,int outfd,unsigned int size,bool * in_closed_after_out,struct wstate * winfo)913 static int copyfd_io_sendfile(int infd, int peerfd, int outfd,
914 			      unsigned int size, bool *in_closed_after_out, struct wstate *winfo)
915 {
916 	int err;
917 
918 	if (listen_mode) {
919 		err = do_recvfile(peerfd, outfd);
920 		if (err)
921 			return err;
922 
923 		err = do_sendfile(infd, peerfd, size, winfo);
924 	} else {
925 		err = do_sendfile(infd, peerfd, size, winfo);
926 		if (err)
927 			return err;
928 
929 		shut_wr(peerfd);
930 
931 		err = do_recvfile(peerfd, outfd);
932 		*in_closed_after_out = true;
933 	}
934 
935 	return err;
936 }
937 
copyfd_io(int infd,int peerfd,int outfd,bool close_peerfd,struct wstate * winfo)938 static int copyfd_io(int infd, int peerfd, int outfd, bool close_peerfd, struct wstate *winfo)
939 {
940 	bool in_closed_after_out = false;
941 	struct timespec start, end;
942 	int file_size;
943 	int ret;
944 
945 	if (cfg_time && (clock_gettime(CLOCK_MONOTONIC, &start) < 0))
946 		xerror("can not fetch start time %d", errno);
947 
948 	switch (cfg_mode) {
949 	case CFG_MODE_POLL:
950 		ret = copyfd_io_poll(infd, peerfd, outfd, &in_closed_after_out,
951 				     winfo);
952 		break;
953 
954 	case CFG_MODE_MMAP:
955 		file_size = get_infd_size(infd);
956 		if (file_size < 0)
957 			return file_size;
958 		ret = copyfd_io_mmap(infd, peerfd, outfd, file_size,
959 				     &in_closed_after_out, winfo);
960 		break;
961 
962 	case CFG_MODE_SENDFILE:
963 		file_size = get_infd_size(infd);
964 		if (file_size < 0)
965 			return file_size;
966 		ret = copyfd_io_sendfile(infd, peerfd, outfd, file_size,
967 					 &in_closed_after_out, winfo);
968 		break;
969 
970 	default:
971 		fprintf(stderr, "Invalid mode %d\n", cfg_mode);
972 
973 		die_usage();
974 		return 1;
975 	}
976 
977 	if (ret)
978 		return ret;
979 
980 	if (close_peerfd)
981 		close(peerfd);
982 
983 	if (cfg_time) {
984 		unsigned int delta_ms;
985 
986 		if (clock_gettime(CLOCK_MONOTONIC, &end) < 0)
987 			xerror("can not fetch end time %d", errno);
988 		delta_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000;
989 		if (delta_ms > cfg_time) {
990 			xerror("transfer slower than expected! runtime %d ms, expected %d ms",
991 			       delta_ms, cfg_time);
992 		}
993 
994 		/* show the runtime only if this end shutdown(wr) before receiving the EOF,
995 		 * (that is, if this end got the longer runtime)
996 		 */
997 		if (in_closed_after_out)
998 			fprintf(stderr, "%d", delta_ms);
999 	}
1000 
1001 	return 0;
1002 }
1003 
check_sockaddr(int pf,struct sockaddr_storage * ss,socklen_t salen)1004 static void check_sockaddr(int pf, struct sockaddr_storage *ss,
1005 			   socklen_t salen)
1006 {
1007 	struct sockaddr_in6 *sin6;
1008 	struct sockaddr_in *sin;
1009 	socklen_t wanted_size = 0;
1010 
1011 	switch (pf) {
1012 	case AF_INET:
1013 		wanted_size = sizeof(*sin);
1014 		sin = (void *)ss;
1015 		if (!sin->sin_port)
1016 			fprintf(stderr, "accept: something wrong: ip connection from port 0");
1017 		break;
1018 	case AF_INET6:
1019 		wanted_size = sizeof(*sin6);
1020 		sin6 = (void *)ss;
1021 		if (!sin6->sin6_port)
1022 			fprintf(stderr, "accept: something wrong: ipv6 connection from port 0");
1023 		break;
1024 	default:
1025 		fprintf(stderr, "accept: Unknown pf %d, salen %u\n", pf, salen);
1026 		return;
1027 	}
1028 
1029 	if (salen != wanted_size)
1030 		fprintf(stderr, "accept: size mismatch, got %d expected %d\n",
1031 			(int)salen, wanted_size);
1032 
1033 	if (ss->ss_family != pf)
1034 		fprintf(stderr, "accept: pf mismatch, expect %d, ss_family is %d\n",
1035 			(int)ss->ss_family, pf);
1036 }
1037 
check_getpeername(int fd,struct sockaddr_storage * ss,socklen_t salen)1038 static void check_getpeername(int fd, struct sockaddr_storage *ss, socklen_t salen)
1039 {
1040 	struct sockaddr_storage peerss;
1041 	socklen_t peersalen = sizeof(peerss);
1042 
1043 	if (getpeername(fd, (struct sockaddr *)&peerss, &peersalen) < 0) {
1044 		perror("getpeername");
1045 		return;
1046 	}
1047 
1048 	if (peersalen != salen) {
1049 		fprintf(stderr, "%s: %d vs %d\n", __func__, peersalen, salen);
1050 		return;
1051 	}
1052 
1053 	if (memcmp(ss, &peerss, peersalen)) {
1054 		char a[INET6_ADDRSTRLEN];
1055 		char b[INET6_ADDRSTRLEN];
1056 		char c[INET6_ADDRSTRLEN];
1057 		char d[INET6_ADDRSTRLEN];
1058 
1059 		xgetnameinfo((struct sockaddr *)ss, salen,
1060 			     a, sizeof(a), b, sizeof(b));
1061 
1062 		xgetnameinfo((struct sockaddr *)&peerss, peersalen,
1063 			     c, sizeof(c), d, sizeof(d));
1064 
1065 		fprintf(stderr, "%s: memcmp failure: accept %s vs peername %s, %s vs %s salen %d vs %d\n",
1066 			__func__, a, c, b, d, peersalen, salen);
1067 	}
1068 }
1069 
check_getpeername_connect(int fd)1070 static void check_getpeername_connect(int fd)
1071 {
1072 	struct sockaddr_storage ss;
1073 	socklen_t salen = sizeof(ss);
1074 	char a[INET6_ADDRSTRLEN];
1075 	char b[INET6_ADDRSTRLEN];
1076 	const char *iface;
1077 	size_t len;
1078 
1079 	if (getpeername(fd, (struct sockaddr *)&ss, &salen) < 0) {
1080 		perror("getpeername");
1081 		return;
1082 	}
1083 
1084 	xgetnameinfo((struct sockaddr *)&ss, salen,
1085 		     a, sizeof(a), b, sizeof(b));
1086 
1087 	iface = strchr(cfg_host, '%');
1088 	if (iface)
1089 		len = iface - cfg_host;
1090 	else
1091 		len = strlen(cfg_host) + 1;
1092 
1093 	if (strncmp(cfg_host, a, len) || strcmp(cfg_port, b))
1094 		fprintf(stderr, "%s: %s vs %s, %s vs %s\n", __func__,
1095 			cfg_host, a, cfg_port, b);
1096 }
1097 
maybe_close(int fd)1098 static void maybe_close(int fd)
1099 {
1100 	unsigned int r = rand();
1101 
1102 	if (!(cfg_join || cfg_remove || cfg_repeat > 1) && (r & 1))
1103 		close(fd);
1104 }
1105 
main_loop_s(int listensock)1106 int main_loop_s(int listensock)
1107 {
1108 	struct sockaddr_storage ss;
1109 	struct wstate winfo;
1110 	struct pollfd polls;
1111 	socklen_t salen;
1112 	int remotesock;
1113 	int err = 0;
1114 	int fd = 0;
1115 
1116 again:
1117 	polls.fd = listensock;
1118 	polls.events = POLLIN;
1119 
1120 	switch (poll(&polls, 1, poll_timeout)) {
1121 	case -1:
1122 		perror("poll");
1123 		return 1;
1124 	case 0:
1125 		fprintf(stderr, "%s: timed out\n", __func__);
1126 		close(listensock);
1127 		return 2;
1128 	}
1129 
1130 	salen = sizeof(ss);
1131 	remotesock = accept(listensock, (struct sockaddr *)&ss, &salen);
1132 	if (remotesock >= 0) {
1133 		maybe_close(listensock);
1134 		check_sockaddr(pf, &ss, salen);
1135 		check_getpeername(remotesock, &ss, salen);
1136 
1137 		if (cfg_input) {
1138 			fd = open(cfg_input, O_RDONLY);
1139 			if (fd < 0)
1140 				xerror("can't open %s: %d", cfg_input, errno);
1141 		}
1142 
1143 		SOCK_TEST_TCPULP(remotesock, 0);
1144 
1145 		memset(&winfo, 0, sizeof(winfo));
1146 		err = copyfd_io(fd, remotesock, 1, true, &winfo);
1147 	} else {
1148 		perror("accept");
1149 		return 1;
1150 	}
1151 
1152 	if (cfg_input)
1153 		close(fd);
1154 
1155 	if (!err && --cfg_repeat > 0)
1156 		goto again;
1157 
1158 	return err;
1159 }
1160 
init_rng(void)1161 static void init_rng(void)
1162 {
1163 	unsigned int foo;
1164 
1165 	if (getrandom(&foo, sizeof(foo), 0) == -1) {
1166 		perror("getrandom");
1167 		exit(1);
1168 	}
1169 
1170 	srand(foo);
1171 }
1172 
xsetsockopt(int fd,int level,int optname,const void * optval,socklen_t optlen)1173 static void xsetsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
1174 {
1175 	int err;
1176 
1177 	err = setsockopt(fd, level, optname, optval, optlen);
1178 	if (err) {
1179 		perror("setsockopt");
1180 		exit(1);
1181 	}
1182 }
1183 
apply_cmsg_types(int fd,const struct cfg_cmsg_types * cmsg)1184 static void apply_cmsg_types(int fd, const struct cfg_cmsg_types *cmsg)
1185 {
1186 	static const unsigned int on = 1;
1187 
1188 	if (cmsg->timestampns)
1189 		xsetsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS_NEW, &on, sizeof(on));
1190 	if (cmsg->tcp_inq)
1191 		xsetsockopt(fd, IPPROTO_TCP, TCP_INQ, &on, sizeof(on));
1192 }
1193 
parse_cmsg_types(const char * type)1194 static void parse_cmsg_types(const char *type)
1195 {
1196 	char *next = strchr(type, ',');
1197 	unsigned int len = 0;
1198 
1199 	cfg_cmsg_types.cmsg_enabled = 1;
1200 
1201 	if (next) {
1202 		parse_cmsg_types(next + 1);
1203 		len = next - type;
1204 	} else {
1205 		len = strlen(type);
1206 	}
1207 
1208 	if (strncmp(type, "TIMESTAMPNS", len) == 0) {
1209 		cfg_cmsg_types.timestampns = 1;
1210 		return;
1211 	}
1212 
1213 	if (strncmp(type, "TCPINQ", len) == 0) {
1214 		cfg_cmsg_types.tcp_inq = 1;
1215 		return;
1216 	}
1217 
1218 	fprintf(stderr, "Unrecognized cmsg option %s\n", type);
1219 	exit(1);
1220 }
1221 
parse_setsock_options(const char * name)1222 static void parse_setsock_options(const char *name)
1223 {
1224 	char *next = strchr(name, ',');
1225 	unsigned int len = 0;
1226 
1227 	if (next) {
1228 		parse_setsock_options(next + 1);
1229 		len = next - name;
1230 	} else {
1231 		len = strlen(name);
1232 	}
1233 
1234 	if (strncmp(name, "TRANSPARENT", len) == 0) {
1235 		cfg_sockopt_types.transparent = 1;
1236 		return;
1237 	}
1238 
1239 	if (strncmp(name, "MPTFO", len) == 0) {
1240 		cfg_sockopt_types.mptfo = 1;
1241 		return;
1242 	}
1243 
1244 	fprintf(stderr, "Unrecognized setsockopt option %s\n", name);
1245 	exit(1);
1246 }
1247 
xdisconnect(int fd)1248 void xdisconnect(int fd)
1249 {
1250 	socklen_t addrlen = sizeof(struct sockaddr_storage);
1251 	struct sockaddr_storage addr, empty;
1252 	int msec_sleep = 10;
1253 	void *raw_addr;
1254 	int i, cmdlen;
1255 	char cmd[128];
1256 
1257 	/* get the local address and convert it to string */
1258 	if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) < 0)
1259 		xerror("getsockname");
1260 
1261 	if (addr.ss_family == AF_INET)
1262 		raw_addr = &(((struct sockaddr_in *)&addr)->sin_addr);
1263 	else if (addr.ss_family == AF_INET6)
1264 		raw_addr = &(((struct sockaddr_in6 *)&addr)->sin6_addr);
1265 	else
1266 		xerror("bad family");
1267 
1268 	strcpy(cmd, "ss -Mnt | grep -q ");
1269 	cmdlen = strlen(cmd);
1270 	if (!inet_ntop(addr.ss_family, raw_addr, &cmd[cmdlen],
1271 		       sizeof(cmd) - cmdlen))
1272 		xerror("inet_ntop");
1273 
1274 	shutdown(fd, SHUT_WR);
1275 
1276 	/*
1277 	 * wait until the pending data is completely flushed and all
1278 	 * the sockets reached the closed status.
1279 	 * disconnect will bypass/ignore/drop any pending data.
1280 	 */
1281 	for (i = 0; ; i += msec_sleep) {
1282 		/* closed socket are not listed by 'ss' */
1283 		if (system(cmd) != 0)
1284 			break;
1285 
1286 		if (i > poll_timeout)
1287 			xerror("timeout while waiting for spool to complete");
1288 		usleep(msec_sleep * 1000);
1289 	}
1290 
1291 	memset(&empty, 0, sizeof(empty));
1292 	empty.ss_family = AF_UNSPEC;
1293 	if (connect(fd, (struct sockaddr *)&empty, addrlen) < 0)
1294 		xerror("can't disconnect: %d", errno);
1295 }
1296 
main_loop(void)1297 int main_loop(void)
1298 {
1299 	int fd = 0, ret, fd_in = 0;
1300 	struct addrinfo *peer;
1301 	struct wstate winfo;
1302 
1303 	if (cfg_input && cfg_sockopt_types.mptfo) {
1304 		fd_in = open(cfg_input, O_RDONLY);
1305 		if (fd_in < 0)
1306 			xerror("can't open %s:%d", cfg_input, errno);
1307 	}
1308 
1309 	memset(&winfo, 0, sizeof(winfo));
1310 	fd = sock_connect_mptcp(cfg_host, cfg_port, cfg_sock_proto, &peer, fd_in, &winfo);
1311 	if (fd < 0)
1312 		return 2;
1313 
1314 again:
1315 	check_getpeername_connect(fd);
1316 
1317 	SOCK_TEST_TCPULP(fd, cfg_sock_proto);
1318 
1319 	if (cfg_rcvbuf)
1320 		set_rcvbuf(fd, cfg_rcvbuf);
1321 	if (cfg_sndbuf)
1322 		set_sndbuf(fd, cfg_sndbuf);
1323 	if (cfg_cmsg_types.cmsg_enabled)
1324 		apply_cmsg_types(fd, &cfg_cmsg_types);
1325 
1326 	if (cfg_input && !cfg_sockopt_types.mptfo) {
1327 		fd_in = open(cfg_input, O_RDONLY);
1328 		if (fd_in < 0)
1329 			xerror("can't open %s:%d", cfg_input, errno);
1330 	}
1331 
1332 	ret = copyfd_io(fd_in, fd, 1, 0, &winfo);
1333 	if (ret)
1334 		goto out;
1335 
1336 	if (cfg_truncate > 0) {
1337 		shutdown(fd, SHUT_WR);
1338 	} else if (--cfg_repeat > 0) {
1339 		xdisconnect(fd);
1340 
1341 		/* the socket could be unblocking at this point, we need the
1342 		 * connect to be blocking
1343 		 */
1344 		set_nonblock(fd, false);
1345 		if (connect(fd, peer->ai_addr, peer->ai_addrlen))
1346 			xerror("can't reconnect: %d", errno);
1347 		if (cfg_input)
1348 			close(fd_in);
1349 		memset(&winfo, 0, sizeof(winfo));
1350 		goto again;
1351 	} else {
1352 		close(fd);
1353 	}
1354 
1355 out:
1356 	if (cfg_input)
1357 		close(fd_in);
1358 	return ret;
1359 }
1360 
parse_proto(const char * proto)1361 int parse_proto(const char *proto)
1362 {
1363 	if (!strcasecmp(proto, "MPTCP"))
1364 		return IPPROTO_MPTCP;
1365 	if (!strcasecmp(proto, "TCP"))
1366 		return IPPROTO_TCP;
1367 
1368 	fprintf(stderr, "Unknown protocol: %s\n.", proto);
1369 	die_usage();
1370 
1371 	/* silence compiler warning */
1372 	return 0;
1373 }
1374 
parse_mode(const char * mode)1375 int parse_mode(const char *mode)
1376 {
1377 	if (!strcasecmp(mode, "poll"))
1378 		return CFG_MODE_POLL;
1379 	if (!strcasecmp(mode, "mmap"))
1380 		return CFG_MODE_MMAP;
1381 	if (!strcasecmp(mode, "sendfile"))
1382 		return CFG_MODE_SENDFILE;
1383 
1384 	fprintf(stderr, "Unknown test mode: %s\n", mode);
1385 	fprintf(stderr, "Supported modes are:\n");
1386 	fprintf(stderr, "\t\t\"poll\" - interleaved read/write using poll()\n");
1387 	fprintf(stderr, "\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n");
1388 	fprintf(stderr, "\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n");
1389 
1390 	die_usage();
1391 
1392 	/* silence compiler warning */
1393 	return 0;
1394 }
1395 
parse_peek(const char * mode)1396 int parse_peek(const char *mode)
1397 {
1398 	if (!strcasecmp(mode, "saveWithPeek"))
1399 		return CFG_WITH_PEEK;
1400 	if (!strcasecmp(mode, "saveAfterPeek"))
1401 		return CFG_AFTER_PEEK;
1402 
1403 	fprintf(stderr, "Unknown: %s\n", mode);
1404 	fprintf(stderr, "Supported MSG_PEEK mode are:\n");
1405 	fprintf(stderr,
1406 		"\t\t\"saveWithPeek\" - recv data with flags 'MSG_PEEK' and save the peek data into file\n");
1407 	fprintf(stderr,
1408 		"\t\t\"saveAfterPeek\" - read and save data into file after recv with flags 'MSG_PEEK'\n");
1409 
1410 	die_usage();
1411 
1412 	/* silence compiler warning */
1413 	return 0;
1414 }
1415 
parse_int(const char * size)1416 static int parse_int(const char *size)
1417 {
1418 	unsigned long s;
1419 
1420 	errno = 0;
1421 
1422 	s = strtoul(size, NULL, 0);
1423 
1424 	if (errno) {
1425 		fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1426 			size, strerror(errno));
1427 		die_usage();
1428 	}
1429 
1430 	if (s > INT_MAX) {
1431 		fprintf(stderr, "Invalid sndbuf size %s (%s)\n",
1432 			size, strerror(ERANGE));
1433 		die_usage();
1434 	}
1435 
1436 	return (int)s;
1437 }
1438 
parse_opts(int argc,char ** argv)1439 static void parse_opts(int argc, char **argv)
1440 {
1441 	int c;
1442 
1443 	while ((c = getopt(argc, argv, "6c:f:hi:I:jlm:M:o:p:P:r:R:s:S:t:T:w:")) != -1) {
1444 		switch (c) {
1445 		case 'f':
1446 			cfg_truncate = atoi(optarg);
1447 
1448 			/* when receiving a fastclose, ignore PIPE signals and
1449 			 * all the I/O errors later in the code
1450 			 */
1451 			if (cfg_truncate < 0) {
1452 				cfg_rcv_trunc = true;
1453 				signal(SIGPIPE, SIG_IGN);
1454 			}
1455 			break;
1456 		case 'j':
1457 			cfg_join = true;
1458 			cfg_mode = CFG_MODE_POLL;
1459 			break;
1460 		case 'r':
1461 			cfg_remove = true;
1462 			cfg_mode = CFG_MODE_POLL;
1463 			cfg_wait = 400000;
1464 			cfg_do_w = atoi(optarg);
1465 			if (cfg_do_w <= 0)
1466 				cfg_do_w = 50;
1467 			break;
1468 		case 'i':
1469 			cfg_input = optarg;
1470 			break;
1471 		case 'I':
1472 			cfg_repeat = atoi(optarg);
1473 			break;
1474 		case 'l':
1475 			listen_mode = true;
1476 			break;
1477 		case 'p':
1478 			cfg_port = optarg;
1479 			break;
1480 		case 's':
1481 			cfg_sock_proto = parse_proto(optarg);
1482 			break;
1483 		case 'h':
1484 			die_usage();
1485 			break;
1486 		case '6':
1487 			pf = AF_INET6;
1488 			break;
1489 		case 't':
1490 			poll_timeout = atoi(optarg) * 1000;
1491 			if (poll_timeout <= 0)
1492 				poll_timeout = -1;
1493 			break;
1494 		case 'T':
1495 			cfg_time = atoi(optarg);
1496 			break;
1497 		case 'm':
1498 			cfg_mode = parse_mode(optarg);
1499 			break;
1500 		case 'S':
1501 			cfg_sndbuf = parse_int(optarg);
1502 			break;
1503 		case 'R':
1504 			cfg_rcvbuf = parse_int(optarg);
1505 			break;
1506 		case 'w':
1507 			cfg_wait = atoi(optarg)*1000000;
1508 			break;
1509 		case 'M':
1510 			cfg_mark = strtol(optarg, NULL, 0);
1511 			break;
1512 		case 'P':
1513 			cfg_peek = parse_peek(optarg);
1514 			break;
1515 		case 'c':
1516 			parse_cmsg_types(optarg);
1517 			break;
1518 		case 'o':
1519 			parse_setsock_options(optarg);
1520 			break;
1521 		}
1522 	}
1523 
1524 	if (optind + 1 != argc)
1525 		die_usage();
1526 	cfg_host = argv[optind];
1527 
1528 	if (strchr(cfg_host, ':'))
1529 		pf = AF_INET6;
1530 }
1531 
main(int argc,char * argv[])1532 int main(int argc, char *argv[])
1533 {
1534 	init_rng();
1535 
1536 	signal(SIGUSR1, handle_signal);
1537 	parse_opts(argc, argv);
1538 
1539 	if (listen_mode) {
1540 		int fd = sock_listen_mptcp(cfg_host, cfg_port);
1541 
1542 		if (fd < 0)
1543 			return 1;
1544 
1545 		if (cfg_rcvbuf)
1546 			set_rcvbuf(fd, cfg_rcvbuf);
1547 		if (cfg_sndbuf)
1548 			set_sndbuf(fd, cfg_sndbuf);
1549 		if (cfg_mark)
1550 			set_mark(fd, cfg_mark);
1551 		if (cfg_cmsg_types.cmsg_enabled)
1552 			apply_cmsg_types(fd, &cfg_cmsg_types);
1553 
1554 		return main_loop_s(fd);
1555 	}
1556 
1557 	return main_loop();
1558 }
1559