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